clif.cpp 752 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // Copyright (c) Hercules Dev Team - Licensed under GNU GPL
  3. // For more information, see LICENCE in the main folder
  4. #include "clif.hpp"
  5. #include <cstdarg>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <ctime>
  10. #include <unordered_set>
  11. #include <common/cbasetypes.hpp>
  12. #include <common/conf.hpp>
  13. #include <common/ers.hpp>
  14. #include <common/grfio.hpp>
  15. #include <common/malloc.hpp>
  16. #include <common/nullpo.hpp>
  17. #include <common/random.hpp>
  18. #include <common/showmsg.hpp>
  19. #include <common/socket.hpp>
  20. #include <common/strlib.hpp>
  21. #include <common/timer.hpp>
  22. #include <common/utilities.hpp>
  23. #include <common/utils.hpp>
  24. #include "achievement.hpp"
  25. #include "atcommand.hpp"
  26. #include "battle.hpp"
  27. #include "battleground.hpp"
  28. #include "cashshop.hpp"
  29. #include "channel.hpp"
  30. #include "chat.hpp"
  31. #include "chrif.hpp"
  32. #include "clan.hpp"
  33. #include "clif.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"
  40. #include "log.hpp"
  41. #include "mail.hpp"
  42. #include "map.hpp"
  43. #include "mercenary.hpp"
  44. #include "mob.hpp"
  45. #include "npc.hpp"
  46. #include "party.hpp"
  47. #include "pc.hpp"
  48. #include "pc_groups.hpp"
  49. #include "pet.hpp"
  50. #include "quest.hpp"
  51. #include "script.hpp"
  52. #include "skill.hpp"
  53. #include "status.hpp"
  54. #include "storage.hpp"
  55. #include "unit.hpp"
  56. #include "vending.hpp"
  57. using namespace rathena;
  58. static inline uint32 client_tick( t_tick tick ){
  59. return (uint32)tick;
  60. }
  61. #if PACKETVER >= 20170830
  62. static inline int64 client_exp(t_exp exp) {
  63. return (int64)u64min(exp, MAX_EXP);
  64. }
  65. #else
  66. static inline int32 client_exp(t_exp exp) {
  67. return (int32)u64min(exp, MAX_EXP);
  68. }
  69. #endif
  70. /* for clif_clearunit_delayed */
  71. static struct eri *delay_clearunit_ers;
  72. struct s_packet_db packet_db[MAX_PACKET_DB + 1];
  73. unsigned long color_table[COLOR_MAX];
  74. #include "clif_obfuscation.hpp"
  75. static bool clif_session_isValid(map_session_data *sd);
  76. static void clif_loadConfirm( map_session_data *sd );
  77. static void clif_favorite_item( map_session_data& sd, uint16 index );
  78. #if PACKETVER >= 20150513
  79. enum mail_type {
  80. MAIL_TYPE_TEXT = 0x0,
  81. MAIL_TYPE_ZENY = 0x2,
  82. MAIL_TYPE_ITEM = 0x4,
  83. MAIL_TYPE_NPC = 0x8
  84. };
  85. #endif
  86. enum e_inventory_type{
  87. INVTYPE_INVENTORY = 0,
  88. INVTYPE_CART = 1,
  89. INVTYPE_STORAGE = 2,
  90. INVTYPE_GUILD_STORAGE = 3,
  91. };
  92. /** Converts item type to display it on client if necessary.
  93. * @param nameid: Item ID
  94. * @return item type. For IT_PETEGG will be displayed as IT_ARMOR. If Shadow Weapon of IT_SHADOWGEAR as IT_WEAPON and else as IT_ARMOR
  95. */
  96. static inline int32 itemtype(t_itemid nameid) {
  97. struct item_data* id = itemdb_search(nameid); //Use itemdb_search, so non-existance item will use dummy data and won't crash the server. bugreport:8468
  98. int32 type = id->type;
  99. if( type == IT_SHADOWGEAR ) {
  100. if( id->equip&EQP_SHADOW_WEAPON )
  101. return IT_WEAPON;
  102. else
  103. return IT_ARMOR;
  104. }
  105. return ( type == IT_PETEGG ) ? IT_ARMOR : type;
  106. }
  107. // TODO: doc
  108. static inline uint16 client_index( uint16 server_index ){
  109. return server_index + 2;
  110. }
  111. static inline uint16 server_index( uint16 client_index ){
  112. return client_index - 2;
  113. }
  114. static inline uint16 client_storage_index( uint16 server_index ){
  115. return server_index + 1;
  116. }
  117. static inline uint16 server_storage_index( uint16 client_index ){
  118. return client_index - 1;
  119. }
  120. static inline uint32 disguised_bl_id( uint32 bl_id ){
  121. // Casting to prevent a compiler warning
  122. return -((int32)bl_id);
  123. }
  124. #if PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  125. static inline uint32 client_nameid( t_itemid server_nameid ){
  126. t_itemid view = itemdb_viewid( server_nameid );
  127. if( view > 0 ){
  128. return view;
  129. }else{
  130. return server_nameid;
  131. }
  132. }
  133. #else
  134. static inline uint16 client_nameid( t_itemid server_nameid ){
  135. t_itemid view = itemdb_viewid( server_nameid );
  136. if( view > 0 ){
  137. if( view > UINT16_MAX ){
  138. return (uint16)UNKNOWN_ITEM_ID;
  139. }
  140. return (uint16)view;
  141. }else{
  142. if( server_nameid > UINT16_MAX ){
  143. return (uint16)UNKNOWN_ITEM_ID;
  144. }
  145. return (uint16)server_nameid;
  146. }
  147. }
  148. #endif
  149. static inline void WBUFPOS(uint8* p, uint16 pos, int16 x, int16 y, unsigned char dir) {
  150. p += pos;
  151. p[0] = (uint8)(x>>2);
  152. p[1] = (uint8)((x<<6) | ((y>>4)&0x3f));
  153. p[2] = (uint8)((y<<4) | (dir&0xf));
  154. }
  155. // client-side: x0+=sx0*0.0625-0.5 and y0+=sy0*0.0625-0.5
  156. static inline void WBUFPOS2(uint8* p, uint16 pos, int16 x0, int16 y0, int16 x1, int16 y1, uint8 sx0, uint8 sy0) {
  157. p += pos;
  158. p[0] = (uint8)(x0>>2);
  159. p[1] = (uint8)((x0<<6) | ((y0>>4)&0x3f));
  160. p[2] = (uint8)((y0<<4) | ((x1>>6)&0x0f));
  161. p[3] = (uint8)((x1<<2) | ((y1>>8)&0x03));
  162. p[4] = (uint8)y1;
  163. p[5] = (uint8)((sx0<<4) | (sy0&0x0f));
  164. }
  165. static inline void WFIFOPOS(int32 fd, uint16 pos, int16 x, int16 y, unsigned char dir) {
  166. WBUFPOS(WFIFOP(fd,pos), 0, x, y, dir);
  167. }
  168. static inline void RBUFPOS(const uint8* p, uint16 pos, int16* x, int16* y, unsigned char* dir) {
  169. p += pos;
  170. if( x ) {
  171. x[0] = ( ( p[0] & 0xff ) << 2 ) | ( p[1] >> 6 );
  172. }
  173. if( y ) {
  174. y[0] = ( ( p[1] & 0x3f ) << 4 ) | ( p[2] >> 4 );
  175. }
  176. if( dir ) {
  177. dir[0] = ( p[2] & 0x0f );
  178. }
  179. }
  180. static inline void RBUFPOS2(const uint8* p, uint16 pos, int16* x0, int16* y0, int16* x1, int16* y1, unsigned char* sx0, unsigned char* sy0) {
  181. p += pos;
  182. if( x0 ) {
  183. x0[0] = ( ( p[0] & 0xff ) << 2 ) | ( p[1] >> 6 );
  184. }
  185. if( y0 ) {
  186. y0[0] = ( ( p[1] & 0x3f ) << 4 ) | ( p[2] >> 4 );
  187. }
  188. if( x1 ) {
  189. x1[0] = ( ( p[2] & 0x0f ) << 6 ) | ( p[3] >> 2 );
  190. }
  191. if( y1 ) {
  192. y1[0] = ( ( p[3] & 0x03 ) << 8 ) | ( p[4] >> 0 );
  193. }
  194. if( sx0 ) {
  195. sx0[0] = ( p[5] & 0xf0 ) >> 4;
  196. }
  197. if( sy0 ) {
  198. sy0[0] = ( p[5] & 0x0f ) >> 0;
  199. }
  200. }
  201. static inline void RFIFOPOS(int32 fd, uint16 pos, int16* x, int16* y, unsigned char* dir) {
  202. RBUFPOS(RFIFOP(fd,pos), 0, x, y, dir);
  203. }
  204. static inline void RFIFOPOS2(int32 fd, uint16 pos, int16* x0, int16* y0, int16* x1, int16* y1, unsigned char* sx0, unsigned char* sy0) {
  205. RBUFPOS2(WFIFOP(fd,pos), 0, x0, y0, x1, y1, sx0, sy0);
  206. }
  207. //To idenfity disguised characters.
  208. static inline bool disguised(struct block_list* bl) {
  209. return (bool)( bl->type == BL_PC && ((TBL_PC*)bl)->disguise );
  210. }
  211. //Guarantees that the given string does not exceeds the allowed size, as well as making sure it's null terminated. [Skotlex]
  212. static inline uint32 mes_len_check(char* mes, uint32 len, uint32 max) {
  213. if( len > max )
  214. len = max;
  215. mes[len-1] = '\0';
  216. return len;
  217. }
  218. static char map_ip_str[128];
  219. static uint32 map_ip;
  220. static uint32 bind_ip = INADDR_ANY;
  221. static uint16 map_port = 5121;
  222. static bool clif_ally_only = false;
  223. int32 map_fd;
  224. static int32 clif_parse (int32 fd);
  225. /*==========================================
  226. * Ip setting of map-server
  227. *------------------------------------------*/
  228. int32 clif_setip(const char* ip) {
  229. char ip_str[16];
  230. map_ip = host2ip(ip);
  231. if (!map_ip) {
  232. ShowWarning("Failed to Resolve Map Server Address! (%s)\n", ip);
  233. return 0;
  234. }
  235. safestrncpy(map_ip_str, ip, sizeof(map_ip_str));
  236. ShowInfo("Map Server IP Address : '" CL_WHITE "%s" CL_RESET "' -> '" CL_WHITE "%s" CL_RESET "'.\n", ip, ip2str(map_ip, ip_str));
  237. return 1;
  238. }
  239. void clif_setbindip(const char* ip)
  240. {
  241. bind_ip = host2ip(ip);
  242. if (bind_ip) {
  243. char ip_str[16];
  244. ShowInfo("Map Server Bind IP Address : '" CL_WHITE "%s" CL_RESET "' -> '" CL_WHITE "%s" CL_RESET "'.\n", ip, ip2str(bind_ip, ip_str));
  245. } else {
  246. ShowWarning("Failed to Resolve Map Server Address! (%s)\n", ip);
  247. }
  248. }
  249. /*==========================================
  250. * Sets map port to 'port'
  251. * is run from map.cpp upon loading map server configuration
  252. *------------------------------------------*/
  253. void clif_setport(uint16 port)
  254. {
  255. map_port = port;
  256. }
  257. /*==========================================
  258. * Returns map server IP
  259. *------------------------------------------*/
  260. uint32 clif_getip(void)
  261. {
  262. return map_ip;
  263. }
  264. //Refreshes map_server ip, returns the new ip if the ip changed, otherwise it returns 0.
  265. uint32 clif_refresh_ip(void)
  266. {
  267. uint32 new_ip;
  268. new_ip = host2ip(map_ip_str);
  269. if (new_ip && new_ip != map_ip) {
  270. map_ip = new_ip;
  271. ShowInfo("Updating IP resolution of [%s].\n", map_ip_str);
  272. return map_ip;
  273. }
  274. return 0;
  275. }
  276. /*==========================================
  277. * Returns map port which is set by clif_setport()
  278. *------------------------------------------*/
  279. uint16 clif_getport(void)
  280. {
  281. return map_port;
  282. }
  283. #if PACKETVER >= 20071106
  284. static inline unsigned char clif_bl_type(struct block_list *bl, bool walking) {
  285. switch (bl->type) {
  286. case BL_PC: return (disguised(bl) && !pcdb_checkid(status_get_viewdata(bl)->class_))? 0x1:0x0; //PC_TYPE
  287. case BL_ITEM: return 0x2; //ITEM_TYPE
  288. case BL_SKILL: return 0x3; //SKILL_TYPE
  289. case BL_CHAT: return 0x4; //UNKNOWN_TYPE
  290. case BL_MOB:
  291. if( pcdb_checkid( status_get_viewdata( bl )->class_ ) ){
  292. return 0x0; //PC_TYPE
  293. }else{
  294. switch( ( (mob_data*)bl )->special_state.ai ){
  295. case AI_ABR:
  296. return 0xd; //NPC_ABR_TYPE
  297. case AI_BIONIC:
  298. return 0xe; //NPC_BIONIC_TYPE
  299. default:
  300. return 0x5; //NPC_MOB_TYPE
  301. }
  302. }
  303. case BL_NPC:
  304. // From 2017-07-26 on NPC type units can also use player sprites.
  305. // There is one exception and this is if they are walking.
  306. // Since walking NPCs are not supported on official servers, the client does not know how to handle it.
  307. #if PACKETVER >= 20170726
  308. if (pcdb_checkid( status_get_viewdata( bl )->class_ ) && walking)
  309. return 0x0;
  310. else if (mobdb_checkid( status_get_viewdata( bl )->class_ )) // FIXME: categorize NPCs able to walk
  311. return 0xC;
  312. else
  313. return 0x6;
  314. #else
  315. return pcdb_checkid(status_get_viewdata(bl)->class_) ? 0x0 : 0x6; //NPC_EVT_TYPE
  316. #endif
  317. case BL_PET: return pcdb_checkid(status_get_viewdata(bl)->class_)?0x0:0x7; //NPC_PET_TYPE
  318. case BL_HOM: return 0x8; //NPC_HOM_TYPE
  319. case BL_MER: return 0x9; //NPC_MERSOL_TYPE
  320. case BL_ELEM: return 0xa; //NPC_ELEMENTAL_TYPE
  321. default: return 0x1; //NPC_TYPE
  322. }
  323. }
  324. #endif
  325. static bool clif_session_isValid(map_session_data *sd) {
  326. return ( sd != nullptr && session_isActive(sd->fd) );
  327. }
  328. /*==========================================
  329. * sub process of clif_send
  330. * Called from a map_foreachinallarea (grabs all players in specific area and subjects them to this function)
  331. * In order to send area-wise packets, such as:
  332. * - AREA : everyone nearby your area
  333. * - AREA_WOSC (AREA WITHOUT SAME CHAT) : Not run for people in the same chat as yours
  334. * - AREA_WOC (AREA WITHOUT CHAT) : Not run for people inside a chat
  335. * - AREA_WOS (AREA WITHOUT SELF) : Not run for self
  336. * - AREA_CHAT_WOC : Everyone in the area of your chat without a chat
  337. *------------------------------------------*/
  338. static int32 clif_send_sub(struct block_list *bl, va_list ap)
  339. {
  340. struct block_list *src_bl;
  341. map_session_data *sd;
  342. unsigned char *buf;
  343. int32 len, type, fd;
  344. nullpo_ret(bl);
  345. nullpo_ret(sd = (map_session_data *)bl);
  346. // Don't send to disconnected clients.
  347. if( !session_isActive( fd = sd->fd ) ){
  348. return 0;
  349. }
  350. buf = va_arg(ap,unsigned char*);
  351. len = va_arg(ap,int32);
  352. nullpo_ret(src_bl = va_arg(ap,struct block_list*));
  353. type = va_arg(ap,int32);
  354. switch(type) {
  355. case AREA_WOS:
  356. if (bl == src_bl)
  357. return 0;
  358. break;
  359. case AREA_WOC:
  360. if (sd->chatID || bl == src_bl)
  361. return 0;
  362. break;
  363. case AREA_WOSC:
  364. {
  365. if(src_bl->type == BL_PC) {
  366. map_session_data *ssd = (map_session_data *)src_bl;
  367. if (ssd && sd->chatID && (sd->chatID == ssd->chatID))
  368. return 0;
  369. }
  370. else if(src_bl->type == BL_NPC) {
  371. struct npc_data *nd = (struct npc_data *)src_bl;
  372. if (nd && sd->chatID && (sd->chatID == nd->chat_id))
  373. return 0;
  374. }
  375. }
  376. break;
  377. }
  378. if( src_bl->type == BL_NPC && npc_is_hidden_dynamicnpc( *( (struct npc_data*)src_bl ), *sd ) ){
  379. // Do not send anything
  380. return 0;
  381. }
  382. /* unless visible, hold it here */
  383. if (!battle_config.update_enemy_position && clif_ally_only && !sd->special_state.intravision &&
  384. !sd->sc.getSCE(SC_INTRAVISION) && battle_check_target(src_bl,&sd->bl,BCT_ENEMY) > 0)
  385. return 0;
  386. WFIFOHEAD(fd, len);
  387. if (WFIFOP(fd,0) == buf) {
  388. ShowError("WARNING: Invalid use of clif_send function\n");
  389. ShowError(" Packet x%4x use a WFIFO of a player instead of to use a buffer.\n", WBUFW(buf,0));
  390. ShowError(" Please correct your code.\n");
  391. // don't send to not move the pointer of the packet for next sessions in the loop
  392. //WFIFOSET(fd,0);//## TODO is this ok?
  393. //NO. It is not ok. There is the chance WFIFOSET actually sends the buffer data, and shifts elements around, which will corrupt the buffer.
  394. return 0;
  395. }
  396. memcpy(WFIFOP(fd,0), buf, len);
  397. WFIFOSET(fd,len);
  398. return 0;
  399. }
  400. /*==========================================
  401. * Packet Delegation (called on all packets that require data to be sent to more than one client)
  402. * functions that are sent solely to one use whose ID it posses use WFIFOSET
  403. *------------------------------------------*/
  404. int32 clif_send(const void* buf, int32 len, struct block_list* bl, enum send_target type)
  405. {
  406. int32 i;
  407. map_session_data *sd, *tsd;
  408. struct party_data *p = nullptr;
  409. std::shared_ptr<s_battleground_data> bg;
  410. int32 x0 = 0, x1 = 0, y0 = 0, y1 = 0, fd;
  411. struct s_mapiterator* iter;
  412. if( type != ALL_CLIENT )
  413. nullpo_ret(bl);
  414. sd = BL_CAST(BL_PC, bl);
  415. switch(type) {
  416. case ALL_CLIENT: //All player clients.
  417. iter = mapit_getallusers();
  418. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  419. if( session_isActive( fd = tsd->fd ) ){
  420. WFIFOHEAD( fd, len );
  421. memcpy( WFIFOP( fd, 0 ), buf, len );
  422. WFIFOSET( fd, len );
  423. }
  424. }
  425. mapit_free(iter);
  426. break;
  427. case ALL_SAMEMAP: //All players on the same map
  428. iter = mapit_getallusers();
  429. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  430. if( bl->m == tsd->bl.m && session_isActive( fd = tsd->fd ) ){
  431. WFIFOHEAD( fd, len );
  432. memcpy( WFIFOP( fd, 0 ), buf, len );
  433. WFIFOSET( fd, len );
  434. }
  435. }
  436. mapit_free(iter);
  437. break;
  438. case AREA:
  439. case AREA_WOSC:
  440. if (sd && bl->prev == nullptr) //Otherwise source misses the packet.[Skotlex]
  441. clif_send (buf, len, bl, SELF);
  442. [[fallthrough]];
  443. case AREA_WOC:
  444. case AREA_WOS:
  445. map_foreachinallarea(clif_send_sub, bl->m, bl->x-AREA_SIZE, bl->y-AREA_SIZE, bl->x+AREA_SIZE, bl->y+AREA_SIZE,
  446. BL_PC, buf, len, bl, type);
  447. break;
  448. case AREA_CHAT_WOC:
  449. map_foreachinallarea(clif_send_sub, bl->m, bl->x-(AREA_SIZE-5), bl->y-(AREA_SIZE-5),
  450. bl->x+(AREA_SIZE-5), bl->y+(AREA_SIZE-5), BL_PC, buf, len, bl, AREA_WOC);
  451. break;
  452. case CHAT:
  453. case CHAT_WOS:
  454. {
  455. struct chat_data *cd;
  456. if (sd) {
  457. cd = (struct chat_data*)map_id2bl(sd->chatID);
  458. } else if (bl->type == BL_CHAT) {
  459. cd = (struct chat_data*)bl;
  460. } else break;
  461. if (cd == nullptr)
  462. break;
  463. for(i = 0; i < cd->users; i++) {
  464. if (type == CHAT_WOS && cd->usersd[i] == sd)
  465. continue;
  466. if( session_isActive( fd = cd->usersd[i]->fd ) ){
  467. WFIFOHEAD(fd,len);
  468. memcpy(WFIFOP(fd,0), buf, len);
  469. WFIFOSET(fd,len);
  470. }
  471. }
  472. }
  473. break;
  474. case PARTY_AREA:
  475. case PARTY_AREA_WOS:
  476. x0 = bl->x - AREA_SIZE;
  477. y0 = bl->y - AREA_SIZE;
  478. x1 = bl->x + AREA_SIZE;
  479. y1 = bl->y + AREA_SIZE;
  480. [[fallthrough]];
  481. case PARTY:
  482. case PARTY_WOS:
  483. case PARTY_SAMEMAP:
  484. case PARTY_SAMEMAP_WOS:
  485. if (sd && sd->status.party_id)
  486. p = party_search(sd->status.party_id);
  487. if (p) {
  488. for(i=0;i<MAX_PARTY;i++){
  489. if( (sd = p->data[i].sd) == nullptr )
  490. continue;
  491. if( !session_isActive( fd = sd->fd ) )
  492. continue;
  493. if( sd->bl.id == bl->id && (type == PARTY_WOS || type == PARTY_SAMEMAP_WOS || type == PARTY_AREA_WOS) )
  494. continue;
  495. if( type != PARTY && type != PARTY_WOS && bl->m != sd->bl.m )
  496. continue;
  497. if( (type == PARTY_AREA || type == PARTY_AREA_WOS) && (sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1) )
  498. continue;
  499. WFIFOHEAD(fd, len);
  500. memcpy(WFIFOP(fd, 0), buf, len);
  501. WFIFOSET(fd, len);
  502. }
  503. if (!enable_spy) //Skip unnecessary parsing. [Skotlex]
  504. break;
  505. iter = mapit_getallusers();
  506. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  507. if( tsd->partyspy == p->party.party_id && session_isActive( fd = tsd->fd ) ){
  508. WFIFOHEAD( fd, len );
  509. memcpy( WFIFOP( fd, 0 ), buf, len );
  510. WFIFOSET( tsd->fd, len );
  511. }
  512. }
  513. mapit_free(iter);
  514. }
  515. break;
  516. case DUEL:
  517. case DUEL_WOS:
  518. if (!sd || !sd->duel_group) break; //Invalid usage.
  519. iter = mapit_getallusers();
  520. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  521. if( type == DUEL_WOS && bl->id == tsd->bl.id )
  522. continue;
  523. if( sd->duel_group == tsd->duel_group && session_isActive( fd = tsd->fd ) ){
  524. WFIFOHEAD( fd, len );
  525. memcpy( WFIFOP( fd, 0 ), buf, len );
  526. WFIFOSET( fd, len );
  527. }
  528. }
  529. mapit_free(iter);
  530. break;
  531. case SELF:
  532. if( clif_session_isValid(sd) ){
  533. fd = sd->fd;
  534. WFIFOHEAD(fd,len);
  535. memcpy(WFIFOP(fd,0), buf, len);
  536. WFIFOSET(fd,len);
  537. }
  538. break;
  539. // New definitions for guilds [Valaris] - Cleaned up and reorganized by [Skotlex]
  540. case GUILD_AREA:
  541. case GUILD_AREA_WOS:
  542. x0 = bl->x - AREA_SIZE;
  543. y0 = bl->y - AREA_SIZE;
  544. x1 = bl->x + AREA_SIZE;
  545. y1 = bl->y + AREA_SIZE;
  546. [[fallthrough]];
  547. case GUILD_SAMEMAP:
  548. case GUILD_SAMEMAP_WOS:
  549. case GUILD:
  550. case GUILD_WOS:
  551. case GUILD_NOBG: {
  552. if (!sd || !sd->status.guild_id || !sd->guild)
  553. break;
  554. const auto &g = sd->guild->guild;
  555. for(i = 0; i < g.max_member; i++) {
  556. if( (sd = g.member[i].sd) != nullptr ){
  557. if( !session_isActive( fd = sd->fd ) )
  558. continue;
  559. if( type == GUILD_NOBG && sd->bg_id )
  560. continue;
  561. if( sd->bl.id == bl->id && (type == GUILD_WOS || type == GUILD_SAMEMAP_WOS || type == GUILD_AREA_WOS) )
  562. continue;
  563. if( type != GUILD && type != GUILD_NOBG && type != GUILD_WOS && sd->bl.m != bl->m )
  564. continue;
  565. if( (type == GUILD_AREA || type == GUILD_AREA_WOS) && (sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1) )
  566. continue;
  567. WFIFOHEAD(fd,len);
  568. memcpy(WFIFOP(fd,0), buf, len);
  569. WFIFOSET(fd,len);
  570. }
  571. }
  572. if (!enable_spy) //Skip unnecessary parsing. [Skotlex]
  573. break;
  574. iter = mapit_getallusers();
  575. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  576. if( tsd->guildspy == g.guild_id && session_isActive( fd = tsd->fd ) ){
  577. WFIFOHEAD( fd, len );
  578. memcpy( WFIFOP( fd, 0 ), buf, len );
  579. WFIFOSET( fd, len );
  580. }
  581. }
  582. mapit_free(iter);
  583. break;
  584. }
  585. case BG_AREA:
  586. case BG_AREA_WOS:
  587. x0 = bl->x - AREA_SIZE;
  588. y0 = bl->y - AREA_SIZE;
  589. x1 = bl->x + AREA_SIZE;
  590. y1 = bl->y + AREA_SIZE;
  591. [[fallthrough]];
  592. case BG_SAMEMAP:
  593. case BG_SAMEMAP_WOS:
  594. case BG:
  595. case BG_WOS:
  596. if( sd && sd->bg_id > 0 && (bg = util::umap_find(bg_team_db, sd->bg_id)))
  597. {
  598. for (const auto &member : bg->members) {
  599. if( ( sd = member.sd ) == nullptr || !session_isActive( fd = sd->fd ) )
  600. continue;
  601. if(sd->bl.id == bl->id && (type == BG_WOS || type == BG_SAMEMAP_WOS || type == BG_AREA_WOS) )
  602. continue;
  603. if( type != BG && type != BG_WOS && sd->bl.m != bl->m )
  604. continue;
  605. if( (type == BG_AREA || type == BG_AREA_WOS) && (sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1) )
  606. continue;
  607. WFIFOHEAD(fd,len);
  608. memcpy(WFIFOP(fd,0), buf, len);
  609. WFIFOSET(fd,len);
  610. }
  611. }
  612. break;
  613. case CLAN:
  614. if( sd && sd->clan ){
  615. struct clan* clan = sd->clan;
  616. for( i = 0; i < clan->max_member; i++ ){
  617. if( ( sd = clan->members[i] ) == nullptr || !session_isActive( fd = sd->fd ) ){
  618. continue;
  619. }
  620. WFIFOHEAD(fd,len);
  621. memcpy(WFIFOP(fd,0), buf, len);
  622. WFIFOSET(fd,len);
  623. }
  624. if (!enable_spy) //Skip unnecessary parsing. [Skotlex]
  625. break;
  626. iter = mapit_getallusers();
  627. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  628. if( tsd->clanspy == clan->id && session_isActive( fd = tsd->fd ) ){
  629. WFIFOHEAD(fd, len);
  630. memcpy(WFIFOP(fd, 0), buf, len);
  631. WFIFOSET(fd, len);
  632. }
  633. }
  634. mapit_free(iter);
  635. }
  636. break;
  637. default:
  638. ShowError("clif_send: Unrecognized type %d\n",type);
  639. return -1;
  640. }
  641. return 0;
  642. }
  643. /// Notifies the client, that it's connection attempt was accepted.
  644. /// 0073 <start time>.L <position>.3B <x size>.B <y size>.B (ZC_ACCEPT_ENTER)
  645. /// 02eb <start time>.L <position>.3B <x size>.B <y size>.B <font>.W (ZC_ACCEPT_ENTER2)
  646. /// 0a18 <start time>.L <position>.3B <x size>.B <y size>.B <font>.W <sex>.B (ZC_ACCEPT_ENTER3)
  647. void clif_authok(map_session_data *sd)
  648. {
  649. PACKET_ZC_ACCEPT_ENTER packet{};
  650. packet.packetType = HEADER_ZC_ACCEPT_ENTER;
  651. packet.startTime = client_tick(gettick());
  652. WBUFPOS(packet.posDir, 0, sd->bl.x, sd->bl.y, sd->ud.dir);
  653. packet.xSize = 5; // ignored
  654. packet.ySize = 5; // ignored
  655. #if PACKETVER >= 20080102
  656. packet.font = sd->status.font;
  657. #endif
  658. #if PACKETVER >= 20141016 && PACKETVER < 20160330
  659. packet.sex = sd->status.sex;
  660. #endif
  661. clif_send(&packet, sizeof(packet), &sd->bl, SELF);
  662. }
  663. /// Notifies the client, that it's connection attempt was refused.
  664. /// 0074 <error code>.B (ZC_REFUSE_ENTER)
  665. /// error code:
  666. /// 0 = client type mismatch
  667. /// 1 = ID mismatch
  668. /// 2 = mobile - out of available time
  669. /// 3 = mobile - already logged in
  670. /// 4 = mobile - waiting state
  671. void clif_authrefuse(int32 fd, uint8 error_code)
  672. {
  673. PACKET_ZC_REFUSE_ENTER packet{};
  674. packet.packetType = HEADER_ZC_REFUSE_ENTER;
  675. packet.errorCode = error_code;
  676. socket_send<PACKET_ZC_REFUSE_ENTER>(fd, packet);
  677. }
  678. /// Notifies the client of a ban or forced disconnect.
  679. /// 0081 <error code>.B (SC_NOTIFY_BAN)
  680. /// error code:
  681. /// 0 = BAN_UNFAIR
  682. /// 1 = server closed -> MsgStringTable[4]
  683. /// 2 = ID already logged in -> MsgStringTable[5]
  684. /// 3 = timeout/too much lag -> MsgStringTable[241]
  685. /// 4 = server full -> MsgStringTable[264]
  686. /// 5 = underaged -> MsgStringTable[305]
  687. /// 8 = Server sill recognizes last connection -> MsgStringTable[441]
  688. /// 9 = too many connections from this ip -> MsgStringTable[529]
  689. /// 10 = out of available time paid for -> MsgStringTable[530]
  690. /// 11 = BAN_PAY_SUSPEND
  691. /// 12 = BAN_PAY_CHANGE
  692. /// 13 = BAN_PAY_WRONGIP
  693. /// 14 = BAN_PAY_PNGAMEROOM
  694. /// 15 = disconnected by a GM -> if( servicetype == taiwan ) MsgStringTable[579]
  695. /// 16 = BAN_JAPAN_REFUSE1
  696. /// 17 = BAN_JAPAN_REFUSE2
  697. /// 18 = BAN_INFORMATION_REMAINED_ANOTHER_ACCOUNT
  698. /// 100 = BAN_PC_IP_UNFAIR
  699. /// 101 = BAN_PC_IP_COUNT_ALL
  700. /// 102 = BAN_PC_IP_COUNT
  701. /// 103 = BAN_GRAVITY_MEM_AGREE
  702. /// 104 = BAN_GAME_MEM_AGREE
  703. /// 105 = BAN_HAN_VALID
  704. /// 106 = BAN_PC_IP_LIMIT_ACCESS
  705. /// 107 = BAN_OVER_CHARACTER_LIST
  706. /// 108 = BAN_IP_BLOCK
  707. /// 109 = BAN_INVALID_PWD_CNT
  708. /// 110 = BAN_NOT_ALLOWED_JOBCLASS
  709. /// ? = disconnected -> MsgStringTable[3]
  710. void clif_authfail_fd(int32 fd, int32 type)
  711. {
  712. if (!session_isValid(fd) || session[fd]->func_parse != clif_parse) //clif_authfail should only be invoked on players!
  713. return;
  714. PACKET_SC_NOTIFY_BAN packet{};
  715. packet.packetType = HEADER_SC_NOTIFY_BAN;
  716. packet.errorCode = static_cast<uint8>(type);
  717. socket_send<PACKET_SC_NOTIFY_BAN>(fd, packet);
  718. set_eof(fd);
  719. }
  720. /// Notifies the client, whether it can disconnect and change servers.
  721. /// 00b3 <type>.B (ZC_RESTART_ACK)
  722. /// type:
  723. /// 1 = disconnect, char-select
  724. /// ? = nothing
  725. void clif_charselectok(int32 id, uint8 ok)
  726. {
  727. map_session_data* sd = map_id2sd(id);
  728. if (sd == nullptr)
  729. return;
  730. PACKET_ZC_RESTART_ACK packet{};
  731. packet.packetType = HEADER_ZC_RESTART_ACK;
  732. packet.type = ok;
  733. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  734. }
  735. /// Makes an item appear on the ground.
  736. /// 009E <id>.L <name id>.W <identified>.B <x>.W <y>.W <subX>.B <subY>.B <amount>.W (ZC_ITEM_FALL_ENTRY)
  737. /// 084B <id>.L <name id>.W <type>.W <identified>.B <x>.W <y>.W <subX>.B <subY>.B <amount>.W (ZC_ITEM_FALL_ENTRY4)
  738. /// 0ADD <id>.L <name id>.W <type>.W <identified>.B <x>.W <y>.W <subX>.B <subY>.B <amount>.W <show drop effect>.B <drop effect mode>.W (ZC_ITEM_FALL_ENTRY5)
  739. void clif_dropflooritem( struct flooritem_data* fitem, bool canShowEffect ){
  740. nullpo_retv(fitem);
  741. if( fitem->item.nameid == 0 ){
  742. return;
  743. }
  744. struct packet_dropflooritem p;
  745. p.PacketType = dropflooritemType;
  746. p.ITAID = fitem->bl.id;
  747. p.ITID = client_nameid( fitem->item.nameid );
  748. #if PACKETVER >= 20130000 /* not sure date */
  749. p.type = itemtype( fitem->item.nameid );
  750. #endif
  751. p.IsIdentified = fitem->item.identify ? 1 : 0;
  752. p.xPos = fitem->bl.x;
  753. p.yPos = fitem->bl.y;
  754. p.subX = fitem->subx;
  755. p.subY = fitem->suby;
  756. p.count = fitem->item.amount;
  757. #if defined(PACKETVER_ZERO) || PACKETVER >= 20180418
  758. if( canShowEffect ){
  759. uint8 dropEffect = itemdb_dropeffect( fitem->item.nameid );
  760. if( dropEffect > 0 ){
  761. p.showdropeffect = 1;
  762. p.dropeffectmode = dropEffect - 1;
  763. }else if (battle_config.rndopt_drop_pillar != 0){
  764. uint8 optionCount = 0;
  765. for (uint8 i = 0; i < MAX_ITEM_RDM_OPT; i++) {
  766. if (fitem->item.option[i].id != 0) {
  767. optionCount++;
  768. }
  769. }
  770. if (optionCount > 0) {
  771. p.showdropeffect = 1;
  772. if (optionCount == 1)
  773. p.dropeffectmode = DROPEFFECT_BLUE_PILLAR - 1;
  774. else if (optionCount == 2)
  775. p.dropeffectmode = DROPEFFECT_YELLOW_PILLAR - 1;
  776. else
  777. p.dropeffectmode = DROPEFFECT_PURPLE_PILLAR - 1;
  778. } else {
  779. p.showdropeffect = 0;
  780. p.dropeffectmode = DROPEFFECT_NONE;
  781. }
  782. } else {
  783. p.showdropeffect = 0;
  784. p.dropeffectmode = DROPEFFECT_NONE;
  785. }
  786. }else{
  787. p.showdropeffect = 0;
  788. p.dropeffectmode = DROPEFFECT_NONE;
  789. }
  790. #endif
  791. clif_send( &p, sizeof(p), &fitem->bl, AREA );
  792. }
  793. /// Makes an item disappear from the ground.
  794. /// 00a1 <id>.L (ZC_ITEM_DISAPPEAR)
  795. void clif_clearflooritem( flooritem_data& fitem, map_session_data* tsd ){
  796. PACKET_ZC_ITEM_DISAPPEAR packet{};
  797. packet.packetType = HEADER_ZC_ITEM_DISAPPEAR;
  798. packet.itemAid = fitem.bl.id;
  799. if( tsd == nullptr ){
  800. clif_send(&packet, sizeof(PACKET_ZC_ITEM_DISAPPEAR), &fitem.bl, AREA);
  801. } else {
  802. clif_send(&packet, sizeof(PACKET_ZC_ITEM_DISAPPEAR), &tsd->bl, SELF );
  803. }
  804. }
  805. /// Makes a unit (char, npc, mob, homun) disappear to one client.
  806. /// 0080 <id>.L <type>.B (ZC_NOTIFY_VANISH)
  807. /// type:
  808. /// 0 = out of sight
  809. /// 1 = died
  810. /// 2 = logged out
  811. /// 3 = teleport
  812. /// 4 = trickdead
  813. void clif_clearunit_single( uint32 GID, clr_type type, map_session_data& tsd ){
  814. PACKET_ZC_NOTIFY_VANISH packet{};
  815. packet.packetType = HEADER_ZC_NOTIFY_VANISH;
  816. packet.gid = GID;
  817. packet.type = static_cast<decltype(packet.type)>(type);
  818. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  819. }
  820. /// Makes a unit (char, npc, mob, homun) disappear to all clients in area.
  821. /// 0080 <id>.L <type>.B (ZC_NOTIFY_VANISH)
  822. /// type:
  823. /// 0 = out of sight
  824. /// 1 = died
  825. /// 2 = logged out
  826. /// 3 = teleport
  827. /// 4 = trickdead
  828. void clif_clearunit_area( block_list& bl, clr_type type ){
  829. PACKET_ZC_NOTIFY_VANISH packet{};
  830. packet.packetType = HEADER_ZC_NOTIFY_VANISH;
  831. packet.gid = bl.id;
  832. packet.type = static_cast<uint8>(type);
  833. clif_send(&packet, sizeof(PACKET_ZC_NOTIFY_VANISH), &bl, type == CLR_DEAD ? AREA : AREA_WOS);
  834. if(disguised(&bl)) {
  835. packet.gid = disguised_bl_id( bl.id );
  836. clif_send(&packet, sizeof(PACKET_ZC_NOTIFY_VANISH), &bl, SELF);
  837. }
  838. }
  839. /// Used to make monsters with player-sprites disappear after dying
  840. /// like normal monsters, because the client does not remove those
  841. /// automatically.
  842. static TIMER_FUNC(clif_clearunit_delayed_sub){
  843. struct block_list *bl = (struct block_list *)data;
  844. if( bl != nullptr ){
  845. clif_clearunit_area( *bl, (clr_type)id );
  846. ers_free( delay_clearunit_ers, bl );
  847. }
  848. return 0;
  849. }
  850. void clif_clearunit_delayed(struct block_list* bl, clr_type type, t_tick tick)
  851. {
  852. struct block_list *tbl = ers_alloc(delay_clearunit_ers, struct block_list);
  853. tbl->next = nullptr;
  854. tbl->prev = nullptr;
  855. tbl->id = bl->id;
  856. tbl->m = bl->m;
  857. tbl->x = bl->x;
  858. tbl->y = bl->y;
  859. tbl->type = BL_NUL;
  860. add_timer(tick, clif_clearunit_delayed_sub, (int32)type, (intptr_t)tbl);
  861. }
  862. void clif_get_weapon_view(map_session_data* sd, t_itemid *rhand, t_itemid *lhand)
  863. {
  864. if(sd->sc.option&OPTION_COSTUME)
  865. {
  866. *rhand = *lhand = 0;
  867. return;
  868. }
  869. #if PACKETVER < 4
  870. *rhand = sd->status.weapon;
  871. *lhand = sd->status.shield;
  872. #else
  873. if (sd->equip_index[EQI_HAND_R] >= 0 &&
  874. sd->inventory_data[sd->equip_index[EQI_HAND_R]])
  875. {
  876. struct item_data* id = sd->inventory_data[sd->equip_index[EQI_HAND_R]];
  877. if (id->view_id > 0)
  878. *rhand = id->view_id;
  879. else
  880. *rhand = id->nameid;
  881. } else
  882. *rhand = 0;
  883. if (sd->equip_index[EQI_HAND_L] >= 0 &&
  884. sd->equip_index[EQI_HAND_L] != sd->equip_index[EQI_HAND_R] &&
  885. sd->inventory_data[sd->equip_index[EQI_HAND_L]])
  886. {
  887. struct item_data* id = sd->inventory_data[sd->equip_index[EQI_HAND_L]];
  888. if (id->view_id > 0)
  889. *lhand = id->view_id;
  890. else
  891. *lhand = id->nameid;
  892. } else
  893. *lhand = 0;
  894. #endif
  895. }
  896. //To make the assignation of the level based on limits clearer/easier. [Skotlex]
  897. static int32 clif_setlevel_sub(int32 lv) {
  898. if( lv < battle_config.max_lv ) {
  899. ;
  900. } else if( lv < battle_config.aura_lv ) {
  901. lv = battle_config.max_lv - 1;
  902. } else {
  903. lv = battle_config.max_lv;
  904. }
  905. return lv;
  906. }
  907. static int32 clif_setlevel(struct block_list* bl) {
  908. int32 lv = status_get_lv(bl);
  909. if( battle_config.client_limit_unit_lv&bl->type )
  910. return clif_setlevel_sub(lv);
  911. switch( bl->type ) {
  912. case BL_NPC:
  913. case BL_PET:
  914. // npcs and pets do not have level
  915. return 0;
  916. }
  917. return lv;
  918. }
  919. /*==========================================
  920. * Prepares 'unit standing/spawning' packet
  921. *------------------------------------------*/
  922. static void clif_set_unit_idle( struct block_list* bl, bool walking, send_target target, struct block_list* tbl ){
  923. nullpo_retv( bl );
  924. map_session_data* sd = BL_CAST( BL_PC, bl );
  925. status_change* sc = status_get_sc( bl );
  926. struct view_data* vd = status_get_viewdata( bl );
  927. int32 g_id = status_get_guild_id( bl );
  928. #if PACKETVER < 20091103
  929. if( !pcdb_checkid( vd->class_ ) ){
  930. struct packet_idle_unit2 p;
  931. p.PacketType = idle_unit2Type;
  932. #if PACKETVER >= 20071106
  933. p.objecttype = clif_bl_type( bl, walking );
  934. #endif
  935. p.GID = bl->id;
  936. p.speed = status_get_speed( bl );
  937. p.bodyState = ( sc ) ? sc->opt1 : 0;
  938. p.healthState = ( sc ) ? sc->opt2 : 0;
  939. p.effectState = ( sc ) ? sc->option : 0;
  940. p.job = vd->class_;
  941. p.head = vd->hair_style;
  942. p.weapon = vd->weapon;
  943. p.accessory = vd->head_bottom;
  944. if( bl->type == BL_NPC && vd->class_ == JT_GUILD_FLAG ){
  945. // The hell, why flags work like this?
  946. p.shield = status_get_emblem_id( bl );
  947. p.accessory2 = GetWord( g_id, 1 );
  948. p.accessory3 = GetWord( g_id, 0 );
  949. }else{
  950. p.shield = vd->shield;
  951. p.accessory2 = vd->head_top;
  952. p.accessory3 = vd->head_mid;
  953. }
  954. p.headpalette = vd->hair_color;
  955. p.bodypalette = vd->cloth_color;
  956. p.headDir = ( sd )? sd->head_dir : 0;
  957. p.GUID = g_id;
  958. p.GEmblemVer = status_get_emblem_id( bl );
  959. p.honor = ( sd ) ? sd->status.manner : 0;
  960. p.virtue = ( sc ) ? sc->opt3 : 0;
  961. p.isPKModeON = ( sd && sd->status.karma ) ? 1 : 0;
  962. p.sex = vd->sex;
  963. WBUFPOS( &p.PosDir[0], 0, bl->x, bl->y, unit_getdir( bl ) );
  964. p.xSize = p.ySize = ( sd ) ? 5 : 0;
  965. p.state = vd->dead_sit;
  966. p.clevel = clif_setlevel( bl );
  967. clif_send( &p, sizeof( p ), tbl, target );
  968. return;
  969. }
  970. #endif
  971. struct packet_idle_unit p;
  972. p.PacketType = idle_unitType;
  973. #if PACKETVER >= 20091103
  974. p.PacketLength = sizeof(p);
  975. p.objecttype = clif_bl_type( bl, walking );
  976. #endif
  977. #if PACKETVER >= 20131223
  978. p.AID = bl->id;
  979. p.GID = (sd) ? sd->status.char_id : 0; // CCODE
  980. #else
  981. p.GID = bl->id;
  982. #endif
  983. p.speed = status_get_speed(bl);
  984. p.bodyState = (sc) ? sc->opt1 : 0;
  985. p.healthState = (sc) ? sc->opt2 : 0;
  986. // npc option changed?
  987. if( tbl && tbl->type == BL_PC && bl->type == BL_NPC ){
  988. map_session_data* sd = (map_session_data*)tbl;
  989. struct npc_data* nd = (struct npc_data*)bl;
  990. int32 option = (sc) ? sc->option : 0;
  991. if( !nd->vd.dead_sit ){
  992. if( std::find( sd->cloaked_npc.begin(), sd->cloaked_npc.end(), nd->bl.id ) != sd->cloaked_npc.end() ){
  993. option ^= OPTION_CLOAK;
  994. }
  995. }
  996. p.effectState = option;
  997. }else{
  998. p.effectState = (sc) ? sc->option : 0;
  999. }
  1000. p.job = vd->class_;
  1001. p.head = vd->hair_style;
  1002. p.weapon = vd->weapon;
  1003. #if PACKETVER < 7 || PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  1004. p.shield = vd->shield;
  1005. #endif
  1006. if( bl->type == BL_NPC && vd->class_ == JT_GUILD_FLAG ){
  1007. // The hell, why flags work like this?
  1008. p.accessory = status_get_emblem_id( bl );
  1009. p.accessory2 = GetWord( g_id, 1 );
  1010. p.accessory3 = GetWord( g_id, 0 );
  1011. }else{
  1012. p.accessory = vd->head_bottom;
  1013. p.accessory2 = vd->head_top;
  1014. p.accessory3 = vd->head_mid;
  1015. }
  1016. p.headpalette = vd->hair_color;
  1017. p.bodypalette = vd->cloth_color;
  1018. p.headDir = (sd)? sd->head_dir : 0;
  1019. #if PACKETVER >= 20101124
  1020. p.robe = vd->robe;
  1021. #endif
  1022. p.GUID = g_id;
  1023. p.GEmblemVer = status_get_emblem_id( bl );
  1024. p.honor = (sd) ? sd->status.manner : 0;
  1025. p.virtue = (sc) ? sc->opt3 : 0;
  1026. p.isPKModeON = (sd && sd->status.karma) ? 1 : 0;
  1027. p.sex = vd->sex;
  1028. WBUFPOS( &p.PosDir[0], 0, bl->x, bl->y, unit_getdir( bl ) );
  1029. p.xSize = p.ySize = (sd) ? 5 : 0;
  1030. p.state = vd->dead_sit;
  1031. p.clevel = clif_setlevel( bl );
  1032. #if PACKETVER >= 20080102
  1033. p.font = (sd) ? sd->status.font : 0;
  1034. #endif
  1035. #if PACKETVER >= 20120221
  1036. if( battle_config.monster_hp_bars_info && !map_getmapflag( bl->m, MF_HIDEMOBHPBAR ) && bl->type == BL_MOB && ( status_get_hp( bl ) < status_get_max_hp( bl ) ) ){
  1037. p.maxHP = status_get_max_hp(bl);
  1038. p.HP = status_get_hp(bl);
  1039. }else{
  1040. p.maxHP = -1;
  1041. p.HP = -1;
  1042. }
  1043. if( bl->type == BL_MOB ){
  1044. p.isBoss = ( (mob_data*)bl )->get_bosstype();
  1045. }else if( bl->type == BL_PET ){
  1046. p.isBoss = ( (pet_data*)bl )->db->get_bosstype();
  1047. }else{
  1048. p.isBoss = BOSSTYPE_NONE;
  1049. }
  1050. #endif
  1051. #if PACKETVER >= 20150513
  1052. p.body = vd->body_style;
  1053. #endif
  1054. /* Might be earlier, this is when the named item bug began */
  1055. #if PACKETVER >= 20131223
  1056. safestrncpy(p.name, status_get_name( *bl ), NAME_LENGTH);
  1057. #endif
  1058. clif_send( &p, sizeof( p ), tbl, target );
  1059. // if disguised, send to self
  1060. if( disguised( bl ) ){
  1061. #if PACKETVER >= 20091103
  1062. p.objecttype = pcdb_checkid( status_get_viewdata( bl )->class_ ) ? 0x0 : 0x5; //PC_TYPE : NPC_MOB_TYPE
  1063. #if PACKETVER >= 20131223
  1064. p.AID = disguised_bl_id( bl->id );
  1065. #else
  1066. p.GID = disguised_bl_id( bl->id );
  1067. #endif
  1068. #else
  1069. p.GID = disguised_bl_id( bl->id );
  1070. #endif
  1071. clif_send(&p, sizeof(p), bl, SELF);
  1072. }
  1073. }
  1074. static void clif_spawn_unit( struct block_list *bl, enum send_target target ){
  1075. nullpo_retv( bl );
  1076. map_session_data* sd = BL_CAST( BL_PC, bl );
  1077. status_change* sc = status_get_sc( bl );
  1078. struct view_data* vd = status_get_viewdata( bl );
  1079. int32 g_id = status_get_guild_id( bl );
  1080. #if PACKETVER < 20091103
  1081. if( !pcdb_checkid( vd->class_ ) ){
  1082. struct packet_spawn_unit2 p;
  1083. p.PacketType = spawn_unit2Type;
  1084. #if PACKETVER >= 20071106
  1085. p.objecttype = clif_bl_type( bl, false );
  1086. #endif
  1087. p.GID = bl->id;
  1088. p.speed = status_get_speed( bl );
  1089. p.bodyState = ( sc ) ? sc->opt1 : 0;
  1090. p.healthState = ( sc ) ? sc->opt2 : 0;
  1091. p.effectState = ( sc ) ? sc->option : 0;
  1092. p.head = vd->hair_style;
  1093. p.weapon = vd->weapon;
  1094. p.accessory = vd->head_bottom;
  1095. p.job = vd->class_;
  1096. if( bl->type == BL_NPC && vd->class_ == JT_GUILD_FLAG ){
  1097. // The hell, why flags work like this?
  1098. p.shield = status_get_emblem_id( bl );
  1099. p.accessory2 = GetWord( g_id, 1 );
  1100. p.accessory3 = GetWord( g_id, 0 );
  1101. }else{
  1102. p.shield = vd->shield;
  1103. p.accessory2 = vd->head_top;
  1104. p.accessory3 = vd->head_mid;
  1105. }
  1106. p.headpalette = vd->hair_color;
  1107. p.bodypalette = vd->cloth_color;
  1108. p.headDir = ( sd ) ? sd->head_dir : 0;
  1109. p.isPKModeON = ( sd && sd->status.karma ) ? 1 : 0;
  1110. p.sex = vd->sex;
  1111. WBUFPOS( &p.PosDir[0], 0, bl->x, bl->y, unit_getdir( bl ) );
  1112. p.xSize = p.ySize = ( sd ) ? 5 : 0;
  1113. clif_send( &p, sizeof( p ), bl, target );
  1114. return;
  1115. }
  1116. #endif
  1117. struct packet_spawn_unit p;
  1118. p.PacketType = spawn_unitType;
  1119. #if PACKETVER >= 20091103
  1120. p.PacketLength = sizeof(p);
  1121. p.objecttype = clif_bl_type( bl, false );
  1122. #endif
  1123. #if PACKETVER >= 20131223
  1124. p.AID = bl->id;
  1125. p.GID = (sd) ? sd->status.char_id : 0; // CCODE
  1126. #else
  1127. p.GID = bl->id;
  1128. #endif
  1129. p.speed = status_get_speed( bl );
  1130. p.bodyState = (sc) ? sc->opt1 : 0;
  1131. p.healthState = (sc) ? sc->opt2 : 0;
  1132. p.effectState = (sc) ? sc->option : 0;
  1133. p.job = vd->class_;
  1134. p.head = vd->hair_style;
  1135. p.weapon = vd->weapon;
  1136. #if PACKETVER < 7 || PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  1137. p.shield = vd->shield;
  1138. #endif
  1139. if( bl->type == BL_NPC && vd->class_ == JT_GUILD_FLAG ){
  1140. // The hell, why flags work like this?
  1141. p.accessory = status_get_emblem_id( bl );
  1142. p.accessory2 = GetWord( g_id, 1 );
  1143. p.accessory3 = GetWord( g_id, 0 );
  1144. }else{
  1145. p.accessory = vd->head_bottom;
  1146. p.accessory2 = vd->head_top;
  1147. p.accessory3 = vd->head_mid;
  1148. }
  1149. p.headpalette = vd->hair_color;
  1150. p.bodypalette = vd->cloth_color;
  1151. p.headDir = (sd)? sd->head_dir : 0;
  1152. #if PACKETVER >= 20101124
  1153. p.robe = vd->robe;
  1154. #endif
  1155. p.GUID = g_id;
  1156. p.GEmblemVer = status_get_emblem_id( bl );
  1157. p.honor = (sd) ? sd->status.manner : 0;
  1158. p.virtue = (sc) ? sc->opt3 : 0;
  1159. p.isPKModeON = (sd && sd->status.karma) ? 1 : 0;
  1160. p.sex = vd->sex;
  1161. WBUFPOS( &p.PosDir[0], 0, bl->x, bl->y, unit_getdir( bl ) );
  1162. p.xSize = p.ySize = (sd) ? 5 : 0;
  1163. p.clevel = clif_setlevel( bl );
  1164. #if PACKETVER >= 20080102
  1165. p.font = (sd) ? sd->status.font : 0;
  1166. #endif
  1167. #if PACKETVER >= 20120221
  1168. if( battle_config.monster_hp_bars_info && bl->type == BL_MOB && !map_getmapflag( bl->m, MF_HIDEMOBHPBAR ) && ( status_get_hp( bl ) < status_get_max_hp( bl ) ) ){
  1169. p.maxHP = status_get_max_hp( bl );
  1170. p.HP = status_get_hp( bl );
  1171. }else{
  1172. p.maxHP = -1;
  1173. p.HP = -1;
  1174. }
  1175. if( bl->type == BL_MOB ){
  1176. p.isBoss = ( (mob_data*)bl )->get_bosstype();
  1177. }else if( bl->type == BL_PET ){
  1178. p.isBoss = ( (pet_data*)bl )->db->get_bosstype();
  1179. }else{
  1180. p.isBoss = BOSSTYPE_NONE;
  1181. }
  1182. #endif
  1183. #if PACKETVER >= 20150513
  1184. p.body = vd->body_style;
  1185. #endif
  1186. /* Might be earlier, this is when the named item bug began */
  1187. #if PACKETVER >= 20131223
  1188. safestrncpy( p.name, status_get_name( *bl ), NAME_LENGTH );
  1189. #endif
  1190. if( disguised( bl ) ){
  1191. nullpo_retv( sd );
  1192. if( sd->status.class_ != sd->disguise ){
  1193. clif_send( &p, sizeof( p ), bl, target );
  1194. }
  1195. #if PACKETVER >= 20091103
  1196. p.objecttype = pcdb_checkid( status_get_viewdata(bl)->class_ ) ? 0x0 : 0x5; //PC_TYPE : NPC_MOB_TYPE
  1197. #if PACKETVER >= 20131223
  1198. p.AID = disguised_bl_id( bl->id );
  1199. #else
  1200. p.GID = disguised_bl_id( bl->id );
  1201. #endif
  1202. #else
  1203. p.GID = disguised_bl_id( bl->id );
  1204. #endif
  1205. clif_send( &p, sizeof( p ), bl, SELF );
  1206. }else{
  1207. clif_send( &p, sizeof( p ), bl, target );
  1208. }
  1209. }
  1210. /*==========================================
  1211. * Prepares 'unit walking' packet
  1212. *------------------------------------------*/
  1213. static void clif_set_unit_walking( struct block_list& bl, map_session_data* tsd, struct unit_data& ud, enum send_target target ){
  1214. struct packet_unit_walking p;
  1215. p.PacketType = unit_walkingType;
  1216. #if PACKETVER >= 20091103
  1217. p.PacketLength = sizeof(p);
  1218. #endif
  1219. #if PACKETVER >= 20071106
  1220. p.objecttype = clif_bl_type( &bl, true );
  1221. #endif
  1222. map_session_data* sd = BL_CAST(BL_PC, &bl);
  1223. #if PACKETVER >= 20131223
  1224. p.AID = bl.id;
  1225. p.GID = (sd) ? sd->status.char_id : 0; // CCODE
  1226. #else
  1227. p.GID = bl.id;
  1228. #endif
  1229. p.speed = status_get_speed( &bl );
  1230. status_change* sc = status_get_sc( &bl );
  1231. p.bodyState = (sc) ? sc->opt1 : 0;
  1232. p.healthState = (sc) ? sc->opt2 : 0;
  1233. p.effectState = (sc) ? sc->option : 0;
  1234. struct view_data* vd = status_get_viewdata( &bl );
  1235. p.job = vd->class_;
  1236. p.head = vd->hair_style;
  1237. p.weapon = vd->weapon;
  1238. p.accessory = vd->head_bottom;
  1239. p.moveStartTime = client_tick(gettick());
  1240. #if PACKETVER < 7 || PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  1241. p.shield = vd->shield;
  1242. #endif
  1243. p.accessory2 = vd->head_top;
  1244. p.accessory3 = vd->head_mid;
  1245. p.headpalette = vd->hair_color;
  1246. p.bodypalette = vd->cloth_color;
  1247. p.headDir = (sd) ? sd->head_dir : 0;
  1248. #if PACKETVER >= 20101124
  1249. p.robe = vd->robe;
  1250. #endif
  1251. p.GUID = status_get_guild_id( &bl );
  1252. p.GEmblemVer = status_get_emblem_id( &bl );
  1253. p.honor = (sd) ? sd->status.manner : 0;
  1254. p.virtue = (sc) ? sc->opt3 : 0;
  1255. p.isPKModeON = (sd && sd->status.karma) ? 1 : 0;
  1256. p.sex = vd->sex;
  1257. WBUFPOS2(&p.MoveData[0], 0, bl.x, bl.y, ud.to_x, ud.to_y, ud.sx, ud.sy);
  1258. p.xSize = p.ySize = (sd) ? 5 : 0;
  1259. p.clevel = clif_setlevel( &bl );
  1260. #if PACKETVER >= 20080102
  1261. p.font = (sd) ? sd->status.font : 0;
  1262. #endif
  1263. #if PACKETVER >= 20120221
  1264. if( battle_config.monster_hp_bars_info && !map_getmapflag(bl.m, MF_HIDEMOBHPBAR) && bl.type == BL_MOB && (status_get_hp( &bl ) < status_get_max_hp( &bl ) ) ){
  1265. p.maxHP = status_get_max_hp( &bl );
  1266. p.HP = status_get_hp( &bl );
  1267. } else {
  1268. p.maxHP = -1;
  1269. p.HP = -1;
  1270. }
  1271. if( bl.type == BL_MOB ){
  1272. p.isBoss = reinterpret_cast<mob_data*>( &bl )->get_bosstype();
  1273. }else if( bl.type == BL_PET ){
  1274. p.isBoss = reinterpret_cast<pet_data*>( &bl )->db->get_bosstype();
  1275. }else{
  1276. p.isBoss = BOSSTYPE_NONE;
  1277. }
  1278. #endif
  1279. #if PACKETVER >= 20150513
  1280. p.body = vd->body_style;
  1281. #endif
  1282. /* Might be earlier, this is when the named item bug began */
  1283. #if PACKETVER >= 20131223
  1284. safestrncpy(p.name, status_get_name( bl ), NAME_LENGTH);
  1285. #endif
  1286. clif_send( &p, sizeof(p), tsd ? &tsd->bl : &bl, target );
  1287. // if disguised, send the info to self
  1288. if( disguised( &bl ) ){
  1289. #if PACKETVER >= 20091103
  1290. p.objecttype = pcdb_checkid( status_get_viewdata( &bl )->class_ ) ? 0x0 : 0x5; //PC_TYPE : NPC_MOB_TYPE
  1291. #if PACKETVER >= 20131223
  1292. p.AID = disguised_bl_id( bl.id );
  1293. #else
  1294. p.GID = disguised_bl_id( bl.id );
  1295. #endif
  1296. #else
  1297. p.GID = disguised_bl_id( bl.id );
  1298. #endif
  1299. clif_send(&p, sizeof(p), &bl, SELF);
  1300. }
  1301. }
  1302. /// Changes sprite of a non player object.
  1303. /// 01b0 <id>.L <type>.B <value>.L (ZC_NPCSPRITE_CHANGE)
  1304. void clif_class_change( block_list& bl, int32 class_, enum send_target target, map_session_data* sd ){
  1305. if( pcdb_checkid( class_ ) ){
  1306. // player classes yield missing sprites
  1307. return;
  1308. }
  1309. PACKET_ZC_NPCSPRITE_CHANGE p = {};
  1310. p.packetType = HEADER_ZC_NPCSPRITE_CHANGE;
  1311. p.GID = bl.id;
  1312. // Unused
  1313. p.type = 0;
  1314. p.class_ = class_;
  1315. if( sd != nullptr ){
  1316. bl = sd->bl;
  1317. }
  1318. clif_send( &p, sizeof( p ), &bl, target );
  1319. }
  1320. void clif_servantball( map_session_data& sd, struct block_list* target, enum send_target send_target ){
  1321. PACKET_ZC_SPIRITS p = {};
  1322. p.PacketType = HEADER_ZC_SPIRITS;
  1323. p.AID = sd.bl.id;
  1324. p.num = sd.servantball;
  1325. if( target == nullptr ){
  1326. target = &sd.bl;
  1327. }
  1328. clif_send( &p, sizeof( p ), target, send_target );
  1329. }
  1330. void clif_abyssball( map_session_data& sd, struct block_list* target, enum send_target send_target ){
  1331. PACKET_ZC_SPIRITS p = {};
  1332. p.PacketType = HEADER_ZC_SPIRITS;
  1333. p.AID = sd.bl.id;
  1334. p.num = sd.abyssball;
  1335. if( target == nullptr ){
  1336. target = &sd.bl;
  1337. }
  1338. clif_send( &p, sizeof( p ), target, send_target );
  1339. }
  1340. /// Notifies the client of an object's Millenium Shields.
  1341. static void clif_millenniumshield_single( map_session_data& sd, map_session_data& tsd ){
  1342. #if PACKETVER >= 20081126
  1343. status_change_entry* sce = sd.sc.getSCE( SC_MILLENNIUMSHIELD );
  1344. if( sce == nullptr ){
  1345. return;
  1346. }
  1347. PACKET_ZC_MILLENNIUMSHIELD packet{};
  1348. packet.packetType = HEADER_ZC_MILLENNIUMSHIELD;
  1349. packet.aid = sd.bl.id;
  1350. packet.num = sce->val2;
  1351. packet.state = 0;
  1352. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  1353. #endif
  1354. }
  1355. /*==========================================
  1356. * Kagerou/Oboro amulet spirit
  1357. *------------------------------------------*/
  1358. static void clif_spiritcharm_single( map_session_data& sd, map_session_data& tsd ){
  1359. #if PACKETVER >= 20111102
  1360. if( sd.spiritcharm_type == CHARM_TYPE_NONE ){
  1361. return;
  1362. }
  1363. if( sd.spiritcharm <= 0 ){
  1364. return;
  1365. }
  1366. PACKET_ZC_SPIRITS_ATTRIBUTE packet{};
  1367. packet.packetType = HEADER_ZC_SPIRITS_ATTRIBUTE;
  1368. packet.aid = sd.bl.id;
  1369. packet.spiritsType = static_cast<decltype(packet.spiritsType)>(sd.spiritcharm_type);
  1370. packet.num = static_cast<decltype(packet.num)>(sd.spiritcharm);
  1371. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  1372. #endif
  1373. }
  1374. /*==========================================
  1375. * Enchanting Shadow / Shadow Scar Spirit
  1376. *------------------------------------------*/
  1377. void clif_enchantingshadow_spirit(unit_data &ud) {
  1378. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191120 || PACKETVER_ZERO_NUM >= 20191127
  1379. PACKET_ZC_TARGET_SPIRITS p = {};
  1380. p.packetType = HEADER_ZC_TARGET_SPIRITS;
  1381. p.GID = ud.bl->id;
  1382. p.unknown_val = 0;
  1383. p.amount = static_cast<uint16>(ud.shadow_scar_timer.size());
  1384. clif_send(&p, sizeof(p), ud.bl, AREA);
  1385. #endif
  1386. }
  1387. /*==========================================
  1388. * Run when player changes map / refreshes
  1389. * Tells its client to display all weather settings being used by this map
  1390. *------------------------------------------*/
  1391. static void clif_weather_check(map_session_data *sd)
  1392. {
  1393. int16 m = sd->bl.m;
  1394. int32 fd = sd->fd;
  1395. if (map_getmapflag(m, MF_SNOW)
  1396. || map_getmapflag(m, MF_CLOUDS)
  1397. || map_getmapflag(m, MF_FOG)
  1398. || map_getmapflag(m, MF_FIREWORKS)
  1399. || map_getmapflag(m, MF_SAKURA)
  1400. || map_getmapflag(m, MF_LEAVES)
  1401. || map_getmapflag(m, MF_CLOUDS2))
  1402. {
  1403. if (map_getmapflag(m, MF_SNOW))
  1404. clif_specialeffect_single(&sd->bl, EF_SNOW, fd);
  1405. if (map_getmapflag(m, MF_CLOUDS))
  1406. clif_specialeffect_single(&sd->bl, EF_CLOUD3, fd);
  1407. if (map_getmapflag(m, MF_CLOUDS2))
  1408. clif_specialeffect_single(&sd->bl, EF_CLOUD5, fd);
  1409. if (map_getmapflag(m, MF_FOG))
  1410. clif_specialeffect_single(&sd->bl, EF_CLOUD4, fd);
  1411. if (map_getmapflag(m, MF_FIREWORKS)) {
  1412. clif_specialeffect_single(&sd->bl, EF_POKJUK, fd);
  1413. clif_specialeffect_single(&sd->bl, EF_THROWITEM2, fd);
  1414. clif_specialeffect_single(&sd->bl, EF_POKJUK_SOUND, fd);
  1415. }
  1416. if (map_getmapflag(m, MF_SAKURA))
  1417. clif_specialeffect_single(&sd->bl, EF_SAKURA, fd);
  1418. if (map_getmapflag(m, MF_LEAVES))
  1419. clif_specialeffect_single(&sd->bl, EF_MAPLE, fd);
  1420. }
  1421. }
  1422. /**
  1423. * Run when the weather on a map changes, throws all players in map id 'm' to clif_weather_check function
  1424. **/
  1425. void clif_weather(int16 m)
  1426. {
  1427. struct s_mapiterator* iter;
  1428. map_session_data *sd=nullptr;
  1429. iter = mapit_getallusers();
  1430. for( sd = (map_session_data*)mapit_first(iter); mapit_exists(iter); sd = (map_session_data*)mapit_next(iter) )
  1431. {
  1432. if( sd->bl.m == m )
  1433. clif_weather_check(sd);
  1434. }
  1435. mapit_free(iter);
  1436. }
  1437. /**
  1438. * Hide a NPC from the effects of Maya Purple card.
  1439. * @param bl: Block data
  1440. * @return True if NPC is disabled or false otherwise
  1441. */
  1442. static inline bool clif_npc_mayapurple(block_list *bl) {
  1443. nullpo_retr(false, bl);
  1444. if (bl->type == BL_NPC) {
  1445. npc_data *nd = map_id2nd(bl->id);
  1446. // TODO: Confirm if waitingroom cause any special cases
  1447. if (/* nd->chat_id == 0 && */ nd->is_invisible)
  1448. return true;
  1449. }
  1450. return false;
  1451. }
  1452. /**
  1453. * Main function to spawn a unit on the client (player/mob/pet/etc)
  1454. **/
  1455. int32 clif_spawn( struct block_list *bl, bool walking ){
  1456. struct view_data *vd;
  1457. vd = status_get_viewdata(bl);
  1458. if( !vd || vd->class_ == JT_INVISIBLE )
  1459. return 0;
  1460. // Hide NPC from Maya Purple card
  1461. if (clif_npc_mayapurple(bl))
  1462. return 0;
  1463. if( bl->type == BL_NPC && !vd->dead_sit ){
  1464. clif_set_unit_idle( bl, walking, AREA_WOS, bl );
  1465. }else{
  1466. clif_spawn_unit( bl, AREA_WOS );
  1467. }
  1468. if (vd->cloth_color)
  1469. clif_refreshlook(bl,bl->id,LOOK_CLOTHES_COLOR,vd->cloth_color,AREA_WOS);
  1470. if (vd->body_style)
  1471. clif_refreshlook(bl,bl->id,LOOK_BODY2,vd->body_style,AREA_WOS);
  1472. switch (bl->type)
  1473. {
  1474. case BL_PC:
  1475. {
  1476. TBL_PC *sd = ((TBL_PC*)bl);
  1477. if (sd->spiritball > 0)
  1478. clif_spiritball(&sd->bl);
  1479. if (sd->sc.getSCE(SC_MILLENNIUMSHIELD))
  1480. clif_millenniumshield( sd->bl, sd->sc.getSCE( SC_MILLENNIUMSHIELD )->val2 );
  1481. if (sd->soulball > 0)
  1482. clif_soulball(sd);
  1483. if (sd->servantball > 0)
  1484. clif_servantball( *sd );
  1485. if (sd->abyssball > 0)
  1486. clif_abyssball( *sd );
  1487. if(sd->state.size==SZ_BIG) // tiny/big players [Valaris]
  1488. clif_specialeffect(bl,EF_GIANTBODY2,AREA);
  1489. else if(sd->state.size==SZ_MEDIUM)
  1490. clif_specialeffect(bl,EF_BABYBODY2,AREA);
  1491. if( sd->bg_id && map_getmapflag(sd->bl.m, MF_BATTLEGROUND) )
  1492. clif_sendbgemblem_area(sd);
  1493. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  1494. clif_spiritcharm( *sd );
  1495. if (sd->status.robe)
  1496. clif_refreshlook(bl,bl->id,LOOK_ROBE,sd->status.robe,AREA);
  1497. clif_efst_status_change_sub(bl, bl, AREA);
  1498. clif_hat_effects( *sd, sd->bl, AREA );
  1499. }
  1500. break;
  1501. case BL_MOB:
  1502. {
  1503. TBL_MOB *md = ((TBL_MOB*)bl);
  1504. if(md->special_state.size==SZ_BIG) // tiny/big mobs [Valaris]
  1505. clif_specialeffect(&md->bl,EF_GIANTBODY2,AREA);
  1506. else if(md->special_state.size==SZ_MEDIUM)
  1507. clif_specialeffect(&md->bl,EF_BABYBODY2,AREA);
  1508. if ( md->special_state.ai == AI_ABR || md->special_state.ai == AI_BIONIC )
  1509. clif_summon_init(*md);
  1510. }
  1511. break;
  1512. case BL_NPC:
  1513. {
  1514. TBL_NPC *nd = ((TBL_NPC*)bl);
  1515. if( nd->size == SZ_BIG )
  1516. clif_specialeffect(&nd->bl,EF_GIANTBODY2,AREA);
  1517. else if( nd->size == SZ_MEDIUM )
  1518. clif_specialeffect(&nd->bl,EF_BABYBODY2,AREA);
  1519. clif_efst_status_change_sub(bl, bl, AREA);
  1520. clif_progressbar_npc_area(nd);
  1521. }
  1522. break;
  1523. case BL_PET:
  1524. // If the pet wears equip
  1525. if( vd->head_bottom != 0 ){
  1526. pet_data& pd = *reinterpret_cast<pet_data*>( bl );
  1527. clif_send_petdata( nullptr, pd, CHANGESTATEPET_ACCESSORY );
  1528. }
  1529. break;
  1530. }
  1531. return 0;
  1532. }
  1533. /// Notifies client of a change in an homunculus' status parameter.
  1534. /// 0x7db <type>.W <value>.L (ZC_HO_PAR_CHANGE)
  1535. /// 0xba5 <type>.W <value>.Q (ZC_HO_PAR_CHANGE2)
  1536. void clif_homunculus_updatestatus(map_session_data& sd, _sp type) {
  1537. #if PACKETVER >= 20090610
  1538. if( !hom_is_active(sd.hd) )
  1539. return;
  1540. PACKET_ZC_HO_PAR_CHANGE p = {};
  1541. p.packetType = HEADER_ZC_HO_PAR_CHANGE;
  1542. p.type = static_cast<decltype(p.type)>(type);
  1543. status_data* status = &sd.hd->battle_status;
  1544. switch (type) {
  1545. case SP_BASEEXP:
  1546. p.value = static_cast<decltype(p.value)>( std::min<decltype(sd.hd->homunculus.exp)>( sd.hd->homunculus.exp, std::numeric_limits<decltype(p.value)>::max() ) );
  1547. break;
  1548. case SP_HP:
  1549. // Homunculus HP and SP bars will screw up if the percentage calculation exceeds signed values
  1550. // Any value above will screw up the bars
  1551. // Since max values depend on PACKET_ZC_PROPERTY_HOMUN we check for each version of that packet
  1552. #if PACKETVER_MAIN_NUM >= 20200819 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20190626
  1553. // Tested maximum: 2147483647 (INT32_MAX)
  1554. if (status->max_hp > INT32_MAX)
  1555. #elif PACKETVER_MAIN_NUM >= 20131230 || PACKETVER_RE_NUM >= 20131230 || defined(PACKETVER_ZERO)
  1556. // Tested maximum: 21474836 (INT32_MAX / 100)
  1557. if (status->max_hp > INT32_MAX / 100)
  1558. #else
  1559. // Tested maximum: 32767 (INT16_MAX)
  1560. if (status->max_hp > INT16_MAX)
  1561. #endif
  1562. p.value = status->hp / (status->max_hp / 100);
  1563. else
  1564. p.value = static_cast<decltype(p.value)>(status->hp);
  1565. break;
  1566. case SP_SP:
  1567. #if PACKETVER_MAIN_NUM >= 20200819 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20190626
  1568. // Tested maximum: 2147483647 (INT32_MAX)
  1569. if (status->max_sp > INT32_MAX)
  1570. #else
  1571. // Tested maximum: 32767 (INT16_MAX)
  1572. if (status->max_sp > INT16_MAX)
  1573. #endif
  1574. p.value = status->sp / (status->max_sp / 100);
  1575. else
  1576. p.value = static_cast<decltype(p.value)>(status->sp);
  1577. break;
  1578. default:
  1579. ShowError("clif_homunculus_updatestatus: Unsupported type %d.\n", type);
  1580. return;
  1581. }
  1582. clif_send(&p, sizeof(p), &sd.bl, SELF);
  1583. #endif
  1584. }
  1585. /// Sends information about owned homunculus to the client . [orn]
  1586. /// 022e <name>.24B <modified>.B <level>.W <hunger>.W <intimacy>.W <equip id>.W <atk>.W <matk>.W <hit>.W <crit>.W <def>.W <mdef>.W <flee>.W <aspd>.W <hp>.W <max hp>.W <sp>.W <max sp>.W <exp>.L <max exp>.L <skill points>.W <atk range>.W (ZC_PROPERTY_HOMUN)
  1587. /// 09f7 <name>.24B <modified>.B <level>.W <hunger>.W <intimacy>.W <equip id>.W <atk>.W <matk>.W <hit>.W <crit>.W <def>.W <mdef>.W <flee>.W <aspd>.W <hp>.L <max hp>.L <sp>.W <max sp>.W <exp>.L <max exp>.L <skill points>.W <atk range>.W (ZC_PROPERTY_HOMUN_2)
  1588. void clif_hominfo( map_session_data *sd, struct homun_data *hd, int32 flag ){
  1589. #if PACKETVER_MAIN_NUM >= 20101005 || PACKETVER_RE_NUM >= 20080827 || defined(PACKETVER_ZERO)
  1590. nullpo_retv( sd );
  1591. nullpo_retv( hd );
  1592. struct status_data *status = &hd->battle_status;
  1593. PACKET_ZC_PROPERTY_HOMUN p = {};
  1594. p.packetType = HEADER_ZC_PROPERTY_HOMUN;
  1595. safestrncpy( p.name, hd->homunculus.name, sizeof( p.name ) );
  1596. // Bit field, bit 0 : rename_flag (1 = already renamed), bit 1 : homunc vaporized (1 = true), bit 2 : homunc dead (1 = true)
  1597. p.flags = ( !battle_config.hom_rename && hd->homunculus.rename_flag ? 0x1 : 0x0 ) | ( hd->homunculus.vaporize == HOM_ST_REST ? 0x2 : 0 ) | ( hd->homunculus.hp > 0 ? 0x4 : 0 );
  1598. p.level = hd->homunculus.level;
  1599. p.hunger = hd->homunculus.hunger;
  1600. p.intimacy = hd->homunculus.intimacy / 100;
  1601. #if !(PACKETVER_MAIN_NUM >= 20190619 || PACKETVER_RE_NUM >= 20190605 || PACKETVER_ZERO_NUM >= 20190626)
  1602. p.itemId = 0; // equip id
  1603. #endif
  1604. p.atk2 = cap_value( status->rhw.atk2 + status->batk, 0, INT16_MAX );
  1605. p.matk = i16min( status->matk_max, INT16_MAX ); //FIXME capping to INT16 here is too late
  1606. p.hit = status->hit;
  1607. if( battle_config.hom_setting&HOMSET_DISPLAY_LUK ){
  1608. p.crit = status->luk / 3 + 1; //crit is a +1 decimal value! Just display purpose.[Vicious]
  1609. }else{
  1610. p.crit = status->cri / 10;
  1611. }
  1612. #ifdef RENEWAL
  1613. p.def = status->def + status->def2;
  1614. p.mdef = status->mdef + status->mdef2;
  1615. #else
  1616. p.def = status->def + status->vit;
  1617. p.mdef = status->mdef;
  1618. #endif
  1619. p.flee = status->flee;
  1620. p.amotion = (flag) ? 0 : status->amotion;
  1621. // Homunculus HP and SP bars will screw up if the percentage calculation exceeds signed values
  1622. // Any value above will screw up the bars
  1623. #if PACKETVER_MAIN_NUM >= 20200819 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20190626
  1624. // Tested maximum: 2147483647 (INT32_MAX)
  1625. if (status->max_hp > INT32_MAX)
  1626. #elif PACKETVER_MAIN_NUM >= 20131230 || PACKETVER_RE_NUM >= 20131230 || defined(PACKETVER_ZERO)
  1627. // Tested maximum: 21474836 (INT32_MAX / 100)
  1628. if (status->max_hp > INT32_MAX / 100)
  1629. #else
  1630. // Tested maximum: 32767 (INT16_MAX)
  1631. if (status->max_hp > INT16_MAX)
  1632. #endif
  1633. {
  1634. p.hp = status->hp / ( status->max_hp / 100 );
  1635. p.maxHp = 100;
  1636. }else{
  1637. p.hp = static_cast<decltype(p.hp)>(status->hp);
  1638. p.maxHp = static_cast<decltype(p.maxHp)>(status->max_hp);
  1639. }
  1640. #if PACKETVER_MAIN_NUM >= 20200819 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20190626
  1641. // Tested maximum: 2147483647 (INT32_MAX)
  1642. if (status->max_sp > INT32_MAX)
  1643. #else
  1644. // Tested maximum: 32767 (INT16_MAX)
  1645. if (status->max_sp > INT16_MAX)
  1646. #endif
  1647. {
  1648. p.sp = status->sp / ( status->max_sp / 100 );
  1649. p.maxSp = 100;
  1650. }else{
  1651. p.sp = static_cast<decltype(p.sp)>(status->sp);
  1652. p.maxSp = static_cast<decltype(p.maxSp)>(status->max_sp);
  1653. }
  1654. p.exp = static_cast<decltype(p.exp)>( std::min<decltype(hd->homunculus.exp)>( hd->homunculus.exp, std::numeric_limits<decltype(p.exp)>::max() ) );
  1655. p.expNext = static_cast<decltype(p.expNext)>( std::min<decltype(hd->exp_next)>( hd->exp_next, std::numeric_limits<decltype(p.expNext)>::max() ) );
  1656. switch( hom_class2type( hd->homunculus.class_ ) ){
  1657. case HT_REG:
  1658. case HT_EVO:
  1659. if( hd->homunculus.level >= battle_config.hom_max_level ){
  1660. p.expNext = 0;
  1661. }
  1662. break;
  1663. case HT_S:
  1664. if( hd->homunculus.level >= battle_config.hom_S_max_level ){
  1665. p.expNext = 0;
  1666. }
  1667. break;
  1668. }
  1669. p.skillPoints = hd->homunculus.skillpts;
  1670. p.range = status_get_range( &hd->bl );
  1671. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  1672. #endif
  1673. }
  1674. /// Notification about a change in homunuculus' state.
  1675. /// 0230 <type>.B <state>.B <id>.L <data>.L (ZC_CHANGESTATE_MER)
  1676. /// type:
  1677. /// unused
  1678. /// state:
  1679. /// 0 = pre-init
  1680. /// 1 = intimacy
  1681. /// 2 = hunger
  1682. /// 3 = accessory?
  1683. /// ? = ignored
  1684. void clif_send_homdata( homun_data& hd, e_hom_state2 state ){
  1685. #if PACKETVER >= 20050523
  1686. if( hd.master == nullptr ){
  1687. return;
  1688. }
  1689. uint32 param;
  1690. switch( state ){
  1691. case SP_ACK:
  1692. param = 0;
  1693. break;
  1694. case SP_INTIMATE:
  1695. // TODO: Why is something like this here? [Lemongrass]
  1696. if( hd.homunculus.class_ == hd.homunculusDB->evo_class && hom_intimacy_intimacy2grade( hd.homunculus.intimacy ) >= HOMGRADE_LOYAL ){
  1697. hom_calc_skilltree( &hd );
  1698. }
  1699. param = hd.homunculus.intimacy / 100;
  1700. break;
  1701. case SP_HUNGRY:
  1702. param = hd.homunculus.hunger;
  1703. break;
  1704. default:
  1705. return;
  1706. }
  1707. PACKET_ZC_CHANGESTATE_MER packet{};
  1708. packet.packetType = HEADER_ZC_CHANGESTATE_MER;
  1709. packet.type = 0;
  1710. packet.state = state;
  1711. packet.gid = hd.bl.id;
  1712. packet.data = param;
  1713. clif_send( &packet, sizeof( packet ), &hd.master->bl, SELF );
  1714. #endif
  1715. }
  1716. void clif_homskillinfoblock( homun_data& hd ){
  1717. #if PACKETVER >= 20050530
  1718. map_session_data* sd = hd.master;
  1719. if( sd == nullptr ){
  1720. return;
  1721. }
  1722. PACKET_ZC_HOSKILLINFO_LIST* packet = reinterpret_cast<PACKET_ZC_HOSKILLINFO_LIST*>(packet_buffer);
  1723. packet->packetType = HEADER_ZC_HOSKILLINFO_LIST;
  1724. packet->packetLength = sizeof( *packet );
  1725. for(int32 i = 0, count = 0; i < MAX_HOMUNSKILL; i++) {
  1726. int32 id = hd.homunculus.hskill[i].id;
  1727. if (id != 0) {
  1728. int32 combo = (hd.homunculus.hskill[i].flag)&SKILL_FLAG_TMP_COMBO;
  1729. int16 idx = hom_skill_get_index(id);
  1730. if (idx == -1)
  1731. continue;
  1732. packet->skills[count].id = id;
  1733. packet->skills[count].inf = (combo) ? INF_SELF_SKILL : skill_get_inf(id);
  1734. packet->skills[count].unknown = 0;
  1735. packet->skills[count].level = hd.homunculus.hskill[idx].lv;
  1736. packet->skills[count].sp = skill_get_sp(id,hd.homunculus.hskill[idx].lv);
  1737. packet->skills[count].range = skill_get_range2(&hd.bl,id,hd.homunculus.hskill[idx].lv,false);
  1738. safestrncpy(packet->skills[count].name, skill_get_name(id), NAME_LENGTH);
  1739. packet->skills[count].upgradable = (hd.homunculus.level < hom_skill_get_min_level(hd.homunculus.class_, id) || hd.homunculus.hskill[idx].lv >= hom_skill_tree_get_max(id, hd.homunculus.class_)) ? 0 : 1;
  1740. packet->packetLength += sizeof( packet->skills[0] );
  1741. count++;
  1742. }
  1743. }
  1744. clif_send( packet, packet->packetLength, &sd->bl, SELF );
  1745. #endif
  1746. }
  1747. void clif_homskillup( homun_data& hd, uint16 skill_id ){
  1748. #if PACKETVER >= 20050531
  1749. int16 idx = hom_skill_get_index( skill_id );
  1750. if( idx == -1 ){
  1751. return;
  1752. }
  1753. map_session_data* sd = hd.master;
  1754. if( sd == nullptr ){
  1755. return;
  1756. }
  1757. PACKET_ZC_HOSKILLINFO_UPDATE packet{};
  1758. packet.packetType = HEADER_ZC_HOSKILLINFO_UPDATE;
  1759. packet.skill_id = skill_id;
  1760. packet.Level = hd.homunculus.hskill[idx].lv;
  1761. packet.SP = skill_get_sp(skill_id,hd.homunculus.hskill[idx].lv);
  1762. packet.AttackRange = skill_get_range2(&hd.bl,skill_id,hd.homunculus.hskill[idx].lv,false);
  1763. packet.upgradable = (hd.homunculus.level < hom_skill_get_min_level(hd.homunculus.class_, skill_id) || hd.homunculus.hskill[idx].lv >= hom_skill_tree_get_max(skill_id, hd.homunculus.class_)) ? 0 : 1;
  1764. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  1765. #endif
  1766. }
  1767. /// Result of request to feed a homun/merc.
  1768. /// 022f <result>.B <name id>.W (ZC_FEED_MER)
  1769. /// result:
  1770. /// 0 = failure
  1771. /// 1 = success
  1772. void clif_hom_food( map_session_data& sd, int32 foodid, bool success ){
  1773. PACKET_ZC_FEED_MER packet{};
  1774. packet.packetType = HEADER_ZC_FEED_MER;
  1775. packet.result = success;
  1776. packet.itemId = client_nameid( foodid );
  1777. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1778. }
  1779. /// Notifies the client, that it is walking.
  1780. /// 0087 <walk start time>.L <walk data>.6B (ZC_NOTIFY_PLAYERMOVE)
  1781. void clif_walkok( map_session_data& sd ){
  1782. PACKET_ZC_NOTIFY_PLAYERMOVE packet{};
  1783. packet.packetType = HEADER_ZC_NOTIFY_PLAYERMOVE;
  1784. packet.moveStartTime = client_tick(gettick());
  1785. WBUFPOS2(packet.moveData, 0, sd.bl.x, sd.bl.y, sd.ud.to_x, sd.ud.to_y, 8, 8);
  1786. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1787. }
  1788. /// Notifies clients in an area, that an other visible object is walking.
  1789. /// Note: unit must not be self
  1790. void clif_move( struct unit_data& ud )
  1791. {
  1792. struct block_list* bl = ud.bl;
  1793. struct view_data* vd = status_get_viewdata(bl);
  1794. if (bl == nullptr || vd == nullptr)
  1795. return;
  1796. // This performance check is needed to keep GM-hidden objects from being notified to bots.
  1797. if (vd->class_ == JT_INVISIBLE)
  1798. return;
  1799. // Hide NPC from Maya Purple card
  1800. if (clif_npc_mayapurple(bl))
  1801. return;
  1802. status_change* sc = nullptr;
  1803. if ((sc = status_get_sc(bl)) && sc->option & (OPTION_HIDE | OPTION_CLOAK | OPTION_INVISIBLE | OPTION_CHASEWALK))
  1804. clif_ally_only = true;
  1805. clif_set_unit_walking( *bl, nullptr, ud, AREA_WOS );
  1806. if (vd->cloth_color)
  1807. clif_refreshlook(bl, bl->id, LOOK_CLOTHES_COLOR, vd->cloth_color, AREA_WOS);
  1808. if (vd->body_style)
  1809. clif_refreshlook(bl, bl->id, LOOK_BODY2, vd->body_style, AREA_WOS);
  1810. switch (bl->type) {
  1811. case BL_PC:
  1812. {
  1813. map_session_data* sd = reinterpret_cast<map_session_data*>( bl );
  1814. if (sd->state.size == SZ_BIG) // tiny/big players [Valaris]
  1815. clif_specialeffect(&sd->bl, EF_GIANTBODY2, AREA);
  1816. else if (sd->state.size == SZ_MEDIUM)
  1817. clif_specialeffect(&sd->bl, EF_BABYBODY2, AREA);
  1818. if (sd->status.robe)
  1819. clif_refreshlook(bl, bl->id, LOOK_ROBE, sd->status.robe, AREA);
  1820. }
  1821. break;
  1822. case BL_MOB:
  1823. {
  1824. mob_data* md = reinterpret_cast<mob_data*>( bl );
  1825. if (md->special_state.size == SZ_BIG) // tiny/big mobs [Valaris]
  1826. clif_specialeffect(&md->bl, EF_GIANTBODY2, AREA);
  1827. else if (md->special_state.size == SZ_MEDIUM)
  1828. clif_specialeffect(&md->bl, EF_BABYBODY2, AREA);
  1829. }
  1830. break;
  1831. case BL_PET:
  1832. // If the pet wears equip
  1833. if( vd->head_bottom != 0 ){
  1834. pet_data& pd = *reinterpret_cast<pet_data*>( bl );
  1835. clif_send_petdata( nullptr, pd, CHANGESTATEPET_ACCESSORY );
  1836. }
  1837. break;
  1838. }
  1839. clif_ally_only = false;
  1840. }
  1841. /*==========================================
  1842. * Delays the map_quit of a player after they are disconnected. [Skotlex]
  1843. *------------------------------------------*/
  1844. static TIMER_FUNC(clif_delayquit){
  1845. map_session_data *sd = nullptr;
  1846. //Remove player from map server
  1847. if ((sd = map_id2sd(id)) != nullptr && sd->fd == 0) //Should be a disconnected player.
  1848. map_quit(sd);
  1849. return 0;
  1850. }
  1851. /*==========================================
  1852. *
  1853. *------------------------------------------*/
  1854. void clif_quitsave(int32 fd,map_session_data *sd) {
  1855. if (!battle_config.prevent_logout ||
  1856. sd->canlog_tick == 0 ||
  1857. DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout)
  1858. map_quit(sd);
  1859. else if (session_isValid(sd->fd)) {
  1860. //Disassociate session from player (session is deleted after this function was called)
  1861. //And set a timer to make him quit later.
  1862. session[sd->fd]->session_data = nullptr;
  1863. sd->fd = 0;
  1864. add_timer(gettick() + 10000, clif_delayquit, sd->bl.id, 0);
  1865. }
  1866. }
  1867. /// Notifies the client of a position change to coordinates on given map (ZC_NPCACK_MAPMOVE).
  1868. /// 0091 <map name>.16B <x>.W <y>.W
  1869. void clif_changemap( map_session_data& sd, int16 m, uint16 x, uint16 y ){
  1870. PACKET_ZC_NPCACK_MAPMOVE packet{};
  1871. packet.packetType = HEADER_ZC_NPCACK_MAPMOVE;
  1872. mapindex_getmapname_ext(map_mapid2mapname(m), packet.mapName);
  1873. packet.xPos = x;
  1874. packet.yPos = y;
  1875. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1876. }
  1877. /// Notifies the client of a position change to coordinates on given map, which is on another map-server.
  1878. /// 0092 <map name>.16B <x>.W <y>.W <ip>.L <port>.W (ZC_NPCACK_SERVERMOVE)
  1879. /// 0ac7 <map name>.16B <x>.W <y>.W <ip>.L <port>.W <unknown>.128B (ZC_NPCACK_SERVERMOVE_DOMAIN)
  1880. void clif_changemapserver( map_session_data& sd, const char* map, uint16 x, uint16 y, uint32 ip, uint16 port ){
  1881. PACKET_ZC_NPCACK_SERVERMOVE packet{};
  1882. packet.packetType = HEADER_ZC_NPCACK_SERVERMOVE;
  1883. mapindex_getmapname_ext(map, packet.mapName);
  1884. packet.xPos = x;
  1885. packet.yPos = y;
  1886. packet.ip = htonl(ip);
  1887. packet.port = ntows(htons(port)); // [!] LE byte order here [!]
  1888. #if PACKETVER >= 20170315
  1889. safestrncpy( packet.domain, "", sizeof( packet.domain ) );
  1890. #endif
  1891. #ifdef DEBUG
  1892. ShowDebug( "Sending the client (%d %d.%d.%d.%d) to map-server with ip %d.%d.%d.%d and port %hu\n", sd.status.account_id, CONVIP(session[sd.fd]->client_addr), CONVIP(ip), port );
  1893. #endif
  1894. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1895. }
  1896. /// In many situations (knockback, backslide, etc.) Aegis sends both clif_slide and clif_fixpos
  1897. /// This function combines both calls and allows to simplify the calling code
  1898. void clif_blown(struct block_list *bl)
  1899. {
  1900. clif_slide(*bl, bl->x, bl->y);
  1901. clif_fixpos( *bl );
  1902. }
  1903. /// Visually moves(slides) a character to x,y. If the target cell
  1904. /// isn't walkable, the char doesn't move at all. If the char is
  1905. /// sitting it will stand up.
  1906. /// 0088 <id>.L <x>.W <y>.W (ZC_STOPMOVE)
  1907. void clif_fixpos( block_list& bl ){
  1908. PACKET_ZC_STOPMOVE packet = {};
  1909. packet.packetType = HEADER_ZC_STOPMOVE;
  1910. packet.AID = bl.id;
  1911. packet.xPos = bl.x;
  1912. packet.yPos = bl.y;
  1913. clif_send( &packet, sizeof( packet ), &bl, AREA);
  1914. if( disguised( &bl ) ){
  1915. packet.AID = disguised_bl_id( bl.id );
  1916. clif_send( &packet, sizeof( packet ), &bl, SELF );
  1917. }
  1918. }
  1919. /// Displays the buy/sell dialog of an NPC shop.
  1920. /// 00c4 <shop id>.L (ZC_SELECT_DEALTYPE)
  1921. void clif_npcbuysell( map_session_data& sd, npc_data& nd ){
  1922. PACKET_ZC_SELECT_DEALTYPE packet{};
  1923. packet.packetType = HEADER_ZC_SELECT_DEALTYPE;
  1924. packet.npcId = nd.bl.id;
  1925. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1926. }
  1927. /// Presents list of items, that can be bought in an NPC shop.
  1928. /// 00c6 <packet len>.W { <price>.L <discount price>.L <item type>.B <name id>.W }* (ZC_PC_PURCHASE_ITEMLIST)
  1929. void clif_buylist( map_session_data& sd, npc_data& nd ){
  1930. PACKET_ZC_PC_PURCHASE_ITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_ITEMLIST*>( packet_buffer );
  1931. p->packetType = HEADER_ZC_PC_PURCHASE_ITEMLIST;
  1932. p->packetLength = sizeof( *p );
  1933. for( int32 i = 0, count = 0, discount = npc_shop_discount( &nd ); i < nd.u.shop.count; i++ ){
  1934. int32 val = nd.u.shop.shop_item[i].value;
  1935. p->items[count].price = val;
  1936. p->items[count].discountPrice = ( discount ) ? pc_modifybuyvalue( &sd, val ) : val;
  1937. p->items[count].itemType = itemtype( nd.u.shop.shop_item[i].nameid );
  1938. p->items[count].itemId = client_nameid( nd.u.shop.shop_item[i].nameid );
  1939. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  1940. std::shared_ptr<item_data> id = item_db.find(nd.u.shop.shop_item[i].nameid);
  1941. p->items[count].viewSprite = id->look;
  1942. p->items[count].location = pc_equippoint_sub( &sd, id.get() );
  1943. #endif
  1944. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  1945. count++;
  1946. }
  1947. clif_send( p, p->packetLength, &sd.bl, SELF );
  1948. }
  1949. /// Presents list of items, that can be sold to an NPC shop.
  1950. /// 00c7 <packet len>.W { <index>.W <price>.L <overcharge price>.L }* (ZC_PC_SELL_ITEMLIST)
  1951. void clif_selllist( map_session_data& sd){
  1952. if( !sd.npc_shopid ){
  1953. return;
  1954. }
  1955. npc_data* nd = map_id2nd( sd.npc_shopid );
  1956. if( nd == nullptr ){
  1957. return;
  1958. }
  1959. PACKET_ZC_PC_SELL_ITEMLIST *packet = reinterpret_cast<PACKET_ZC_PC_SELL_ITEMLIST*>(packet_buffer);
  1960. packet->packetType = HEADER_ZC_PC_SELL_ITEMLIST;
  1961. packet->packetLength = sizeof( *packet );
  1962. for( int32 i = 0, c = 0; i < MAX_INVENTORY; i++ ){
  1963. if( sd.inventory.u.items_inventory[i].nameid <= 0 ){
  1964. continue;
  1965. }
  1966. if( sd.inventory_data[i] == nullptr ){
  1967. continue;
  1968. }
  1969. if( !pc_can_sell_item( &sd, &sd.inventory.u.items_inventory[i], nd->subtype ) ){
  1970. continue;
  1971. }
  1972. int32 price;
  1973. if( battle_config.rental_item_novalue && sd.inventory.u.items_inventory[i].expire_time ){
  1974. price = 0;
  1975. }else{
  1976. price = sd.inventory_data[i]->value_sell;
  1977. if( price < 0 ){
  1978. continue;
  1979. }
  1980. }
  1981. packet->items[c].index = client_index(i);
  1982. packet->items[c].price = price;
  1983. packet->items[c].overcharge = pc_modifysellvalue( &sd, price );
  1984. packet->packetLength += sizeof( packet->items[0] );
  1985. c++;
  1986. }
  1987. clif_send( packet, packet->packetLength, &sd.bl, SELF );
  1988. }
  1989. /// Closes shop (CZ_NPC_TRADE_QUIT).
  1990. /// 09d4
  1991. void clif_parse_NPCShopClosed(int32 fd, map_session_data *sd) {
  1992. // TODO: State tracking?
  1993. sd->npc_shopid = 0;
  1994. }
  1995. /**
  1996. * Presents list of items, that can be sold to a Market shop.
  1997. * @author: Ind and Yommy
  1998. **/
  1999. /// 0x9d5 <packet len>.W { <name id>.W <type>.B <price>.L <qty>.L <weight>.W }* (ZC_NPC_MARKET_OPEN)
  2000. void clif_npc_market_open( map_session_data& sd, npc_data& nd ){
  2001. #if PACKETVER >= 20131223
  2002. if( sd.state.trading ){
  2003. return;
  2004. }
  2005. PACKET_ZC_NPC_MARKET_OPEN* p = reinterpret_cast<PACKET_ZC_NPC_MARKET_OPEN*>( packet_buffer );
  2006. p->packetType = HEADER_ZC_NPC_MARKET_OPEN;
  2007. p->packetLength = sizeof( *p );
  2008. for( int32 i = 0, count = 0; i < nd.u.shop.count; i++ ){
  2009. struct npc_item_list *item = &nd.u.shop.shop_item[i];
  2010. if( !item->nameid ){
  2011. continue;
  2012. }
  2013. std::shared_ptr<item_data> id = item_db.find(item->nameid);
  2014. if( !id ){
  2015. continue;
  2016. }
  2017. PACKET_ZC_NPC_MARKET_OPEN_sub& entry = p->list[count];
  2018. entry.nameid = client_nameid( item->nameid );
  2019. entry.type = itemtype( item->nameid );
  2020. entry.price = item->value;
  2021. entry.qty = item->qty;
  2022. entry.weight = id->weight;
  2023. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  2024. entry.location = pc_equippoint_sub( &sd, id.get() );
  2025. #endif
  2026. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  2027. count++;
  2028. }
  2029. clif_send( p, p->packetLength, &sd.bl, SELF );
  2030. sd.state.trading = 1;
  2031. #endif
  2032. }
  2033. /// Closes the Market shop window.
  2034. void clif_parse_NPCMarketClosed(int32 fd, map_session_data *sd) {
  2035. nullpo_retv(sd);
  2036. sd->npc_shopid = 0;
  2037. sd->state.trading = 0;
  2038. }
  2039. /// Purchase item from Market shop.
  2040. /// 0x9d7 <packet len>.W <count>.B { <name id>.W <qty>.W <price>.L }* (ZC_NPC_MARKET_PURCHASE_RESULT)
  2041. void clif_npc_market_purchase_ack( map_session_data& sd, e_purchase_result res, std::vector<s_npc_buy_list>& list ){
  2042. #if PACKETVER >= 20131223
  2043. struct npc_data *nd = map_id2nd( sd.npc_shopid );
  2044. if( nd == nullptr ){
  2045. return;
  2046. }
  2047. PACKET_ZC_NPC_MARKET_PURCHASE_RESULT* p = reinterpret_cast<PACKET_ZC_NPC_MARKET_PURCHASE_RESULT*>( packet_buffer );
  2048. p->PacketType = HEADER_ZC_NPC_MARKET_PURCHASE_RESULT;
  2049. p->PacketLength = sizeof( *p );
  2050. #if PACKETVER_MAIN_NUM >= 20190807 || PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190814
  2051. p->result = ( res == e_purchase_result::PURCHASE_SUCCEED ? 0 : -1 );
  2052. #else
  2053. p->result = ( res == e_purchase_result::PURCHASE_SUCCEED ? 1 : 0 );
  2054. #endif
  2055. if( res == e_purchase_result::PURCHASE_SUCCEED ){
  2056. for( int32 i = 0, j, count = 0; i < list.size(); i++ ){
  2057. ARR_FIND( 0, nd->u.shop.count, j, list[i].nameid == nd->u.shop.shop_item[j].nameid );
  2058. // Not found
  2059. if( j == nd->u.shop.count ){
  2060. continue;
  2061. }
  2062. p->list[count].ITID = client_nameid( list[i].nameid );
  2063. p->list[count].qty = list[i].qty;
  2064. p->list[count].price = nd->u.shop.shop_item[j].value;
  2065. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->list[0] ) );
  2066. count++;
  2067. }
  2068. }
  2069. clif_send( p, p->PacketLength, &sd.bl, SELF );
  2070. #endif
  2071. }
  2072. /// Purchase item from Market shop.
  2073. /// 0x9d6 <len>.W { <name id>.W <qty>.L } (CZ_NPC_MARKET_PURCHASE)
  2074. void clif_parse_NPCMarketPurchase(int32 fd, map_session_data *sd) {
  2075. #if PACKETVER >= 20131223
  2076. if( sd == nullptr ){
  2077. return;
  2078. }
  2079. if( !sd->npc_shopid ){
  2080. return;
  2081. }
  2082. const PACKET_CZ_NPC_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  2083. int32 count = ( p->PacketLength - sizeof( *p ) ) / sizeof( p->list[0] );
  2084. std::vector<s_npc_buy_list> items;
  2085. items.reserve( count );
  2086. for( int32 i = 0; i < count; i++ ){
  2087. s_npc_buy_list item = {};
  2088. item.nameid = p->list[i].ITID;
  2089. item.qty = p->list[i].qty;
  2090. items.push_back( item );
  2091. }
  2092. e_purchase_result res = npc_buylist( sd, items );
  2093. clif_npc_market_purchase_ack( *sd, res, items );
  2094. #endif
  2095. }
  2096. /// Displays an NPC dialog message.
  2097. /// 00b4 <packet len>.W <npc id>.L <message>.?B (ZC_SAY_DIALOG)
  2098. /// Client behavior (dialog window):
  2099. /// - disable mouse targeting
  2100. /// - open the dialog window
  2101. /// - set npcid of dialog window (0 by default)
  2102. /// - if set to clear on next mes, clear contents
  2103. /// - append this text
  2104. void clif_scriptmes( map_session_data& sd, uint32 npcid, const char *mes ){
  2105. PACKET_ZC_SAY_DIALOG* p = reinterpret_cast<PACKET_ZC_SAY_DIALOG*>( packet_buffer );
  2106. int16 length = (int16)( strlen( mes ) + 1 );
  2107. p->PacketType = HEADER_ZC_SAY_DIALOG;
  2108. p->PacketLength = sizeof( *p ) + length;
  2109. p->NpcID = npcid;
  2110. safestrncpy( p->message, mes, length );
  2111. clif_send( p, p->PacketLength, &sd.bl, SELF );
  2112. }
  2113. /// Adds a 'next' button to an NPC dialog.
  2114. /// 00b5 <npc id>.L (ZC_WAIT_DIALOG)
  2115. /// Client behavior (dialog window):
  2116. /// - disable mouse targeting
  2117. /// - open the dialog window
  2118. /// - add 'next' button
  2119. /// When 'next' is pressed:
  2120. /// - 00B9 <npcid of dialog window>.L
  2121. /// - set to clear on next mes
  2122. /// - remove 'next' button
  2123. void clif_scriptnext( map_session_data& sd, uint32 npcid ){
  2124. PACKET_ZC_WAIT_DIALOG p = {};
  2125. p.PacketType = HEADER_ZC_WAIT_DIALOG;
  2126. p.NpcID = npcid;
  2127. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  2128. }
  2129. /// Adds a 'close' button to an NPC dialog.
  2130. /// 00b6 <npc id>.L (ZC_CLOSE_DIALOG)
  2131. /// Client behavior:
  2132. /// - if dialog window is open:
  2133. /// - remove 'next' button
  2134. /// - add 'close' button
  2135. /// - else:
  2136. /// - enable mouse targeting
  2137. /// - close the dialog window
  2138. /// - close the menu window
  2139. /// When 'close' is pressed:
  2140. /// - enable mouse targeting
  2141. /// - close the dialog window
  2142. /// - close the menu window
  2143. /// - 0146 <npcid of dialog window>.L
  2144. void clif_scriptclose( map_session_data& sd, uint32 npcid ){
  2145. PACKET_ZC_CLOSE_DIALOG packet{};
  2146. packet.packetType = HEADER_ZC_CLOSE_DIALOG;
  2147. packet.npcId = npcid;
  2148. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2149. }
  2150. /**
  2151. * Close script when player is idle
  2152. * 08d6 <npc id>.L (ZC_CLEAR_DIALOG)
  2153. * @author [Ind/Hercules]
  2154. * @param sd : player pointer
  2155. * @param npcid : npc gid to close
  2156. */
  2157. void clif_scriptclear( map_session_data& sd, int32 npcid ){
  2158. PACKET_ZC_CLEAR_DIALOG p = {};
  2159. p.packetType = HEADER_ZC_CLEAR_DIALOG;
  2160. p.GID = npcid;
  2161. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  2162. }
  2163. void clif_sendfakenpc( map_session_data& sd, uint32 npcid ){
  2164. sd.state.using_fake_npc = 1;
  2165. // Code below is a copy of clif_set_unit_idle
  2166. struct packet_idle_unit p;
  2167. p.PacketType = idle_unitType;
  2168. #if PACKETVER >= 20091103
  2169. p.PacketLength = sizeof(p);
  2170. p.objecttype = 0x1; // NPC_TYPE
  2171. #endif
  2172. #if PACKETVER >= 20131223
  2173. p.AID = npcid;
  2174. p.GID = 0;
  2175. #else
  2176. p.GID = npcid;
  2177. #endif
  2178. p.speed = 0;
  2179. p.bodyState = 0;
  2180. p.healthState = 0;
  2181. p.effectState = 0;
  2182. p.job = JT_HIDDEN_NPC;
  2183. p.head = 0;
  2184. p.weapon = 0;
  2185. #if PACKETVER < 7 || PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  2186. p.shield = 0;
  2187. #endif
  2188. p.accessory = 0;
  2189. p.accessory2 = 0;
  2190. p.accessory3 = 0;
  2191. p.headpalette = 0;
  2192. p.bodypalette = 0;
  2193. p.headDir = 0;
  2194. #if PACKETVER >= 20101124
  2195. p.robe = 0;
  2196. #endif
  2197. p.GUID = 0;
  2198. p.GEmblemVer = 0;
  2199. p.honor = 0;
  2200. p.virtue = 0;
  2201. p.isPKModeON = 0;
  2202. p.sex = SEX_FEMALE;
  2203. WBUFPOS( &p.PosDir[0], 0, sd.bl.x, sd.bl.y, sd.ud.dir );
  2204. p.xSize = p.ySize = 5;
  2205. p.state = 0;
  2206. p.clevel = 0;
  2207. #if PACKETVER >= 20080102
  2208. p.font = 0;
  2209. #endif
  2210. #if PACKETVER >= 20120221
  2211. p.maxHP = -1;
  2212. p.HP = -1;
  2213. #endif
  2214. #if PACKETVER >= 20150513
  2215. p.body = 0;
  2216. #endif
  2217. /* Might be earlier, this is when the named item bug began */
  2218. #if PACKETVER >= 20131223
  2219. safestrncpy( p.name, "", NAME_LENGTH);
  2220. #endif
  2221. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  2222. }
  2223. /// Displays an NPC dialog menu.
  2224. /// 00b7 <packet len>.W <npc id>.L <menu items>.?B (ZC_MENU_LIST)
  2225. /// Client behavior:
  2226. /// - disable mouse targeting
  2227. /// - close the menu window
  2228. /// - open the menu window
  2229. /// - add options to the menu (separated in the text by ":")
  2230. /// - set npcid of menu window
  2231. /// - if dialog window is open:
  2232. /// - remove 'next' button
  2233. /// When 'ok' is pressed:
  2234. /// - 00B8 <npcid of menu window>.L <selected option>.B
  2235. /// - close the menu window
  2236. /// When 'cancel' is pressed:
  2237. /// - 00B8 <npcid of menu window>.L <-1>.B
  2238. /// - enable mouse targeting
  2239. /// - close a bunch of windows...
  2240. /// WARNING: the 'cancel' button closes other windows besides the dialog window and the menu window.
  2241. /// Which suggests their have intertwined behavior. (probably the mouse targeting)
  2242. /// TODO investigate behavior of other windows [FlavioJS]
  2243. void clif_scriptmenu( map_session_data& sd, uint32 npcid, const char* mes ){
  2244. struct block_list *bl = nullptr;
  2245. if (!sd.state.using_fake_npc && (npcid == fake_nd->bl.id || ((bl = map_id2bl(npcid)) && (bl->m!=sd.bl.m ||
  2246. bl->x<sd.bl.x-AREA_SIZE-1 || bl->x>sd.bl.x+AREA_SIZE+1 ||
  2247. bl->y<sd.bl.y-AREA_SIZE-1 || bl->y>sd.bl.y+AREA_SIZE+1))))
  2248. clif_sendfakenpc( sd, npcid );
  2249. // String length + 1 byte for zero termination
  2250. size_t mes_length = strlen( mes ) + 1;
  2251. PACKET_ZC_MENU_LIST* packet = reinterpret_cast<PACKET_ZC_MENU_LIST*>( packet_buffer );
  2252. packet->packetType = HEADER_ZC_MENU_LIST;
  2253. packet->packetLength = sizeof( PACKET_ZC_MENU_LIST );
  2254. packet->npcId = npcid;
  2255. safestrncpy( packet->menu, mes, mes_length );
  2256. packet->packetLength += static_cast<decltype(packet->packetLength)>( mes_length );
  2257. clif_send( packet, packet->packetLength, &sd.bl, SELF );
  2258. }
  2259. /// Displays an NPC dialog input box for numbers.
  2260. /// 0142 <npc id>.L (ZC_OPEN_EDITDLG)
  2261. /// Client behavior (inputnum window):
  2262. /// - if npcid exists in the client:
  2263. /// - open the inputnum window
  2264. /// - set npcid of inputnum window
  2265. /// When 'ok' is pressed:
  2266. /// - if inputnum window has text:
  2267. /// - if npcid exists in the client:
  2268. /// - 0143 <npcid of inputnum window>.L <atoi(text)>.L
  2269. /// - close inputnum window
  2270. void clif_scriptinput( map_session_data& sd, uint32 npcid ){
  2271. struct block_list *bl = nullptr;
  2272. if (!sd.state.using_fake_npc && (npcid == fake_nd->bl.id || ((bl = map_id2bl(npcid)) && (bl->m!=sd.bl.m ||
  2273. bl->x<sd.bl.x-AREA_SIZE-1 || bl->x>sd.bl.x+AREA_SIZE+1 ||
  2274. bl->y<sd.bl.y-AREA_SIZE-1 || bl->y>sd.bl.y+AREA_SIZE+1))))
  2275. clif_sendfakenpc( sd, npcid );
  2276. PACKET_ZC_OPEN_EDITDLG packet{};
  2277. packet.packetType = HEADER_ZC_OPEN_EDITDLG;
  2278. packet.npcId = npcid;
  2279. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2280. }
  2281. /// Displays an NPC dialog input box for strings.
  2282. /// 01d4 <npc id>.L (ZC_OPEN_EDITDLGSTR)
  2283. /// Client behavior (inputstr window):
  2284. /// - if npcid is 0 or npcid exists in the client:
  2285. /// - open the inputstr window
  2286. /// - set npcid of inputstr window
  2287. /// When 'ok' is pressed:
  2288. /// - if inputstr window has text and isn't an insult(manner.txt):
  2289. /// - if npcid is 0 or npcid exists in the client:
  2290. /// - 01d5 <packetlen>.W <npcid of inputstr window>.L <text>.?B
  2291. /// - close inputstr window
  2292. void clif_scriptinputstr( map_session_data& sd, uint32 npcid ){
  2293. struct block_list *bl = nullptr;
  2294. if (!sd.state.using_fake_npc && (npcid == fake_nd->bl.id || ((bl = map_id2bl(npcid)) && (bl->m!=sd.bl.m ||
  2295. bl->x<sd.bl.x-AREA_SIZE-1 || bl->x>sd.bl.x+AREA_SIZE+1 ||
  2296. bl->y<sd.bl.y-AREA_SIZE-1 || bl->y>sd.bl.y+AREA_SIZE+1))))
  2297. clif_sendfakenpc( sd, npcid );
  2298. PACKET_ZC_OPEN_EDITDLGSTR packet{};
  2299. packet.packetType = HEADER_ZC_OPEN_EDITDLGSTR;
  2300. packet.npcId = npcid;
  2301. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2302. }
  2303. /// Marks a position on client's minimap.
  2304. /// 0144 <npc id>.L <type>.L <x>.L <y>.L <id>.B <color>.L (ZC_COMPASS)
  2305. /// npc id:
  2306. /// is ignored in the client
  2307. /// type:
  2308. /// 0 = display mark for 15 seconds
  2309. /// 1 = display mark until dead or teleported
  2310. /// 2 = remove mark
  2311. /// color:
  2312. /// 0x00RRGGBB
  2313. void clif_viewpoint( map_session_data& sd, uint32 npc_id, int32 type, uint16 x, uint16 y, int32 id, uint32 color ){
  2314. PACKET_ZC_COMPASS packet{};
  2315. packet.packetType = HEADER_ZC_COMPASS;
  2316. packet.npcId = npc_id;
  2317. packet.type = type;
  2318. packet.xPos = x;
  2319. packet.yPos = y;
  2320. packet.id = id;
  2321. packet.color = color;
  2322. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2323. }
  2324. /// Displays an illustration image.
  2325. /// 0145 <image name>.16B <type>.B (ZC_SHOW_IMAGE)
  2326. /// 01b3 <image name>.64B <type>.B (ZC_SHOW_IMAGE2)
  2327. /// type:
  2328. /// 0 = bottom left corner
  2329. /// 1 = bottom middle
  2330. /// 2 = bottom right corner
  2331. /// 3 = middle of screen, inside a movable window
  2332. /// 4 = middle of screen, movable with a close button, chrome-less
  2333. /// 255 = clear all displayed cutins
  2334. void clif_cutin( map_session_data& sd, const char* image, int32 type ){
  2335. PACKET_ZC_SHOW_IMAGE packet{};
  2336. packet.packetType = HEADER_ZC_SHOW_IMAGE;
  2337. safestrncpy(packet.image, image, sizeof(packet.image));
  2338. packet.type = type;
  2339. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2340. }
  2341. /*==========================================
  2342. * Fills in card data from the given item and into the buffer. [Skotlex]
  2343. *------------------------------------------*/
  2344. static void clif_addcards( struct EQUIPSLOTINFO* buf, struct item* item ){
  2345. nullpo_retv( buf );
  2346. // Blank data
  2347. if( item == nullptr ){
  2348. buf->card[0] = 0;
  2349. buf->card[1] = 0;
  2350. buf->card[2] = 0;
  2351. buf->card[3] = 0;
  2352. return;
  2353. }
  2354. // Pet eggs
  2355. if( item->card[0] == CARD0_PET ){
  2356. buf->card[0] = 0;
  2357. buf->card[1] = 0;
  2358. // Pet intimacy
  2359. // No idea when this was added exactly, but older clients have no problem if we send it anyway
  2360. buf->card[2] = item->card[3] >> 1;
  2361. // Pet renamed flag
  2362. buf->card[3] = item->card[3] & 1;
  2363. return;
  2364. }
  2365. // Forged/created items
  2366. if( item->card[0] == CARD0_FORGE || item->card[0] == CARD0_CREATE ){
  2367. buf->card[0] = item->card[0];
  2368. buf->card[1] = item->card[1];
  2369. buf->card[2] = item->card[2];
  2370. buf->card[3] = item->card[3];
  2371. return;
  2372. }
  2373. int32 i = 0, j;
  2374. // Client only receives four cards.. so randomly send them a set of cards. [Skotlex]
  2375. if( MAX_SLOTS > 4 && ( j = itemdb_slots( item->nameid ) ) > 4 ){
  2376. i = rnd() % ( j - 3 ); //eg: 6 slots, possible i values: 0->3, 1->4, 2->5 => i = rnd()%3;
  2377. }
  2378. // Normal items.
  2379. for( int32 k = 0; k < 4; k++, i++ ){
  2380. if( item->card[i] > 0 ){
  2381. buf->card[k] = client_nameid( item->card[i] );
  2382. }else{
  2383. buf->card[k] = 0;
  2384. }
  2385. }
  2386. }
  2387. /// Fills in part of the item buffers that calls for variable bonuses data. [Napster]
  2388. /// A maximum of 5 random options can be supported.
  2389. static void clif_add_random_options( struct ItemOptions buf[MAX_ITEM_RDM_OPT], struct item& it ){
  2390. for( int32 i = 0; i < MAX_ITEM_RDM_OPT; i++ ){
  2391. if( it.option[i].id ){
  2392. buf[i].index = it.option[i].id; // OptIndex
  2393. buf[i].value = it.option[i].value; // Value
  2394. buf[i].param = it.option[i].param; // Param1
  2395. }else{
  2396. buf[i].index = 0;
  2397. buf[i].value = 0;
  2398. buf[i].param = 0;
  2399. }
  2400. }
  2401. #if MAX_ITEM_RDM_OPT < 5
  2402. for( ; i < 5; i++ ){
  2403. buf[i].index = 0; // OptIndex
  2404. buf[i].value = 0; // Value
  2405. buf[i].param = 0; // Param1
  2406. }
  2407. #endif
  2408. }
  2409. /// Notifies the client, about a received inventory item or the result of a pick-up request.
  2410. /// 00a0 <index>.W <amount>.W <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W <equip location>.W <item type>.B <result>.B (ZC_ITEM_PICKUP_ACK)
  2411. /// 029a <index>.W <amount>.W <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W <equip location>.W <item type>.B <result>.B <expire time>.L (ZC_ITEM_PICKUP_ACK2)
  2412. /// 02d4 <index>.W <amount>.W <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W <equip location>.W <item type>.B <result>.B <expire time>.L <bindOnEquipType>.W (ZC_ITEM_PICKUP_ACK3)
  2413. /// 0990 <index>.W <amount>.W <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W <equip location>.L <item type>.B <result>.B <expire time>.L <bindOnEquipType>.W (ZC_ITEM_PICKUP_ACK_V5)
  2414. /// 0a0c <index>.W <amount>.W <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W <equip location>.L <item type>.B <result>.B <expire time>.L <bindOnEquipType>.W { <option id>.W <option value>.W <option param>.B }*5 (ZC_ITEM_PICKUP_ACK_V6)
  2415. /// 0a37 <index>.W <amount>.W <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W <equip location>.L <item type>.B <result>.B <expire time>.L <bindOnEquipType>.W { <option id>.W <option value>.W <option param>.B }*5 <favorite>.B <view id>.W (ZC_ITEM_PICKUP_ACK_V7)
  2416. void clif_additem( map_session_data *sd, int32 n, int32 amount, unsigned char fail ){
  2417. nullpo_retv( sd );
  2418. int32 fd = sd->fd;
  2419. if( !session_isActive( fd ) ){
  2420. return;
  2421. }
  2422. PACKET_ZC_ITEM_PICKUP_ACK p = {};
  2423. if( fail ){
  2424. p = {};
  2425. }else{
  2426. if( n < 0 || n >= MAX_INVENTORY || sd->inventory.u.items_inventory[n].nameid == 0 || sd->inventory_data[n] == nullptr ){
  2427. return;
  2428. }
  2429. p.nameid = client_nameid( sd->inventory.u.items_inventory[n].nameid );
  2430. p.IsIdentified = sd->inventory.u.items_inventory[n].identify ? 1 : 0;
  2431. p.IsDamaged = ( sd->inventory.u.items_inventory[n].attribute ) != 0 ? 1 : 0;
  2432. p.refiningLevel =sd->inventory.u.items_inventory[n].refine;
  2433. clif_addcards( &p.slot, &sd->inventory.u.items_inventory[n] );
  2434. p.location = pc_equippoint(sd,n);
  2435. p.type = itemtype( sd->inventory.u.items_inventory[n].nameid );
  2436. #if PACKETVER >= 20061218
  2437. p.HireExpireDate = sd->inventory.u.items_inventory[n].expire_time;
  2438. #if PACKETVER >= 20071002
  2439. /* why restrict the flag to non-stackable? because this is the only packet allows stackable to,
  2440. * show the color, and therefore it'd be inconsistent with the rest (aka it'd show yellow, you relog/refresh and boom its gone)
  2441. */
  2442. p.bindOnEquipType = sd->inventory.u.items_inventory[n].bound && !itemdb_isstackable2( sd->inventory_data[n] ) ? 2 : sd->inventory_data[n]->flag.bindOnEquip ? 1 : 0;
  2443. #if PACKETVER >= 20150226
  2444. clif_add_random_options( p.option_data, sd->inventory.u.items_inventory[n] );
  2445. #if PACKETVER >= 20160921
  2446. p.favorite = ( sd->inventory.u.items_inventory[n].favorite != 0 ) ? 1 : 0;
  2447. p.look = sd->inventory_data[n]->look;
  2448. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  2449. p.grade = sd->inventory.u.items_inventory[n].enchantgrade;
  2450. #endif
  2451. #endif
  2452. #endif
  2453. #endif
  2454. #endif
  2455. }
  2456. p.PacketType = HEADER_ZC_ITEM_PICKUP_ACK;
  2457. p.Index = client_index( n );
  2458. p.count = amount;
  2459. p.result = fail;
  2460. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  2461. }
  2462. /// Notifies the client, that an inventory item was deleted or dropped.
  2463. /// 00af <index>.W <amount>.W (ZC_ITEM_THROW_ACK)
  2464. void clif_dropitem( map_session_data& sd, int32 index, int32 amount ){
  2465. PACKET_ZC_ITEM_THROW_ACK packet{};
  2466. packet.packetType = HEADER_ZC_ITEM_THROW_ACK;
  2467. packet.index = client_index( index );
  2468. packet.count = amount;
  2469. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2470. }
  2471. /// Notifies the client, that an inventory item was deleted.
  2472. /// 07fa <delete type>.W <index>.W <amount>.W (ZC_DELETE_ITEM_FROM_BODY)
  2473. /// delete type:
  2474. /// 0 = Normal
  2475. /// 1 = Item used for a skill
  2476. /// 2 = Refine failed
  2477. /// 3 = Material changed
  2478. /// 4 = Moved to storage
  2479. /// 5 = Moved to cart
  2480. /// 6 = Item sold
  2481. /// 7 = Consumed by Four Spirit Analysis (SO_EL_ANALYSIS) skill
  2482. void clif_delitem( map_session_data& sd, int32 index, int32 amount, int16 reason ){
  2483. #if PACKETVER >= 20091117
  2484. PACKET_ZC_DELETE_ITEM_FROM_BODY packet{};
  2485. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_BODY;
  2486. packet.deleteType = reason;
  2487. packet.index = client_index( index );
  2488. packet.count = amount;
  2489. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2490. #else
  2491. clif_dropitem( sd, index, amount );
  2492. #endif
  2493. }
  2494. static void clif_item_equip( int16 idx, struct EQUIPITEM_INFO *p, struct item *it, struct item_data *id, int32 eqp_pos ){
  2495. nullpo_retv( p );
  2496. nullpo_retv( it );
  2497. nullpo_retv( id );
  2498. p->index = idx;
  2499. p->ITID = client_nameid( it->nameid );
  2500. p->type = itemtype( it->nameid );
  2501. #if PACKETVER < 20120925
  2502. p->IsIdentified = it->identify ? 1 : 0;
  2503. #endif
  2504. p->location = eqp_pos;
  2505. p->WearState = it->equip;
  2506. #if PACKETVER < 20120925
  2507. p->IsDamaged = it->attribute != 0 ? 1 : 0;
  2508. #endif
  2509. p->RefiningLevel = it->refine;
  2510. clif_addcards( &p->slot, it );
  2511. #if PACKETVER >= 20071002
  2512. p->HireExpireDate = it->expire_time;
  2513. #endif
  2514. #if PACKETVER >= 20080102
  2515. p->bindOnEquipType = it->bound ? 2 : id->flag.bindOnEquip ? 1 : 0;
  2516. #endif
  2517. #if PACKETVER >= 20100629
  2518. // TODO: WBUFW(buf,n+8) = (equip == -2 && id->equip == EQP_AMMO) ? id->equip : 0;
  2519. p->wItemSpriteNumber = ( id->equip&EQP_VISIBLE ) ? id->look : 0;
  2520. #endif
  2521. #if PACKETVER >= 20120925
  2522. p->Flag.IsIdentified = it->identify ? 1 : 0;
  2523. p->Flag.IsDamaged = it->attribute ? 1 : 0;
  2524. p->Flag.PlaceETCTab = it->favorite ? 1 : 0;
  2525. p->Flag.SpareBits = 0;
  2526. #endif
  2527. #if PACKETVER >= 20150226
  2528. clif_add_random_options( p->option_data, *it );
  2529. // Client ingores the value anyway and recalculates it
  2530. p->option_count = 0;
  2531. #endif
  2532. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  2533. p->grade = it->enchantgrade;
  2534. #endif
  2535. }
  2536. static void clif_item_normal( int16 idx, struct NORMALITEM_INFO *p, struct item *i, struct item_data *id ){
  2537. nullpo_retv(p);
  2538. nullpo_retv(i);
  2539. nullpo_retv(id);
  2540. p->index = idx;
  2541. p->ITID = client_nameid( i->nameid );
  2542. p->type = itemtype( i->nameid );
  2543. #if PACKETVER < 20120925
  2544. p->IsIdentified = i->identify ? 1 : 0;
  2545. #endif
  2546. p->count = i->amount;
  2547. p->WearState = id->equip;
  2548. #if PACKETVER >= 5
  2549. clif_addcards( &p->slot, i );
  2550. #endif
  2551. #if PACKETVER >= 20080102
  2552. p->HireExpireDate = i->expire_time;
  2553. #endif
  2554. #if PACKETVER >= 20120925
  2555. p->Flag.IsIdentified = i->identify ? 1 : 0;
  2556. p->Flag.PlaceETCTab = ( i->favorite != 0 ) ? 1 : 0;
  2557. p->Flag.SpareBits = 0;
  2558. #endif
  2559. }
  2560. static void clif_inventoryStart( map_session_data*sd, e_inventory_type type, const char *name ){
  2561. #if PACKETVER_RE_NUM >= 20180829 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2562. nullpo_retv(sd);
  2563. nullpo_retv(name);
  2564. char buf[sizeof(PACKET_ZC_INVENTORY_START) + 24];
  2565. memset(buf, 0, sizeof(buf));
  2566. PACKET_ZC_INVENTORY_START *p = (PACKET_ZC_INVENTORY_START *)buf;
  2567. p->packetType = HEADER_ZC_INVENTORY_START;
  2568. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2569. p->invType = type;
  2570. #endif
  2571. #if PACKETVER_RE_NUM >= 20180919 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2572. int32 strLen = (int32)safestrnlen(name, 24) + 1;
  2573. if (strLen > 24)
  2574. strLen = 24;
  2575. const int32 len = sizeof(PACKET_ZC_INVENTORY_START) + strLen;
  2576. p->packetLength = len;
  2577. safestrncpy(p->name, name, strLen);
  2578. #else
  2579. const int32 len = sizeof(PACKET_ZC_INVENTORY_START);
  2580. safestrncpy(p->name, name, NAME_LENGTH);
  2581. #endif
  2582. clif_send( p, len, &sd->bl, SELF );
  2583. #endif
  2584. }
  2585. static void clif_inventoryEnd( map_session_data *sd, e_inventory_type type ){
  2586. #if PACKETVER_RE_NUM >= 20180829 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2587. nullpo_retv(sd);
  2588. PACKET_ZC_INVENTORY_END p = {};
  2589. p.packetType = HEADER_ZC_INVENTORY_END;
  2590. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2591. p.invType = type;
  2592. #endif
  2593. p.flag = 0;
  2594. clif_send( &p, sizeof(p), &sd->bl, SELF );
  2595. #endif
  2596. }
  2597. //Unified inventory function which sends all of the inventory (requires two packets, one for equipable items and one for stackable ones. [Skotlex]
  2598. void clif_inventorylist( map_session_data *sd ){
  2599. nullpo_retv( sd );
  2600. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2601. e_inventory_type type = INVTYPE_INVENTORY;
  2602. clif_inventoryStart( sd, type, "" );
  2603. #endif
  2604. static struct packet_itemlist_normal itemlist_normal;
  2605. static struct packet_itemlist_equip itemlist_equip;
  2606. int32 equip = 0;
  2607. int32 normal = 0;
  2608. storage_sortitem( sd->storage.u.items_inventory, ARRAYLENGTH( sd->storage.u.items_inventory ) );
  2609. for( int32 i = 0; i < MAX_INVENTORY; i++ ){
  2610. if( sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr ){
  2611. continue;
  2612. }
  2613. // Non-stackable (Equippable)
  2614. if( !itemdb_isstackable2( sd->inventory_data[i] ) ){
  2615. clif_item_equip( client_index( i ), &itemlist_equip.list[equip++], &sd->inventory.u.items_inventory[i], sd->inventory_data[i], pc_equippoint( sd, i ) );
  2616. if( equip == MAX_INVENTORY_ITEM_PACKET_NORMAL ){
  2617. itemlist_equip.PacketType = inventorylistequipType;
  2618. itemlist_equip.PacketLength = static_cast<decltype(itemlist_equip.PacketLength)>( ( sizeof( itemlist_equip ) - sizeof( itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2619. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2620. itemlist_equip.invType = type;
  2621. #endif
  2622. clif_send( &itemlist_equip, itemlist_equip.PacketLength, &sd->bl, SELF );
  2623. equip = 0;
  2624. }
  2625. // Stackable (Normal)
  2626. }else{
  2627. clif_item_normal( client_index( i ), &itemlist_normal.list[normal++], &sd->inventory.u.items_inventory[i], sd->inventory_data[i] );
  2628. if( normal == MAX_INVENTORY_ITEM_PACKET_NORMAL ){
  2629. itemlist_normal.PacketType = inventorylistnormalType;
  2630. itemlist_normal.PacketLength = static_cast<decltype(itemlist_normal.PacketLength)>( ( sizeof( itemlist_normal ) - sizeof( itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2631. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2632. itemlist_normal.invType = type;
  2633. #endif
  2634. clif_send( &itemlist_normal, itemlist_normal.PacketLength, &sd->bl, SELF );
  2635. normal = 0;
  2636. }
  2637. }
  2638. }
  2639. if( normal ){
  2640. itemlist_normal.PacketType = inventorylistnormalType;
  2641. itemlist_normal.PacketLength = static_cast<decltype(itemlist_normal.PacketLength)>( ( sizeof( itemlist_normal ) - sizeof( itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2642. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2643. itemlist_normal.invType = type;
  2644. #endif
  2645. clif_send( &itemlist_normal, itemlist_normal.PacketLength, &sd->bl, SELF );
  2646. }
  2647. if( sd->equip_index[EQI_AMMO] >= 0 )
  2648. clif_arrowequip( *sd );
  2649. if( equip ) {
  2650. itemlist_equip.PacketType = inventorylistequipType;
  2651. itemlist_equip.PacketLength = static_cast<decltype(itemlist_equip.PacketLength)>( ( sizeof( itemlist_equip ) - sizeof( itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2652. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2653. itemlist_equip.invType = type;
  2654. #endif
  2655. clif_send( &itemlist_equip, itemlist_equip.PacketLength, &sd->bl, SELF );
  2656. }
  2657. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2658. clif_inventoryEnd( sd, type );
  2659. #endif
  2660. /* on 20120925 onwards this is a field on clif_item_equip/normal */
  2661. #if PACKETVER >= 20111122 && PACKETVER < 20120925
  2662. for( int32 i = 0; i < MAX_INVENTORY; i++ ){
  2663. if( sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr )
  2664. continue;
  2665. if ( sd->inventory.u.items_inventory[i].favorite != 0 )
  2666. clif_favorite_item( *sd, i );
  2667. }
  2668. #endif
  2669. }
  2670. //Required when items break/get-repaired. Only sends equippable item list.
  2671. void clif_equiplist( map_session_data *sd ){
  2672. // TODO: implement again => only send equip part
  2673. clif_inventorylist( sd );
  2674. }
  2675. void clif_storagelist(map_session_data* sd, struct item* items, int32 items_length, const char *storename){
  2676. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2677. e_inventory_type type = INVTYPE_STORAGE;
  2678. clif_inventoryStart( sd, type, storename );
  2679. #endif
  2680. static struct ZC_STORE_ITEMLIST_NORMAL storage_itemlist_normal;
  2681. static struct ZC_STORE_ITEMLIST_EQUIP storage_itemlist_equip;
  2682. int32 equip = 0;
  2683. int32 normal = 0;
  2684. for( int32 i = 0; i < items_length; i++ ){
  2685. if( items[i].nameid == 0 ){
  2686. continue;
  2687. }
  2688. struct item_data* id = itemdb_search( items[i].nameid );
  2689. // Non-stackable (Equippable)
  2690. if( !itemdb_isstackable2( id ) ){
  2691. clif_item_equip( client_storage_index( i ), &storage_itemlist_equip.list[equip++], &items[i], id, pc_equippoint_sub( sd, id ) );
  2692. if( equip == MAX_STORAGE_ITEM_PACKET_EQUIP ){
  2693. storage_itemlist_equip.PacketType = storageListEquipType;
  2694. storage_itemlist_equip.PacketLength = static_cast<decltype(storage_itemlist_equip.PacketLength)>( ( sizeof( storage_itemlist_equip ) - sizeof( storage_itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2695. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2696. storage_itemlist_equip.invType = type;
  2697. #elif PACKETVER >= 20120925
  2698. safestrncpy( storage_itemlist_equip.name, storename, NAME_LENGTH );
  2699. #endif
  2700. clif_send( &storage_itemlist_equip, storage_itemlist_equip.PacketLength, &sd->bl, SELF );
  2701. equip = 0;
  2702. }
  2703. // Stackable (Normal)
  2704. }else{
  2705. clif_item_normal( client_storage_index( i ), &storage_itemlist_normal.list[normal++], &items[i], id );
  2706. if( normal == MAX_STORAGE_ITEM_PACKET_NORMAL ){
  2707. storage_itemlist_normal.PacketType = storageListNormalType;
  2708. storage_itemlist_normal.PacketLength = static_cast<decltype(storage_itemlist_normal.PacketLength)>( ( sizeof( storage_itemlist_normal ) - sizeof( storage_itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2709. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2710. storage_itemlist_normal.invType = type;
  2711. #elif PACKETVER >= 20120925
  2712. safestrncpy( storage_itemlist_normal.name, storename, NAME_LENGTH );
  2713. #endif
  2714. clif_send( &storage_itemlist_normal, storage_itemlist_normal.PacketLength, &sd->bl, SELF );
  2715. normal = 0;
  2716. }
  2717. }
  2718. }
  2719. if( normal ){
  2720. storage_itemlist_normal.PacketType = storageListNormalType;
  2721. storage_itemlist_normal.PacketLength = static_cast<decltype(storage_itemlist_normal.PacketLength)>( ( sizeof( storage_itemlist_normal ) - sizeof( storage_itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2722. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2723. storage_itemlist_normal.invType = type;
  2724. #elif PACKETVER >= 20120925
  2725. safestrncpy( storage_itemlist_normal.name, storename, NAME_LENGTH );
  2726. #endif
  2727. clif_send( &storage_itemlist_normal, storage_itemlist_normal.PacketLength, &sd->bl, SELF );
  2728. }
  2729. if( equip ) {
  2730. storage_itemlist_equip.PacketType = storageListEquipType;
  2731. storage_itemlist_equip.PacketLength = static_cast<decltype(storage_itemlist_equip.PacketLength)>( ( sizeof( storage_itemlist_equip ) - sizeof( storage_itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2732. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2733. storage_itemlist_equip.invType = type;
  2734. #elif PACKETVER >= 20120925
  2735. safestrncpy( storage_itemlist_equip.name, storename, NAME_LENGTH );
  2736. #endif
  2737. clif_send( &storage_itemlist_equip, storage_itemlist_equip.PacketLength, &sd->bl, SELF );
  2738. }
  2739. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2740. clif_inventoryEnd( sd, type );
  2741. #endif
  2742. }
  2743. void clif_cartlist( map_session_data *sd ){
  2744. nullpo_retv( sd );
  2745. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2746. e_inventory_type type = INVTYPE_CART;
  2747. clif_inventoryStart( sd, type, "" );
  2748. #endif
  2749. static struct packet_itemlist_normal itemlist_normal;
  2750. static struct packet_itemlist_equip itemlist_equip;
  2751. int32 normal = 0;
  2752. int32 equip = 0;
  2753. storage_sortitem( sd->storage.u.items_cart, ARRAYLENGTH( sd->storage.u.items_cart ) );
  2754. for( int32 i = 0; i < MAX_CART; i++ ){
  2755. if( sd->cart.u.items_cart[i].nameid == 0 ){
  2756. continue;
  2757. }
  2758. struct item_data* id = itemdb_search( sd->cart.u.items_cart[i].nameid );
  2759. // Non-stackable (Equippable)
  2760. if( !itemdb_isstackable2(id) ){
  2761. clif_item_equip( client_index( i ), &itemlist_equip.list[equip++], &sd->cart.u.items_cart[i], id, id->equip );
  2762. // Stackable (Normal)
  2763. }else{
  2764. clif_item_normal( client_index( i ), &itemlist_normal.list[normal++], &sd->cart.u.items_cart[i], id );
  2765. }
  2766. }
  2767. if( normal ){
  2768. itemlist_normal.PacketType = cartlistnormalType;
  2769. itemlist_normal.PacketLength = static_cast<decltype(itemlist_normal.PacketLength)>( ( sizeof( itemlist_normal ) - sizeof( itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2770. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2771. itemlist_normal.invType = type;
  2772. #endif
  2773. clif_send( &itemlist_normal, itemlist_normal.PacketLength, &sd->bl, SELF );
  2774. }
  2775. if( equip ){
  2776. itemlist_equip.PacketType = cartlistequipType;
  2777. itemlist_equip.PacketLength = static_cast<decltype(itemlist_equip.PacketLength)>( ( sizeof( itemlist_equip ) - sizeof( itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2778. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2779. itemlist_equip.invType = type;
  2780. #endif
  2781. clif_send( &itemlist_equip, itemlist_equip.PacketLength, &sd->bl, SELF );
  2782. }
  2783. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2784. clif_inventoryEnd( sd, type );
  2785. #endif
  2786. }
  2787. /// Removes cart.
  2788. /// 012b (ZC_CARTOFF)
  2789. /// Client behaviour:
  2790. /// Closes the cart storage and removes all it's items from memory.
  2791. /// The Num & Weight values of the cart are left untouched and the cart is NOT removed.
  2792. void clif_clearcart(int32 fd)
  2793. {
  2794. PACKET_ZC_CARTOFF packet{};
  2795. packet.packetType = HEADER_ZC_CARTOFF;
  2796. socket_send<PACKET_ZC_CARTOFF>(fd, packet);
  2797. }
  2798. /// Sends XY location to all other guild members
  2799. /// 01eb <account id>.L <x>.W <y>.W (ZC_NOTIFY_POSITION_TO_GUILDM)
  2800. void clif_guild_xy( map_session_data& sd ){
  2801. PACKET_ZC_NOTIFY_POSITION_TO_GUILDM packet{};
  2802. packet.packetType = HEADER_ZC_NOTIFY_POSITION_TO_GUILDM;
  2803. packet.aid = sd.status.account_id;
  2804. packet.xPos = sd.bl.x;
  2805. packet.yPos = sd.bl.y;
  2806. clif_send(&packet, sizeof(packet), &sd.bl, GUILD_SAMEMAP_WOS);
  2807. }
  2808. /// Sends XY location to a specific guild member
  2809. /// 01eb <account id>.L <x>.W <y>.W (ZC_NOTIFY_POSITION_TO_GUILDM)
  2810. void clif_guild_xy_single( map_session_data& sd, map_session_data& tsd ){
  2811. if( sd.bg_id ){
  2812. return;
  2813. }
  2814. PACKET_ZC_NOTIFY_POSITION_TO_GUILDM packet{};
  2815. packet.packetType = HEADER_ZC_NOTIFY_POSITION_TO_GUILDM;
  2816. packet.aid = sd.status.account_id;
  2817. packet.xPos = sd.bl.x;
  2818. packet.yPos = sd.bl.y;
  2819. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  2820. }
  2821. /// Removes XY location from all other guild members
  2822. /// 01eb <account id>.L <x>.W <y>.W (ZC_NOTIFY_POSITION_TO_GUILDM)
  2823. void clif_guild_xy_remove( map_session_data& sd ){
  2824. PACKET_ZC_NOTIFY_POSITION_TO_GUILDM packet{};
  2825. packet.packetType = HEADER_ZC_NOTIFY_POSITION_TO_GUILDM;
  2826. packet.aid = sd.status.account_id;
  2827. packet.xPos = -1;
  2828. packet.yPos = -1;
  2829. clif_send( &packet, sizeof( packet ), &sd.bl, GUILD_SAMEMAP_WOS );
  2830. }
  2831. /*==========================================
  2832. * Load castle list for guild UI. [Asheraf] / [Balfear]
  2833. *------------------------------------------*/
  2834. void clif_guild_castle_list(map_session_data& sd){
  2835. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190717 || PACKETVER_ZERO_NUM >= 20190814
  2836. auto &g = sd.guild;
  2837. if (g == nullptr)
  2838. return;
  2839. int32 castle_count = guild_checkcastles(g->guild);
  2840. if( castle_count <= 0 ){
  2841. return;
  2842. }
  2843. PACKET_ZC_GUILD_AGIT_INFO* p = reinterpret_cast<PACKET_ZC_GUILD_AGIT_INFO*>( packet_buffer );
  2844. p->packetType = HEADER_ZC_GUILD_AGIT_INFO;
  2845. p->packetLength = sizeof( *p );
  2846. int32 i = 0;
  2847. for (const auto& gc : castle_db) {
  2848. if (gc.second->guild_id == g->guild.guild_id && gc.second->client_id) {
  2849. p->castle_list[i] = static_cast<int8>( gc.second->client_id );
  2850. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->castle_list[0] ) );
  2851. ++i;
  2852. }
  2853. }
  2854. clif_send(p, p->packetLength, &sd.bl, SELF);
  2855. #endif
  2856. }
  2857. /*==========================================
  2858. * Send castle info Economy/Defence. [Asheraf] / [Balfear]
  2859. *------------------------------------------*/
  2860. void clif_guild_castleinfo(map_session_data& sd, std::shared_ptr<guild_castle> castle ){
  2861. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190717 || PACKETVER_ZERO_NUM >= 20190814
  2862. if( castle->client_id == 0 ){
  2863. return;
  2864. }
  2865. PACKET_ZC_REQ_ACK_AGIT_INVESTMENT p = {};
  2866. p.packetType = HEADER_ZC_REQ_ACK_AGIT_INVESTMENT;
  2867. p.castle_id = static_cast<int8>( castle->client_id );
  2868. p.economy = castle->economy;
  2869. p.defense = castle->defense;
  2870. clif_send(&p, sizeof(p), &sd.bl, SELF);
  2871. #endif
  2872. }
  2873. /*==========================================
  2874. * Show teleport request result. [Asheraf] / [Balfear]
  2875. *------------------------------------------*/
  2876. void clif_guild_castle_teleport_res(map_session_data& sd, enum e_siege_teleport_result result){
  2877. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190717 || PACKETVER_ZERO_NUM >= 20190814
  2878. PACKET_ZC_REQ_ACK_MOVE_GUILD_AGIT p = {};
  2879. p.packetType = HEADER_ZC_REQ_ACK_MOVE_GUILD_AGIT;
  2880. p.result = static_cast<int16>( result );
  2881. clif_send(&p, sizeof(p), &sd.bl, SELF);
  2882. #endif
  2883. }
  2884. /*==========================================
  2885. * Request castle info. [Asheraf] / [Balfear]
  2886. *------------------------------------------*/
  2887. void clif_parse_guild_castle_info_request(int32 fd, map_session_data* sd){
  2888. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190522 || PACKETVER_ZERO_NUM >= 20190515
  2889. if( sd == nullptr ){
  2890. return;
  2891. }
  2892. auto &g = sd->guild;
  2893. if (g == nullptr)
  2894. return;
  2895. const PACKET_CZ_REQ_AGIT_INVESTMENT* p = reinterpret_cast<PACKET_CZ_REQ_AGIT_INVESTMENT*>( RFIFOP( fd, 0 ) );
  2896. std::shared_ptr<guild_castle> gc = castle_db.find_by_clientid( p->castle_id );
  2897. if (gc == nullptr)
  2898. return;
  2899. if (gc->guild_id != g->guild.guild_id)
  2900. return;
  2901. clif_guild_castleinfo(*sd, gc);
  2902. #endif
  2903. }
  2904. /*==========================================
  2905. * Teleport to castle. [Asheraf] / [Balfear]
  2906. *------------------------------------------*/
  2907. void clif_parse_guild_castle_teleport_request(int32 fd, map_session_data* sd){
  2908. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190522 || PACKETVER_ZERO_NUM >= 20190515
  2909. const PACKET_CZ_REQ_MOVE_GUILD_AGIT* p = reinterpret_cast<PACKET_CZ_REQ_MOVE_GUILD_AGIT*>RFIFOP( fd, 0 );
  2910. auto &g = sd->guild;
  2911. if (g == nullptr)
  2912. return;
  2913. std::shared_ptr<guild_castle> gc = castle_db.find_by_clientid( p->castle_id );
  2914. if (gc == nullptr)
  2915. return;
  2916. if (!gc->warp_enabled)
  2917. return;
  2918. if (gc->guild_id != g->guild.guild_id)
  2919. return;
  2920. if (map_getmapflag(sd->bl.m, MF_GVG_CASTLE)
  2921. || map_getmapflag(sd->bl.m, MF_NOTELEPORT)
  2922. || map_getmapflag(sd->bl.m, MF_NOWARP))
  2923. return;
  2924. uint32 zeny = gc->zeny;
  2925. switch( gc->type ){
  2926. case WOE_FIRST_EDITION:
  2927. if( agit_flag ){
  2928. zeny = gc->zeny_siege;
  2929. }
  2930. break;
  2931. case WOE_SECOND_EDITION:
  2932. if( agit2_flag ){
  2933. zeny = gc->zeny_siege;
  2934. }
  2935. break;
  2936. case WOE_THIRD_EDITION:
  2937. if( agit3_flag ){
  2938. zeny = gc->zeny_siege;
  2939. }
  2940. break;
  2941. }
  2942. if (zeny && pc_payzeny(sd, zeny, LOG_TYPE_OTHER)) {
  2943. clif_guild_castle_teleport_res(*sd, SIEGE_TP_NOT_ENOUGH_ZENY);
  2944. return;
  2945. }
  2946. pc_setpos(sd, gc->mapindex, gc->warp_x, gc->warp_y, CLR_OUTSIGHT);
  2947. #endif
  2948. }
  2949. /*==========================================
  2950. *
  2951. *------------------------------------------*/
  2952. static int32 clif_hpmeter_sub( struct block_list *bl, va_list ap ){
  2953. map_session_data* sd = va_arg( ap, map_session_data* );
  2954. map_session_data* tsd = BL_CAST( BL_PC, bl );
  2955. nullpo_ret(sd);
  2956. nullpo_ret(tsd);
  2957. if( pc_has_permission( tsd, PC_PERM_VIEW_HPMETER ) ){
  2958. clif_hpmeter_single( *tsd, sd->status.account_id, sd->battle_status.hp, sd->battle_status.max_hp );
  2959. }
  2960. return 0;
  2961. }
  2962. /*==========================================
  2963. * Server tells all players that are allowed to view HP bars
  2964. * and are nearby 'sd' that 'sd' hp bar was updated.
  2965. *------------------------------------------*/
  2966. static int32 clif_hpmeter(map_session_data *sd)
  2967. {
  2968. nullpo_ret(sd);
  2969. map_foreachinallarea(clif_hpmeter_sub, sd->bl.m, sd->bl.x-AREA_SIZE, sd->bl.y-AREA_SIZE, sd->bl.x+AREA_SIZE, sd->bl.y+AREA_SIZE, BL_PC, sd);
  2970. return 0;
  2971. }
  2972. /**
  2973. * Send HP bar update to others.
  2974. * @param sd: Player invoking update
  2975. */
  2976. void clif_update_hp(map_session_data &sd) {
  2977. if (map_getmapdata(sd.bl.m)->hpmeter_visible)
  2978. clif_hpmeter(&sd);
  2979. if (!battle_config.party_hp_mode && sd.status.party_id)
  2980. clif_party_hp( sd );
  2981. if (sd.bg_id)
  2982. clif_bg_hp(&sd);
  2983. }
  2984. /// Notifies client of a character parameter change.
  2985. /// 00b0 <var id>.W <value>.L (ZC_PAR_CHANGE)
  2986. static void clif_par_change(map_session_data& sd, uint16 varId, int32 count) {
  2987. PACKET_ZC_PAR_CHANGE packet{};
  2988. packet.PacketType = HEADER_ZC_PAR_CHANGE;
  2989. packet.varID = varId;
  2990. packet.count = count;
  2991. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2992. }
  2993. /// Notifies client of a character parameter change.
  2994. /// 00b1 <var id>.W <value>.L (ZC_LONGPAR_CHANGE)
  2995. static void clif_longpar_change(map_session_data& sd, uint16 varId, int32 amount) {
  2996. PACKET_ZC_LONGPAR_CHANGE packet{};
  2997. packet.PacketType = HEADER_ZC_LONGPAR_CHANGE;
  2998. packet.varID = varId;
  2999. packet.amount = amount;
  3000. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3001. }
  3002. /// Notifies client of a character parameter change.
  3003. /// 00be <status id>.W <value>.B (ZC_STATUS_CHANGE)
  3004. static void clif_zc_status_change(map_session_data& sd, uint16 status_id, uint8 value) {
  3005. PACKET_ZC_STATUS_CHANGE packet{};
  3006. packet.PacketType = HEADER_ZC_STATUS_CHANGE;
  3007. packet.statusID = status_id;
  3008. packet.value = value;
  3009. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3010. }
  3011. /// Notifies client of a character parameter change.
  3012. /// 0121 <current count>.W <max count>.W <current weight>.L <max weight>.L (ZC_NOTIFY_CARTITEM_COUNTINFO)
  3013. static void clif_cartcount( map_session_data& sd ){
  3014. PACKET_ZC_NOTIFY_CARTITEM_COUNTINFO packet{};
  3015. packet.PacketType = HEADER_ZC_NOTIFY_CARTITEM_COUNTINFO;
  3016. packet.curCount = sd.cart_num;
  3017. packet.maxCount = MAX_CART;
  3018. packet.curWeight = sd.cart_weight;
  3019. packet.maxWeight = sd.cart_weight_max;
  3020. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3021. }
  3022. /// Tells the client how far it is allowed to attack (weapon range)
  3023. /// 013a <atk range>.W (ZC_ATTACK_RANGE)
  3024. static void clif_attackrange( map_session_data& sd, int16 range ){
  3025. PACKET_ZC_ATTACK_RANGE packet{};
  3026. packet.PacketType = HEADER_ZC_ATTACK_RANGE;
  3027. packet.currentAttRange = range;
  3028. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3029. }
  3030. /// Notifies client of a character parameter change.
  3031. /// 0141 <status id>.L <base status>.L <plus status>.L (ZC_COUPLESTATUS)
  3032. static void clif_couplestatus(map_session_data& sd, uint32 status_type, int32 defaultStatus, int32 plusStatus) {
  3033. PACKET_ZC_COUPLESTATUS packet{};
  3034. packet.PacketType = HEADER_ZC_COUPLESTATUS;
  3035. packet.statusType = status_type;
  3036. packet.defaultStatus = defaultStatus;
  3037. packet.plusStatus = plusStatus;
  3038. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  3039. }
  3040. /// Notifies client of a character parameter change.
  3041. /// 0acb <var id>.W <value>.Q (ZC_LONGLONGPAR_CHANGE)
  3042. static void clif_longlongpar_change(map_session_data& sd, uint16 varId, int64 amount) {
  3043. #if PACKETVER_MAIN_NUM >= 20170906 || PACKETVER_RE_NUM >= 20170830 || defined(PACKETVER_ZERO)
  3044. PACKET_ZC_LONGLONGPAR_CHANGE packet{};
  3045. packet.PacketType = HEADER_ZC_LONGLONGPAR_CHANGE;
  3046. packet.varID = varId;
  3047. packet.amount = amount;
  3048. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3049. #endif
  3050. }
  3051. /// Notifies client of a character parameter change.
  3052. void clif_updatestatus( map_session_data& sd, enum _sp type ){
  3053. switch(type){
  3054. case SP_WEIGHT:
  3055. pc_updateweightstatus(&sd);
  3056. clif_par_change(sd, type, sd.weight);
  3057. break;
  3058. case SP_MAXWEIGHT:
  3059. clif_par_change(sd, type, sd.max_weight);
  3060. break;
  3061. case SP_SPEED:
  3062. clif_par_change(sd, type, sd.battle_status.speed);
  3063. break;
  3064. case SP_BASELEVEL:
  3065. clif_par_change(sd, type, sd.status.base_level);
  3066. break;
  3067. case SP_JOBLEVEL:
  3068. clif_par_change(sd, type, sd.status.job_level);
  3069. break;
  3070. case SP_KARMA:
  3071. // Adding this back, I wonder if the client intercepts this - [Lance]
  3072. clif_par_change(sd, type, sd.status.karma);
  3073. break;
  3074. case SP_MANNER:
  3075. clif_par_change(sd, type, sd.status.manner);
  3076. break;
  3077. case SP_STATUSPOINT:
  3078. clif_par_change(sd, type, sd.status.status_point);
  3079. break;
  3080. case SP_SKILLPOINT:
  3081. clif_par_change(sd, type, sd.status.skill_point);
  3082. break;
  3083. case SP_HIT:
  3084. clif_par_change(sd, type, sd.battle_status.hit);
  3085. break;
  3086. case SP_FLEE1:
  3087. clif_par_change(sd, type, sd.battle_status.flee);
  3088. break;
  3089. case SP_FLEE2:
  3090. clif_par_change(sd, type, sd.battle_status.flee2/10);
  3091. break;
  3092. case SP_MAXHP:
  3093. clif_par_change(sd, type, sd.battle_status.max_hp);
  3094. break;
  3095. case SP_MAXSP:
  3096. clif_par_change(sd, type, sd.battle_status.max_sp);
  3097. break;
  3098. case SP_HP:
  3099. // On officials the HP never go below 1, even if you die [Lemongrass]
  3100. // On officials the HP Novice class never go below 50%, even if you die [Napster]
  3101. if (sd.battle_status.hp == 0) {
  3102. clif_par_change(sd, type, (sd.class_&MAPID_UPPERMASK) != MAPID_NOVICE ? 1 : sd.battle_status.max_hp/2);
  3103. } else {
  3104. clif_par_change(sd, type, sd.battle_status.hp);
  3105. }
  3106. break;
  3107. case SP_SP:
  3108. clif_par_change(sd, type, sd.battle_status.sp);
  3109. break;
  3110. case SP_ASPD:
  3111. clif_par_change(sd, type, sd.battle_status.amotion);
  3112. break;
  3113. case SP_ATK1:
  3114. clif_par_change(sd, type, pc_leftside_atk(&sd));
  3115. break;
  3116. case SP_DEF1:
  3117. clif_par_change(sd, type, pc_leftside_def(&sd));
  3118. break;
  3119. case SP_MDEF1:
  3120. clif_par_change(sd, type, pc_leftside_mdef(&sd));
  3121. break;
  3122. case SP_ATK2:
  3123. clif_par_change(sd, type, pc_rightside_atk(&sd));
  3124. break;
  3125. case SP_DEF2:
  3126. clif_par_change(sd, type, pc_rightside_def(&sd));
  3127. break;
  3128. case SP_MDEF2: {
  3129. //negative check (in case you have something like Berserk active)
  3130. int32 mdef2 = pc_rightside_mdef(&sd);
  3131. #ifndef RENEWAL
  3132. clif_par_change(sd, type, mdef2 < 0 ? 0 : mdef2);
  3133. #else
  3134. clif_par_change(sd, type, mdef2);
  3135. #endif
  3136. }
  3137. break;
  3138. case SP_CRITICAL:
  3139. clif_par_change(sd, type, sd.battle_status.cri/10);
  3140. break;
  3141. case SP_MATK1:
  3142. clif_par_change(sd, type, pc_rightside_matk(&sd));
  3143. break;
  3144. case SP_MATK2:
  3145. clif_par_change(sd, type, pc_leftside_matk(&sd));
  3146. break;
  3147. case SP_ZENY:
  3148. clif_longpar_change(sd, type, sd.status.zeny);
  3149. break;
  3150. #if PACKETVER >= 20170830
  3151. case SP_BASEEXP:
  3152. clif_longlongpar_change(sd, type, client_exp(sd.status.base_exp));
  3153. break;
  3154. case SP_JOBEXP:
  3155. clif_longlongpar_change(sd, type, client_exp(sd.status.job_exp));
  3156. break;
  3157. case SP_NEXTBASEEXP:
  3158. clif_longlongpar_change(sd, type, client_exp(pc_nextbaseexp(&sd)));
  3159. break;
  3160. case SP_NEXTJOBEXP:
  3161. clif_longlongpar_change(sd, type, client_exp(pc_nextjobexp(&sd)));
  3162. break;
  3163. #else
  3164. case SP_BASEEXP:
  3165. clif_longpar_change(sd, type, client_exp(sd.status.base_exp));
  3166. break;
  3167. case SP_JOBEXP:
  3168. clif_longpar_change(sd, type, client_exp(sd.status.job_exp));
  3169. break;
  3170. case SP_NEXTBASEEXP:
  3171. clif_longpar_change(sd, type, client_exp(pc_nextbaseexp(&sd)));
  3172. break;
  3173. case SP_NEXTJOBEXP:
  3174. clif_longpar_change(sd, type, client_exp(pc_nextjobexp(&sd)));
  3175. break;
  3176. #endif
  3177. /**
  3178. * SP_U<STAT> are used to update the amount of points necessary to increase that stat
  3179. **/
  3180. case SP_USTR:
  3181. case SP_UAGI:
  3182. case SP_UVIT:
  3183. case SP_UINT:
  3184. case SP_UDEX:
  3185. case SP_ULUK:
  3186. clif_zc_status_change(sd, static_cast<uint16>(type), static_cast<uint8>(pc_need_status_point(&sd, type-SP_USTR+SP_STR, 1)));
  3187. break;
  3188. case SP_ATTACKRANGE:
  3189. clif_attackrange( sd, sd.battle_status.rhw.range );
  3190. break;
  3191. case SP_STR:
  3192. clif_couplestatus(sd, type, sd.status.str, sd.battle_status.str - sd.status.str);
  3193. break;
  3194. case SP_AGI:
  3195. clif_couplestatus(sd, type, sd.status.agi, sd.battle_status.agi - sd.status.agi);
  3196. break;
  3197. case SP_VIT:
  3198. clif_couplestatus(sd, type, sd.status.vit, sd.battle_status.vit - sd.status.vit);
  3199. break;
  3200. case SP_INT:
  3201. clif_couplestatus(sd, type, sd.status.int_, sd.battle_status.int_ - sd.status.int_);
  3202. break;
  3203. case SP_DEX:
  3204. clif_couplestatus(sd, type, sd.status.dex, sd.battle_status.dex - sd.status.dex);
  3205. break;
  3206. case SP_LUK:
  3207. clif_couplestatus(sd, type, sd.status.luk, sd.battle_status.luk - sd.status.luk);
  3208. break;
  3209. case SP_CARTINFO:
  3210. clif_cartcount( sd );
  3211. break;
  3212. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  3213. case SP_AP:
  3214. clif_par_change( sd, SP_AP, sd.battle_status.ap );
  3215. break;
  3216. case SP_TRAITPOINT:
  3217. clif_par_change( sd, SP_TRAITPOINT, sd.status.trait_point );
  3218. break;
  3219. case SP_MAXAP:
  3220. clif_par_change( sd, SP_MAXAP, sd.battle_status.max_ap );
  3221. break;
  3222. case SP_POW:
  3223. clif_couplestatus( sd, SP_POW, sd.status.pow, sd.battle_status.pow - sd.status.pow );
  3224. break;
  3225. case SP_STA:
  3226. clif_couplestatus( sd, SP_STA, sd.status.sta, sd.battle_status.sta - sd.status.sta );
  3227. break;
  3228. case SP_WIS:
  3229. clif_couplestatus( sd, SP_WIS, sd.status.wis, sd.battle_status.wis - sd.status.wis );
  3230. break;
  3231. case SP_SPL:
  3232. clif_couplestatus( sd, SP_SPL, sd.status.spl, sd.battle_status.spl - sd.status.spl );
  3233. break;
  3234. case SP_CON:
  3235. clif_couplestatus( sd, SP_CON, sd.status.con, sd.battle_status.con - sd.status.con );
  3236. break;
  3237. case SP_CRT:
  3238. clif_couplestatus( sd, SP_CRT, sd.status.crt, sd.battle_status.crt - sd.status.crt );
  3239. break;
  3240. case SP_UPOW:
  3241. case SP_USTA:
  3242. case SP_UWIS:
  3243. case SP_USPL:
  3244. case SP_UCON:
  3245. case SP_UCRT:
  3246. clif_zc_status_change( sd, static_cast<uint16>( type ), static_cast<uint8>( pc_need_trait_point( &sd, type - SP_UPOW + SP_POW, 1 ) ) );
  3247. break;
  3248. case SP_PATK:
  3249. clif_par_change( sd, SP_PATK, sd.battle_status.patk );
  3250. break;
  3251. case SP_SMATK:
  3252. clif_par_change( sd, SP_SMATK, sd.battle_status.smatk );
  3253. break;
  3254. case SP_RES:
  3255. clif_par_change( sd, SP_RES, sd.battle_status.res );
  3256. break;
  3257. case SP_MRES:
  3258. clif_par_change( sd, SP_MRES, sd.battle_status.mres );
  3259. break;
  3260. case SP_HPLUS:
  3261. clif_par_change( sd, SP_HPLUS, sd.battle_status.hplus );
  3262. break;
  3263. case SP_CRATE:
  3264. clif_par_change( sd, SP_CRATE, sd.battle_status.crate );
  3265. break;
  3266. #else
  3267. case SP_AP:
  3268. case SP_TRAITPOINT:
  3269. case SP_MAXAP:
  3270. case SP_POW:
  3271. case SP_STA:
  3272. case SP_WIS:
  3273. case SP_SPL:
  3274. case SP_CON:
  3275. case SP_CRT:
  3276. case SP_UPOW:
  3277. case SP_USTA:
  3278. case SP_UWIS:
  3279. case SP_USPL:
  3280. case SP_UCON:
  3281. case SP_UCRT:
  3282. case SP_PATK:
  3283. case SP_SMATK:
  3284. case SP_RES:
  3285. case SP_MRES:
  3286. case SP_HPLUS:
  3287. case SP_CRATE:
  3288. // 4th job status are not supported by older clients
  3289. return;
  3290. #endif
  3291. default:
  3292. ShowError("clif_updatestatus : unrecognized type %d\n",type);
  3293. return;
  3294. }
  3295. // Additional update packets that should be sent right after
  3296. switch( type ){
  3297. case SP_BASELEVEL:
  3298. if( sd.status.party_id ){
  3299. struct party_data* p;
  3300. int32 i;
  3301. if( ( p = party_search( sd.status.party_id ) ) != nullptr ){
  3302. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd.status.char_id);
  3303. if( i < MAX_PARTY ){
  3304. p->party.member[i].lv = sd.status.base_level;
  3305. clif_party_job_and_level( sd );
  3306. }
  3307. }
  3308. }
  3309. break;
  3310. case SP_HP:
  3311. clif_update_hp( sd );
  3312. break;
  3313. }
  3314. }
  3315. /// Notifies client of a parameter change of an another player.
  3316. /// 01ab <account id>.L <var id>.W <value>.L (ZC_PAR_CHANGE_USER)
  3317. /// var id:
  3318. /// SP_MANNER
  3319. /// ?
  3320. void clif_changemanner( map_session_data& sd ) {
  3321. PACKET_ZC_PAR_CHANGE_USER packet{};
  3322. packet.packetType = HEADER_ZC_PAR_CHANGE_USER;
  3323. packet.gid = sd.bl.id;
  3324. packet.type = static_cast<decltype(packet.type)>(SP_MANNER);
  3325. packet.value = sd.status.manner;
  3326. clif_send( &packet, sizeof( packet ), &sd.bl, AREA_WOS );
  3327. }
  3328. /// 00c3 <id>.L <type>.B <value>.B (ZC_SPRITE_CHANGE)
  3329. /// 01d7 <id>.L <type>.B <value1>.W <value2>.W (ZC_SPRITE_CHANGE2)
  3330. void clif_sprite_change( struct block_list *bl, int32 id, int32 type, int32 val, int32 val2, enum send_target target ){
  3331. PACKET_ZC_SPRITE_CHANGE p = {};
  3332. p.packetType = sendLookType;
  3333. p.AID = id;
  3334. p.type = type;
  3335. p.val = val;
  3336. #if PACKETVER >= 4
  3337. p.val2 = val2;
  3338. #endif
  3339. clif_send( &p, sizeof( p ), bl, target );
  3340. }
  3341. /// Updates sprite/style properties of an object.
  3342. void clif_changelook(struct block_list *bl, int32 type, int32 val) {
  3343. map_session_data* sd;
  3344. status_change* sc;
  3345. struct view_data* vd;
  3346. enum send_target target = AREA;
  3347. int32 val2 = 0;
  3348. nullpo_retv(bl);
  3349. sd = BL_CAST(BL_PC, bl);
  3350. sc = status_get_sc(bl);
  3351. vd = status_get_viewdata(bl);
  3352. if( vd ) //temp hack to let Warp Portal change appearance
  3353. switch(type) {
  3354. case LOOK_WEAPON:
  3355. if (sd) {
  3356. clif_get_weapon_view(sd, &vd->weapon, &vd->shield);
  3357. val = vd->weapon;
  3358. }
  3359. else
  3360. vd->weapon = val;
  3361. break;
  3362. case LOOK_SHIELD:
  3363. if (sd) {
  3364. clif_get_weapon_view(sd, &vd->weapon, &vd->shield);
  3365. val = vd->shield;
  3366. }
  3367. else
  3368. vd->shield = val;
  3369. break;
  3370. case LOOK_BASE:
  3371. if (!sd)
  3372. break;
  3373. if ( val == JT_INVISIBLE )
  3374. return;
  3375. if (sd->sc.option&OPTION_COSTUME)
  3376. vd->weapon = vd->shield = 0;
  3377. if (!vd->cloth_color)
  3378. break;
  3379. if (sd) {
  3380. if (sd->sc.option&OPTION_WEDDING && battle_config.wedding_ignorepalette)
  3381. vd->cloth_color = 0;
  3382. if (sd->sc.option&OPTION_XMAS && battle_config.xmas_ignorepalette)
  3383. vd->cloth_color = 0;
  3384. if (sd->sc.option&(OPTION_SUMMER|OPTION_SUMMER2) && battle_config.summer_ignorepalette)
  3385. vd->cloth_color = 0;
  3386. if (sd->sc.option&OPTION_HANBOK && battle_config.hanbok_ignorepalette)
  3387. vd->cloth_color = 0;
  3388. if (sd->sc.option&OPTION_OKTOBERFEST && battle_config.oktoberfest_ignorepalette)
  3389. vd->cloth_color = 0;
  3390. if (vd->body_style && sd->sc.option&OPTION_COSTUME)
  3391. vd->body_style = 0;
  3392. }
  3393. break;
  3394. case LOOK_HAIR:
  3395. vd->hair_style = val;
  3396. break;
  3397. case LOOK_HEAD_BOTTOM:
  3398. vd->head_bottom = val;
  3399. break;
  3400. case LOOK_HEAD_TOP:
  3401. vd->head_top = val;
  3402. break;
  3403. case LOOK_HEAD_MID:
  3404. vd->head_mid = val;
  3405. break;
  3406. case LOOK_HAIR_COLOR:
  3407. vd->hair_color = val;
  3408. break;
  3409. case LOOK_CLOTHES_COLOR:
  3410. if (val && sd) {
  3411. if (sd->sc.option&OPTION_WEDDING && battle_config.wedding_ignorepalette)
  3412. val = 0;
  3413. if (sd->sc.option&OPTION_XMAS && battle_config.xmas_ignorepalette)
  3414. val = 0;
  3415. if (sd->sc.option&(OPTION_SUMMER|OPTION_SUMMER2) && battle_config.summer_ignorepalette)
  3416. val = 0;
  3417. if (sd->sc.option&OPTION_HANBOK && battle_config.hanbok_ignorepalette)
  3418. val = 0;
  3419. if (sd->sc.option&OPTION_OKTOBERFEST && battle_config.oktoberfest_ignorepalette)
  3420. val = 0;
  3421. }
  3422. vd->cloth_color = val;
  3423. break;
  3424. case LOOK_SHOES:
  3425. #if PACKETVER > 3
  3426. if (sd) {
  3427. int32 n;
  3428. if((n = sd->equip_index[EQI_SHOES]) >= 0 && sd->inventory_data[n]) {
  3429. if(sd->inventory_data[n]->view_id > 0)
  3430. val = sd->inventory_data[n]->view_id;
  3431. else
  3432. val = sd->inventory.u.items_inventory[n].nameid;
  3433. } else
  3434. val = 0;
  3435. }
  3436. #endif
  3437. //Shoes? No packet uses this....
  3438. break;
  3439. case LOOK_BODY:
  3440. // unknown purpose
  3441. break;
  3442. case LOOK_ROBE:
  3443. #if PACKETVER < 20110111
  3444. return;
  3445. #else
  3446. vd->robe = val;
  3447. #endif
  3448. break;
  3449. case LOOK_BODY2:
  3450. #if PACKETVER < 20150513
  3451. return;
  3452. #else
  3453. if (val && sd && sd->sc.option&OPTION_COSTUME)
  3454. val = 0;
  3455. vd->body_style = val;
  3456. #endif
  3457. break;
  3458. }
  3459. // prevent leaking the presence of GM-hidden objects
  3460. if( sc && sc->option&OPTION_INVISIBLE )
  3461. target = SELF;
  3462. #if PACKETVER < 4
  3463. if (target != SELF && bl->type == BL_NPC && (((TBL_NPC*)bl)->is_invisible))
  3464. target = SELF;
  3465. clif_sprite_change(bl, bl->id, type, val, 0, target);
  3466. #else
  3467. if (bl->type != BL_NPC) {
  3468. if (type == LOOK_WEAPON || type == LOOK_SHIELD) {
  3469. type = LOOK_WEAPON;
  3470. val = (vd ? vd->weapon : 0);
  3471. val2 = (vd ? vd->shield : 0);
  3472. }
  3473. if (disguised(bl)) {
  3474. clif_sprite_change(bl, bl->id, type, val, val2, AREA_WOS);
  3475. clif_sprite_change(bl, disguised_bl_id(bl->id), type, val, val2, SELF);
  3476. } else
  3477. clif_sprite_change(bl, bl->id, type, val, val2, target);
  3478. } else
  3479. unit_refresh(bl);
  3480. #endif
  3481. }
  3482. //Sends a change-base-look packet required for traps as they are triggered.
  3483. void clif_changetraplook(struct block_list *bl,int32 val) {
  3484. clif_sprite_change(bl, bl->id, LOOK_BASE, val, 0, AREA);
  3485. }
  3486. /// For the stupid cloth-dye bug. Resends the given view data to the area specified by bl.
  3487. void clif_refreshlook(struct block_list *bl, int32 id, int32 type, int32 val, enum send_target target) {
  3488. clif_sprite_change(bl, id, type, val, 0, target);
  3489. }
  3490. /// Character status (ZC_STATUS).
  3491. /// 00bd <stpoint>.W <str>.B <need str>.B <agi>.B <need agi>.B <vit>.B <need vit>.B
  3492. /// <int>.B <need int>.B <dex>.B <need dex>.B <luk>.B <need luk>.B <atk>.W <atk2>.W
  3493. /// <matk min>.W <matk max>.W <def>.W <def2>.W <mdef>.W <mdef2>.W <hit>.W
  3494. /// <flee>.W <flee2>.W <crit>.W <aspd>.W <aspd2>.W
  3495. void clif_initialstatus( map_session_data& sd ) {
  3496. PACKET_ZC_STATUS packet{};
  3497. packet.packetType = HEADER_ZC_STATUS;
  3498. packet.point = min(sd.status.status_point, INT16_MAX);
  3499. packet.str = min(sd.status.str, UINT8_MAX);
  3500. packet.standardStr = pc_need_status_point( &sd, SP_STR, 1 );
  3501. packet.agi = min(sd.status.agi, UINT8_MAX);
  3502. packet.standardAgi = pc_need_status_point( &sd, SP_AGI,1 );
  3503. packet.vit = min(sd.status.vit, UINT8_MAX);
  3504. packet.standardVit = pc_need_status_point( &sd, SP_VIT,1 );
  3505. packet.int_ = min(sd.status.int_, UINT8_MAX);
  3506. packet.standardInt = pc_need_status_point( &sd, SP_INT,1 );
  3507. packet.dex = min(sd.status.dex, UINT8_MAX);
  3508. packet.standardDex = pc_need_status_point( &sd, SP_DEX,1 );
  3509. packet.luk = min(sd.status.luk, UINT8_MAX);
  3510. packet.standardLuk = pc_need_status_point( &sd, SP_LUK,1 );
  3511. packet.attPower = pc_leftside_atk( &sd );
  3512. packet.refiningPower = pc_rightside_atk( &sd );
  3513. packet.max_mattPower = pc_rightside_matk( &sd );
  3514. packet.min_mattPower = pc_leftside_matk( &sd );
  3515. packet.itemdefPower = pc_leftside_def( &sd );
  3516. packet.plusdefPower = pc_rightside_def( &sd );
  3517. packet.mdefPower = pc_leftside_mdef( &sd );
  3518. #ifdef RENEWAL
  3519. packet.plusmdefPower = pc_rightside_mdef( &sd );
  3520. #else
  3521. // Negative check for Frenzy'ed characters.
  3522. packet.plusmdefPower = std::max( pc_rightside_mdef( &sd ), 0 );
  3523. #endif
  3524. packet.hitSuccessValue = sd.battle_status.hit;
  3525. packet.avoidSuccessValue = sd.battle_status.flee;
  3526. packet.plusAvoidSuccessValue = sd.battle_status.flee2 / 10;
  3527. packet.criticalSuccessValue = sd.battle_status.cri / 10;
  3528. packet.ASPD = sd.battle_status.amotion;
  3529. packet.plusASPD = 0;
  3530. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3531. clif_updatestatus(sd, SP_STR);
  3532. clif_updatestatus(sd, SP_AGI);
  3533. clif_updatestatus(sd, SP_VIT);
  3534. clif_updatestatus(sd, SP_INT);
  3535. clif_updatestatus(sd, SP_DEX);
  3536. clif_updatestatus(sd, SP_LUK);
  3537. clif_updatestatus(sd, SP_ATTACKRANGE);
  3538. clif_updatestatus(sd, SP_ASPD);
  3539. #ifdef RENEWAL
  3540. clif_updatestatus(sd, SP_POW);
  3541. clif_updatestatus(sd, SP_STA);
  3542. clif_updatestatus(sd, SP_WIS);
  3543. clif_updatestatus(sd, SP_SPL);
  3544. clif_updatestatus(sd, SP_CON);
  3545. clif_updatestatus(sd, SP_CRT);
  3546. clif_updatestatus(sd, SP_PATK);
  3547. clif_updatestatus(sd, SP_SMATK);
  3548. clif_updatestatus(sd, SP_RES);
  3549. clif_updatestatus(sd, SP_MRES);
  3550. clif_updatestatus(sd, SP_HPLUS);
  3551. clif_updatestatus(sd, SP_CRATE);
  3552. clif_updatestatus(sd, SP_TRAITPOINT);
  3553. clif_updatestatus(sd, SP_AP);
  3554. clif_updatestatus(sd, SP_MAXAP);
  3555. clif_updatestatus(sd, SP_UPOW);
  3556. clif_updatestatus(sd, SP_USTA);
  3557. clif_updatestatus(sd, SP_UWIS);
  3558. clif_updatestatus(sd, SP_USPL);
  3559. clif_updatestatus(sd, SP_UCON);
  3560. clif_updatestatus(sd, SP_UCRT);
  3561. #endif
  3562. }
  3563. /// Marks an ammunition item in inventory as equipped.
  3564. /// 013c <index>.W (ZC_EQUIP_ARROW)
  3565. void clif_arrowequip( map_session_data& sd ) {
  3566. #if PACKETVER >= 20121128
  3567. clif_status_change(&sd.bl, EFST_CLIENT_ONLY_EQUIP_ARROW, 1, INFINITE_TICK, 0, 0, 0);
  3568. #endif
  3569. PACKET_ZC_EQUIP_ARROW packet{};
  3570. packet.packetType = HEADER_ZC_EQUIP_ARROW;
  3571. // Inventory index of the arrow
  3572. packet.index = client_index( sd.equip_index[EQI_AMMO] );
  3573. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3574. }
  3575. /// Ammunition action message.
  3576. /// 013b <type>.W (ZC_ACTION_FAILURE)
  3577. /// type:
  3578. /// 0 = MsgStringTable[242]="Please equip the proper ammunition first."
  3579. /// 1 = MsgStringTable[243]="You can't Attack or use Skills because your Weight Limit has been exceeded."
  3580. /// 2 = MsgStringTable[244]="You can't use Skills because Weight Limit has been exceeded."
  3581. /// 3 = assassin, baby_assassin, assassin_cross => MsgStringTable[1040]="You have equipped throwing daggers."
  3582. /// gunslinger => MsgStringTable[1175]="Bullets have been equipped."
  3583. /// NOT ninja => MsgStringTable[245]="Ammunition has been equipped."
  3584. void clif_arrow_fail( map_session_data& sd, e_action_failure type ) {
  3585. PACKET_ZC_ACTION_FAILURE packet{};
  3586. packet.packetType = HEADER_ZC_ACTION_FAILURE;
  3587. packet.type = static_cast<decltype(packet.type)>(type);
  3588. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3589. }
  3590. /// Presents a list of items, that can be processed by Arrow Crafting (ZC_MAKINGARROW_LIST).
  3591. /// 01ad <packet len>.W { <name id>.W }*
  3592. void clif_arrow_create_list( map_session_data& sd ){
  3593. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  3594. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  3595. p->packetLength = sizeof( *p );
  3596. int32 count = 0;
  3597. for (const auto &it : skill_arrow_db) {
  3598. t_itemid nameid = it.second->nameid;
  3599. if( !item_db.exists( nameid ) ){
  3600. continue;
  3601. }
  3602. int32 index = pc_search_inventory( &sd, nameid );
  3603. if( index < 0 ){
  3604. continue;
  3605. }
  3606. if( sd.inventory.u.items_inventory[index].equip ){
  3607. continue;
  3608. }
  3609. if( !sd.inventory.u.items_inventory[index].identify ){
  3610. continue;
  3611. }
  3612. p->items[count].itemId = client_nameid( nameid );
  3613. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  3614. count++;
  3615. }
  3616. clif_send( p, p->packetLength, &sd.bl, SELF );
  3617. if( count > 0 ){
  3618. sd.menuskill_id = AC_MAKINGARROW;
  3619. sd.menuskill_val = count;
  3620. }
  3621. }
  3622. /// Notifies the client, about the result of an status change request.
  3623. /// 00bc <status id>.W <result>.B <value>.B (ZC_STATUS_CHANGE_ACK)
  3624. /// status id:
  3625. /// SP_STR ~ SP_LUK and SP_POW ~ SP_CRT
  3626. /// result:
  3627. /// 0 = failure
  3628. /// 1 = success
  3629. void clif_statusupack( map_session_data& sd, int32 type, bool success, int32 val ) {
  3630. PACKET_ZC_STATUS_CHANGE_ACK packet{};
  3631. packet.packetType = HEADER_ZC_STATUS_CHANGE_ACK;
  3632. packet.sp = static_cast<decltype(packet.sp)>(type);
  3633. packet.ok = success;
  3634. packet.value = cap_value(val, 0, UINT8_MAX);
  3635. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3636. }
  3637. /// Notifies the client about the result of a request to equip an item.
  3638. /// 00aa <index>.W <equip location>.W <result>.B (ZC_REQ_WEAR_EQUIP_ACK)
  3639. /// 00aa <index>.W <equip location>.W <view id>.W <result>.B (PACKETVER >= 20100629)
  3640. /// 08d0 <index>.W <equip location>.W <view id>.W <result>.B (ZC_REQ_WEAR_EQUIP_ACK2)
  3641. /// 0999 <index>.W <equip location>.L <view id>.W <result>.B (ZC_ACK_WEAR_EQUIP_V5)
  3642. /// @ok: //inversed forv2 v5
  3643. /// 0 = failure
  3644. /// 1 = success
  3645. /// 2 = failure due to low level
  3646. void clif_equipitemack( map_session_data& sd, uint8 flag, int32 index, int32 pos ){
  3647. PACKET_ZC_REQ_WEAR_EQUIP_ACK p = {};
  3648. p.PacketType = HEADER_ZC_REQ_WEAR_EQUIP_ACK;
  3649. p.index = client_index( index );
  3650. p.wearLocation = pos;
  3651. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20100629
  3652. if( flag == ITEM_EQUIP_ACK_OK && sd.inventory_data[index]->equip&EQP_VISIBLE ){
  3653. p.wItemSpriteNumber = sd.inventory_data[index]->look;
  3654. }else{
  3655. p.wItemSpriteNumber = 0;
  3656. }
  3657. #endif
  3658. p.result = flag;
  3659. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  3660. }
  3661. /// Notifies the client about the result of a request to take off an item.
  3662. /// 00ac <index>.W <equip location>.W <result>.B (ZC_REQ_TAKEOFF_EQUIP_ACK)
  3663. /// 08d1 <index>.W <equip location>.W <result>.B (ZC_REQ_TAKEOFF_EQUIP_ACK2)
  3664. /// 099a <index>.W <equip location>.L <result>.B (ZC_ACK_TAKEOFF_EQUIP_V5)
  3665. /// @success : //inversed for v2 v5
  3666. /// 0 = failure
  3667. /// 1 = success
  3668. void clif_unequipitemack( map_session_data& sd, uint16 server_index, int32 pos, bool success ){
  3669. #if PACKETVER >= 20110824
  3670. success = !success;
  3671. #endif
  3672. PACKET_ZC_REQ_TAKEOFF_EQUIP_ACK p{};
  3673. p.packetType = HEADER_ZC_REQ_TAKEOFF_EQUIP_ACK;
  3674. p.index = client_index(server_index);
  3675. p.wearLocation = static_cast<decltype(p.wearLocation)>(pos);
  3676. p.flag = success;
  3677. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3678. }
  3679. /// Notifies clients in the area about an special/visual effect.
  3680. /// 019b <id>.L <effect id>.L (ZC_NOTIFY_EFFECT)
  3681. /// effect id:
  3682. /// 0 = base level up
  3683. /// 1 = job level up
  3684. /// 2 = refine failure
  3685. /// 3 = refine success
  3686. /// 4 = game over
  3687. /// 5 = pharmacy success
  3688. /// 6 = pharmacy failure
  3689. /// 7 = base level up (super novice)
  3690. /// 8 = job level up (super novice)
  3691. /// 9 = base level up (taekwon)
  3692. void clif_misceffect( block_list& bl, e_notify_effect type ){
  3693. PACKET_ZC_NOTIFY_EFFECT packet{};
  3694. packet.packetType = HEADER_ZC_NOTIFY_EFFECT;
  3695. packet.aid = bl.id;
  3696. packet.effectId = static_cast<decltype(packet.effectId)>(type);
  3697. clif_send( &packet, sizeof( packet ), &bl, AREA );
  3698. }
  3699. /// Notifies clients in the area of a state change.
  3700. /// 0119 <id>.L <body state>.W <health state>.W <effect state>.W <pk mode>.B (ZC_STATE_CHANGE)
  3701. /// 0229 <id>.L <body state>.W <health state>.W <effect state>.L <pk mode>.B (ZC_STATE_CHANGE3)
  3702. void clif_changeoption_target( struct block_list* bl, struct block_list* target ){
  3703. nullpo_retv( bl );
  3704. status_change* sc = status_get_sc( bl );
  3705. //How can an option change if there's no sc?
  3706. if( sc == nullptr ){
  3707. return;
  3708. }
  3709. map_session_data* sd = BL_CAST( BL_PC, bl );
  3710. PACKET_ZC_STATE_CHANGE p = {};
  3711. p.packetType = HEADER_ZC_STATE_CHANGE;
  3712. p.AID = bl->id;
  3713. p.bodyState = sc->opt1;
  3714. p.healthState = sc->opt2;
  3715. p.effectState = sc->option;
  3716. p.isPKModeON = sd ? sd->status.karma : false;
  3717. if( target == nullptr ){
  3718. if( disguised( bl ) ){
  3719. clif_send( &p, sizeof( p ), bl, AREA_WOS );
  3720. p.AID = disguised_bl_id( p.AID );
  3721. clif_send( &p, sizeof( p ), bl, SELF );
  3722. p.AID = disguised_bl_id( p.AID );
  3723. p.effectState = OPTION_INVISIBLE;
  3724. clif_send( &p, sizeof( p ), bl, SELF );
  3725. }else{
  3726. clif_send( &p, sizeof( p ), bl, AREA );
  3727. }
  3728. //Whenever we send "changeoption" to the client, the provoke icon is lost
  3729. //There is probably an option for the provoke icon, but as we don't know it, we have to do this for now
  3730. if( sc->getSCE(SC_PROVOKE) ){
  3731. const struct TimerData *td = get_timer( sc->getSCE(SC_PROVOKE)->timer );
  3732. clif_status_change( bl, status_db.getIcon(SC_PROVOKE), 1, ( !td ? INFINITE_TICK : DIFF_TICK( td->tick, gettick() ) ), 0, 0, 0 );
  3733. }
  3734. }else{
  3735. if( disguised( bl ) ){
  3736. p.AID = disguised_bl_id( p.AID );
  3737. clif_send( &p, sizeof( p ), target, SELF );
  3738. p.AID = disguised_bl_id( p.AID );
  3739. p.effectState = OPTION_INVISIBLE;
  3740. clif_send( &p, sizeof( p ), target, SELF );
  3741. }else{
  3742. clif_send( &p, sizeof( p ), target, SELF );
  3743. }
  3744. }
  3745. }
  3746. /// Displays status change effects on NPCs/monsters.
  3747. /// 028a <id>.L <effect state>.L <level>.L <showEFST>.L (ZC_NPC_SHOWEFST_UPDATE)
  3748. void clif_changeoption2( block_list& bl ){
  3749. status_change *sc = status_get_sc(&bl);
  3750. if (!sc)
  3751. return; //How can an option change if there's no sc?
  3752. PACKET_ZC_NPC_SHOWEFST_UPDATE packet{};
  3753. packet.packetType = HEADER_ZC_NPC_SHOWEFST_UPDATE;
  3754. packet.gid = bl.id;
  3755. packet.effectState = sc->option;
  3756. packet.level = clif_setlevel(&bl);
  3757. packet.showEFST = sc->opt3;
  3758. if (disguised(&bl)) {
  3759. clif_send( &packet, sizeof( packet ), &bl, AREA_WOS );
  3760. packet.gid = disguised_bl_id( bl.id );
  3761. clif_send( &packet, sizeof( packet ), &bl, SELF );
  3762. packet.gid = bl.id;
  3763. packet.effectState = OPTION_INVISIBLE;
  3764. clif_send( &packet, sizeof( packet ), &bl, SELF );
  3765. } else {
  3766. clif_send( &packet, sizeof( packet ), &bl, AREA );
  3767. }
  3768. }
  3769. /// Notifies the client about the result of an item use request.
  3770. /// 00a8 <index>.W <amount>.W <result>.B (ZC_USE_ITEM_ACK)
  3771. /// 01c8 <index>.W <name id>.W <id>.L <amount>.W <result>.B (ZC_USE_ITEM_ACK2)
  3772. void clif_useitemack( map_session_data *sd, int32 index, int32 amount, bool ok ){
  3773. nullpo_retv( sd );
  3774. int32 fd = sd->fd;
  3775. if( !session_isActive( fd ) ){
  3776. return;
  3777. }
  3778. if( index < 0 || index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0 || sd->inventory_data[index] == nullptr ){
  3779. return;
  3780. }
  3781. PACKET_ZC_USE_ITEM_ACK p = {};
  3782. p.packetType = useItemAckType;
  3783. p.index = index + 2;
  3784. #if PACKETVER > 3
  3785. p.itemId = client_nameid( sd->inventory.u.items_inventory[index].nameid );
  3786. p.AID = sd->bl.id;
  3787. #endif
  3788. p.amount = amount;
  3789. p.result = ok;
  3790. if( !ok ){
  3791. clif_send( &p, sizeof(p), &sd->bl, SELF );
  3792. }else{
  3793. clif_send( &p, sizeof(p), &sd->bl, AREA );
  3794. }
  3795. }
  3796. /// Inform client whether chatroom creation was successful or not.
  3797. /// 00d6 <flag>.B (ZC_ACK_CREATE_CHATROOM)
  3798. /// flag:
  3799. /// 0 = Room has been successfully created (opens chat room)
  3800. /// 1 = Room limit exceeded
  3801. /// 2 = Same room already exists
  3802. void clif_createchat( map_session_data& sd, e_create_chatroom flag ){
  3803. PACKET_ZC_ACK_CREATE_CHATROOM packet{};
  3804. packet.packetType = HEADER_ZC_ACK_CREATE_CHATROOM;
  3805. packet.flag = static_cast<decltype(packet.flag)>(flag);
  3806. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3807. }
  3808. /// Calculates the chat status
  3809. /// type:
  3810. /// 0 = private (password protected)
  3811. /// 1 = public
  3812. /// 2 = arena (npc waiting room)
  3813. /// 3 = PK zone (non-clickable)
  3814. static e_status_chatroom clif_chat_status( const chat_data &cd ){
  3815. if (cd.owner->type == BL_PC)
  3816. return (cd.pub) ? STATUSROOM_PUBLIC : STATUSROOM_PRIVATE;
  3817. else if (cd.owner->type == BL_NPC)
  3818. return (cd.limit) ? STATUSROOM_ARENA : STATUSROOM_PK_ZONE;
  3819. else
  3820. return STATUSROOM_PUBLIC;
  3821. }
  3822. /// Calculates the chat user count
  3823. static uint16 clif_chat_usercount( const chat_data& cd ){
  3824. if( cd.owner->type == BL_NPC ){
  3825. return cd.users + 1;
  3826. }else{
  3827. return cd.users;
  3828. }
  3829. }
  3830. /// Display a chat above the owner.
  3831. /// 00d7 <packet len>.W <owner id>.L <char id>.L <limit>.W <users>.W <type>.B <title>.?B (ZC_ROOM_NEWENTRY)
  3832. void clif_dispchat( const chat_data& cd ){
  3833. if( cd.owner == nullptr ){
  3834. return;
  3835. }
  3836. PACKET_ZC_ROOM_NEWENTRY* p = reinterpret_cast<PACKET_ZC_ROOM_NEWENTRY*>( packet_buffer );
  3837. p->packetType = HEADER_ZC_ROOM_NEWENTRY;
  3838. p->packetSize = sizeof( *p );
  3839. p->owner = cd.owner->id;
  3840. p->id = cd.bl.id;
  3841. p->limit = cd.limit;
  3842. p->users = clif_chat_usercount( cd );
  3843. p->type = clif_chat_status( cd );
  3844. // not zero-terminated
  3845. size_t max = safestrnlen( cd.title, CHATROOM_TITLE_SIZE );
  3846. strncpy( p->title, cd.title, max );
  3847. p->packetSize += static_cast<decltype(p->packetSize)>( max );
  3848. clif_send( p, p->packetSize, cd.owner, AREA_WOSC );
  3849. }
  3850. /// Chatroom properties adjustment.
  3851. /// 00df <packet len>.W <owner id>.L <chat id>.L <limit>.W <users>.W <type>.B <title>.?B (ZC_CHANGE_CHATROOM)
  3852. void clif_changechatstatus(chat_data& cd) {
  3853. if( cd.owner == nullptr ){
  3854. return;
  3855. }
  3856. PACKET_ZC_CHANGE_CHATROOM* p = reinterpret_cast<PACKET_ZC_CHANGE_CHATROOM*>( packet_buffer );
  3857. p->packetType = HEADER_ZC_CHANGE_CHATROOM;
  3858. p->packetSize = sizeof(*p);
  3859. p->ownerId = cd.owner->id;
  3860. p->chatId = cd.bl.id;
  3861. p->limit = cd.limit;
  3862. p->users = clif_chat_usercount( cd );
  3863. p->flag = clif_chat_status( cd );
  3864. // not zero-terminated
  3865. size_t max = safestrnlen( cd.title, CHATROOM_TITLE_SIZE );
  3866. strncpy( p->title, cd.title, max );
  3867. p->packetSize += static_cast<decltype(p->packetSize)>( max );
  3868. clif_send(p,p->packetSize,cd.owner,CHAT);
  3869. }
  3870. /// Removes the chatroom
  3871. /// 00d8 <chat id>.L (ZC_DESTROY_ROOM)
  3872. void clif_clearchat(chat_data &cd){
  3873. PACKET_ZC_DESTROY_ROOM p{};
  3874. p.packetType = HEADER_ZC_DESTROY_ROOM;
  3875. p.chatId = cd.bl.id;
  3876. clif_send(&p,sizeof(p),cd.owner,AREA_WOSC);
  3877. }
  3878. /// Displays messages regarding join chat failures.
  3879. /// 00da <result>.B (ZC_REFUSE_ENTER_ROOM)
  3880. /// result:
  3881. /// 0 = room full
  3882. /// 1 = wrong password
  3883. /// 2 = kicked
  3884. /// 3 = success (no message)
  3885. /// 4 = no enough zeny
  3886. /// 5 = too low level
  3887. /// 6 = too high level
  3888. /// 7 = unsuitable job class
  3889. void clif_joinchatfail( map_session_data& sd, e_refuse_enter_room result ){
  3890. PACKET_ZC_REFUSE_ENTER_ROOM packet{};
  3891. packet.packetType = HEADER_ZC_REFUSE_ENTER_ROOM;
  3892. packet.result = static_cast<decltype(packet.result)>(result);
  3893. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3894. }
  3895. /// Notifies the client about entering a chatroom (ZC_ENTER_ROOM).
  3896. /// 00db <packet len>.W <chat id>.L { <role>.L <name>.24B }*
  3897. /// role:
  3898. /// 0 = owner (menu)
  3899. /// 1 = normal
  3900. void clif_joinchatok(map_session_data& sd, chat_data& cd){
  3901. PACKET_ZC_ENTER_ROOM* p = reinterpret_cast<PACKET_ZC_ENTER_ROOM*>( packet_buffer );
  3902. p->packetType = HEADER_ZC_ENTER_ROOM;
  3903. p->packetSize = sizeof(*p);
  3904. p->chatId = cd.bl.id;
  3905. if(cd.owner->type == BL_NPC){
  3906. PACKET_ZC_ENTER_ROOM_sub& owner = p->members[0];
  3907. owner.flag = 0;
  3908. safestrncpy(owner.name, reinterpret_cast<npc_data*>(cd.owner)->name, sizeof(owner.name));
  3909. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( owner ) );
  3910. for (size_t i = 0; i < cd.users; i++) {
  3911. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i + 1];
  3912. member.flag = 1;
  3913. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3914. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3915. }
  3916. }else{
  3917. for (size_t i = 0; i < cd.users; i++) {
  3918. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i];
  3919. member.flag = i > 0;
  3920. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3921. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3922. }
  3923. }
  3924. clif_send(p,p->packetSize,&sd.bl,SELF);
  3925. }
  3926. /// Notifies clients in a chat about a new member.
  3927. /// 00dc <users>.W <name>.24B (ZC_MEMBER_NEWENTRY)
  3928. void clif_addchat( chat_data& cd, map_session_data& sd ){
  3929. PACKET_ZC_MEMBER_NEWENTRY p = {};
  3930. p.packetType = HEADER_ZC_MEMBER_NEWENTRY;
  3931. p.count = cd.users;
  3932. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  3933. clif_send( &p, sizeof( p ), &sd.bl, CHAT_WOS );
  3934. }
  3935. /// Announce the new owner.
  3936. /// 00e1 <role>.L <nick>.24B (ZC_ROLE_CHANGE)
  3937. /// role:
  3938. /// 0 = owner (menu)
  3939. /// 1 = normal
  3940. void clif_chat_role( chat_data& cd, map_session_data& sd ){
  3941. PACKET_ZC_ROLE_CHANGE p = {};
  3942. p.packetType = HEADER_ZC_ROLE_CHANGE;
  3943. p.flag = cd.usersd[0] != &sd;
  3944. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  3945. clif_send( &p, sizeof( p ), &sd.bl, CHAT );
  3946. }
  3947. /// Notify about user leaving the chatroom.
  3948. /// 00dd <users>.W <nick>.24B <flag>.B (ZC_MEMBER_EXIT)
  3949. /// flag:
  3950. /// 0 = left
  3951. /// 1 = kicked
  3952. void clif_chat_leave( chat_data& cd, map_session_data& sd, bool kicked ){
  3953. PACKET_ZC_MEMBER_EXIT p = {};
  3954. p.packetType = HEADER_ZC_MEMBER_EXIT;
  3955. p.count = cd.users - 1;
  3956. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  3957. p.kicked = kicked;
  3958. clif_send( &p, sizeof( p ), &sd.bl, CHAT );
  3959. }
  3960. /// Opens a trade request window from char 'name'.
  3961. /// 00e5 <nick>.24B (ZC_REQ_EXCHANGE_ITEM)
  3962. /// 01f4 <nick>.24B <targetid>.L <baselvl>.W (ZC_REQ_EXCHANGE_ITEM2)
  3963. void clif_traderequest(map_session_data& sd, const char* name){
  3964. PACKET_ZC_REQ_EXCHANGE_ITEM p{};
  3965. p.packetType = HEADER_ZC_REQ_EXCHANGE_ITEM;
  3966. safestrncpy(p.requesterName, name, sizeof(p.requesterName));
  3967. #if PACKETVER > 6
  3968. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  3969. p.targetLv = sd.trade_partner.lv;
  3970. #endif
  3971. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3972. }
  3973. /// Reply to a trade-request.
  3974. /// 00e7 <result>.B (ZC_ACK_EXCHANGE_ITEM)
  3975. /// 01f5 <result>.B <targetid>.L <baselvl>.W (ZC_ACK_EXCHANGE_ITEM2)
  3976. /// result:
  3977. /// 0 = Char is too far
  3978. /// 1 = Character does not exist
  3979. /// 2 = Trade failed
  3980. /// 3 = Accept
  3981. /// 4 = Cancel
  3982. /// 5 = Busy
  3983. void clif_traderesponse( map_session_data& sd, e_ack_trade_response result ){
  3984. PACKET_ZC_ACK_EXCHANGE_ITEM p{};
  3985. p.packetType = HEADER_ZC_ACK_EXCHANGE_ITEM;
  3986. p.result = static_cast<decltype(p.result)>( result );
  3987. #if PACKETVER > 6
  3988. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  3989. p.targetLv = sd.trade_partner.lv;
  3990. #endif
  3991. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3992. }
  3993. /// Notifies the client about an item from other player in current trade.
  3994. /// 00e9 <amount>.L <nameid>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_EXCHANGE_ITEM)
  3995. /// 080f <nameid>.W <item type>.B <amount>.L <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_EXCHANGE_ITEM2)
  3996. /// 0a09 <nameid>.W <item type>.B <amount>.L <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W { <option id>.W <option value>.W <option param>.B }*5 (ZC_ADD_EXCHANGE_ITEM3)
  3997. void clif_tradeadditem( map_session_data* sd, map_session_data* tsd, int32 index, int32 amount ){
  3998. nullpo_retv( sd );
  3999. nullpo_retv( tsd );
  4000. int32 fd = tsd->fd;
  4001. if( !session_isActive( fd ) ){
  4002. return;
  4003. }
  4004. PACKET_ZC_ADD_EXCHANGE_ITEM p = {};
  4005. if( index ){
  4006. index = server_index( index );
  4007. if( index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0 || sd->inventory_data[index] == nullptr ){
  4008. return;
  4009. }
  4010. p.itemId = client_nameid( sd->inventory.u.items_inventory[index].nameid );
  4011. #if PACKETVER >= 20100223
  4012. p.itemType = sd->inventory_data[index]->type;
  4013. #endif
  4014. p.identified = sd->inventory.u.items_inventory[index].identify;
  4015. p.damaged = sd->inventory.u.items_inventory[index].attribute;
  4016. p.refine = sd->inventory.u.items_inventory[index].refine;
  4017. clif_addcards( &p.slot, &sd->inventory.u.items_inventory[index] );
  4018. #if PACKETVER >= 20150226
  4019. clif_add_random_options( p.option_data, sd->inventory.u.items_inventory[index] );
  4020. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  4021. p.location = pc_equippoint_sub( sd, sd->inventory_data[index] );
  4022. p.look = sd->inventory_data[index]->look;
  4023. p.grade = sd->inventory.u.items_inventory[index].enchantgrade;
  4024. #endif
  4025. #endif
  4026. }else{
  4027. p = {};
  4028. }
  4029. p.packetType = HEADER_ZC_ADD_EXCHANGE_ITEM;
  4030. p.amount = amount;
  4031. clif_send( &p, sizeof( p ), &tsd->bl, SELF );
  4032. }
  4033. /// Notifies the client about the result of request to add an item to the current trade (ZC_ACK_ADD_EXCHANGE_ITEM).
  4034. /// 00ea <index>.W <result>.B
  4035. /// result:
  4036. /// 0 = success
  4037. /// 1 = overweight
  4038. /// 2 = trade canceled
  4039. void clif_tradeitemok(map_session_data& sd, int32 index, e_exitem_add_result result)
  4040. {
  4041. PACKET_ZC_ACK_ADD_EXCHANGE_ITEM p = {};
  4042. p.packetType = HEADER_ZC_ACK_ADD_EXCHANGE_ITEM;
  4043. p.index = client_index(index);
  4044. p.result = static_cast<uint8>(result);
  4045. clif_send(&p, sizeof(p), &sd.bl, SELF);
  4046. }
  4047. /// Notifies the client about finishing one side of the current trade.
  4048. /// 00ec <who>.B (ZC_CONCLUDE_EXCHANGE_ITEM)
  4049. /// who:
  4050. /// 0 = self
  4051. /// 1 = other player
  4052. void clif_tradedeal_lock( map_session_data& sd, bool who ){
  4053. PACKET_ZC_CONCLUDE_EXCHANGE_ITEM packet{};
  4054. packet.packetType = HEADER_ZC_CONCLUDE_EXCHANGE_ITEM;
  4055. packet.who = who;
  4056. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4057. }
  4058. /// Notifies the client about the trade being canceled.
  4059. /// 00ee (ZC_CANCEL_EXCHANGE_ITEM)
  4060. void clif_tradecancelled( map_session_data& sd ){
  4061. PACKET_ZC_CANCEL_EXCHANGE_ITEM packet{};
  4062. packet.packetType = HEADER_ZC_CANCEL_EXCHANGE_ITEM;
  4063. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4064. }
  4065. /// Result of a trade.
  4066. /// 00f0 <result>.B (ZC_EXEC_EXCHANGE_ITEM)
  4067. /// result:
  4068. /// 0 = success
  4069. /// 1 = failure
  4070. void clif_tradecompleted( map_session_data& sd ){
  4071. PACKET_ZC_EXEC_EXCHANGE_ITEM packet{};
  4072. packet.packetType = HEADER_ZC_EXEC_EXCHANGE_ITEM;
  4073. packet.result = 0;
  4074. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4075. }
  4076. /// Resets the trade window on the send side.
  4077. /// 00f1 (ZC_EXCHANGEITEM_UNDO)
  4078. /// NOTE: Unknown purpose. Items are not removed until the window is
  4079. /// refreshed (ex. by putting another item in there).
  4080. void clif_tradeundo( map_session_data& sd ){
  4081. PACKET_ZC_EXCHANGEITEM_UNDO packet{};
  4082. packet.packetType = HEADER_ZC_EXCHANGEITEM_UNDO;
  4083. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4084. }
  4085. /// Updates storage total amount.
  4086. /// 00f2 <current count>.W <max count>.W (ZC_NOTIFY_STOREITEM_COUNTINFO)
  4087. void clif_updatestorageamount( map_session_data& sd, uint16 amount, uint16 max_amount ){
  4088. PACKET_ZC_NOTIFY_STOREITEM_COUNTINFO packet{};
  4089. packet.packetType = HEADER_ZC_NOTIFY_STOREITEM_COUNTINFO;
  4090. packet.amount = amount;
  4091. packet.max_amount = max_amount;
  4092. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4093. }
  4094. /// Notifies the client of an item being added to the storage.
  4095. /// 00f4 <index>.W <amount>.L <nameid>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_ITEM_TO_STORE)
  4096. /// 01c4 <index>.W <amount>.L <nameid>.W <type>.B <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_ITEM_TO_STORE2)
  4097. /// 0a0a <index>.W <amount>.L <nameid>.W <type>.B <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W { <option id>.W <option value>.W <option param>.B }*5 (ZC_ADD_ITEM_TO_STORE3)
  4098. void clif_storageitemadded( map_session_data* sd, struct item* i, int32 index, int32 amount ){
  4099. nullpo_retv( sd );
  4100. nullpo_retv( i );
  4101. int32 fd = sd->fd;
  4102. if( !session_isActive( fd ) ){
  4103. return;
  4104. }
  4105. PACKET_ZC_ADD_ITEM_TO_STORE p = {};
  4106. p.packetType = HEADER_ZC_ADD_ITEM_TO_STORE; // Storage item added
  4107. p.index = client_storage_index( index ); // index
  4108. p.amount = amount; // amount
  4109. p.itemId = client_nameid( i->nameid ); // id
  4110. #if PACKETVER >= 5
  4111. p.itemType = itemtype( i->nameid ); //type
  4112. #endif
  4113. p.identified = i->identify; //identify flag
  4114. p.damaged = i->attribute; // attribute
  4115. p.refine = i->refine; //refine
  4116. clif_addcards( &p.slot, i );
  4117. #if PACKETVER_MAIN_NUM >= 20140813 || PACKETVER_RE_NUM >= 20140402 || defined(PACKETVER_ZERO)
  4118. clif_add_random_options( p.option_data, *i );
  4119. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20221024
  4120. p.grade = i->enchantgrade;
  4121. #endif
  4122. #endif
  4123. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  4124. }
  4125. /// Notifies the client of an item being deleted from the storage.
  4126. /// 00f6 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_STORE)
  4127. void clif_storageitemremoved( map_session_data& sd, uint16 index, uint32 amount ){
  4128. PACKET_ZC_DELETE_ITEM_FROM_STORE packet{};
  4129. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_STORE;
  4130. packet.index = client_storage_index(index);
  4131. packet.amount = amount;
  4132. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4133. }
  4134. /// Closes storage.
  4135. /// 00f8 (ZC_CLOSE_STORE)
  4136. void clif_storageclose( map_session_data& sd ){
  4137. PACKET_ZC_CLOSE_STORE packet{};
  4138. packet.packetType = HEADER_ZC_CLOSE_STORE;
  4139. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4140. }
  4141. /// Notifies clients in an area of a player's souls.
  4142. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  4143. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  4144. /// 0b73 <id>.L <amount>.W (ZC_SOULENERGY)
  4145. void clif_soulball( map_session_data *sd, struct block_list* target, enum send_target send_target ){
  4146. #if PACKETVER_MAIN_NUM >= 20200414 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20200506
  4147. PACKET_ZC_SOULENERGY p = {};
  4148. p.PacketType = HEADER_ZC_SOULENERGY;
  4149. #else
  4150. PACKET_ZC_SPIRITS p = {};
  4151. p.PacketType = HEADER_ZC_SPIRITS;
  4152. #endif
  4153. p.AID = sd->bl.id;
  4154. p.num = sd->soulball;
  4155. clif_send( &p, sizeof( p ), target == nullptr ? &sd->bl : target, send_target );
  4156. }
  4157. /*==========================================
  4158. * Server tells 'sd' player client the abouts of 'dstsd' player
  4159. *------------------------------------------*/
  4160. static void clif_getareachar_pc(map_session_data* sd,map_session_data* dstsd)
  4161. {
  4162. struct block_list *d_bl;
  4163. int32 i;
  4164. if( dstsd->chatID ) {
  4165. chat_data* cd = map_id2cd( dstsd->chatID );
  4166. if( cd != nullptr && cd->usersd[0] == dstsd ){
  4167. clif_dispchat(*cd);
  4168. }
  4169. } else if( dstsd->state.vending )
  4170. clif_showvendingboard( *dstsd, SELF, &sd->bl );
  4171. else if( dstsd->state.buyingstore )
  4172. clif_buyingstore_entry( *dstsd, &sd->bl );
  4173. if(dstsd->spiritball > 0)
  4174. clif_spiritball( &dstsd->bl, &sd->bl, SELF );
  4175. clif_millenniumshield_single( *dstsd, *sd );
  4176. clif_spiritcharm_single( *dstsd, *sd );
  4177. if (dstsd->soulball > 0)
  4178. clif_soulball( dstsd, &sd->bl, SELF );
  4179. if (dstsd->servantball > 0)
  4180. clif_servantball( *dstsd, &sd->bl, SELF );
  4181. if (dstsd->abyssball > 0)
  4182. clif_abyssball( *dstsd, &sd->bl, SELF );
  4183. if( (sd->status.party_id && dstsd->status.party_id == sd->status.party_id) || //Party-mate, or hpdisp setting.
  4184. (sd->bg_id && sd->bg_id == dstsd->bg_id) || //BattleGround
  4185. pc_has_permission(sd, PC_PERM_VIEW_HPMETER)
  4186. )
  4187. clif_hpmeter_single( *sd, dstsd->bl.id, dstsd->battle_status.hp, dstsd->battle_status.max_hp );
  4188. // display link (sd - dstsd) to sd
  4189. ARR_FIND( 0, MAX_DEVOTION, i, sd->devotion[i] == dstsd->bl.id );
  4190. if( i < MAX_DEVOTION )
  4191. clif_devotion(&sd->bl, sd);
  4192. // display links (dstsd - devotees) to sd
  4193. ARR_FIND( 0, MAX_DEVOTION, i, dstsd->devotion[i] > 0 );
  4194. if( i < MAX_DEVOTION )
  4195. clif_devotion(&dstsd->bl, sd);
  4196. // display link (dstsd - crusader) to sd
  4197. if( dstsd->sc.getSCE(SC_DEVOTION) && (d_bl = map_id2bl(dstsd->sc.getSCE(SC_DEVOTION)->val1)) != nullptr )
  4198. clif_devotion(d_bl, sd);
  4199. }
  4200. void clif_getareachar_unit( map_session_data* sd,struct block_list *bl ){
  4201. struct unit_data *ud;
  4202. struct view_data *vd;
  4203. bool option = false;
  4204. uint32 option_val = 0;
  4205. vd = status_get_viewdata(bl);
  4206. if (!vd || vd->class_ == JT_INVISIBLE)
  4207. return;
  4208. // Hide NPC from Maya Purple card
  4209. if (clif_npc_mayapurple(bl))
  4210. return;
  4211. if( bl->type == BL_NPC && npc_is_hidden_dynamicnpc( *( (struct npc_data*)bl ), *sd ) ){
  4212. // Do not send anything
  4213. return;
  4214. }
  4215. ud = unit_bl2ud(bl);
  4216. if( ud && ud->walktimer != INVALID_TIMER ){
  4217. clif_set_unit_walking( *bl, sd, *ud, SELF );
  4218. }else{
  4219. clif_set_unit_idle( bl, false, SELF, &sd->bl );
  4220. }
  4221. if (vd->cloth_color)
  4222. clif_refreshlook(&sd->bl,bl->id,LOOK_CLOTHES_COLOR,vd->cloth_color,SELF);
  4223. if (vd->body_style)
  4224. clif_refreshlook(&sd->bl,bl->id,LOOK_BODY2,vd->body_style,SELF);
  4225. switch (bl->type)
  4226. {
  4227. case BL_PC:
  4228. {
  4229. TBL_PC* tsd = (TBL_PC*)bl;
  4230. clif_getareachar_pc(sd, tsd);
  4231. if(tsd->state.size==SZ_BIG) // tiny/big players [Valaris]
  4232. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4233. else if(tsd->state.size==SZ_MEDIUM)
  4234. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4235. if( tsd->bg_id && map_getmapflag(tsd->bl.m, MF_BATTLEGROUND) )
  4236. clif_sendbgemblem_single(sd->fd,tsd);
  4237. if ( tsd->status.robe )
  4238. clif_refreshlook(&sd->bl,bl->id,LOOK_ROBE,tsd->status.robe,SELF);
  4239. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4240. clif_hat_effects( *sd, tsd->bl, SELF );
  4241. }
  4242. break;
  4243. case BL_MER: // Devotion Effects
  4244. if( ((TBL_MER*)bl)->devotion_flag )
  4245. clif_devotion(bl, sd);
  4246. break;
  4247. case BL_NPC:
  4248. {
  4249. TBL_NPC* nd = (TBL_NPC*)bl;
  4250. if( nd->chat_id ){
  4251. chat_data* cd = map_id2cd( nd->chat_id );
  4252. if( cd != nullptr ){
  4253. clif_dispchat( *cd );
  4254. }
  4255. }
  4256. if( nd->size == SZ_BIG )
  4257. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4258. else if( nd->size == SZ_MEDIUM )
  4259. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4260. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4261. clif_progressbar_npc(nd, sd);
  4262. }
  4263. break;
  4264. case BL_MOB:
  4265. {
  4266. TBL_MOB* md = (TBL_MOB*)bl;
  4267. if(md->special_state.size==SZ_BIG) // tiny/big mobs [Valaris]
  4268. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4269. else if(md->special_state.size==SZ_MEDIUM)
  4270. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4271. #if PACKETVER >= 20120404
  4272. if (battle_config.monster_hp_bars_info && !map_getmapflag(bl->m, MF_HIDEMOBHPBAR)) {
  4273. // Must show hp bar to all char who already hit the mob.
  4274. for( const auto& entry : md->dmglog ){
  4275. if( entry.id == sd->status.char_id ){
  4276. clif_monster_hp_bar(md, sd->fd);
  4277. }
  4278. }
  4279. }
  4280. #endif
  4281. }
  4282. break;
  4283. case BL_PET:
  4284. // If the pet wears equip
  4285. if( vd->head_bottom != 0 ){
  4286. pet_data& pd = *reinterpret_cast<pet_data*>( bl );
  4287. clif_send_petdata( sd, pd, CHANGESTATEPET_ACCESSORY );
  4288. }
  4289. break;
  4290. }
  4291. }
  4292. //Modifies the type of damage according to status changes [Skotlex]
  4293. //Aegis data specifies that: 4 endure against single hit sources, 9 against multi-hit.
  4294. static enum e_damage_type clif_calc_delay(enum e_damage_type type, int32 div, int64 damage, int32 delay)
  4295. {
  4296. return ( delay == 0 && damage > 0 ) ? ( div > 1 ? DMG_MULTI_HIT_ENDURE : DMG_ENDURE ) : type;
  4297. }
  4298. /*==========================================
  4299. * Estimates walk delay based on the damage criteria. [Skotlex]
  4300. *------------------------------------------*/
  4301. static int32 clif_calc_walkdelay(struct block_list *bl,int32 delay, char type, int64 damage, int32 div_)
  4302. {
  4303. if (type == DMG_ENDURE || type == DMG_MULTI_HIT_ENDURE || damage <= 0)
  4304. return 0;
  4305. if (bl->type == BL_PC) {
  4306. if (battle_config.pc_walk_delay_rate != 100)
  4307. delay = delay*battle_config.pc_walk_delay_rate/100;
  4308. } else
  4309. if (battle_config.walk_delay_rate != 100)
  4310. delay = delay*battle_config.walk_delay_rate/100;
  4311. if (div_ > 1) //Multi-hit skills mean higher delays.
  4312. delay += battle_config.multihit_delay*(div_-1);
  4313. return (delay > 0) ? delay:1; //Return 1 to specify there should be no noticeable delay, but you should stop walking.
  4314. }
  4315. /*========================================== [Playtester]
  4316. * Returns hallucination damage the client displays
  4317. *------------------------------------------*/
  4318. static int64 clif_hallucination_damage( block_list& bl, int64 damage ){
  4319. status_change* sc = status_get_sc( &bl );
  4320. if( sc == nullptr ){
  4321. return damage;
  4322. }
  4323. if( sc->empty() ){
  4324. return damage;
  4325. }
  4326. if( sc->getSCE( SC_HALLUCINATION ) == nullptr ){
  4327. return damage;
  4328. }
  4329. if( damage == 0 ){
  4330. return damage;
  4331. }
  4332. int32 digit = rnd() % 5 + 1;
  4333. switch (digit)
  4334. {
  4335. case 1:
  4336. return (rnd() % 10);
  4337. case 2:
  4338. return (rnd() % 100);
  4339. case 3:
  4340. return (rnd() % 1000);
  4341. case 4:
  4342. return (rnd() % 10000);
  4343. }
  4344. return (rnd() % 32767);
  4345. }
  4346. #define DEFAULT_ANIMATION_SPEED 432
  4347. /// Sends a 'damage' packet (src performs action on dst)
  4348. /// 008a <src ID>.L <dst ID>.L <server tick>.L <src speed>.L <dst speed>.L <damage>.W <div>.W <type>.B <damage2>.W (ZC_NOTIFY_ACT)
  4349. /// 02e1 <src ID>.L <dst ID>.L <server tick>.L <src speed>.L <dst speed>.L <damage>.L <div>.W <type>.B <damage2>.L (ZC_NOTIFY_ACT2)
  4350. /// 08c8 <src ID>.L <dst ID>.L <server tick>.L <src speed>.L <dst speed>.L <damage>.L <IsSPDamage>.B <div>.W <type>.B <damage2>.L (ZC_NOTIFY_ACT3)
  4351. /// type:
  4352. /// 0 = damage [ damage: total damage, div: amount of hits, damage2: assassin dual-wield damage ]
  4353. /// 1 = pick up item
  4354. /// 2 = sit down
  4355. /// 3 = stand up
  4356. /// 4 = damage (endure)
  4357. /// 5 = (splash?)
  4358. /// 6 = (skill?)
  4359. /// 7 = (repeat damage?)
  4360. /// 8 = multi-hit damage
  4361. /// 9 = multi-hit damage (endure)
  4362. /// 10 = critical hit
  4363. /// 11 = lucky dodge
  4364. /// 12 = (touch skill?)
  4365. /// 13 = multi-hit critical
  4366. int32 clif_damage(block_list& src, block_list& dst, t_tick tick, int32 sdelay, int32 ddelay, int64 sdamage, int32 div, enum e_damage_type type, int64 sdamage2, bool spdamage){
  4367. int32 damage = (int32)cap_value(sdamage,INT_MIN,INT_MAX);
  4368. int32 damage2 = (int32)cap_value(sdamage2,INT_MIN,INT_MAX);
  4369. if (type != DMG_MULTI_HIT_CRITICAL)
  4370. type = clif_calc_delay(type,div,damage+damage2,ddelay);
  4371. damage = static_cast<decltype(damage)>(clif_hallucination_damage(dst, damage));
  4372. damage2 = static_cast<decltype(damage2)>(clif_hallucination_damage(dst, damage2));
  4373. // Calculate what sdelay to send to the client so it applies damage at the same time as the server
  4374. if (battle_config.synchronize_damage && src.type == BL_MOB) {
  4375. // When a clif_damage packet is sent to the client it will also send "sdelay" (amotion) as value.
  4376. // The client however does not interpret this value as AttackMotion but incorrectly as an inverted
  4377. // animation speed modifier, with 432 standing for 1x animation speed.
  4378. // The client will ignore all values above 432, but lower values will speed up the animation.
  4379. // 216 for example means play the animation at double the speed. 108 is quadruple speed.
  4380. // Each monster has an attack animation and may define the frame in the attack animation on which
  4381. // it displays the damage and makes the target flinch / stop. If the damage frame is undefined,
  4382. // it instead displays the damage / flinch / stop at the beginning of the second to last frame.
  4383. // We define the time after which the damage frame shows at 1x speed as clientamotion.
  4384. uint16 clientamotion = std::max((uint16)1, status_get_clientamotion(&src));
  4385. // Knowing when the damage frame happens in the animation allows us to synchronize the timing
  4386. // between client and server using the formula below.
  4387. sdelay = sdelay * DEFAULT_ANIMATION_SPEED / clientamotion;
  4388. // Hint: If amotion is larger than clientamotion this results in a value above 432 which makes the
  4389. // client display the attack at 1x speed. In this case we need to shorten the delay damage timer
  4390. // on the server to clientamotion ms instead (see battle_delay_damage).
  4391. sdelay = std::min(sdelay, DEFAULT_ANIMATION_SPEED);
  4392. }
  4393. PACKET_ZC_NOTIFY_ACT p{};
  4394. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4395. p.srcID = src.id;
  4396. p.targetID = dst.id;
  4397. p.serverTick = client_tick(tick);
  4398. p.srcSpeed = sdelay;
  4399. p.dmgSpeed = ddelay;
  4400. if (battle_config.hide_woe_damage && map_flag_gvg(src.m)) {
  4401. p.damage = damage ? div : 0;
  4402. p.damage2 = damage2 ? div : 0;
  4403. } else {
  4404. p.damage = static_cast<decltype(p.damage)>( std::min( damage, static_cast<decltype(damage)>( std::numeric_limits<decltype(p.damage)>::max() ) ) );
  4405. p.damage2 = static_cast<decltype(p.damage2)>( std::min( damage2, static_cast<decltype(damage2)>( std::numeric_limits<decltype(p.damage2)>::max() ) ) );
  4406. }
  4407. #if PACKETVER >= 20131223
  4408. p.isSPDamage = (spdamage) ? 1 : 0; // IsSPDamage - Displays blue digits.
  4409. #endif
  4410. p.div = div;
  4411. p.type = type;
  4412. if(disguised(&dst)) {
  4413. clif_send( &p, sizeof(p), &dst, AREA_WOS);
  4414. p.targetID = disguised_bl_id( dst.id );
  4415. clif_send( &p, sizeof(p), &dst, SELF);
  4416. } else
  4417. clif_send(&p, sizeof(p), &dst, AREA);
  4418. if(disguised(&src)) {
  4419. p.srcID = disguised_bl_id( src.id );
  4420. if(damage > 0)
  4421. p.damage = -1;
  4422. if(damage2 > 0)
  4423. p.damage2 = -1;
  4424. clif_send( &p, sizeof(p), &src, SELF);
  4425. }
  4426. if(&src == &dst)
  4427. unit_setdir(&src, unit_getdir(&src));
  4428. // In case this assignment is bypassed by DMG_MULTI_HIT_CRITICAL
  4429. type = clif_calc_delay(type, div, damage + damage2, ddelay);
  4430. //Return adjusted can't walk delay for further processing.
  4431. return clif_calc_walkdelay(&dst, ddelay, type, damage+damage2, div);
  4432. }
  4433. /*==========================================
  4434. * src picks up dst
  4435. *------------------------------------------*/
  4436. void clif_takeitem(block_list& src, block_list& dst){
  4437. PACKET_ZC_NOTIFY_ACT p{};
  4438. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4439. p.srcID = src.id;
  4440. p.targetID = dst.id;
  4441. p.type = DMG_PICKUP_ITEM;
  4442. clif_send(&p, sizeof(p), &src, AREA);
  4443. }
  4444. /*==========================================
  4445. * inform clients in area that `bl` is sitting
  4446. *------------------------------------------*/
  4447. void clif_sitting(block_list& bl){
  4448. PACKET_ZC_NOTIFY_ACT p{};
  4449. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4450. p.srcID = bl.id;
  4451. p.type = DMG_SIT_DOWN;
  4452. clif_send(&p, sizeof(p), &bl, AREA);
  4453. if(disguised(&bl)) {
  4454. p.srcID = disguised_bl_id( bl.id );
  4455. clif_send(&p, sizeof(p), &bl, SELF);
  4456. }
  4457. }
  4458. /*==========================================
  4459. * inform clients in area that `bl` is standing
  4460. *------------------------------------------*/
  4461. void clif_standing(block_list& bl){
  4462. PACKET_ZC_NOTIFY_ACT p{};
  4463. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4464. p.srcID = bl.id;
  4465. p.type = DMG_STAND_UP;
  4466. clif_send(&p, sizeof(p), &bl, AREA);
  4467. if(disguised(&bl)) {
  4468. p.srcID = disguised_bl_id( bl.id );
  4469. clif_send(&p, sizeof(p), &bl, SELF);
  4470. }
  4471. }
  4472. /// Inform client(s) about a map-cell change.
  4473. /// 0192 <x>.W <y>.W <type>.W <map name>.16B (ZC_UPDATE_MAPINFO)
  4474. void clif_changemapcell( int16 m, int16 x, int16 y, int16 type, send_target target, block_list* tbl ){
  4475. PACKET_ZC_UPDATE_MAPINFO p = {};
  4476. p.packetType = HEADER_ZC_UPDATE_MAPINFO;
  4477. p.x = x;
  4478. p.y = y;
  4479. p.type = type;
  4480. mapindex_getmapname_ext( map_mapid2mapname( m ), p.mapname );
  4481. if( tbl != nullptr ){
  4482. clif_send( &p, sizeof( p ), tbl, target );
  4483. }else{
  4484. block_list dummy_bl = {};
  4485. dummy_bl.type = BL_NUL;
  4486. dummy_bl.x = x;
  4487. dummy_bl.y = y;
  4488. dummy_bl.m = m;
  4489. clif_send( &p, sizeof( p ), &dummy_bl, target );
  4490. }
  4491. }
  4492. /// Notifies the client about an item on floor.
  4493. /// 009d <id>.L <name id>.W <identified>.B <x>.W <y>.W <amount>.W <subX>.B <subY>.B (ZC_ITEM_ENTRY)
  4494. void clif_getareachar_item( map_session_data& sd, flooritem_data& fitem ){
  4495. PACKET_ZC_ITEM_ENTRY p = {};
  4496. p.packetType = HEADER_ZC_ITEM_ENTRY;
  4497. p.AID = fitem.bl.id;
  4498. p.itemId = client_nameid( fitem.item.nameid );
  4499. p.identify = fitem.item.identify;
  4500. p.x = fitem.bl.x;
  4501. p.y = fitem.bl.y;
  4502. p.amount = fitem.item.amount;
  4503. p.subX = fitem.subx;
  4504. p.subY = fitem.suby;
  4505. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  4506. }
  4507. /// Notifes client about Graffiti
  4508. /// 01c9 <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B <has msg>.B <msg>.80B (ZC_SKILL_ENTRY2)
  4509. static void clif_graffiti( skill_unit& unit, send_target target, block_list& bl ){
  4510. packet_graffiti_entry p = {};
  4511. p.PacketType = graffiti_entryType;
  4512. p.AID = unit.bl.id;
  4513. p.creatorAID = unit.group->src_id;
  4514. p.xPos = unit.bl.x;
  4515. p.yPos = unit.bl.y;
  4516. p.job = unit.group->unit_id;
  4517. p.isContens = 1;
  4518. p.isVisible = 1;
  4519. safestrncpy( p.msg, unit.group->valstr, sizeof( p.msg ) );
  4520. clif_send( &p, sizeof( p ), &bl, target );
  4521. }
  4522. /// Notifies the client of a skill unit.
  4523. /// 011f <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B (ZC_SKILL_ENTRY)
  4524. /// 08c7 <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.B <range>.W <visible>.B (ZC_SKILL_ENTRY3)
  4525. /// 099f <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.L <range>.W <visible>.B (ZC_SKILL_ENTRY4)
  4526. /// 09ca <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.L <range>.B <visible>.B <skill level>.B (ZC_SKILL_ENTRY5)
  4527. void clif_getareachar_skillunit(struct block_list *bl, struct skill_unit *unit, enum send_target target, bool visible) {
  4528. int32 header = 0, unit_id = 0, pos = 0, fd = 0, len = -1;
  4529. unsigned char buf[128];
  4530. nullpo_retv(bl);
  4531. nullpo_retv(unit);
  4532. if (bl->type == BL_PC)
  4533. fd = ((TBL_PC*)bl)->fd;
  4534. if (unit->group->state.guildaura)
  4535. return;
  4536. if (unit->group->state.song_dance&0x1 && unit->val2&(1 << UF_ENSEMBLE))
  4537. unit_id = unit->val2&(1 << UF_SONG) ? UNT_DISSONANCE : UNT_UGLYDANCE;
  4538. else if (skill_get_unit_flag(unit->group->skill_id, UF_RANGEDSINGLEUNIT) && !(unit->val2 & (1 << UF_RANGEDSINGLEUNIT)))
  4539. unit_id = UNT_DUMMYSKILL; // Use invisible unit id for other case of rangedsingle unit
  4540. else
  4541. unit_id = unit->group->unit_id;
  4542. if (!visible)
  4543. unit_id = UNT_DUMMYSKILL; // Hack to makes hidden trap really hidden!
  4544. #if PACKETVER >= 3
  4545. if (unit_id == UNT_GRAFFITI) { // Graffiti [Valaris]
  4546. clif_graffiti( *unit, target, *bl );
  4547. return;
  4548. }
  4549. #endif
  4550. #if PACKETVER <= 20120702
  4551. header = 0x011f;
  4552. //#if PACKETVER < 20110718
  4553. // header = 0x011f;
  4554. //#elif PACKETVER < 20121212
  4555. // header = 0x08c7;
  4556. #elif PACKETVER < 20130731
  4557. header = 0x099f;
  4558. #else
  4559. header = 0x09ca;
  4560. #endif
  4561. len = packet_len(header);
  4562. WBUFW(buf,pos) = header;
  4563. if (header != 0x011f) {
  4564. WBUFW(buf, pos+2) = len;
  4565. pos += 2;
  4566. }
  4567. WBUFL(buf,pos+2) = unit->bl.id;
  4568. WBUFL(buf,pos+6) = unit->group->src_id;
  4569. WBUFW(buf,pos+10) = unit->bl.x;
  4570. WBUFW(buf,pos+12) = unit->bl.y;
  4571. switch (header) {
  4572. case 0x011f:
  4573. WBUFB(buf,pos+14) = unit_id;
  4574. WBUFB(buf,pos+15) = visible;
  4575. break;
  4576. case 0x08c7:
  4577. WBUFB(buf,pos+14) = unit_id;
  4578. WBUFW(buf,pos+15) = unit->range;
  4579. WBUFB(buf,pos+17) = visible;
  4580. break;
  4581. case 0x099f:
  4582. WBUFL(buf,pos+14) = unit_id;
  4583. WBUFW(buf,pos+18) = unit->range;
  4584. WBUFB(buf,pos+20) = visible;
  4585. break;
  4586. case 0x09ca:
  4587. WBUFL(buf,pos+14) = unit_id;
  4588. WBUFB(buf,pos+18) = (unsigned char)unit->range;
  4589. WBUFB(buf,pos+19) = visible;
  4590. WBUFB(buf,pos+20) = (unsigned char)unit->group->skill_lv;
  4591. break;
  4592. }
  4593. clif_send(buf, len, bl, target);
  4594. if (unit->group->skill_id == WZ_ICEWALL)
  4595. clif_changemapcell( unit->bl.m, unit->bl.x, unit->bl.y, 5, SELF, bl );
  4596. }
  4597. /// 09ca <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.L <range>.B <visible>.B <skill level>.B (ZC_SKILL_ENTRY5)
  4598. void clif_skill_unit_test(struct block_list *bl, int16 x, int16 y, int32 unit_id, int16 range, int16 skill_lv) {
  4599. unsigned char buf[128];
  4600. nullpo_retv(bl);
  4601. WBUFW(buf, 0) = 0x09ca;
  4602. WBUFW(buf, 2) = packet_len(0x09ca);
  4603. WBUFL(buf, 4) = 1000;
  4604. WBUFL(buf, 8) = 2000;
  4605. WBUFW(buf, 12) = x;
  4606. WBUFW(buf, 14) = y;
  4607. WBUFL(buf, 16) = unit_id;
  4608. WBUFB(buf, 20) = (unsigned char)range;
  4609. WBUFB(buf, 21) = 1;
  4610. WBUFB(buf, 22) = (unsigned char)skill_lv;
  4611. clif_send(buf, packet_len(0x09ca), bl, AREA);
  4612. }
  4613. /// Server tells client to remove unit of id 'unit->bl.id'
  4614. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4615. static void clif_clearchar_skillunit( skill_unit& unit, map_session_data& sd ){
  4616. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4617. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4618. packet.GID = unit.bl.id;
  4619. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4620. if( unit.group && unit.group->skill_id == WZ_ICEWALL ){
  4621. clif_changemapcell( unit.bl.m, unit.bl.x, unit.bl.y, unit.val2, SELF, &sd.bl );
  4622. }
  4623. }
  4624. /// Removes a skill unit.
  4625. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4626. void clif_skill_delunit( skill_unit& unit ){
  4627. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4628. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4629. packet.GID = unit.bl.id;
  4630. clif_send( &packet, sizeof( packet ), &unit.bl, AREA );
  4631. }
  4632. /// Sent when an object gets ankle-snared.
  4633. /// 01ac <id>.L (ZC_SKILL_UPDATE)
  4634. /// Only affects units with class [139,153] client-side.
  4635. void clif_skillunit_update( block_list& bl ){
  4636. PACKET_ZC_SKILL_UPDATE packet{};
  4637. packet.packetType = HEADER_ZC_SKILL_UPDATE;
  4638. packet.GID = bl.id;
  4639. clif_send( &packet, sizeof( packet ), &bl, AREA );
  4640. }
  4641. /*==========================================
  4642. *
  4643. *------------------------------------------*/
  4644. static int32 clif_getareachar(struct block_list* bl,va_list ap)
  4645. {
  4646. map_session_data *sd;
  4647. nullpo_ret(bl);
  4648. sd=va_arg(ap,map_session_data*);
  4649. if (!clif_session_isValid(sd))
  4650. return 0;
  4651. switch(bl->type){
  4652. case BL_ITEM:
  4653. clif_getareachar_item( *sd, *reinterpret_cast<flooritem_data*>( bl ) );
  4654. break;
  4655. case BL_SKILL:
  4656. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &sd->bl);
  4657. break;
  4658. default:
  4659. if(&sd->bl == bl)
  4660. break;
  4661. clif_getareachar_unit(sd,bl);
  4662. break;
  4663. }
  4664. return 0;
  4665. }
  4666. /*==========================================
  4667. * tbl has gone out of view-size of bl
  4668. *------------------------------------------*/
  4669. int32 clif_outsight(struct block_list *bl,va_list ap)
  4670. {
  4671. struct block_list *tbl;
  4672. struct view_data *vd;
  4673. TBL_PC *sd, *tsd;
  4674. tbl=va_arg(ap,struct block_list*);
  4675. if(bl == tbl) return 0;
  4676. sd = BL_CAST(BL_PC, bl);
  4677. tsd = BL_CAST(BL_PC, tbl);
  4678. if (clif_session_isValid(tsd)) { //tsd has lost sight of the bl object.
  4679. nullpo_ret(bl);
  4680. switch(bl->type){
  4681. case BL_PC:
  4682. if(sd->vd.class_ != JT_INVISIBLE)
  4683. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4684. if(sd->chatID){
  4685. struct chat_data *cd;
  4686. cd=(struct chat_data*)map_id2bl(sd->chatID);
  4687. if(cd->usersd[0]==sd)
  4688. clif_dispchat(*cd);
  4689. }
  4690. if(sd->state.vending)
  4691. clif_closevendingboard(bl,tsd->fd);
  4692. if(sd->state.buyingstore)
  4693. clif_buyingstore_disappear_entry( *sd, &tsd->bl );
  4694. break;
  4695. case BL_ITEM:
  4696. clif_clearflooritem( *reinterpret_cast<flooritem_data*>( bl ), tsd );
  4697. break;
  4698. case BL_SKILL:
  4699. clif_clearchar_skillunit( *((skill_unit *)bl), *tsd );
  4700. break;
  4701. case BL_NPC:
  4702. if(!(((TBL_NPC*)bl)->is_invisible))
  4703. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4704. break;
  4705. default:
  4706. if((vd=status_get_viewdata(bl)) && vd->class_ != JT_INVISIBLE)
  4707. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4708. break;
  4709. }
  4710. }
  4711. if (clif_session_isValid(sd)) { //sd is watching tbl go out of view.
  4712. nullpo_ret(tbl);
  4713. if(tbl->type == BL_SKILL) //Trap knocked out of sight
  4714. clif_clearchar_skillunit( *((skill_unit *)tbl), *sd );
  4715. else if(((vd=status_get_viewdata(tbl)) && vd->class_ != JT_INVISIBLE) &&
  4716. !(tbl->type == BL_NPC && (((TBL_NPC*)tbl)->is_invisible)))
  4717. clif_clearunit_single( tbl->id, CLR_OUTSIGHT, *sd );
  4718. }
  4719. return 0;
  4720. }
  4721. /*==========================================
  4722. * tbl has come into view of bl
  4723. *------------------------------------------*/
  4724. int32 clif_insight(struct block_list *bl,va_list ap)
  4725. {
  4726. struct block_list *tbl;
  4727. TBL_PC *sd, *tsd;
  4728. tbl=va_arg(ap,struct block_list*);
  4729. if (bl == tbl) return 0;
  4730. sd = BL_CAST(BL_PC, bl);
  4731. tsd = BL_CAST(BL_PC, tbl);
  4732. if (clif_session_isValid(tsd)) { //Tell tsd that bl entered into his view
  4733. switch(bl->type){
  4734. case BL_ITEM:
  4735. clif_getareachar_item( *tsd, *reinterpret_cast<flooritem_data*>( bl ) );
  4736. break;
  4737. case BL_SKILL:
  4738. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &tsd->bl);
  4739. break;
  4740. default:
  4741. clif_getareachar_unit(tsd,bl);
  4742. break;
  4743. }
  4744. }
  4745. if (clif_session_isValid(sd)) { //Tell sd that tbl walked into his view
  4746. clif_getareachar_unit(sd,tbl);
  4747. }
  4748. return 0;
  4749. }
  4750. /// Updates whole skill tree (ZC_SKILLINFO_LIST).
  4751. /// 010f <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  4752. void clif_skillinfoblock(map_session_data *sd)
  4753. {
  4754. int32 fd;
  4755. int32 i,len,id;
  4756. nullpo_retv(sd);
  4757. fd = sd->fd;
  4758. if (!session_isActive(fd))
  4759. return;
  4760. WFIFOHEAD(fd, MAX_SKILL * 37 + 4);
  4761. WFIFOW(fd,0) = 0x10f;
  4762. bool haveCallPartnerSkill = false;
  4763. for ( i = 0, len = 4; i < MAX_SKILL; i++)
  4764. {
  4765. if( (id = sd->status.skill[i].id) != 0 )
  4766. {
  4767. // workaround for bugreport:5348
  4768. if (len + 37 > 8192)
  4769. break;
  4770. // skip WE_CALLPARTNER and send it in special way
  4771. if (id == WE_CALLPARTNER) {
  4772. haveCallPartnerSkill = true;
  4773. continue;
  4774. }
  4775. WFIFOW(fd,len) = id;
  4776. WFIFOL(fd,len+2) = skill_get_inf(id);
  4777. WFIFOW(fd,len+6) = sd->status.skill[i].lv;
  4778. WFIFOW(fd,len+8) = skill_get_sp(id,sd->status.skill[i].lv);
  4779. WFIFOW(fd,len+10)= skill_get_range2(&sd->bl,id,sd->status.skill[i].lv,false);
  4780. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  4781. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  4782. WFIFOB(fd,len+36) = (sd->status.skill[i].lv < skill_tree_get_max(id, sd->status.class_))? 1:0;
  4783. else
  4784. WFIFOB(fd,len+36) = 0;
  4785. len += 37;
  4786. }
  4787. }
  4788. WFIFOW(fd,2)=len;
  4789. WFIFOSET(fd,len);
  4790. // adoption fix
  4791. if (haveCallPartnerSkill) {
  4792. clif_addskill(*sd, WE_CALLPARTNER);
  4793. clif_skillinfo( *sd, WE_CALLPARTNER );
  4794. }
  4795. // workaround for bugreport:5348; send the remaining skills one by one to bypass packet size limit
  4796. for ( ; i < MAX_SKILL; i++)
  4797. {
  4798. if( (id = sd->status.skill[i].id) != 0 && ( id != WE_CALLPARTNER || !haveCallPartnerSkill ) )
  4799. {
  4800. clif_addskill(*sd, id);
  4801. clif_skillinfo( *sd, id );
  4802. }
  4803. }
  4804. }
  4805. /**
  4806. * Server tells client 'sd' to add skill of id 'id' to it's skill tree (e.g. with Ice Falcion item)
  4807. **/
  4808. /// Adds new skill to the skill tree.
  4809. /// 0111 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B (ZC_ADD_SKILL)
  4810. /// 0b31 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <upgradable>.B <isnew>.B (ZC_ADD_SKILL2)
  4811. void clif_addskill(map_session_data &sd, uint16 skill_id){
  4812. uint16 idx = skill_get_index(skill_id);
  4813. if (idx == 0)
  4814. return;
  4815. if( sd.status.skill[idx].id <= 0 )
  4816. return;
  4817. PACKET_ZC_ADD_SKILL p{};
  4818. p.packetType = HEADER_ZC_ADD_SKILL;
  4819. p.skill.id = skill_id;
  4820. p.skill.inf = skill_get_inf(skill_id);
  4821. p.skill.level = sd.status.skill[idx].lv;
  4822. p.skill.sp = static_cast<decltype(p.skill.sp)>( skill_get_sp(skill_id,sd.status.skill[idx].lv) );
  4823. p.skill.range2 = static_cast<decltype(p.skill.range2)>( skill_get_range2(&sd.bl,skill_id,sd.status.skill[idx].lv,false) );
  4824. #if PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190918
  4825. p.skill.level2 = pc_checkskill(&sd,skill_id);
  4826. #else
  4827. safestrncpy(p.skill.name, skill_get_name(skill_id), sizeof(p.skill.name));
  4828. #endif
  4829. if( sd.status.skill[idx].flag == SKILL_FLAG_PERMANENT && sd.status.skill[idx].lv < skill_tree_get_max(skill_id, sd.status.class_))
  4830. p.skill.upFlag = 1;
  4831. else
  4832. p.skill.upFlag = 0;
  4833. clif_send(&p,sizeof(p),&sd.bl,SELF);
  4834. }
  4835. /// Deletes a skill from the skill tree.
  4836. /// 0441 <skill id>.W (ZC_SKILLINFO_DELETE)
  4837. void clif_deleteskill(map_session_data& sd, uint16 skill_id, bool skip_infoblock){
  4838. #if PACKETVER >= 20081126
  4839. uint16 idx = skill_get_index(skill_id);
  4840. if (idx == 0)
  4841. return;
  4842. PACKET_ZC_SKILLINFO_DELETE p{};
  4843. p.packetType = HEADER_ZC_SKILLINFO_DELETE;
  4844. p.skillID = skill_id;
  4845. clif_send(&p,sizeof(p),&sd.bl,SELF);
  4846. #endif
  4847. #if PACKETVER_MAIN_NUM >= 20190807 || PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190918
  4848. if (!skip_infoblock)
  4849. #endif
  4850. clif_skillinfoblock(&sd);
  4851. }
  4852. /// Updates a skill in the skill tree.
  4853. /// 010e <skill id>.W <level>.W <sp cost>.W <attack range>.W <upgradable>.B (ZC_SKILLINFO_UPDATE)
  4854. void clif_skillup( map_session_data& sd, uint16 skill_id, uint16 lv, uint16 range, bool upgradable ){
  4855. uint16 idx = skill_get_index(skill_id);
  4856. if( idx == 0 ){
  4857. return;
  4858. }
  4859. PACKET_ZC_SKILLINFO_UPDATE p{};
  4860. p.packetType = HEADER_ZC_SKILLINFO_UPDATE;
  4861. p.skillId = skill_id;
  4862. p.level = lv;
  4863. p.sp = static_cast<decltype(p.sp)>( skill_get_sp( skill_id, lv ) );
  4864. p.range2 = range;
  4865. p.upFlag = upgradable;
  4866. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  4867. }
  4868. /// Updates a skill in the skill tree
  4869. /// 07e1 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <upgradable>.B (ZC_SKILLINFO_UPDATE2)
  4870. /// 0b33 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <upgradable>.B <level2>.W (ZC_SKILLINFO_UPDATE3)
  4871. void clif_skillinfo( map_session_data& sd, uint16 skill_id ){
  4872. #if PACKETVER >= 20090715
  4873. uint16 idx = skill_get_index(skill_id);
  4874. if( idx == 0 ){
  4875. return;
  4876. }
  4877. PACKET_ZC_SKILLINFO_UPDATE2 p = {};
  4878. p.packetType = HEADER_ZC_SKILLINFO_UPDATE2;
  4879. p.id = skill_id;
  4880. p.level = sd.status.skill[idx].lv;
  4881. p.sp = static_cast<decltype(p.sp)>( skill_get_sp( skill_id,sd.status.skill[idx].lv ) );
  4882. p.range2 = static_cast<decltype(p.range2)>( skill_get_range2( &sd.bl,skill_id,sd.status.skill[idx].lv,false ) );
  4883. p.inf = skill_get_inf( skill_id );
  4884. if( sd.status.skill[idx].flag == SKILL_FLAG_PERMANENT && sd.status.skill[idx].lv < skill_tree_get_max( skill_id, sd.status.class_ ) ){
  4885. p.upFlag = true;
  4886. }else{
  4887. p.upFlag = false;
  4888. }
  4889. #if PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190918
  4890. p.level2 = p.level;
  4891. #endif
  4892. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  4893. #endif
  4894. }
  4895. void clif_skill_scale( struct block_list *bl, int32 src_id, int32 x, int32 y, uint16 skill_id, uint16 skill_lv, int32 casttime ){
  4896. #if PACKETVER >= 20151223
  4897. if( !battle_config.show_skill_scale ){
  4898. return;
  4899. }
  4900. PACKET_ZC_SKILL_SCALE p = {};
  4901. p.PacketType = skillscale;
  4902. p.AID = src_id;
  4903. p.skill_id = skill_id;
  4904. p.skill_lv = skill_lv;
  4905. p.x = x;
  4906. p.y = y;
  4907. p.casttime = casttime;
  4908. if( disguised( bl ) ){
  4909. clif_send( &p, sizeof( p ), bl, AREA_WOS );
  4910. p.AID = disguised_bl_id( bl->id );
  4911. clif_send( &p, sizeof( p ), bl, SELF );
  4912. }else{
  4913. clif_send( &p, sizeof( p ), bl, AREA );
  4914. }
  4915. #endif
  4916. }
  4917. /// Notifies clients in area, that an object is about to use a skill.
  4918. /// 013e <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L (ZC_USESKILL_ACK)
  4919. /// 07fb <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L <is disposable>.B (ZC_USESKILL_ACK2)
  4920. /// property:
  4921. /// 0 = Yellow cast aura
  4922. /// 1 = Water elemental cast aura
  4923. /// 2 = Earth elemental cast aura
  4924. /// 3 = Fire elemental cast aura
  4925. /// 4 = Wind elemental cast aura
  4926. /// 5 = Poison elemental cast aura
  4927. /// 6 = Holy elemental cast aura
  4928. /// ? = like 0
  4929. /// is disposable:
  4930. /// 0 = yellow chat text "[src name] will use skill [skill name]."
  4931. /// 1 = no text
  4932. void clif_skillcasting(struct block_list* bl, int32 src_id, int32 dst_id, int32 dst_x, int32 dst_y, uint16 skill_id, uint16 skill_lv, int32 property, int32 casttime)
  4933. {
  4934. #if PACKETVER < 20091124
  4935. const int32 cmd = 0x13e;
  4936. #else
  4937. const int32 cmd = 0x7fb;
  4938. #endif
  4939. unsigned char buf[32];
  4940. WBUFW(buf,0) = cmd;
  4941. WBUFL(buf,2) = src_id;
  4942. WBUFL(buf,6) = dst_id;
  4943. WBUFW(buf,10) = dst_x;
  4944. WBUFW(buf,12) = dst_y;
  4945. WBUFW(buf,14) = skill_id;
  4946. WBUFL(buf,16) = property<0?0:property; //Avoid sending negatives as element [Skotlex]
  4947. WBUFL(buf,20) = casttime;
  4948. #if PACKETVER >= 20091124
  4949. WBUFB(buf,24) = 0; // isDisposable
  4950. #endif
  4951. if (disguised(bl)) {
  4952. clif_send(buf,packet_len(cmd), bl, AREA_WOS);
  4953. WBUFL(buf,2) = disguised_bl_id( src_id );
  4954. clif_send(buf,packet_len(cmd), bl, SELF);
  4955. } else
  4956. clif_send(buf,packet_len(cmd), bl, AREA);
  4957. if( skill_get_inf2( skill_id, INF2_SHOWSCALE ) ){
  4958. clif_skill_scale( bl, src_id, bl->x, bl->y, skill_id, skill_lv, casttime );
  4959. }
  4960. }
  4961. /// Notifies clients in area, that an object canceled casting.
  4962. /// 01b9 <id>.L (ZC_DISPEL)
  4963. void clif_skillcastcancel( block_list& bl ){
  4964. PACKET_ZC_DISPEL packet{};
  4965. packet.packetType = HEADER_ZC_DISPEL;
  4966. packet.gid = bl.id;
  4967. clif_send( &packet, sizeof(PACKET_ZC_DISPEL), &bl, AREA );
  4968. }
  4969. /// Notifies the client about the result of a skill use request (ZC_ACK_TOUSESKILL).
  4970. /// 0110 <skill id>.W <num>.L <result>.B <cause>.B
  4971. /// num (only used when skill id = NV_BASIC and cause = 0):
  4972. /// 0 = "skill failed" MsgStringTable[159]
  4973. /// 1 = "no emotions" MsgStringTable[160]
  4974. /// 2 = "no sit" MsgStringTable[161]
  4975. /// 3 = "no chat" MsgStringTable[162]
  4976. /// 4 = "no party" MsgStringTable[163]
  4977. /// 5 = "no shout" MsgStringTable[164]
  4978. /// 6 = "no PKing" MsgStringTable[165]
  4979. /// 7 = "no alligning" MsgStringTable[383]
  4980. /// ? = ignored
  4981. /// cause:
  4982. /// 0 = "not enough skill level" MsgStringTable[214] (AL_WARP)
  4983. /// "steal failed" MsgStringTable[205] (TF_STEAL)
  4984. /// "envenom failed" MsgStringTable[207] (TF_POISON)
  4985. /// "skill failed" MsgStringTable[204] (otherwise)
  4986. /// ... = @see enum useskill_fail_cause
  4987. /// ? = ignored
  4988. ///
  4989. /// if(result!=0) doesn't display any of the previous messages
  4990. /// Note: when this packet is received an unknown flag is always set to 0,
  4991. /// suggesting this is an ACK packet for the UseSkill packets and should be sent on success too [FlavioJS]
  4992. void clif_skill_fail( map_session_data& sd, uint16 skill_id, enum useskill_fail_cause cause, int32 btype, t_itemid itemId ){
  4993. if(battle_config.display_skill_fail&1)
  4994. return; //Disable all skill failed messages
  4995. if(cause==USESKILL_FAIL_SKILLINTERVAL && !sd.state.showdelay)
  4996. return; //Disable delay failed messages
  4997. if(skill_id == RG_SNATCHER && battle_config.display_skill_fail&4)
  4998. return;
  4999. if(skill_id == TF_POISON && battle_config.display_skill_fail&8)
  5000. return;
  5001. PACKET_ZC_ACK_TOUSESKILL p = {};
  5002. p.packetType = HEADER_ZC_ACK_TOUSESKILL;
  5003. p.skillId = skill_id;
  5004. p.btype = btype;
  5005. p.itemId = itemId;
  5006. p.flag = 0; // 0 - failed
  5007. p.cause = cause;
  5008. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  5009. }
  5010. /// Skill cooldown display icon.
  5011. /// 043d <skill ID>.W <tick>.L (ZC_SKILL_POSTDELAY)
  5012. void clif_skill_cooldown( map_session_data &sd, uint16 skill_id, t_tick tick ){
  5013. #if PACKETVER_MAIN_NUM >= 20081112 || PACKETVER_RE_NUM >= 20081111 || defined(PACKETVER_ZERO)
  5014. PACKET_ZC_SKILL_POSTDELAY packet{};
  5015. packet.PacketType = HEADER_ZC_SKILL_POSTDELAY;
  5016. packet.SKID = skill_id;
  5017. packet.DelayTM = client_tick(tick);
  5018. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5019. #endif
  5020. }
  5021. /// Skill attack effect and damage.
  5022. /// 0114 <skill id>.W <src id>.L <dst id>.L <tick>.L <src delay>.L <dst delay>.L <damage>.W <level>.W <div>.W <type>.B (ZC_NOTIFY_SKILL)
  5023. /// 01de <skill id>.W <src id>.L <dst id>.L <tick>.L <src delay>.L <dst delay>.L <damage>.L <level>.W <div>.W <type>.B (ZC_NOTIFY_SKILL2)
  5024. int32 clif_skill_damage( block_list& src, block_list& dst, t_tick tick, int32 sdelay, int32 ddelay, int64 sdamage, int32 div, uint16 skill_id, uint16 skill_lv, e_damage_type type ){
  5025. type = clif_calc_delay( type, div, sdamage, ddelay );
  5026. sdamage = clif_hallucination_damage( dst, sdamage );
  5027. PACKET_ZC_NOTIFY_SKILL packet{};
  5028. packet.PacketType = HEADER_ZC_NOTIFY_SKILL;
  5029. packet.SKID = skill_id;
  5030. packet.AID = src.id;
  5031. packet.targetID = dst.id;
  5032. packet.startTime = client_tick( tick );
  5033. packet.attackMT = sdelay;
  5034. packet.attackedMT = ddelay;
  5035. auto damage = std::min( static_cast<decltype(packet.damage)>( sdamage ), std::numeric_limits<decltype(packet.damage)>::max() );
  5036. if (battle_config.hide_woe_damage && map_flag_gvg(src.m)) {
  5037. packet.damage = static_cast<decltype(packet.damage)>(damage ? div : 0);
  5038. } else {
  5039. packet.damage = damage;
  5040. }
  5041. packet.level = skill_lv;
  5042. packet.count = static_cast<decltype(packet.count)>(div);
  5043. // For some reason, late 2013 and newer clients have
  5044. // a issue that causes players and monsters to endure
  5045. // type 6 (ACTION_SKILL) skills. So we have to do a small
  5046. // hack to set all type 6 to be sent as type 8 ACTION_ATTACK_MULTIPLE
  5047. #if PACKETVER < 20131223
  5048. packet.action = static_cast<decltype(packet.action)>(type);
  5049. #else
  5050. packet.action = static_cast<decltype(packet.action)>(( type == DMG_SINGLE ) ? DMG_MULTI_HIT : type);
  5051. #endif
  5052. if (disguised(&dst)) {
  5053. clif_send( &packet, sizeof( packet ), &dst, AREA_WOS );
  5054. packet.targetID = disguised_bl_id( dst.id );
  5055. clif_send( &packet, sizeof( packet ), &dst, SELF );
  5056. } else {
  5057. clif_send( &packet, sizeof( packet ), &dst, AREA );
  5058. }
  5059. if (disguised(&src)) {
  5060. packet.AID = disguised_bl_id( src.id );
  5061. if (disguised(&dst)) {
  5062. packet.targetID = dst.id;
  5063. }
  5064. if (damage > 0) {
  5065. packet.damage = -1;
  5066. }
  5067. clif_send( &packet, sizeof( packet ), &src, SELF );
  5068. }
  5069. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5070. return clif_calc_walkdelay( &dst, ddelay, type, damage, div );
  5071. }
  5072. /// Ground skill attack effect and damage (ZC_NOTIFY_SKILL_POSITION).
  5073. /// 0115 <skill id>.W <src id>.L <dst id>.L <tick>.L <src delay>.L <dst delay>.L <x>.W <y>.W <damage>.W <level>.W <div>.W <type>.B
  5074. /*
  5075. int32 clif_skill_damage2(struct block_list *src,struct block_list *dst,t_tick tick,int32 sdelay,int32 ddelay,int32 damage,int32 div,uint16 skill_id,uint16 skill_lv,enum e_damage_type type)
  5076. {
  5077. unsigned char buf[64];
  5078. nullpo_ret(src);
  5079. nullpo_ret(dst);
  5080. type = (type>DMG_NORMAL)?type:skill_get_hit(skill_id);
  5081. type = clif_calc_delay(type,div,damage,ddelay);
  5082. damage = clif_hallucination_damage( *dst, damage );
  5083. WBUFW(buf,0)=0x115;
  5084. WBUFW(buf,2)=skill_id;
  5085. WBUFL(buf,4)=src->id;
  5086. WBUFL(buf,8)=dst->id;
  5087. WBUFL(buf,12)=client_tick(tick);
  5088. WBUFL(buf,16)=sdelay;
  5089. WBUFL(buf,20)=ddelay;
  5090. WBUFW(buf,24)=dst->x;
  5091. WBUFW(buf,26)=dst->y;
  5092. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5093. WBUFW(buf,28)=damage?div:0;
  5094. } else {
  5095. WBUFW(buf,28)=damage;
  5096. }
  5097. WBUFW(buf,30)=skill_lv;
  5098. WBUFW(buf,32)=div;
  5099. WBUFB(buf,34)=type;
  5100. clif_send(buf,packet_len(0x115),src,AREA);
  5101. if(disguised(src)) {
  5102. WBUFL(buf,4)=disguised_bl_id(src->id);
  5103. if(damage > 0)
  5104. WBUFW(buf,28)=-1;
  5105. clif_send(buf,packet_len(0x115),src,SELF);
  5106. }
  5107. if (disguised(dst)) {
  5108. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5109. if (disguised(src))
  5110. WBUFL(buf,4)=src->id;
  5111. else if(damage > 0)
  5112. WBUFW(buf,28)=-1;
  5113. clif_send(buf,packet_len(0x115),dst,SELF);
  5114. }
  5115. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5116. return clif_calc_walkdelay(dst,ddelay,type,damage,div);
  5117. }
  5118. */
  5119. /// Non-damaging skill effect
  5120. /// 011a <skill id>.W <heal>.W <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL)
  5121. /// 09cb <skill id>.W <heal>.L <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL2)
  5122. bool clif_skill_nodamage( block_list* src, block_list& dst, uint16 skill_id, int32 heal, bool success ){
  5123. PACKET_ZC_USE_SKILL p{};
  5124. p.PacketType = HEADER_ZC_USE_SKILL;
  5125. p.SKID = skill_id;
  5126. p.level = std::min( static_cast<decltype(p.level)>( heal ), std::numeric_limits<decltype(p.level)>::max() );
  5127. p.targetAID = dst.id;
  5128. p.result = success;
  5129. if(src != nullptr){
  5130. p.srcAID = src->id;
  5131. }else{
  5132. p.srcAID = 0;
  5133. }
  5134. if (disguised(&dst)) {
  5135. clif_send(&p, sizeof(p), &dst, AREA_WOS);
  5136. p.targetAID = disguised_bl_id(dst.id);
  5137. clif_send(&p, sizeof(p), &dst, SELF);
  5138. } else
  5139. clif_send(&p, sizeof(p), &dst, AREA);
  5140. if(src != nullptr && disguised(src)) {
  5141. p.srcAID = disguised_bl_id(src->id);
  5142. if (disguised(&dst)){
  5143. // It is necessary to revert the changes done above for the disguised target
  5144. p.targetAID = dst.id;
  5145. }
  5146. clif_send(&p, sizeof(p), src, SELF);
  5147. }
  5148. return success;
  5149. }
  5150. /// Non-damaging ground skill effect.
  5151. /// 0117 <skill id>.W <src id>.L <level>.W <x>.W <y>.W <tick>.L (ZC_NOTIFY_GROUNDSKILL)
  5152. void clif_skill_poseffect( block_list& bl, uint16 skill_id, uint16 skill_lv, uint16 x, uint16 y, t_tick tick ){
  5153. PACKET_ZC_NOTIFY_GROUNDSKILL packet{};
  5154. packet.PacketType = HEADER_ZC_NOTIFY_GROUNDSKILL;
  5155. packet.SKID = skill_id;
  5156. packet.AID = bl.id;
  5157. packet.level = skill_lv;
  5158. packet.xPos = x;
  5159. packet.yPos = y;
  5160. packet.startTime = client_tick( tick );
  5161. if (disguised(&bl)) {
  5162. clif_send( &packet, sizeof( packet ), &bl, AREA_WOS );
  5163. packet.AID = disguised_bl_id( bl.id );
  5164. clif_send( &packet, sizeof( packet ), &bl, SELF );
  5165. } else {
  5166. clif_send( &packet, sizeof( packet ), &bl, AREA );
  5167. }
  5168. }
  5169. /// Presents a list of available warp destinations.
  5170. /// 011c <skill id>.W { <map name>.16B }*4 (ZC_WARPLIST)
  5171. /// 0abe <lenght>.W <skill id>.W { <map name>.16B }*? (ZC_WARPLIST2)
  5172. void clif_skill_warppoint( map_session_data& sd, uint16 skill_id, uint16 skill_lv, std::vector<std::string>& maps ){
  5173. if(maps.empty())
  5174. return;
  5175. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  5176. PACKET_ZC_WARPLIST* p = reinterpret_cast<PACKET_ZC_WARPLIST*>( packet_buffer );
  5177. p->packetType = HEADER_ZC_WARPLIST;
  5178. p->packetLength = sizeof( *p );
  5179. p->skillId = skill_id;
  5180. size_t memoCount = 0;
  5181. for( std::string& map : maps ){
  5182. if( map.empty() ){
  5183. continue;
  5184. }
  5185. PACKET_ZC_WARPLIST_sub& warp = p->maps[memoCount];
  5186. mapindex_getmapname_ext( map.c_str(), warp.map );
  5187. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( warp ) );
  5188. memoCount++;
  5189. }
  5190. clif_send( p, p->packetLength, &sd.bl, SELF );
  5191. #else
  5192. PACKET_ZC_WARPLIST p = {};
  5193. p.packetType = HEADER_ZC_WARPLIST;
  5194. p.skillId = skill_id;
  5195. size_t memoCount = 0, max = 4;
  5196. for( std::string& map : maps ){
  5197. if( map.empty() ){
  5198. continue;
  5199. }
  5200. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5201. mapindex_getmapname_ext( map.c_str(), warp.map );
  5202. if( memoCount++ == max ){
  5203. break;
  5204. }
  5205. }
  5206. for( ; memoCount < max; memoCount++ ){
  5207. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5208. strcpy( warp.map, "" );
  5209. }
  5210. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  5211. #endif
  5212. sd.menuskill_id = skill_id;
  5213. if (skill_id == AL_WARP) {
  5214. sd.menuskill_val = (sd.ud.skillx<<16)|sd.ud.skilly; //Store warp position here.
  5215. sd.state.workinprogress = WIP_DISABLE_ALL;
  5216. } else
  5217. sd.menuskill_val = skill_lv;
  5218. }
  5219. /// Memo message.
  5220. /// 011e <type>.B (ZC_ACK_REMEMBER_WARPPOINT)
  5221. /// type:
  5222. /// 0 = "Saved location as a Memo Point for Warp skill." in color 0xFFFF00 (cyan)
  5223. /// 1 = "Skill Level is not high enough." in color 0x0000FF (red)
  5224. /// 2 = "You haven't learned Warp." in color 0x0000FF (red)
  5225. ///
  5226. /// @param sd Who receives the message
  5227. /// @param type What message
  5228. void clif_skill_memomessage( map_session_data& sd, e_ack_remember_warppoint_result result ){
  5229. PACKET_ZC_ACK_REMEMBER_WARPPOINT packet{};
  5230. packet.packetType = HEADER_ZC_ACK_REMEMBER_WARPPOINT;
  5231. packet.type = static_cast<decltype(packet.type)>(result);
  5232. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5233. }
  5234. /// Teleport message.
  5235. /// 0189 <type>.W (ZC_NOTIFY_MAPINFO)
  5236. /// type:
  5237. /// 0 = "Unable to Teleport in this area" in color 0xFFFF00 (cyan)
  5238. /// 1 = "Saved point cannot be memorized." in color 0x0000FF (red)
  5239. /// 2 = "This skill cannot be used in this area"
  5240. /// 3 = "This item cannot be used in this area"
  5241. ///
  5242. /// @param sd Who receives the message
  5243. /// @param type What message
  5244. void clif_skill_teleportmessage( map_session_data& sd, e_notify_mapinfo_result result ){
  5245. PACKET_ZC_NOTIFY_MAPINFO packet{};
  5246. packet.packetType = HEADER_ZC_NOTIFY_MAPINFO;
  5247. packet.type = static_cast<decltype(packet.type)>(result);
  5248. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5249. }
  5250. /// Displays Sense (WZ_ESTIMATION) information window.
  5251. /// 018c <class>.W <level>.W <size>.W <hp>.L <def>.W <race>.W <mdef>.W <element>.W
  5252. /// <water%>.B <earth%>.B <fire%>.B <wind%>.B <poison%>.B <holy%>.B <shadow%>.B <ghost%>.B <undead%>.B (ZC_MONSTER_INFO)
  5253. void clif_skill_estimation( map_session_data& sd, mob_data& md ){
  5254. PACKET_ZC_MONSTER_INFO packet{};
  5255. packet.packetType = HEADER_ZC_MONSTER_INFO;
  5256. packet.class_ = md.vd->class_;
  5257. packet.level = static_cast<decltype(packet.level)>( md.level );
  5258. packet.size = md.status.size;
  5259. packet.hp = md.status.hp;
  5260. packet.def = static_cast<decltype(packet.def)>( ((battle_config.estimation_type&1) ? md.status.def : 0 ) + ((battle_config.estimation_type&2) ? md.status.def2 : 0 ) );
  5261. packet.race = md.status.race;
  5262. packet.mdef = static_cast<decltype(packet.mdef)>( ((battle_config.estimation_type&1) ? md.status.mdef : 0 ) + ((battle_config.estimation_type&2) ? md.status.mdef2 : 0 ) );
  5263. packet.element = md.status.def_ele;
  5264. // The following caps negative attributes to 0 since the client displays them as 255-fix. [Skotlex]
  5265. packet.water = static_cast<decltype(packet.water)>( std::max( elemental_attribute_db.getAttribute(md.status.ele_lv, ELE_WATER, md.status.def_ele), (int16)0 ) );
  5266. packet.earth = static_cast<decltype(packet.earth)>( std::max( elemental_attribute_db.getAttribute(md.status.ele_lv, ELE_EARTH, md.status.def_ele), (int16)0 ) );
  5267. packet.fire = static_cast<decltype(packet.fire)>( std::max( elemental_attribute_db.getAttribute(md.status.ele_lv, ELE_FIRE, md.status.def_ele), (int16)0 ) );
  5268. packet.wind = static_cast<decltype(packet.wind)>( std::max( elemental_attribute_db.getAttribute(md.status.ele_lv, ELE_WIND, md.status.def_ele), (int16)0 ) );
  5269. packet.poison = static_cast<decltype(packet.poison)>( std::max( elemental_attribute_db.getAttribute(md.status.ele_lv, ELE_POISON, md.status.def_ele), (int16)0 ) );
  5270. packet.holy = static_cast<decltype(packet.holy)>( std::max( elemental_attribute_db.getAttribute(md.status.ele_lv, ELE_HOLY, md.status.def_ele), (int16)0 ) );
  5271. packet.shadow = static_cast<decltype(packet.shadow)>( std::max( elemental_attribute_db.getAttribute(md.status.ele_lv, ELE_DARK, md.status.def_ele), (int16)0 ) );
  5272. packet.ghost = static_cast<decltype(packet.ghost)>( std::max( elemental_attribute_db.getAttribute(md.status.ele_lv, ELE_GHOST, md.status.def_ele), (int16)0 ) );
  5273. packet.undead = static_cast<decltype(packet.undead)>( std::max( elemental_attribute_db.getAttribute(md.status.ele_lv, ELE_UNDEAD, md.status.def_ele), (int16)0 ) );
  5274. clif_send( &packet, sizeof( packet ), &sd.bl, (sd.status.party_id > 0) ? PARTY_SAMEMAP : SELF );
  5275. }
  5276. /// Presents a textual list of producable items.
  5277. /// 018d <packet len>.W { <name id>.W { <material id>.W }*3 }* (ZC_MAKABLEITEMLIST)
  5278. /// material id:
  5279. /// unused by the client
  5280. void clif_skill_produce_mix_list( map_session_data& sd, int32 skill_id, int32 trigger ){
  5281. // Avoid resending the menu
  5282. if( sd.menuskill_id == skill_id ){
  5283. return;
  5284. }
  5285. if (skill_id == GC_CREATENEWPOISON)
  5286. skill_id = GC_RESEARCHNEWPOISON;
  5287. PACKET_ZC_MAKABLEITEMLIST* p = reinterpret_cast<PACKET_ZC_MAKABLEITEMLIST*>( packet_buffer );
  5288. p->packetType = HEADER_ZC_MAKABLEITEMLIST;
  5289. p->packetLength = sizeof( *p );
  5290. int32 count = 0;
  5291. for( int32 i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5292. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, 1 ) ){
  5293. continue;
  5294. }
  5295. if( skill_id > 0 && skill_produce_db[i].req_skill != skill_id ){
  5296. continue;
  5297. }
  5298. PACKET_ZC_MAKABLEITEMLIST_sub& entry = p->items[count];
  5299. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5300. entry.material[0] = 0;
  5301. entry.material[1] = 0;
  5302. entry.material[2] = 0;
  5303. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5304. count++;
  5305. }
  5306. clif_send( p, p->packetLength, &sd.bl, SELF );
  5307. if( count > 0 ){
  5308. sd.menuskill_id = skill_id;
  5309. sd.menuskill_val = trigger;
  5310. }
  5311. }
  5312. /// Present a list of producable items.
  5313. /// 025a <packet len>.W <mk type>.W { <name id>.W }* (ZC_MAKINGITEM_LIST)
  5314. /// mk type:
  5315. /// 1 = cooking
  5316. /// 2 = arrow
  5317. /// 3 = elemental
  5318. /// 4 = GN_MIX_COOKING
  5319. /// 5 = GN_MAKEBOMB
  5320. /// 6 = GN_S_PHARMACY
  5321. /// 7 = MT_M_MACHINE
  5322. /// 8 = BO_BIONIC_PHARMACY
  5323. void clif_cooking_list( map_session_data& sd, int32 trigger, uint16 skill_id, int32 qty, int32 list_type ){
  5324. #if PACKETVER >= 20051010
  5325. // Avoid resending the menu
  5326. if( sd.menuskill_id == skill_id ){
  5327. return;
  5328. }
  5329. PACKET_ZC_MAKINGITEM_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGITEM_LIST*>( packet_buffer );
  5330. p->packetType = HEADER_ZC_MAKINGITEM_LIST;
  5331. p->packetLength = sizeof( *p );
  5332. p->makeItem = list_type;
  5333. int32 count = 0;
  5334. for( int32 i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5335. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, qty ) ){
  5336. continue;
  5337. }
  5338. PACKET_ZC_MAKINGITEM_LIST_sub& entry = p->items[count];
  5339. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5340. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5341. count++;
  5342. }
  5343. if( count > 0 || skill_id == AM_PHARMACY ){
  5344. clif_send( p, p->packetLength, &sd.bl, SELF );
  5345. sd.menuskill_id = skill_id;
  5346. sd.menuskill_val = trigger;
  5347. sd.menuskill_val2 = qty; // amount.
  5348. }else{
  5349. clif_menuskill_clear( &sd );
  5350. #if PACKETVER >= 20090922
  5351. clif_msg_skill( &sd, skill_id, MSI_SKILL_INVENTORY_KINDCNT_OVER );
  5352. #else
  5353. clif_send( p, p->packetLength, &sd.bl, SELF );
  5354. #endif
  5355. }
  5356. #endif
  5357. }
  5358. /// Notifies clients of a status change.
  5359. /// 0196 <index>.W <id>.L <state>.B (ZC_MSG_STATE_CHANGE) [used for ending status changes and starting them on non-pc units (when needed)]
  5360. /// 043f <index>.W <id>.L <state>.B <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE2) [used exclusively for starting statuses on pcs]
  5361. /// 0983 <index>.W <id>.L <state>.B <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE3) (PACKETVER >= 20120618)
  5362. /// @param bl Sends packet to clients around this object
  5363. /// @param id ID of object that has this effect
  5364. /// @param type Status icon see enum efst_type
  5365. /// @param flag 1:Active, 0:Deactive
  5366. /// @param tick Duration in ms
  5367. /// @param val1
  5368. /// @param val2
  5369. /// @param val3
  5370. void clif_status_change_sub(struct block_list *bl, int32 id, int32 type, int32 flag, t_tick tick, int32 val1, int32 val2, int32 val3, enum send_target target_type)
  5371. {
  5372. unsigned char buf[32];
  5373. if (type == EFST_BLANK) //It shows nothing on the client...
  5374. return;
  5375. if (type == EFST_POSTDELAY && tick == 0)
  5376. return;
  5377. nullpo_retv(bl);
  5378. // Statuses with an infinite duration, but still needs a duration sent to display properly.
  5379. if (type == EFST_LUNARSTANCE || type == EFST_UNIVERSESTANCE || type == EFST_SUNSTANCE || type == EFST_STARSTANCE)
  5380. tick = 200;
  5381. #if PACKETVER >= 20120618
  5382. if (flag && battle_config.display_status_timers)
  5383. WBUFW(buf,0) = 0x983;
  5384. else
  5385. #elif PACKETVER >= 20090121
  5386. if (flag && battle_config.display_status_timers)
  5387. WBUFW(buf,0) = 0x43f;
  5388. else
  5389. #endif
  5390. WBUFW(buf,0) = 0x196;
  5391. WBUFW(buf,2) = type;
  5392. WBUFL(buf,4) = id;
  5393. WBUFB(buf,8) = flag;
  5394. #if PACKETVER >= 20120618
  5395. if (flag && battle_config.display_status_timers) {
  5396. if (tick <= 0)
  5397. tick = 9999; // this is indeed what official servers do
  5398. WBUFL(buf,9) = client_tick(tick);/* at this stage remain and total are the same value I believe */
  5399. WBUFL(buf,13) = client_tick(tick);
  5400. WBUFL(buf,17) = val1;
  5401. WBUFL(buf,21) = val2;
  5402. WBUFL(buf,25) = val3;
  5403. }
  5404. #elif PACKETVER >= 20090121
  5405. if (flag && battle_config.display_status_timers) {
  5406. if (tick <= 0)
  5407. tick = 9999; // this is indeed what official servers do
  5408. WBUFL(buf,9) = client_tick(tick);
  5409. WBUFL(buf,13) = val1;
  5410. WBUFL(buf,17) = val2;
  5411. WBUFL(buf,21) = val3;
  5412. }
  5413. #endif
  5414. clif_send(buf, packet_len(WBUFW(buf,0)), bl, target_type);
  5415. }
  5416. /* Sends status effect to clients around the bl
  5417. * @param bl Object that has the effect
  5418. * @param type Status icon see enum efst_type
  5419. * @param flag 1:Active, 0:Deactive
  5420. * @param tick Duration in ms
  5421. * @param val1
  5422. * @param val2
  5423. * @param val3
  5424. */
  5425. void clif_status_change(struct block_list *bl, int32 type, int32 flag, t_tick tick, int32 val1, int32 val2, int32 val3) {
  5426. map_session_data *sd = nullptr;
  5427. if (type == EFST_BLANK) //It shows nothing on the client...
  5428. return;
  5429. if (type == EFST_POSTDELAY && tick == 0)
  5430. return;
  5431. if (type == EFST_ILLUSION && !battle_config.display_hallucination) // Disable Hallucination.
  5432. return;
  5433. #if !( PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 )
  5434. // Older clients display normal riding icon.
  5435. if (type == EFST_MADOGEAR_TYPE)
  5436. type = EFST_RIDING;
  5437. #endif
  5438. nullpo_retv(bl);
  5439. sd = BL_CAST(BL_PC, bl);
  5440. // Check if current bl type is in the returned bitmask and only send status changes that actually matter to the client
  5441. if (!(status_efst_get_bl_type(static_cast<efst_type>(type)) & bl->type))
  5442. return;
  5443. clif_status_change_sub(bl, bl->id, type, flag, tick, val1, val2, val3, ((sd ? (pc_isinvisible(sd) ? SELF : AREA) : AREA_WOS)));
  5444. }
  5445. /**
  5446. * Send any active EFST to those around.
  5447. * @param tbl: Unit to send the packet to
  5448. * @param bl: Objects walking into view
  5449. * @param target: Client send type
  5450. */
  5451. void clif_efst_status_change_sub(struct block_list *tbl, struct block_list *bl, enum send_target target) {
  5452. unsigned char i;
  5453. struct sc_display_entry **sc_display;
  5454. unsigned char sc_display_count;
  5455. bool spheres_sent;
  5456. nullpo_retv(bl);
  5457. switch( bl->type ){
  5458. case BL_PC: {
  5459. map_session_data* sd = (map_session_data*)bl;
  5460. sc_display = sd->sc_display;
  5461. sc_display_count = sd->sc_display_count;
  5462. spheres_sent = !sd->state.connect_new;
  5463. }
  5464. break;
  5465. case BL_NPC: {
  5466. struct npc_data* nd = (struct npc_data*)bl;
  5467. sc_display = nd->sc_display;
  5468. sc_display_count = nd->sc_display_count;
  5469. spheres_sent = true;
  5470. }
  5471. break;
  5472. default:
  5473. return;
  5474. }
  5475. for (i = 0; i < sc_display_count; i++) {
  5476. enum sc_type type = sc_display[i]->type;
  5477. status_change *sc = status_get_sc(bl);
  5478. const struct TimerData *td = (sc && sc->getSCE(type) ? get_timer(sc->getSCE(type)->timer) : nullptr);
  5479. t_tick tick = 0;
  5480. if (td)
  5481. tick = DIFF_TICK(td->tick, gettick());
  5482. // Status changes that need special handling
  5483. switch( type ){
  5484. case SC_SPHERE_1:
  5485. case SC_SPHERE_2:
  5486. case SC_SPHERE_3:
  5487. case SC_SPHERE_4:
  5488. case SC_SPHERE_5:
  5489. if( spheres_sent ){
  5490. target = AREA_WOS;
  5491. }
  5492. break;
  5493. case SC_HELLS_PLANT:
  5494. if( sc && sc->getSCE(type) ){
  5495. tick = sc->getSCE(type)->val4;
  5496. }
  5497. break;
  5498. }
  5499. #if PACKETVER > 20120418
  5500. clif_efst_status_change(tbl, bl->id, target, status_db.getIcon(type), tick, sc_display[i]->val1, sc_display[i]->val2, sc_display[i]->val3);
  5501. #else
  5502. clif_status_change_sub(tbl, bl->id, status_db.getIcon(type), 1, tick, sc_display[i]->val1, sc_display[i]->val2, sc_display[i]->val3, target);
  5503. #endif
  5504. }
  5505. }
  5506. /// Notifies the client when a player enters the screen with an active EFST.
  5507. /// 08ff <id>.L <index>.W <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER) (PACKETVER >= 20111108)
  5508. /// 0984 <id>.L <index>.W <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER2) (PACKETVER >= 20120618)
  5509. void clif_efst_status_change(struct block_list *bl, int32 tid, enum send_target target, int32 type, t_tick tick, int32 val1, int32 val2, int32 val3) {
  5510. #if PACKETVER >= 20111108
  5511. unsigned char buf[32];
  5512. #if PACKETVER >= 20120618
  5513. const int32 cmd = 0x984;
  5514. #elif PACKETVER >= 20111108
  5515. const int32 cmd = 0x8ff;
  5516. #endif
  5517. int32 offset = 0;
  5518. if (type == EFST_BLANK)
  5519. return;
  5520. nullpo_retv(bl);
  5521. if (tick <= 0)
  5522. tick = 9999;
  5523. WBUFW(buf,offset + 0) = cmd;
  5524. WBUFL(buf,offset + 2) = tid;
  5525. WBUFW(buf,offset + 6) = type;
  5526. #if PACKETVER >= 20111108
  5527. WBUFL(buf,offset + 8) = client_tick(tick); // Set remaining status duration [exneval]
  5528. #if PACKETVER >= 20120618
  5529. WBUFL(buf,offset + 12) = client_tick(tick);
  5530. offset += 4;
  5531. #endif
  5532. WBUFL(buf,offset + 12) = val1;
  5533. WBUFL(buf,offset + 16) = val2;
  5534. WBUFL(buf,offset + 20) = val3;
  5535. #endif
  5536. clif_send(buf,packet_len(cmd),bl,target);
  5537. #endif
  5538. }
  5539. /// Send message (modified by [Yor]) (ZC_NOTIFY_PLAYERCHAT).
  5540. /// 008e <packet len>.W <message>.?B
  5541. void clif_displaymessage(const int32 fd, const char* mes)
  5542. {
  5543. nullpo_retv(mes);
  5544. if( session_isActive( fd ) ){
  5545. char *message, *line;
  5546. message = aStrdup(mes);
  5547. line = strtok(message, "\n");
  5548. while(line != nullptr) {
  5549. #if PACKETVER == 20141022
  5550. /** for some reason game client crashes depending on message pattern (only for this packet) **/
  5551. /** so we redirect to ZC_NPC_CHAT **/
  5552. //clif_messagecolor(&sd->bl, color_table[COLOR_DEFAULT], mes, false, SELF);
  5553. unsigned long color = (color_table[COLOR_DEFAULT] & 0x0000FF) << 16 | (color_table[COLOR_DEFAULT] & 0x00FF00) | (color_table[COLOR_DEFAULT] & 0xFF0000) >> 16; // RGB to BGR
  5554. uint16 len = strnlen(line, CHAT_SIZE_MAX);
  5555. if (len > 0) {
  5556. WFIFOHEAD(fd, 13 + len);
  5557. WFIFOW(fd, 0) = 0x2C1;
  5558. WFIFOW(fd, 2) = 13 + len;
  5559. WFIFOL(fd, 4) = 0;
  5560. WFIFOL(fd, 8) = color;
  5561. safestrncpy(WFIFOCP(fd, 12), line, len + 1);
  5562. WFIFOSET(fd, WFIFOW(fd, 2));
  5563. }
  5564. #else
  5565. // Limit message to 255+1 characters (otherwise it causes a buffer overflow in the client)
  5566. int16 len;
  5567. len = static_cast<decltype(len)>( strnlen(line, CHAT_SIZE_MAX) );
  5568. if (len > 0) { // don't send a void message (it's not displaying on the client chat). @help can send void line.
  5569. WFIFOHEAD(fd, 5 + len);
  5570. WFIFOW(fd,0) = 0x8e;
  5571. WFIFOW(fd,2) = 5 + len; // 4 + len + nullptr teminate
  5572. safestrncpy(WFIFOCP(fd,4), line, len + 1);
  5573. WFIFOSET(fd, 5 + len);
  5574. }
  5575. #endif
  5576. line = strtok(nullptr, "\n");
  5577. }
  5578. aFree(message);
  5579. }
  5580. }
  5581. /// Send broadcast message in yellow or blue without font formatting.
  5582. /// 009a <packet len>.W <message>.?B (ZC_BROADCAST)
  5583. void clif_broadcast(struct block_list* bl, const char* mes, size_t len, int32 type, enum send_target target)
  5584. {
  5585. nullpo_retv(mes);
  5586. if (len < 2)
  5587. return;
  5588. PACKET_ZC_BROADCAST* p = reinterpret_cast<PACKET_ZC_BROADCAST*>( packet_buffer );
  5589. p->packetType = HEADER_ZC_BROADCAST;
  5590. p->PacketLength = sizeof( *p );
  5591. if( ( type&BC_BLUE ) != 0 ){
  5592. // If there's "blue" at the beginning of the message, game client will display it in blue instead of yellow.
  5593. const char* color = "blue";
  5594. size_t length = strlen( color );
  5595. strcpy( p->message, color );
  5596. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  5597. strncpy( &p->message[length], mes, len );
  5598. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5599. }else if( ( type&BC_WOE ) != 0 ){
  5600. // If there's "ssss", game client will recognize message as 'WoE broadcast'.
  5601. const char* color = "ssss";
  5602. size_t length = strlen( color );
  5603. strcpy( p->message, color );
  5604. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  5605. strncpy( &p->message[length], mes, len );
  5606. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5607. }else{
  5608. strncpy( p->message, mes, len );
  5609. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5610. }
  5611. clif_send( p, p->PacketLength, bl, target );
  5612. }
  5613. /*==========================================
  5614. * Displays a message on a 'bl' to all it's nearby clients
  5615. * 008d <PacketLength>.W <GID>.L <message>.?B (ZC_NOTIFY_CHAT)
  5616. *------------------------------------------*/
  5617. void clif_GlobalMessage( block_list& bl, const char* message, enum send_target target ){
  5618. nullpo_retv(message);
  5619. int16 len = (int16)( strlen( message ) + 1 );
  5620. if( len > CHAT_SIZE_MAX ) {
  5621. ShowWarning("clif_GlobalMessage: Truncating too long message '%s' (len=%" PRId16 ").\n", message, len);
  5622. len = CHAT_SIZE_MAX;
  5623. }
  5624. PACKET_ZC_NOTIFY_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT*>( packet_buffer );
  5625. p->PacketType = HEADER_ZC_NOTIFY_CHAT;
  5626. p->PacketLength = sizeof( *p );
  5627. p->GID = bl.id;
  5628. safestrncpy( p->Message, message, len );
  5629. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5630. clif_send( p, p->PacketLength, &bl, target );
  5631. }
  5632. /// Send broadcast message with font formatting.
  5633. /// 01c3 <packet len>.W <fontColor>.L <fontType>.W <fontSize>.W <fontAlign>.W <fontY>.W <message>.?B (ZC_BROADCAST2)
  5634. void clif_broadcast2(struct block_list* bl, const char* mes, size_t len, unsigned long fontColor, int16 fontType, int16 fontSize, int16 fontAlign, int16 fontY, enum send_target target)
  5635. {
  5636. nullpo_retv(mes);
  5637. if (len < 2)
  5638. return;
  5639. PACKET_ZC_BROADCAST2* p = reinterpret_cast<PACKET_ZC_BROADCAST2*>( packet_buffer );
  5640. p->packetType = HEADER_ZC_BROADCAST2;
  5641. p->PacketLength = sizeof( *p );
  5642. p->fontColor = fontColor;
  5643. p->fontType = fontType;
  5644. p->fontSize = fontSize;
  5645. p->fontAlign = fontAlign;
  5646. p->fontY = fontY;
  5647. strncpy( p->message, mes, len );
  5648. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5649. clif_send( p, p->PacketLength, bl, target );
  5650. }
  5651. /*
  5652. * Display *msg to all *users in channel
  5653. */
  5654. void clif_channel_msg(struct Channel *channel, const char *msg, unsigned long color) {
  5655. DBIterator *iter;
  5656. map_session_data *user;
  5657. uint16 msg_len = 0, len = 0;
  5658. unsigned char buf[CHAT_SIZE_MAX];
  5659. if (!channel || !msg)
  5660. return;
  5661. msg_len = (uint16)(strlen(msg) + 1);
  5662. if( msg_len > sizeof(buf)-12 ) {
  5663. ShowWarning("clif_channel_msg: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  5664. msg_len = sizeof(buf)-12;
  5665. }
  5666. WBUFW(buf,0) = 0x2C1;
  5667. WBUFW(buf,2) = (len = msg_len + 12);
  5668. WBUFL(buf,4) = 0;
  5669. WBUFL(buf,8) = color;
  5670. safestrncpy(WBUFCP(buf,12), msg, msg_len);
  5671. iter = db_iterator(channel->users);
  5672. for( user = (map_session_data *)dbi_first(iter); dbi_exists(iter); user = (map_session_data *)dbi_next(iter) ) {
  5673. clif_send(buf, len, &user->bl, SELF);
  5674. }
  5675. dbi_destroy(iter);
  5676. }
  5677. /// Displays heal effect.
  5678. /// 013d <var id>.W <amount>.W (ZC_RECOVERY)
  5679. /// 0a27 <var id>.W <amount>.L (ZC_RECOVERY2)
  5680. /// var id:
  5681. /// 5 = HP (SP_HP)
  5682. /// 7 = SP (SP_SP)
  5683. /// ? = ignored
  5684. void clif_heal( map_session_data& sd, int32 type, uint32 val ) {
  5685. PACKET_ZC_RECOVERY packet{};
  5686. packet.packetType = HEADER_ZC_RECOVERY;
  5687. packet.type = static_cast<decltype(packet.type)>(type);
  5688. packet.amount = std::min( static_cast<decltype(packet.amount)>( val ), std::numeric_limits<decltype(packet.amount)>::max() );
  5689. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5690. }
  5691. /// Displays resurrection effect.
  5692. /// 0148 <id>.L <type>.W (ZC_RESURRECTION)
  5693. /// type:
  5694. /// ignored
  5695. void clif_resurrection( block_list& bl ){
  5696. PACKET_ZC_RESURRECTION packet{};
  5697. packet.packetType = HEADER_ZC_RESURRECTION;
  5698. packet.gid = bl.id;
  5699. packet.type = 0;
  5700. clif_send( &packet, sizeof( packet ), &bl, AREA );
  5701. if (disguised(&bl)) {
  5702. clif_spawn(&bl);
  5703. }
  5704. }
  5705. /// Sets the map property
  5706. /// 0199 <type>.W (ZC_NOTIFY_MAPPROPERTY)
  5707. /// 099b <type>.W <flags>.L (ZC_MAPPROPERTY_R2)
  5708. void clif_map_property(struct block_list *bl, enum map_property property, enum send_target t)
  5709. {
  5710. #if PACKETVER >= 20121010
  5711. int16 cmd = 0x99b;
  5712. unsigned char buf[8];
  5713. #else
  5714. int16 cmd = 0x199;
  5715. unsigned char buf[4];
  5716. #endif
  5717. WBUFW(buf,0)=cmd;
  5718. WBUFW(buf,2)=property;
  5719. #if PACKETVER >= 20121010
  5720. struct map_data *mapdata = map_getmapdata(bl->m);
  5721. map_session_data *sd = BL_CAST(BL_PC, bl);
  5722. WBUFL(buf,4) = ((mapdata->getMapFlag(MF_PVP) || (sd && sd->duel_group > 0))<<0)| // PARTY - Show attack cursor on non-party members (PvP)
  5723. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<1)|// GUILD - Show attack cursor on non-guild members (GvG)
  5724. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<2)|// SIEGE - Show emblem over characters heads when in GvG (WoE castle)
  5725. ((mapdata->getMapFlag(MF_FORCEMINEFFECT) || mapdata_flag_gvg2(mapdata))<<3)| // USE_SIMPLE_EFFECT - Forces simpler skill effects, like /mineffect command
  5726. ((mapdata->getMapFlag(MF_NOLOCKON) || mapdata_flag_vs(mapdata) || (sd && sd->duel_group > 0))<<4)| // DISABLE_LOCKON - Only allow attacks on other players with shift key or /ns active
  5727. ((mapdata->getMapFlag(MF_PVP))<<5)| // COUNT_PK - Show the PvP counter
  5728. ((mapdata->getMapFlag(MF_PARTYLOCK))<<6)| // NO_PARTY_FORMATION - Prevents party creation/modification (Might be used for instance dungeons)
  5729. ((mapdata->getMapFlag(MF_BATTLEGROUND))<<7)| // BATTLEFIELD - Unknown (Does something for battlegrounds areas)
  5730. ((mapdata->getMapFlag(MF_NOCOSTUME))<<8)| // DISABLE_COSTUMEITEM - Disable costume sprites
  5731. ((!mapdata->getMapFlag(MF_NOUSECART))<<9)| // USECART - Allow opening cart inventory (Well force it to always allow it)
  5732. ((!mapdata->getMapFlag(MF_NOSUNMOONSTARMIRACLE))<<10); // SUNMOONSTAR_MIRACLE - Allows Star Gladiator's Miracle to activate
  5733. //(1<<11); // Unused bits. 1 - 10 is 0x1 length and 11 is 0x15 length. May be used for future settings.
  5734. #endif
  5735. clif_send(buf,packet_len(cmd),bl,t);
  5736. }
  5737. /// Set the map type.
  5738. /// 01d6 <type>.W (ZC_NOTIFY_MAPPROPERTY2)
  5739. void clif_map_type( map_session_data& sd, e_map_type type ){
  5740. PACKET_ZC_NOTIFY_MAPPROPERTY2 packet{};
  5741. packet.packetType = HEADER_ZC_NOTIFY_MAPPROPERTY2;
  5742. packet.type = static_cast<decltype(packet.type)>(type);
  5743. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5744. }
  5745. /// Updates PvP ranking (ZC_NOTIFY_RANKING).
  5746. /// 019a <id>.L <ranking>.L <total>.L
  5747. void clif_pvpset(map_session_data *sd,int32 pvprank,int32 pvpnum,int32 type)
  5748. {
  5749. if(type == 2) {
  5750. int32 fd = sd->fd;
  5751. WFIFOHEAD(fd,packet_len(0x19a));
  5752. WFIFOW(fd,0) = 0x19a;
  5753. WFIFOL(fd,2) = sd->bl.id;
  5754. WFIFOL(fd,6) = pvprank;
  5755. WFIFOL(fd,10) = pvpnum;
  5756. WFIFOSET(fd,packet_len(0x19a));
  5757. } else {
  5758. unsigned char buf[32];
  5759. WBUFW(buf,0) = 0x19a;
  5760. WBUFL(buf,2) = sd->bl.id;
  5761. if(sd->sc.option&(OPTION_HIDE|OPTION_CLOAK))
  5762. WBUFL(buf,6) = UINT32_MAX; //On client displays as --
  5763. else
  5764. WBUFL(buf,6) = pvprank;
  5765. WBUFL(buf,10) = pvpnum;
  5766. if(pc_isinvisible(sd) || sd->disguise) //Causes crashes when a 'mob' with pvp info dies.
  5767. clif_send(buf,packet_len(0x19a),&sd->bl,SELF);
  5768. else if(!type)
  5769. clif_send(buf,packet_len(0x19a),&sd->bl,AREA);
  5770. else
  5771. clif_send(buf,packet_len(0x19a),&sd->bl,ALL_SAMEMAP);
  5772. }
  5773. }
  5774. /*==========================================
  5775. *
  5776. *------------------------------------------*/
  5777. void clif_map_property_mapall(int32 map_idx, enum map_property property)
  5778. {
  5779. struct block_list bl;
  5780. bl.id = 0;
  5781. bl.type = BL_NUL;
  5782. bl.m = map_idx;
  5783. clif_map_property( &bl, property, ALL_SAMEMAP );
  5784. }
  5785. /// Notifies the client about the result of a refine attempt.
  5786. /// 0188 <result>.W <index>.W <refine>.W (ZC_ACK_ITEMREFINING)
  5787. /// result:
  5788. /// 0 = success
  5789. /// 1 = failure
  5790. /// 2 = downgrade
  5791. /// 3 = failure (?)
  5792. void clif_refine( map_session_data& sd, uint16 index, e_ack_itemrefining result ){
  5793. PACKET_ZC_ACK_ITEMREFINING packet{};
  5794. packet.packetType = HEADER_ZC_ACK_ITEMREFINING;
  5795. packet.result = static_cast<decltype(packet.result)>(result);
  5796. packet.index = client_index( index );
  5797. packet.value = static_cast<decltype(packet.value)>( sd.inventory.u.items_inventory[index].refine );
  5798. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5799. }
  5800. /// Notifies the client about the result of a weapon refine attempt (ZC_ACK_WEAPONREFINE).
  5801. /// 0223 <result>.L <nameid>.W
  5802. /// result:
  5803. /// 0 = "weapon upgraded: %s" MsgStringTable[911] in rgb(0,255,255)
  5804. /// 1 = "weapon upgraded: %s" MsgStringTable[912] in rgb(0,205,205)
  5805. /// 2 = "cannot upgrade %s until you level up the upgrade weapon skill" MsgStringTable[913] in rgb(255,200,200)
  5806. /// 3 = "you lack the item %s to upgrade the weapon" MsgStringTable[914] in rgb(255,200,200)
  5807. void clif_upgrademessage( map_session_data* sd, int32 result, t_itemid item_id ){
  5808. PACKET_ZC_ACK_WEAPONREFINE p = {};
  5809. p.packetType = HEADER_ZC_ACK_WEAPONREFINE;
  5810. p.result = result;
  5811. p.itemId = client_nameid( item_id );
  5812. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  5813. }
  5814. /// Whisper is transmitted to the destination player (ZC_WHISPER).
  5815. /// 0097 <packet len>.W <nick>.24B <message>.?B
  5816. /// 0097 <packet len>.W <nick>.24B <isAdmin>.L <message>.?B (PACKETVER >= 20091104)
  5817. void clif_wis_message(map_session_data* sd, const char* nick, const char* mes, size_t mes_len, int32 gmlvl){
  5818. PACKET_ZC_WHISPER* p = reinterpret_cast<PACKET_ZC_WHISPER*>( packet_buffer );
  5819. p->PacketType = HEADER_ZC_WHISPER;
  5820. p->PacketLength = sizeof( *p );
  5821. safestrncpy( p->sender, nick, NAME_LENGTH );
  5822. safestrncpy( p->message, mes, mes_len );
  5823. p->PacketLength += static_cast<decltype( p->PacketLength )>( mes_len );
  5824. #if PACKETVER >= 20091104
  5825. map_session_data* ssd = map_nick2sd(nick, false);
  5826. // If it is not a message from the server or a player from another map-server
  5827. if( ssd != nullptr ){
  5828. gmlvl = pc_get_group_level(ssd);
  5829. }
  5830. p->isAdmin = (gmlvl == 99) ? 1 : 0;
  5831. #if PACKETVER_MAIN_NUM >= 20131204 || PACKETVER_RE_NUM >= 20131120 || defined(PACKETVER_ZERO)
  5832. if( ssd != nullptr ){
  5833. p->senderGID = ssd->bl.id;
  5834. }else{
  5835. p->senderGID = 0;
  5836. }
  5837. #endif
  5838. #endif
  5839. clif_send( p, p->PacketLength, &sd->bl, SELF );
  5840. }
  5841. /// Inform the player about the result of his whisper action
  5842. /// 0098 <result>.B (ZC_ACK_WHISPER).
  5843. /// 09df <result>.B <CID>.L (ZC_ACK_WHISPER02).
  5844. /// result:
  5845. /// 0 = success to send whisper
  5846. /// 1 = target character is not loged in
  5847. /// 2 = ignored by target
  5848. /// 3 = everyone ignored by target
  5849. void clif_wis_end( map_session_data& sd, e_ack_whisper result ){
  5850. PACKET_ZC_ACK_WHISPER packet{};
  5851. packet.packetType = HEADER_ZC_ACK_WHISPER;
  5852. packet.result = static_cast<decltype(packet.result)>(result);
  5853. #if PACKETVER >= 20131223
  5854. packet.CID = sd.status.char_id;
  5855. #endif
  5856. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5857. }
  5858. /// Returns character name requested by char_id.
  5859. /// 0194 <char id>.L <name>.24B (ZC_ACK_REQNAME_BYGID)
  5860. /// 0af7 <flag>.W <char id>.L <name>.24B (ZC_ACK_REQNAME_BYGID)
  5861. void clif_solved_charname( map_session_data& sd, uint32 charid, const char* name ){
  5862. PACKET_ZC_ACK_REQNAME_BYGID packet{};
  5863. packet.packetType = HEADER_ZC_ACK_REQNAME_BYGID;
  5864. #if PACKETVER >= 20180221
  5865. packet.flag = name[0] ? 3 : 2;
  5866. #endif
  5867. packet.CID = charid;
  5868. safestrncpy( packet.name, name, NAME_LENGTH );
  5869. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5870. }
  5871. /// Presents a list of items that can be carded/composed (ZC_ITEMCOMPOSITION_LIST).
  5872. /// 017b <packet len>.W { <name id>.W }*
  5873. void clif_use_card(map_session_data *sd,int32 idx)
  5874. {
  5875. int32 i,c,ep;
  5876. int32 fd=sd->fd;
  5877. nullpo_retv(sd);
  5878. if (idx < 0 || idx >= MAX_INVENTORY) //Crash-fix from bad packets.
  5879. return;
  5880. if (!sd->inventory_data[idx] || sd->inventory_data[idx]->type != IT_CARD)
  5881. return; //Avoid parsing invalid item indexes (no card/no item)
  5882. ep=sd->inventory_data[idx]->equip;
  5883. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5884. WFIFOW(fd,0)=0x17b;
  5885. for(i=c=0;i<MAX_INVENTORY;i++){
  5886. int32 j;
  5887. if(sd->inventory_data[i] == nullptr)
  5888. continue;
  5889. if(sd->inventory_data[i]->type!=IT_WEAPON && sd->inventory_data[i]->type!=IT_ARMOR)
  5890. continue;
  5891. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0])) //Can't slot it
  5892. continue;
  5893. if(sd->inventory.u.items_inventory[i].identify==0 ) //Not identified
  5894. continue;
  5895. if((sd->inventory_data[i]->equip&ep)==0) //Not equippable on this part.
  5896. continue;
  5897. if(sd->inventory_data[i]->type==IT_WEAPON && ep==EQP_SHIELD) //Shield card won't go on left weapon.
  5898. continue;
  5899. if(sd->inventory_data[i]->type == IT_ARMOR && (ep & EQP_ACC) && ((ep & EQP_ACC) != EQP_ACC) && ((sd->inventory_data[i]->equip & EQP_ACC) != (ep & EQP_ACC)) ) // specific accessory-card can only be inserted to specific accessory.
  5900. continue;
  5901. ARR_FIND( 0, sd->inventory_data[i]->slots, j, sd->inventory.u.items_inventory[i].card[j] == 0 );
  5902. if( j == sd->inventory_data[i]->slots ) // No room
  5903. continue;
  5904. if( sd->inventory.u.items_inventory[i].equip > 0 ) // Do not check items that are already equipped
  5905. continue;
  5906. WFIFOW(fd,4+c*2)=i+2;
  5907. c++;
  5908. }
  5909. if( !c ) return; // no item is available for card insertion
  5910. WFIFOW(fd,2)=4+c*2;
  5911. WFIFOSET(fd,WFIFOW(fd,2));
  5912. }
  5913. /// Notifies the client about the result of item carding/composition.
  5914. /// 017d <equip index>.W <card index>.W <result>.B (ZC_ACK_ITEMCOMPOSITION)
  5915. /// result:
  5916. /// 0 = success
  5917. /// 1 = failure
  5918. void clif_insert_card( map_session_data& sd, int32 idx_equip, int32 idx_card, bool failure ){
  5919. PACKET_ZC_ACK_ITEMCOMPOSITION packet{};
  5920. packet.packetType = HEADER_ZC_ACK_ITEMCOMPOSITION;
  5921. packet.equipIndex = client_index( idx_equip );
  5922. packet.cardIndex = client_index( idx_card );
  5923. packet.result = failure;
  5924. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5925. }
  5926. /// Presents a list of items that can be identified (ZC_ITEMIDENTIFY_LIST).
  5927. /// 0177 <packet len>.W { <index>.W }*
  5928. void clif_item_identify_list(map_session_data *sd)
  5929. {
  5930. int32 i,c;
  5931. int32 fd;
  5932. nullpo_retv(sd);
  5933. fd=sd->fd;
  5934. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5935. WFIFOW(fd,0)=0x177;
  5936. for(i=c=0;i<MAX_INVENTORY;i++){
  5937. if(sd->inventory.u.items_inventory[i].nameid > 0 && !sd->inventory.u.items_inventory[i].identify){
  5938. WFIFOW(fd,c*2+4)=client_index(i);
  5939. c++;
  5940. }
  5941. }
  5942. if(c > 0) {
  5943. WFIFOW(fd,2)=c*2+4;
  5944. WFIFOSET(fd,WFIFOW(fd,2));
  5945. sd->menuskill_id = MC_IDENTIFY;
  5946. sd->menuskill_val = c;
  5947. sd->state.workinprogress = WIP_DISABLE_ALL;
  5948. }
  5949. }
  5950. /// Notifies the client about the result of a item identify request.
  5951. /// 0179 <index>.W <result>.B (ZC_ACK_ITEMIDENTIFY)
  5952. /// result:
  5953. /// 0 = success
  5954. /// 1 = failure
  5955. void clif_item_identified( map_session_data& sd, int32 idx, bool failure ){
  5956. PACKET_ZC_ACK_ITEMIDENTIFY packet{};
  5957. packet.packetType = HEADER_ZC_ACK_ITEMIDENTIFY;
  5958. packet.index = client_index( idx );
  5959. packet.result = failure;
  5960. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5961. }
  5962. /// Presents a list of items that can be repaired.
  5963. /// 01fc <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }* (ZC_REPAIRITEMLIST)
  5964. void clif_item_repair_list( map_session_data& sd, map_session_data& dstsd, uint16 lv ){
  5965. PACKET_ZC_REPAIRITEMLIST* p = reinterpret_cast<PACKET_ZC_REPAIRITEMLIST*>( packet_buffer );
  5966. p->packetType = HEADER_ZC_REPAIRITEMLIST;
  5967. p->packetLength = sizeof( *p );
  5968. size_t c = 0;
  5969. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  5970. if( dstsd.inventory.u.items_inventory[i].nameid > 0 && dstsd.inventory.u.items_inventory[i].attribute != 0 && !itemdb_ishatched_egg( &dstsd.inventory.u.items_inventory[i] ) ){ // && skill_can_repair(sd,nameid)){
  5971. p->items[c].index = static_cast<decltype( p->items[0].index )>( i );
  5972. p->items[c].itemId = client_nameid( dstsd.inventory.u.items_inventory[i].nameid );
  5973. p->items[c].refine = dstsd.inventory.u.items_inventory[i].refine;
  5974. clif_addcards( &p->items[c].slot, &dstsd.inventory.u.items_inventory[i] );
  5975. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  5976. c++;
  5977. }
  5978. }
  5979. if( c > 0 ){
  5980. clif_send( p, p->packetLength, &sd.bl, SELF );
  5981. sd.menuskill_id = BS_REPAIRWEAPON;
  5982. sd.menuskill_val = dstsd.bl.id;
  5983. sd.menuskill_val2 = lv;
  5984. }else{
  5985. clif_skill_fail( sd, sd.ud.skill_id );
  5986. }
  5987. }
  5988. /// Notifies the client about the result of a item repair request.
  5989. /// 01fe <index>.W <result>.B (ZC_ACK_ITEMREPAIR)
  5990. /// index:
  5991. /// ignored (inventory index)
  5992. /// result:
  5993. /// 0 = Item repair success.
  5994. /// 1 = Item repair failure.
  5995. void clif_item_repaireffect( map_session_data& sd, int32 idx, bool failure ){
  5996. PACKET_ZC_ACK_ITEMREPAIR packet{};
  5997. packet.packetType = HEADER_ZC_ACK_ITEMREPAIR;
  5998. packet.index = client_index( idx );
  5999. packet.result = failure;
  6000. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6001. }
  6002. /// Displays a message, that an equipment got damaged.
  6003. /// 02bb <equip location>.W <account id>.L (ZC_EQUIPITEM_DAMAGED)
  6004. void clif_item_damaged( map_session_data& sd, uint16 position ){
  6005. PACKET_ZC_EQUIPITEM_DAMAGED packet{};
  6006. packet.packetType = HEADER_ZC_EQUIPITEM_DAMAGED;
  6007. packet.equipLocation = position;
  6008. packet.GID = sd.bl.id;
  6009. // TODO: the packet seems to be sent to other people as well, probably party and/or guild.
  6010. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6011. }
  6012. /// Presents a list of weapon items that can be refined [Taken from jAthena] (ZC_NOTIFY_WEAPONITEMLIST).
  6013. /// 0221 <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }*
  6014. void clif_item_refine_list( map_session_data& sd ){
  6015. PACKET_ZC_NOTIFY_WEAPONITEMLIST* p = reinterpret_cast<PACKET_ZC_NOTIFY_WEAPONITEMLIST*>( packet_buffer );
  6016. p->packetType = HEADER_ZC_NOTIFY_WEAPONITEMLIST;
  6017. p->packetLength = sizeof( *p );
  6018. uint16 skill_lv = pc_checkskill( &sd, WS_WEAPONREFINE );
  6019. int32 refine_item[MAX_WEAPON_LEVEL];
  6020. refine_item[0] = pc_search_inventory( &sd, ITEMID_PHRACON );
  6021. refine_item[1] = pc_search_inventory( &sd, ITEMID_EMVERETARCON );
  6022. refine_item[2] = refine_item[3] = pc_search_inventory( &sd, ITEMID_ORIDECON );
  6023. #ifdef RENEWAL
  6024. refine_item[4] = -1;
  6025. #endif
  6026. int32 count = 0;
  6027. for( int32 i = 0; i < MAX_INVENTORY; i++ ){
  6028. if( sd.inventory.u.items_inventory[i].nameid > 0 && sd.inventory.u.items_inventory[i].refine < skill_lv &&
  6029. sd.inventory_data[i] != nullptr && sd.inventory_data[i]->type == IT_WEAPON &&
  6030. sd.inventory.u.items_inventory[i].identify && sd.inventory_data[i]->weapon_level >= 1 &&
  6031. refine_item[sd.inventory_data[i]->weapon_level - 1] != -1 && !( sd.inventory.u.items_inventory[i].equip & EQP_ARMS ) ){
  6032. p->items[count].index = client_index( i );
  6033. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  6034. p->items[count].refine = sd.inventory.u.items_inventory[i].refine;
  6035. clif_addcards( &p->items[count].slot, &sd.inventory.u.items_inventory[i] );
  6036. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  6037. count++;
  6038. }
  6039. }
  6040. clif_send( p, p->packetLength, &sd.bl, SELF );
  6041. if( count > 0 ){
  6042. sd.menuskill_id = WS_WEAPONREFINE;
  6043. sd.menuskill_val = skill_lv;
  6044. }
  6045. }
  6046. /// Notification of an auto-casted skill (ZC_AUTORUN_SKILL).
  6047. /// 0147 <skill id>.W <type>.L <level>.W <sp cost>.W <atk range>.W <skill name>.24B <upgradable>.B
  6048. void clif_item_skill( map_session_data *sd, uint16 skill_id, uint16 skill_lv ){
  6049. nullpo_retv( sd );
  6050. PACKET_ZC_AUTORUN_SKILL p = {};
  6051. p.packetType = HEADER_ZC_AUTORUN_SKILL;
  6052. p.skill_id = skill_id;
  6053. p.skill_type = skill_get_inf( skill_id );
  6054. p.skill_lv = skill_lv;
  6055. p.skill_sp = skill_get_sp( skill_id, skill_lv );
  6056. p.skill_range = skill_get_range2( &sd->bl, skill_id, skill_lv, false );
  6057. safestrncpy( p.skill_name, skill_get_name( skill_id ), NAME_LENGTH );
  6058. p.up_flag = false;
  6059. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6060. }
  6061. /// Adds an item to character's cart.
  6062. /// 0124 <index>.W <amount>.L <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_ITEM_TO_CART)
  6063. /// 01c5 <index>.W <amount>.L <name id>.W <type>.B <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_ITEM_TO_CART2)
  6064. /// 0a0b <index>.W <amount>.L <name id>.W <type>.B <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W { <option id>.W <option value>.W <option param>.B }*5 (ZC_ADD_ITEM_TO_CART3)
  6065. void clif_cart_additem( map_session_data *sd, int32 n, int32 amount ){
  6066. nullpo_retv( sd );
  6067. int32 fd = sd->fd;
  6068. if( !session_isActive( fd ) ){
  6069. return;
  6070. }
  6071. if( n < 0 || n >= MAX_CART || sd->cart.u.items_cart[n].nameid == 0 ){
  6072. return;
  6073. }
  6074. PACKET_ZC_ADD_ITEM_TO_CART p = {};
  6075. p.packetType = HEADER_ZC_ADD_ITEM_TO_CART;
  6076. p.index = client_index( n );
  6077. p.amount = amount;
  6078. p.itemId = client_nameid( sd->cart.u.items_cart[n].nameid );
  6079. #if PACKETVER >= 5
  6080. p.itemType = itemdb_type( sd->cart.u.items_cart[n].nameid );
  6081. #endif
  6082. p.identified = sd->cart.u.items_cart[n].identify;
  6083. p.damaged = sd->cart.u.items_cart[n].attribute;
  6084. p.refine = sd->cart.u.items_cart[n].refine;
  6085. clif_addcards( &p.slot, &sd->cart.u.items_cart[n] );
  6086. #if PACKETVER >= 20150226
  6087. clif_add_random_options( p.option_data, sd->cart.u.items_cart[n] );
  6088. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6089. p.grade = sd->cart.u.items_cart[n].enchantgrade;
  6090. #endif
  6091. #endif
  6092. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6093. }
  6094. /// Acknowledge an item have been added to cart
  6095. /// 012c <result>.B (ZC_ACK_ADDITEM_TO_CART)
  6096. /// result:
  6097. /// 0 = ADDITEM_TO_CART_FAIL_WEIGHT
  6098. /// 1 = ADDITEM_TO_CART_FAIL_COUNT
  6099. void clif_cart_additem_ack( map_session_data& sd, e_ack_additem_to_cart flag ){
  6100. PACKET_ZC_ACK_ADDITEM_TO_CART packet{};
  6101. packet.packetType = HEADER_ZC_ACK_ADDITEM_TO_CART;
  6102. packet.result = static_cast<decltype(packet.result)>(flag);
  6103. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6104. }
  6105. // 09B7 <unknow data> (ZC_ACK_OPEN_BANKING)
  6106. void clif_bank_open( map_session_data& sd ){
  6107. #if PACKETVER >= 20130717
  6108. PACKET_ZC_ACK_OPEN_BANKING p = {};
  6109. p.packetType = HEADER_ZC_ACK_OPEN_BANKING;
  6110. p.unknown = 0;
  6111. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6112. #endif
  6113. }
  6114. /*
  6115. * Request to Open the banking system
  6116. * 09B6 <aid>L (CZ_REQ_OPEN_BANKING)
  6117. */
  6118. void clif_parse_BankOpen(int32 fd, map_session_data* sd) {
  6119. #if PACKETVER >= 20130717
  6120. const PACKET_CZ_REQ_OPEN_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BANKING*>( RFIFOP( fd, 0 ) );
  6121. //TODO check if preventing trade or stuff like that
  6122. //also mark something in case char ain't available for saving, should we check now ?
  6123. nullpo_retv(sd);
  6124. if( !battle_config.feature_banking ) {
  6125. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6126. return;
  6127. }
  6128. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6129. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6130. return;
  6131. }
  6132. else {
  6133. if(sd->status.account_id == p->AID){
  6134. sd->state.banking = 1;
  6135. //request save ?
  6136. //chrif_bankdata_request(sd->status.account_id, sd->status.char_id);
  6137. //on succes open bank ?
  6138. clif_bank_open( *sd );
  6139. }
  6140. }
  6141. #endif
  6142. }
  6143. // 09B9 <unknow data> (ZC_ACK_CLOSE_BANKING)
  6144. void clif_bank_close( map_session_data& sd ){
  6145. #if PACKETVER >= 20130717
  6146. PACKET_ZC_ACK_CLOSE_BANKING p = {};
  6147. p.packetType = HEADER_ZC_ACK_CLOSE_BANKING;
  6148. p.unknown = 0;
  6149. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6150. #endif
  6151. }
  6152. /*
  6153. * Request to close the banking system
  6154. * 09B8 <aid>L (CZ_REQ_CLOSE_BANKING)
  6155. */
  6156. void clif_parse_BankClose(int32 fd, map_session_data* sd) {
  6157. #if PACKETVER >= 20130717
  6158. const PACKET_CZ_REQ_CLOSE_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_CLOSE_BANKING*>( RFIFOP( fd, 0 ) );
  6159. nullpo_retv(sd);
  6160. if( !battle_config.feature_banking ) {
  6161. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6162. //still allow to go trough to not stuck player if we have disable it while they was in
  6163. }
  6164. if(sd->status.account_id == p->AID){
  6165. sd->state.banking = 0;
  6166. clif_bank_close( *sd );
  6167. }
  6168. #endif
  6169. }
  6170. /*
  6171. * Display how much we got in bank (I suppose)
  6172. * 09A6 <Bank_Vault>Q <Reason>W (ZC_BANKING_CHECK)
  6173. */
  6174. void clif_Bank_Check( map_session_data& sd ){
  6175. #if PACKETVER >= 20130717
  6176. PACKET_ZC_BANKING_CHECK p = {};
  6177. p.packetType = HEADER_ZC_BANKING_CHECK;
  6178. p.money = sd.bank_vault;
  6179. p.reason = 0;
  6180. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6181. #endif
  6182. }
  6183. /*
  6184. * Requesting the data in bank
  6185. * 09AB <aid>L (CZ_REQ_BANKING_CHECK)
  6186. */
  6187. void clif_parse_BankCheck(int32 fd, map_session_data* sd) {
  6188. #if PACKETVER >= 20130717
  6189. const PACKET_CZ_REQ_BANKING_CHECK* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_CHECK*>( RFIFOP( fd, 0 ) );
  6190. nullpo_retv(sd);
  6191. if( !battle_config.feature_banking ) {
  6192. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6193. return;
  6194. }
  6195. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6196. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6197. return;
  6198. }
  6199. else {
  6200. if(sd->status.account_id == p->AID) //since we have it let check it for extra security
  6201. clif_Bank_Check( *sd );
  6202. }
  6203. #endif
  6204. }
  6205. /*
  6206. * Acknowledge of deposit some money in bank
  6207. 09A8 <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_DEPOSIT)
  6208. */
  6209. void clif_bank_deposit( map_session_data& sd, enum e_BANKING_DEPOSIT_ACK reason ){
  6210. #if PACKETVER >= 20130717
  6211. PACKET_ZC_ACK_BANKING_DEPOSIT p = {};
  6212. p.packetType = HEADER_ZC_ACK_BANKING_DEPOSIT;
  6213. p.money = sd.bank_vault;
  6214. p.zeny = sd.status.zeny;
  6215. p.reason = reason;
  6216. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6217. #endif
  6218. }
  6219. /*
  6220. * Request saving some money in bank
  6221. * @author : original [Yommy/Hercules]
  6222. * 09A7 <AID>L <Money>L (CZ_REQ_BANKING_DEPOSIT)
  6223. */
  6224. void clif_parse_BankDeposit(int32 fd, map_session_data* sd) {
  6225. #if PACKETVER >= 20130717
  6226. const PACKET_CZ_REQ_BANKING_DEPOSIT* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_DEPOSIT*>( RFIFOP( fd, 0 ) );
  6227. nullpo_retv(sd);
  6228. if( !battle_config.feature_banking ) {
  6229. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6230. return;
  6231. }
  6232. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6233. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6234. return;
  6235. }
  6236. else {
  6237. if(sd->status.account_id == p->AID){
  6238. enum e_BANKING_DEPOSIT_ACK reason = pc_bank_deposit(sd,max(0,p->zeny));
  6239. clif_bank_deposit( *sd, reason );
  6240. }
  6241. }
  6242. #endif
  6243. }
  6244. /*
  6245. * Acknowledge of withdrawing some money from bank
  6246. * 09AA <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_WITHDRAW)
  6247. */
  6248. void clif_bank_withdraw( map_session_data& sd, enum e_BANKING_WITHDRAW_ACK reason ){
  6249. #if PACKETVER >= 20130717
  6250. PACKET_ZC_ACK_BANKING_WITHDRAW p = {};
  6251. p.packetType = HEADER_ZC_ACK_BANKING_WITHDRAW;
  6252. p.reason = reason;
  6253. p.money = sd.bank_vault;
  6254. p.zeny = sd.status.zeny;
  6255. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6256. #endif
  6257. }
  6258. /*
  6259. * Request Withdrawing some money from bank
  6260. * 09A9 <AID>L <Money>L (CZ_REQ_BANKING_WITHDRAW)
  6261. */
  6262. void clif_parse_BankWithdraw(int32 fd, map_session_data* sd) {
  6263. #if PACKETVER >= 20130717
  6264. const PACKET_CZ_REQ_BANKING_WITHDRAW* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_WITHDRAW*>( RFIFOP( fd, 0 ) );
  6265. nullpo_retv(sd);
  6266. if( !battle_config.feature_banking ) {
  6267. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6268. return;
  6269. }
  6270. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6271. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6272. return;
  6273. }
  6274. else {
  6275. if(sd->status.account_id == p->AID){
  6276. enum e_BANKING_WITHDRAW_ACK reason = pc_bank_withdraw(sd,max(0,p->zeny));
  6277. clif_bank_withdraw( *sd, reason );
  6278. }
  6279. }
  6280. #endif
  6281. }
  6282. /// Deletes an item from character's cart.
  6283. /// 0125 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_CART)
  6284. void clif_cart_delitem( map_session_data& sd, int32 index, int32 amount ){
  6285. PACKET_ZC_DELETE_ITEM_FROM_CART packet{};
  6286. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_CART;
  6287. packet.index = client_index( index );
  6288. packet.amount = amount;
  6289. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6290. }
  6291. /// Opens the shop creation menu.
  6292. /// 012d <num>.W (ZC_OPENSTORE)
  6293. /// num:
  6294. /// number of allowed item slots
  6295. void clif_openvendingreq( map_session_data& sd, uint16 num ){
  6296. PACKET_ZC_OPENSTORE packet{};
  6297. packet.packetType = HEADER_ZC_OPENSTORE;
  6298. packet.num = num;
  6299. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6300. }
  6301. /// Displays a vending board to target/area (ZC_STORE_ENTRY).
  6302. /// 0131 <owner id>.L <message>.80B
  6303. void clif_showvendingboard( map_session_data& sd, enum send_target target, struct block_list* tbl ){
  6304. if( tbl == nullptr ){
  6305. tbl = &sd.bl;
  6306. target = AREA_WOS;
  6307. }
  6308. PACKET_ZC_STORE_ENTRY p = {};
  6309. p.packetType = HEADER_ZC_STORE_ENTRY;
  6310. p.makerAID = sd.status.account_id;
  6311. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  6312. clif_send( &p, sizeof( p ), tbl, target );
  6313. }
  6314. /// Removes a vending board from screen (ZC_DISAPPEAR_ENTRY).
  6315. /// 0132 <owner id>.L
  6316. void clif_closevendingboard(struct block_list* bl, int32 fd)
  6317. {
  6318. unsigned char buf[16];
  6319. nullpo_retv(bl);
  6320. WBUFW(buf,0) = 0x132;
  6321. WBUFL(buf,2) = bl->id;
  6322. if( session_isActive(fd) ) {
  6323. WFIFOHEAD(fd,packet_len(0x132));
  6324. memcpy(WFIFOP(fd,0),buf,packet_len(0x132));
  6325. WFIFOSET(fd,packet_len(0x132));
  6326. } else {
  6327. clif_send(buf,packet_len(0x132),bl,AREA_WOS);
  6328. }
  6329. }
  6330. /// Sends a list of items in a shop.
  6331. /// R 0133 <packet len>.W <owner id>.L { <price>.L <amount>.W <index>.W <type>.B <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }* (ZC_PC_PURCHASE_ITEMLIST_FROMMC)
  6332. /// R 0800 <packet len>.W <owner id>.L <unique id>.L { <price>.L <amount>.W <index>.W <type>.B <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }* (ZC_PC_PURCHASE_ITEMLIST_FROMMC2)
  6333. void clif_vendinglist( map_session_data& sd, map_session_data& vsd ){
  6334. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC*>( packet_buffer );
  6335. p->packetType = HEADER_ZC_PC_PURCHASE_ITEMLIST_FROMMC;
  6336. p->packetLength = sizeof( *p );
  6337. p->AID = vsd.status.account_id;
  6338. #if PACKETVER >= 20100105
  6339. p->venderId = vsd.vender_id;
  6340. #endif
  6341. for( int32 i = 0, count = 0; i < vsd.vend_num; i++ ){
  6342. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC_sub& entry = p->items[count];
  6343. int32 index = vsd.vending[i].index;
  6344. struct item_data* data = itemdb_search( vsd.cart.u.items_cart[index].nameid );
  6345. if( data == nullptr ){
  6346. continue;
  6347. }
  6348. entry.price = vsd.vending[i].value;
  6349. entry.amount = vsd.vending[i].amount;
  6350. entry.index = client_index( index );
  6351. entry.itemType = itemtype( vsd.cart.u.items_cart[index].nameid );
  6352. entry.itemId = client_nameid( vsd.cart.u.items_cart[index].nameid );
  6353. entry.identified = vsd.cart.u.items_cart[index].identify;
  6354. entry.damaged = vsd.cart.u.items_cart[index].attribute;
  6355. entry.refine = vsd.cart.u.items_cart[index].refine;
  6356. clif_addcards( &entry.slot, &vsd.cart.u.items_cart[index] );
  6357. #if PACKETVER >= 20150226
  6358. clif_add_random_options( entry.option_data, vsd.cart.u.items_cart[index] );
  6359. #if PACKETVER >= 20160921
  6360. entry.location = pc_equippoint_sub( &sd, data );
  6361. entry.viewSprite = data->look;
  6362. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6363. entry.grade = vsd.cart.u.items_cart[index].enchantgrade;
  6364. #endif
  6365. #endif
  6366. #endif
  6367. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6368. count++;
  6369. }
  6370. clif_send( p, p->packetLength, &sd.bl, SELF );
  6371. }
  6372. /// Shop purchase failure.
  6373. /// 0135 <index>.W <amount>.W <result>.B (ZC_PC_PURCHASE_RESULT_FROMMC)
  6374. /// result:
  6375. /// 0 = success
  6376. /// 1 = not enough zeny
  6377. /// 2 = overweight
  6378. /// 4 = out of stock
  6379. /// 5 = "cannot use an npc shop while in a trade"
  6380. /// 6 = Because the store information was incorrect the item was not purchased.
  6381. /// 7 = No sales information.
  6382. void clif_buyvending( map_session_data& sd, uint16 index, uint16 amount, e_pc_purchase_result_frommc result ){
  6383. PACKET_ZC_PC_PURCHASE_RESULT_FROMMC packet{};
  6384. packet.packetType = HEADER_ZC_PC_PURCHASE_RESULT_FROMMC;
  6385. packet.index = client_index( index );
  6386. packet.amount = amount;
  6387. packet.result = static_cast<decltype(packet.result)>(result);
  6388. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6389. }
  6390. /// Show's vending player its list of items for sale.
  6391. /// 0a28 <result>.B (ZC_ACK_OPENSTORE2)
  6392. void clif_openvending_ack( map_session_data& sd, e_ack_openstore2 result ){
  6393. #if PACKETVER >= 20141022
  6394. PACKET_ZC_ACK_OPENSTORE2 packet{};
  6395. packet.packetType = HEADER_ZC_ACK_OPENSTORE2;
  6396. packet.result = static_cast<decltype(packet.result)>(result);
  6397. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6398. #endif
  6399. }
  6400. /// Shop creation success.
  6401. /// 0136 <packet len>.W <owner id>.L { <price>.L <index>.W <amount>.W <type>.B <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }* (ZC_PC_PURCHASE_MYITEMLIST)
  6402. void clif_openvending( map_session_data& sd ){
  6403. PACKET_ZC_PC_PURCHASE_MYITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_MYITEMLIST*>( packet_buffer );
  6404. p->packetType = HEADER_ZC_PC_PURCHASE_MYITEMLIST;
  6405. p->packetLength = sizeof( *p );
  6406. p->AID = sd.bl.id;
  6407. for( int32 i = 0; i < sd.vend_num; i++ ) {
  6408. PACKET_ZC_PC_PURCHASE_MYITEMLIST_sub& entry = p->items[i];
  6409. int16 index = sd.vending[i].index;
  6410. entry.price = sd.vending[i].value;
  6411. entry.index = client_index( index );
  6412. entry.amount = sd.vending[i].amount;
  6413. entry.itemType = itemtype( sd.cart.u.items_cart[index].nameid );
  6414. entry.itemId = client_nameid( sd.cart.u.items_cart[index].nameid );
  6415. entry.identified = sd.cart.u.items_cart[index].identify;
  6416. entry.damaged = sd.cart.u.items_cart[index].attribute;
  6417. entry.refine = sd.cart.u.items_cart[index].refine;
  6418. clif_addcards( &entry.slot, &sd.cart.u.items_cart[index] );
  6419. #if PACKETVER >= 20150226
  6420. clif_add_random_options( entry.option_data, sd.cart.u.items_cart[index] );
  6421. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6422. entry.grade = sd.cart.u.items_cart[index].enchantgrade;
  6423. #endif
  6424. #endif
  6425. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6426. }
  6427. clif_send( p, p->packetLength, &sd.bl, SELF );
  6428. clif_openvending_ack( sd, OPENSTORE2_SUCCESS );
  6429. }
  6430. /// Inform merchant that someone has bought an item.
  6431. /// 0137 <index>.W <amount>.W (ZC_DELETEITEM_FROM_MCSTORE)
  6432. /// 09e5 <index>.W <amount>.W <GID>.L <Date>.L <zeny>.L (ZC_DELETEITEM_FROM_MCSTORE2)
  6433. void clif_vendingreport( map_session_data& sd, uint16 index, uint16 amount, uint32 char_id, int32 zeny ){
  6434. PACKET_ZC_DELETEITEM_FROM_MCSTORE packet{};
  6435. packet.packetType = HEADER_ZC_DELETEITEM_FROM_MCSTORE;
  6436. packet.index = client_index( index );
  6437. packet.amount = amount;
  6438. // TODO : not sure for client date [Napster]
  6439. #if PACKETVER >= 20141016
  6440. packet.buyerCID = char_id;
  6441. packet.date = client_tick( time(nullptr) );
  6442. packet.zeny = zeny;
  6443. #endif
  6444. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6445. }
  6446. /// Result of organizing a party (ZC_ACK_MAKE_GROUP).
  6447. /// 00fa <result>.B
  6448. /// result:
  6449. /// 0 = opens party window and shows MsgStringTable[77]="party successfully organized"
  6450. /// 1 = MsgStringTable[78]="party name already exists"
  6451. /// 2 = MsgStringTable[79]="already in a party"
  6452. /// 3 = cannot organize parties on this map
  6453. /// ? = nothing
  6454. void clif_party_created( map_session_data& sd, int32 result ){
  6455. PACKET_ZC_ACK_MAKE_GROUP p = {};
  6456. p.PacketType = HEADER_ZC_ACK_MAKE_GROUP;
  6457. p.result = result;
  6458. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6459. }
  6460. /// Adds new member to a party.
  6461. /// 0104 <account id>.L <role>.L <x>.W <y>.W <state>.B <party name>.24B <char name>.24B <map name>.16B (ZC_ADD_MEMBER_TO_GROUP)
  6462. /// 01e9 <account id>.L <role>.L <x>.W <y>.W <state>.B <party name>.24B <char name>.24B <map name>.16B <item pickup rule>.B <item share rule>.B (ZC_ADD_MEMBER_TO_GROUP2)
  6463. /// 0a43 <account id>.L <role>.L <class>.W <base level>.W <x>.W <y>.W <state>.B <party name>.24B <char name>.24B <map name>.16B <item pickup rule>.B <item share rule>.B (ZC_ADD_MEMBER_TO_GROUP3)
  6464. /// role:
  6465. /// 0 = leader
  6466. /// 1 = normal
  6467. /// state:
  6468. /// 0 = connected
  6469. /// 1 = disconnected
  6470. void clif_party_member_info( struct party_data& party, map_session_data& sd ){
  6471. int32 id = party_getmemberid( &party, &sd );
  6472. if( id < 0 ){
  6473. return;
  6474. }
  6475. PACKET_ZC_ADD_MEMBER_TO_GROUP p = {};
  6476. p.packetType = partymemberinfo;
  6477. p.AID = sd.status.account_id;
  6478. #if PACKETVER >= 20171207
  6479. p.GID = sd.status.char_id;
  6480. #endif
  6481. p.leader = ( party.party.member[id].leader != 0 ) ? 0 : 1;
  6482. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6483. p.class_ = sd.status.class_;
  6484. p.baseLevel = sd.status.base_level;
  6485. #endif
  6486. p.x = sd.bl.x;
  6487. p.y = sd.bl.y;
  6488. p.offline = ( party.party.member[id].online != 0 ) ? 0 : 1;
  6489. safestrncpy( p.partyName, party.party.name, sizeof( p.partyName ) );
  6490. safestrncpy( p.playerName, sd.status.name, sizeof( p.playerName ) );
  6491. mapindex_getmapname_ext( map_mapid2mapname( sd.bl.m ), p.mapName );
  6492. p.sharePickup = ( party.party.item&1 ) ? 1 : 0;
  6493. p.shareLoot = ( party.party.item&2 )? 1 : 0;
  6494. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6495. }
  6496. /// Sends party information (ZC_GROUP_LIST).
  6497. /// 00fb <packet len>.W <party name>.24B { <account id>.L <nick>.24B <map name>.16B <role>.B <state>.B }*
  6498. /// role:
  6499. /// 0 = leader
  6500. /// 1 = normal
  6501. /// state:
  6502. /// 0 = connected
  6503. /// 1 = disconnected
  6504. /// 0a44 <packet len>.W <party name>.24B { <account id>.L <nick>.24B <map name>.16B <role>.B <state>.B <class>.W <base level>.W }* <item pickup rule>.B <item share rule>.B <unknown>.L
  6505. void clif_party_info( struct party_data& party, map_session_data* sd ){
  6506. send_target target;
  6507. if( sd == nullptr ){
  6508. sd = party_getavailablesd( &party );
  6509. if( sd == nullptr ){
  6510. // Nobody is online to notify
  6511. return;
  6512. }
  6513. // Send to whole party
  6514. target = PARTY;
  6515. }else{
  6516. // Send only to self
  6517. target = SELF;
  6518. }
  6519. PACKET_ZC_GROUP_LIST* p = reinterpret_cast<PACKET_ZC_GROUP_LIST*>( packet_buffer );
  6520. p->packetType = partyinfo;
  6521. p->packetLen = sizeof( *p );
  6522. safestrncpy( p->partyName, party.party.name, sizeof( p->partyName ) );
  6523. for( int32 i = 0, c = 0; i < MAX_PARTY; i++ ){
  6524. struct party_member& m = party.party.member[i];
  6525. if( m.account_id == 0 ){
  6526. continue;
  6527. }
  6528. PACKET_ZC_GROUP_LIST_SUB& member = p->members[c];
  6529. member.AID = m.account_id;
  6530. #if PACKETVER >= 20171207
  6531. member.GID = m.char_id;
  6532. #endif
  6533. safestrncpy( member.playerName, m.name, sizeof( member.playerName ) );
  6534. mapindex_getmapname_ext( m.map, member.mapName );
  6535. member.leader = ( m.leader ) ? 0 : 1;
  6536. member.offline = ( m.online ) ? 0 : 1;
  6537. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6538. member.class_ = m.class_;
  6539. member.baseLevel = m.lv;
  6540. #endif
  6541. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( member ) );
  6542. c++;
  6543. }
  6544. clif_send( p, p->packetLen, &sd->bl, target );
  6545. }
  6546. /// The player's 'party invite' state, sent during login (ZC_PARTY_CONFIG).
  6547. /// 02c9 <flag>.B
  6548. /// flag:
  6549. /// 0 = allow party invites
  6550. /// 1 = auto-deny party invites
  6551. void clif_partyinvitationstate( map_session_data& sd ){
  6552. #if PACKETVER_MAIN_NUM >= 20070911 || defined(PACKETVER_RE) || PACKETVER_AD_NUM >= 20070911 || PACKETVER_SAK_NUM >= 20070904 || defined(PACKETVER_ZERO)
  6553. PACKET_ZC_PARTY_CONFIG p = {};
  6554. p.packetType = HEADER_ZC_PARTY_CONFIG;
  6555. p.denyPartyInvites = sd.status.disable_partyinvite;
  6556. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6557. #endif
  6558. }
  6559. /// Party invitation request.
  6560. /// 00fe <party id>.L <party name>.24B (ZC_REQ_JOIN_GROUP)
  6561. /// 02c6 <party id>.L <party name>.24B (ZC_PARTY_JOIN_REQ)
  6562. void clif_party_invite( map_session_data& sd, map_session_data& tsd ){
  6563. struct party_data* party = party_search( sd.status.party_id );
  6564. if( party == nullptr ){
  6565. return;
  6566. }
  6567. PACKET_ZC_PARTY_JOIN_REQ p = {};
  6568. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ;
  6569. p.GRID = party->party.party_id;
  6570. safestrncpy( p.groupName, party->party.name, sizeof( p.groupName ) );
  6571. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6572. }
  6573. /// Party invite result.
  6574. /// 00fd <nick>.24S <result>.B (ZC_ACK_REQ_JOIN_GROUP)
  6575. /// 02c5 <nick>.24S <result>.L (ZC_PARTY_JOIN_REQ_ACK)
  6576. /// result=0 : char is already in a party -> MsgStringTable[80]
  6577. /// result=1 : party invite was rejected -> MsgStringTable[81]
  6578. /// result=2 : party invite was accepted -> MsgStringTable[82]
  6579. /// result=3 : party is full -> MsgStringTable[83]
  6580. /// result=4 : char of the same account already joined the party -> MsgStringTable[608]
  6581. /// result=5 : char blocked party invite -> MsgStringTable[1324] (since 20070904)
  6582. /// result=7 : char is not online or doesn't exist -> MsgStringTable[71] (since 20070904)
  6583. /// result=8 : (%s) are currently in restricted map to join a party. -> MsgStringTable[1388] (since 20080527)
  6584. /// result=9 : Cannot join a party in this map. -> MsgStringTable[1871] (since 20110215)
  6585. /// result=10 : You cannot invite or withdraw while in memorial dungeon -> message: MSG_ID_BD3 (since 20161130)
  6586. /// result=11 : The character is a level that can not join the party -> message: MSG_ID_C9A (since 20170412)
  6587. void clif_party_invite_reply( map_session_data& sd, const char* nick, enum e_party_invite_reply reply ){
  6588. #if PACKETVER < 20070904
  6589. if( reply == PARTY_REPLY_OFFLINE ){
  6590. clif_displaymessage( sd.fd, msg_txt( &sd, 3 ) ); // Character not found.
  6591. return;
  6592. }
  6593. #endif
  6594. PACKET_ZC_PARTY_JOIN_REQ_ACK p = {};
  6595. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ_ACK;
  6596. safestrncpy( p.characterName, nick, sizeof( p.characterName ) );
  6597. p.result = reply;
  6598. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6599. }
  6600. /// Updates party settings.
  6601. /// 0101 <exp option>.L (ZC_GROUPINFO_CHANGE)
  6602. /// 07d8 <exp option>.L <item pick rule>.B <item share rule>.B (ZC_REQ_GROUPINFO_CHANGE_V2)
  6603. /// exp option:
  6604. /// 0 = exp sharing disabled
  6605. /// 1 = exp sharing enabled
  6606. /// 2 = cannot change exp sharing
  6607. ///
  6608. /// flag:
  6609. /// 0 = send to party
  6610. /// 1 = send to sd
  6611. void clif_party_option(struct party_data *p,map_session_data *sd,int32 flag)
  6612. {
  6613. unsigned char buf[16];
  6614. #if PACKETVER < 20090603
  6615. const int32 cmd = 0x101;
  6616. #else
  6617. const int32 cmd = 0x7d8;
  6618. #endif
  6619. nullpo_retv(p);
  6620. if(!sd && flag==0){
  6621. int32 i;
  6622. ARR_FIND(0,MAX_PARTY,i,p->data[i].sd);
  6623. if (i < MAX_PARTY)
  6624. sd = p->data[i].sd;
  6625. }
  6626. if(!sd) return;
  6627. WBUFW(buf,0)=cmd;
  6628. WBUFL(buf,2)=((flag&0x01)?2:p->party.exp);
  6629. #if PACKETVER >= 20090603
  6630. WBUFB(buf,6)=(p->party.item&1)?1:0;
  6631. WBUFB(buf,7)=(p->party.item&2)?1:0;
  6632. #endif
  6633. if(flag==0)
  6634. clif_send(buf,packet_len(cmd),&sd->bl,PARTY);
  6635. else
  6636. clif_send(buf,packet_len(cmd),&sd->bl,SELF);
  6637. }
  6638. /**
  6639. * 0105 <account id>.L <char name>.24B <result>.B (ZC_DELETE_MEMBER_FROM_GROUP).
  6640. * @param sd Send the notification for this player
  6641. * @param account_id Account ID of kicked member
  6642. * @param name Name of kicked member
  6643. * @param result Party leave result @see PARTY_MEMBER_WITHDRAW
  6644. * @param target Send target
  6645. **/
  6646. void clif_party_withdraw( map_session_data& sd, uint32 account_id, const char* name, enum e_party_member_withdraw result, enum send_target target ){
  6647. PACKET_ZC_DELETE_MEMBER_FROM_GROUP p = {};
  6648. p.PacketType = HEADER_ZC_DELETE_MEMBER_FROM_GROUP;
  6649. p.AID = account_id;
  6650. safestrncpy( p.characterName, name, sizeof( p.characterName ) );
  6651. p.result = result;
  6652. clif_send( &p, sizeof( p ), &sd.bl, target );
  6653. }
  6654. /// Party chat message (ZC_NOTIFY_CHAT_PARTY).
  6655. /// 0109 <packet len>.W <account id>.L <message>.?B
  6656. void clif_party_message( struct party_data& party, uint32 account_id, const char* mes, size_t len ){
  6657. map_session_data* sd = party_getavailablesd( &party );
  6658. if( sd == nullptr ){
  6659. return;
  6660. }
  6661. if (len > CHAT_SIZE_MAX) {
  6662. ShowWarning( "clif_party_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", party_id=%d).\n", mes, len, CHAT_SIZE_MAX, party.party.party_id );
  6663. len = CHAT_SIZE_MAX;
  6664. }
  6665. PACKET_ZC_NOTIFY_CHAT_PARTY* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT_PARTY*>( packet_buffer );
  6666. p->PacketType = HEADER_ZC_NOTIFY_CHAT_PARTY;
  6667. p->PacketLength = sizeof( *p );
  6668. p->AID = account_id;
  6669. safestrncpy( p->chatMsg, mes, len );
  6670. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  6671. clif_send( p, p->PacketLength, &sd->bl, PARTY );
  6672. }
  6673. /// Updates the position of a party member on the minimap (ZC_NOTIFY_POSITION_TO_GROUPM).
  6674. /// 0107 <account id>.L <x>.W <y>.W
  6675. void clif_party_xy( map_session_data& sd ){
  6676. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6677. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6678. p.AID = sd.status.account_id;
  6679. p.xPos = sd.bl.x;
  6680. p.yPos = sd.bl.y;
  6681. clif_send( &p, sizeof( p ), &sd.bl, PARTY_SAMEMAP_WOS );
  6682. }
  6683. /*==========================================
  6684. * Sends x/y dot to a single fd. [Skotlex]
  6685. *------------------------------------------*/
  6686. void clif_party_xy_single( map_session_data& sd, map_session_data& tsd ){
  6687. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6688. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6689. p.AID = sd.status.account_id;
  6690. p.xPos = sd.bl.x;
  6691. p.yPos = sd.bl.y;
  6692. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6693. }
  6694. /// Updates HP bar of a party member.
  6695. /// 0106 <account id>.L <hp>.W <max hp>.W (ZC_NOTIFY_HP_TO_GROUPM)
  6696. /// 080e <account id>.L <hp>.L <max hp>.L (ZC_NOTIFY_HP_TO_GROUPM_R2)
  6697. void clif_party_hp( map_session_data& sd ){
  6698. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6699. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6700. p.AID = sd.status.account_id;
  6701. #if PACKETVER < 20100119
  6702. // To correctly display the %hp bar. [Skotlex]
  6703. if( sd.battle_status.max_hp > INT16_MAX ){
  6704. p.hp = sd.battle_status.hp / ( sd.battle_status.max_hp / 100 );
  6705. p.maxhp = 100;
  6706. }else{
  6707. p.hp = sd.battle_status.hp;
  6708. p.maxhp = sd.battle_status.max_hp;
  6709. }
  6710. #else
  6711. p.hp = sd.battle_status.hp;
  6712. p.maxhp = sd.battle_status.max_hp;
  6713. #endif
  6714. clif_send( &p, sizeof( p ), &sd.bl, PARTY_AREA_WOS );
  6715. }
  6716. /// Notifies the party members of a character's death or revival.
  6717. /// 0AB2 <GID>.L <dead>.B
  6718. void clif_party_dead( map_session_data& sd ){
  6719. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6720. PACKET_ZC_GROUP_ISALIVE p = {};
  6721. p.packetType = HEADER_ZC_GROUP_ISALIVE;
  6722. p.AID = sd.status.account_id;
  6723. p.isDead = pc_isdead( &sd );
  6724. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6725. #endif
  6726. }
  6727. /// Updates the job and level of a party member
  6728. /// 0abd <account id>.L <job>.W <level>.W
  6729. void clif_party_job_and_level( map_session_data& sd ){
  6730. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  6731. PACKET_ZC_NOTIFY_MEMBERINFO_TO_GROUPM p = {};
  6732. p.PacketType = HEADER_ZC_NOTIFY_MEMBERINFO_TO_GROUPM;
  6733. p.AID = sd.status.account_id;
  6734. p.job = sd.status.class_;
  6735. p.level = sd.status.base_level;
  6736. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6737. #endif
  6738. }
  6739. /*==========================================
  6740. * Sends HP bar to a single fd. [Skotlex]
  6741. *------------------------------------------*/
  6742. void clif_hpmeter_single( map_session_data& sd, uint32 id, uint32 hp, uint32 maxhp ){
  6743. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6744. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6745. p.AID = id;
  6746. #if PACKETVER < 20100119
  6747. // To correctly display the %hp bar. [Skotlex]
  6748. if( maxhp > INT16_MAX ){
  6749. p.hp = hp / ( maxhp / 100 );
  6750. p.maxhp = 100;
  6751. }else{
  6752. p.hp = hp;
  6753. p.maxhp = maxhp;
  6754. }
  6755. #else
  6756. p.hp = hp;
  6757. p.maxhp = maxhp;
  6758. #endif
  6759. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6760. }
  6761. /// Notifies the client, that it's attack target is too far.
  6762. /// 0139 <target id>.L <target x>.W <target y>.W <x>.W <y>.W <atk range>.W (ZC_ATTACK_FAILURE_FOR_DISTANCE)
  6763. void clif_movetoattack( map_session_data& sd, block_list& bl ){
  6764. PACKET_ZC_ATTACK_FAILURE_FOR_DISTANCE packet{};
  6765. packet.PacketType = HEADER_ZC_ATTACK_FAILURE_FOR_DISTANCE;
  6766. packet.targetAID = bl.id;
  6767. packet.targetXPos = bl.x;
  6768. packet.targetYPos = bl.y;
  6769. packet.xPos = sd.bl.x;
  6770. packet.yPos = sd.bl.y;
  6771. packet.currentAttRange = sd.battle_status.rhw.range;
  6772. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6773. }
  6774. /// Notifies the client about the result of an item produce request.
  6775. /// 018f <result>.W <name id>.W (ZC_ACK_REQMAKINGITEM)
  6776. /// result:
  6777. /// 0 = success
  6778. /// 1 = failure
  6779. /// 2 = success (alchemist)
  6780. /// 3 = failure (alchemist)
  6781. void clif_produceeffect(map_session_data* sd,int32 flag, t_itemid nameid){
  6782. if( sd == nullptr ){
  6783. return;
  6784. }
  6785. clif_solved_charname( *sd, sd->status.char_id, sd->status.name );
  6786. PACKET_ZC_ACK_REQMAKINGITEM p = {};
  6787. p.packetType = HEADER_ZC_ACK_REQMAKINGITEM;
  6788. p.result = flag;
  6789. p.itemId = client_nameid( nameid );
  6790. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6791. }
  6792. /// Initiates the pet taming process.
  6793. /// 019e (ZC_START_CAPTURE)
  6794. void clif_catch_process( map_session_data& sd ){
  6795. PACKET_ZC_START_CAPTURE packet{};
  6796. packet.PacketType = HEADER_ZC_START_CAPTURE;
  6797. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6798. }
  6799. /// Displays the result of a pet taming attempt.
  6800. /// 01a0 <result>.B (ZC_TRYCAPTURE_MONSTER)
  6801. /// 0 = failure
  6802. /// 1 = success
  6803. void clif_pet_roulette( map_session_data& sd, bool success ){
  6804. PACKET_ZC_TRYCAPTURE_MONSTER packet{};
  6805. packet.PacketType = HEADER_ZC_TRYCAPTURE_MONSTER;
  6806. packet.result = success;
  6807. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6808. }
  6809. /// Presents a list of pet eggs that can be hatched (ZC_PETEGG_LIST).
  6810. /// 01a6 <packet len>.W { <index>.W }*
  6811. void clif_sendegg(map_session_data *sd)
  6812. {
  6813. int32 i,n=0,fd;
  6814. nullpo_retv(sd);
  6815. fd=sd->fd;
  6816. if (battle_config.pet_no_gvg && map_flag_gvg2(sd->bl.m)) { //Disable pet hatching in GvG grounds during Guild Wars [Skotlex]
  6817. clif_displaymessage(fd, msg_txt(sd,666));
  6818. return;
  6819. }
  6820. WFIFOHEAD(fd, MAX_INVENTORY * 2 + 4);
  6821. WFIFOW(fd,0)=0x1a6;
  6822. for(i=0,n=0;i<MAX_INVENTORY;i++){
  6823. if(sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr ||
  6824. sd->inventory_data[i]->type != IT_PETEGG ||
  6825. sd->inventory.u.items_inventory[i].amount <= 0)
  6826. continue;
  6827. WFIFOW(fd,n*2+4)=client_index(i);
  6828. n++;
  6829. }
  6830. WFIFOW(fd,2)=4+n*2;
  6831. WFIFOSET(fd,WFIFOW(fd,2));
  6832. sd->menuskill_id = SA_TAMINGMONSTER;
  6833. sd->menuskill_val = -1;
  6834. }
  6835. /// Sends a specific pet data update.
  6836. /// 01a4 <type>.B <id>.L <data>.L (ZC_CHANGESTATE_PET)
  6837. /// type:
  6838. /// 0 = pre-init (data = 0)
  6839. /// 1 = intimacy (data = 0~4)
  6840. /// 2 = hunger (data = 0~4)
  6841. /// 3 = accessory
  6842. /// 4 = performance (data = 1~3: normal, 4: special)
  6843. /// 5 = hairstyle
  6844. /// 6 = close egg selection ui and update egg in inventory (PACKETVER >= 20180704)
  6845. ///
  6846. /// If sd is null, the update is sent to nearby objects, otherwise it is sent only to that player.
  6847. void clif_send_petdata( map_session_data* sd, pet_data& pd, e_changestate_pet data_type ){
  6848. int32 value;
  6849. switch( data_type ) {
  6850. case CHANGESTATEPET_INIT:
  6851. value = 0;
  6852. break;
  6853. case CHANGESTATEPET_INTIMACY:
  6854. value = pd.pet.intimate;
  6855. break;
  6856. case CHANGESTATEPET_HUNGER:
  6857. value = pd.pet.hungry;
  6858. break;
  6859. case CHANGESTATEPET_ACCESSORY:
  6860. value = pd.vd.head_bottom;
  6861. break;
  6862. case CHANGESTATEPET_PERFORMANCE: {
  6863. int32 num;
  6864. // data = 1~3: normal, 4: special
  6865. if (pd.pet.intimate > PET_INTIMATE_LOYAL)
  6866. num = pd.get_pet_db()->s_perfor ? 4 : 3;
  6867. else if (pd.pet.intimate > PET_INTIMATE_CORDIAL) //TODO: this is way too high
  6868. num = 2;
  6869. else
  6870. num = 1;
  6871. value = rnd_value(1, num);
  6872. break;
  6873. }
  6874. case CHANGESTATEPET_HAIRSTYLE:
  6875. value = battle_config.pet_hair_style;
  6876. break;
  6877. case CHANGESTATEPET_UPDATE_EGG:
  6878. value = ( pd.pet.intimate == 1 ) ? 0 : 1;
  6879. break;
  6880. default: // shouldn't happen
  6881. return;
  6882. }
  6883. PACKET_ZC_CHANGESTATE_PET packet{};
  6884. packet.PacketType = HEADER_ZC_CHANGESTATE_PET;
  6885. packet.type = static_cast<decltype(packet.type)>( data_type );
  6886. packet.GID = pd.bl.id;
  6887. packet.data = value;
  6888. if (sd != nullptr)
  6889. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  6890. else
  6891. clif_send( &packet, sizeof( packet ), &pd.bl, AREA );
  6892. }
  6893. /// Pet's base data.
  6894. /// 01a2 <name>.24B <renamed>.B <level>.W <hunger>.W <intimacy>.W <accessory id>.W <class>.W (ZC_PROPERTY_PET)
  6895. void clif_send_petstatus( map_session_data& sd, pet_data& pd ){
  6896. PACKET_ZC_PROPERTY_PET packet{};
  6897. packet.PacketType = HEADER_ZC_PROPERTY_PET;
  6898. safestrncpy( packet.szName, pd.pet.name, NAME_LENGTH );
  6899. packet.bModified = battle_config.pet_rename ? 0 : pd.pet.rename_flag;
  6900. packet.nLevel = pd.pet.level;
  6901. packet.nFullness = pd.pet.hungry;
  6902. packet.nRelationship = pd.pet.intimate;
  6903. packet.ITID = static_cast<decltype(packet.ITID)>( pd.pet.equip );
  6904. #if PACKETVER >= 20081126
  6905. packet.job = pd.pet.class_;
  6906. #endif
  6907. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6908. }
  6909. /// Notification about a pet's emotion/talk.
  6910. /// 01aa <id>.L <data>.L (ZC_PET_ACT)
  6911. /// data:
  6912. /// @see CZ_PET_ACT.
  6913. void clif_pet_emotion( pet_data& pd, int32 param ){
  6914. PACKET_ZC_PET_ACT packet{};
  6915. packet.packetType = HEADER_ZC_PET_ACT;
  6916. packet.GID = pd.bl.id;
  6917. packet.data = param;
  6918. clif_send( &packet, sizeof( packet ), &pd.bl, AREA );
  6919. }
  6920. /// Result of request to feed a pet.
  6921. /// 01a3 <result>.B <name id>.W (ZC_FEED_PET)
  6922. /// result:
  6923. /// 0 = failure
  6924. /// 1 = success
  6925. void clif_pet_food( map_session_data& sd, int32 foodid, bool success ){
  6926. PACKET_ZC_FEED_PET packet{};
  6927. packet.packetType = HEADER_ZC_FEED_PET;
  6928. packet.result = success;
  6929. packet.itemId = client_nameid( foodid );
  6930. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6931. }
  6932. /// Send pet auto feed info.
  6933. void clif_pet_autofeed_status(map_session_data* sd, bool force) {
  6934. #if PACKETVER >= 20141008
  6935. nullpo_retv(sd);
  6936. if (force || battle_config.pet_autofeed_always) {
  6937. // Always send ON or OFF
  6938. if (sd->pd && battle_config.feature_pet_autofeed) {
  6939. clif_configuration(sd, CONFIG_PET_AUTOFEED, sd->pd->pet.autofeed);
  6940. }
  6941. else {
  6942. clif_configuration(sd, CONFIG_PET_AUTOFEED, false);
  6943. }
  6944. }
  6945. else {
  6946. // Only send when enabled
  6947. if (sd->pd && battle_config.feature_pet_autofeed && sd->pd->pet.autofeed) {
  6948. clif_configuration(sd, CONFIG_PET_AUTOFEED, true);
  6949. }
  6950. }
  6951. #endif
  6952. }
  6953. /// Presents a list of skills that can be auto-spelled.
  6954. /// 01cd { <skill id>.L }*7 (ZC_AUTOSPELLLIST)
  6955. void clif_autospell( map_session_data& sd, uint16 skill_lv ){
  6956. struct s_autospell_requirement{
  6957. uint16 skill_id;
  6958. uint16 required_autospell_skill_lv;
  6959. };
  6960. #ifdef RENEWAL
  6961. const std::vector<s_autospell_requirement> autospell_skills = {
  6962. { MG_FIREBOLT, 0 },
  6963. { MG_COLDBOLT, 0 },
  6964. { MG_LIGHTNINGBOLT, 0 },
  6965. { MG_SOULSTRIKE, 3 },
  6966. { MG_FIREBALL, 3 },
  6967. { WZ_EARTHSPIKE, 6 },
  6968. { MG_FROSTDIVER, 6 },
  6969. { MG_THUNDERSTORM, 9 },
  6970. { WZ_HEAVENDRIVE, 9 }
  6971. };
  6972. #else
  6973. const std::vector<s_autospell_requirement> autospell_skills = {
  6974. { MG_NAPALMBEAT, 0 },
  6975. { MG_COLDBOLT, 1 },
  6976. { MG_FIREBOLT, 1 },
  6977. { MG_LIGHTNINGBOLT, 1 },
  6978. { MG_SOULSTRIKE, 4 },
  6979. { MG_FIREBALL, 7 },
  6980. { MG_FROSTDIVER, 9 },
  6981. };
  6982. #endif
  6983. #if PACKETVER_MAIN_NUM >= 20181128 || PACKETVER_RE_NUM >= 20181031
  6984. PACKET_ZC_AUTOSPELLLIST* p = reinterpret_cast<PACKET_ZC_AUTOSPELLLIST*>( packet_buffer );
  6985. p->packetType = HEADER_ZC_AUTOSPELLLIST;
  6986. p->packetLength = sizeof( *p );
  6987. size_t count = 0;
  6988. for( const s_autospell_requirement& requirement : autospell_skills ){
  6989. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  6990. p->skills[count++] = requirement.skill_id;
  6991. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skills[0] ) );
  6992. }
  6993. }
  6994. clif_send( p, p->packetLength, &sd.bl, SELF );
  6995. #elif PACKETVER_MAIN_NUM >= 20090406 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_SAK_NUM >= 20080618
  6996. PACKET_ZC_AUTOSPELLLIST p = {};
  6997. p.packetType = HEADER_ZC_AUTOSPELLLIST;
  6998. size_t count = 0;
  6999. for( const s_autospell_requirement& requirement : autospell_skills ){
  7000. if( count == ARRAYLENGTH( p.skills ) ){
  7001. break;
  7002. }
  7003. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  7004. p.skills[count++] = requirement.skill_id;
  7005. }else{
  7006. p.skills[count++] = 0;
  7007. }
  7008. }
  7009. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7010. #endif
  7011. sd.menuskill_id = SA_AUTOSPELL;
  7012. sd.menuskill_val = skill_lv;
  7013. }
  7014. /// Devotion's visual effect (ZC_DEVOTIONLIST).
  7015. /// 01cf <devoter id>.L { <devotee id>.L }*5 <max distance>.W
  7016. void clif_devotion(struct block_list *src, map_session_data *tsd)
  7017. {
  7018. unsigned char buf[56];
  7019. nullpo_retv(src);
  7020. memset(buf,0,packet_len(0x1cf));
  7021. WBUFW(buf,0) = 0x1cf;
  7022. WBUFL(buf,2) = src->id;
  7023. if( src->type == BL_MER )
  7024. {
  7025. s_mercenary_data *md = BL_CAST(BL_MER,src);
  7026. if( md && md->master && md->devotion_flag )
  7027. WBUFL(buf,6) = md->master->bl.id;
  7028. WBUFW(buf,26) = skill_get_range2(src, ML_DEVOTION, mercenary_checkskill(md, ML_DEVOTION), false);
  7029. }
  7030. else
  7031. {
  7032. int32 i;
  7033. map_session_data *sd = BL_CAST(BL_PC,src);
  7034. if( sd == nullptr )
  7035. return;
  7036. for( i = 0; i < 5 /*MAX_DEVOTION*/; i++ ) // Client only able show to 5 links
  7037. WBUFL(buf,6+4*i) = sd->devotion[i];
  7038. WBUFW(buf,26) = skill_get_range2(src, CR_DEVOTION, pc_checkskill(sd, CR_DEVOTION), false);
  7039. }
  7040. if( tsd )
  7041. clif_send(buf, packet_len(0x1cf), &tsd->bl, SELF);
  7042. else
  7043. clif_send(buf, packet_len(0x1cf), src, AREA);
  7044. }
  7045. /// Notifies the client of an object's spirits.
  7046. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  7047. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  7048. void clif_spiritball( struct block_list *bl, struct block_list* target, enum send_target send_target ){
  7049. nullpo_retv( bl );
  7050. PACKET_ZC_SPIRITS p = {};
  7051. p.PacketType = HEADER_ZC_SPIRITS;
  7052. p.AID = bl->id;
  7053. switch( bl->type ){
  7054. case BL_PC:
  7055. p.num = ( (map_session_data*)bl )->spiritball;
  7056. break;
  7057. case BL_HOM:
  7058. p.num = ( (struct homun_data*)bl )->homunculus.spiritball;
  7059. break;
  7060. }
  7061. clif_send( &p, sizeof( p ), target == nullptr ? bl : target, send_target );
  7062. }
  7063. /// Notifies clients in area of a character's combo delay.
  7064. /// 01d2 <account id>.L <delay>.L (ZC_COMBODELAY)
  7065. void clif_combo_delay( block_list& bl, t_tick wait ){
  7066. PACKET_ZC_COMBODELAY packet{};
  7067. packet.packetType = HEADER_ZC_COMBODELAY;
  7068. packet.AID = bl.id;
  7069. packet.delay = client_tick( wait );
  7070. clif_send( &packet, sizeof( packet ), &bl, AREA );
  7071. }
  7072. /// Notifies clients in area that a character has blade-stopped another.
  7073. /// 01d1 <src id>.L <dst id>.L <flag>.L (ZC_BLADESTOP)
  7074. /// flag:
  7075. /// 0 = inactive
  7076. /// 1 = active
  7077. void clif_bladestop( block_list& src, uint32 target_id, bool active ){
  7078. PACKET_ZC_BLADESTOP packet{};
  7079. packet.packetType = HEADER_ZC_BLADESTOP;
  7080. packet.srcId = src.id;
  7081. packet.targetId = target_id;
  7082. packet.flag = active;
  7083. clif_send( &packet, sizeof( packet ), &src, AREA );
  7084. }
  7085. /// MVP effect.
  7086. /// 010c <account id>.L (ZC_MVP)
  7087. void clif_mvp_effect( map_session_data& sd ){
  7088. PACKET_ZC_MVP packet{};
  7089. packet.packetType = HEADER_ZC_MVP;
  7090. packet.AID = sd.bl.id;
  7091. clif_send( &packet, sizeof( packet ), &sd.bl, AREA );
  7092. }
  7093. /// MVP item reward message (ZC_MVP_GETTING_ITEM).
  7094. /// 010a <name id>.W
  7095. void clif_mvp_item( map_session_data *sd, t_itemid nameid ){
  7096. PACKET_ZC_MVP_GETTING_ITEM p = {};
  7097. p.packetType = HEADER_ZC_MVP_GETTING_ITEM;
  7098. p.itemId = client_nameid( nameid );
  7099. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  7100. }
  7101. /// MVP EXP reward message.
  7102. /// 010b <exp>.L (ZC_MVP_GETTING_SPECIAL_EXP)
  7103. void clif_mvp_exp( map_session_data& sd, t_exp exp ){
  7104. #if PACKETVER >= 20131223 // Kro remove this packet [Napster]
  7105. if (battle_config.mvp_exp_reward_message) {
  7106. char e_msg[CHAT_SIZE_MAX];
  7107. sprintf(e_msg, msg_txt(&sd, 717), exp); // Congratulations! You are the MVP! Your reward EXP Points are %u !!
  7108. clif_messagecolor( &sd.bl, color_table[COLOR_CYAN], e_msg, false, SELF );
  7109. }
  7110. #else
  7111. PACKET_ZC_MVP_GETTING_SPECIAL_EXP packet{};
  7112. packet.packetType = HEADER_ZC_MVP_GETTING_SPECIAL_EXP;
  7113. packet.exp = std::min( static_cast<decltype(packet.exp)>( exp ), MAX_EXP );
  7114. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  7115. #endif
  7116. }
  7117. /// Dropped MVP item reward message.
  7118. /// 010d (ZC_THROW_MVPITEM)
  7119. ///
  7120. /// "You are the MVP, but cannot obtain the reward because you are overweight."
  7121. void clif_mvp_noitem( map_session_data& sd ){
  7122. PACKET_ZC_THROW_MVPITEM packet{};
  7123. packet.packetType = HEADER_ZC_THROW_MVPITEM;
  7124. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  7125. }
  7126. /// Guild creation result.
  7127. /// 0167 <result>.B (ZC_RESULT_MAKE_GUILD)
  7128. /// result:
  7129. /// 0 = "Guild has been created."
  7130. /// 1 = "You are already in a Guild."
  7131. /// 2 = "That Guild Name already exists."
  7132. /// 3 = "You need the neccessary item to create a Guild."
  7133. void clif_guild_created( map_session_data& sd, int32 flag ){
  7134. PACKET_ZC_RESULT_MAKE_GUILD p = {};
  7135. p.packetType = HEADER_ZC_RESULT_MAKE_GUILD;
  7136. p.result = static_cast<decltype(p.result)>( flag );
  7137. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7138. }
  7139. /// Notifies the client that it is belonging to a guild (ZC_UPDATE_GDID).
  7140. /// 016c <guild id>.L <emblem id>.L <mode>.L <ismaster>.B <inter sid>.L <guild name>.24B
  7141. /// mode:
  7142. /// &0x01 = allow invite
  7143. /// &0x10 = allow expel
  7144. void clif_guild_belonginfo( map_session_data& sd ){
  7145. if( sd.guild == nullptr ){
  7146. return;
  7147. }
  7148. const auto &guild = sd.guild->guild;
  7149. int32 position = guild_getposition(sd);
  7150. if( position < 0 ){
  7151. return;
  7152. }
  7153. PACKET_ZC_UPDATE_GDID p = {};
  7154. p.PacketType = HEADER_ZC_UPDATE_GDID;
  7155. p.guildId = guild.guild_id;
  7156. p.emblemVersion = guild.emblem_id;
  7157. p.mode = guild.position[position].mode;
  7158. p.isMaster = ( sd.state.gmaster_flag == 1 );
  7159. p.interSid = 0; // InterSID (unknown purpose)
  7160. safestrncpy( p.guildName, guild.name, sizeof( p.guildName ) );
  7161. #if PACKETVER_MAIN_NUM >= 20220216
  7162. p.masterGID = guild.member[0].char_id;
  7163. #endif
  7164. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7165. }
  7166. /// Guild member login notice.
  7167. /// 016d <account id>.L <char id>.L <status>.L (ZC_UPDATE_CHARSTAT)
  7168. /// 01f2 <account id>.L <char id>.L <status>.L <gender>.W <hair style>.W <hair color>.W (ZC_UPDATE_CHARSTAT2)
  7169. /// status:
  7170. /// 0 = offline
  7171. /// 1 = online
  7172. void clif_guild_memberlogin_notice(const struct mmo_guild &g,int32 idx,int32 flag)
  7173. {
  7174. unsigned char buf[64];
  7175. map_session_data* sd;
  7176. WBUFW(buf, 0)=0x1f2;
  7177. WBUFL(buf, 2)=g.member[idx].account_id;
  7178. WBUFL(buf, 6)=g.member[idx].char_id;
  7179. WBUFL(buf,10)=flag;
  7180. if( ( sd = g.member[idx].sd ) != nullptr )
  7181. {
  7182. WBUFW(buf,14) = sd->status.sex;
  7183. WBUFW(buf,16) = sd->status.hair;
  7184. WBUFW(buf,18) = sd->status.hair_color;
  7185. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD_WOS);
  7186. }
  7187. else if( ( sd = guild_getavailablesd(g) ) != nullptr )
  7188. {
  7189. WBUFW(buf,14) = 0;
  7190. WBUFW(buf,16) = 0;
  7191. WBUFW(buf,18) = 0;
  7192. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD);
  7193. }
  7194. }
  7195. // Function `clif_guild_memberlogin_notice` sends info about
  7196. // logins and logouts of a guild member to the rest members.
  7197. // But at the 1st time (after a player login or map changing)
  7198. // the client won't show the message.
  7199. // So I suggest use this function for sending "first-time-info"
  7200. // to some player on entering the game or changing location.
  7201. // At next time the client would always show the message.
  7202. // The function sends all the statuses in the single packet
  7203. // to economize traffic. [LuzZza]
  7204. void clif_guild_send_onlineinfo(map_session_data *sd)
  7205. {
  7206. unsigned char buf[14*128];
  7207. int32 i, count=0, p_len;
  7208. nullpo_retv(sd);
  7209. p_len = packet_len(0x16d);
  7210. auto &g = sd->guild;
  7211. if (!g)
  7212. return;
  7213. for(i=0; i<g->guild.max_member; i++) {
  7214. if(g->guild.member[i].account_id > 0 &&
  7215. g->guild.member[i].account_id != sd->status.account_id) {
  7216. WBUFW(buf,count*p_len) = 0x16d;
  7217. WBUFL(buf,count*p_len+2) = g->guild.member[i].account_id;
  7218. WBUFL(buf,count*p_len+6) = g->guild.member[i].char_id;
  7219. WBUFL(buf,count*p_len+10) = g->guild.member[i].online;
  7220. count++;
  7221. }
  7222. }
  7223. clif_send(buf, p_len*count, &sd->bl, SELF);
  7224. }
  7225. /// Bitmask of enabled guild window tabs
  7226. /// 014e <menu flag>.L (ZC_ACK_GUILD_MENUINTERFACE)
  7227. /// menu flag:
  7228. /// 0x00 = Basic Info (always on)
  7229. /// &0x01 = Member manager
  7230. /// &0x02 = Positions
  7231. /// &0x04 = Skills
  7232. /// &0x10 = Expulsion list
  7233. /// &0x40 = Unknown (GMENUFLAG_ALLGUILDLIST)
  7234. /// &0x80 = Notice
  7235. void clif_guild_masterormember(map_session_data& sd){
  7236. PACKET_ZC_ACK_GUILD_MENUINTERFACE p{};
  7237. p.packetType = HEADER_ZC_ACK_GUILD_MENUINTERFACE;
  7238. if(sd.state.gmaster_flag){
  7239. p.menuFlag = 0xd7;
  7240. }else{
  7241. p.menuFlag = 0x57;
  7242. }
  7243. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7244. }
  7245. /// Guild basic information (Territories [Valaris])
  7246. /// 0150 <guild id>.L <level>.L <member num>.L <member max>.L <exp>.L <max exp>.L <points>.L <honor>.L <virtue>.L <emblem id>.L <name>.24B <master name>.24B <manage land>.16B (ZC_GUILD_INFO)
  7247. /// 01b6 <guild id>.L <level>.L <member num>.L <member max>.L <exp>.L <max exp>.L <points>.L <honor>.L <virtue>.L <emblem id>.L <name>.24B <master name>.24B <manage land>.16B <zeny>.L (ZC_GUILD_INFO2)
  7248. /// 0a84 <guild id>.L <level>.L <member num>.L <member max>.L <exp>.L <max exp>.L <points>.L <honor>.L <virtue>.L <emblem id>.L <name>.24B <manage land>.16B <zeny>.L <master char id>.L (ZC_GUILD_INFO3)
  7249. void clif_guild_basicinfo( map_session_data& sd ){
  7250. if( sd.guild == nullptr ){
  7251. return;
  7252. }
  7253. const auto &guild = sd.guild->guild;
  7254. PACKET_ZC_GUILD_INFO p = {};
  7255. p.PacketType = HEADER_ZC_GUILD_INFO;
  7256. p.GDID = guild.guild_id;
  7257. p.level = guild.guild_lv;
  7258. p.userNum = guild.connect_member;
  7259. p.maxUserNum = guild.max_member;
  7260. p.userAverageLevel = guild.average_lv;
  7261. p.exp = (uint32)cap_value( guild.exp, 0, MAX_GUILD_EXP );
  7262. p.maxExp = (uint32)cap_value( guild.next_exp, 0, MAX_GUILD_EXP );
  7263. p.point = 0; // Tax Points
  7264. p.honor = 0; // Honor: (left) Vulgar [-100,100] Famed (right)
  7265. p.virtue = 0; // Virtue: (down) Wicked [-100,100] Righteous (up)
  7266. p.emblemVersion = guild.emblem_id;
  7267. safestrncpy( p.guildname, guild.name, sizeof( p.guildname ) );
  7268. safestrncpy( p.manageLand, msg_txt( &sd, 300 + guild_checkcastles( guild ) ), sizeof( p.manageLand ) );
  7269. p.zeny = 0;
  7270. #if PACKETVER >= 20200902
  7271. p.masterGID = guild.member[0].char_id; // leader
  7272. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7273. #elif PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7274. p.masterGID = guild.member[0].char_id; // leader
  7275. #else
  7276. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7277. #endif
  7278. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7279. }
  7280. /// Guild alliance and opposition list
  7281. /// 014c <packet len>.W { <relation>.L <guild id>.L <guild name>.24B }* (ZC_MYGUILD_BASIC_INFO)
  7282. void clif_guild_allianceinfo(map_session_data& sd){
  7283. auto &g = sd.guild;
  7284. if (g == nullptr){
  7285. return;
  7286. }
  7287. PACKET_ZC_MYGUILD_BASIC_INFO* p = reinterpret_cast<PACKET_ZC_MYGUILD_BASIC_INFO*>( packet_buffer );
  7288. p->PacketType = HEADER_ZC_MYGUILD_BASIC_INFO;
  7289. p->PacketLength = sizeof(*p);
  7290. for(size_t i=0, c = 0;i<MAX_GUILDALLIANCE;i++){
  7291. guild_alliance &a = g->guild.alliance[i];
  7292. if(a.guild_id<=0){
  7293. continue;
  7294. }
  7295. RELATED_GUILD_INFO& info = p->rgInfo[c];
  7296. info.relation = a.opposition;
  7297. info.GDID = a.guild_id;
  7298. safestrncpy(info.guildname,a.name,sizeof(info.guildname));
  7299. p->PacketLength += static_cast<decltype(p->PacketLength)>(sizeof(info));
  7300. c++;
  7301. }
  7302. clif_send(p,p->PacketLength,&sd.bl,SELF);
  7303. }
  7304. /// Guild member manager information
  7305. /// 0154 <packet len>.W { <account>.L <char id>.L <hair style>.W <hair color>.W <gender>.W <class>.W <level>.W <contrib exp>.L <state>.L <position>.L <memo>.50B <name>.24B }* (ZC_MEMBERMGR_INFO)
  7306. /// state:
  7307. /// 0 = offline
  7308. /// 1 = online
  7309. /// memo:
  7310. /// probably member's self-introduction (unused, no client UI/packets for editing it)
  7311. /// 0aa5 <packet len>.W { <account>.L <char id>.L <hair style>.W <hair color>.W <gender>.W <class>.W <level>.W <contrib exp>.L <state>.L <position>.L <lastlogin>.L }* (ZC_MEMBERMGR_INFO2)
  7312. void clif_guild_memberlist( map_session_data& sd ){
  7313. if( sd.guild == nullptr ){
  7314. return;
  7315. }
  7316. const auto &guild = sd.guild->guild;
  7317. PACKET_ZC_MEMBERMGR_INFO* p = reinterpret_cast<PACKET_ZC_MEMBERMGR_INFO*>( packet_buffer );
  7318. p->PacketType = HEADER_ZC_MEMBERMGR_INFO;
  7319. p->packetLength = sizeof( *p );
  7320. for( int32 i = 0, c = 0; i < guild.max_member; i++ ){
  7321. const auto &member = guild.member[i];
  7322. if( member.account_id == 0 ){
  7323. continue;
  7324. }
  7325. GUILD_MEMBER_INFO& member_info = p->guildMemberInfo[c];
  7326. member_info.AID = member.account_id;
  7327. member_info.GID = member.char_id;
  7328. member_info.head = member.hair;
  7329. member_info.headPalette = member.hair_color;
  7330. member_info.sex = member.gender;
  7331. member_info.job = member.class_;
  7332. member_info.level = member.lv;
  7333. member_info.contributionExp = (uint32)cap_value( member.exp, 0, MAX_GUILD_EXP );
  7334. member_info.currentState = member.online;
  7335. member_info.positionID = member.position;
  7336. #if PACKETVER >= 20200902
  7337. member_info.lastLoginTime = member.last_login;
  7338. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7339. #elif PACKETVER_MAIN_NUM >= 20161214 || PACKETVER_RE_NUM >= 20161130 || defined(PACKETVER_ZERO)
  7340. member_info.lastLoginTime = member.last_login;
  7341. #else
  7342. memset( member_info.intro, 0, sizeof( member_info.intro ) ); //[Ind] - This is displayed in the 'note' column but being you can't edit it it's sent empty.
  7343. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7344. #endif
  7345. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( member_info ) );
  7346. c++;
  7347. }
  7348. clif_send( p, p->packetLength, &sd.bl, SELF );
  7349. }
  7350. /// Guild position name information (ZC_POSITION_ID_NAME_INFO).
  7351. /// 0166 <packet len>.W { <position id>.L <position name>.24B }*
  7352. void clif_guild_positionnamelist(map_session_data *sd)
  7353. {
  7354. int32 i,fd;
  7355. nullpo_retv(sd);
  7356. auto &g = sd->guild;
  7357. if (!g)
  7358. return;
  7359. fd = sd->fd;
  7360. WFIFOHEAD(fd, MAX_GUILDPOSITION * 28 + 4);
  7361. WFIFOW(fd, 0)=0x166;
  7362. for(i=0;i<MAX_GUILDPOSITION;i++){
  7363. WFIFOL(fd,i*28+4)=i;
  7364. safestrncpy(WFIFOCP(fd,i*28+8),g->guild.position[i].name,NAME_LENGTH);
  7365. }
  7366. WFIFOW(fd,2)=i*28+4;
  7367. WFIFOSET(fd,WFIFOW(fd,2));
  7368. }
  7369. /// Guild position information
  7370. /// 0160 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L }* (ZC_POSITION_INFO)
  7371. /// mode:
  7372. /// See enum e_guild_permission
  7373. /// ranking:
  7374. /// TODO
  7375. static void clif_guild_positioninfolist(map_session_data& sd){
  7376. auto &g = sd.guild;
  7377. if (g == nullptr){
  7378. return;
  7379. }
  7380. PACKET_ZC_POSITION_INFO* p = reinterpret_cast<PACKET_ZC_POSITION_INFO*>( packet_buffer );
  7381. p->PacketType = HEADER_ZC_POSITION_INFO;
  7382. p->PacketLength = sizeof(*p);
  7383. for(size_t i=0;i<MAX_GUILDPOSITION;i++){
  7384. guild_position& gp = g->guild.position[i];
  7385. PACKET_ZC_POSITION_INFO_sub& position = p->posInfo[i];
  7386. position.positionID = static_cast<decltype(position.positionID)>( i );
  7387. position.right = gp.mode;
  7388. position.ranking = static_cast<decltype(position.ranking)>( i );
  7389. position.payRate = gp.exp_mode;
  7390. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( position ) );
  7391. }
  7392. clif_send(p,p->PacketLength,&sd.bl,SELF);
  7393. }
  7394. /// Notifies clients in a guild about updated position information.
  7395. /// 0174 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <position name>.24B }* (ZC_ACK_CHANGE_GUILD_POSITIONINFO)
  7396. /// mode:
  7397. /// &0x01 = allow invite
  7398. /// &0x10 = allow expel
  7399. /// ranking:
  7400. /// TODO
  7401. void clif_guild_positionchanged(const struct mmo_guild &g,int32 idx)
  7402. {
  7403. // FIXME: This packet is intended to update the clients after a
  7404. // commit of position info changes, not sending one packet per
  7405. // position.
  7406. map_session_data *sd;
  7407. unsigned char buf[128];
  7408. WBUFW(buf, 0)=0x174;
  7409. WBUFW(buf, 2)=44; // packet len
  7410. // GUILD_REG_POSITION_INFO{
  7411. WBUFL(buf, 4)=idx;
  7412. WBUFL(buf, 8)=g.position[idx].mode;
  7413. WBUFL(buf,12)=idx;
  7414. WBUFL(buf,16)=g.position[idx].exp_mode;
  7415. safestrncpy(WBUFCP(buf,20),g.position[idx].name,NAME_LENGTH);
  7416. // }*
  7417. if( (sd=guild_getavailablesd(g))!=nullptr )
  7418. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7419. }
  7420. /// Notifies clients in a guild about updated member position assignments.
  7421. /// 0156 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (ZC_ACK_REQ_CHANGE_MEMBERS)
  7422. void clif_guild_memberpositionchanged(const struct mmo_guild &g, int32 idx)
  7423. {
  7424. // FIXME: This packet is intended to update the clients after a
  7425. // commit of member position assignment changes, not sending one
  7426. // packet per position.
  7427. map_session_data *sd;
  7428. unsigned char buf[64];
  7429. WBUFW(buf, 0)=0x156;
  7430. WBUFW(buf, 2)=16; // packet len
  7431. // MEMBER_POSITION_INFO{
  7432. WBUFL(buf, 4)=g.member[idx].account_id;
  7433. WBUFL(buf, 8)=g.member[idx].char_id;
  7434. WBUFL(buf,12)=g.member[idx].position;
  7435. // }*
  7436. if( (sd=guild_getavailablesd(g))!=nullptr )
  7437. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7438. }
  7439. /// Sends emblems bitmap data to the client that requested it.
  7440. /// 0152 <packet len>.W <guild id>.L <emblem id>.L <emblem data>.?B (ZC_GUILD_EMBLEM_IMG)
  7441. void clif_guild_emblem(const map_session_data &sd, const struct mmo_guild &g)
  7442. {
  7443. int32 fd = sd.fd;
  7444. if( g.emblem_len <= 0 )
  7445. return;
  7446. WFIFOHEAD(fd,g.emblem_len+12);
  7447. WFIFOW(fd,0)=0x152;
  7448. WFIFOW(fd,2)=g.emblem_len+12;
  7449. WFIFOL(fd,4)=g.guild_id;
  7450. WFIFOL(fd,8)=g.emblem_id;
  7451. memcpy(WFIFOP(fd,12),g.emblem_data,g.emblem_len);
  7452. WFIFOSET(fd,WFIFOW(fd,2));
  7453. }
  7454. /// Sends update of the guild id/emblem id to everyone in the area.
  7455. /// 01b4 <id>.L <guild id>.L <emblem id>.W (ZC_CHANGE_GUILD)
  7456. /// 0b1f <guild id>.L <version>.L <AID>.L (ZC_NEW_EMBLEM_DOWNLOAD)
  7457. /// 0b47 <guild id>.L <version>.L <AID>.L (ZC_ACK_ADD_NEW_EMBLEM)
  7458. void clif_guild_emblem_area(struct block_list* bl)
  7459. {
  7460. // TODO this packet doesn't force the update of ui components that have the emblem visible
  7461. // (emblem in the flag npcs and emblem over the head in agit maps) [FlavioJS]
  7462. PACKET_ZC_CHANGE_GUILD p{};
  7463. p.packetType = HEADER_ZC_CHANGE_GUILD;
  7464. p.guild_id = status_get_guild_id(bl);
  7465. p.emblem_id = status_get_emblem_id(bl);
  7466. p.AID = bl->id;
  7467. clif_send(&p, sizeof(p), bl, AREA);
  7468. }
  7469. /// Sends guild skills.
  7470. /// 0162 <packet len>.W <skill points>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <atk range>.W <skill name>.24B <upgradable>.B }* (ZC_GUILD_SKILLINFO)
  7471. void clif_guild_skillinfo( map_session_data& sd ){
  7472. auto& g = sd.guild;
  7473. if( g == nullptr ){
  7474. return;
  7475. }
  7476. PACKET_ZC_GUILD_SKILLINFO* p = reinterpret_cast<PACKET_ZC_GUILD_SKILLINFO*>( packet_buffer );
  7477. p->PacketType = HEADER_ZC_GUILD_SKILLINFO;
  7478. p->PacketLength = sizeof( *p );
  7479. p->skillPoint = g->guild.skill_point;
  7480. for( size_t i = 0, c = 0; i < MAX_GUILDSKILL; i++ ){
  7481. if( g->guild.skill[i].id <= 0 ){
  7482. continue;
  7483. }
  7484. if( !guild_check_skill_require( g->guild, g->guild.skill[i].id ) ){
  7485. continue;
  7486. }
  7487. GUILD_SKILLDATA& gs = p->skillInfo[c];
  7488. int32 skill_id = g->guild.skill[i].id;
  7489. gs.id = skill_id;
  7490. gs.inf = skill_get_inf(skill_id);
  7491. gs.level = g->guild.skill[i].lv;
  7492. if( g->guild.skill[i].lv > 0 ){
  7493. gs.sp = skill_get_sp( skill_id, g->guild.skill[i].lv );
  7494. gs.range2 = skill_get_range( skill_id, g->guild.skill[i].lv );
  7495. }else{
  7496. gs.sp = 0;
  7497. gs.range2 = 0;
  7498. }
  7499. safestrncpy( gs.name, skill_get_name( skill_id ), sizeof( gs.name ) );
  7500. gs.upFlag = ( g->guild.skill[i].lv < guild_skill_get_max( skill_id ) && &sd == g->guild.member[0].sd ) ? 1 : 0;
  7501. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( gs ) );
  7502. c++;
  7503. }
  7504. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7505. }
  7506. /// Sends guild notice to client.
  7507. /// 016f <subject>.60B <notice>.120B (ZC_GUILD_NOTICE)
  7508. void clif_guild_notice( map_session_data& sd ){
  7509. auto& g = sd.guild;
  7510. if( g == nullptr ){
  7511. return;
  7512. }
  7513. if( g->guild.mes1[0] == '\0' && g->guild.mes2[0] == '\0' ){
  7514. return;
  7515. }
  7516. PACKET_ZC_GUILD_NOTICE p = {};
  7517. p.packetType = HEADER_ZC_GUILD_NOTICE;
  7518. safestrncpy( p.subject, g->guild.mes1, sizeof( p.subject ) );
  7519. safestrncpy( p.notice, g->guild.mes2, sizeof( p.notice ) );
  7520. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7521. }
  7522. /// Guild invite.
  7523. /// 016a <guild id>.L <guild name>.24B (ZC_REQ_JOIN_GUILD)
  7524. void clif_guild_invite( map_session_data& sd, const struct mmo_guild& g ){
  7525. PACKET_ZC_REQ_JOIN_GUILD p = {};
  7526. p.packetType = HEADER_ZC_REQ_JOIN_GUILD;
  7527. p.guild_id = g.guild_id;
  7528. safestrncpy( p.guild_name, g.name, sizeof( p.guild_name ) );
  7529. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7530. }
  7531. /// Reply to invite request.
  7532. /// 0169 <answer>.B (ZC_ACK_REQ_JOIN_GUILD)
  7533. /// answer:
  7534. /// 0 = Already in guild.
  7535. /// 1 = Offer rejected.
  7536. /// 2 = Offer accepted.
  7537. /// 3 = Guild full.
  7538. void clif_guild_inviteack( map_session_data& sd, int32 flag ){
  7539. PACKET_ZC_ACK_REQ_JOIN_GUILD p = {};
  7540. p.packetType = HEADER_ZC_ACK_REQ_JOIN_GUILD;
  7541. p.result = static_cast<decltype(p.result)>( flag );
  7542. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7543. }
  7544. /// Notifies clients of a guild of a leaving member.
  7545. /// 015a <char name>.24B <reason>.40B (ZC_ACK_LEAVE_GUILD)
  7546. /// 0a83 <CID>.L <reason>.40B (ZC_ACK_LEAVE_GUILD_DELNAME)
  7547. void clif_guild_leave( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7548. PACKET_ZC_ACK_LEAVE_GUILD p = {};
  7549. p.packetType = guildLeave;
  7550. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7551. p.GID = char_id;
  7552. #else
  7553. safestrncpy( p.name, name, sizeof( p.name ) );
  7554. #endif
  7555. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7556. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7557. }
  7558. /// Notifies clients of a guild of an expelled member.
  7559. /// 015c <char name>.24B <reason>.40B <account name>.24B (ZC_ACK_BAN_GUILD)
  7560. /// 0839 <char name>.24B <reason>.40B (ZC_ACK_BAN_GUILD_SSO)
  7561. /// 0a82 <CID>.L <reason>.40B (ZC_ACK_BAN_GUILD_DELNAME)
  7562. void clif_guild_expulsion( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7563. PACKET_ZC_ACK_BAN_GUILD p = {};
  7564. p.packetType = guildExpulsion;
  7565. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7566. p.GID = char_id;
  7567. #else
  7568. safestrncpy( p.name, name, sizeof( p.name ) );
  7569. #endif
  7570. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7571. #if PACKETVER < 20100803
  7572. // account name is not sent for security reasons
  7573. safestrncpy( p.account_name, "", sizeof( p.account_name ) );
  7574. #endif
  7575. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7576. }
  7577. /// Guild expulsion list
  7578. /// 0163 <packet len>.W { <char name>.24B <account name>.24B <reason>.40B }* (ZC_BAN_LIST)
  7579. /// 0163 <packet len>.W { <char name>.24B <reason>.40B }* (PACKETVER >= 20100803) (ZC_BAN_LIST)
  7580. /// 0a87 <packet len>.W { <charid>.L <reason>.40B }* (ZC_BAN_LIST2)
  7581. /// 0b7c <packet len>.W { <charid>.L <reason>.40B <char name>.24B }* (ZC_BAN_LIST3)
  7582. static void clif_guild_expulsionlist(map_session_data& sd){
  7583. auto &g = sd.guild;
  7584. if (!g)
  7585. return;
  7586. PACKET_ZC_BAN_LIST* p = reinterpret_cast<PACKET_ZC_BAN_LIST*>( packet_buffer );
  7587. p->packetType = HEADER_ZC_BAN_LIST;
  7588. p->packetLen = sizeof(*p);
  7589. for( size_t i = 0, c = 0; i < MAX_GUILDEXPULSION; i++ ){
  7590. struct guild_expulsion& e = g->guild.expulsion[i];
  7591. if( e.account_id == 0 ){
  7592. continue;
  7593. }
  7594. PACKET_ZC_BAN_LIST_sub& banned = p->chars[c];
  7595. #if PACKETVER >= 20161019
  7596. banned.char_id = e.char_id;
  7597. #elif PACKETVER < 20100803
  7598. // account name (not used for security reasons)
  7599. safestrncpy(banned.account_name, "", sizeof(banned.account_name));
  7600. #endif
  7601. #if PACKETVER >= 20200902 || PACKETVER < 20161019
  7602. safestrncpy(banned.char_name, e.name, sizeof(banned.char_name));
  7603. #endif
  7604. safestrncpy(banned.message, e.mes, sizeof(banned.message));
  7605. p->packetLen += static_cast<decltype(p->packetLen)>(sizeof(banned));
  7606. c++;
  7607. }
  7608. clif_send(p,p->packetLen,&sd.bl,SELF);
  7609. }
  7610. /// Guild chat message
  7611. /// 017f <packet len>.W <message>.?B (ZC_GUILD_CHAT)
  7612. void clif_guild_message( const struct mmo_guild& g, const char* mes, size_t len ){
  7613. PACKET_ZC_GUILD_CHAT *p = reinterpret_cast<PACKET_ZC_GUILD_CHAT*>( packet_buffer );
  7614. // -1 for null terminator
  7615. static const size_t max_len = CHAT_SIZE_MAX - sizeof( *p ) - 1;
  7616. map_session_data* sd = guild_getavailablesd(g);
  7617. // Ignore this message, if no guildmember is available
  7618. if (sd == nullptr)
  7619. return;
  7620. if( len == 0 ){
  7621. return;
  7622. } else if( len > max_len ){
  7623. ShowWarning("clif_guild_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", guild_id=%u).\n", mes, len, max_len, g.guild_id);
  7624. len = max_len;
  7625. }
  7626. p->packetType = HEADER_ZC_GUILD_CHAT;
  7627. p->packetLength = sizeof(*p);
  7628. safestrncpy(p->message, mes, len+1);
  7629. p->packetLength += static_cast<decltype(p->packetLength)>( len + 1 );
  7630. clif_send(p, p->packetLength, &sd->bl, GUILD_NOBG);
  7631. }
  7632. /// Request for guild alliance
  7633. /// 0171 <inviter account id>.L <guild name>.24B (ZC_REQ_ALLY_GUILD).
  7634. void clif_guild_reqalliance(map_session_data& sd,uint32 account_id,const char *name)
  7635. {
  7636. PACKET_ZC_REQ_ALLY_GUILD p{};
  7637. p.packetType = HEADER_ZC_REQ_ALLY_GUILD;
  7638. p.inviterId = account_id;
  7639. safestrncpy(p.inviterGuildName,name,sizeof(p.inviterGuildName));
  7640. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7641. }
  7642. /// Notifies the client about the result of a alliance request.
  7643. /// 0173 <answer>.B (ZC_ACK_REQ_ALLY_GUILD)
  7644. /// answer:
  7645. /// 0 = Already allied.
  7646. /// 1 = You rejected the offer.
  7647. /// 2 = You accepted the offer.
  7648. /// 3 = They have too any alliances.
  7649. /// 4 = You have too many alliances.
  7650. /// 5 = Alliances are disabled.
  7651. void clif_guild_allianceack(map_session_data& sd,uint8 flag){
  7652. PACKET_ZC_ACK_REQ_ALLY_GUILD p{};
  7653. p.packetType = HEADER_ZC_ACK_REQ_ALLY_GUILD;
  7654. p.flag = flag;
  7655. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7656. }
  7657. /// Notifies the client that a alliance or opposition has been removed.
  7658. /// 0184 <other guild id>.L <relation>.L (ZC_DELETE_RELATED_GUILD)
  7659. /// relation:
  7660. /// 0 = Ally
  7661. /// 1 = Enemy
  7662. void clif_guild_delalliance(map_session_data& sd,uint32 guild_id,uint32 flag)
  7663. {
  7664. PACKET_ZC_DELETE_RELATED_GUILD p{};
  7665. p.packetType = HEADER_ZC_DELETE_RELATED_GUILD;
  7666. p.allyID = guild_id;
  7667. p.flag = flag;
  7668. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7669. }
  7670. /// Notifies the client about the result of a opposition request.
  7671. /// 0181 <result>.B (ZC_ACK_REQ_HOSTILE_GUILD)
  7672. /// result:
  7673. /// 0 = Antagonist has been set.
  7674. /// 1 = Guild has too many Antagonists.
  7675. /// 2 = Already set as an Antagonist.
  7676. /// 3 = Antagonists are disabled.
  7677. void clif_guild_oppositionack(map_session_data& sd,uint8 flag){
  7678. PACKET_ZC_ACK_REQ_HOSTILE_GUILD p{};
  7679. p.packetType = HEADER_ZC_ACK_REQ_HOSTILE_GUILD;
  7680. p.flag = flag;
  7681. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7682. }
  7683. /// Adds alliance or opposition (ZC_ADD_RELATED_GUILD).
  7684. /// 0185 <relation>.L <guild id>.L <guild name>.24B
  7685. /*
  7686. void clif_guild_allianceadded(struct guild *g,int32 idx)
  7687. {
  7688. unsigned char buf[64];
  7689. WBUFW(buf,0)=0x185;
  7690. WBUFL(buf,2)=g->alliance[idx].opposition;
  7691. WBUFL(buf,6)=g->alliance[idx].guild_id;
  7692. safestrncpy(WBUFP(buf,10),g->alliance[idx].name,NAME_LENGTH);
  7693. clif_send(buf,packet_len(0x185),guild_getavailablesd(g),GUILD);
  7694. }
  7695. */
  7696. /// Notifies the client about the result of a guild break.
  7697. /// 015e <reason>.L (ZC_ACK_DISORGANIZE_GUILD_RESULT)
  7698. /// 0 = success
  7699. /// 1 = invalid key (guild name, @see clif_parse_GuildBreak)
  7700. /// 2 = there are still members in the guild
  7701. void clif_guild_broken( map_session_data& sd, int32 flag ){
  7702. PACKET_ZC_ACK_DISORGANIZE_GUILD_RESULT p = {};
  7703. p.packetType = HEADER_ZC_ACK_DISORGANIZE_GUILD_RESULT;
  7704. p.result = flag;
  7705. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7706. }
  7707. void clif_guild_position_selected(map_session_data& sd)
  7708. {
  7709. #if PACKETVER_MAIN_NUM >= 20180605 || PACKETVER_RE_NUM >= 20180605 || PACKETVER_ZERO_NUM >= 20180605
  7710. PACKET_ZC_GUILD_POSITION* p = reinterpret_cast<PACKET_ZC_GUILD_POSITION*>(packet_buffer);
  7711. p->packetType = HEADER_ZC_GUILD_POSITION;
  7712. p->packetLength = sizeof( PACKET_ZC_GUILD_POSITION );
  7713. p->AID = sd.bl.id;
  7714. if( sd.guild != nullptr ){
  7715. const auto& g = sd.guild->guild;
  7716. if( int ps = guild_getposition( sd ); ps != -1 ){
  7717. safestrncpy( p->position, g.position[ps].name, NAME_LENGTH );
  7718. p->packetLength += static_cast<decltype(p->packetLength)>( NAME_LENGTH );
  7719. }
  7720. }
  7721. clif_send( p, p->packetLength, &sd.bl, AREA );
  7722. #else
  7723. clif_name_area(&sd.bl);
  7724. #endif
  7725. }
  7726. /// Displays emotion on an object (ZC_EMOTION).
  7727. /// 00c0 <id>.L <type>.B
  7728. /// type:
  7729. /// enum emotion_type
  7730. void clif_emotion(struct block_list *bl,int32 type)
  7731. {
  7732. unsigned char buf[8];
  7733. nullpo_retv(bl);
  7734. WBUFW(buf,0)=0xc0;
  7735. WBUFL(buf,2)=bl->id;
  7736. WBUFB(buf,6)=type;
  7737. clif_send(buf,packet_len(0xc0),bl,AREA);
  7738. }
  7739. /// Displays the contents of a talkiebox trap.
  7740. /// 0191 <id>.L <contents>.80B (ZC_TALKBOX_CHATCONTENTS)
  7741. void clif_talkiebox( struct block_list* bl, const char* talkie ){
  7742. nullpo_retv( bl );
  7743. nullpo_retv( talkie );
  7744. PACKET_ZC_TALKBOX_CHATCONTENTS p = {};
  7745. p.PacketType = HEADER_ZC_TALKBOX_CHATCONTENTS;
  7746. p.aid = bl->id;
  7747. safestrncpy( p.message, talkie, TALKBOX_MESSAGE_SIZE );
  7748. clif_send( &p, sizeof( PACKET_ZC_TALKBOX_CHATCONTENTS ), bl, AREA );
  7749. }
  7750. /// Displays wedding effect centered on an object (ZC_CONGRATULATION).
  7751. /// 01ea <id>.L
  7752. void clif_wedding_effect(struct block_list *bl)
  7753. {
  7754. unsigned char buf[6];
  7755. nullpo_retv(bl);
  7756. WBUFW(buf,0) = 0x1ea;
  7757. WBUFL(buf,2) = bl->id;
  7758. clif_send(buf, packet_len(0x1ea), bl, AREA);
  7759. }
  7760. /// Notifies the client of the name of the partner character (ZC_COUPLENAME).
  7761. /// 01e6 <partner name>.24B
  7762. void clif_callpartner(map_session_data& sd)
  7763. {
  7764. PACKET_ZC_COUPLENAME p = { 0 };
  7765. p.packetType = HEADER_ZC_COUPLENAME;
  7766. if( sd.status.partner_id ) {
  7767. map_session_data *p_sd = pc_get_partner(&sd);
  7768. if (p_sd != nullptr && !p_sd->state.autotrade)
  7769. safestrncpy(p.name, p_sd->status.name, NAME_LENGTH);
  7770. else
  7771. p.name[0] = 0;
  7772. } else {// Send zero-length name if no partner, to initialize the client buffer.
  7773. p.name[0] = 0;
  7774. }
  7775. clif_send(&p, sizeof(p), &sd.bl, AREA);
  7776. }
  7777. /// Initiates the partner "taming" process [DracoRPG] (ZC_START_COUPLE).
  7778. /// 01e4
  7779. /// This packet while still implemented by the client is no longer being officially used.
  7780. /*
  7781. void clif_marriage_process(map_session_data *sd)
  7782. {
  7783. int32 fd;
  7784. nullpo_retv(sd);
  7785. fd=sd->fd;
  7786. WFIFOHEAD(fd,packet_len(0x1e4));
  7787. WFIFOW(fd,0)=0x1e4;
  7788. WFIFOSET(fd,packet_len(0x1e4));
  7789. }
  7790. */
  7791. /// Notice of divorce (ZC_DIVORCE).
  7792. /// 0205 <partner name>.24B
  7793. void clif_divorced(map_session_data* sd, const char* name)
  7794. {
  7795. int32 fd;
  7796. nullpo_retv(sd);
  7797. fd=sd->fd;
  7798. WFIFOHEAD(fd,packet_len(0x205));
  7799. WFIFOW(fd,0)=0x205;
  7800. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  7801. WFIFOSET(fd, packet_len(0x205));
  7802. }
  7803. /// Marriage proposal (ZC_REQ_COUPLE).
  7804. /// 01e2 <account id>.L <char id>.L <char name>.24B
  7805. /// This packet while still implemented by the client is no longer being officially used.
  7806. /*
  7807. void clif_marriage_proposal(int32 fd, map_session_data *sd, map_session_data* ssd)
  7808. {
  7809. nullpo_retv(sd);
  7810. WFIFOHEAD(fd,packet_len(0x1e2));
  7811. WFIFOW(fd,0) = 0x1e2;
  7812. WFIFOL(fd,2) = ssd->status.account_id;
  7813. WFIFOL(fd,6) = ssd->status.char_id;
  7814. safestrncpy(WFIFOCP(fd,10), ssd->status.name, NAME_LENGTH);
  7815. WFIFOSET(fd, packet_len(0x1e2));
  7816. }
  7817. */
  7818. /*==========================================
  7819. * Displays a message using the guild-chat colors to the specified targets. [Skotlex]
  7820. *------------------------------------------*/
  7821. void clif_disp_message(struct block_list* src, const char* mes, size_t len, enum send_target target)
  7822. {
  7823. unsigned char buf[256];
  7824. if( len == 0 ) {
  7825. return;
  7826. } else if( len > sizeof(buf)-5 ) {
  7827. ShowWarning("clif_disp_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", aid=%d).\n", mes, len, sizeof(buf)-5, src->id);
  7828. len = sizeof(buf)-5;
  7829. }
  7830. WBUFW(buf, 0) = 0x17f;
  7831. WBUFW(buf, 2) = static_cast<int16>( len + 5 );
  7832. safestrncpy(WBUFCP(buf,4), mes, len+1);
  7833. clif_send(buf, WBUFW(buf,2), src, target);
  7834. }
  7835. /// Notifies the client about the result of a request to disconnect another player (ZC_ACK_DISCONNECT_CHARACTER).
  7836. /// 00cd <result>.L (unknown packet version or invalid information at packet_len_table)
  7837. /// 00cd <result>.B
  7838. /// result:
  7839. /// 0 = failure
  7840. /// 1 = success
  7841. void clif_GM_kickack(map_session_data *sd, int32 id)
  7842. {
  7843. int32 fd;
  7844. nullpo_retv(sd);
  7845. fd = sd->fd;
  7846. WFIFOHEAD(fd,packet_len(0xcd));
  7847. WFIFOW(fd,0) = 0xcd;
  7848. WFIFOB(fd,2) = id; // FIXME: this is not account id
  7849. WFIFOSET(fd, packet_len(0xcd));
  7850. }
  7851. void clif_GM_kick(map_session_data *sd, map_session_data *tsd)
  7852. {
  7853. nullpo_retv(tsd);
  7854. if (sd == nullptr)
  7855. tsd->state.keepshop = true;
  7856. if (session_isActive(tsd->fd))
  7857. clif_authfail_fd(tsd->fd, 15);
  7858. else
  7859. map_quit(tsd);
  7860. if (sd)
  7861. clif_GM_kickack(sd, tsd->status.account_id);
  7862. }
  7863. /// Displays various manner-related status messages (ZC_ACK_GIVE_MANNER_POINT).
  7864. /// 014a <result>.L
  7865. /// result:
  7866. /// 0 = "A manner point has been successfully aligned."
  7867. /// 1 = MP_FAILURE_EXHAUST
  7868. /// 2 = MP_FAILURE_ALREADY_GIVING
  7869. /// 3 = "Chat Block has been applied by GM due to your ill-mannerous action."
  7870. /// 4 = "Automated Chat Block has been applied due to Anti-Spam System."
  7871. /// 5 = "You got a good point from %s."
  7872. void clif_manner_message(map_session_data* sd, uint32 type)
  7873. {
  7874. int32 fd;
  7875. nullpo_retv(sd);
  7876. fd = sd->fd;
  7877. WFIFOHEAD(fd,packet_len(0x14a));
  7878. WFIFOW(fd,0) = 0x14a;
  7879. WFIFOL(fd,2) = type;
  7880. WFIFOSET(fd, packet_len(0x14a));
  7881. }
  7882. /// Followup to 0x14a type 3/5, informs who did the manner adjustment action (ZC_NOTIFY_MANNER_POINT_GIVEN).
  7883. /// 014b <type>.B <GM name>.24B
  7884. /// type:
  7885. /// 0 = positive (unmute)
  7886. /// 1 = negative (mute)
  7887. void clif_GM_silence(map_session_data* sd, map_session_data* tsd, uint8 type)
  7888. {
  7889. int32 fd;
  7890. nullpo_retv(sd);
  7891. nullpo_retv(tsd);
  7892. fd = tsd->fd;
  7893. WFIFOHEAD(fd,packet_len(0x14b));
  7894. WFIFOW(fd,0) = 0x14b;
  7895. WFIFOB(fd,2) = type;
  7896. safestrncpy(WFIFOCP(fd,3), sd->status.name, NAME_LENGTH);
  7897. WFIFOSET(fd, packet_len(0x14b));
  7898. }
  7899. /// Notifies the client about the result of a request to allow/deny whispers from a player (ZC_SETTING_WHISPER_PC).
  7900. /// 00d1 <type>.B <result>.B
  7901. /// type:
  7902. /// 0 = /ex (deny)
  7903. /// 1 = /in (allow)
  7904. /// result:
  7905. /// 0 = success
  7906. /// 1 = failure
  7907. /// 2 = too many blocks
  7908. void clif_wisexin(map_session_data *sd,int32 type,int32 flag)
  7909. {
  7910. int32 fd;
  7911. nullpo_retv(sd);
  7912. fd=sd->fd;
  7913. WFIFOHEAD(fd,packet_len(0xd1));
  7914. WFIFOW(fd,0)=0xd1;
  7915. WFIFOB(fd,2)=type;
  7916. WFIFOB(fd,3)=flag;
  7917. WFIFOSET(fd,packet_len(0xd1));
  7918. }
  7919. /// Notifies the client about the result of a request to allow/deny whispers from anyone (ZC_SETTING_WHISPER_STATE).
  7920. /// 00d2 <type>.B <result>.B
  7921. /// type:
  7922. /// 0 = /exall (deny)
  7923. /// 1 = /inall (allow)
  7924. /// result:
  7925. /// 0 = success
  7926. /// 1 = failure
  7927. void clif_wisall(map_session_data *sd,int32 type,int32 flag)
  7928. {
  7929. int32 fd;
  7930. nullpo_retv(sd);
  7931. fd=sd->fd;
  7932. WFIFOHEAD(fd,packet_len(0xd2));
  7933. WFIFOW(fd,0)=0xd2;
  7934. WFIFOB(fd,2)=type;
  7935. WFIFOB(fd,3)=flag;
  7936. WFIFOSET(fd,packet_len(0xd2));
  7937. }
  7938. /// Play a BGM! [Rikter/Yommy] (ZC_PLAY_NPC_BGM).
  7939. /// 07fe <bgm>.24B
  7940. void clif_playBGM( map_session_data& sd, const char* name ){
  7941. #if PACKETVER_MAIN_NUM >= 20220504
  7942. PACKET_ZC_PLAY_NPC_BGM* p = reinterpret_cast<PACKET_ZC_PLAY_NPC_BGM*>( packet_buffer );
  7943. size_t length = strlen( name ) + 1;
  7944. p->PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7945. p->PacketLength = sizeof( *p );
  7946. p->playType = 0; // TODO: implement, send loop for the time being
  7947. safestrncpy( p->bgm, name, length );
  7948. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  7949. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7950. #elif PACKETVER >= 20091201
  7951. PACKET_ZC_PLAY_NPC_BGM p = {};
  7952. p.PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7953. safestrncpy( p.bgm, name, sizeof( p.bgm ) );
  7954. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7955. #endif
  7956. }
  7957. /// Plays/stops a wave sound (ZC_SOUND).
  7958. /// 01d3 <file name>.24B <act>.B <term>.L <npc id>.L
  7959. /// file name:
  7960. /// relative to data\wav
  7961. /// act:
  7962. /// 0 = play (once)
  7963. /// 1 = play (repeat, does not work)
  7964. /// 2 = stops all sound instances of file name (does not work)
  7965. /// term:
  7966. /// unknown purpose, only relevant to act = 1
  7967. /// npc id:
  7968. /// The accustic direction of the sound is determined by the
  7969. /// relative position of the NPC to the player (3D sound).
  7970. void clif_soundeffect( struct block_list& bl, const char* name, int32 type, enum send_target target ){
  7971. PACKET_ZC_SOUND p = {};
  7972. p.PacketType = HEADER_ZC_SOUND;
  7973. safestrncpy( p.name, name, sizeof( p.name ) );
  7974. p.act = type;
  7975. p.term = 0; // TODO: implement
  7976. p.AID = bl.id;
  7977. clif_send( &p, sizeof( p ), &bl, target );
  7978. }
  7979. /// Displays special effects (npcs, weather, etc) [Valaris] (ZC_NOTIFY_EFFECT2).
  7980. /// 01f3 <id>.L <effect id>.L
  7981. /// effect id:
  7982. /// @see doc/effect_list.txt
  7983. void clif_specialeffect(struct block_list* bl, int32 type, enum send_target target)
  7984. {
  7985. unsigned char buf[24];
  7986. nullpo_retv(bl);
  7987. memset(buf, 0, packet_len(0x1f3));
  7988. WBUFW(buf,0) = 0x1f3;
  7989. WBUFL(buf,2) = bl->id;
  7990. WBUFL(buf,6) = type;
  7991. clif_send(buf, packet_len(0x1f3), bl, target);
  7992. if (disguised(bl)) {
  7993. WBUFL(buf,2) = disguised_bl_id(bl->id);
  7994. clif_send(buf, packet_len(0x1f3), bl, SELF);
  7995. }
  7996. }
  7997. void clif_specialeffect_single(struct block_list* bl, int32 type, int32 fd)
  7998. {
  7999. WFIFOHEAD(fd,10);
  8000. WFIFOW(fd,0) = 0x1f3;
  8001. WFIFOL(fd,2) = bl->id;
  8002. WFIFOL(fd,6) = type;
  8003. WFIFOSET(fd,10);
  8004. }
  8005. /// Notifies clients of an special/visual effect that accepts an value (ZC_NOTIFY_EFFECT3).
  8006. /// 0284 <id>.L <effect id>.L <num data>.L
  8007. /// effect id:
  8008. /// @see doc/effect_list.txt
  8009. /// num data:
  8010. /// effect-dependent value
  8011. void clif_specialeffect_value(struct block_list* bl, int32 effect_id, int32 num, send_target target)
  8012. {
  8013. uint8 buf[14];
  8014. WBUFW(buf,0) = 0x284;
  8015. WBUFL(buf,2) = bl->id;
  8016. WBUFL(buf,6) = effect_id;
  8017. WBUFL(buf,10) = num;
  8018. clif_send(buf, packet_len(0x284), bl, target);
  8019. if( disguised(bl) )
  8020. {
  8021. WBUFL(buf,2) = disguised_bl_id(bl->id);
  8022. clif_send(buf, packet_len(0x284), bl, SELF);
  8023. }
  8024. }
  8025. void clif_specialeffect_remove(struct block_list* bl_src, int32 effect, enum send_target e_target, struct block_list* bl_target)
  8026. {
  8027. #if PACKETVER >= 20181002
  8028. nullpo_retv( bl_src );
  8029. nullpo_retv( bl_target );
  8030. PACKET_ZC_REMOVE_EFFECT p = {};
  8031. p.packetType = HEADER_ZC_REMOVE_EFFECT;
  8032. p.aid = bl_src->id;
  8033. p.effectId = effect;
  8034. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_target, e_target );
  8035. if( disguised(bl_src) )
  8036. {
  8037. p.aid = disguised_bl_id( bl_src->id );
  8038. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_src, SELF );
  8039. }
  8040. #endif
  8041. }
  8042. /// Monster/NPC color chat [SnakeDrak] (ZC_NPC_CHAT).
  8043. /// 02c1 <packet len>.W <id>.L <color>.L <message>.?B
  8044. void clif_messagecolor_target(struct block_list *bl, unsigned long color, const char *msg, bool rgb2bgr, enum send_target type, map_session_data *sd) {
  8045. uint16 msg_len = (uint16)(strlen(msg) + 1);
  8046. uint8 buf[CHAT_SIZE_MAX];
  8047. nullpo_retv(bl);
  8048. if (msg_len > sizeof(buf) - 12) {
  8049. ShowWarning("clif_messagecolor: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  8050. msg_len = sizeof(buf) - 12;
  8051. }
  8052. if (rgb2bgr)
  8053. color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16; // RGB to BGR
  8054. WBUFW(buf,0) = 0x2C1;
  8055. WBUFW(buf,2) = msg_len + 12;
  8056. WBUFL(buf,4) = bl->id;
  8057. WBUFL(buf,8) = color;
  8058. memcpy(WBUFCP(buf,12), msg, msg_len);
  8059. clif_send(buf, WBUFW(buf,2), (sd == nullptr ? bl : &(sd->bl)), type);
  8060. }
  8061. /**
  8062. * Notifies the client that the storage window is still open
  8063. *
  8064. * Should only be used in cases where the client closed the
  8065. * storage window without server's consent
  8066. */
  8067. void clif_refresh_storagewindow(map_session_data *sd) {
  8068. // Notify the client that the storage is open
  8069. if( sd->state.storage_flag == 1 ) {
  8070. storage_sortitem(sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage));
  8071. clif_storagelist(sd, sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage), storage_getName(0));
  8072. clif_updatestorageamount(*sd, sd->storage.amount, sd->storage.max_amount);
  8073. }
  8074. // Notify the client that the gstorage is open otherwise it will
  8075. // remain locked forever and nobody will be able to access it
  8076. if( sd->state.storage_flag == 2 ) {
  8077. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  8078. if( !gstor ) // Shouldn't happen. The information should already be at the map-server
  8079. intif_request_guild_storage(sd->status.account_id, sd->status.guild_id);
  8080. else {
  8081. storage_sortitem(gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild));
  8082. clif_storagelist(sd, gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild), "Guild Storage");
  8083. clif_updatestorageamount(*sd, gstor->amount, gstor->max_amount);
  8084. }
  8085. }
  8086. // Notify the client that the premium storage is open
  8087. if (sd->state.storage_flag == 3) {
  8088. storage_sortitem(sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage));
  8089. clif_storagelist(sd, sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage), storage_getName(sd->premiumStorage.stor_id));
  8090. clif_updatestorageamount(*sd, sd->premiumStorage.amount, sd->premiumStorage.max_amount);
  8091. }
  8092. }
  8093. // refresh the client's screen, getting rid of any effects
  8094. void clif_refresh(map_session_data *sd)
  8095. {
  8096. nullpo_retv(sd);
  8097. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8098. clif_inventorylist(sd);
  8099. clif_equipswitch_list(sd);
  8100. if(pc_iscarton(sd)) {
  8101. clif_cartlist(sd);
  8102. clif_updatestatus(*sd,SP_CARTINFO);
  8103. }
  8104. clif_updatestatus(*sd,SP_WEIGHT);
  8105. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8106. clif_updatestatus(*sd,SP_STR);
  8107. clif_updatestatus(*sd,SP_AGI);
  8108. clif_updatestatus(*sd,SP_VIT);
  8109. clif_updatestatus(*sd,SP_INT);
  8110. clif_updatestatus(*sd,SP_DEX);
  8111. clif_updatestatus(*sd,SP_LUK);
  8112. #ifdef RENEWAL
  8113. clif_updatestatus(*sd,SP_POW);
  8114. clif_updatestatus(*sd,SP_STA);
  8115. clif_updatestatus(*sd,SP_WIS);
  8116. clif_updatestatus(*sd,SP_SPL);
  8117. clif_updatestatus(*sd,SP_CON);
  8118. clif_updatestatus(*sd,SP_CRT);
  8119. #endif
  8120. if (sd->spiritball)
  8121. clif_spiritball( &sd->bl, &sd->bl, SELF );
  8122. clif_millenniumshield_single( *sd, *sd );
  8123. clif_spiritcharm_single( *sd, *sd );
  8124. if (sd->soulball)
  8125. clif_soulball( sd, &sd->bl, SELF );
  8126. if (sd->servantball)
  8127. clif_servantball( *sd, &sd->bl, SELF );
  8128. if (sd->abyssball)
  8129. clif_abyssball( *sd, &sd->bl, SELF );
  8130. if (sd->vd.cloth_color)
  8131. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8132. if (sd->vd.body_style)
  8133. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8134. if(hom_is_active(sd->hd))
  8135. clif_send_homdata( *sd->hd, SP_ACK );
  8136. if( sd->md ) {
  8137. clif_mercenary_info(sd);
  8138. clif_mercenary_skillblock(sd);
  8139. }
  8140. if( sd->ed )
  8141. clif_elemental_info(sd);
  8142. map_foreachinallrange(clif_getareachar,&sd->bl,AREA_SIZE,BL_ALL,sd);
  8143. clif_weather_check(sd);
  8144. if( sd->chatID )
  8145. chat_leavechat(sd,0);
  8146. if( sd->state.vending )
  8147. clif_openvending( *sd );
  8148. if( pc_issit(sd) )
  8149. clif_sitting(sd->bl); // FIXME: just send to self, not area
  8150. if( pc_isdead(sd) ) // When you refresh, resend the death packet.
  8151. clif_clearunit_single( sd->bl.id, CLR_DEAD, *sd );
  8152. else
  8153. clif_changed_dir(sd->bl, SELF);
  8154. clif_efst_status_change_sub(&sd->bl,&sd->bl,SELF);
  8155. //Issue #2143
  8156. //Cancel Trading State
  8157. if (sd->state.trading)
  8158. trade_tradecancel(sd);
  8159. //Cancel Buying/Selling State
  8160. if (sd->state.buyingstore)
  8161. buyingstore_close(sd);
  8162. mail_clear(sd);
  8163. clif_loadConfirm( sd );
  8164. if( disguised(&sd->bl) ) {/* refresh-da */
  8165. int16 disguise = sd->disguise;
  8166. pc_disguise(sd, 0);
  8167. pc_disguise(sd, disguise);
  8168. }
  8169. clif_refresh_storagewindow(sd);
  8170. }
  8171. /// Updates the object's (bl) name on client.
  8172. /// Used to update when a char leaves a party/guild. [Skotlex]
  8173. /// Needed because when you send a 0x95 packet, the client will not remove the cached party/guild info that is not sent.
  8174. /// 0095 <id>.L <char name>.24B (ZC_ACK_REQNAME)
  8175. /// 0195 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B (ZC_ACK_REQNAMEALL)
  8176. /// 0a30 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B <title ID>.L (ZC_ACK_REQNAMEALL2)
  8177. void clif_name( struct block_list* src, struct block_list *bl, send_target target ){
  8178. nullpo_retv( src );
  8179. nullpo_retv( bl );
  8180. switch( bl->type ){
  8181. case BL_PC: {
  8182. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8183. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8184. packet.gid = bl->id;
  8185. map_session_data *sd = (map_session_data *)bl;
  8186. //Requesting your own "shadow" name. [Skotlex]
  8187. if( src == bl && target == SELF && sd->disguise ){
  8188. packet.gid = -bl->id;
  8189. }
  8190. if( sd->fakename[0] ) {
  8191. safestrncpy( packet.name, sd->fakename, NAME_LENGTH );
  8192. clif_send( &packet, sizeof(packet), src, target );
  8193. return;
  8194. }
  8195. safestrncpy( packet.name, sd->status.name, NAME_LENGTH );
  8196. party_data *p = nullptr;
  8197. if( sd->status.party_id ){
  8198. p = party_search( sd->status.party_id );
  8199. }
  8200. // do not display party unless the player is also in a guild
  8201. if( p && ( sd->guild || battle_config.display_party_name ) ){
  8202. safestrncpy( packet.party_name, p->party.name, NAME_LENGTH );
  8203. }
  8204. if( sd->guild ){
  8205. int32 position;
  8206. // Will get the position of the guild the player is in
  8207. position = guild_getposition(*sd);
  8208. safestrncpy( packet.guild_name, sd->guild->guild.name, NAME_LENGTH );
  8209. safestrncpy( packet.position_name, sd->guild->guild.position[position].name, NAME_LENGTH );
  8210. }else if( sd->clan ){
  8211. safestrncpy( packet.position_name, sd->clan->name, NAME_LENGTH );
  8212. }
  8213. #if PACKETVER_MAIN_NUM >= 20150225 || PACKETVER_RE_NUM >= 20141126 || defined( PACKETVER_ZERO )
  8214. packet.title_id = sd->status.title_id; // Title ID
  8215. #endif
  8216. clif_send(&packet, sizeof(packet), src, target);
  8217. }
  8218. break;
  8219. //[blackhole89]
  8220. case BL_HOM:
  8221. case BL_MER:
  8222. case BL_PET:
  8223. case BL_NPC:
  8224. case BL_ELEM: {
  8225. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8226. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8227. packet.gid = bl->id;
  8228. switch (bl->type) {
  8229. case BL_HOM:
  8230. memcpy(packet.name, ((TBL_HOM *)bl)->homunculus.name, NAME_LENGTH);
  8231. break;
  8232. case BL_MER:
  8233. memcpy(packet.name, ((TBL_MER *)bl)->db->name.c_str(), NAME_LENGTH);
  8234. break;
  8235. case BL_PET:
  8236. safestrncpy(packet.name, ((TBL_PET *)bl)->pet.name, NAME_LENGTH);
  8237. break;
  8238. case BL_NPC:
  8239. safestrncpy(packet.name, ((TBL_NPC *)bl)->name, NAME_LENGTH);
  8240. break;
  8241. case BL_ELEM:
  8242. safestrncpy(packet.name, ((TBL_ELEM *)bl)->db->name.c_str(), NAME_LENGTH);
  8243. break;
  8244. }
  8245. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8246. unit_data *ud = unit_bl2ud(bl);
  8247. if (ud != nullptr) {
  8248. memcpy(packet.title, ud->title, NAME_LENGTH);
  8249. packet.groupId = ud->group_id;
  8250. }
  8251. #endif
  8252. clif_send(&packet, sizeof(packet), src, target);
  8253. }
  8254. break;
  8255. case BL_MOB: {
  8256. mob_data *md = (mob_data *)bl;
  8257. if( md->guardian_data && md->guardian_data->guild_id ){
  8258. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8259. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8260. packet.gid = bl->id;
  8261. safestrncpy( packet.name, md->name, NAME_LENGTH );
  8262. safestrncpy( packet.guild_name, md->guardian_data->guild_name, NAME_LENGTH );
  8263. safestrncpy( packet.position_name, md->guardian_data->castle->castle_name, NAME_LENGTH );
  8264. clif_send(&packet, sizeof(packet), src, target);
  8265. }else if( battle_config.show_mob_info ){
  8266. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8267. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8268. packet.gid = bl->id;
  8269. safestrncpy( packet.name, md->name, NAME_LENGTH );
  8270. char mobhp[50], *str_p = mobhp;
  8271. if( battle_config.show_mob_info&4 ){
  8272. str_p += sprintf( str_p, "Lv. %d | ", md->level );
  8273. }
  8274. if( battle_config.show_mob_info&1 ){
  8275. str_p += sprintf( str_p, "HP: %u/%u | ", md->status.hp, md->status.max_hp );
  8276. }
  8277. if( battle_config.show_mob_info&2 ){
  8278. str_p += sprintf( str_p, "HP: %u%% | ", get_percentage( md->status.hp, md->status.max_hp ) );
  8279. }
  8280. // Even thought mobhp ain't a name, we send it as one so the client can parse it. [Skotlex]
  8281. if( str_p != mobhp ){
  8282. *(str_p-3) = '\0'; //Remove trailing space + pipe.
  8283. safestrncpy( packet.party_name, mobhp, NAME_LENGTH );
  8284. }
  8285. clif_send(&packet, sizeof(packet), src, target);
  8286. } else {
  8287. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8288. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8289. packet.gid = bl->id;
  8290. safestrncpy(packet.name, md->name, NAME_LENGTH);
  8291. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8292. unit_data *ud = unit_bl2ud(bl);
  8293. if (ud != nullptr) {
  8294. memcpy(packet.title, ud->title, NAME_LENGTH);
  8295. packet.groupId = ud->group_id;
  8296. }
  8297. #endif
  8298. clif_send(&packet, sizeof(packet), src, target);
  8299. }
  8300. }
  8301. break;
  8302. case BL_CHAT:
  8303. case BL_SKILL:
  8304. // Newer clients request this, but do not need an answer
  8305. return;
  8306. default:
  8307. ShowError("clif_name: bad type %d(%d)\n", bl->type, bl->id);
  8308. return;
  8309. }
  8310. }
  8311. /// Taekwon Jump (TK_HIGHJUMP) effect (ZC_HIGHJUMP).
  8312. /// 01ff <id>.L <x>.W <y>.W
  8313. ///
  8314. /// Visually moves(instant) a character to x,y. The char moves even
  8315. /// when the target cell isn't walkable. If the char is sitting it
  8316. /// stays that way.
  8317. void clif_slide(block_list& bl, int32 x, int32 y){
  8318. PACKET_ZC_HIGHJUMP p{};
  8319. p.packetType = HEADER_ZC_HIGHJUMP;
  8320. p.srcId = bl.id;
  8321. p.x = x;
  8322. p.y = y;
  8323. clif_send(&p, sizeof(p), &bl, AREA);
  8324. if( disguised(&bl) )
  8325. {
  8326. p.srcId = disguised_bl_id(bl.id);
  8327. clif_send(&p, sizeof(p), &bl, SELF);
  8328. }
  8329. }
  8330. /// Public chat message (ZC_NOTIFY_CHAT). lordalfa/Skotlex - used by @me as well
  8331. /// 008d <packet len>.W <id>.L <message>.?B
  8332. void clif_disp_overhead_(struct block_list *bl, const char* mes, enum send_target flag)
  8333. {
  8334. unsigned char buf[256]; //This should be more than sufficient, the theorical max is CHAT_SIZE + 8 (pads and extra inserted crap)
  8335. int16 len_mes;
  8336. len_mes = static_cast<decltype(len_mes)>( strlen(mes) + 1 ); //Account for \0
  8337. if (len_mes > sizeof(buf)-8) {
  8338. ShowError("clif_disp_overhead: Message too long (length %d)\n", len_mes);
  8339. len_mes = sizeof(buf)-8; //Trunk it to avoid problems.
  8340. }
  8341. // send message to others
  8342. if (flag == AREA) {
  8343. WBUFW(buf,0) = 0x8d;
  8344. WBUFW(buf,2) = len_mes + 8; // len of message + 8 (command+len+id)
  8345. WBUFL(buf,4) = bl->id;
  8346. safestrncpy(WBUFCP(buf,8), mes, len_mes);
  8347. clif_send(buf, WBUFW(buf,2), bl, AREA_CHAT_WOC);
  8348. }
  8349. // send back message to the speaker
  8350. if( bl->type == BL_PC ) {
  8351. WBUFW(buf,0) = 0x8e;
  8352. WBUFW(buf, 2) = len_mes + 4;
  8353. safestrncpy(WBUFCP(buf,4), mes, len_mes);
  8354. clif_send(buf, WBUFW(buf,2), bl, SELF);
  8355. }
  8356. }
  8357. /*==========================
  8358. * Minimap fix [Kevin]
  8359. * Remove dot from minimap
  8360. *--------------------------*/
  8361. void clif_party_xy_remove(map_session_data* sd)
  8362. {
  8363. nullpo_retv(sd);
  8364. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p{};
  8365. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  8366. p.AID = sd->status.account_id;
  8367. p.xPos = -1;
  8368. p.yPos = -1;
  8369. clif_send(&p, sizeof(p), &sd->bl, PARTY_SAMEMAP_WOS);
  8370. }
  8371. /// Displays a skill message (thanks to Rayce) (ZC_SKILLMSG).
  8372. /// 0215 <msg id>.L
  8373. /// msg id:
  8374. /// 0x15 = End all negative status (PA_GOSPEL)
  8375. /// 0x16 = Immunity to all status (PA_GOSPEL)
  8376. /// 0x17 = MaxHP +100% (PA_GOSPEL)
  8377. /// 0x18 = MaxSP +100% (PA_GOSPEL)
  8378. /// 0x19 = All stats +20 (PA_GOSPEL)
  8379. /// 0x1c = Enchant weapon with Holy element (PA_GOSPEL)
  8380. /// 0x1d = Enchant armor with Holy element (PA_GOSPEL)
  8381. /// 0x1e = DEF +25% (PA_GOSPEL)
  8382. /// 0x1f = ATK +100% (PA_GOSPEL)
  8383. /// 0x20 = HIT/Flee +50 (PA_GOSPEL)
  8384. /// 0x28 = Full strip failed because of coating (ST_FULLSTRIP)
  8385. /// ? = nothing
  8386. void clif_gospel_info(map_session_data *sd, int32 type)
  8387. {
  8388. int32 fd=sd->fd;
  8389. WFIFOHEAD(fd,packet_len(0x215));
  8390. WFIFOW(fd,0)=0x215;
  8391. WFIFOL(fd,2)=type;
  8392. WFIFOSET(fd, packet_len(0x215));
  8393. }
  8394. /// Multi-purpose mission information packet (ZC_STARSKILL).
  8395. /// 020e <mapname>.24B <monster_id>.L <star>.B <result>.B
  8396. /// result:
  8397. /// 0 = Star Gladiator %s has designed <mapname>'s as the %s.
  8398. /// star:
  8399. /// 0 = Place of the Sun
  8400. /// 1 = Place of the Moon
  8401. /// 2 = Place of the Stars
  8402. /// 1 = Star Gladiator %s's %s: <mapname>
  8403. /// star:
  8404. /// 0 = Place of the Sun
  8405. /// 1 = Place of the Moon
  8406. /// 2 = Place of the Stars
  8407. /// 10 = Star Gladiator %s has designed <mapname>'s as the %s.
  8408. /// star:
  8409. /// 0 = Target of the Sun
  8410. /// 1 = Target of the Moon
  8411. /// 2 = Target of the Stars
  8412. /// 11 = Star Gladiator %s's %s: <mapname used as monster name>
  8413. /// star:
  8414. /// 0 = Monster of the Sun
  8415. /// 1 = Monster of the Moon
  8416. /// 2 = Monster of the Stars
  8417. /// 20 = [TaeKwon Mission] Target Monster : <mapname used as monster name> (<star>%)
  8418. /// 21 = [Taming Mission] Target Monster : <mapname used as monster name>
  8419. /// 22 = [Collector Rank] Target Item : <monster_id used as item id>
  8420. /// 30 = [Sun, Moon and Stars Angel] Designed places and monsters have been reset.
  8421. /// 40 = Target HP : <monster_id used as HP>
  8422. void clif_starskill(map_session_data* sd, const char* mapname, int32 monster_id, unsigned char star, unsigned char result)
  8423. {
  8424. int32 fd = sd->fd;
  8425. WFIFOHEAD(fd,packet_len(0x20e));
  8426. WFIFOW(fd,0) = 0x20e;
  8427. safestrncpy(WFIFOCP(fd,2), mapname, NAME_LENGTH);
  8428. WFIFOL(fd,26) = monster_id;
  8429. WFIFOB(fd,30) = star;
  8430. WFIFOB(fd,31) = result;
  8431. WFIFOSET(fd,packet_len(0x20e));
  8432. }
  8433. /*==========================================
  8434. * Info about Star Glaldiator save map [Komurka]
  8435. * type: 1: Information, 0: Map registered
  8436. *------------------------------------------*/
  8437. void clif_feel_info(map_session_data* sd, unsigned char feel_level, unsigned char type)
  8438. {
  8439. char mapname[MAP_NAME_LENGTH_EXT];
  8440. mapindex_getmapname_ext(mapindex_id2name(sd->feel_map[feel_level].index), mapname);
  8441. clif_starskill(sd, mapname, 0, feel_level, type ? 1 : 0);
  8442. }
  8443. /*==========================================
  8444. * Info about Star Glaldiator hate mob [Komurka]
  8445. * type: 1: Register mob, 0: Information.
  8446. *------------------------------------------*/
  8447. void clif_hate_info(map_session_data *sd, unsigned char hate_level,int32 class_, unsigned char type)
  8448. {
  8449. if( pcdb_checkid(class_) ) {
  8450. clif_starskill(sd, job_name(class_), class_, hate_level, type ? 10 : 11);
  8451. } else if( mobdb_checkid(class_) ) {
  8452. clif_starskill(sd, mob_db.find(class_)->jname.c_str(), class_, hate_level, type ? 10 : 11);
  8453. } else {
  8454. ShowWarning("clif_hate_info: Received invalid class %d for this packet (char_id=%d, hate_level=%u, type=%u).\n", class_, sd->status.char_id, (uint32)hate_level, (uint32)type);
  8455. }
  8456. }
  8457. /*==========================================
  8458. * Info about TaeKwon Do TK_MISSION mob [Skotlex]
  8459. *------------------------------------------*/
  8460. void clif_mission_info(map_session_data *sd, int32 mob_id, unsigned char progress)
  8461. {
  8462. clif_starskill(sd, mob_db.find(mob_id)->jname.c_str(), mob_id, progress, 20);
  8463. }
  8464. /*==========================================
  8465. * Feel/Hate reset (thanks to Rayce) [Skotlex]
  8466. *------------------------------------------*/
  8467. void clif_feel_hate_reset(map_session_data *sd)
  8468. {
  8469. clif_starskill(sd, "", 0, 0, 30);
  8470. }
  8471. /// Send out reply to configuration change
  8472. /// 02d9 <type>.L <value>.L (ZC_CONFIG)
  8473. /// type: see enum e_config_type
  8474. /// value:
  8475. /// false = disabled
  8476. /// true = enabled
  8477. void clif_configuration( map_session_data* sd, enum e_config_type type, bool enabled ){
  8478. int32 fd;
  8479. nullpo_retv(sd);
  8480. fd = sd->fd;
  8481. WFIFOHEAD(fd, packet_len(0x2d9));
  8482. WFIFOW(fd, 0) = 0x2d9;
  8483. WFIFOL(fd, 2) = type;
  8484. WFIFOL(fd, 6) = enabled;
  8485. WFIFOSET(fd, packet_len(0x2d9));
  8486. }
  8487. /// The player's 'view equip' state, sent during login (ZC_CONFIG_NOTIFY).
  8488. /// 02da <open equip window>.B
  8489. /// open equip window:
  8490. /// 0 = disabled
  8491. /// 1 = enabled
  8492. void clif_equipcheckbox(map_session_data* sd)
  8493. {
  8494. int32 fd;
  8495. nullpo_retv(sd);
  8496. fd = sd->fd;
  8497. WFIFOHEAD(fd, packet_len(0x2da));
  8498. WFIFOW(fd, 0) = 0x2da;
  8499. WFIFOB(fd, 2) = (sd->status.show_equip ? 1 : 0);
  8500. WFIFOSET(fd, packet_len(0x2da));
  8501. }
  8502. /// Sends info about a player's equipped items.
  8503. /// 02d7 <packet len>.W <name>.24B <class>.W <hairstyle>.W <up-viewid>.W <mid-viewid>.W <low-viewid>.W <haircolor>.W <cloth-dye>.W <gender>.B {equip item}.26B* (ZC_EQUIPWIN_MICROSCOPE)
  8504. /// 02d7 <packet len>.W <name>.24B <class>.W <hairstyle>.W <bottom-viewid>.W <mid-viewid>.W <up-viewid>.W <haircolor>.W <cloth-dye>.W <gender>.B {equip item}.28B* (ZC_EQUIPWIN_MICROSCOPE, PACKETVER >= 20100629)
  8505. /// 0859 <packet len>.W <name>.24B <class>.W <hairstyle>.W <bottom-viewid>.W <mid-viewid>.W <up-viewid>.W <haircolor>.W <cloth-dye>.W <gender>.B {equip item}.28B* (ZC_EQUIPWIN_MICROSCOPE2, PACKETVER >= 20101124)
  8506. /// 0859 <packet len>.W <name>.24B <class>.W <hairstyle>.W <bottom-viewid>.W <mid-viewid>.W <up-viewid>.W <robe>.W <haircolor>.W <cloth-dye>.W <gender>.B {equip item}.28B* (ZC_EQUIPWIN_MICROSCOPE2, PACKETVER >= 20110111)
  8507. /// 0997 <packet len>.W <name>.24B <class>.W <hairstyle>.W <bottom-viewid>.W <mid-viewid>.W <up-viewid>.W <robe>.W <haircolor>.W <cloth-dye>.W <gender>.B {equip item}.31B* (ZC_EQUIPWIN_MICROSCOPE_V5, PACKETVER >= 20120925)
  8508. /// 0a2d <packet len>.W <name>.24B <class>.W <hairstyle>.W <bottom-viewid>.W <mid-viewid>.W <up-viewid>.W <robe>.W <haircolor>.W <cloth-dye>.W <gender>.B {equip item}.57B* (ZC_EQUIPWIN_MICROSCOPE_V6, PACKETVER >= 20150225)
  8509. void clif_viewequip_ack( map_session_data& sd, map_session_data& tsd ){
  8510. #if PACKETVER_AD_NUM >= 20071211 || PACKETVER_SAK_NUM >= 20071127 || PACKETVER_MAIN_NUM >= 20071211 || defined(PACKETVER_RE)
  8511. PACKET_ZC_EQUIPWIN_MICROSCOPE* p = reinterpret_cast<PACKET_ZC_EQUIPWIN_MICROSCOPE*>( packet_buffer );
  8512. p->PacketType = HEADER_ZC_EQUIPWIN_MICROSCOPE;
  8513. p->PacketLength = sizeof( *p );
  8514. safestrncpy( p->characterName, tsd.status.name, NAME_LENGTH );
  8515. p->job = tsd.status.class_;
  8516. p->head = tsd.vd.hair_style;
  8517. p->accessory = tsd.vd.head_bottom;
  8518. p->accessory2 = tsd.vd.head_mid;
  8519. p->accessory3 = tsd.vd.head_top;
  8520. #if PACKETVER >= 20110111
  8521. p->robe = tsd.vd.robe;
  8522. #endif
  8523. p->headpalette = tsd.vd.hair_color;
  8524. p->bodypalette = tsd.vd.cloth_color;
  8525. #if PACKETVER_MAIN_NUM >= 20180801 || PACKETVER_RE_NUM >= 20180801 || PACKETVER_ZERO_NUM >= 20180808
  8526. p->body2 = tsd.vd.body_style;
  8527. #endif
  8528. p->sex = tsd.vd.sex;
  8529. for( int32 i = 0, equip = 0; i < EQI_MAX; i++ ){
  8530. int32 k = tsd.equip_index[i];
  8531. if( k >= 0 ){
  8532. if( tsd.inventory.u.items_inventory[k].nameid == 0 || tsd.inventory_data[k] == nullptr ){ // Item doesn't exist
  8533. continue;
  8534. }
  8535. if( !tsd.inventory.u.items_inventory[k].equip ){
  8536. continue;
  8537. }
  8538. if( !itemdb_isequip2( tsd.inventory_data[k] ) ){ // Is not equippable
  8539. continue;
  8540. }
  8541. clif_item_equip( client_index( k ), &p->list[equip], &tsd.inventory.u.items_inventory[k], tsd.inventory_data[k], pc_equippoint( &tsd, k ) );
  8542. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->list[0] ) );
  8543. equip++;
  8544. }
  8545. }
  8546. clif_send( p, p->PacketLength, &sd.bl, SELF );
  8547. #endif
  8548. }
  8549. /// Display msgstringtable.txt string (ZC_MSG).
  8550. /// 0291 <message>.W
  8551. void clif_msg(map_session_data* sd, uint16 id)
  8552. {
  8553. int32 fd;
  8554. nullpo_retv(sd);
  8555. fd = sd->fd;
  8556. WFIFOHEAD(fd, packet_len(0x291));
  8557. WFIFOW(fd, 0) = 0x291;
  8558. WFIFOW(fd, 2) = id; // zero-based msgstringtable.txt index
  8559. WFIFOSET(fd, packet_len(0x291));
  8560. }
  8561. /// Display msgstringtable.txt string and fill in a valid for %d format (ZC_MSG_VALUE).
  8562. /// 0x7e2 <message>.W <value>.L
  8563. void clif_msg_value(map_session_data* sd, uint16 id, int32 value)
  8564. {
  8565. int32 fd = sd->fd;
  8566. WFIFOHEAD(fd, packet_len(0x7e2));
  8567. WFIFOW(fd,0) = 0x7e2;
  8568. WFIFOW(fd,2) = id;
  8569. WFIFOL(fd,4) = value;
  8570. WFIFOSET(fd, packet_len(0x7e2));
  8571. }
  8572. /// Displays msgstringtable.txt string, prefixed with a skill name. (ZC_MSG_SKILL).
  8573. /// 07e6 <skill id>.W <msg id>.L
  8574. ///
  8575. /// NOTE: Message has following format and is printed in color 0xCDCDFF (purple):
  8576. /// "[SkillName] Message"
  8577. void clif_msg_skill(map_session_data* sd, uint16 skill_id, int32 msg_id)
  8578. {
  8579. int32 fd = sd->fd;
  8580. WFIFOHEAD(fd, packet_len(0x7e6));
  8581. WFIFOW(fd,0) = 0x7e6;
  8582. WFIFOW(fd,2) = skill_id;
  8583. WFIFOL(fd,4) = msg_id;
  8584. WFIFOSET(fd, packet_len(0x7e6));
  8585. }
  8586. /// Displays msgstringtable.txt string in a color. (ZC_MSG_COLOR).
  8587. /// 09cd <msg id>.W <color>.L
  8588. void clif_msg_color( map_session_data *sd, uint16 msg_id, uint32 color ){
  8589. nullpo_retv(sd);
  8590. int32 fd = sd->fd;
  8591. WFIFOHEAD(fd, packet_len(0x9cd));
  8592. WFIFOW(fd, 0) = 0x9cd;
  8593. WFIFOW(fd, 2) = msg_id;
  8594. WFIFOL(fd, 4) = color;
  8595. WFIFOSET(fd, packet_len(0x9cd));
  8596. }
  8597. /// Validates one global/guild/party/whisper message packet and tries to recognize its components.
  8598. /// Returns true if the packet was parsed successfully.
  8599. /// Formats: false - <packet id>.w <packet len>.w (<name> : <message>).?B 00
  8600. /// true - <packet id>.w <packet len>.w <name>.24B <message>.?B 00
  8601. static bool clif_process_message(map_session_data* sd, bool whisperFormat, char* out_name, char* out_message, char* out_full_message ){
  8602. const char* seperator = " : ";
  8603. int32 fd;
  8604. struct s_packet_db* info;
  8605. uint16 packetLength, inputLength;
  8606. const char *input, *name, *message;
  8607. size_t nameLength, messageLength;
  8608. fd = sd->fd;
  8609. info = &packet_db[RFIFOW(fd,0)];
  8610. packetLength = RFIFOW(fd,info->pos[0]);
  8611. input = RFIFOCP(fd,info->pos[1]);
  8612. // basic structure check for the 4-byte header
  8613. if( packetLength < 4 ){
  8614. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no message data)!\n", sd->status.name);
  8615. return false;
  8616. }else{
  8617. inputLength = packetLength - 4;
  8618. }
  8619. // process <name> part of the packet
  8620. if( whisperFormat ) {
  8621. // name has fixed width
  8622. if( inputLength < NAME_LENGTH + 1 ) {
  8623. ShowWarning("clif_process_message: Received malformed packet from player '%s' (packet length is incorrect)!\n", sd->status.name);
  8624. return false;
  8625. }
  8626. name = input;
  8627. // validate name
  8628. nameLength = strnlen( name, NAME_LENGTH - 1 );
  8629. // only restriction is that the name must be zero-terminated
  8630. if( name[nameLength] != '\0' ) {
  8631. ShowWarning("clif_process_message: Player '%s' sent an unterminated name!\n", sd->status.name);
  8632. return false;
  8633. }
  8634. message = input + NAME_LENGTH;
  8635. messageLength = inputLength - NAME_LENGTH;
  8636. }else{
  8637. // name and message are separated by ' : '
  8638. size_t seperatorLength = strnlen( seperator, NAME_LENGTH );
  8639. nameLength = strnlen( sd->status.name, NAME_LENGTH - 1 ); // name length (w/o zero byte)
  8640. // check if there's enough data provided
  8641. if( inputLength < nameLength + seperatorLength + 1 ){
  8642. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no username data)!\n", sd->status.name);
  8643. return false;
  8644. }
  8645. name = input;
  8646. // validate name
  8647. if( strncmp( name, sd->status.name, nameLength ) || // the text must start with the speaker's name
  8648. strncmp( name + nameLength, seperator, seperatorLength ) ) // followed by the seperator
  8649. {
  8650. //Hacked message, or infamous "client desynch" issue where they pick one char while loading another.
  8651. ShowWarning("clif_process_message: Player '%s' sent a message using an incorrect name! Forcing a relog...\n", sd->status.name);
  8652. set_eof(sd->fd); // Just kick them out to correct it.
  8653. return false;
  8654. }
  8655. message = input + nameLength + seperatorLength;
  8656. messageLength = inputLength - nameLength - seperatorLength;
  8657. }
  8658. #if PACKETVER < 20151001
  8659. // the declared length must match real length
  8660. if( messageLength != strnlen(message, messageLength)+1 ) {
  8661. ShowWarning("clif_process_message: Received malformed packet from player '%s' (length is incorrect)!\n", sd->status.name);
  8662. return false;
  8663. }
  8664. // verify <message> part of the packet
  8665. if( message[messageLength-1] != '\0' ) { // message must be zero-terminated
  8666. ShowWarning("clif_process_message: Player '%s' sent an unterminated message string!\n", sd->status.name);
  8667. return false;
  8668. }
  8669. #else
  8670. // No zero termination anymore
  8671. messageLength += 1;
  8672. #endif
  8673. // messages mustn't be too long
  8674. if( messageLength > CHAT_SIZE_MAX-1 ) {
  8675. // Normally you can only enter CHATBOX_SIZE-1 letters into the chat box, but Frost Joke / Dazzler's text can be longer.
  8676. // Also, the physical size of strings that use multibyte encoding can go multiple times over the chatbox capacity.
  8677. // Neither the official client nor server place any restriction on the length of the data in the packet,
  8678. // but we'll only allow reasonably long strings here. This also makes sure that they fit into the `chatlog` table.
  8679. ShowWarning("clif_process_message: Player '%s' sent a message too long ('%.*s')!\n", sd->status.name, CHAT_SIZE_MAX-1, message);
  8680. return false;
  8681. }
  8682. // If it is not a whisper message, set the name to the fakename of the player
  8683. if( whisperFormat == false && sd->fakename[0] != '\0' ){
  8684. strcpy( out_name, sd->fakename );
  8685. }else{
  8686. safestrncpy( out_name, name, nameLength + 1 );
  8687. }
  8688. safestrncpy( out_message, message, messageLength );
  8689. if( whisperFormat ){
  8690. sprintf( out_full_message, "%-24s%s", out_name, out_message );
  8691. out_full_message[nameLength] = '\0';
  8692. }else{
  8693. sprintf( out_full_message, "%s%s%s", out_name, seperator, out_message );
  8694. }
  8695. if( is_atcommand( fd, sd, out_message, 1 ) )
  8696. return false;
  8697. if (sd->sc.cant.chat || (sd->state.block_action & PCBLOCK_CHAT))
  8698. return false; //no "chatting" while muted.
  8699. if( battle_config.min_chat_delay ) { //[Skotlex]
  8700. if (DIFF_TICK(sd->cantalk_tick, gettick()) > 0)
  8701. return false;
  8702. sd->cantalk_tick = gettick() + battle_config.min_chat_delay;
  8703. }
  8704. if (battle_config.idletime_option&IDLE_CHAT)
  8705. sd->idletime = last_tick;
  8706. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_CHAT)
  8707. sd->idletime_hom = last_tick;
  8708. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_CHAT)
  8709. sd->idletime_mer = last_tick;
  8710. //achievement_update_objective(sd, AG_CHATTING, 1, 1); // !TODO: Confirm how this achievement is triggered
  8711. return true;
  8712. }
  8713. /**
  8714. * Displays a message if the player enters a PK Zone (during pk_mode)
  8715. * @param sd: Player data
  8716. */
  8717. inline void clif_pk_mode_message(map_session_data * sd)
  8718. {
  8719. if (battle_config.pk_mode && battle_config.pk_mode_mes &&
  8720. sd && map_getmapflag(sd->bl.m, MF_PVP)) {
  8721. if( (int32)sd->status.base_level < battle_config.pk_min_level ) {
  8722. char output[CHAT_SIZE_MAX];
  8723. // 1504: You've entered a PK Zone (safe until level %d).
  8724. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(sd,1504),
  8725. battle_config.pk_min_level);
  8726. clif_showscript(&sd->bl, output, SELF);
  8727. } else {
  8728. // 1503: You've entered a PK Zone.
  8729. clif_showscript(&sd->bl, msg_txt(sd,1503), SELF);
  8730. }
  8731. }
  8732. return;
  8733. }
  8734. // ---------------------
  8735. // clif_parse_wanttoconnect
  8736. // ---------------------
  8737. // Parses a WantToConnection packet to try to identify which is the packet version used. [Skotlex]
  8738. // error codes:
  8739. // 0 - Success
  8740. // 1 - Unknown packet_ver
  8741. // 2 - Invalid account_id
  8742. // 3 - Invalid char_id
  8743. // 4 - Invalid login_id1 (reserved)
  8744. // 5 - Invalid client_tick (reserved)
  8745. // 6 - Invalid sex
  8746. // Only the first 'invalid' error that appears is used.
  8747. static int32 clif_parse_WantToConnection_sub(int32 fd)
  8748. {
  8749. int32 value; //Value is used to temporarily store account/char_id/sex
  8750. //By default, start searching on the default one.
  8751. uint16 cmd = RFIFOW(fd, 0);
  8752. int16 packet_len;
  8753. packet_len = static_cast<decltype(packet_len)>( RFIFOREST( fd ) );
  8754. // FIXME: If the packet is not received at once, this will FAIL.
  8755. // Figure out, when it happens, that only part of the packet is
  8756. // received, or fix the function to be able to deal with that
  8757. // case.
  8758. if( packet_len != packet_db[cmd].len )
  8759. return 1; /* wrong length */
  8760. else if( (value=(int32)RFIFOL(fd, packet_db[cmd].pos[0])) < START_ACCOUNT_NUM || value > END_ACCOUNT_NUM )
  8761. return 2; /* invalid account_id */
  8762. else if( (value=(int32)RFIFOL(fd, packet_db[cmd].pos[1])) <= 0 )
  8763. return 3; /* invalid char_id */
  8764. /* RFIFOL(fd, packet_db[cmd].pos[2]) - don't care about login_id1 */
  8765. /* RFIFOL(fd, packet_db[cmd].pos[3]) - don't care about client_tick */
  8766. else if( (value=(int32)RFIFOB(fd, packet_db[cmd].pos[4])) != 0 && value != 1 )
  8767. return 6; /* invalid sex */
  8768. else
  8769. return 0;
  8770. }
  8771. /// Request to connect to map-server.
  8772. /// 0072 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER)
  8773. /// 0436 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER2)
  8774. /// There are various variants of this packet, some of them have padding between fields.
  8775. void clif_parse_WantToConnection(int32 fd, map_session_data* sd)
  8776. {
  8777. struct block_list* bl;
  8778. struct auth_node* node;
  8779. int32 cmd, account_id, char_id, login_id1, sex, err;
  8780. t_tick client_tick; //The client tick is a tick, therefore it needs be unsigned. [Skotlex]
  8781. if (sd) {
  8782. ShowError("clif_parse_WantToConnection : invalid request (character already logged in)\n");
  8783. return;
  8784. }
  8785. cmd = RFIFOW(fd, 0);
  8786. // TODO: shuffle packet
  8787. account_id = RFIFOL(fd, packet_db[cmd].pos[0]);
  8788. char_id = RFIFOL(fd, packet_db[cmd].pos[1]);
  8789. login_id1 = RFIFOL(fd, packet_db[cmd].pos[2]);
  8790. client_tick = RFIFOL(fd, packet_db[cmd].pos[3]);
  8791. sex = RFIFOB(fd, packet_db[cmd].pos[4]);
  8792. err = clif_parse_WantToConnection_sub(fd);
  8793. if( err ){ // connection rejected
  8794. ShowInfo("clif_parse: Disconnecting session #%d with unknown connect packet 0x%04x(length:%d)%s\n", fd, cmd, RFIFOREST(fd), (
  8795. err == 1 ? "." :
  8796. err == 2 ? ", possibly for having an invalid account_id." :
  8797. err == 3 ? ", possibly for having an invalid char_id." :
  8798. /* Uncomment when checks are added in clif_parse_WantToConnection_sub. [FlavioJS]
  8799. err == 4 ? ", possibly for having an invalid login_id1." :
  8800. err == 5 ? ", possibly for having an invalid client_tick." :
  8801. */
  8802. err == 6 ? ", possibly for having an invalid sex." :
  8803. ". ERROR invalid error code"));
  8804. WFIFOHEAD(fd,packet_len(0x6a));
  8805. WFIFOW(fd,0) = 0x6a;
  8806. WFIFOB(fd,2) = err;
  8807. WFIFOSET(fd,packet_len(0x6a));
  8808. #ifdef DUMP_INVALID_PACKET
  8809. ShowDump(RFIFOP(fd, 0), RFIFOREST(fd));
  8810. #endif
  8811. RFIFOSKIP(fd, RFIFOREST(fd));
  8812. set_eof(fd);
  8813. return;
  8814. }
  8815. if( !global_core->is_running() ){ // not allowed
  8816. clif_authfail_fd(fd,1);// server closed
  8817. return;
  8818. }
  8819. //Check for double login.
  8820. bl = map_id2bl(account_id);
  8821. if(bl && bl->type != BL_PC) {
  8822. ShowError("clif_parse_WantToConnection: a non-player object already has id %d, please increase the starting account number\n", account_id);
  8823. WFIFOHEAD(fd,packet_len(0x6a));
  8824. WFIFOW(fd,0) = 0x6a;
  8825. WFIFOB(fd,2) = 3; // Rejected by server
  8826. WFIFOSET(fd,packet_len(0x6a));
  8827. set_eof(fd);
  8828. return;
  8829. }
  8830. if (bl ||
  8831. ((node=chrif_search(account_id)) && //An already existing node is valid only if it is for this login.
  8832. !(node->account_id == account_id && node->char_id == char_id && node->state == ST_LOGIN)))
  8833. {
  8834. clif_authfail_fd(fd, 8); //Still recognizes last connection
  8835. return;
  8836. }
  8837. CREATE(sd, TBL_PC, 1);
  8838. new(sd) map_session_data();
  8839. sd->fd = fd;
  8840. #ifdef PACKET_OBFUSCATION
  8841. sd->cryptKey = (((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF) * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF;
  8842. #endif
  8843. session[fd]->session_data = sd;
  8844. pc_setnewpc(sd, account_id, char_id, login_id1, client_tick, sex, fd);
  8845. #if PACKETVER < 20070521
  8846. WFIFOHEAD(fd,4);
  8847. WFIFOL(fd,0) = sd->bl.id;
  8848. WFIFOSET(fd,4);
  8849. #else
  8850. WFIFOHEAD(fd,packet_len(0x283));
  8851. WFIFOW(fd,0) = 0x283;
  8852. WFIFOL(fd,2) = sd->bl.id;
  8853. WFIFOSET(fd,packet_len(0x283));
  8854. #endif
  8855. chrif_authreq(sd,false);
  8856. }
  8857. /// Notification from the client, that it has finished map loading and is about to display player's character (CZ_NOTIFY_ACTORINIT).
  8858. /// 007d
  8859. void clif_parse_LoadEndAck(int32 fd,map_session_data *sd)
  8860. {
  8861. bool guild_notice = false;
  8862. if(sd->bl.prev != nullptr)
  8863. return;
  8864. // Autotraders should ignore this entirely, clif_parse_LoadEndAck is always invoked manually for them
  8865. if (!sd->state.active || (!sd->state.autotrade && !sd->state.pc_loaded)) { //Character loading is not complete yet!
  8866. //Let pc_reg_received or pc_scdata_received reinvoke this when ready.
  8867. sd->state.connect_new = 0;
  8868. return;
  8869. }
  8870. if (sd->state.rewarp) { //Rewarp player.
  8871. sd->state.rewarp = 0;
  8872. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8873. return;
  8874. }
  8875. sd->state.warping = 0;
  8876. // look
  8877. #if PACKETVER < 4
  8878. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8879. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8880. #else
  8881. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8882. #endif
  8883. pc_set_costume_view(sd);
  8884. if(sd->vd.cloth_color)
  8885. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8886. if(sd->vd.body_style)
  8887. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8888. // item
  8889. clif_inventorylist(sd); // inventory list first, otherwise deleted items in pc_checkitem show up as 'unknown item'
  8890. pc_checkitem(sd);
  8891. clif_equipswitch_list(sd);
  8892. // cart
  8893. if(pc_iscarton(sd)) {
  8894. clif_cartlist(sd);
  8895. clif_updatestatus(*sd,SP_CARTINFO);
  8896. }
  8897. // weight
  8898. clif_updatestatus(*sd,SP_WEIGHT);
  8899. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8900. // guild
  8901. // (needs to go before clif_spawn() to show guild emblems correctly)
  8902. if(sd->status.guild_id)
  8903. guild_send_memberinfoshort(sd,1);
  8904. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8905. if(battle_config.pc_invincible_time > 0) {
  8906. if(mapdata_flag_gvg(mapdata))
  8907. pc_setinvincibletimer(sd,battle_config.pc_invincible_time<<1);
  8908. else
  8909. pc_setinvincibletimer(sd,battle_config.pc_invincible_time);
  8910. }
  8911. if( mapdata->users++ == 0 && battle_config.dynamic_mobs )
  8912. map_spawnmobs(sd->bl.m);
  8913. if( !pc_isinvisible(sd) ) { // increment the number of pvp players on the map
  8914. mapdata->users_pvp++;
  8915. }
  8916. sd->state.debug_remove_map = 0; // temporary state to track double remove_map's [FlavioJS]
  8917. // reset the callshop flag if the player changes map
  8918. sd->state.callshop = 0;
  8919. if(map_addblock(&sd->bl))
  8920. return;
  8921. clif_spawn(&sd->bl);
  8922. // Party
  8923. // (needs to go after clif_spawn() to show hp bars correctly)
  8924. if(sd->status.party_id) {
  8925. party_send_movemap(sd);
  8926. clif_party_hp( *sd ); // Show hp after displacement [LuzZza]
  8927. }
  8928. if( sd->bg_id ) clif_bg_hp(sd); // BattleGround System
  8929. if(!pc_isinvisible(sd) && mapdata->getMapFlag(MF_PVP)) {
  8930. if(!battle_config.pk_mode) { // remove pvp stuff for pk_mode [Valaris]
  8931. if (!mapdata->getMapFlag(MF_PVP_NOCALCRANK))
  8932. sd->pvp_timer = add_timer(gettick()+200, pc_calc_pvprank_timer, sd->bl.id, 0);
  8933. sd->pvp_rank = 0;
  8934. sd->pvp_lastusers = 0;
  8935. sd->pvp_point = 5;
  8936. sd->pvp_won = 0;
  8937. sd->pvp_lost = 0;
  8938. }
  8939. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8940. } else if(sd->duel_group) // set flag, if it's a duel [LuzZza]
  8941. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8942. else if (mapdata->getMapFlag(MF_GVG_DUNGEON))
  8943. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF); //TODO: Figure out the real packet to send here.
  8944. else if( mapdata_flag_gvg(mapdata) )
  8945. clif_map_property(&sd->bl, MAPPROPERTY_AGITZONE, SELF);
  8946. else
  8947. clif_map_property(&sd->bl, MAPPROPERTY_NOTHING, SELF);
  8948. // info about nearby objects
  8949. // must use foreachinarea (CIRCULAR_AREA interferes with foreachinrange)
  8950. map_foreachinallarea(clif_getareachar, sd->bl.m, sd->bl.x-AREA_SIZE, sd->bl.y-AREA_SIZE, sd->bl.x+AREA_SIZE, sd->bl.y+AREA_SIZE, BL_ALL, sd);
  8951. // pet
  8952. if( sd->pd ) {
  8953. if( battle_config.pet_no_gvg && mapdata_flag_gvg(mapdata) ) { //Return the pet to egg. [Skotlex]
  8954. clif_displaymessage(sd->fd, msg_txt(sd,666));
  8955. pet_return_egg( sd, sd->pd );
  8956. } else {
  8957. if(map_addblock(&sd->pd->bl))
  8958. return;
  8959. clif_spawn(&sd->pd->bl);
  8960. clif_send_petdata( sd, *sd->pd, CHANGESTATEPET_INIT );
  8961. clif_send_petstatus( *sd, *sd->pd );
  8962. // skill_unit_move(&sd->pd->bl,gettick(),1);
  8963. }
  8964. }
  8965. //homunculus [blackhole89]
  8966. if( hom_is_active(sd->hd) ) {
  8967. if(map_addblock(&sd->hd->bl))
  8968. return;
  8969. clif_spawn(&sd->hd->bl);
  8970. clif_send_homdata( *sd->hd, SP_ACK );
  8971. // For some reason, official servers send the homunculus info twice, then update the HP/SP again.
  8972. clif_hominfo(sd, sd->hd, 1);
  8973. clif_hominfo(sd, sd->hd, 0);
  8974. clif_homunculus_updatestatus(*sd, SP_HP);
  8975. clif_homunculus_updatestatus(*sd, SP_SP);
  8976. clif_homskillinfoblock( *sd->hd );
  8977. status_calc_bl(&sd->hd->bl, { SCB_SPEED });
  8978. if( !(battle_config.hom_setting&HOMSET_NO_INSTANT_LAND_SKILL) )
  8979. skill_unit_move(&sd->hd->bl,gettick(),1); // apply land skills immediately
  8980. }
  8981. if( sd->md ) {
  8982. if(map_addblock(&sd->md->bl))
  8983. return;
  8984. clif_spawn(&sd->md->bl);
  8985. clif_mercenary_info(sd);
  8986. clif_mercenary_skillblock(sd);
  8987. status_calc_bl(&sd->md->bl, { SCB_SPEED }); // Mercenary mimic their master's speed on each map change
  8988. }
  8989. if( sd->ed ) {
  8990. if (map_addblock(&sd->ed->bl))
  8991. return;
  8992. clif_spawn(&sd->ed->bl);
  8993. clif_elemental_info(sd);
  8994. clif_elemental_updatestatus(*sd, SP_HP);
  8995. clif_hpmeter_single( *sd, sd->ed->bl.id, sd->ed->battle_status.hp, sd->ed->battle_status.max_hp );
  8996. clif_elemental_updatestatus(*sd, SP_SP);
  8997. status_calc_bl(&sd->ed->bl, { SCB_SPEED }); //Elemental mimic their master's speed on each map change
  8998. }
  8999. if(sd->state.connect_new) {
  9000. int32 lv;
  9001. guild_notice = true;
  9002. clif_skillinfoblock(sd);
  9003. clif_hotkeys_send(sd,0);
  9004. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9005. clif_hotkeys_send(sd,1);
  9006. #endif
  9007. clif_updatestatus(*sd,SP_BASEEXP);
  9008. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  9009. clif_updatestatus(*sd,SP_JOBEXP);
  9010. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  9011. clif_updatestatus(*sd,SP_SKILLPOINT);
  9012. clif_initialstatus( *sd );
  9013. if (sd->sc.option&OPTION_FALCON)
  9014. clif_status_load(&sd->bl, EFST_FALCON, 1);
  9015. else if (sd->sc.option&(OPTION_RIDING|OPTION_DRAGON))
  9016. clif_status_load(&sd->bl, EFST_RIDING, 1);
  9017. else if (sd->sc.option&OPTION_WUGRIDER)
  9018. clif_status_load(&sd->bl, EFST_WUGRIDER, 1);
  9019. else if (sd->sc.getSCE(SC_ALL_RIDING))
  9020. clif_status_load(&sd->bl, EFST_ALL_RIDING, 1);
  9021. if(sd->status.manner < 0)
  9022. sc_start(&sd->bl,&sd->bl,SC_NOCHAT,100,0,0);
  9023. //Auron reported that This skill only triggers when you logon on the map o.O [Skotlex]
  9024. if ((lv = pc_checkskill(sd,SG_KNOWLEDGE)) > 0) {
  9025. if(sd->bl.m == sd->feel_map[0].m
  9026. || sd->bl.m == sd->feel_map[1].m
  9027. || sd->bl.m == sd->feel_map[2].m)
  9028. sc_start(&sd->bl,&sd->bl, SC_KNOWLEDGE, 100, lv, skill_get_time(SG_KNOWLEDGE, lv));
  9029. }
  9030. if(sd->pd && sd->pd->pet.intimate > 900)
  9031. clif_pet_emotion( *sd->pd, (sd->pd->pet.class_ - 100)*100 + 50 + pet_hungry_val(sd->pd) );
  9032. if(hom_is_active(sd->hd))
  9033. hom_init_timers(sd->hd);
  9034. if (night_flag && mapdata->getMapFlag(MF_NIGHTENABLED)) {
  9035. sd->state.night = 1;
  9036. clif_status_load(&sd->bl, EFST_SKE, 1);
  9037. }
  9038. // Notify everyone that this char logged in [Skotlex].
  9039. map_foreachpc(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 1);
  9040. if (!sd->state.autotrade) { // Don't trigger NPC event or opening vending/buyingstore will be failed
  9041. npc_script_event( *sd, NPCE_LOGIN );
  9042. }
  9043. // Set facing direction before check below to update client
  9044. if (battle_config.spawn_direction)
  9045. unit_setdir(&sd->bl, sd->status.body_direction, false);
  9046. } else {
  9047. //For some reason the client "loses" these on warp/map-change.
  9048. clif_updatestatus(*sd,SP_STR);
  9049. clif_updatestatus(*sd,SP_AGI);
  9050. clif_updatestatus(*sd,SP_VIT);
  9051. clif_updatestatus(*sd,SP_INT);
  9052. clif_updatestatus(*sd,SP_DEX);
  9053. clif_updatestatus(*sd,SP_LUK);
  9054. #ifdef RENEWAL
  9055. clif_updatestatus(*sd,SP_POW);
  9056. clif_updatestatus(*sd,SP_STA);
  9057. clif_updatestatus(*sd,SP_WIS);
  9058. clif_updatestatus(*sd,SP_SPL);
  9059. clif_updatestatus(*sd,SP_CON);
  9060. clif_updatestatus(*sd,SP_CRT);
  9061. #endif
  9062. // abort currently running script
  9063. sd->state.using_fake_npc = 0;
  9064. sd->state.menu_or_input = 0;
  9065. sd->npc_menu = 0;
  9066. if(sd->npc_id)
  9067. npc_event_dequeue(sd);
  9068. }
  9069. if( sd->state.changemap ) {// restore information that gets lost on map-change
  9070. clif_partyinvitationstate( *sd );
  9071. #if PACKETVER >= 20070918
  9072. clif_equipcheckbox(sd);
  9073. #endif
  9074. clif_pet_autofeed_status(sd,false);
  9075. clif_configuration( sd, CONFIG_CALL, sd->status.disable_call );
  9076. #if PACKETVER >= 20170920
  9077. if( battle_config.homunculus_autofeed_always ){
  9078. // Always send ON or OFF
  9079. if( sd->hd && battle_config.feature_homunculus_autofeed ){
  9080. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, sd->hd->homunculus.autofeed );
  9081. }else{
  9082. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, false );
  9083. }
  9084. }else{
  9085. // Only send when enabled
  9086. if( sd->hd && battle_config.feature_homunculus_autofeed && sd->hd->homunculus.autofeed ){
  9087. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, true );
  9088. }
  9089. }
  9090. #endif
  9091. clif_reputation_list( *sd );
  9092. if (sd->guild && battle_config.guild_notice_changemap == 1){
  9093. // Displays after VIP
  9094. clif_guild_notice( *sd );
  9095. guild_notice = false; // Do not display it twice
  9096. }
  9097. if (battle_config.bg_flee_penalty != 100 || battle_config.gvg_flee_penalty != 100) {
  9098. struct map_data *pmap = map_getmapdata(sd->state.pmap);
  9099. if ((pmap != nullptr && (mapdata_flag_gvg(pmap) || pmap->getMapFlag(MF_BATTLEGROUND))) || (mapdata != nullptr && (mapdata_flag_gvg(mapdata) || mapdata->getMapFlag(MF_BATTLEGROUND))))
  9100. status_calc_bl(&sd->bl, { SCB_FLEE }); //Refresh flee penalty
  9101. }
  9102. if( night_flag && mapdata->getMapFlag(MF_NIGHTENABLED) )
  9103. { //Display night.
  9104. if( !sd->state.night )
  9105. {
  9106. sd->state.night = 1;
  9107. clif_status_load(&sd->bl, EFST_SKE, 1);
  9108. }
  9109. }
  9110. else if( sd->state.night )
  9111. { //Clear night display.
  9112. sd->state.night = 0;
  9113. clif_status_load(&sd->bl, EFST_SKE, 0);
  9114. }
  9115. if( mapdata->getMapFlag(MF_BATTLEGROUND) )
  9116. {
  9117. clif_map_type( *sd, MAPTYPE_BATTLEFIELD ); // Battleground Mode
  9118. if( map_getmapflag(sd->bl.m, MF_BATTLEGROUND) == 2 )
  9119. clif_bg_updatescore_single(sd);
  9120. }
  9121. if( mapdata->getMapFlag(MF_ALLOWKS) && !mapdata_flag_ks(mapdata) )
  9122. {
  9123. char output[128];
  9124. sprintf(output, "[ Kill Steal Protection Disable. KS is allowed in this map ]");
  9125. clif_broadcast(&sd->bl, output, strlen(output) + 1, BC_BLUE, SELF);
  9126. }
  9127. if( pc_has_permission(sd,PC_PERM_VIEW_HPMETER) ) {
  9128. mapdata->hpmeter_visible++;
  9129. sd->state.hpmeter_visible = 1;
  9130. }
  9131. status_change_clear_onChangeMap(&sd->bl, &sd->sc);
  9132. map_iwall_get(sd); // Updates Walls Info on this Map to Client
  9133. status_calc_pc(sd, sd->state.autotrade ? SCO_FIRST : SCO_NONE); // Some conditions are map-dependent so we must recalculate
  9134. #ifdef VIP_ENABLE
  9135. if (!sd->state.connect_new &&
  9136. !sd->vip.disableshowrate &&
  9137. sd->state.pmap != sd->bl.m &&
  9138. map_getmapflag(sd->state.pmap, MF_BEXP) != mapdata->getMapFlag(MF_BEXP)
  9139. )
  9140. {
  9141. clif_display_pinfo( *sd );
  9142. }
  9143. #endif
  9144. // Instances do not need their own channels
  9145. if( channel_config.map_tmpl.name[0] && (channel_config.map_tmpl.opt&CHAN_OPT_AUTOJOIN) && !mapdata->instance_id && !mapdata->getMapFlag(MF_NOMAPCHANNELAUTOJOIN) )
  9146. channel_mjoin(sd); //join new map
  9147. clif_pk_mode_message(sd);
  9148. }
  9149. if( sd->guild && ( battle_config.guild_notice_changemap == 2 || guild_notice ) ){
  9150. // Displays at end
  9151. clif_guild_notice( *sd );
  9152. }
  9153. mail_clear(sd);
  9154. /* Guild Aura Init */
  9155. if( sd->guild && sd->state.gmaster_flag ) {
  9156. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  9157. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  9158. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  9159. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  9160. }
  9161. if( sd->state.vending ) { /* show we have a vending */
  9162. clif_openvending( *sd );
  9163. clif_showvendingboard( *sd );
  9164. }
  9165. // Don't trigger NPC event or opening vending/buyingstore will be failed
  9166. if(!sd->state.autotrade && mapdata->getMapFlag(MF_LOADEVENT)) // Lance
  9167. npc_script_event( *sd, NPCE_LOADMAP );
  9168. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  9169. clif_status_load(&sd->bl, EFST_DEVIL1, 1); //blindness [Komurka]
  9170. if (sd->sc.opt2) //Client loses these on warp.
  9171. clif_changeoption(&sd->bl);
  9172. if ((sd->sc.getSCE(SC_MONSTER_TRANSFORM) || sd->sc.getSCE(SC_ACTIVE_MONSTER_TRANSFORM)) && battle_config.mon_trans_disable_in_gvg && mapdata_flag_gvg2(mapdata)) {
  9173. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM);
  9174. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM);
  9175. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  9176. }
  9177. clif_weather_check(sd);
  9178. // For automatic triggering of NPCs after map loading (so you don't need to walk 1 step first)
  9179. if (map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNPC))
  9180. npc_touch_area_allnpc(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  9181. else
  9182. sd->areanpc.clear();
  9183. /* it broke at some point (e.g. during a crash), so we make it visibly dead again. */
  9184. if( !sd->status.hp && !pc_isdead(sd) && status_isdead(sd->bl) )
  9185. pc_setdead(sd);
  9186. // If player is dead, and is spawned (such as @refresh) send death packet. [Valaris]
  9187. if(pc_isdead(sd))
  9188. clif_clearunit_area( sd->bl, CLR_DEAD );
  9189. else {
  9190. skill_usave_trigger(sd);
  9191. if (battle_config.spawn_direction)
  9192. clif_changed_dir(sd->bl, SELF);
  9193. }
  9194. // Trigger skill effects if you appear standing on them
  9195. if(!battle_config.pc_invincible_time)
  9196. skill_unit_move(&sd->bl,gettick(),1);
  9197. pc_show_questinfo_reinit(sd);
  9198. pc_show_questinfo(sd);
  9199. #if PACKETVER >= 20150513
  9200. if( sd->mail.inbox.unread ){
  9201. clif_Mail_new(sd, 0, nullptr, nullptr);
  9202. }
  9203. #endif
  9204. sd->state.connect_new = 0;
  9205. sd->state.changemap = false;
  9206. }
  9207. /// Server's tick (ZC_NOTIFY_TIME).
  9208. /// 007f <time>.L
  9209. void clif_notify_time(map_session_data* sd, t_tick time)
  9210. {
  9211. int32 fd = sd->fd;
  9212. WFIFOHEAD(fd,packet_len(0x7f));
  9213. WFIFOW(fd,0) = 0x7f;
  9214. WFIFOL(fd,2) = client_tick(time);
  9215. WFIFOSET(fd,packet_len(0x7f));
  9216. }
  9217. /// Request for server's tick.
  9218. /// 007e <client tick>.L (CZ_REQUEST_TIME)
  9219. /// 0360 <client tick>.L (CZ_REQUEST_TIME2)
  9220. /// There are various variants of this packet, some of them have padding between fields.
  9221. void clif_parse_TickSend(int32 fd, map_session_data *sd)
  9222. {
  9223. // TODO: shuffle packet
  9224. sd->client_tick = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9225. clif_notify_time(sd, gettick());
  9226. }
  9227. /// Sends hotkey bar.
  9228. /// 02b9 { <is skill>.B <id>.L <count>.W }*27 (ZC_SHORTCUT_KEY_LIST)
  9229. /// 07d9 { <is skill>.B <id>.L <count>.W }*36 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090603)
  9230. /// 07d9 { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090617)
  9231. /// 0a00 <rotate>.B { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V3, PACKETVER >= 20141022)
  9232. void clif_hotkeys_send( map_session_data *sd, int32 tab ){
  9233. #if defined(HOTKEY_SAVING) && ( PACKETVER_MAIN_NUM >= 20070711 || PACKETVER_RE_NUM >= 20080827 || PACKETVER_AD_NUM >= 20070711 || PACKETVER_SAK_NUM >= 20070628 )
  9234. nullpo_retv( sd );
  9235. PACKET_ZC_SHORTCUT_KEY_LIST p = {};
  9236. p.packetType = HEADER_ZC_SHORTCUT_KEY_LIST;
  9237. #if PACKETVER_MAIN_NUM >= 20141022 || PACKETVER_RE_NUM >= 20141015 || defined(PACKETVER_ZERO)
  9238. if( tab == 0 ){
  9239. p.rotate = sd->status.hotkey_rowshift;
  9240. }else{
  9241. p.rotate = sd->status.hotkey_rowshift2;
  9242. }
  9243. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9244. p.tab = tab;
  9245. #endif
  9246. #endif
  9247. for( int32 i = 0, offset = tab * MAX_HOTKEYS; i < MAX_HOTKEYS_PACKET; i++ ){
  9248. p.hotkey[i].isSkill = sd->status.hotkeys[i + offset].type;
  9249. p.hotkey[i].id = sd->status.hotkeys[i + offset].id;
  9250. p.hotkey[i].count = sd->status.hotkeys[i + offset].lv;
  9251. }
  9252. clif_send( &p, sizeof(PACKET_ZC_SHORTCUT_KEY_LIST), &sd->bl, SELF );
  9253. #endif
  9254. }
  9255. /// Request to update a position on the hotkey row bar
  9256. void clif_parse_HotkeyRowShift( int32 fd, map_session_data *sd ){
  9257. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9258. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE2* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE2*>( RFIFOP( fd, 0 ) );
  9259. if( p->tab == 0 ){
  9260. sd->status.hotkey_rowshift = p->rowshift;
  9261. }else{
  9262. sd->status.hotkey_rowshift2 = p->rowshift;
  9263. }
  9264. #elif PACKETVER_MAIN_NUM >= 20140129 || PACKETVER_RE_NUM >= 20140129 || defined(PACKETVER_ZERO)
  9265. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE1* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE1*>( RFIFOP( fd, 0 ) );
  9266. sd->status.hotkey_rowshift = p->rowshift;
  9267. #endif
  9268. }
  9269. /// Request to update a position on the hotkey bar.
  9270. /// 02ba <index>.W <is skill>.B <id>.L <count>.W (CZ_SHORTCUT_KEY_CHANGE)
  9271. void clif_parse_Hotkey(int32 fd, map_session_data *sd) {
  9272. #ifdef HOTKEY_SAVING
  9273. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9274. const PACKET_CZ_SHORTCUT_KEY_CHANGE2* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE2*>( RFIFOP( fd, 0 ) );
  9275. const uint16 idx = p->index + p->tab * MAX_HOTKEYS;
  9276. if( idx >= MAX_HOTKEYS_DB ){
  9277. return;
  9278. }
  9279. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9280. sd->status.hotkeys[idx].id = p->hotkey.id;
  9281. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9282. #elif PACKETVER_MAIN_NUM >= 20070618 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_AD_NUM >= 20070618 || PACKETVER_SAK_NUM >= 20070618
  9283. const PACKET_CZ_SHORTCUT_KEY_CHANGE1* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE1*>( RFIFOP( fd, 0 ) );
  9284. const uint16 idx = p->index;
  9285. if( idx >= MAX_HOTKEYS_DB ){
  9286. return;
  9287. }
  9288. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9289. sd->status.hotkeys[idx].id = p->hotkey.id;
  9290. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9291. #endif
  9292. #endif
  9293. }
  9294. /// Displays cast-like progress bar (ZC_PROGRESS).
  9295. /// 02f0 <color>.L <time>.L
  9296. void clif_progressbar(map_session_data * sd, unsigned long color, uint32 second)
  9297. {
  9298. int32 fd = sd->fd;
  9299. WFIFOHEAD(fd,packet_len(0x2f0));
  9300. WFIFOW(fd,0) = 0x2f0;
  9301. WFIFOL(fd,2) = color;
  9302. WFIFOL(fd,6) = second;
  9303. WFIFOSET(fd,packet_len(0x2f0));
  9304. }
  9305. /// Removes an ongoing progress bar (ZC_PROGRESS_CANCEL).
  9306. /// 02f2
  9307. void clif_progressbar_abort(map_session_data * sd)
  9308. {
  9309. int32 fd = sd->fd;
  9310. WFIFOHEAD(fd,packet_len(0x2f2));
  9311. WFIFOW(fd,0) = 0x2f2;
  9312. WFIFOSET(fd,packet_len(0x2f2));
  9313. }
  9314. /// Notification from the client, that the progress bar has reached 100%.
  9315. /// 02f1 (CZ_PROGRESS)
  9316. void clif_parse_progressbar(int32 fd, map_session_data * sd){
  9317. // No progressbar active, ignore it
  9318. if( !sd->progressbar.npc_id ){
  9319. return;
  9320. }
  9321. int32 npc_id = sd->progressbar.npc_id;
  9322. bool closing = false;
  9323. // Check if the progress was canceled
  9324. if( gettick() < sd->progressbar.timeout && sd->st ){
  9325. closing = true;
  9326. sd->st->state = CLOSE; // will result in END in npc_scriptcont
  9327. // If a message window was open, offer a close button to the user
  9328. if( sd->st->mes_active ){
  9329. clif_scriptclose( *sd, npc_id );
  9330. }
  9331. }
  9332. sd->progressbar.npc_id = 0;
  9333. sd->progressbar.timeout = 0;
  9334. sd->state.workinprogress = WIP_DISABLE_NONE;
  9335. if( battle_config.idletime_option&IDLE_NPC_PROGRESS ){
  9336. sd->idletime = last_tick;
  9337. }
  9338. npc_scriptcont(sd, npc_id, closing);
  9339. }
  9340. /// Displays cast-like progress bar on a NPC
  9341. /// 09d1 <id>.L <color>.L <time>.L (ZC_PROGRESS_ACTOR)
  9342. void clif_progressbar_npc( struct npc_data *nd, map_session_data* sd ){
  9343. #if PACKETVER >= 20130821
  9344. unsigned char buf[14];
  9345. if( nd->progressbar.timeout > 0 ){
  9346. WBUFW(buf,0) = 0x9d1;
  9347. WBUFL(buf,2) = nd->bl.id;
  9348. WBUFL(buf,6) = nd->progressbar.color;
  9349. WBUFL(buf,10) = client_tick( ( nd->progressbar.timeout - gettick() ) / 1000 );
  9350. if( sd ){
  9351. clif_send(buf, packet_len(0x9d1), &sd->bl, SELF);
  9352. }else{
  9353. clif_send(buf, packet_len(0x9d1), &nd->bl, AREA);
  9354. }
  9355. }
  9356. #endif
  9357. }
  9358. /// Request to walk to a certain position on the current map.
  9359. /// 0085 <dest>.3B (CZ_REQUEST_MOVE)
  9360. /// 035f <dest>.3B (CZ_REQUEST_MOVE2)
  9361. /// There are various variants of this packet, some of them have padding between fields.
  9362. void clif_parse_WalkToXY(int32 fd, map_session_data *sd)
  9363. {
  9364. int16 x, y;
  9365. if (pc_isdead(sd)) {
  9366. clif_clearunit_area( sd->bl, CLR_DEAD );
  9367. return;
  9368. }
  9369. if (sd->sc.opt1 && ( sd->sc.opt1 == OPT1_STONEWAIT || sd->sc.opt1 == OPT1_BURNING ))
  9370. ; //You CAN walk on this OPT1 value.
  9371. else if (sd->progressbar.npc_id) {
  9372. clif_progressbar_abort(sd);
  9373. return; // First walk attempt cancels the progress bar
  9374. } else if (pc_cant_act(sd))
  9375. return;
  9376. if(sd->sc.getSCE(SC_RUN) || sd->sc.getSCE(SC_WUGDASH))
  9377. return;
  9378. // TODO: shuffle packet
  9379. RFIFOPOS(fd, packet_db[RFIFOW(fd,0)].pos[0], &x, &y, nullptr);
  9380. //A move command one cell west is only valid if the target cell is free
  9381. if(battle_config.official_cell_stack_limit > 0
  9382. && sd->bl.x == x+1 && sd->bl.y == y && map_count_oncell(sd->bl.m, x, y, BL_CHAR|BL_NPC, 1))
  9383. return;
  9384. // Cloaking wall check is actually updated when you click to process next movement
  9385. // not when you move each cell. This is official behaviour.
  9386. if (sd->sc.getSCE(SC_CLOAKING))
  9387. skill_check_cloaking(&sd->bl, sd->sc.getSCE(SC_CLOAKING));
  9388. status_change_end(&sd->bl, SC_ROLLINGCUTTER); // If you move, you lose your counters. [malufett]
  9389. status_change_end(&sd->bl, SC_CRESCIVEBOLT);
  9390. status_change_end(&sd->bl, SC_KI_SUL_RAMPAGE);
  9391. pc_delinvincibletimer(sd);
  9392. //Set last idle time... [Skotlex]
  9393. if (battle_config.idletime_option&IDLE_WALK)
  9394. sd->idletime = last_tick;
  9395. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_WALK)
  9396. sd->idletime_hom = last_tick;
  9397. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_WALK)
  9398. sd->idletime_mer = last_tick;
  9399. unit_walktoxy(&sd->bl, x, y, 4);
  9400. }
  9401. /// Notification about the result of a disconnect request (ZC_ACK_REQ_DISCONNECT).
  9402. /// 018b <result>.W
  9403. /// result:
  9404. /// 0 = disconnect (quit)
  9405. /// 1 = cannot disconnect (wait 10 seconds)
  9406. /// ? = ignored
  9407. void clif_disconnect_ack(map_session_data* sd, int16 result)
  9408. {
  9409. int32 fd = sd->fd;
  9410. WFIFOHEAD(fd,packet_len(0x18b));
  9411. WFIFOW(fd,0) = 0x18b;
  9412. WFIFOW(fd,2) = result;
  9413. WFIFOSET(fd,packet_len(0x18b));
  9414. }
  9415. /// Request to disconnect from server (CZ_REQ_DISCONNECT).
  9416. /// 018a <type>.W
  9417. /// type:
  9418. /// 0 = quit
  9419. void clif_parse_QuitGame(int32 fd, map_session_data *sd)
  9420. {
  9421. /* Rovert's prevent logout option fixed [Valaris] */
  9422. if( !sd->sc.getSCE(SC_CLOAKING) && !sd->sc.getSCE(SC_HIDING) && !sd->sc.getSCE(SC_CHASEWALK) && !sd->sc.getSCE(SC_CLOAKINGEXCEED) && !sd->sc.getSCE(SC_SUHIDE) && !sd->sc.getSCE(SC_NEWMOON) &&
  9423. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9424. {
  9425. clif_disconnect_ack(sd, 0);
  9426. flush_fifo( fd );
  9427. if( battle_config.drop_connection_on_quit ){
  9428. set_eof( fd );
  9429. }
  9430. } else {
  9431. clif_disconnect_ack(sd, 1);
  9432. }
  9433. }
  9434. /// Requesting unit's name.
  9435. /// 0094 <id>.L (CZ_REQNAME)
  9436. /// 0368 <id>.L (CZ_REQNAME2)
  9437. /// There are various variants of this packet, some of them have padding between fields.
  9438. void clif_parse_GetCharNameRequest(int32 fd, map_session_data *sd)
  9439. {
  9440. // TODO: shuffle packet
  9441. int32 id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9442. struct block_list* bl;
  9443. //status_change *sc;
  9444. if( id < 0 && -id == sd->bl.id ) // for disguises [Valaris]
  9445. id = sd->bl.id;
  9446. bl = map_id2bl(id);
  9447. if( bl == nullptr )
  9448. return; // Lagged clients could request names of already gone mobs/players. [Skotlex]
  9449. if( sd->bl.m != bl->m || !check_distance_bl(&sd->bl, bl, AREA_SIZE) )
  9450. return; // Block namerequests past view range
  9451. // 'see people in GM hide' cheat detection
  9452. /* disabled due to false positives (network lag + request name of char that's about to hide = race condition)
  9453. sc = status_get_sc(bl);
  9454. if (sc && sc->option&OPTION_INVISIBLE && !disguised(bl) &&
  9455. bl->type != BL_NPC && //Skip hidden NPCs which can be seen using Maya Purple
  9456. pc_get_group_level(sd) < battle_config.hack_info_GM_level
  9457. ) {
  9458. char gm_msg[256];
  9459. sprintf(gm_msg, "Hack on NameRequest: character '%s' (account: %d) requested the name of an invisible target (id: %d).\n", sd->status.name, sd->status.account_id, id);
  9460. ShowWarning(gm_msg);
  9461. // information is sent to all online GMs
  9462. intif_wis_message_to_gm(wisp_server_name, battle_config.hack_info_GM_level, gm_msg);
  9463. return;
  9464. }
  9465. */
  9466. clif_name(&sd->bl,bl,SELF);
  9467. }
  9468. /// Validates and processes global messages
  9469. /// 008c <packet len>.W <text>.?B (<name> : <message>) 00 (CZ_REQUEST_CHAT)
  9470. /// There are various variants of this packet.
  9471. void clif_parse_GlobalMessage(int32 fd, map_session_data* sd)
  9472. {
  9473. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9474. size_t length;
  9475. // validate packet and retrieve name and message
  9476. if( !clif_process_message(sd, false, name, message, output ) )
  9477. return;
  9478. if( sd->gcbind && ((sd->gcbind->opt&CHAN_OPT_CAN_CHAT) || pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN)) ) {
  9479. channel_send(sd->gcbind,sd,message);
  9480. return;
  9481. }
  9482. // send message to others (using the send buffer for temp. storage)
  9483. clif_GlobalMessage( sd->bl, output, sd->chatID ? CHAT_WOS : AREA_CHAT_WOC );
  9484. length = strlen(output) + 1;
  9485. // send back message to the speaker
  9486. WFIFOHEAD(fd,4+length);
  9487. WFIFOW(fd,0) = 0x8e;
  9488. WFIFOW(fd,2) = (uint16)(4+length);
  9489. safestrncpy(WFIFOCP(fd,4), output, length );
  9490. WFIFOSET(fd, WFIFOW(fd,2));
  9491. #ifdef PCRE_SUPPORT
  9492. // trigger listening npcs
  9493. map_foreachinallrange(npc_chat_sub, &sd->bl, AREA_SIZE, BL_NPC, output, strlen(output), &sd->bl);
  9494. #endif
  9495. // Chat logging type 'O' / Global Chat
  9496. log_chat(LOG_CHAT_GLOBAL, 0, sd->status.char_id, sd->status.account_id, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y, nullptr, message);
  9497. //achievement_update_objective(sd, AG_CHAT, 1, sd->bl.m); //! TODO: What's the official use of this achievement type?
  9498. }
  9499. /// /mm /mapmove (as @rura GM command).
  9500. /// Request to warp to a map on given coordinates.
  9501. /// 0140 <map name>.16B <x>.W <y>.W (CZ_MOVETO_MAP)
  9502. void clif_parse_MapMove( int32 fd, map_session_data* sd){
  9503. const PACKET_CZ_MOVETO_MAP* p = reinterpret_cast<PACKET_CZ_MOVETO_MAP*>( RFIFOP( fd, 0 ) );
  9504. char map_name[MAP_NAME_LENGTH_EXT];
  9505. safestrncpy( map_name, p->map, sizeof( map_name ) );
  9506. char command[CHAT_SIZE_MAX];
  9507. safesnprintf( command, sizeof( command ),"%cmapmove %s %hu %hu", atcommand_symbol, map_name, p->x, p->y );
  9508. is_atcommand(fd, sd, command, 1);
  9509. }
  9510. /// Updates body and head direction of an object (ZC_CHANGE_DIRECTION).
  9511. /// 009c <id>.L <head dir>.W <dir>.B
  9512. /// head dir:
  9513. /// 0 = straight
  9514. /// 1 = turned CW
  9515. /// 2 = turned CCW
  9516. /// dir:
  9517. /// 0 = north
  9518. /// 1 = northwest
  9519. /// 2 = west
  9520. /// 3 = southwest
  9521. /// 4 = south
  9522. /// 5 = southeast
  9523. /// 6 = east
  9524. /// 7 = northeast
  9525. void clif_changed_dir(block_list& bl, enum send_target target){
  9526. PACKET_ZC_CHANGE_DIRECTION p{};
  9527. p.packetType = HEADER_ZC_CHANGE_DIRECTION;
  9528. p.srcId = bl.id;
  9529. if( bl.type == BL_PC ){
  9530. p.headDir = reinterpret_cast<map_session_data*>(&bl)->head_dir;
  9531. }else{
  9532. p.headDir = 0;
  9533. }
  9534. p.dir = unit_getdir(&bl);
  9535. clif_send(&p, sizeof(p), &bl, target);
  9536. if (disguised(&bl)) {
  9537. p.srcId = disguised_bl_id(bl.id);
  9538. p.headDir = 0;
  9539. clif_send(&p, sizeof(p), &bl, SELF);
  9540. }
  9541. }
  9542. /// Request to change own body and head direction.
  9543. /// 009b <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION)
  9544. /// 0361 <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION2)
  9545. /// There are various variants of this packet, some of them have padding between fields.
  9546. void clif_parse_ChangeDir(int32 fd, map_session_data *sd)
  9547. {
  9548. unsigned char headdir, dir;
  9549. // TODO: shuffle packet
  9550. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9551. headdir = RFIFOB(fd,info->pos[0]);
  9552. dir = RFIFOB(fd,info->pos[1]);
  9553. pc_setdir(sd, dir, headdir);
  9554. clif_changed_dir(sd->bl, AREA_WOS);
  9555. }
  9556. /// Request to show an emotion (CZ_REQ_EMOTION).
  9557. /// 00bf <type>.B
  9558. /// type:
  9559. /// @see enum emotion_type
  9560. void clif_parse_Emotion(int32 fd, map_session_data *sd){
  9561. if( sd == nullptr ){
  9562. return;
  9563. }
  9564. int32 emoticon = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9565. if (battle_config.basic_skill_check == 0 || pc_checkskill(sd, NV_BASIC) >= 2 || pc_checkskill(sd, SU_BASIC_SKILL) >= 1) {
  9566. if (emoticon == ET_CHAT_PROHIBIT) {// prevent use of the mute emote [Valaris]
  9567. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9568. return;
  9569. }
  9570. // fix flood of emotion icon (ro-proxy): flood only the hacker player
  9571. if (sd->emotionlasttime + 1 >= time(nullptr)) { // not more than 1 per second
  9572. sd->emotionlasttime = time(nullptr);
  9573. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9574. return;
  9575. }
  9576. sd->emotionlasttime = time(nullptr);
  9577. if (battle_config.idletime_option&IDLE_EMOTION)
  9578. sd->idletime = last_tick;
  9579. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_EMOTION)
  9580. sd->idletime_hom = last_tick;
  9581. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_EMOTION)
  9582. sd->idletime_mer = last_tick;
  9583. if (sd->state.block_action & PCBLOCK_EMOTION) {
  9584. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9585. return;
  9586. }
  9587. if(battle_config.client_reshuffle_dice && emoticon>=ET_DICE1 && emoticon<=ET_DICE6) {// re-roll dice
  9588. emoticon = rnd()%6+ET_DICE1;
  9589. }
  9590. clif_emotion(&sd->bl, emoticon);
  9591. } else
  9592. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9593. }
  9594. /// Amount of currently online players, reply to /w /who
  9595. /// 00c2 <count>.L (ZC_USER_COUNT)
  9596. static void clif_user_count(map_session_data& sd)
  9597. {
  9598. PACKET_ZC_USER_COUNT p{};
  9599. p.packetType = HEADER_ZC_USER_COUNT;
  9600. p.playersCount = map_getusers();
  9601. clif_send(&p,sizeof(p),&sd.bl,SELF);
  9602. }
  9603. /// /w /who (CZ_REQ_USER_COUNT).
  9604. /// Request to display amount of currently connected players.
  9605. /// 00c1
  9606. void clif_parse_HowManyConnections(int32 fd, map_session_data *sd) {
  9607. if( sd == nullptr ){
  9608. return;
  9609. }
  9610. clif_user_count(*sd);
  9611. }
  9612. void clif_parse_ActionRequest_sub( map_session_data& sd, uint8 action_type, int32 target_id, t_tick tick ){
  9613. if (pc_isdead(&sd)) {
  9614. clif_clearunit_area(sd.bl, CLR_DEAD);
  9615. return;
  9616. }
  9617. // Statuses that don't let the player sit / stand / talk with NPCs (targeted)
  9618. if (action_type != DMG_NORMAL && action_type != DMG_REPEAT) {
  9619. if (sd.sc.cant.interact)
  9620. return;
  9621. unit_stop_walking( &sd.bl, USW_FIXPOS );
  9622. }
  9623. unit_stop_attack( &sd.bl );
  9624. if(target_id<0 && -target_id == sd.bl.id) // for disguises [Valaris]
  9625. target_id = sd.bl.id;
  9626. switch(action_type)
  9627. {
  9628. case DMG_NORMAL: // once attack
  9629. case DMG_REPEAT: // continuous attack
  9630. if( pc_cant_act(&sd) )
  9631. return;
  9632. if (!battle_config.sdelay_attack_enable && pc_checkskill(&sd, SA_FREECAST) <= 0) {
  9633. if (DIFF_TICK(tick, sd.ud.canact_tick) < 0) {
  9634. clif_skill_fail( sd, 1, USESKILL_FAIL_SKILLINTERVAL );
  9635. return;
  9636. }
  9637. }
  9638. pc_delinvincibletimer(&sd);
  9639. if (battle_config.idletime_option&IDLE_ATTACK)
  9640. sd.idletime = last_tick;
  9641. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_ATTACK)
  9642. sd.idletime_hom = last_tick;
  9643. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_ATTACK)
  9644. sd.idletime_mer = last_tick;
  9645. unit_attack(&sd.bl, target_id, action_type != 0);
  9646. break;
  9647. case DMG_SIT_DOWN: // sitdown
  9648. if (battle_config.basic_skill_check && pc_checkskill(&sd, NV_BASIC) < 3 && pc_checkskill(&sd, SU_BASIC_SKILL) < 1) {
  9649. clif_skill_fail( sd, 1, USESKILL_FAIL_LEVEL, 2 );
  9650. break;
  9651. }
  9652. if(pc_issit(&sd)) {
  9653. //Bugged client? Just refresh them.
  9654. clif_sitting(sd.bl);
  9655. return;
  9656. }
  9657. if (sd.ud.skilltimer != INVALID_TIMER || (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING))
  9658. break;
  9659. if (!sd.sc.empty() && (
  9660. sd.sc.getSCE(SC_DANCING) ||
  9661. (sd.sc.getSCE(SC_GRAVITATION) && sd.sc.getSCE(SC_GRAVITATION)->val3 == BCT_SELF)
  9662. )) //No sitting during these states either.
  9663. break;
  9664. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9665. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9666. break;
  9667. }
  9668. if (battle_config.idletime_option&IDLE_SIT)
  9669. sd.idletime = last_tick;
  9670. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9671. sd.idletime_hom = last_tick;
  9672. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9673. sd.idletime_mer = last_tick;
  9674. pc_setsit(&sd);
  9675. skill_sit(&sd, true);
  9676. clif_sitting(sd.bl);
  9677. break;
  9678. case DMG_STAND_UP: // standup
  9679. if (!pc_issit(&sd)) {
  9680. //Bugged client? Just refresh them.
  9681. clif_standing(sd.bl);
  9682. return;
  9683. }
  9684. if (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING)
  9685. break;
  9686. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9687. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9688. break;
  9689. }
  9690. if (pc_setstand(&sd, false)) {
  9691. if (battle_config.idletime_option&IDLE_SIT)
  9692. sd.idletime = last_tick;
  9693. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9694. sd.idletime_hom = last_tick;
  9695. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9696. sd.idletime_mer = last_tick;
  9697. skill_sit(&sd, false);
  9698. clif_standing(sd.bl);
  9699. }
  9700. break;
  9701. }
  9702. }
  9703. /// Request for an action.
  9704. /// 0089 <target id>.L <action>.B (CZ_REQUEST_ACT)
  9705. /// 0437 <target id>.L <action>.B (CZ_REQUEST_ACT2)
  9706. /// action:
  9707. /// 0 = attack
  9708. /// 1 = pick up item
  9709. /// 2 = sit down
  9710. /// 3 = stand up
  9711. /// 7 = continous attack
  9712. /// 12 = (touch skill?)
  9713. /// There are various variants of this packet, some of them have padding between fields.
  9714. void clif_parse_ActionRequest(int32 fd, map_session_data *sd)
  9715. {
  9716. if( sd == nullptr ){
  9717. return;
  9718. }
  9719. // TODO: shuffle packet
  9720. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9721. clif_parse_ActionRequest_sub( *sd,
  9722. RFIFOB(fd,info->pos[1]),
  9723. RFIFOL(fd,info->pos[0]),
  9724. gettick()
  9725. );
  9726. }
  9727. /// Response to the death/system menu (CZ_RESTART).
  9728. /// 00b2 <type>.B
  9729. /// type:
  9730. /// 0 = restart (respawn)
  9731. /// 1 = char-select (disconnect)
  9732. void clif_parse_Restart(int32 fd, map_session_data *sd)
  9733. {
  9734. switch(RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0])) {
  9735. case 0x00:
  9736. pc_respawn(sd,CLR_OUTSIGHT);
  9737. break;
  9738. case 0x01:
  9739. /* Rovert's Prevent logout option - Fixed [Valaris] */
  9740. if( !sd->sc.getSCE(SC_CLOAKING) && !sd->sc.getSCE(SC_HIDING) && !sd->sc.getSCE(SC_CHASEWALK) && !sd->sc.getSCE(SC_CLOAKINGEXCEED) && !sd->sc.getSCE(SC_SUHIDE) && !sd->sc.getSCE(SC_NEWMOON) &&
  9741. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9742. { //Send to char-server for character selection.
  9743. chrif_charselectreq(sd, session[fd]->client_addr);
  9744. } else {
  9745. clif_disconnect_ack(sd, 1);
  9746. }
  9747. break;
  9748. }
  9749. }
  9750. /// Validates and processes whispered messages (CZ_WHISPER).
  9751. /// 0096 <packet len>.W <nick>.24B <message>.?B
  9752. void clif_parse_WisMessage(int32 fd, map_session_data* sd)
  9753. {
  9754. map_session_data* dstsd;
  9755. int32 i;
  9756. char target[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9757. // validate packet and retrieve name and message
  9758. if( !clif_process_message( sd, true, target, message, output ) )
  9759. return;
  9760. // Chat logging type 'W' / Whisper
  9761. log_chat(LOG_CHAT_WHISPER, 0, sd->status.char_id, sd->status.account_id, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y, target, message);
  9762. //-------------------------------------------------------//
  9763. // Lordalfa - Paperboy - To whisper NPC commands //
  9764. //-------------------------------------------------------//
  9765. if (target[0] && (strncasecmp(target,"NPC:",4) == 0) && (strlen(target) > 4))
  9766. {
  9767. char* str = target+4; //Skip the NPC: string part.
  9768. struct npc_data* npc;
  9769. if ((npc = npc_name2id(str))) {
  9770. char split_data[NUM_WHISPER_VAR][CHAT_SIZE_MAX];
  9771. char *split;
  9772. char event[EVENT_NAME_LENGTH];
  9773. str = message;
  9774. // skip codepage indicator, if detected
  9775. if( str[0] == '|' && strlen(str) >= 4 )
  9776. str += 3;
  9777. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {// Splits the message using '#' as separators
  9778. split = strchr(str,'#');
  9779. if( split == nullptr ) { // use the remaining string
  9780. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9781. for( ++i; i < NUM_WHISPER_VAR; ++i )
  9782. split_data[i][0] = '\0';
  9783. break;
  9784. }
  9785. *split = '\0';
  9786. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9787. str = split+1;
  9788. }
  9789. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {
  9790. char variablename[CHAT_SIZE_MAX];
  9791. safesnprintf(variablename,sizeof(variablename),"@whispervar%d$", i);
  9792. set_var_str( sd, variablename, split_data[i] );
  9793. }
  9794. safesnprintf(event,sizeof(event),"%s::%s", npc->exname,script_config.onwhisper_event_name);
  9795. npc_event(sd,event,0); // Calls the NPC label
  9796. return;
  9797. }
  9798. } else if( target[0] == '#' ) {
  9799. struct Channel *channel = nullptr;
  9800. char* chname = target;
  9801. channel = channel_name2channel(chname,sd,3);
  9802. if(channel && (pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) || ((channel->opt&CHAN_OPT_CAN_CHAT) && channel_pccheckgroup(channel,sd->group_id)))){
  9803. if(channel_pc_haschan(sd,channel)>=0){ //we are in the chan
  9804. channel_send(channel,sd,message);
  9805. }
  9806. else if( channel->pass[0] == '\0') { //no pass needed
  9807. if (channel_join(channel,sd)==0)
  9808. channel_send(channel,sd,message); //join success
  9809. }
  9810. else {
  9811. clif_displaymessage(fd, msg_txt(sd,1402)); //You're not in that channel, type '@join <#channel_name>'
  9812. }
  9813. return;
  9814. }
  9815. }
  9816. // searching destination character
  9817. dstsd = map_nick2sd(target,false);
  9818. if (dstsd == nullptr || strcmp(dstsd->status.name, target) != 0) {
  9819. // player is not on this map-server
  9820. // At this point, don't send wisp/page if it's not exactly the same name, because (example)
  9821. // if there are 'Test' player on an other map-server and 'test' player on this map-server,
  9822. // and if we ask for 'Test', we must not contact 'test' player
  9823. // so, we send information to inter-server, which is the only one which decide (and copy correct name).
  9824. intif_wis_message(sd, target, message, strlen(message) + 1);
  9825. return;
  9826. }
  9827. // if player ignores everyone
  9828. if (dstsd->state.ignoreAll && pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9829. if (pc_isinvisible(dstsd) && pc_get_group_level(sd) < pc_get_group_level(dstsd))
  9830. clif_wis_end( *sd, ACKWHISPER_TARGET_OFFLINE );
  9831. else
  9832. clif_wis_end( *sd, ACKWHISPER_ALL_IGNORED );
  9833. return;
  9834. }
  9835. // if player is autotrading
  9836. if (dstsd->state.autotrade == 1){
  9837. safesnprintf(output,sizeof(output),"%s is in autotrade mode and cannot receive whispered messages.", dstsd->status.name);
  9838. clif_wis_message(sd, wisp_server_name, output, strlen(output) + 1, 0);
  9839. return;
  9840. }
  9841. if (pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9842. // if player ignores the source character
  9843. ARR_FIND(0, MAX_IGNORE_LIST, i, dstsd->ignore[i].name[0] == '\0' || strcmp(dstsd->ignore[i].name, sd->status.name) == 0);
  9844. if(i < MAX_IGNORE_LIST && dstsd->ignore[i].name[0] != '\0') { // source char present in ignore list
  9845. clif_wis_end( *sd, ACKWHISPER_IGNORED );
  9846. return;
  9847. }
  9848. }
  9849. // notify sender of success
  9850. clif_wis_end( *sd, ACKWHISPER_SUCCESS );
  9851. // Normal message
  9852. clif_wis_message(dstsd, sd->status.name, message, strlen(message)+1, 0);
  9853. }
  9854. /// /b /nb.
  9855. /// Request to broadcast a message on whole server.
  9856. /// 0099 <packet len>.W <text>.?B 00 (CZ_BROADCAST)
  9857. void clif_parse_Broadcast(int32 fd, map_session_data* sd) {
  9858. const PACKET_CZ_BROADCAST* p = reinterpret_cast<PACKET_CZ_BROADCAST*>( RFIFOP( fd, 0 ) );
  9859. if( p->packetSize < sizeof( *p ) ){
  9860. return;
  9861. }
  9862. char message[CHAT_SIZE_MAX];
  9863. safestrncpy( message, p->message, std::min<size_t>( sizeof( message ), p->packetSize - sizeof( *p ) + 1 ) );
  9864. char command[CHAT_SIZE_MAX];
  9865. safesnprintf( command, sizeof( command ),"%ckami %s", atcommand_symbol, message );
  9866. is_atcommand(fd, sd, command, 1);
  9867. }
  9868. /// Request to pick up an item.
  9869. /// 009f <id>.L (CZ_ITEM_PICKUP)
  9870. /// 0362 <id>.L (CZ_ITEM_PICKUP2)
  9871. /// There are various variants of this packet, some of them have padding between fields.
  9872. void clif_parse_TakeItem(int32 fd, map_session_data *sd)
  9873. {
  9874. struct flooritem_data *fitem;
  9875. int32 map_object_id;
  9876. // TODO: shuffle packet
  9877. map_object_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9878. fitem = (struct flooritem_data*)map_id2bl(map_object_id);
  9879. do {
  9880. if (pc_isdead(sd)) {
  9881. clif_clearunit_area( sd->bl, CLR_DEAD );
  9882. break;
  9883. }
  9884. if (fitem == nullptr || fitem->bl.type != BL_ITEM || fitem->bl.m != sd->bl.m)
  9885. break;
  9886. if (pc_cant_act(sd))
  9887. break;
  9888. if (!pc_takeitem(sd, fitem))
  9889. break;
  9890. return;
  9891. } while (0);
  9892. // Client REQUIRES a fail packet or you can no longer pick items.
  9893. clif_additem(sd,0,0,6);
  9894. }
  9895. /// Request to drop an item.
  9896. /// 00a2 <index>.W <amount>.W (CZ_ITEM_THROW)
  9897. /// 0363 <index>.W <amount>.W (CZ_ITEM_THROW2)
  9898. /// There are various variants of this packet, some of them have padding between fields.
  9899. void clif_parse_DropItem(int32 fd, map_session_data *sd){
  9900. // TODO: shuffle packet
  9901. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9902. int32 item_index = RFIFOW(fd,info->pos[0]) -2;
  9903. int32 item_amount = RFIFOW(fd,info->pos[1]) ;
  9904. for(;;) {
  9905. if (pc_isdead(sd))
  9906. break;
  9907. if (pc_cant_act2(sd) || sd->npc_id)
  9908. break;
  9909. if (sd->sc.cant.drop)
  9910. break;
  9911. if (!pc_dropitem(sd, item_index, item_amount))
  9912. break;
  9913. if (battle_config.idletime_option&IDLE_DROPITEM)
  9914. sd->idletime = last_tick;
  9915. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_DROPITEM)
  9916. sd->idletime_hom = last_tick;
  9917. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_DROPITEM)
  9918. sd->idletime_mer = last_tick;
  9919. return;
  9920. }
  9921. //Because the client does not like being ignored.
  9922. clif_dropitem( *sd, item_index, 0 );
  9923. }
  9924. /// Request to use an item.
  9925. /// 00a7 <index>.W <account id>.L (CZ_USE_ITEM)
  9926. /// 0439 <index>.W <account id>.L (CZ_USE_ITEM2)
  9927. /// There are various variants of this packet, some of them have padding between fields.
  9928. void clif_parse_UseItem(int32 fd, map_session_data *sd)
  9929. {
  9930. int32 n;
  9931. if (pc_isdead(sd)) {
  9932. clif_clearunit_area( sd->bl, CLR_DEAD );
  9933. return;
  9934. }
  9935. if ( (!sd->npc_id && pc_istrading(sd)) || sd->chatID || (sd->state.block_action & PCBLOCK_USEITEM) ) {
  9936. clif_msg(sd, MSI_BUSY);
  9937. return;
  9938. }
  9939. //Whether the item is used or not is irrelevant, the char ain't idle. [Skotlex]
  9940. if (battle_config.idletime_option&IDLE_USEITEM)
  9941. sd->idletime = last_tick;
  9942. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9943. sd->idletime_hom = last_tick;
  9944. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  9945. sd->idletime_mer = last_tick;
  9946. n = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  9947. if(n <0 || n >= MAX_INVENTORY)
  9948. return;
  9949. if (!pc_useitem(sd,n))
  9950. clif_useitemack(sd,n,0,false); //Send an empty ack packet or the client gets stuck.
  9951. }
  9952. /// Request to equip an item
  9953. /// 00a9 <index>.W <position>.W (CZ_REQ_WEAR_EQUIP).
  9954. /// 0998 <index>.W <position>.L (CZ_REQ_WEAR_EQUIP_V5)
  9955. void clif_parse_EquipItem( int32 fd, map_session_data* sd ){
  9956. const PACKET_CZ_REQ_WEAR_EQUIP* p = reinterpret_cast<PACKET_CZ_REQ_WEAR_EQUIP*>( RFIFOP( fd, 0 ) );
  9957. if(pc_isdead(sd)) {
  9958. clif_clearunit_area( sd->bl, CLR_DEAD );
  9959. return;
  9960. }
  9961. uint16 index = server_index( p->index );
  9962. if( index >= MAX_INVENTORY ){
  9963. return;
  9964. }
  9965. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  9966. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  9967. return;
  9968. } else if (sd->state.storage_flag || sd->sc.opt1)
  9969. ; //You can equip/unequip stuff while storage is open/under status changes
  9970. else if (pc_cant_act2(sd))
  9971. return;
  9972. if(!sd->inventory.u.items_inventory[index].identify) {
  9973. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, index ); // fail
  9974. return;
  9975. }
  9976. if(!sd->inventory_data[index])
  9977. return;
  9978. if(sd->inventory_data[index]->type == IT_PETARMOR){
  9979. pet_equipitem(sd,index);
  9980. return;
  9981. }
  9982. if (battle_config.idletime_option&IDLE_USEITEM)
  9983. sd->idletime = last_tick;
  9984. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9985. sd->idletime_hom = last_tick;
  9986. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  9987. sd->idletime_mer = last_tick;
  9988. //Client doesn't send the position for ammo.
  9989. if(sd->inventory_data[index]->type == IT_AMMO)
  9990. pc_equipitem(sd,index,EQP_AMMO);
  9991. else{
  9992. pc_equipitem( sd, index, p->position );
  9993. }
  9994. }
  9995. /// Request to take off an equip (CZ_REQ_TAKEOFF_EQUIP).
  9996. /// 00ab <index>.W
  9997. void clif_parse_UnequipItem(int32 fd,map_session_data *sd)
  9998. {
  9999. int32 index;
  10000. if(pc_isdead(sd)) {
  10001. clif_clearunit_area( sd->bl, CLR_DEAD );
  10002. return;
  10003. }
  10004. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  10005. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  10006. return;
  10007. } else if (sd->state.storage_flag || sd->sc.opt1)
  10008. ; //You can equip/unequip stuff while storage is open/under status changes
  10009. else if (pc_cant_act2(sd))
  10010. return;
  10011. index = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  10012. if (battle_config.idletime_option&IDLE_USEITEM)
  10013. sd->idletime = last_tick;
  10014. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  10015. sd->idletime_hom = last_tick;
  10016. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  10017. sd->idletime_mer = last_tick;
  10018. pc_unequipitem(sd,index,1);
  10019. }
  10020. /// Request to start a conversation with an NPC.
  10021. /// 0090 <id>.L <type>.B (CZ_CONTACTNPC)
  10022. /// type:
  10023. /// 1 = click
  10024. void clif_parse_NpcClicked( int32 fd, map_session_data* sd ){
  10025. const PACKET_CZ_CONTACTNPC* p = reinterpret_cast<PACKET_CZ_CONTACTNPC*>( RFIFOP( fd, 0 ) );
  10026. if( sd == nullptr ){
  10027. return;
  10028. }
  10029. if(pc_isdead(sd)) {
  10030. clif_clearunit_area( sd->bl, CLR_DEAD );
  10031. return;
  10032. }
  10033. if( pc_cant_act2(sd) || sd->npc_id || pc_hasprogress( sd, WIP_DISABLE_NPC ) ){
  10034. #ifdef RENEWAL
  10035. clif_msg( sd, MSI_BUSY);
  10036. #endif
  10037. return;
  10038. }
  10039. if( sd->state.mail_writing )
  10040. return;
  10041. block_list* bl = map_id2bl( p->AID );
  10042. if( bl == nullptr ){
  10043. return;
  10044. }
  10045. switch (bl->type) {
  10046. case BL_MOB:
  10047. case BL_PC:
  10048. clif_parse_ActionRequest_sub( *sd, DMG_REPEAT, bl->id, gettick() );
  10049. break;
  10050. case BL_NPC:
  10051. #ifdef RENEWAL
  10052. if (sd->ud.skill_id < RK_ENCHANTBLADE && sd->ud.skilltimer != INVALID_TIMER) { // Should only show an error message for non-3rd job skills with a running timer
  10053. clif_msg(sd, MSI_BUSY);
  10054. break;
  10055. }
  10056. #endif
  10057. if( bl->m != -1 ){ // the user can't click floating npcs directly (hack attempt)
  10058. struct npc_data* nd = (struct npc_data*)bl;
  10059. // Progressbar is running
  10060. if( nd->progressbar.timeout > 0 ){
  10061. return;
  10062. }
  10063. npc_click(sd,nd);
  10064. }
  10065. break;
  10066. }
  10067. }
  10068. /// Selection between buy/sell was made.
  10069. /// 00c5 <id>.L <type>.B (CZ_ACK_SELECT_DEALTYPE)
  10070. /// type:
  10071. /// 0 = buy
  10072. /// 1 = sell
  10073. void clif_parse_NpcBuySellSelected( int32 fd, map_session_data* sd ){
  10074. const PACKET_CZ_ACK_SELECT_DEALTYPE* p = reinterpret_cast<PACKET_CZ_ACK_SELECT_DEALTYPE*>( RFIFOP( fd, 0 ) );
  10075. if( sd == nullptr ){
  10076. return;
  10077. }
  10078. if (sd->state.trading)
  10079. return;
  10080. npc_buysellsel( sd, p->GID, p->type );
  10081. }
  10082. /// Notification about the result of a purchase attempt from an NPC shop (ZC_PC_PURCHASE_RESULT).
  10083. /// 00ca <result>.B
  10084. /// result:
  10085. /// 0 = "The deal has successfully completed."
  10086. /// 1 = "You do not have enough zeny."
  10087. /// 2 = "You are over your Weight Limit."
  10088. /// 3 = "Out of the maximum capacity, you have too many items."
  10089. /// 9 = "Amounts are exceeded the possession of the item is not available for purchase."
  10090. /// 10 = "Props open-air store sales will be traded in RODEX"
  10091. /// 11 = "The exchange failed."
  10092. /// 12 = "The exchange was well done."
  10093. /// 13 = "The item is already sold and out of stock."
  10094. /// 14 = "There is not enough goods to exchange."
  10095. void clif_npc_buy_result( map_session_data* sd, e_purchase_result result ){
  10096. PACKET_ZC_PC_PURCHASE_RESULT p = {};
  10097. p.packetType = HEADER_ZC_PC_PURCHASE_RESULT;
  10098. p.result = (uint8)result;
  10099. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  10100. }
  10101. /// Request to buy chosen items from npc shop.
  10102. /// 00c8 <packet len>.W { <amount>.W <name id>.W }* (CZ_PC_PURCHASE_ITEMLIST)
  10103. void clif_parse_NpcBuyListSend( int32 fd, map_session_data* sd ){
  10104. const PACKET_CZ_PC_PURCHASE_ITEMLIST *p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10105. uint16 n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->items[0] );
  10106. e_purchase_result result;
  10107. if( sd->state.trading || !sd->npc_shopid )
  10108. result = e_purchase_result::PURCHASE_FAIL_MONEY;
  10109. else{
  10110. std::vector<s_npc_buy_list> items = {};
  10111. items.reserve( n );
  10112. for( uint16 i = 0; i < n; i++ ){
  10113. s_npc_buy_list item = {};
  10114. item.nameid = p->items[i].itemId;
  10115. item.qty = p->items[i].amount;
  10116. items.push_back( item );
  10117. }
  10118. result = npc_buylist( sd, items );
  10119. }
  10120. sd->npc_shopid = 0; //Clear shop data.
  10121. clif_npc_buy_result(sd, result);
  10122. }
  10123. /// Notification about the result of a sell attempt to an NPC shop (ZC_PC_SELL_RESULT).
  10124. /// 00cb <result>.B
  10125. /// result:
  10126. /// 0 = "The deal has successfully completed."
  10127. /// 1 = "The deal has failed."
  10128. void clif_npc_sell_result(map_session_data* sd, unsigned char result)
  10129. {
  10130. int32 fd = sd->fd;
  10131. WFIFOHEAD(fd,packet_len(0xcb));
  10132. WFIFOW(fd,0) = 0xcb;
  10133. WFIFOB(fd,2) = result;
  10134. WFIFOSET(fd,packet_len(0xcb));
  10135. }
  10136. /// Request to sell chosen items to npc shop (CZ_PC_SELL_ITEMLIST).
  10137. /// 00c9 <packet len>.W { <index>.W <amount>.W }*
  10138. void clif_parse_NpcSellListSend(int32 fd,map_session_data *sd)
  10139. {
  10140. int32 fail=0;
  10141. const PACKET_CZ_PC_SELL_ITEMLIST* p = reinterpret_cast<PACKET_CZ_PC_SELL_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10142. int32 n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->sellList[0] );
  10143. if (sd->state.trading || !sd->npc_shopid)
  10144. fail = 1;
  10145. else
  10146. fail = npc_selllist(sd, n, p->sellList);
  10147. sd->npc_shopid = 0; //Clear shop data.
  10148. clif_npc_sell_result(sd, fail);
  10149. }
  10150. /// Chatroom creation request.
  10151. /// 00d5 <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B (CZ_CREATE_CHATROOM)
  10152. /// type:
  10153. /// 0 = private
  10154. /// 1 = public
  10155. void clif_parse_CreateChatRoom( int32 fd, map_session_data* sd){
  10156. const PACKET_CZ_CREATE_CHATROOM* p = reinterpret_cast<PACKET_CZ_CREATE_CHATROOM*>( RFIFOP( fd, 0 ) );
  10157. if( sd == nullptr ){
  10158. return;
  10159. }
  10160. if( sd->sc.getSCE( SC_NOCHAT ) && sd->sc.getSCE( SC_NOCHAT )->val1&MANNER_NOROOM ){
  10161. return;
  10162. }
  10163. if(battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 4 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10164. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 3 );
  10165. return;
  10166. }
  10167. if( npc_isnear(&sd->bl) ) {
  10168. // uncomment to send msg_txt.
  10169. //char output[150];
  10170. //sprintf(output, msg_txt(662), battle_config.min_npc_vendchat_distance);
  10171. //clif_displaymessage(sd->fd, output);
  10172. clif_skill_fail( *sd, 1, USESKILL_FAIL_THERE_ARE_NPC_AROUND );
  10173. return;
  10174. }
  10175. if( p->packetSize < sizeof( *p ) ){
  10176. // invalid input
  10177. return;
  10178. }
  10179. char password[CHATROOM_PASS_SIZE];
  10180. safestrncpy( password, p->password, sizeof( password ) );
  10181. char title[CHATROOM_TITLE_SIZE];
  10182. safestrncpy( title, p->title, std::min<size_t>( sizeof( title ), p->packetSize - sizeof( *p ) + 1 ) );
  10183. chat_createpcchat( sd, title, password, p->limit, p->type != 0 );
  10184. }
  10185. /// Chatroom join request.
  10186. /// 00d9 <chat ID>.L <passwd>.8B (CZ_REQ_ENTER_ROOM)
  10187. void clif_parse_ChatAddMember(int32 fd, map_session_data* sd){
  10188. if( sd == nullptr ){
  10189. return;
  10190. }
  10191. const PACKET_CZ_REQ_ENTER_ROOM* p = reinterpret_cast<PACKET_CZ_REQ_ENTER_ROOM*>( RFIFOP( fd, 0 ) );
  10192. chat_joinchat( sd, p->chat_id, p->password );
  10193. }
  10194. /// Chatroom properties adjustment request.
  10195. /// 00de <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B (CZ_CHANGE_CHATROOM)
  10196. /// type:
  10197. /// 0 = private
  10198. /// 1 = public
  10199. void clif_parse_ChatRoomStatusChange(int32 fd, map_session_data* sd){
  10200. if( sd == nullptr ){
  10201. return;
  10202. }
  10203. const PACKET_CZ_CHANGE_CHATROOM* p = reinterpret_cast<PACKET_CZ_CHANGE_CHATROOM*>( RFIFOP( fd, 0 ) );
  10204. size_t len = p->packetSize - sizeof( *p );
  10205. if( len <= 0 )
  10206. return; // invalid input
  10207. char s_password[CHATROOM_PASS_SIZE];
  10208. char s_title[CHATROOM_TITLE_SIZE];
  10209. safestrncpy( s_password, p->password, sizeof( s_password ) );
  10210. // NOTE: assumes that safestrncpy will not access the len+1'th byte
  10211. safestrncpy( s_title, p->title, min( len + 1, CHATROOM_TITLE_SIZE ) );
  10212. chat_changechatstatus( sd, s_title, s_password, p->limit, p->type );
  10213. }
  10214. /// Request to change the chat room ownership (CZ_REQ_ROLE_CHANGE).
  10215. /// 00e0 <role>.L <nick>.24B
  10216. /// role:
  10217. /// 0 = owner
  10218. /// 1 = normal
  10219. void clif_parse_ChangeChatOwner(int32 fd, map_session_data* sd)
  10220. {
  10221. //int32 role = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10222. chat_changechatowner(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]));
  10223. }
  10224. /// Request to expel a player from chat room (CZ_REQ_EXPEL_MEMBER).
  10225. /// 00e2 <name>.24B
  10226. void clif_parse_KickFromChat(int32 fd,map_session_data *sd)
  10227. {
  10228. chat_kickchat(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10229. }
  10230. /// Request to leave the current chatroom (CZ_EXIT_ROOM).
  10231. /// 00e3
  10232. void clif_parse_ChatLeave(int32 fd, map_session_data* sd)
  10233. {
  10234. chat_leavechat(sd,0);
  10235. }
  10236. // Handles notifying asker and rejecter of what has just ocurred.
  10237. // Type is used to determine the correct msg_txt to use
  10238. void clif_noask_sub( map_session_data& sd, map_session_data& tsd, int32 type ){
  10239. char output[CHAT_SIZE_MAX];
  10240. // Your request has been rejected by autoreject option.
  10241. clif_messagecolor( &sd.bl, color_table[COLOR_LIGHT_GREEN], msg_txt( &sd, 392 ), false, SELF );
  10242. // Notice that a request was rejected.
  10243. safesnprintf( output, CHAT_SIZE_MAX, msg_txt( &tsd, type ), sd.status.name );
  10244. clif_messagecolor( &tsd.bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  10245. }
  10246. /// Request to begin a trade (CZ_REQ_EXCHANGE_ITEM).
  10247. /// 00e4 <account id>.L
  10248. void clif_parse_TradeRequest(int32 fd,map_session_data *sd)
  10249. {
  10250. map_session_data *t_sd;
  10251. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10252. if(!sd->chatID && pc_cant_act(sd))
  10253. return; //You can trade while in a chatroom.
  10254. if(t_sd){
  10255. // @noask [LuzZza]
  10256. if(t_sd->state.noask) {
  10257. clif_noask_sub( *sd, *t_sd, 393 ); // Autorejected trade request from %s.
  10258. return;
  10259. }
  10260. if (t_sd->state.mail_writing) {
  10261. // Fake trading
  10262. clif_traderesponse(*sd,TRADE_ACK_BUSY);
  10263. return;
  10264. }
  10265. }
  10266. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 1 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10267. clif_skill_fail( *sd, 1 );
  10268. return;
  10269. }
  10270. trade_traderequest(sd,t_sd);
  10271. }
  10272. /// Answer to a trade request (CZ_ACK_EXCHANGE_ITEM).
  10273. /// 00e6 <result>.B
  10274. /// result:
  10275. /// 3 = accepted
  10276. /// 4 = rejected
  10277. void clif_parse_TradeAck(int32 fd,map_session_data *sd)
  10278. {
  10279. trade_tradeack(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10280. }
  10281. /// Request to add an item to current trade.
  10282. /// 00e8 <index>.W <amount>.L (CZ_ADD_EXCHANGE_ITEM)
  10283. void clif_parse_TradeAddItem(int32 fd,map_session_data *sd)
  10284. {
  10285. if( sd == nullptr ){
  10286. return;
  10287. }
  10288. const PACKET_CZ_ADD_EXCHANGE_ITEM* p = reinterpret_cast<PACKET_CZ_ADD_EXCHANGE_ITEM*>( RFIFOP( fd, 0 ) );
  10289. if( p->index == 0 ){
  10290. trade_tradeaddzeny( sd, p->amount );
  10291. }else{
  10292. trade_tradeadditem( sd, server_index( p->index ), static_cast<int16>( p->amount ) );
  10293. }
  10294. }
  10295. /// Request to lock items in current trade (CZ_CONCLUDE_EXCHANGE_ITEM).
  10296. /// 00eb
  10297. void clif_parse_TradeOk(int32 fd,map_session_data *sd)
  10298. {
  10299. trade_tradeok(sd);
  10300. }
  10301. /// Request to cancel current trade (CZ_CANCEL_EXCHANGE_ITEM).
  10302. /// 00ed
  10303. void clif_parse_TradeCancel(int32 fd,map_session_data *sd)
  10304. {
  10305. trade_tradecancel(sd);
  10306. }
  10307. /// Request to commit current trade (CZ_EXEC_EXCHANGE_ITEM).
  10308. /// 00ef
  10309. void clif_parse_TradeCommit(int32 fd,map_session_data *sd)
  10310. {
  10311. trade_tradecommit(sd);
  10312. }
  10313. /// Request to stop chasing/attacking an unit (CZ_CANCEL_LOCKON).
  10314. /// 0118
  10315. void clif_parse_StopAttack(int32 fd,map_session_data *sd)
  10316. {
  10317. unit_stop_attack( &sd->bl );
  10318. sd->ud.state.attack_continue = 0;
  10319. }
  10320. /// Request to move an item from inventory to cart.
  10321. /// 0126 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_CART)
  10322. void clif_parse_PutItemToCart( int32 fd, map_session_data *sd ){
  10323. if (pc_istrading(sd) || !pc_iscarton(sd) || pc_cant_act2(sd))
  10324. return;
  10325. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10326. return;
  10327. const PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART* p = reinterpret_cast<PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART*>( RFIFOP( fd, 0 ) );
  10328. pc_putitemtocart( sd, server_index( p->index ), p->count);
  10329. }
  10330. /// Request to move an item from cart to inventory.
  10331. /// 0127 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_CART_TO_BODY)
  10332. void clif_parse_GetItemFromCart(int32 fd,map_session_data *sd)
  10333. {
  10334. if( sd == nullptr ){
  10335. return;
  10336. }
  10337. if (!pc_iscarton(sd) || pc_cant_act2(sd))
  10338. return;
  10339. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10340. return;
  10341. const PACKET_CZ_MOVE_ITEM_FROM_CART_TO_BODY* p = reinterpret_cast<PACKET_CZ_MOVE_ITEM_FROM_CART_TO_BODY*>( RFIFOP( fd, 0 ) );
  10342. pc_getitemfromcart( sd, server_index( p->index ), p->amount );
  10343. }
  10344. /// Request to remove cart/falcon/peco/dragon/mado (CZ_REQ_CARTOFF).
  10345. /// 012a
  10346. void clif_parse_RemoveOption(int32 fd,map_session_data *sd)
  10347. {
  10348. if( !(sd->sc.option&(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR))
  10349. #ifdef NEW_CARTS
  10350. && sd->sc.getSCE(SC_PUSH_CART) )
  10351. pc_setcart(sd,0);
  10352. #else
  10353. )
  10354. pc_setoption(sd,sd->sc.option&~OPTION_CART);
  10355. #endif
  10356. else // priority to remove this option before we can clear cart
  10357. pc_setoption(sd,sd->sc.option&~(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR));
  10358. }
  10359. /// Request to select cart's visual look for new cart design (ZC_SELECTCART).
  10360. /// 097f <Length>.W <identity>.L <type>.B
  10361. void clif_SelectCart(map_session_data *sd) {
  10362. #if PACKETVER >= 20150826
  10363. int32 i = 0, carts = 3;
  10364. int32 fd = sd->fd;
  10365. WFIFOHEAD(fd,8 + carts);
  10366. WFIFOW(fd,0) = 0x97f;
  10367. WFIFOW(fd,2) = 8 + carts;
  10368. WFIFOL(fd,4) = sd->status.account_id;
  10369. // Right now we have 10-12, tested it you can also enable selection for all cart styles here(1-12)
  10370. for( i = 0; i < carts; i++ ) {
  10371. WFIFOB(fd,8 + i) = 10 + i;
  10372. }
  10373. WFIFOSET(fd,8 + carts);
  10374. #endif
  10375. }
  10376. /// Request to select cart's visual look for new cart design (CZ_SELECTCART).
  10377. /// 0980 <identity>.L <type>.B
  10378. void clif_parse_SelectCart(int32 fd,map_session_data *sd) {
  10379. #if PACKETVER >= 20150826
  10380. int32 type;
  10381. // Check identity
  10382. if( !sd || pc_checkskill(sd,MC_CARTDECORATE) < 1 || RFIFOL(fd,2) != sd->status.account_id )
  10383. return;
  10384. type = (int32)RFIFOB(fd,6);
  10385. // Check type
  10386. if( type < 10 || type > 12 )
  10387. return;
  10388. pc_setcart(sd, type);
  10389. #endif
  10390. }
  10391. /// Request to change cart's visual look (CZ_REQ_CHANGECART).
  10392. /// 01af <num>.W
  10393. void clif_parse_ChangeCart(int32 fd,map_session_data *sd)
  10394. {// TODO: State tracking?
  10395. int32 type;
  10396. if( !sd || pc_checkskill(sd, MC_CHANGECART) < 1 )
  10397. return;
  10398. #ifdef RENEWAL
  10399. if (sd->npc_id || pc_hasprogress(sd, WIP_DISABLE_SKILLITEM)) {
  10400. clif_msg(sd, MSI_BUSY);
  10401. return;
  10402. }
  10403. #endif
  10404. type = (int32)RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10405. if(
  10406. #ifdef NEW_CARTS
  10407. #if PACKETVER >= 20191106
  10408. (type == 13 && sd->status.base_level > 100) ||
  10409. #endif
  10410. (type == 9 && sd->status.base_level > 130) ||
  10411. (type == 8 && sd->status.base_level > 120) ||
  10412. (type == 7 && sd->status.base_level > 110) ||
  10413. (type == 6 && sd->status.base_level > 100) ||
  10414. #endif
  10415. (type == 5 && sd->status.base_level > 90) ||
  10416. (type == 4 && sd->status.base_level > 80) ||
  10417. (type == 3 && sd->status.base_level > 65) ||
  10418. (type == 2 && sd->status.base_level > 40) ||
  10419. (type == 1))
  10420. pc_setcart(sd, type);
  10421. }
  10422. /// Request to increase status (CZ_STATUS_CHANGE).
  10423. /// 00bb <status id>.W <amount>.B
  10424. /// status id:
  10425. /// SP_STR ~ SP_LUK
  10426. /// amount:
  10427. /// Old clients always send 1 for this, even when using /str+ and the like.
  10428. /// Newer clients (2013-12-23 and newer) send the correct amount.
  10429. void clif_parse_StatusUp(int32 fd,map_session_data *sd)
  10430. {
  10431. int32 increase_amount = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  10432. if( increase_amount < 0 ) {
  10433. ShowDebug("clif_parse_StatusUp: Negative 'increase' value sent by client! (fd: %d, value: %d)\n",
  10434. fd, increase_amount);
  10435. }
  10436. pc_statusup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]),increase_amount);
  10437. }
  10438. /// Request to increase trait status.
  10439. /// 0b24 <status id>.W <amount>.W (CZ_ADVANCED_STATUS_CHANGE)
  10440. /// status id:
  10441. /// SP_POW ~ SP_CON
  10442. /// amount:
  10443. /// The amount to increase the trait status
  10444. void clif_parse_traitstatus_up( int32 fd, map_session_data *sd ){
  10445. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10446. const PACKET_CZ_ADVANCED_STATUS_CHANGE* p = reinterpret_cast<PACKET_CZ_ADVANCED_STATUS_CHANGE*>( RFIFOP( fd, 0 ) );
  10447. if( p->amount < 0 ){
  10448. ShowDebug( "clif_parse_traitstatus_up: Negative 'increase' value sent by client! %s (AID: %d, CID: %d, value: %d)\n", sd->status.name, sd->status.account_id, sd->status.char_id, p->amount );
  10449. return;
  10450. }
  10451. pc_traitstatusup( sd, p->type, p->amount );
  10452. #endif
  10453. }
  10454. /// Request to increase level of a skill (CZ_UPGRADE_SKILLLEVEL).
  10455. /// 0112 <skill id>.W
  10456. void clif_parse_SkillUp(int32 fd,map_session_data *sd)
  10457. {
  10458. pc_skillup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10459. }
  10460. static void clif_parse_UseSkillToId_homun(struct homun_data *hd, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, int32 target_id)
  10461. {
  10462. int32 lv;
  10463. if( !hd )
  10464. return;
  10465. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10466. clif_emotion(&hd->bl, ET_THINK);
  10467. return;
  10468. }
  10469. if( hd->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10470. target_id = hd->bl.id;
  10471. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10472. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10473. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10474. clif_emotion(&hd->bl, ET_THINK);
  10475. if (hd->master)
  10476. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10477. return;
  10478. }
  10479. lv = hom_checkskill(hd, skill_id);
  10480. if( skill_lv > lv )
  10481. skill_lv = lv;
  10482. if( skill_lv )
  10483. unit_skilluse_id(&hd->bl, target_id, skill_id, skill_lv);
  10484. }
  10485. static void clif_parse_UseSkillToPos_homun(struct homun_data *hd, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, int16 x, int16 y, int32 skillmoreinfo)
  10486. {
  10487. int32 lv;
  10488. if( !hd )
  10489. return;
  10490. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10491. clif_emotion(&hd->bl, ET_THINK);
  10492. return;
  10493. }
  10494. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10495. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10496. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10497. clif_emotion(&hd->bl, ET_THINK);
  10498. if (hd->master)
  10499. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10500. return;
  10501. }
  10502. #ifdef RENEWAL
  10503. if (hd->sc.getSCE(SC_BASILICA_CELL))
  10504. #else
  10505. if (hd->sc.getSCE(SC_BASILICA))
  10506. #endif
  10507. return;
  10508. lv = hom_checkskill(hd, skill_id);
  10509. if( skill_lv > lv )
  10510. skill_lv = lv;
  10511. if( skill_lv )
  10512. unit_skilluse_pos(&hd->bl, x, y, skill_id, skill_lv);
  10513. }
  10514. static void clif_parse_UseSkillToId_mercenary(s_mercenary_data *md, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, int32 target_id)
  10515. {
  10516. int32 lv;
  10517. if( !md )
  10518. return;
  10519. if( skill_isNotOk_mercenary(skill_id, *md) )
  10520. return;
  10521. if( md->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10522. target_id = md->bl.id;
  10523. if( md->ud.skilltimer != INVALID_TIMER )
  10524. {
  10525. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10526. }
  10527. else if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10528. return;
  10529. lv = mercenary_checkskill(md, skill_id);
  10530. if( skill_lv > lv )
  10531. skill_lv = lv;
  10532. if( skill_lv )
  10533. unit_skilluse_id(&md->bl, target_id, skill_id, skill_lv);
  10534. }
  10535. static void clif_parse_UseSkillToPos_mercenary(s_mercenary_data *md, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, int16 x, int16 y, int32 skillmoreinfo)
  10536. {
  10537. int32 lv;
  10538. if( !md )
  10539. return;
  10540. if( skill_isNotOk_mercenary(skill_id, *md) )
  10541. return;
  10542. if( md->ud.skilltimer != INVALID_TIMER )
  10543. return;
  10544. if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10545. {
  10546. if (md->master)
  10547. clif_skill_fail( *md->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10548. return;
  10549. }
  10550. #ifdef RENEWAL
  10551. if (md->sc.getSCE(SC_BASILICA_CELL))
  10552. #else
  10553. if (md->sc.getSCE(SC_BASILICA))
  10554. #endif
  10555. return;
  10556. lv = mercenary_checkskill(md, skill_id);
  10557. if( skill_lv > lv )
  10558. skill_lv = lv;
  10559. if( skill_lv )
  10560. unit_skilluse_pos(&md->bl, x, y, skill_id, skill_lv);
  10561. }
  10562. void clif_parse_skill_toid( map_session_data* sd, uint16 skill_id, uint16 skill_lv, int32 target_id ){
  10563. if( sd == nullptr ){
  10564. return;
  10565. }
  10566. t_tick tick = gettick();
  10567. if( skill_lv < 1 ) skill_lv = 1; //No clue, I have seen the client do this with guild skills :/ [Skotlex]
  10568. int32 inf = skill_get_inf(skill_id);
  10569. if (inf&INF_GROUND_SKILL || !inf)
  10570. return; //Using a ground/passive skill on a target? WRONG.
  10571. if (sd->state.block_action & PCBLOCK_SKILL) {
  10572. clif_msg(sd, MSI_BUSY);
  10573. return;
  10574. }
  10575. if( SKILL_CHK_HOMUN(skill_id) ) {
  10576. clif_parse_UseSkillToId_homun(sd->hd, sd, tick, skill_id, skill_lv, target_id);
  10577. return;
  10578. }
  10579. if( SKILL_CHK_MERC(skill_id) ) {
  10580. clif_parse_UseSkillToId_mercenary(sd->md, sd, tick, skill_id, skill_lv, target_id);
  10581. return;
  10582. }
  10583. // Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10584. // This is done here, because homunculi and mercenaries can be triggered by AI and not by the player itself
  10585. if (battle_config.idletime_option&IDLE_USESKILLTOID)
  10586. sd->idletime = last_tick;
  10587. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USESKILLTOID)
  10588. sd->idletime_hom = last_tick;
  10589. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USESKILLTOID)
  10590. sd->idletime_mer = last_tick;
  10591. if( sd->npc_id ){
  10592. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag || !( inf & INF_SELF_SKILL ) ){
  10593. #ifdef RENEWAL
  10594. clif_msg( sd, MSI_BUSY);
  10595. #endif
  10596. return;
  10597. }
  10598. }
  10599. if ((pc_cant_act2(sd) || sd->chatID) &&
  10600. skill_id != RK_REFRESH &&
  10601. !( ( skill_id == SR_GENTLETOUCH_CURE || skill_id == SU_GROOMING ) && (sd->sc.opt1 == OPT1_STONE || sd->sc.opt1 == OPT1_FREEZE || sd->sc.opt1 == OPT1_STUN)) &&
  10602. !(sd->state.storage_flag && (inf&INF_SELF_SKILL))) //SELF skills can be used with the storage open, issue: 8027
  10603. return;
  10604. if( pc_issit(sd) )
  10605. return;
  10606. if( skill_isNotOk(skill_id, *sd) )
  10607. return;
  10608. if( sd->bl.id != target_id && inf&INF_SELF_SKILL )
  10609. target_id = sd->bl.id; // never trust the client
  10610. if( target_id < 0 && -target_id == sd->bl.id ) // for disguises [Valaris]
  10611. target_id = sd->bl.id;
  10612. if( sd->ud.skilltimer != INVALID_TIMER ) {
  10613. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST )
  10614. return;
  10615. } else if( DIFF_TICK(tick, sd->ud.canact_tick) < 0 ) {
  10616. if( sd->skillitem != skill_id ) {
  10617. clif_skill_fail( *sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10618. return;
  10619. }
  10620. }
  10621. if( sd->sc.option&OPTION_COSTUME )
  10622. return;
  10623. #ifndef RENEWAL
  10624. if( sd->sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd->sc.getSCE(SC_BASILICA)->val4 != sd->bl.id) )
  10625. return; // On basilica only caster can use Basilica again to stop it.
  10626. #endif
  10627. if( sd->menuskill_id ) {
  10628. if( sd->menuskill_id == SA_TAMINGMONSTER ) {
  10629. clif_menuskill_clear(sd); //Cancel pet capture.
  10630. }else if( sd->menuskill_id == SG_FEEL ){
  10631. clif_menuskill_clear( sd ); // Cancel selection
  10632. }else if( sd->menuskill_id != SA_AUTOSPELL )
  10633. return; //Can't use skills while a menu is open.
  10634. }
  10635. if( sd->skillitem == skill_id ) {
  10636. if( skill_lv != sd->skillitemlv )
  10637. skill_lv = sd->skillitemlv;
  10638. if( !(inf&INF_SELF_SKILL) )
  10639. pc_delinvincibletimer(sd); // Target skills thru items cancel invincibility. [Inkfish]
  10640. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10641. return;
  10642. }
  10643. sd->skillitem = sd->skillitemlv = 0;
  10644. if( SKILL_CHK_GUILD(skill_id) ) {
  10645. if (sd->guild != nullptr && (sd->state.gmaster_flag || skill_id == GD_CHARGESHOUT_BEATING))
  10646. skill_lv = guild_checkskill(sd->guild->guild, skill_id);
  10647. else
  10648. skill_lv = 0;
  10649. } else {
  10650. if( skill_id != ALL_EQSWITCH ){
  10651. skill_lv = min(pc_checkskill(sd, skill_id),skill_lv); //never trust client
  10652. }
  10653. }
  10654. pc_delinvincibletimer(sd);
  10655. if( skill_lv )
  10656. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10657. }
  10658. /// Request to use a targeted skill.
  10659. /// 0113 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL)
  10660. /// 0438 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL2)
  10661. /// There are various variants of this packet, some of them have padding between fields.
  10662. void clif_parse_UseSkillToId( int32 fd, map_session_data *sd ){
  10663. // TODO: shuffle packet
  10664. struct s_packet_db* info = &packet_db[RFIFOW(fd, 0)];
  10665. clif_parse_skill_toid( sd, RFIFOW(fd, info->pos[1]), RFIFOW(fd, info->pos[0]), RFIFOL(fd, info->pos[2]) );
  10666. }
  10667. /*==========================================
  10668. * Client tells server he'd like to use AoE skill id 'skill_id' of level 'skill_lv' on 'x','y' location
  10669. *------------------------------------------*/
  10670. static void clif_parse_UseSkillToPosSub( int32 fd, map_session_data& sd, uint16 skill_lv, uint16 skill_id, int16 x, int16 y, int32 skillmoreinfo ){
  10671. t_tick tick = gettick();
  10672. if( !(skill_get_inf(skill_id)&INF_GROUND_SKILL) )
  10673. return; //Using a target skill on the ground? WRONG.
  10674. if (sd.state.block_action & PCBLOCK_SKILL) {
  10675. clif_msg(&sd, MSI_BUSY);
  10676. return;
  10677. }
  10678. if( SKILL_CHK_HOMUN(skill_id) ) {
  10679. clif_parse_UseSkillToPos_homun(sd.hd, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10680. return;
  10681. }
  10682. if( SKILL_CHK_MERC(skill_id) ) {
  10683. clif_parse_UseSkillToPos_mercenary(sd.md, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10684. return;
  10685. }
  10686. if( pc_hasprogress( &sd, WIP_DISABLE_SKILLITEM ) ){
  10687. #ifdef RENEWAL
  10688. clif_msg( &sd, MSI_BUSY);
  10689. #endif
  10690. return;
  10691. }
  10692. //Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10693. if (battle_config.idletime_option&IDLE_USESKILLTOPOS)
  10694. sd.idletime = last_tick;
  10695. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_USESKILLTOPOS)
  10696. sd.idletime_hom = last_tick;
  10697. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_USESKILLTOPOS)
  10698. sd.idletime_mer = last_tick;
  10699. if( skill_isNotOk(skill_id, sd) )
  10700. return;
  10701. if( skillmoreinfo != -1 ) {
  10702. if( pc_issit(&sd) ) {
  10703. clif_skill_fail( sd, skill_id );
  10704. return;
  10705. }
  10706. //You can't use Graffiti/TalkieBox AND have a vending open, so this is safe.
  10707. safestrncpy(sd.message, RFIFOCP(fd,skillmoreinfo), MESSAGE_SIZE);
  10708. }
  10709. if( sd.ud.skilltimer != INVALID_TIMER )
  10710. return;
  10711. if( DIFF_TICK(tick, sd.ud.canact_tick) < 0 ) {
  10712. if( sd.skillitem != skill_id ) {
  10713. clif_skill_fail( sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10714. return;
  10715. }
  10716. }
  10717. if( sd.sc.option&OPTION_COSTUME )
  10718. return;
  10719. #ifndef RENEWAL
  10720. if( sd.sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd.sc.getSCE(SC_BASILICA)->val4 != sd.bl.id) )
  10721. return; // On basilica only caster can use Basilica again to stop it.
  10722. #endif
  10723. if( sd.menuskill_id ) {
  10724. if( sd.menuskill_id == SA_TAMINGMONSTER ) {
  10725. clif_menuskill_clear(&sd); //Cancel pet capture.
  10726. }else if( sd.menuskill_id == SG_FEEL ){
  10727. clif_menuskill_clear( &sd ); // Cancel selection
  10728. } else if( sd.menuskill_id != SA_AUTOSPELL )
  10729. return; //Can't use skills while a menu is open.
  10730. }
  10731. pc_delinvincibletimer(&sd);
  10732. if( sd.skillitem == skill_id ) {
  10733. if( skill_lv != sd.skillitemlv )
  10734. skill_lv = sd.skillitemlv;
  10735. unit_skilluse_pos(&sd.bl, x, y, skill_id, skill_lv);
  10736. } else {
  10737. int32 lv;
  10738. sd.skillitem = sd.skillitemlv = 0;
  10739. if( (lv = pc_checkskill(&sd, skill_id)) > 0 ) {
  10740. if( skill_lv > lv )
  10741. skill_lv = lv;
  10742. unit_skilluse_pos(&sd.bl, x, y, skill_id,skill_lv);
  10743. }
  10744. }
  10745. }
  10746. /// Request to use a ground skill.
  10747. /// 0116 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND)
  10748. /// 0366 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND2)
  10749. /// 0AF4 <skill lv>.W <skill id>.W <x>.W <y>.W <unknown>.B (CZ_USE_SKILL_TOGROUND3)
  10750. /// There are various variants of this packet, some of them have padding between fields.
  10751. void clif_parse_UseSkillToPos(int32 fd, map_session_data *sd)
  10752. {
  10753. if( sd == nullptr ){
  10754. return;
  10755. }
  10756. // TODO: shuffle packet
  10757. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10758. if (pc_cant_act(sd))
  10759. return;
  10760. if (pc_issit(sd))
  10761. return;
  10762. clif_parse_UseSkillToPosSub(fd, *sd,
  10763. RFIFOW(fd,info->pos[0]), //skill lv
  10764. RFIFOW(fd,info->pos[1]), //skill num
  10765. RFIFOW(fd,info->pos[2]), //pos x
  10766. RFIFOW(fd,info->pos[3]), //pos y
  10767. // TODO: find out what this is intended to do
  10768. //RFIFOB(fd, info->pos[4])
  10769. -1 //Skill more info.
  10770. );
  10771. }
  10772. /// Request to use a ground skill with text.
  10773. /// 0190 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX)
  10774. /// 0367 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX2)
  10775. /// There are various variants of this packet, some of them have padding between fields.
  10776. void clif_parse_UseSkillToPosMoreInfo(int32 fd, map_session_data *sd)
  10777. {
  10778. if( sd == nullptr ){
  10779. return;
  10780. }
  10781. // TODO: shuffle packet
  10782. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10783. if (pc_cant_act(sd))
  10784. return;
  10785. if (pc_issit(sd))
  10786. return;
  10787. clif_parse_UseSkillToPosSub(fd, *sd,
  10788. RFIFOW(fd,info->pos[0]), //Skill lv
  10789. RFIFOW(fd,info->pos[1]), //Skill num
  10790. RFIFOW(fd,info->pos[2]), //pos x
  10791. RFIFOW(fd,info->pos[3]), //pos y
  10792. info->pos[4] //skill more info
  10793. );
  10794. }
  10795. /// Answer to map selection dialog.
  10796. /// 011b <skill id>.W <map name>.16B (CZ_SELECT_WARPPOINT)
  10797. void clif_parse_UseSkillMap(int32 fd, map_session_data* sd)
  10798. {
  10799. if( sd == nullptr ){
  10800. return;
  10801. }
  10802. sd->state.workinprogress = WIP_DISABLE_NONE;
  10803. const PACKET_CZ_SELECT_WARPPOINT* p = reinterpret_cast<PACKET_CZ_SELECT_WARPPOINT*>( RFIFOP( fd, 0 ) );
  10804. if( p->skill_id != sd->menuskill_id ){
  10805. return;
  10806. }
  10807. //It is possible to use teleport with the storage window open bugreport:8027
  10808. if( pc_cant_act( sd ) && !sd->state.storage_flag && p->skill_id != AL_TELEPORT ){
  10809. clif_menuskill_clear(sd);
  10810. return;
  10811. }
  10812. pc_delinvincibletimer(sd);
  10813. char map_name[MAP_NAME_LENGTH];
  10814. mapindex_getmapname( p->mapname, map_name );
  10815. skill_castend_map( sd, p->skill_id, map_name );
  10816. }
  10817. /// Request to set a memo on current map (CZ_REMEMBER_WARPPOINT).
  10818. /// 011d
  10819. void clif_parse_RequestMemo(int32 fd,map_session_data *sd)
  10820. {
  10821. if (!pc_isdead(sd))
  10822. pc_memo(sd,-1);
  10823. }
  10824. /// Answer to pharmacy item selection dialog.
  10825. /// 018e <name id>.W { <material id>.W }*3 (CZ_REQMAKINGITEM)
  10826. void clif_parse_ProduceMix(int32 fd,map_session_data *sd){
  10827. if( sd == nullptr ){
  10828. return;
  10829. }
  10830. const PACKET_CZ_REQMAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQMAKINGITEM*>( RFIFOP( fd, 0 ) );
  10831. switch( sd->menuskill_id ) {
  10832. case -1:
  10833. case AM_PHARMACY:
  10834. case RK_RUNEMASTERY:
  10835. case GC_RESEARCHNEWPOISON:
  10836. break;
  10837. default:
  10838. return;
  10839. }
  10840. if (pc_istrading(sd)) {
  10841. //Make it fail to avoid shop exploits where you sell something different than you see.
  10842. clif_skill_fail( *sd, sd->ud.skill_id );
  10843. clif_menuskill_clear(sd);
  10844. return;
  10845. }
  10846. int32 produce_idx;
  10847. if( (produce_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, 1)) )
  10848. skill_produce_mix(sd,0,p->itemId,p->material[0],p->material[1],p->material[2],1,produce_idx-1);
  10849. clif_menuskill_clear(sd);
  10850. }
  10851. /// Answer to mixing item selection dialog.
  10852. /// 025b <mk type>.W <name id>.W (CZ_REQ_MAKINGITEM)
  10853. /// mk type:
  10854. /// 1 = cooking
  10855. /// 2 = arrow
  10856. /// 3 = elemental
  10857. /// 4 = GN_MIX_COOKING
  10858. /// 5 = GN_MAKEBOMB
  10859. /// 6 = GN_S_PHARMACY
  10860. /// 7 = MT_M_MACHINE - Unconfirmed
  10861. /// 8 = BO_BIONIC_PHARMACY - Unconfirmed
  10862. void clif_parse_Cooking(int32 fd,map_session_data *sd) {
  10863. #if PACKETVER >= 20051010
  10864. if( sd == nullptr ){
  10865. return;
  10866. }
  10867. const PACKET_CZ_REQ_MAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGITEM*>( RFIFOP( fd, 0 ) );
  10868. int32 amount = sd->menuskill_val2 ? sd->menuskill_val2 : 1;
  10869. int16 food_idx = -1;
  10870. if( p->type == 6 && sd->menuskill_id != GN_MIX_COOKING && sd->menuskill_id != GN_S_PHARMACY )
  10871. return;
  10872. if (pc_istrading(sd)) {
  10873. //Make it fail to avoid shop exploits where you sell something different than you see.
  10874. clif_skill_fail( *sd, sd->ud.skill_id );
  10875. clif_menuskill_clear(sd);
  10876. return;
  10877. }
  10878. if( (food_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, amount)) )
  10879. skill_produce_mix(sd,(p->type>1?sd->menuskill_id:0),p->itemId,0,0,0,amount,food_idx-1);
  10880. clif_menuskill_clear(sd);
  10881. #endif
  10882. }
  10883. /// Answer to repair weapon item selection dialog
  10884. /// 01fd <index> W (CZ_REQ_ITEMREPAIR)
  10885. /// 01fd <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W ???
  10886. void clif_parse_RepairItem( int32 fd, map_session_data *sd ){
  10887. if( sd == nullptr ){
  10888. return;
  10889. }
  10890. // Hercules has wrong date -> use correct one here
  10891. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10892. const PACKET_CZ_REQ_ITEMREPAIR2* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR2*>( RFIFOP( fd, 0 ) );
  10893. #else
  10894. const PACKET_CZ_REQ_ITEMREPAIR1* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR1*>( RFIFOP( fd, 0 ) );
  10895. #endif
  10896. if (sd->menuskill_id != BS_REPAIRWEAPON)
  10897. return;
  10898. if (pc_istrading(sd)) {
  10899. //Make it fail to avoid shop exploits where you sell something different than you see.
  10900. clif_skill_fail( *sd, sd->ud.skill_id );
  10901. clif_menuskill_clear(sd);
  10902. return;
  10903. }
  10904. skill_repairweapon( *sd, p->item.index );
  10905. clif_menuskill_clear(sd);
  10906. }
  10907. /// Answer to refine weapon item selection dialog (CZ_REQ_WEAPONREFINE).
  10908. /// 0222 <index>.L
  10909. void clif_parse_WeaponRefine( int32 fd, map_session_data *sd ){
  10910. if( sd == nullptr ){
  10911. return;
  10912. }
  10913. if (sd->menuskill_id != WS_WEAPONREFINE) //Packet exploit?
  10914. return;
  10915. if (pc_istrading(sd)) {
  10916. //Make it fail to avoid shop exploits where you sell something different than you see.
  10917. clif_skill_fail( *sd, sd->ud.skill_id );
  10918. clif_menuskill_clear(sd);
  10919. return;
  10920. }
  10921. skill_weaponrefine( *sd, server_index( RFIFOL( fd, 2 ) ) );
  10922. clif_menuskill_clear(sd);
  10923. }
  10924. /// Answer to script menu dialog (CZ_CHOOSE_MENU).
  10925. /// 00b8 <npc id>.L <choice>.B
  10926. /// choice:
  10927. /// 1~254 = menu item
  10928. /// 255 = cancel
  10929. /// NOTE: If there were more than 254 items in the list, choice
  10930. /// overflows to choice%256.
  10931. void clif_parse_NpcSelectMenu(int32 fd,map_session_data *sd){
  10932. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10933. int32 npc_id = RFIFOL(fd,info->pos[0]);
  10934. uint8 select = RFIFOB(fd,info->pos[1]);
  10935. #ifdef SECURE_NPCTIMEOUT
  10936. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  10937. return;
  10938. #endif
  10939. if( (select > sd->npc_menu && select != 0xff) || select == 0 ) {
  10940. TBL_NPC* nd = map_id2nd(npc_id);
  10941. ShowWarning("Invalid menu selection on npc %d:'%s' - got %d, valid range is [%d..%d] (player AID:%d, CID:%d, name:'%s')!\n", npc_id, (nd)?nd->name:"invalid npc id", select, 1, sd->npc_menu, sd->bl.id, sd->status.char_id, sd->status.name);
  10942. clif_GM_kick(nullptr,sd);
  10943. return;
  10944. }
  10945. sd->npc_menu = select;
  10946. if( battle_config.idletime_option&IDLE_NPC_MENU ){
  10947. sd->idletime = last_tick;
  10948. }
  10949. npc_scriptcont(sd,npc_id, false);
  10950. }
  10951. /// NPC dialog 'next' click (CZ_REQ_NEXT_SCRIPT).
  10952. /// 00b9 <npc id>.L
  10953. void clif_parse_NpcNextClicked(int32 fd,map_session_data *sd)
  10954. {
  10955. if( battle_config.idletime_option&IDLE_NPC_NEXT ){
  10956. sd->idletime = last_tick;
  10957. }
  10958. npc_scriptcont(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), false);
  10959. }
  10960. /// NPC numeric input dialog value (CZ_INPUT_EDITDLG).
  10961. /// 0143 <npc id>.L <value>.L
  10962. void clif_parse_NpcAmountInput(int32 fd,map_session_data *sd){
  10963. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10964. int32 npcid = RFIFOL(fd,info->pos[0]);
  10965. int32 amount = (int32)RFIFOL(fd,info->pos[1]);
  10966. sd->npc_amount = amount;
  10967. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  10968. sd->idletime = last_tick;
  10969. }
  10970. npc_scriptcont(sd, npcid, false);
  10971. }
  10972. /// NPC text input dialog value (CZ_INPUT_EDITDLGSTR).
  10973. /// 01d5 <packet len>.W <npc id>.L <string>.?B
  10974. void clif_parse_NpcStringInput(int32 fd, map_session_data* sd){
  10975. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10976. int32 message_len = RFIFOW(fd,info->pos[0])-8;
  10977. int32 npcid = RFIFOL(fd,info->pos[1]);
  10978. const char* message = RFIFOCP(fd,info->pos[2]);
  10979. if( message_len <= 0 )
  10980. return; // invalid input
  10981. #if PACKETVER >= 20151001
  10982. message_len++;
  10983. #endif
  10984. safestrncpy(sd->npc_str, message, min(message_len,CHATBOX_SIZE));
  10985. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  10986. sd->idletime = last_tick;
  10987. }
  10988. npc_scriptcont(sd, npcid, false);
  10989. }
  10990. /// NPC dialog 'close' click (CZ_CLOSE_DIALOG).
  10991. /// 0146 <npc id>.L
  10992. void clif_parse_NpcCloseClicked(int32 fd,map_session_data *sd)
  10993. {
  10994. if (!sd->npc_id) //Avoid parsing anything when the script was done with. [Skotlex]
  10995. return;
  10996. if( battle_config.idletime_option&IDLE_NPC_CLOSE ){
  10997. sd->idletime = last_tick;
  10998. }
  10999. npc_scriptcont(sd, RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), true);
  11000. }
  11001. /// Answer to identify item selection dialog (CZ_REQ_ITEMIDENTIFY).
  11002. /// 0178 <index>.W
  11003. /// index:
  11004. /// -1 = cancel
  11005. void clif_parse_ItemIdentify(int32 fd,map_session_data *sd) {
  11006. int16 idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2;
  11007. if (sd->menuskill_id != MC_IDENTIFY)
  11008. return;
  11009. // Ignore the request
  11010. // - Invalid item index
  11011. // - Invalid item ID or item doesn't exist
  11012. // - Item is already identified
  11013. if (idx < 0 || idx >= MAX_INVENTORY ||
  11014. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  11015. sd->inventory.u.items_inventory[idx].identify) {// cancel pressed
  11016. sd->state.workinprogress = WIP_DISABLE_NONE;
  11017. clif_menuskill_clear(sd);
  11018. return;
  11019. }
  11020. skill_identify(sd, idx);
  11021. clif_menuskill_clear(sd);
  11022. }
  11023. /// Answer to arrow crafting item selection dialog.
  11024. /// 01ae <name id>.W (CZ_REQ_MAKINGARROW)
  11025. void clif_parse_SelectArrow(int32 fd,map_session_data *sd) {
  11026. if( sd == nullptr ){
  11027. return;
  11028. }
  11029. if (pc_istrading(sd)) {
  11030. //Make it fail to avoid shop exploits where you sell something different than you see.
  11031. clif_skill_fail( *sd, sd->ud.skill_id );
  11032. clif_menuskill_clear(sd);
  11033. return;
  11034. }
  11035. const PACKET_CZ_REQ_MAKINGARROW* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGARROW*>( RFIFOP( fd, 0 ) );
  11036. switch (sd->menuskill_id) {
  11037. case AC_MAKINGARROW:
  11038. skill_arrow_create(sd,p->itemId);
  11039. break;
  11040. case SA_CREATECON:
  11041. skill_produce_mix(sd,SA_CREATECON,p->itemId,0,0,0,1,-1);
  11042. break;
  11043. case GC_POISONINGWEAPON:
  11044. skill_poisoningweapon(*sd,p->itemId);
  11045. break;
  11046. case NC_MAGICDECOY:
  11047. skill_magicdecoy(*sd,p->itemId);
  11048. break;
  11049. }
  11050. clif_menuskill_clear(sd);
  11051. }
  11052. /// Answer to SA_AUTOSPELL skill selection dialog (CZ_SELECTAUTOSPELL).
  11053. /// 01ce <skill id>.L
  11054. void clif_parse_AutoSpell(int32 fd,map_session_data *sd)
  11055. {
  11056. if (sd->menuskill_id != SA_AUTOSPELL)
  11057. return;
  11058. sd->state.workinprogress = WIP_DISABLE_NONE;
  11059. skill_autospell(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11060. clif_menuskill_clear(sd);
  11061. }
  11062. /// Request to display item carding/composition list (CZ_REQ_ITEMCOMPOSITION_LIST).
  11063. /// 017a <card index>.W
  11064. void clif_parse_UseCard(int32 fd,map_session_data *sd)
  11065. {
  11066. if (sd->state.trading != 0)
  11067. return;
  11068. clif_use_card(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  11069. }
  11070. /// Answer to carding/composing item selection dialog (CZ_REQ_ITEMCOMPOSITION).
  11071. /// 017c <card index>.W <equip index>.W
  11072. void clif_parse_InsertCard(int32 fd,map_session_data *sd)
  11073. {
  11074. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11075. if (sd->state.trading != 0)
  11076. return;
  11077. pc_insert_card(sd,RFIFOW(fd,info->pos[0])-2,RFIFOW(fd,info->pos[1])-2);
  11078. }
  11079. /// Request of character's name by char ID.
  11080. /// 0193 <char id>.L (CZ_REQNAME_BYGID)
  11081. /// 0369 <char id>.L (CZ_REQNAME_BYGID2)
  11082. /// There are various variants of this packet, some of them have padding between fields.
  11083. void clif_parse_SolveCharName(int32 fd, map_session_data *sd)
  11084. {
  11085. int32 charid;
  11086. // TODO: shuffle packet
  11087. charid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11088. map_reqnickdb(sd, charid);
  11089. }
  11090. /// /resetskill /resetstate (CZ_RESET).
  11091. /// Request to reset stats or skills.
  11092. /// 0197 <type>.W
  11093. /// type:
  11094. /// 0 = state
  11095. /// 1 = skill
  11096. void clif_parse_ResetChar(int32 fd, map_session_data *sd) {
  11097. char cmd[15];
  11098. if( RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) )
  11099. safesnprintf(cmd,sizeof(cmd),"%cresetskill",atcommand_symbol);
  11100. else
  11101. safesnprintf(cmd,sizeof(cmd),"%cresetstat",atcommand_symbol);
  11102. is_atcommand(fd, sd, cmd, 1);
  11103. }
  11104. /// /lb /nlb (CZ_LOCALBROADCAST).
  11105. /// Request to broadcast a message on current map.
  11106. /// 019c <packet len>.W <text>.?B
  11107. void clif_parse_LocalBroadcast(int32 fd, map_session_data* sd)
  11108. {
  11109. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11110. char command[CHAT_SIZE_MAX+16];
  11111. uint32 len = RFIFOW(fd,info->pos[0])-4;
  11112. char* msg = RFIFOCP(fd,info->pos[1]);
  11113. // as the length varies depending on the command used, just block unreasonably long strings
  11114. mes_len_check(msg, len, CHAT_SIZE_MAX);
  11115. safesnprintf(command,sizeof(command),"%clkami %s", atcommand_symbol, msg);
  11116. is_atcommand(fd, sd, command, 1);
  11117. }
  11118. /// Request to move an item from inventory to storage.
  11119. /// 00f3 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE)
  11120. /// 0364 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE2)
  11121. /// There are various variants of this packet, some of them have padding between fields.
  11122. void clif_parse_MoveToKafra(int32 fd, map_session_data *sd)
  11123. {
  11124. int32 item_index, item_amount;
  11125. // TODO: shuffle packet
  11126. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11127. if (pc_istrading(sd))
  11128. return;
  11129. item_index = RFIFOW(fd,info->pos[0])-2;
  11130. item_amount = RFIFOL(fd,info->pos[1]);
  11131. if (item_index < 0 || item_index >= MAX_INVENTORY || item_amount < 1)
  11132. return;
  11133. if( sd->inventory.u.items_inventory[item_index].equipSwitch ){
  11134. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11135. return;
  11136. }
  11137. if (sd->state.storage_flag == 1)
  11138. storage_storageadd(sd, &sd->storage, item_index, item_amount);
  11139. else
  11140. if (sd->state.storage_flag == 2)
  11141. storage_guild_storageadd(sd, item_index, item_amount);
  11142. else if (sd->state.storage_flag == 3)
  11143. storage_storageadd(sd, &sd->premiumStorage, item_index, item_amount);
  11144. }
  11145. /// Request to move an item from storage to inventory.
  11146. /// 00f5 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY)
  11147. /// 0365 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY2)
  11148. /// There are various variants of this packet, some of them have padding between fields.
  11149. void clif_parse_MoveFromKafra(int32 fd,map_session_data *sd)
  11150. {
  11151. int32 item_index, item_amount;
  11152. // TODO: shuffle packet
  11153. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11154. item_index = RFIFOW(fd,info->pos[0])-1;
  11155. item_amount = RFIFOL(fd,info->pos[1]);
  11156. if (sd->state.storage_flag == 1)
  11157. storage_storageget(sd, &sd->storage, item_index, item_amount);
  11158. else if(sd->state.storage_flag == 2)
  11159. storage_guild_storageget(sd, item_index, item_amount);
  11160. else if(sd->state.storage_flag == 3)
  11161. storage_storageget(sd, &sd->premiumStorage, item_index, item_amount);
  11162. }
  11163. /// Request to move an item from cart to storage (CZ_MOVE_ITEM_FROM_CART_TO_STORE).
  11164. /// 0129 <index>.W <amount>.L
  11165. void clif_parse_MoveToKafraFromCart(int32 fd, map_session_data *sd){
  11166. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11167. int32 idx = RFIFOW(fd,info->pos[0]) - 2;
  11168. int32 amount = RFIFOL(fd,info->pos[1]);
  11169. if( sd->state.vending )
  11170. return;
  11171. if (!pc_iscarton(sd))
  11172. return;
  11173. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11174. return;
  11175. if (idx < 0 || idx >= MAX_INVENTORY || amount < 1)
  11176. return;
  11177. if( sd->inventory.u.items_inventory[idx].equipSwitch ){
  11178. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11179. return;
  11180. }
  11181. if (sd->state.storage_flag == 1)
  11182. storage_storageaddfromcart(sd, &sd->storage, idx, amount);
  11183. else if (sd->state.storage_flag == 2)
  11184. storage_guild_storageaddfromcart(sd, idx, amount);
  11185. else if (sd->state.storage_flag == 3)
  11186. storage_storageaddfromcart(sd, &sd->premiumStorage, idx, amount);
  11187. }
  11188. /// Request to move an item from storage to cart (CZ_MOVE_ITEM_FROM_STORE_TO_CART).
  11189. /// 0128 <index>.W <amount>.L
  11190. void clif_parse_MoveFromKafraToCart(int32 fd, map_session_data *sd){
  11191. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11192. int32 idx = RFIFOW(fd,info->pos[0]) - 1;
  11193. int32 amount = RFIFOL(fd,info->pos[1]);
  11194. if( sd->state.vending )
  11195. return;
  11196. if (!pc_iscarton(sd))
  11197. return;
  11198. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11199. return;
  11200. if (sd->state.storage_flag == 1)
  11201. storage_storagegettocart(sd, &sd->storage, idx, amount);
  11202. else
  11203. if (sd->state.storage_flag == 2)
  11204. storage_guild_storagegettocart(sd, idx, amount);
  11205. else if (sd->state.storage_flag == 3)
  11206. storage_storagegettocart(sd, &sd->premiumStorage, idx, amount);
  11207. }
  11208. /// Request to close storage (CZ_CLOSE_STORE).
  11209. /// 00f7
  11210. void clif_parse_CloseKafra(int32 fd, map_session_data *sd)
  11211. {
  11212. if( sd->state.storage_flag == 1 )
  11213. storage_storageclose(sd);
  11214. else
  11215. if( sd->state.storage_flag == 2 )
  11216. storage_guild_storageclose(sd);
  11217. else if( sd->state.storage_flag == 3 )
  11218. storage_premiumStorage_close(sd);
  11219. }
  11220. /// Displays kafra storage password dialog (ZC_REQ_STORE_PASSWORD).
  11221. /// 023a <info>.W
  11222. /// info:
  11223. /// 0 = password has not been set yet
  11224. /// 1 = storage is password-protected
  11225. /// 8 = too many wrong passwords
  11226. /// ? = ignored
  11227. /// NOTE: This packet is only available on certain non-kRO clients.
  11228. void clif_storagepassword(map_session_data* sd, int16 info)
  11229. {
  11230. int32 fd = sd->fd;
  11231. WFIFOHEAD(fd,packet_len(0x23a));
  11232. WFIFOW(fd,0) = 0x23a;
  11233. WFIFOW(fd,2) = info;
  11234. WFIFOSET(fd,packet_len(0x23a));
  11235. }
  11236. /// Answer to the kafra storage password dialog (CZ_ACK_STORE_PASSWORD).
  11237. /// 023b <type>.W <password>.16B <new password>.16B
  11238. /// type:
  11239. /// 2 = change password
  11240. /// 3 = check password
  11241. /// NOTE: This packet is only available on certain non-kRO clients.
  11242. void clif_parse_StoragePassword(int32 fd, map_session_data *sd){ //@TODO
  11243. // TODO: shuffle packet
  11244. // struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11245. // int32 type = RFIFOW(fd,info->pos[0]);
  11246. // char* password = RFIFOP(fd,info->pos[1]);
  11247. // char* new_password = RFIFOP(fd,info->pos[2]);
  11248. // storage_changepass(sd,password,new_password);
  11249. }
  11250. /// Result of kafra storage password validation (ZC_RESULT_STORE_PASSWORD).
  11251. /// 023c <result>.W <error count>.W
  11252. /// result:
  11253. /// 4 = password change success
  11254. /// 5 = password change failure
  11255. /// 6 = password check success
  11256. /// 7 = password check failure
  11257. /// 8 = too many wrong passwords
  11258. /// ? = ignored
  11259. /// NOTE: This packet is only available on certain non-kRO clients.
  11260. void clif_storagepassword_result(map_session_data* sd, int16 result, int16 error_count)
  11261. {
  11262. int32 fd = sd->fd;
  11263. WFIFOHEAD(fd,packet_len(0x23c));
  11264. WFIFOW(fd,0) = 0x23c;
  11265. WFIFOW(fd,2) = result;
  11266. WFIFOW(fd,4) = error_count;
  11267. WFIFOSET(fd,packet_len(0x23c));
  11268. }
  11269. /// Party creation request
  11270. /// 00f9 <party name>.24B (CZ_MAKE_GROUP)
  11271. void clif_parse_CreateParty(int32 fd, map_session_data *sd){
  11272. if( sd == nullptr ){
  11273. return;
  11274. }
  11275. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11276. name[NAME_LENGTH-1] = '\0';
  11277. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11278. clif_displaymessage(fd, msg_txt(sd,227));
  11279. return;
  11280. }
  11281. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11282. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11283. return;
  11284. }
  11285. party_create( *sd, name, 0, 0 );
  11286. }
  11287. /// 01e8 <party name>.24B <item pickup rule>.B <item share rule>.B (CZ_MAKE_GROUP2)
  11288. void clif_parse_CreateParty2(int32 fd, map_session_data *sd){
  11289. if( sd == nullptr ){
  11290. return;
  11291. }
  11292. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11293. char* name = RFIFOCP(fd,info->pos[0]);
  11294. int32 item1 = RFIFOB(fd,info->pos[1]);
  11295. int32 item2 = RFIFOB(fd,info->pos[2]);
  11296. name[NAME_LENGTH-1] = '\0';
  11297. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11298. clif_displaymessage(fd, msg_txt(sd,227));
  11299. return;
  11300. }
  11301. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11302. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11303. return;
  11304. }
  11305. party_create( *sd, name, item1, item2 );
  11306. }
  11307. /// Party invitation request by account id
  11308. /// 00fc <account id>.L (CZ_REQ_JOIN_GROUP)
  11309. void clif_parse_PartyInvite( int32 fd, map_session_data *sd ){
  11310. if( sd == nullptr ){
  11311. return;
  11312. }
  11313. const PACKET_CZ_REQ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11314. party_invite( *sd, map_id2sd( p->AID ) );
  11315. }
  11316. /// Party invitation request by name
  11317. /// 02c4 <char name>.24B (CZ_PARTY_JOIN_REQ)
  11318. void clif_parse_PartyInvite2( int32 fd, map_session_data *sd ){
  11319. #if PACKETVER >= 20070227
  11320. if( sd == nullptr ){
  11321. return;
  11322. }
  11323. const PACKET_CZ_PARTY_JOIN_REQ* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ*>( RFIFOP( fd, 0 ) );
  11324. char name[NAME_LENGTH];
  11325. safestrncpy( name, p->name, sizeof( name ) );
  11326. party_invite( *sd, map_nick2sd( name, false ) );
  11327. #endif
  11328. }
  11329. /// Party invitation reply
  11330. /// 00ff <party id>.L <flag>.L (CZ_JOIN_GROUP)
  11331. /// flag:
  11332. /// 0 = reject
  11333. /// 1 = accept
  11334. void clif_parse_ReplyPartyInvite( int32 fd, map_session_data *sd ){
  11335. if( sd == nullptr ){
  11336. return;
  11337. }
  11338. const PACKET_CZ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11339. party_reply_invite( *sd, p->party_id, p->flag );
  11340. }
  11341. /// Party invitation reply
  11342. /// 02c7 <party id>.L <flag>.B (CZ_PARTY_JOIN_REQ_ACK)
  11343. /// flag:
  11344. /// 0 = reject
  11345. /// 1 = accept
  11346. void clif_parse_ReplyPartyInvite2( int32 fd, map_session_data *sd ){
  11347. #if PACKETVER >= 20070227
  11348. if( sd == nullptr ){
  11349. return;
  11350. }
  11351. const PACKET_CZ_PARTY_JOIN_REQ_ACK* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ_ACK*>( RFIFOP( fd, 0 ) );
  11352. party_reply_invite( *sd, p->party_id, p->flag );
  11353. #endif
  11354. }
  11355. /// Request to leave party.
  11356. /// 0100 (CZ_REQ_LEAVE_GROUP)
  11357. void clif_parse_LeaveParty( int32 fd, map_session_data *sd ){
  11358. if( sd == nullptr ){
  11359. return;
  11360. }
  11361. //const PACKET_CZ_REQ_LEAVE_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GROUP*>( RFIFOP( fd, 0 ) );
  11362. party_leave( *sd, true );
  11363. }
  11364. /// Request to expel a party member.
  11365. /// 0103 <account id>.L <char name>.24B (CZ_REQ_EXPEL_GROUP_MEMBER)
  11366. void clif_parse_RemovePartyMember( int32 fd, map_session_data* sd ){
  11367. if( sd == nullptr ){
  11368. return;
  11369. }
  11370. const PACKET_CZ_REQ_EXPEL_GROUP_MEMBER* p = reinterpret_cast<PACKET_CZ_REQ_EXPEL_GROUP_MEMBER*>( RFIFOP( fd, 0 ) );
  11371. party_removemember( *sd, p->AID, p->name );
  11372. }
  11373. /// Request to change party options.
  11374. /// 0102 <exp share rule>.L (CZ_CHANGE_GROUPEXPOPTION)
  11375. /// 07d7 <exp share rule>.L <item pickup rule>.B <item share rule>.B (CZ_GROUPINFO_CHANGE_V2)
  11376. void clif_parse_PartyChangeOption(int32 fd, map_session_data *sd)
  11377. {
  11378. struct party_data *p;
  11379. int32 i,expflag;
  11380. int32 cmd = RFIFOW(fd,0);
  11381. struct s_packet_db* info = &packet_db[cmd];
  11382. if( !sd->status.party_id )
  11383. return;
  11384. p = party_search(sd->status.party_id);
  11385. if( p == nullptr )
  11386. return;
  11387. ARR_FIND( 0, MAX_PARTY, i, p->data[i].sd == sd );
  11388. if( i == MAX_PARTY )
  11389. return; //Shouldn't happen
  11390. if( !p->party.member[i].leader )
  11391. return;
  11392. expflag = RFIFOL(fd,info->pos[0]);
  11393. if(cmd == 0x0102){ //Client can't change the item-field
  11394. party_changeoption(sd, expflag, p->party.item);
  11395. }
  11396. else {
  11397. int32 itemflag = (RFIFOB(fd,info->pos[1])?1:0)|(RFIFOB(fd,info->pos[2])?2:0);
  11398. party_changeoption(sd, expflag, itemflag);
  11399. }
  11400. }
  11401. /// Validates and processes party messages (CZ_REQUEST_CHAT_PARTY).
  11402. /// 0108 <packet len>.W <text>.?B (<name> : <message>) 00
  11403. void clif_parse_PartyMessage(int32 fd, map_session_data* sd){
  11404. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11405. // validate packet and retrieve name and message
  11406. if( !clif_process_message( sd, false, name, message, output ) )
  11407. return;
  11408. party_send_message(sd, output, strlen(output) + 1 );
  11409. }
  11410. /// Changes Party Leader (CZ_CHANGE_GROUP_MASTER).
  11411. /// 07da <account id>.L
  11412. void clif_parse_PartyChangeLeader(int32 fd, map_session_data* sd){
  11413. party_changeleader(sd, map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0])),nullptr);
  11414. }
  11415. /// Party Booking in KRO [Spiria]
  11416. ///
  11417. /// Request to register a party booking advertisment (CZ_PARTY_BOOKING_REQ_REGISTER).
  11418. /// 0802 <level>.W <map id>.W { <job>.W }*6
  11419. void clif_parse_PartyBookingRegisterReq(int32 fd, map_session_data* sd){
  11420. // TODO: shuffle packet
  11421. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11422. int16 level = RFIFOW(fd,info->pos[0]);
  11423. int16 mapid = RFIFOW(fd,info->pos[1]);
  11424. int32 idxpbj = info->pos[2];
  11425. int16 job[MAX_PARTY_BOOKING_JOBS];
  11426. int32 i;
  11427. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11428. job[i] = RFIFOB(fd,idxpbj+i*2);
  11429. party_booking_register(sd, level, mapid, job);
  11430. }
  11431. /// Result of request to register a party booking advertisment (ZC_PARTY_BOOKING_ACK_REGISTER).
  11432. /// 0803 <result>.W
  11433. /// result:
  11434. /// 0 = success
  11435. /// 1 = failure
  11436. /// 2 = already registered
  11437. void clif_PartyBookingRegisterAck(map_session_data *sd, int32 flag)
  11438. {
  11439. int32 fd = sd->fd;
  11440. WFIFOHEAD(fd,packet_len(0x803));
  11441. WFIFOW(fd,0) = 0x803;
  11442. WFIFOW(fd,2) = flag;
  11443. WFIFOSET(fd,packet_len(0x803));
  11444. }
  11445. /// Request to search for party booking advertisments (CZ_PARTY_BOOKING_REQ_SEARCH).
  11446. /// 0804 <level>.W <map id>.W <job>.W <last index>.L <result count>.W
  11447. void clif_parse_PartyBookingSearchReq(int32 fd, map_session_data* sd){
  11448. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11449. int16 level = RFIFOW(fd,info->pos[0]);
  11450. int16 mapid = RFIFOW(fd,info->pos[1]);
  11451. int16 job = RFIFOW(fd,info->pos[2]);
  11452. unsigned long lastindex = RFIFOL(fd,info->pos[3]);
  11453. int16 resultcount = RFIFOW(fd,info->pos[4]);
  11454. party_booking_search(sd, level, mapid, job, lastindex, resultcount);
  11455. }
  11456. /// Party booking search results (ZC_PARTY_BOOKING_ACK_SEARCH).
  11457. /// 0805 <packet len>.W <more results>.B { <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6 }*
  11458. /// more results:
  11459. /// 0 = no
  11460. /// 1 = yes
  11461. void clif_PartyBookingSearchAck(int32 fd, struct party_booking_ad_info** results, int32 count, bool more_result)
  11462. {
  11463. int32 i, j;
  11464. int32 size = sizeof(struct party_booking_ad_info); // structure size (48)
  11465. struct party_booking_ad_info *pb_ad;
  11466. WFIFOHEAD(fd,size*count + 5);
  11467. WFIFOW(fd,0) = 0x805;
  11468. WFIFOW(fd,2) = size*count + 5;
  11469. WFIFOB(fd,4) = more_result;
  11470. for(i=0; i<count; i++)
  11471. {
  11472. pb_ad = results[i];
  11473. WFIFOL(fd,i*size+5) = pb_ad->index;
  11474. safestrncpy(WFIFOCP(fd,i*size+9),pb_ad->charname,NAME_LENGTH);
  11475. WFIFOL(fd,i*size+33) = pb_ad->starttime; // FIXME: This is expire time
  11476. WFIFOW(fd,i*size+37) = pb_ad->p_detail.level;
  11477. WFIFOW(fd,i*size+39) = pb_ad->p_detail.mapid;
  11478. for(j=0; j<MAX_PARTY_BOOKING_JOBS; j++)
  11479. WFIFOW(fd,i*size+41+j*2) = pb_ad->p_detail.job[j];
  11480. }
  11481. WFIFOSET(fd,WFIFOW(fd,2));
  11482. }
  11483. /// Request to delete own party booking advertisment (CZ_PARTY_BOOKING_REQ_DELETE).
  11484. /// 0806
  11485. void clif_parse_PartyBookingDeleteReq(int32 fd, map_session_data* sd)
  11486. {
  11487. if(party_booking_delete(sd))
  11488. clif_PartyBookingDeleteAck(sd, 0);
  11489. }
  11490. /// Result of request to delete own party booking advertisment (ZC_PARTY_BOOKING_ACK_DELETE).
  11491. /// 0807 <result>.W
  11492. /// result:
  11493. /// 0 = success
  11494. /// 1 = success (auto-removed expired ad)
  11495. /// 2 = failure
  11496. /// 3 = nothing registered
  11497. void clif_PartyBookingDeleteAck(map_session_data* sd, int32 flag)
  11498. {
  11499. int32 fd = sd->fd;
  11500. WFIFOHEAD(fd,packet_len(0x807));
  11501. WFIFOW(fd,0) = 0x807;
  11502. WFIFOW(fd,2) = flag;
  11503. WFIFOSET(fd,packet_len(0x807));
  11504. }
  11505. /// Request to update party booking advertisment (CZ_PARTY_BOOKING_REQ_UPDATE).
  11506. /// 0808 { <job>.W }*6
  11507. void clif_parse_PartyBookingUpdateReq(int32 fd, map_session_data* sd)
  11508. {
  11509. int16 job[MAX_PARTY_BOOKING_JOBS];
  11510. int32 i;
  11511. int32 idxpbu = packet_db[RFIFOW(fd,0)].pos[0];
  11512. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11513. job[i] = RFIFOW(fd,idxpbu+i*2);
  11514. party_booking_update(sd, job);
  11515. }
  11516. /// Notification about new party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_INSERT).
  11517. /// 0809 <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6
  11518. void clif_PartyBookingInsertNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11519. {
  11520. int32 i;
  11521. uint8 buf[38+MAX_PARTY_BOOKING_JOBS*2];
  11522. if(pb_ad == nullptr) return;
  11523. WBUFW(buf,0) = 0x809;
  11524. WBUFL(buf,2) = pb_ad->index;
  11525. safestrncpy(WBUFCP(buf,6),pb_ad->charname,NAME_LENGTH);
  11526. WBUFL(buf,30) = pb_ad->starttime; // FIXME: This is expire time
  11527. WBUFW(buf,34) = pb_ad->p_detail.level;
  11528. WBUFW(buf,36) = pb_ad->p_detail.mapid;
  11529. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11530. WBUFW(buf,38+i*2) = pb_ad->p_detail.job[i];
  11531. clif_send(buf, packet_len(0x809), &sd->bl, ALL_CLIENT);
  11532. }
  11533. /// Notification about updated party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_UPDATE).
  11534. /// 080a <index>.L { <job>.W }*6
  11535. void clif_PartyBookingUpdateNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11536. {
  11537. int32 i;
  11538. uint8 buf[6+MAX_PARTY_BOOKING_JOBS*2];
  11539. if(pb_ad == nullptr) return;
  11540. WBUFW(buf,0) = 0x80a;
  11541. WBUFL(buf,2) = pb_ad->index;
  11542. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11543. WBUFW(buf,6+i*2) = pb_ad->p_detail.job[i];
  11544. clif_send(buf,packet_len(0x80a),&sd->bl,ALL_CLIENT); // Now UPDATE all client.
  11545. }
  11546. /// Notification about deleted party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_DELETE).
  11547. /// 080b <index>.L
  11548. void clif_PartyBookingDeleteNotify(map_session_data* sd, int32 index)
  11549. {
  11550. uint8 buf[6];
  11551. WBUFW(buf,0) = 0x80b;
  11552. WBUFL(buf,2) = index;
  11553. clif_send(buf, packet_len(0x80b), &sd->bl, ALL_CLIENT); // Now UPDATE all client.
  11554. }
  11555. /// Request to close own vending (CZ_REQ_CLOSESTORE).
  11556. /// 012e
  11557. void clif_parse_CloseVending(int32 fd, map_session_data* sd)
  11558. {
  11559. vending_closevending(sd);
  11560. }
  11561. /// Request to open a vending shop (CZ_REQ_BUY_FROMMC).
  11562. /// 0130 <account id>.L
  11563. void clif_parse_VendingListReq(int32 fd, map_session_data* sd)
  11564. {
  11565. if( sd->npc_id ) {// using an NPC
  11566. return;
  11567. }
  11568. vending_vendinglistreq(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11569. }
  11570. /// Shop item(s) purchase request.
  11571. /// 0134 <packet len>.W <account id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC)
  11572. void clif_parse_PurchaseReq(int32 fd, map_session_data* sd){
  11573. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC*>( RFIFOP( fd, 0 ) );
  11574. vending_purchasereq( sd, p->AID, sd->vended_id, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11575. // whether it fails or not, the buy window is closed
  11576. sd->vended_id = 0;
  11577. }
  11578. /// Shop item(s) purchase request.
  11579. /// 0801 <packet len>.W <account id>.L <unique id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC2)
  11580. void clif_parse_PurchaseReq2(int32 fd, map_session_data* sd){
  11581. #if PACKETVER >= 20100105
  11582. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2*>( RFIFOP( fd, 0 ) );
  11583. vending_purchasereq( sd, p->AID, p->UniqueID, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11584. // whether it fails or not, the buy window is closed
  11585. sd->vended_id = 0;
  11586. #endif
  11587. }
  11588. /// Confirm or cancel the shop preparation window.
  11589. /// 012f <packet len>.W <shop name>.80B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE)
  11590. /// 01b2 <packet len>.W <shop name>.80B <result>.B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE2)
  11591. /// result:
  11592. /// 0 = canceled
  11593. /// 1 = open
  11594. void clif_parse_OpenVending(int32 fd, map_session_data* sd){
  11595. if( sd == nullptr ){
  11596. return;
  11597. }
  11598. int32 cmd = RFIFOW(fd,0);
  11599. struct s_packet_db* info = &packet_db[cmd];
  11600. int16 len = (int16)RFIFOW(fd,info->pos[0]);
  11601. const char* message = RFIFOCP(fd,info->pos[1]);
  11602. const uint8* data = (uint8*)RFIFOP(fd,info->pos[3]);
  11603. if(cmd == 0x12f){ // (CZ_REQ_OPENSTORE)
  11604. len -= 84;
  11605. }
  11606. else { //(CZ_REQ_OPENSTORE2)
  11607. bool flag;
  11608. len -= 85;
  11609. flag = RFIFOB(fd,info->pos[2]) != 0;
  11610. if (!flag) {
  11611. sd->state.prevend = 0;
  11612. sd->state.workinprogress = WIP_DISABLE_NONE;
  11613. }
  11614. }
  11615. if( sd->sc.getSCE(SC_NOCHAT) && sd->sc.getSCE(SC_NOCHAT)->val1&MANNER_NOROOM )
  11616. return;
  11617. if( map_getmapflag(sd->bl.m, MF_NOVENDING) ) {
  11618. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  11619. return;
  11620. }
  11621. if( map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNOVENDING) ) {
  11622. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  11623. return;
  11624. }
  11625. if( message[0] == '\0' ) // invalid input
  11626. return;
  11627. vending_openvending(*sd, message, data, len/8, nullptr);
  11628. }
  11629. /// Guild creation request (CZ_REQ_MAKE_GUILD).
  11630. /// 0165 <char id>.L <guild name>.24B
  11631. void clif_parse_CreateGuild(int32 fd,map_session_data *sd){
  11632. //int32 charid = RFIFOL(fd,packet_db[cmd].pos[0]);
  11633. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11634. name[NAME_LENGTH-1] = '\0';
  11635. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11636. clif_displaymessage(fd, msg_txt(sd,228));
  11637. return;
  11638. }
  11639. if(sd->clan){
  11640. // Should display a clientside message "You are currently joined in Clan !!" so we ignore it
  11641. return;
  11642. }
  11643. guild_create( *sd, name );
  11644. }
  11645. /// Request for guild window interface permissions
  11646. /// 014d (CZ_REQ_GUILD_MENUINTERFACE)
  11647. static void clif_parse_GuildCheckMaster(int32 fd, map_session_data *sd){
  11648. if(sd == nullptr)
  11649. return;
  11650. clif_guild_masterormember(*sd);
  11651. }
  11652. /// Request for guild window information (CZ_REQ_GUILD_MENU).
  11653. /// 014f <type>.L
  11654. /// type:
  11655. /// 0 = basic info
  11656. /// 1 = member manager
  11657. /// 2 = positions
  11658. /// 3 = skills
  11659. /// 4 = expulsion list
  11660. /// 5 = unknown (GM_ALLGUILDLIST)
  11661. /// 6 = notice
  11662. void clif_parse_GuildRequestInfo(int32 fd, map_session_data *sd)
  11663. {
  11664. int32 type = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11665. if( !sd->status.guild_id && !sd->bg_id )
  11666. return;
  11667. switch( type )
  11668. {
  11669. case 0: // Basic Information Guild, hostile alliance information
  11670. clif_guild_basicinfo( *sd );
  11671. clif_guild_allianceinfo(*sd);
  11672. clif_guild_castle_list(*sd);
  11673. break;
  11674. case 1: // Members list, list job title
  11675. clif_guild_positionnamelist(sd);
  11676. clif_guild_memberlist( *sd );
  11677. break;
  11678. case 2: // List job title, title information list
  11679. clif_guild_positionnamelist(sd);
  11680. clif_guild_positioninfolist(*sd);
  11681. break;
  11682. case 3: // Skill list
  11683. clif_guild_skillinfo( *sd );
  11684. break;
  11685. case 4: // Expulsion list
  11686. clif_guild_expulsionlist(*sd);
  11687. break;
  11688. default:
  11689. ShowError("clif: guild request info: unknown type %d\n", type);
  11690. break;
  11691. }
  11692. }
  11693. /// Request to update guild positions (CZ_REG_CHANGE_GUILD_POSITIONINFO).
  11694. /// 0161 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <name>.24B }*
  11695. void clif_parse_GuildChangePositionInfo(int32 fd, map_session_data *sd)
  11696. {
  11697. int32 i;
  11698. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11699. int32 len = RFIFOW(fd,info->pos[0]);
  11700. int32 idxgpos = info->pos[1];
  11701. if(!sd->state.gmaster_flag)
  11702. return;
  11703. for(i = idxgpos; i < len; i += 40 ){
  11704. guild_change_position(sd->status.guild_id, RFIFOL(fd,i), RFIFOL(fd,i+4), RFIFOL(fd,i+12), RFIFOCP(fd,i+16));
  11705. }
  11706. }
  11707. /// Request to update the position of guild members.
  11708. /// 0155 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (CZ_REQ_CHANGE_MEMBERPOS)
  11709. void clif_parse_GuildChangeMemberPosition( int32 fd, map_session_data *sd ){
  11710. if(!sd->state.gmaster_flag)
  11711. return;
  11712. const PACKET_CZ_REQ_CHANGE_MEMBERPOS* p = reinterpret_cast<PACKET_CZ_REQ_CHANGE_MEMBERPOS*>( RFIFOP( fd, 0 ) );
  11713. int16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  11714. for( int16 i = 0; i < entries; i++ ){
  11715. const PACKET_CZ_REQ_CHANGE_MEMBERPOS_sub& entry = p->list[i];
  11716. // Guild leadership change
  11717. if( entry.position == 0 ){
  11718. if( !battle_config.guild_leaderchange_woe && is_agit_start() ){
  11719. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_IN_SIEGE_TIME );
  11720. return;
  11721. }
  11722. if( battle_config.guild_leaderchange_delay && DIFF_TICK( time( nullptr ),sd->guild->guild.last_leader_change ) < battle_config.guild_leaderchange_delay ){
  11723. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_NOT_TIME );
  11724. return;
  11725. }
  11726. guild_gm_change( sd->status.guild_id, entry.CID );
  11727. // No further entries will be processed - the requesting player lost his guild master status
  11728. return;
  11729. }else if( entry.position > 0 ){
  11730. guild_change_memberposition( sd->status.guild_id, entry.AID, entry.CID, entry.position );
  11731. }
  11732. }
  11733. }
  11734. /// Request for guild emblem data (CZ_REQ_GUILD_EMBLEM_IMG).
  11735. /// 0151 <guild id>.L
  11736. void clif_parse_GuildRequestEmblem(int32 fd,map_session_data *sd)
  11737. {
  11738. int32 guild_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11739. auto g = guild_search(guild_id);
  11740. if (g)
  11741. clif_guild_emblem(*sd, g->guild);
  11742. }
  11743. /// Validates data of a guild emblem (compressed bitmap)
  11744. enum e_result_validate_emblem { // Used as Result for clif_validate_emblem
  11745. EMBVALIDATE_SUCCESS = 0,
  11746. EMBVALIDATE_ERR_RAW_FILEFORMAT, // Invalid File Format (Error in zlib/decompression or malformed BMP header)
  11747. EMBVALIDATE_ERR_TRANSPARENCY // uploaded emblem does not met the requirements of inter_config.emblem_transparency_limit
  11748. };
  11749. static enum e_result_validate_emblem clif_validate_emblem(const uint8* emblem, unsigned long emblem_len)
  11750. {
  11751. uint8 buf[1800]; // no well-formed emblem bitmap is larger than 1782 (24 bit) / 1654 (8 bit) bytes
  11752. unsigned long buf_len = sizeof(buf);
  11753. int32 offset = 0;
  11754. if(!(( decode_zip(buf, &buf_len, emblem, emblem_len) == 0 && buf_len >= 18 ) // sizeof(BITMAPFILEHEADER) + sizeof(biSize) of the following info header struct
  11755. && RBUFW(buf,0) == 0x4d42 // BITMAPFILEHEADER.bfType (signature)
  11756. && RBUFL(buf,2) == buf_len // BITMAPFILEHEADER.bfSize (file size)
  11757. && (offset = RBUFL(buf,10)) < buf_len // BITMAPFILEHEADER.bfOffBits (offset to bitmap bits)
  11758. ))
  11759. return EMBVALIDATE_ERR_RAW_FILEFORMAT;
  11760. if(inter_config.emblem_transparency_limit != 100) {
  11761. int32 i, transcount = 1, tmp[3];
  11762. for(i = offset; i < buf_len-1; i++) {
  11763. int32 j = i%3;
  11764. tmp[j] = RBUFL(buf,i);
  11765. if(j == 2 && (tmp[0] == 0xFFFF00FF) && (tmp[1] == 0xFFFF00) && (tmp[2] == 0xFF00FFFF)) //if pixel is transparent
  11766. transcount++;
  11767. }
  11768. if(((transcount*300)/(buf_len-offset)) > inter_config.emblem_transparency_limit) //convert in % to chk
  11769. return EMBVALIDATE_ERR_TRANSPARENCY;
  11770. }
  11771. return EMBVALIDATE_SUCCESS;
  11772. }
  11773. /// Request to update the guild emblem (CZ_REGISTER_GUILD_EMBLEM_IMG).
  11774. /// 0153 <packet len>.W <emblem data>.?B
  11775. void clif_parse_GuildChangeEmblem(int32 fd,map_session_data *sd){
  11776. if( sd == nullptr ){
  11777. return;
  11778. }
  11779. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11780. unsigned long emblem_len = RFIFOW(fd,info->pos[0])-4;
  11781. const uint8* emblem = RFIFOP(fd,info->pos[1]);
  11782. int32 emb_val=0;
  11783. if( !emblem_len || !sd->state.gmaster_flag )
  11784. return;
  11785. if(!(inter_config.emblem_woe_change) && is_agit_start() ){
  11786. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,385),false,SELF); //"You not allowed to change emblem during woe"
  11787. return;
  11788. }
  11789. emb_val = clif_validate_emblem(emblem, emblem_len);
  11790. if(emb_val == EMBVALIDATE_ERR_RAW_FILEFORMAT){
  11791. ShowWarning("clif_parse_GuildChangeEmblem: Rejected malformed guild emblem (size=%lu, accound_id=%d, char_id=%d, guild_id=%d).\n", emblem_len, sd->status.account_id, sd->status.char_id, sd->status.guild_id);
  11792. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,386),false,SELF); //"The chosen emblem was detected invalid\n"
  11793. return;
  11794. } else if(emb_val == EMBVALIDATE_ERR_TRANSPARENCY){
  11795. char output[128];
  11796. safesnprintf(output,sizeof(output),msg_txt(sd,387),inter_config.emblem_transparency_limit);
  11797. clif_messagecolor(&sd->bl,color_table[COLOR_RED],output,false,SELF); //"The chosen emblem was detected invalid as it contain too much transparency (limit=%d)\n"
  11798. return;
  11799. }
  11800. guild_change_emblem( *sd, emblem_len, (const char*)emblem );
  11801. }
  11802. /// Request to update the guild emblem id
  11803. /// 0b46 <guild id>.L <version>.L
  11804. void clif_parse_GuildChangeEmblem2(int32 fd, map_session_data* sd) {
  11805. if( sd == nullptr ){
  11806. return;
  11807. }
  11808. #if PACKETVER >= 20190724
  11809. const PACKET_CZ_REQ_ADD_NEW_EMBLEM* p = reinterpret_cast<PACKET_CZ_REQ_ADD_NEW_EMBLEM*>( RFIFOP( fd, 0 ) );
  11810. auto &g = sd->guild;
  11811. if (!g || g->guild.guild_id != p->guild_id)
  11812. return;
  11813. if (!sd->state.gmaster_flag)
  11814. return;
  11815. if (!inter_config.emblem_woe_change && is_agit_start()) {
  11816. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 385), false, SELF); //"You not allowed to change emblem during woe"
  11817. return;
  11818. }
  11819. guild_change_emblem_version( *sd, p->version );
  11820. #endif
  11821. }
  11822. /// Guild notice update request (CZ_GUILD_NOTICE).
  11823. /// 016e <guild id>.L <msg1>.60B <msg2>.120B
  11824. void clif_parse_GuildChangeNotice(int32 fd, map_session_data* sd){
  11825. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11826. int32 guild_id = RFIFOL(fd,info->pos[0]);
  11827. char* msg1 = RFIFOCP(fd,info->pos[1]);
  11828. char* msg2 = RFIFOCP(fd,info->pos[2]);
  11829. if(!sd->state.gmaster_flag)
  11830. return;
  11831. // compensate for some client defects when using multilanguage mode
  11832. if (msg1[0] == '|' && msg1[3] == '|') msg1+= 3; // skip duplicate marker
  11833. if (msg2[0] == '|' && msg2[3] == '|') msg2+= 3; // skip duplicate marker
  11834. if (msg2[0] == '|') msg2[strnlen(msg2, MAX_GUILDMES2)-1] = '\0'; // delete extra space at the end of string
  11835. guild_change_notice(sd, guild_id, msg1, msg2);
  11836. }
  11837. /// Guild invite request.
  11838. /// 0168 <account id>.L <inviter account id>.L <inviter char id>.L (CZ_REQ_JOIN_GUILD)
  11839. void clif_parse_GuildInvite( int32 fd,map_session_data *sd ){
  11840. if( sd == nullptr ){
  11841. return;
  11842. }
  11843. const PACKET_CZ_REQ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11844. guild_invite( *sd, map_id2sd( p->AID ) );
  11845. }
  11846. /// Guild invite request (/guildinvite)
  11847. /// 0916 <char name>.24B (CZ_REQ_JOIN_GUILD2)
  11848. void clif_parse_GuildInvite2( int32 fd, map_session_data *sd ){
  11849. #if PACKETVER >= 20120410
  11850. if( sd == nullptr ){
  11851. return;
  11852. }
  11853. const PACKET_CZ_REQ_JOIN_GUILD2* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD2*>( RFIFOP( fd, 0 ) );
  11854. char nick[NAME_LENGTH] = {0};
  11855. safestrncpy( nick, p->name, NAME_LENGTH );
  11856. guild_invite( *sd, map_nick2sd( nick, false ) );
  11857. #endif
  11858. }
  11859. /// Answer to guild invitation.
  11860. /// 016b <guild id>.L <answer>.L (CZ_JOIN_GUILD)
  11861. /// answer:
  11862. /// 0 = refuse
  11863. /// 1 = accept
  11864. void clif_parse_GuildReplyInvite( int32 fd, map_session_data *sd ){
  11865. if( sd == nullptr ){
  11866. return;
  11867. }
  11868. const PACKET_CZ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11869. guild_reply_invite( *sd, p->guild_id, p->answer );
  11870. }
  11871. /// Request to leave guild.
  11872. /// 0159 <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_LEAVE_GUILD)
  11873. void clif_parse_GuildLeave(int32 fd,map_session_data *sd){
  11874. if( sd == nullptr ){
  11875. return;
  11876. }
  11877. const PACKET_CZ_REQ_LEAVE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GUILD*>( RFIFOP( fd, 0 ) );
  11878. guild_leave( *sd, p->guild_id, p->AID, p->CID, p->message );
  11879. }
  11880. /// Request to expel a member of a guild.
  11881. /// 015b <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_BAN_GUILD)
  11882. void clif_parse_GuildExpulsion(int32 fd,map_session_data *sd){
  11883. if( sd == nullptr ){
  11884. return;
  11885. }
  11886. const PACKET_CZ_REQ_BAN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_BAN_GUILD*>( RFIFOP( fd, 0 ) );
  11887. guild_expulsion( *sd, p->guild_id, p->AID, p->CID, p->message );
  11888. }
  11889. /// Validates and processes guild messages (CZ_GUILD_CHAT).
  11890. /// 017e <packet len>.W <text>.?B (<name> : <message>) 00
  11891. void clif_parse_GuildMessage(int32 fd, map_session_data* sd){
  11892. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11893. // validate packet and retrieve name and message
  11894. if( !clif_process_message( sd, false, name, message, output ) )
  11895. return;
  11896. if( sd->bg_id )
  11897. bg_send_message(sd, output, strlen(output) );
  11898. else
  11899. guild_send_message(sd, output, strlen(output) );
  11900. }
  11901. /// Guild alliance request (CZ_REQ_ALLY_GUILD).
  11902. /// 0170 <account id>.L <inviter account id>.L <inviter char id>.L
  11903. void clif_parse_GuildRequestAlliance(int32 fd, map_session_data *sd)
  11904. {
  11905. map_session_data *t_sd;
  11906. if(!sd->state.gmaster_flag)
  11907. return;
  11908. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11909. clif_displaymessage(fd, msg_txt(sd,228));
  11910. return;
  11911. }
  11912. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11913. //inv_aid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11914. //inv_cid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[2]);
  11915. // @noask [LuzZza]
  11916. if(t_sd && t_sd->state.noask) {
  11917. clif_noask_sub( *sd, *t_sd, 396 ); // Autorejected alliance request from %s.
  11918. return;
  11919. }
  11920. guild_reqalliance(sd,t_sd);
  11921. }
  11922. /// Answer to a guild alliance request (CZ_ALLY_GUILD).
  11923. /// 0172 <inviter account id>.L <answer>.L
  11924. /// answer:
  11925. /// 0 = refuse
  11926. /// 1 = accept
  11927. void clif_parse_GuildReplyAlliance(int32 fd, map_session_data *sd){
  11928. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11929. guild_reply_reqalliance(sd,
  11930. RFIFOL(fd,info->pos[0]),
  11931. RFIFOL(fd,info->pos[1]));
  11932. }
  11933. /// Request to delete a guild alliance or opposition (CZ_REQ_DELETE_RELATED_GUILD).
  11934. /// 0183 <opponent guild id>.L <relation>.L
  11935. /// relation:
  11936. /// 0 = Ally
  11937. /// 1 = Enemy
  11938. void clif_parse_GuildDelAlliance(int32 fd, map_session_data *sd){
  11939. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11940. if(!sd->state.gmaster_flag)
  11941. return;
  11942. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11943. clif_displaymessage(fd, msg_txt(sd,228));
  11944. return;
  11945. }
  11946. guild_delalliance(sd,
  11947. RFIFOL(fd,info->pos[0]),
  11948. RFIFOL(fd,info->pos[1]));
  11949. }
  11950. /// Request to set a guild as opposition (CZ_REQ_HOSTILE_GUILD).
  11951. /// 0180 <account id>.L
  11952. void clif_parse_GuildOpposition(int32 fd, map_session_data *sd)
  11953. {
  11954. map_session_data *t_sd;
  11955. if(!sd->state.gmaster_flag)
  11956. return;
  11957. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11958. clif_displaymessage(fd, msg_txt(sd,228));
  11959. return;
  11960. }
  11961. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11962. // @noask [LuzZza]
  11963. if(t_sd && t_sd->state.noask) {
  11964. clif_noask_sub( *sd, *t_sd, 397 ); // Autorejected opposition request from %s.
  11965. return;
  11966. }
  11967. guild_opposition(sd,t_sd);
  11968. }
  11969. /// Request to delete own guild.
  11970. /// 015d <key>.40B (CZ_REQ_DISORGANIZE_GUILD)
  11971. /// key:
  11972. /// now guild name; might have been (intended) email, since the
  11973. /// field name and size is same as the one in CH_DELETE_CHAR.
  11974. void clif_parse_GuildBreak( int32 fd, map_session_data *sd ){
  11975. if( sd == nullptr ){
  11976. return;
  11977. }
  11978. const PACKET_CZ_REQ_DISORGANIZE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_DISORGANIZE_GUILD*>( RFIFOP( fd, 0 ) );
  11979. guild_break( *sd, p->key );
  11980. }
  11981. /// Pet
  11982. ///
  11983. /// Request to invoke a pet menu action (CZ_COMMAND_PET).
  11984. /// 01a1 <type>.B
  11985. /// type:
  11986. /// 0 = pet information
  11987. /// 1 = feed
  11988. /// 2 = performance
  11989. /// 3 = return to egg
  11990. /// 4 = unequip accessory
  11991. void clif_parse_PetMenu(int32 fd, map_session_data *sd){
  11992. pet_menu(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11993. }
  11994. /// Attempt to tame a monster (CZ_TRYCAPTURE_MONSTER).
  11995. /// 019f <id>.L
  11996. void clif_parse_CatchPet(int32 fd, map_session_data *sd){
  11997. pet_catch_process_end(*sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11998. }
  11999. /// Answer to pet incubator egg selection dialog (CZ_SELECT_PETEGG).
  12000. /// 01a7 <index>.W
  12001. void clif_parse_SelectEgg(int32 fd, map_session_data *sd){
  12002. if (sd->menuskill_id != SA_TAMINGMONSTER || sd->menuskill_val != -1)
  12003. return;
  12004. pet_select_egg(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  12005. clif_menuskill_clear(sd);
  12006. }
  12007. /// Request to display pet's emotion/talk (CZ_PET_ACT).
  12008. /// 01a9 <data>.L
  12009. /// data:
  12010. /// is either emotion (@see enum emotion_type) or a compound value
  12011. /// (((mob id)-100)*100+(act id)*10+(hunger)) that describes an
  12012. /// entry (given in parentheses) in data\pettalktable.xml
  12013. /// act id:
  12014. /// 0 = feeding
  12015. /// 1 = hunting
  12016. /// 2 = danger
  12017. /// 3 = dead
  12018. /// 4 = normal (stand)
  12019. /// 5 = special performance (perfor_s)
  12020. /// 6 = level up (levelup)
  12021. /// 7 = performance 1 (perfor_1)
  12022. /// 8 = performance 2 (perfor_2)
  12023. /// 9 = performance 3 (perfor_3)
  12024. /// 10 = log-in greeting (connect)
  12025. /// hungry value:
  12026. /// 0 = very hungry (hungry)
  12027. /// 1 = hungry (bit_hungry)
  12028. /// 2 = satisfied (noting)
  12029. /// 3 = stuffed (full)
  12030. /// 4 = full (so_full)
  12031. void clif_parse_SendEmotion(int32 fd, map_session_data *sd)
  12032. {
  12033. if(sd->pd)
  12034. clif_pet_emotion( *sd->pd, RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]) );
  12035. }
  12036. /// Request to change pet's name (CZ_RENAME_PET).
  12037. /// 01a5 <name>.24B
  12038. void clif_parse_ChangePetName(int32 fd, map_session_data *sd)
  12039. {
  12040. pet_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12041. }
  12042. /// /kill (CZ_DISCONNECT_CHARACTER).
  12043. /// Request to disconnect a character.
  12044. /// 00cc <account id>.L
  12045. /// NOTE: Also sent when using GM right click menu "(name) force to quit"
  12046. void clif_parse_GMKick(int32 fd, map_session_data *sd)
  12047. {
  12048. struct block_list *target;
  12049. int32 tid;
  12050. tid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12051. target = map_id2bl(tid);
  12052. if (!target) {
  12053. clif_GM_kickack(sd, 0);
  12054. return;
  12055. }
  12056. switch (target->type) {
  12057. case BL_PC:
  12058. {
  12059. char command[NAME_LENGTH+6];
  12060. safesnprintf(command,sizeof(command),"%ckick %s", atcommand_symbol, status_get_name(*target));
  12061. is_atcommand(fd, sd, command, 1);
  12062. }
  12063. break;
  12064. /**
  12065. * This one does not invoke any atcommand, so we need to check for permissions.
  12066. */
  12067. case BL_MOB:
  12068. {
  12069. char command[100];
  12070. if( !pc_can_use_command(sd, "killmonster", COMMAND_ATCOMMAND)) {
  12071. clif_GM_kickack(sd, 0);
  12072. return;
  12073. }
  12074. safesnprintf(command,sizeof(command),"/kick %s (%d)", status_get_name(*target), status_get_class(target));
  12075. log_atcommand(sd, command);
  12076. status_percent_damage(&sd->bl, target, 100, 0, true); // can invalidate 'target'
  12077. }
  12078. break;
  12079. case BL_NPC:
  12080. {
  12081. struct npc_data* nd = (struct npc_data *)target;
  12082. if( pc_can_use_command(sd, "unloadnpc", COMMAND_ATCOMMAND)) {
  12083. npc_unload_duplicates(nd);
  12084. npc_unload(nd,true);
  12085. npc_read_event_script();
  12086. }
  12087. }
  12088. break;
  12089. default:
  12090. clif_GM_kickack(sd, 0);
  12091. }
  12092. }
  12093. /// /killall (CZ_DISCONNECT_ALL_CHARACTER).
  12094. /// Request to disconnect all characters.
  12095. /// 00ce
  12096. void clif_parse_GMKickAll(int32 fd, map_session_data* sd) {
  12097. char cmd[15];
  12098. safesnprintf(cmd,sizeof(cmd),"%ckickall",atcommand_symbol);
  12099. is_atcommand(fd, sd, cmd, 1);
  12100. }
  12101. /// /remove (CZ_REMOVE_AID).
  12102. /// Request to warp to a character with given login ID.
  12103. /// 01ba <account name>.24B
  12104. /// /shift (CZ_SHIFT).
  12105. /// Request to warp to a character with given name.
  12106. /// 01bb <char name>.24B
  12107. void clif_parse_GMShift(int32 fd, map_session_data *sd)
  12108. {// FIXME: remove is supposed to receive account name for clients prior 20100803RE
  12109. char *player_name;
  12110. char command[NAME_LENGTH+8];
  12111. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12112. player_name[NAME_LENGTH-1] = '\0';
  12113. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, player_name);
  12114. is_atcommand(fd, sd, command, 1);
  12115. }
  12116. /// /remove (CZ_REMOVE_AID_SSO).
  12117. /// Request to warp to a character with given account ID.
  12118. /// 0843 <account id>.L
  12119. void clif_parse_GMRemove2(int32 fd, map_session_data* sd)
  12120. {
  12121. uint32 account_id;
  12122. map_session_data* pl_sd;
  12123. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12124. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12125. char command[NAME_LENGTH+8];
  12126. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, pl_sd->status.name);
  12127. is_atcommand(fd, sd, command, 1);
  12128. }
  12129. }
  12130. /// /recall (CZ_RECALL).
  12131. /// Request to summon a player with given login ID to own position.
  12132. /// 01bc <account name>.24B
  12133. /// /summon (CZ_RECALL_GID).
  12134. /// Request to summon a player with given name to own position.
  12135. /// 01bd <char name>.24B
  12136. void clif_parse_GMRecall(int32 fd, map_session_data *sd)
  12137. {// FIXME: recall is supposed to receive account name for clients prior 20100803RE
  12138. char *player_name;
  12139. char command [NAME_LENGTH+8];
  12140. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12141. player_name[NAME_LENGTH-1] = '\0';
  12142. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, player_name);
  12143. is_atcommand(fd, sd, command, 1);
  12144. }
  12145. /// /recall (CZ_RECALL_SSO).
  12146. /// Request to summon a player with given account ID to own position.
  12147. /// 0842 <account id>.L
  12148. void clif_parse_GMRecall2(int32 fd, map_session_data* sd)
  12149. {
  12150. uint32 account_id;
  12151. map_session_data* pl_sd;
  12152. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12153. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12154. char command[NAME_LENGTH+8];
  12155. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, pl_sd->status.name);
  12156. is_atcommand(fd, sd, command, 1);
  12157. }
  12158. }
  12159. /// /item /monster (CZ_ITEM_CREATE).
  12160. /// Request to execute GM commands.
  12161. /// Usage:
  12162. /// /item n - summon n monster or acquire n item/s
  12163. /// /item money - grants 2147483647 zenies
  12164. /// /item whereisboss - locate boss mob in current map.(not yet implemented)
  12165. /// /item regenboss_n t - regenerate n boss monster by t millisecond.(not yet implemented)
  12166. /// /item onekillmonster - toggle an ability to kill mobs in one hit.(not yet implemented)
  12167. /// /item bossinfo - display the information of a boss monster in current map.(not yet implemented)
  12168. /// /item cap_n - capture n monster as pet.(not yet implemented)
  12169. /// /item agitinvest - reset current global agit investments.(not yet implemented)
  12170. /// 013f <item/mob name>.24B
  12171. /// 09ce <item/mob name>.100B [Ind/Yommy]
  12172. void clif_parse_GM_Item_Monster(int32 fd, map_session_data *sd)
  12173. {
  12174. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12175. StringBuf command;
  12176. char *str;
  12177. //#if PACKETVER >= 20131218
  12178. // char str[100];
  12179. //#else
  12180. // char str[24];
  12181. //#endif
  12182. str = RFIFOCP(fd,info->pos[0]);
  12183. if (!str || str[0] == '\0')
  12184. return;
  12185. if (strcmpi(str,"SPPOINT") == 0 || strcmpi(str,"JOBLEVEL") == 0) //! TODO /sp sends these values
  12186. return;
  12187. trim(str);
  12188. // Zeny
  12189. if( strcmpi(str, "money") == 0 ) {
  12190. StringBuf_Init(&command);
  12191. StringBuf_Printf(&command, "%czeny %d", atcommand_symbol, INT_MAX);
  12192. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12193. StringBuf_Destroy(&command);
  12194. return;
  12195. }
  12196. std::shared_ptr<item_data> id = item_db.searchname( str );
  12197. // Item
  12198. if( id ){
  12199. StringBuf_Init(&command);
  12200. if( !itemdb_isstackable2( id.get() ) ){ //Nonstackable
  12201. StringBuf_Printf(&command, "%citem2 %u 1 0 0 0 0 0 0 0", atcommand_symbol, id->nameid);
  12202. }else{
  12203. if (id->flag.guid)
  12204. StringBuf_Printf(&command, "%citem %u 1", atcommand_symbol, id->nameid);
  12205. else
  12206. StringBuf_Printf(&command, "%citem %u 20", atcommand_symbol, id->nameid);
  12207. }
  12208. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12209. StringBuf_Destroy(&command);
  12210. return;
  12211. }
  12212. // Monster
  12213. if (pc_can_use_command(sd, "monster", COMMAND_ATCOMMAND)) {
  12214. // If AegisName matches exactly, summon that monster (official behavior)
  12215. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname(str);
  12216. // Otherwise, search for monster with that ID or name (rAthena added behavior)
  12217. if (mob == nullptr) {
  12218. // Check for ID first as this is faster; if search string is not a number it will return 0
  12219. int32 mob_id = util::strtoint32def(str);
  12220. if (mob_id == 0 || mobdb_checkid(mob_id) == 0) {
  12221. mob_id = mobdb_searchname(str);
  12222. if (mob_id != 0) {
  12223. mob = mob_db.find(mob_id);
  12224. }
  12225. }
  12226. else
  12227. mob = mob_db.find(mob_id);
  12228. }
  12229. // Call corresponding atcommand when a valid monster was found
  12230. if (mob != nullptr) {
  12231. char command[CHAT_SIZE_MAX];
  12232. safesnprintf(command, sizeof(command), "%cmonster %s", atcommand_symbol, mob->sprite.c_str());
  12233. is_atcommand(fd, sd, command, 1);
  12234. return;
  12235. }
  12236. }
  12237. }
  12238. /// /hide (CZ_CHANGE_EFFECTSTATE).
  12239. /// 019d <effect state>.L
  12240. /// effect state:
  12241. /// TODO: Any OPTION_* ?
  12242. void clif_parse_GMHide(int32 fd, map_session_data *sd) {
  12243. char cmd[6];
  12244. //int32 eff_st = RFIFOL(packet_db[RFIFOW(fd,0)].pos[0]);
  12245. safesnprintf(cmd,sizeof(cmd),"%chide",atcommand_symbol);
  12246. is_atcommand(fd, sd, cmd, 1);
  12247. }
  12248. /// /resetcooltime
  12249. /// 0a88 (CZ_CMD_RESETCOOLTIME).
  12250. void clif_parse_gm_resetcooltime( int32 fd, map_session_data* sd ){
  12251. #if PACKETVER_MAIN_NUM >= 20160622 || PACKETVER_RE_NUM >= 20160622 || defined(PACKETVER_ZERO)
  12252. const PACKET_CZ_CMD_RESETCOOLTIME* p = reinterpret_cast<const PACKET_CZ_CMD_RESETCOOLTIME*>( RFIFOP( fd, 0 ) );
  12253. char cmd[CHAT_SIZE_MAX];
  12254. safesnprintf(cmd,sizeof(cmd),"%cresetcooltime",atcommand_symbol);
  12255. is_atcommand(fd, sd, cmd, 1);
  12256. #endif
  12257. }
  12258. /// Request to adjust player's manner points (CZ_REQ_GIVE_MANNER_POINT).
  12259. /// 0149 <account id>.L <type>.B <value>.W
  12260. /// type:
  12261. /// 0 = positive points
  12262. /// 1 = negative points
  12263. /// 2 = self mute (+10 minutes)
  12264. void clif_parse_GMReqNoChat(int32 fd,map_session_data *sd)
  12265. {
  12266. int32 id, type, value;
  12267. map_session_data *dstsd;
  12268. char command[NAME_LENGTH+15];
  12269. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12270. id = RFIFOL(fd,info->pos[0]);
  12271. type = RFIFOB(fd,info->pos[1]);
  12272. value = RFIFOW(fd,info->pos[2]);
  12273. if( type == 0 )
  12274. value = -value;
  12275. //If type is 2 and the ids don't match, this is a crafted hacked packet!
  12276. //Disabled because clients keep self-muting when you give players public @ commands... [Skotlex]
  12277. if (type == 2 /* && (pc_get_group_level(sd) > 0 || sd->bl.id != id)*/)
  12278. return;
  12279. dstsd = map_id2sd(id);
  12280. if( dstsd == nullptr )
  12281. return;
  12282. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, value, dstsd->status.name);
  12283. is_atcommand(fd, sd, command, 1);
  12284. }
  12285. /// /rc (CZ_REQ_GIVE_MANNER_BYNAME).
  12286. /// GM adjustment of a player's manner value by -60.
  12287. /// 0212 <char name>.24B
  12288. void clif_parse_GMRc(int32 fd, map_session_data* sd)
  12289. {
  12290. char command[NAME_LENGTH+15];
  12291. char *name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12292. name[NAME_LENGTH-1] = '\0';
  12293. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, 60, name);
  12294. is_atcommand(fd, sd, command, 1);
  12295. }
  12296. /// Result of request to resolve account name (ZC_ACK_ACCOUNTNAME).
  12297. /// 01e0 <account id>.L <account name>.24B
  12298. void clif_account_name(int32 fd, uint32 account_id, const char* accname)
  12299. {
  12300. WFIFOHEAD(fd,packet_len(0x1e0));
  12301. WFIFOW(fd,0) = 0x1e0;
  12302. WFIFOL(fd,2) = account_id;
  12303. safestrncpy(WFIFOCP(fd,6), accname, NAME_LENGTH);
  12304. WFIFOSET(fd,packet_len(0x1e0));
  12305. }
  12306. /// GM requesting account name (for right-click gm menu) (CZ_REQ_ACCOUNTNAME).
  12307. /// 01df <account id>.L
  12308. //! TODO: Figure out how does this actually work
  12309. void clif_parse_GMReqAccountName(int32 fd, map_session_data *sd)
  12310. {
  12311. uint32 account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12312. /*
  12313. char query[30];
  12314. safesnprintf(query,sizeof(query),"%d", account_id);
  12315. intif_request_accinfo(sd->fd, sd->bl.id, pc_get_group_level(sd), query, 1); //will call clif_account_name at return
  12316. */
  12317. clif_account_name(fd, account_id, "");
  12318. }
  12319. /// /changemaptype <x> <y> <type> (CZ_CHANGE_MAPTYPE).
  12320. /// GM single cell type change request.
  12321. /// 0198 <x>.W <y>.W <type>.W
  12322. /// type:
  12323. /// 0 = not walkable
  12324. /// 1 = walkable
  12325. void clif_parse_GMChangeMapType(int32 fd, map_session_data *sd)
  12326. {
  12327. int32 x,y,type;
  12328. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12329. if(! pc_has_permission(sd, PC_PERM_USE_CHANGEMAPTYPE) )
  12330. return;
  12331. x = RFIFOW(fd,info->pos[0]);
  12332. y = RFIFOW(fd,info->pos[1]);
  12333. type = RFIFOW(fd,info->pos[2]);
  12334. map_setgatcell(sd->bl.m,x,y,type);
  12335. clif_changemapcell( sd->bl.m, x, y, type, ALL_SAMEMAP, &sd->bl );
  12336. //FIXME: once players leave the map, the client 'forgets' this information.
  12337. }
  12338. /// /in /ex (CZ_SETTING_WHISPER_PC).
  12339. /// Request to allow/deny whispers from a nick.
  12340. /// 00cf <nick>.24B <type>.B
  12341. /// type:
  12342. /// 0 = (/ex nick) deny speech from nick
  12343. /// 1 = (/in nick) allow speech from nick
  12344. void clif_parse_PMIgnore(int32 fd, map_session_data* sd)
  12345. {
  12346. char* nick;
  12347. uint8 type;
  12348. int32 i;
  12349. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12350. nick = RFIFOCP(fd,info->pos[0]);
  12351. nick[NAME_LENGTH-1] = '\0'; // to be sure that the player name has at most 23 characters
  12352. type = RFIFOB(fd,info->pos[1]);
  12353. if( type == 0 ) { // Add name to ignore list (block)
  12354. if (strcmp(wisp_server_name, nick) == 0) {
  12355. clif_wisexin(sd, type, 1); // fail
  12356. return;
  12357. }
  12358. // try to find a free spot, while checking for duplicates at the same time
  12359. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12360. if( i == MAX_IGNORE_LIST ) {// no space for new entry
  12361. clif_wisexin(sd, type, 2); // too many blocks
  12362. return;
  12363. }
  12364. if( sd->ignore[i].name[0] != '\0' ) {// name already exists
  12365. clif_wisexin(sd, type, 0); // Aegis reports success.
  12366. return;
  12367. }
  12368. //Insert in position i
  12369. safestrncpy(sd->ignore[i].name, nick, NAME_LENGTH);
  12370. } else { // Remove name from ignore list (unblock)
  12371. // find entry
  12372. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12373. if( i == MAX_IGNORE_LIST || sd->ignore[i].name[0] == '\0' ) { //Not found
  12374. clif_wisexin(sd, type, 1); // fail
  12375. return;
  12376. }
  12377. // move everything one place down to overwrite removed entry
  12378. memmove(sd->ignore[i].name, sd->ignore[i+1].name, (MAX_IGNORE_LIST-i-1)*sizeof(sd->ignore[0].name));
  12379. // wipe last entry
  12380. memset(sd->ignore[MAX_IGNORE_LIST-1].name, 0, sizeof(sd->ignore[0].name));
  12381. }
  12382. clif_wisexin(sd, type, 0); // success
  12383. }
  12384. /// /inall /exall (CZ_SETTING_WHISPER_STATE).
  12385. /// Request to allow/deny all whispers.
  12386. /// 00d0 <type>.B
  12387. /// type:
  12388. /// 0 = (/exall) deny all speech
  12389. /// 1 = (/inall) allow all speech
  12390. void clif_parse_PMIgnoreAll(int32 fd, map_session_data *sd)
  12391. {
  12392. int32 type = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]), flag;
  12393. if( type == 0 ) {// Deny all
  12394. if( sd->state.ignoreAll ) {
  12395. flag = 1; // fail
  12396. } else {
  12397. sd->state.ignoreAll = 1;
  12398. flag = 0; // success
  12399. }
  12400. } else {//Unblock everyone
  12401. if( sd->state.ignoreAll ) {
  12402. sd->state.ignoreAll = 0;
  12403. flag = 0; // success
  12404. } else {
  12405. if (sd->ignore[0].name[0] != '\0')
  12406. { //Wipe the ignore list.
  12407. memset(sd->ignore, 0, sizeof(sd->ignore));
  12408. flag = 0; // success
  12409. } else {
  12410. flag = 1; // fail
  12411. }
  12412. }
  12413. }
  12414. clif_wisall(sd, type, flag);
  12415. }
  12416. /// Whisper ignore list (ZC_WHISPER_LIST).
  12417. /// 00d4 <packet len>.W { <char name>.24B }*
  12418. void clif_PMIgnoreList(map_session_data* sd)
  12419. {
  12420. int32 i, fd = sd->fd;
  12421. WFIFOHEAD(fd,4+ARRAYLENGTH(sd->ignore)*NAME_LENGTH);
  12422. WFIFOW(fd,0) = 0xd4;
  12423. for( i = 0; i < ARRAYLENGTH(sd->ignore) && sd->ignore[i].name[0]; i++ ) {
  12424. safestrncpy(WFIFOCP(fd,4+i*NAME_LENGTH), sd->ignore[i].name, NAME_LENGTH);
  12425. }
  12426. WFIFOW(fd,2) = 4+i*NAME_LENGTH;
  12427. WFIFOSET(fd,WFIFOW(fd,2));
  12428. }
  12429. /// Whisper ignore list request (CZ_REQ_WHISPER_LIST).
  12430. /// 00d3
  12431. void clif_parse_PMIgnoreList(int32 fd,map_session_data *sd)
  12432. {
  12433. clif_PMIgnoreList(sd);
  12434. }
  12435. /// Request to invoke the /doridori recovery bonus (CZ_DORIDORI).
  12436. /// 01e7
  12437. void clif_parse_NoviceDoriDori(int32 fd, map_session_data *sd)
  12438. {
  12439. if (sd->state.doridori) return;
  12440. switch (sd->class_&MAPID_UPPERMASK) {
  12441. case MAPID_SOUL_LINKER:
  12442. case MAPID_STAR_GLADIATOR:
  12443. case MAPID_TAEKWON:
  12444. if (!sd->state.rest)
  12445. break;
  12446. [[fallthrough]];
  12447. case MAPID_SUPER_NOVICE:
  12448. case MAPID_SUPER_BABY:
  12449. case MAPID_SUPER_NOVICE_E:
  12450. case MAPID_SUPER_BABY_E:
  12451. sd->state.doridori=1;
  12452. break;
  12453. }
  12454. }
  12455. /// Request to invoke the effect of super novice's guardian angel prayer (CZ_CHOPOKGI).
  12456. /// 01ed
  12457. /// Note: This packet is caused by 7 lines of any text, followed by
  12458. /// the prayer and an another line of any text. The prayer is
  12459. /// defined by lines 790~793 in data\msgstringtable.txt
  12460. /// "Dear angel, can you hear my voice?"
  12461. /// "I am" (space separated player name) "Super Novice~"
  12462. /// "Help me out~ Please~ T_T"
  12463. void clif_parse_NoviceExplosionSpirits(int32 fd, map_session_data *sd)
  12464. {
  12465. if( (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE ) {
  12466. t_exp next = pc_nextbaseexp(sd);
  12467. if( next ) {
  12468. int32 percent = (int32)( ( (double)sd->status.base_exp/(double)next )*1000. );
  12469. if( percent && ( percent%100 ) == 0 ) {// 10.0%, 20.0%, ..., 90.0%
  12470. sc_start(&sd->bl,&sd->bl, SC_EXPLOSIONSPIRITS, 100, 17, skill_get_time(MO_EXPLOSIONSPIRITS, 5)); //Lv17-> +50 critical (noted by Poki) [Skotlex]
  12471. clif_skill_nodamage(&sd->bl, sd->bl, MO_EXPLOSIONSPIRITS, 5); // prayer always shows successful Lv5 cast and disregards noskill restrictions
  12472. }
  12473. }
  12474. }
  12475. }
  12476. /// Friends List
  12477. ///
  12478. /// Toggles a single friend online/offline [Skotlex] (ZC_FRIENDS_STATE).
  12479. /// 0206 <account id>.L <char id>.L <state>.B
  12480. /// 0206 <account id>.L <char id>.L <state>.B <name>.24B >= 20180221
  12481. /// state:
  12482. /// 0 = online
  12483. /// 1 = offline
  12484. void clif_friendslist_toggle(map_session_data *sd,uint32 account_id, uint32 char_id, int32 online)
  12485. {
  12486. int32 i, fd = sd->fd;
  12487. //Seek friend.
  12488. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id &&
  12489. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12490. if(i == MAX_FRIENDS || sd->status.friends[i].char_id == 0)
  12491. return; //Not found
  12492. WFIFOHEAD(fd,packet_len(0x206));
  12493. WFIFOW(fd, 0) = 0x206;
  12494. WFIFOL(fd, 2) = sd->status.friends[i].account_id;
  12495. WFIFOL(fd, 6) = sd->status.friends[i].char_id;
  12496. WFIFOB(fd,10) = !online; //Yeah, a 1 here means "logged off", go figure...
  12497. #if PACKETVER >= 20180221
  12498. safestrncpy(WFIFOCP(fd, 11), sd->status.friends[i].name, NAME_LENGTH);
  12499. #endif
  12500. WFIFOSET(fd, packet_len(0x206));
  12501. }
  12502. //Subfunction called from clif_foreachclient to toggle friends on/off [Skotlex]
  12503. int32 clif_friendslist_toggle_sub(map_session_data *sd,va_list ap)
  12504. {
  12505. uint32 account_id, char_id, online;
  12506. account_id = va_arg(ap, int32);
  12507. char_id = va_arg(ap, int32);
  12508. online = va_arg(ap, int32);
  12509. clif_friendslist_toggle(sd, account_id, char_id, online);
  12510. return 0;
  12511. }
  12512. /// Sends the whole friends list (ZC_FRIENDS_LIST).
  12513. /// 0201 <packet len>.W { <account id>.L <char id>.L <name>.24B }*
  12514. /// 0201 <packet len>.W { <account id>.L <char id>.L }* >= 20180221
  12515. void clif_friendslist_send( map_session_data& sd ){
  12516. PACKET_ZC_FRIENDS_LIST* p = reinterpret_cast<PACKET_ZC_FRIENDS_LIST*>( packet_buffer );
  12517. p->packetType = HEADER_ZC_FRIENDS_LIST;
  12518. p->PacketLength = sizeof( *p );
  12519. for( int32 i = 0, c = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12520. PACKET_ZC_FRIENDS_LIST_sub& f = p->friends[c];
  12521. f.AID = sd.status.friends[i].account_id;
  12522. f.CID = sd.status.friends[i].char_id;
  12523. #if !( PACKETVER_MAIN_NUM >= 20180307 || PACKETVER_RE_NUM >= 20180221 || PACKETVER_ZERO_NUM >= 20180328 ) || PACKETVER >= 20200902
  12524. safestrncpy( f.name, sd.status.friends[i].name, sizeof( f.name ) );
  12525. #endif
  12526. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( f ) );
  12527. c++;
  12528. }
  12529. clif_send( p, p->PacketLength, &sd.bl, SELF );
  12530. // Sending the online players
  12531. for( int32 i = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12532. if( map_charid2sd( sd.status.friends[i].char_id ) ){
  12533. clif_friendslist_toggle( &sd, sd.status.friends[i].account_id, sd.status.friends[i].char_id, 1 );
  12534. }
  12535. }
  12536. }
  12537. /// Notification about the result of a friend add request (ZC_ADD_FRIENDS_LIST).
  12538. /// 0209 <result>.W <account id>.L <char id>.L <name>.24B
  12539. /// result:
  12540. /// 0 = MsgStringTable[821]="You have become friends with (%s)."
  12541. /// 1 = MsgStringTable[822]="(%s) does not want to be friends with you."
  12542. /// 2 = MsgStringTable[819]="Your Friend List is full."
  12543. /// 3 = MsgStringTable[820]="(%s)'s Friend List is full."
  12544. void clif_friendslist_reqack(map_session_data *sd, map_session_data *f_sd, int32 type)
  12545. {
  12546. int32 fd;
  12547. nullpo_retv(sd);
  12548. fd = sd->fd;
  12549. WFIFOHEAD(fd,packet_len(0x209));
  12550. WFIFOW(fd,0) = 0x209;
  12551. WFIFOW(fd,2) = type;
  12552. if (f_sd) {
  12553. WFIFOL(fd,4) = f_sd->status.account_id;
  12554. WFIFOL(fd,8) = f_sd->status.char_id;
  12555. safestrncpy(WFIFOCP(fd, 12), f_sd->status.name,NAME_LENGTH);
  12556. }
  12557. WFIFOSET(fd, packet_len(0x209));
  12558. }
  12559. /// Asks a player for permission to be added as friend (ZC_REQ_ADD_FRIENDS).
  12560. /// 0207 <req account id>.L <req char id>.L <req char name>.24B
  12561. void clif_friendlist_req(map_session_data* sd, uint32 account_id, uint32 char_id, const char* name)
  12562. {
  12563. int32 fd = sd->fd;
  12564. WFIFOHEAD(fd,packet_len(0x207));
  12565. WFIFOW(fd,0) = 0x207;
  12566. WFIFOL(fd,2) = account_id;
  12567. WFIFOL(fd,6) = char_id;
  12568. safestrncpy(WFIFOCP(fd,10), name, NAME_LENGTH);
  12569. WFIFOSET(fd,packet_len(0x207));
  12570. }
  12571. /// Request to add a player as friend (CZ_ADD_FRIENDS).
  12572. /// 0202 <name>.24B
  12573. void clif_parse_FriendsListAdd(int32 fd, map_session_data *sd)
  12574. {
  12575. map_session_data *f_sd;
  12576. int32 i;
  12577. // TODO: shuffle packet
  12578. f_sd = map_nick2sd(RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]),false);
  12579. // Friend doesn't exist (no player with this name)
  12580. if (f_sd == nullptr) {
  12581. clif_displaymessage(fd, msg_txt(sd,3));
  12582. return;
  12583. }
  12584. if( sd->bl.id == f_sd->bl.id ) {// adding oneself as friend
  12585. return;
  12586. }
  12587. // ensure that the request player's friend list is not full
  12588. ARR_FIND(0, MAX_FRIENDS, i, sd->status.friends[i].char_id == 0);
  12589. if( i == MAX_FRIENDS ){
  12590. clif_friendslist_reqack(sd, f_sd, 2);
  12591. return;
  12592. }
  12593. // @noask [LuzZza]
  12594. if(f_sd->state.noask) {
  12595. clif_noask_sub( *sd, *f_sd, 398 ); // Autorejected friend request from %s.
  12596. return;
  12597. }
  12598. // Friend already exists
  12599. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id != 0; i++) {
  12600. if (sd->status.friends[i].char_id == f_sd->status.char_id) {
  12601. clif_displaymessage(fd, msg_txt(sd,671)); //"Friend already exists."
  12602. return;
  12603. }
  12604. }
  12605. f_sd->friend_req = sd->status.char_id;
  12606. sd->friend_req = f_sd->status.char_id;
  12607. clif_friendlist_req(f_sd, sd->status.account_id, sd->status.char_id, sd->status.name);
  12608. }
  12609. /// Answer to a friend add request (CZ_ACK_REQ_ADD_FRIENDS).
  12610. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.B
  12611. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.L (PACKETVER >= 6)
  12612. /// reply:
  12613. /// 0 = rejected
  12614. /// 1 = accepted
  12615. void clif_parse_FriendsListReply(int32 fd, map_session_data *sd)
  12616. {
  12617. map_session_data *f_sd;
  12618. uint32 account_id;
  12619. char reply;
  12620. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12621. account_id = RFIFOL(fd,info->pos[0]);
  12622. //char_id = RFIFOL(fd,info->pos[1]);
  12623. #if PACKETVER < 6
  12624. reply = RFIFOB(fd,info->pos[2]);
  12625. #else
  12626. reply = RFIFOL(fd,info->pos[2]);
  12627. #endif
  12628. if( sd->bl.id == account_id ) {// adding oneself as friend
  12629. return;
  12630. }
  12631. f_sd = map_id2sd(account_id); //The account id is the same as the bl.id of players.
  12632. if (f_sd == nullptr)
  12633. return;
  12634. if (reply == 0 || !( sd->friend_req == f_sd->status.char_id && f_sd->friend_req == sd->status.char_id ) )
  12635. clif_friendslist_reqack(f_sd, sd, 1);
  12636. else {
  12637. int32 i;
  12638. // Find an empty slot
  12639. for (i = 0; i < MAX_FRIENDS; i++)
  12640. if (f_sd->status.friends[i].char_id == 0)
  12641. break;
  12642. if (i == MAX_FRIENDS) {
  12643. clif_friendslist_reqack(f_sd, sd, 2);
  12644. return;
  12645. }
  12646. f_sd->status.friends[i].account_id = sd->status.account_id;
  12647. f_sd->status.friends[i].char_id = sd->status.char_id;
  12648. safestrncpy(f_sd->status.friends[i].name, sd->status.name, NAME_LENGTH);
  12649. clif_friendslist_reqack(f_sd, sd, 0);
  12650. achievement_update_objective(f_sd, AG_ADD_FRIEND, 1, i + 1);
  12651. if (battle_config.friend_auto_add) {
  12652. // Also add f_sd to sd's friendlist.
  12653. for (i = 0; i < MAX_FRIENDS; i++) {
  12654. if (sd->status.friends[i].char_id == f_sd->status.char_id)
  12655. return; //No need to add anything.
  12656. if (sd->status.friends[i].char_id == 0)
  12657. break;
  12658. }
  12659. if (i == MAX_FRIENDS) {
  12660. clif_friendslist_reqack(sd, f_sd, 2);
  12661. return;
  12662. }
  12663. sd->status.friends[i].account_id = f_sd->status.account_id;
  12664. sd->status.friends[i].char_id = f_sd->status.char_id;
  12665. safestrncpy(sd->status.friends[i].name, f_sd->status.name, NAME_LENGTH);
  12666. clif_friendslist_reqack(sd, f_sd, 0);
  12667. achievement_update_objective(sd, AG_ADD_FRIEND, 1, i + 1);
  12668. }
  12669. }
  12670. }
  12671. /// Request to delete a friend (CZ_DELETE_FRIENDS).
  12672. /// 0203 <account id>.L <char id>.L
  12673. void clif_parse_FriendsListRemove(int32 fd, map_session_data *sd)
  12674. {
  12675. map_session_data *f_sd = nullptr;
  12676. uint32 account_id, char_id;
  12677. int32 i, j;
  12678. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12679. account_id = RFIFOL(fd,info->pos[0]);
  12680. char_id = RFIFOL(fd,info->pos[1]);
  12681. // Search friend
  12682. for (i = 0; i < MAX_FRIENDS &&
  12683. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12684. if (i == MAX_FRIENDS) {
  12685. clif_displaymessage(fd, msg_txt(sd,672)); //"Name not found in list."
  12686. return;
  12687. }
  12688. //remove from friend's list first
  12689. if( (f_sd = map_id2sd(account_id)) && f_sd->status.char_id == char_id) {
  12690. for (i = 0; i < MAX_FRIENDS &&
  12691. (f_sd->status.friends[i].char_id != sd->status.char_id || f_sd->status.friends[i].account_id != sd->status.account_id); i++);
  12692. if (i != MAX_FRIENDS) {
  12693. // move all chars up
  12694. for(j = i + 1; j < MAX_FRIENDS; j++)
  12695. memcpy(&f_sd->status.friends[j-1], &f_sd->status.friends[j], sizeof(f_sd->status.friends[0]));
  12696. memset(&f_sd->status.friends[MAX_FRIENDS-1], 0, sizeof(f_sd->status.friends[MAX_FRIENDS-1]));
  12697. //should the guy be notified of some message? we should add it here if so
  12698. WFIFOHEAD(f_sd->fd,packet_len(0x20a));
  12699. WFIFOW(f_sd->fd,0) = 0x20a;
  12700. WFIFOL(f_sd->fd,2) = sd->status.account_id;
  12701. WFIFOL(f_sd->fd,6) = sd->status.char_id;
  12702. WFIFOSET(f_sd->fd, packet_len(0x20a));
  12703. }
  12704. } else { //friend not online -- ask char server to delete from his friendlist
  12705. if(chrif_removefriend(char_id,sd->status.char_id)) { // char-server offline, abort
  12706. clif_displaymessage(fd, msg_txt(sd,673)); //"This action can't be performed at the moment. Please try again later."
  12707. return;
  12708. }
  12709. }
  12710. // We can now delete from original requester
  12711. for (i = 0; i < MAX_FRIENDS &&
  12712. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12713. // move all chars up
  12714. for(j = i + 1; j < MAX_FRIENDS; j++)
  12715. memcpy(&sd->status.friends[j-1], &sd->status.friends[j], sizeof(sd->status.friends[0]));
  12716. memset(&sd->status.friends[MAX_FRIENDS-1], 0, sizeof(sd->status.friends[MAX_FRIENDS-1]));
  12717. clif_displaymessage(fd, msg_txt(sd,674)); //"Friend removed"
  12718. WFIFOHEAD(fd,packet_len(0x20a));
  12719. WFIFOW(fd,0) = 0x20a;
  12720. WFIFOL(fd,2) = account_id;
  12721. WFIFOL(fd,6) = char_id;
  12722. WFIFOSET(fd, packet_len(0x20a));
  12723. }
  12724. /// /pvpinfo list (ZC_ACK_PVPPOINT).
  12725. /// 0210 <char id>.L <account id>.L <win point>.L <lose point>.L <point>.L
  12726. void clif_PVPInfo(map_session_data* sd)
  12727. {
  12728. int32 fd = sd->fd;
  12729. WFIFOHEAD(fd,packet_len(0x210));
  12730. WFIFOW(fd,0) = 0x210;
  12731. WFIFOL(fd,2) = sd->status.char_id;
  12732. WFIFOL(fd,6) = sd->status.account_id;
  12733. WFIFOL(fd,10) = sd->pvp_won; // times won
  12734. WFIFOL(fd,14) = sd->pvp_lost; // times lost
  12735. WFIFOL(fd,18) = sd->pvp_point;
  12736. WFIFOSET(fd, packet_len(0x210));
  12737. }
  12738. /// /pvpinfo (CZ_REQ_PVPPOINT).
  12739. /// 020f <char id>.L <account id>.L
  12740. void clif_parse_PVPInfo(int32 fd,map_session_data *sd)
  12741. {
  12742. // TODO: Is there a way to use this on an another player (char/acc id)?
  12743. //int32 cid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12744. //int32 aid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  12745. clif_PVPInfo(sd);
  12746. }
  12747. /// SG Feel save OK [Komurka] (CZ_AGREE_STARPLACE).
  12748. /// 0254 <which>.B
  12749. /// which: (unused atm for security purpose)
  12750. /// 0 = sun
  12751. /// 1 = moon
  12752. /// 2 = star
  12753. void clif_parse_FeelSaveOk(int32 fd,map_session_data *sd)
  12754. {
  12755. int32 i;
  12756. //int32 wich = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12757. if (sd->menuskill_id != SG_FEEL)
  12758. return;
  12759. i = sd->menuskill_val-1;
  12760. if (i<0 || i >= MAX_PC_FEELHATE) return; //Bug?
  12761. sd->feel_map[i].index = map_id2index(sd->bl.m);
  12762. sd->feel_map[i].m = sd->bl.m;
  12763. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), sd->feel_map[i].index);
  12764. //Are these really needed? Shouldn't they show up automatically from the feel save packet?
  12765. // clif_misceffect2(&sd->bl, 0x1b0);
  12766. // clif_misceffect2(&sd->bl, 0x21f);
  12767. clif_feel_info(sd, i, 0);
  12768. clif_menuskill_clear(sd);
  12769. }
  12770. /// Star Gladiator's Feeling map confirmation prompt (ZC_STARPLACE).
  12771. /// 0253 <which>.B
  12772. /// which:
  12773. /// 0 = sun
  12774. /// 1 = moon
  12775. /// 2 = star
  12776. void clif_feel_req(int32 fd, map_session_data *sd, uint16 skill_lv)
  12777. {
  12778. WFIFOHEAD(fd,packet_len(0x253));
  12779. WFIFOW(fd,0)=0x253;
  12780. WFIFOB(fd,2)=TOB(skill_lv-1);
  12781. WFIFOSET(fd, packet_len(0x253));
  12782. sd->menuskill_id = SG_FEEL;
  12783. sd->menuskill_val = skill_lv;
  12784. }
  12785. /// Request to change homunculus' name (CZ_RENAME_MER).
  12786. /// 0231 <name>.24B
  12787. void clif_parse_ChangeHomunculusName(int32 fd, map_session_data *sd){
  12788. hom_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12789. }
  12790. /// Request to warp/move homunculus/mercenary to it's owner (CZ_REQUEST_MOVETOOWNER).
  12791. /// 0234 <id>.L
  12792. void clif_parse_HomMoveToMaster(int32 fd, map_session_data *sd){
  12793. int32 id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]); // Mercenary or Homunculus
  12794. struct block_list *bl = nullptr;
  12795. struct unit_data *ud = nullptr;
  12796. if( sd->md && sd->md->bl.id == id )
  12797. bl = &sd->md->bl;
  12798. else if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12799. bl = &sd->hd->bl; // Moving Homunculus
  12800. else
  12801. return;
  12802. unit_calc_pos(bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  12803. ud = unit_bl2ud(bl);
  12804. unit_walktoxy(bl, ud->to_x, ud->to_y, 4);
  12805. }
  12806. /// Request to move homunculus/mercenary.
  12807. /// 0232 <id>.L <position data>.3B (CZ_REQUEST_MOVENPC)
  12808. void clif_parse_HomMoveTo(int32 fd, map_session_data *sd){
  12809. #if PACKETVER >= 20050425
  12810. const PACKET_CZ_REQUEST_MOVENPC* p = reinterpret_cast<const PACKET_CZ_REQUEST_MOVENPC*>( RFIFOP( fd, 0 ) );
  12811. struct block_list *bl = nullptr;
  12812. int16 x, y;
  12813. RBUFPOS( p->PosDir, 0, &x, &y, nullptr );
  12814. if( sd->md && sd->md->bl.id == p->GID )
  12815. bl = &sd->md->bl; // Moving Mercenary
  12816. else if( hom_is_active(sd->hd) && sd->hd->bl.id == p->GID )
  12817. bl = &sd->hd->bl; // Moving Homunculus
  12818. else
  12819. return;
  12820. if (x < 0 || y < 0 || (x == bl->x && y == bl->y))
  12821. return;
  12822. unit_walktoxy(bl, x, y, 4);
  12823. #endif
  12824. }
  12825. /// Request to do an action with homunculus/mercenary (CZ_REQUEST_ACTNPC).
  12826. /// 0233 <id>.L <target id>.L <action>.B
  12827. /// action:
  12828. /// always 0
  12829. void clif_parse_HomAttack(int32 fd,map_session_data *sd)
  12830. {
  12831. struct block_list *bl = nullptr;
  12832. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12833. int32 id = RFIFOL(fd,info->pos[0]);
  12834. int32 target_id = RFIFOL(fd,info->pos[1]);
  12835. int32 action_type = RFIFOB(fd,info->pos[2]);
  12836. if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12837. bl = &sd->hd->bl;
  12838. else if( sd->md && sd->md->bl.id == id )
  12839. bl = &sd->md->bl;
  12840. else return;
  12841. unit_attack(bl, target_id, action_type != 0);
  12842. }
  12843. /// Request to invoke a homunculus menu action (CZ_COMMAND_MER).
  12844. /// 022d <type>.W <command>.B
  12845. /// type:
  12846. /// always 0
  12847. /// command:
  12848. /// 0 = homunculus information
  12849. /// 1 = feed
  12850. /// 2 = delete
  12851. void clif_parse_HomMenu(int32 fd, map_session_data *sd)
  12852. { //[orn]
  12853. int32 cmd = RFIFOW(fd,0);
  12854. //int32 type = RFIFOW(fd,packet_db[cmd].pos[0]);
  12855. if(!hom_is_active(sd->hd))
  12856. return;
  12857. // TODO: shuffle packet
  12858. hom_menu(sd,RFIFOB(fd,packet_db[cmd].pos[1]));
  12859. }
  12860. /// Request to resurrect oneself using Token of Siegfried (CZ_STANDING_RESURRECTION).
  12861. /// 0292
  12862. void clif_parse_AutoRevive(int32 fd, map_session_data *sd)
  12863. {
  12864. pc_revive_item(sd);
  12865. }
  12866. /// Information about character's status values (ZC_ACK_STATUS_GM).
  12867. /// 0214 <str>.B <standardStr>.B <agi>.B <standardAgi>.B <vit>.B <standardVit>.B
  12868. /// <int>.B <standardInt>.B <dex>.B <standardDex>.B <luk>.B <standardLuk>.B
  12869. /// <attPower>.W <refiningPower>.W <max_mattPower>.W <min_mattPower>.W
  12870. /// <itemdefPower>.W <plusdefPower>.W <mdefPower>.W <plusmdefPower>.W
  12871. /// <hitSuccessValue>.W <avoidSuccessValue>.W <plusAvoidSuccessValue>.W
  12872. /// <criticalSuccessValue>.W <ASPD>.W <plusASPD>.W
  12873. void clif_check(int32 fd, map_session_data* pl_sd)
  12874. {
  12875. WFIFOHEAD(fd,packet_len(0x214));
  12876. WFIFOW(fd, 0) = 0x214;
  12877. WFIFOB(fd, 2) = min(pl_sd->status.str, UINT8_MAX);
  12878. WFIFOB(fd, 3) = pc_need_status_point(pl_sd, SP_STR, 1);
  12879. WFIFOB(fd, 4) = min(pl_sd->status.agi, UINT8_MAX);
  12880. WFIFOB(fd, 5) = pc_need_status_point(pl_sd, SP_AGI, 1);
  12881. WFIFOB(fd, 6) = min(pl_sd->status.vit, UINT8_MAX);
  12882. WFIFOB(fd, 7) = pc_need_status_point(pl_sd, SP_VIT, 1);
  12883. WFIFOB(fd, 8) = min(pl_sd->status.int_, UINT8_MAX);
  12884. WFIFOB(fd, 9) = pc_need_status_point(pl_sd, SP_INT, 1);
  12885. WFIFOB(fd,10) = min(pl_sd->status.dex, UINT8_MAX);
  12886. WFIFOB(fd,11) = pc_need_status_point(pl_sd, SP_DEX, 1);
  12887. WFIFOB(fd,12) = min(pl_sd->status.luk, UINT8_MAX);
  12888. WFIFOB(fd,13) = pc_need_status_point(pl_sd, SP_LUK, 1);
  12889. WFIFOW(fd,14) = pl_sd->battle_status.batk+pl_sd->battle_status.rhw.atk+pl_sd->battle_status.lhw.atk;
  12890. WFIFOW(fd,16) = pl_sd->battle_status.rhw.atk2+pl_sd->battle_status.lhw.atk2;
  12891. WFIFOW(fd,18) = pl_sd->battle_status.matk_max;
  12892. WFIFOW(fd,20) = pl_sd->battle_status.matk_min;
  12893. WFIFOW(fd,22) = pl_sd->battle_status.def;
  12894. WFIFOW(fd,24) = pl_sd->battle_status.def2;
  12895. WFIFOW(fd,26) = pl_sd->battle_status.mdef;
  12896. WFIFOW(fd,28) = pl_sd->battle_status.mdef2;
  12897. WFIFOW(fd,30) = pl_sd->battle_status.hit;
  12898. WFIFOW(fd,32) = pl_sd->battle_status.flee;
  12899. WFIFOW(fd,34) = pl_sd->battle_status.flee2/10;
  12900. WFIFOW(fd,36) = pl_sd->battle_status.cri/10;
  12901. WFIFOW(fd,38) = (2000-pl_sd->battle_status.amotion)/10; // aspd
  12902. WFIFOW(fd,40) = 0; // FIXME: What is 'plusASPD' supposed to be? Maybe adelay?
  12903. WFIFOSET(fd,packet_len(0x214));
  12904. }
  12905. /// /check (CZ_REQ_STATUS_GM).
  12906. /// Request character's status values.
  12907. /// 0213 <char name>.24B
  12908. void clif_parse_Check(int32 fd, map_session_data *sd)
  12909. {
  12910. char charname[NAME_LENGTH];
  12911. map_session_data* pl_sd;
  12912. if(!pc_has_permission(sd, PC_PERM_USE_CHECK))
  12913. return;
  12914. safestrncpy(charname, RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]), sizeof(charname));
  12915. if( ( pl_sd = map_nick2sd(charname,false) ) == nullptr || pc_get_group_level(sd) < pc_get_group_level(pl_sd) )
  12916. {
  12917. return;
  12918. }
  12919. clif_check(fd, pl_sd);
  12920. }
  12921. /// MAIL SYSTEM
  12922. /// By Zephyrus
  12923. ///
  12924. /// Notification about the result of adding an item to mail
  12925. /// 0255 <index>.W <result>.B (ZC_ACK_MAIL_ADD_ITEM)
  12926. /// result:
  12927. /// 0 = success
  12928. /// 1 = failure
  12929. /// 0a05 <result>.B <index>.W <amount>.W <item id>.W <item type>.B <identified>.B <damaged>.B <refine>.B (ZC_ACK_ADD_ITEM_TO_MAIL)
  12930. /// { <card id>.W }*4 { <option index>.W <option value>.W <option parameter>.B }*5
  12931. /// result:
  12932. /// 0 = success
  12933. /// 1 = over weight
  12934. /// 2 = fatal error
  12935. void clif_Mail_setattachment( map_session_data* sd, int32 index, int32 amount, uint8 flag ){
  12936. int32 fd = sd->fd;
  12937. #if PACKETVER < 20150513
  12938. WFIFOHEAD(fd,packet_len(0x255));
  12939. WFIFOW(fd,0) = 0x255;
  12940. WFIFOW(fd,2) = index;
  12941. WFIFOB(fd,4) = flag;
  12942. WFIFOSET(fd,packet_len(0x255));
  12943. #else
  12944. PACKET_ZC_ACK_ADD_ITEM_RODEX p = {};
  12945. if( flag ){
  12946. memset( &p, 0, sizeof( p ) );
  12947. }else{
  12948. struct item* item = &sd->inventory.u.items_inventory[server_index(index)];
  12949. p.index = index;
  12950. p.count = amount;
  12951. p.itemId = client_nameid( item->nameid );
  12952. p.type = itemtype( item->nameid );
  12953. p.IsIdentified = item->identify ? 1 : 0;
  12954. p.IsDamaged = item->attribute ? 1 : 0;
  12955. p.refiningLevel = item->refine;
  12956. clif_addcards( &p.slot, item );
  12957. clif_add_random_options( p.optionData, *item );
  12958. p.weight = 0;
  12959. for( int32 i = 0; i < MAIL_MAX_ITEM; i++ ){
  12960. if( sd->mail.item[i].nameid == 0 ){
  12961. continue;
  12962. }
  12963. p.weight += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  12964. }
  12965. p.favorite = ( item->favorite != 0 ) ? 1 : 0;
  12966. p.location = pc_equippoint( sd, server_index( index ) );
  12967. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  12968. p.grade = item->enchantgrade;
  12969. #endif
  12970. }
  12971. p.PacketType = HEADER_ZC_ACK_ADD_ITEM_RODEX;
  12972. p.result = flag;
  12973. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  12974. #endif
  12975. }
  12976. /// Notification about the result of retrieving a mail attachment
  12977. /// 0245 <result>.B (ZC_MAIL_REQ_GET_ITEM)
  12978. /// result:
  12979. /// 0 = success
  12980. /// 1 = failure
  12981. /// 2 = too many items
  12982. /// 09f2 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ZENY_FROM_MAIL)
  12983. /// 09f4 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ITEM_FROM_MAIL)
  12984. void clif_mail_getattachment(map_session_data* sd, struct mail_message *msg, uint8 result, enum mail_attachment_type type) {
  12985. #if PACKETVER < 20150513
  12986. int32 fd = sd->fd;
  12987. WFIFOHEAD(fd,packet_len(0x245));
  12988. WFIFOW(fd,0) = 0x245;
  12989. WFIFOB(fd,2) = result;
  12990. WFIFOSET(fd,packet_len(0x245));
  12991. #else
  12992. switch( type ){
  12993. case MAIL_ATT_ITEM:
  12994. case MAIL_ATT_ZENY:
  12995. break;
  12996. default:
  12997. return;
  12998. }
  12999. PACKET_ZC_ACK_ITEM_FROM_MAIL p = { 0 };
  13000. p.PacketType = type == MAIL_ATT_ZENY ? rodexgetzeny : rodexgetitem;
  13001. p.MailID = msg->id;
  13002. p.opentype = msg->type;
  13003. p.result = result;
  13004. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13005. clif_Mail_refreshinbox( sd, msg->type, 0 );
  13006. #endif
  13007. }
  13008. /// Notification about the result of sending a mail
  13009. /// 0249 <result>.B (ZC_MAIL_REQ_SEND)
  13010. /// result:
  13011. /// 0 = success
  13012. /// 1 = recipinent does not exist
  13013. /// 09ed <result>.B (ZC_ACK_WRITE_MAIL)
  13014. void clif_Mail_send(map_session_data* sd, enum mail_send_result result){
  13015. #if PACKETVER < 20150513
  13016. int32 fd = sd->fd;
  13017. WFIFOHEAD(fd,packet_len(0x249));
  13018. WFIFOW(fd,0) = 0x249;
  13019. WFIFOB(fd,2) = result != WRITE_MAIL_SUCCESS;
  13020. WFIFOSET(fd,packet_len(0x249));
  13021. #else
  13022. PACKET_ZC_WRITE_MAIL_RESULT p = { 0 };
  13023. p.PacketType = rodexwriteresult;
  13024. p.result = result;
  13025. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13026. #endif
  13027. }
  13028. /// Notification about the result of deleting a mail.
  13029. /// 0257 <mail id>.L <result>.W (ZC_ACK_MAIL_DELETE)
  13030. /// result:
  13031. /// 0 = success
  13032. /// 1 = failure
  13033. // 09f6 <mail tab>.B <mail id>.Q (ZC_ACK_DELETE_MAIL)
  13034. void clif_mail_delete( map_session_data* sd, struct mail_message *msg, bool success ){
  13035. #if PACKETVER < 20150513
  13036. int32 fd = sd->fd;
  13037. WFIFOHEAD(fd, packet_len(0x257));
  13038. WFIFOW(fd,0) = 0x257;
  13039. WFIFOL(fd,2) = msg->id;
  13040. WFIFOW(fd,6) = !success;
  13041. WFIFOSET(fd, packet_len(0x257));
  13042. #else
  13043. // This is only a success notification
  13044. if( success ){
  13045. PACKET_ZC_ACK_DELETE_MAIL p = { 0 };
  13046. p.PacketType = rodexdelete;
  13047. p.opentype = msg->type;
  13048. p.MailID = msg->id;
  13049. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13050. }
  13051. #endif
  13052. }
  13053. /// Notification about the result of returning a mail (ZC_ACK_MAIL_RETURN).
  13054. /// 0274 <mail id>.L <result>.W
  13055. /// result:
  13056. /// 0 = success
  13057. /// 1 = failure
  13058. void clif_Mail_return(int32 fd, int32 mail_id, int16 fail)
  13059. {
  13060. WFIFOHEAD(fd,packet_len(0x274));
  13061. WFIFOW(fd,0) = 0x274;
  13062. WFIFOL(fd,2) = mail_id;
  13063. WFIFOW(fd,6) = fail;
  13064. WFIFOSET(fd,packet_len(0x274));
  13065. }
  13066. /// Notification about new mail.
  13067. /// 024a <mail id>.L <title>.40B <sender>.24B (ZC_MAIL_RECEIVE)
  13068. /// 09e7 <result>.B (ZC_NOTIFY_UNREADMAIL)
  13069. void clif_Mail_new(map_session_data* sd, int32 mail_id, const char *sender, const char *title){
  13070. #if PACKETVER < 20150513
  13071. int32 fd = sd->fd;
  13072. WFIFOHEAD(fd,packet_len(0x24a));
  13073. WFIFOW(fd,0) = 0x24a;
  13074. WFIFOL(fd,2) = mail_id;
  13075. safestrncpy(WFIFOCP(fd,6), title, MAIL_TITLE_LENGTH);
  13076. safestrncpy(WFIFOCP(fd,46), sender, NAME_LENGTH);
  13077. WFIFOSET(fd,packet_len(0x24a));
  13078. #else
  13079. PACKET_ZC_NOTIFY_UNREADMAIL p = { 0 };
  13080. p.PacketType = rodexicon;
  13081. p.result = sd->mail.inbox.unread > 0 || sd->mail.inbox.unchecked > 0; // unread
  13082. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13083. #endif
  13084. }
  13085. /// Opens/closes the mail window (ZC_MAIL_WINDOWS).
  13086. /// 0260 <type>.L
  13087. /// type:
  13088. /// 0 = open
  13089. /// 1 = close
  13090. void clif_Mail_window(int32 fd, int32 flag)
  13091. {
  13092. WFIFOHEAD(fd,packet_len(0x260));
  13093. WFIFOW(fd,0) = 0x260;
  13094. WFIFOL(fd,2) = flag;
  13095. WFIFOSET(fd,packet_len(0x260));
  13096. }
  13097. /// Lists mails stored in inbox.
  13098. /// 0240 <packet len>.W <amount>.L { <mail id>.L <title>.40B <read>.B <sender>.24B <time>.L }*amount (ZC_MAIL_REQ_GET_LIST)
  13099. /// read:
  13100. /// 0 = unread
  13101. /// 1 = read
  13102. /// 09f0 <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST)
  13103. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13104. /// 0a7d <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST2)
  13105. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13106. /// 0ac2 <packet len>.W <unknown>.B (ZC_ACK_MAIL_LIST3)
  13107. /// { <type>.B <mail id>.Q <read>.B <type>.B <sender>.24B <expires>.L <title length>.W <title>.?B }*
  13108. void clif_Mail_refreshinbox(map_session_data *sd,enum mail_inbox_type type,int64 mailID){
  13109. #if PACKETVER < 20150513
  13110. int32 fd = sd->fd;
  13111. struct mail_data *md = &sd->mail.inbox;
  13112. struct mail_message *msg;
  13113. int32 len, i, j;
  13114. len = 8 + (73 * md->amount);
  13115. WFIFOHEAD(fd,len);
  13116. WFIFOW(fd,0) = 0x240;
  13117. WFIFOW(fd,2) = len;
  13118. WFIFOL(fd,4) = md->amount;
  13119. for( i = j = 0; i < MAIL_MAX_INBOX && j < md->amount; i++ )
  13120. {
  13121. msg = &md->msg[i];
  13122. if (msg->id < 1)
  13123. continue;
  13124. WFIFOL(fd,8+73*j) = msg->id;
  13125. memcpy(WFIFOP(fd,12+73*j), msg->title, MAIL_TITLE_LENGTH);
  13126. WFIFOB(fd,52+73*j) = (msg->status != MAIL_UNREAD);
  13127. safestrncpy(WFIFOCP(fd,53+73*j), msg->send_name, NAME_LENGTH);
  13128. WFIFOL(fd,77+73*j) = (uint32)msg->timestamp;
  13129. j++;
  13130. }
  13131. WFIFOSET(fd,len);
  13132. if( md->full ) {// TODO: is this official?
  13133. char output[100];
  13134. safesnprintf(output,sizeof(output),"Inbox is full (Max %d). Delete some mails.", MAIL_MAX_INBOX);
  13135. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  13136. }
  13137. #else
  13138. int32 fd = sd->fd;
  13139. struct mail_data *md = &sd->mail.inbox;
  13140. struct mail_message *msg;
  13141. int32 i, j, k, offset, titleLength;
  13142. uint8 mailType, amount, remaining;
  13143. uint32 now = (uint32)time(nullptr);
  13144. #if PACKETVER >= 20170419
  13145. int32 cmd = 0xac2;
  13146. #elif PACKETVER >= 20160601
  13147. int32 cmd = 0xa7d;
  13148. #else
  13149. int32 cmd = 0x9f0;
  13150. #endif
  13151. if( battle_config.mail_daily_count ){
  13152. mail_refresh_remaining_amount(sd);
  13153. }
  13154. #if PACKETVER >= 20170419
  13155. // Always send all
  13156. i = md->amount;
  13157. #else
  13158. // If a starting mail id was sent
  13159. if( mailID != 0 ){
  13160. ARR_FIND( 0, md->amount, i, md->msg[i].id == mailID );
  13161. // Unknown mail
  13162. if( i == md->amount ){
  13163. // Ignore the request for now
  13164. return; // TODO: Should we just show the first page instead?
  13165. }
  13166. // It was actually the oldest/first mail, there is no further page
  13167. if( i == 0 ){
  13168. return;
  13169. }
  13170. // We actually want the next/older mail
  13171. i -= 1;
  13172. }else{
  13173. i = md->amount;
  13174. }
  13175. #endif
  13176. // Count the remaining mails from the starting mail or the beginning
  13177. // Only count mails of the target type(before 2017-04-19) and those that should not have been deleted already
  13178. for( j = i, remaining = 0; j >= 0; j-- ){
  13179. msg = &md->msg[j];
  13180. if (msg->id < 1)
  13181. continue;
  13182. #if PACKETVER < 20170419
  13183. if (msg->type != type)
  13184. continue;
  13185. #endif
  13186. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13187. continue;
  13188. remaining++;
  13189. }
  13190. #if PACKETVER >= 20170419
  13191. // Always send all
  13192. amount = remaining;
  13193. #else
  13194. if( remaining > MAIL_PAGE_SIZE ){
  13195. amount = MAIL_PAGE_SIZE;
  13196. }else{
  13197. amount = remaining;
  13198. }
  13199. #endif
  13200. WFIFOHEAD(fd, 7 + ((44 + MAIL_TITLE_LENGTH) * amount));
  13201. WFIFOW(fd, 0) = cmd;
  13202. #if PACKETVER >= 20170419
  13203. WFIFOB(fd, 4) = 1; // Unknown
  13204. offset = 5;
  13205. #else
  13206. WFIFOB(fd, 4) = type;
  13207. WFIFOB(fd, 5) = amount;
  13208. WFIFOB(fd, 6) = ( remaining < MAIL_PAGE_SIZE ); // last page
  13209. offset = 7;
  13210. #endif
  13211. for( amount = 0; i >= 0; i-- ){
  13212. msg = &md->msg[i];
  13213. if (msg->id < 1)
  13214. continue;
  13215. #if PACKETVER < 20170419
  13216. if (msg->type != type)
  13217. continue;
  13218. #endif
  13219. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13220. continue;
  13221. #if PACKETVER >= 20170419
  13222. WFIFOB(fd, offset) = msg->type;
  13223. offset += 1;
  13224. #endif
  13225. WFIFOQ(fd, offset + 0) = (uint64)msg->id;
  13226. WFIFOB(fd, offset + 8) = (msg->status != MAIL_UNREAD);
  13227. mailType = MAIL_TYPE_TEXT; // Normal letter
  13228. if( msg->zeny > 0 ){
  13229. mailType |= MAIL_TYPE_ZENY; // Mail contains zeny
  13230. }
  13231. for( k = 0; k < MAIL_MAX_ITEM; k++ ){
  13232. if( msg->item[k].nameid > 0 ){
  13233. mailType |= MAIL_TYPE_ITEM; // Mail contains items
  13234. break;
  13235. }
  13236. }
  13237. #if PACKETVER >= 20170419
  13238. // If it came from an npc?
  13239. if( !msg->send_id ){
  13240. mailType |= MAIL_TYPE_NPC;
  13241. }
  13242. #endif
  13243. WFIFOB(fd, offset + 9) = mailType;
  13244. safestrncpy(WFIFOCP(fd, offset + 10), msg->send_name, NAME_LENGTH);
  13245. #if PACKETVER < 20170419
  13246. // How much time has passed since you received the mail
  13247. WFIFOL(fd, offset + 34 ) = now - (uint32)msg->timestamp;
  13248. offset += 4;
  13249. #endif
  13250. // If automatic return/deletion of mails is enabled, notify the client when it will kick in
  13251. if( msg->scheduled_deletion > 0 ){
  13252. WFIFOL(fd, offset + 34) = (uint32)msg->scheduled_deletion - now;
  13253. }else{
  13254. // Fake the scheduled deletion to one year in the future
  13255. // Sadly the client always displays the scheduled deletion after 24 hours no matter how high this value gets [Lemongrass]
  13256. WFIFOL(fd, offset + 34) = 365 * 24 * 60 * 60;
  13257. }
  13258. WFIFOW(fd, offset + 38) = titleLength = (int16)(strlen(msg->title) + 1);
  13259. safestrncpy(WFIFOCP(fd, offset + 40), msg->title, titleLength);
  13260. offset += 40 + titleLength;
  13261. }
  13262. WFIFOW(fd, 2) = (int16)offset;
  13263. WFIFOSET(fd, offset);
  13264. #endif
  13265. }
  13266. /// Mail inbox list request.
  13267. /// 023f (CZ_MAIL_GET_LIST)
  13268. /// 09e8 <mail tab>.B <mail id>.Q (CZ_OPEN_MAILBOX)
  13269. /// 09ee <mail tab>.B <mail id>.Q (CZ_REQ_NEXT_MAIL_LIST)
  13270. /// 09ef <mail tab>.B <mail id>.Q (CZ_REQ_REFRESH_MAIL_LIST)
  13271. /// 0ac0 <mail id>.Q <unknown>.16B (CZ_OPEN_MAILBOX2)
  13272. /// 0ac1 <mail id>.Q <unknown>.16B (CZ_REQ_REFRESH_MAIL_LIST2)
  13273. void clif_parse_Mail_refreshinbox(int32 fd, map_session_data *sd){
  13274. if( mail_invalid_operation( sd ) ){
  13275. return;
  13276. }
  13277. #if PACKETVER < 20150513
  13278. struct mail_data* md = &sd->mail.inbox;
  13279. if( md->amount < MAIL_MAX_INBOX && (md->full || sd->mail.changed) )
  13280. intif_Mail_requestinbox(sd->status.char_id, 1, MAIL_INBOX_NORMAL);
  13281. else
  13282. clif_Mail_refreshinbox(sd, MAIL_INBOX_NORMAL,0);
  13283. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13284. mail_removezeny(sd, false);
  13285. #else
  13286. int32 cmd = RFIFOW(fd, 0);
  13287. #if PACKETVER < 20170419
  13288. uint8 openType = RFIFOB(fd, 2);
  13289. uint64 mailId = RFIFOQ(fd, 3);
  13290. #else
  13291. uint8 openType;
  13292. uint64 mailId = RFIFOQ(fd, 2);
  13293. int32 i;
  13294. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mailId);
  13295. if( i == MAIL_MAX_INBOX ){
  13296. openType = MAIL_INBOX_NORMAL;
  13297. mailId = 0;
  13298. }else{
  13299. openType = sd->mail.inbox.msg[i].type;
  13300. mailId = 0;
  13301. }
  13302. #endif
  13303. switch( openType ){
  13304. case MAIL_INBOX_NORMAL:
  13305. case MAIL_INBOX_ACCOUNT:
  13306. case MAIL_INBOX_RETURNED:
  13307. break;
  13308. default:
  13309. // Unknown type: ignore request
  13310. return;
  13311. }
  13312. if( sd->mail.changed || ( cmd == 0x9ef || cmd == 0xac1 ) ){
  13313. intif_Mail_requestinbox(sd->status.char_id, 1, (enum mail_inbox_type)openType);
  13314. return;
  13315. }
  13316. // If it is not a next page request
  13317. if( cmd != 0x9ee ){
  13318. mailId = 0;
  13319. }
  13320. clif_Mail_refreshinbox(sd,(enum mail_inbox_type)openType,mailId);
  13321. #endif
  13322. }
  13323. /// Opens a mail
  13324. /// 0242 <packet len>.W <mail id>.L <title>.40B <sender>.24B <time>.L <zeny>.L (ZC_MAIL_REQ_OPEN)
  13325. /// <amount>.L <name id>.W <item type>.W <identified>.B <damaged>.B <refine>.B
  13326. /// <card1>.W <card2>.W <card3>.W <card4>.W <message>.?B
  13327. /// 09eb <packet len>.W <type>.B <mail id>.Q <message length>.W <zeny>.Q <item count>.B (ZC_ACK_READ_MAIL)
  13328. /// { }*n
  13329. // TODO: Packet description => for repeated block
  13330. void clif_Mail_read( map_session_data *sd, int32 mail_id ){
  13331. int32 i, fd = sd->fd;
  13332. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13333. if( i == MAIL_MAX_INBOX ) {
  13334. clif_Mail_return(sd->fd, mail_id, 1); // Mail doesn't exist
  13335. ShowWarning("clif_parse_Mail_read: char '%s' trying to read a message not the inbox.\n", sd->status.name);
  13336. return;
  13337. } else {
  13338. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13339. struct item *item;
  13340. int16 msg_len;
  13341. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13342. if( msg_len == 0 ) {
  13343. strcpy(msg->body, "(no message)"); // TODO: confirm for RODEX
  13344. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13345. }
  13346. #if PACKETVER < 20150513
  13347. item = &msg->item[0];
  13348. int16 len = 101 + msg_len;
  13349. WFIFOHEAD(fd,len);
  13350. WFIFOW(fd,0) = 0x242;
  13351. WFIFOW(fd,2) = len;
  13352. WFIFOL(fd,4) = msg->id;
  13353. safestrncpy(WFIFOCP(fd,8), msg->title, MAIL_TITLE_LENGTH + 1);
  13354. safestrncpy(WFIFOCP(fd,48), msg->send_name, NAME_LENGTH + 1);
  13355. WFIFOL(fd,72) = 0;
  13356. WFIFOL(fd,76) = msg->zeny;
  13357. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13358. if( item->nameid && data != nullptr ) {
  13359. WFIFOL(fd,80) = item->amount;
  13360. WFIFOW(fd,84) = client_nameid( item->nameid );
  13361. WFIFOW(fd,86) = itemtype( item->nameid );
  13362. WFIFOB(fd,88) = item->identify;
  13363. WFIFOB(fd,89) = item->attribute;
  13364. WFIFOB(fd,90) = item->refine;
  13365. WFIFOW(fd,91) = item->card[0];
  13366. WFIFOW(fd,93) = item->card[1];
  13367. WFIFOW(fd,95) = item->card[2];
  13368. WFIFOW(fd,97) = item->card[3];
  13369. } else // no item, set all to zero
  13370. memset(WFIFOP(fd,80), 0x00, 19);
  13371. WFIFOB(fd,99) = (unsigned char)msg_len;
  13372. safestrncpy(WFIFOCP(fd,100), msg->body, msg_len + 1);
  13373. WFIFOSET(fd,len);
  13374. #else
  13375. msg_len += 1; // Zero Termination
  13376. PACKET_ZC_ACK_READ_RODEX* p = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX*>( packet_buffer );
  13377. p->PacketType = HEADER_ZC_ACK_READ_RODEX;
  13378. p->PacketLength = sizeof( *p );
  13379. p->opentype = msg->type;
  13380. p->MailID = msg->id;
  13381. p->TextcontentsLength = static_cast<decltype(p->TextcontentsLength)>( msg_len );
  13382. p->zeny = msg->zeny;
  13383. safestrncpy( WBUFCP( p, p->PacketLength ), msg->body, msg_len );
  13384. p->PacketLength += p->TextcontentsLength;
  13385. p->ItemCnt = 0;
  13386. for( int32 j = 0; j < MAIL_MAX_ITEM; j++ ){
  13387. item = &msg->item[j];
  13388. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13389. if( item->nameid > 0 && item->amount > 0 && data != nullptr ){
  13390. PACKET_ZC_ACK_READ_RODEX_SUB* mailitem = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX_SUB*>( WBUFP( p, p->PacketLength ) );
  13391. mailitem->ITID = client_nameid( item->nameid );
  13392. mailitem->count = item->amount;
  13393. mailitem->type = itemtype( item->nameid );
  13394. mailitem->IsIdentified = item->identify ? 1 : 0;
  13395. mailitem->IsDamaged = item->attribute ? 1 : 0;
  13396. mailitem->refiningLevel = item->refine;
  13397. mailitem->location = pc_equippoint_sub( sd, data.get() );
  13398. mailitem->viewSprite = data->look;
  13399. mailitem->bindOnEquip = item->bound ? 2 : data->flag.bindOnEquip ? 1 : 0;
  13400. clif_addcards( &mailitem->slot, item );
  13401. clif_add_random_options( mailitem->option_data, *item );
  13402. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  13403. mailitem->grade = item->enchantgrade;
  13404. #endif
  13405. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( *mailitem ) );
  13406. p->ItemCnt++;
  13407. }
  13408. }
  13409. clif_send( p, p->PacketLength, &sd->bl, SELF );
  13410. #endif
  13411. if (msg->status == MAIL_UNREAD) {
  13412. msg->status = MAIL_READ;
  13413. intif_Mail_read(mail_id);
  13414. clif_parse_Mail_refreshinbox(fd, sd);
  13415. sd->mail.inbox.unread--;
  13416. clif_Mail_new(sd, 0, msg->send_name, msg->title);
  13417. }
  13418. }
  13419. }
  13420. /// Request to open a mail.
  13421. /// 0241 <mail id>.L (CZ_MAIL_OPEN)
  13422. /// 09ea <mail tab>.B <mail id>.Q (CZ_REQ_READ_MAIL)
  13423. void clif_parse_Mail_read(int32 fd, map_session_data *sd){
  13424. #if PACKETVER < 20150513
  13425. int32 mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13426. #else
  13427. //uint8 openType = RFIFOB(fd, 2);
  13428. int32 mail_id = (int32)RFIFOQ(fd, 3);
  13429. #endif
  13430. if( mail_id <= 0 )
  13431. return;
  13432. if( mail_invalid_operation(sd) )
  13433. return;
  13434. clif_Mail_read(sd, mail_id);
  13435. }
  13436. /// Allow a player to begin writing a mail
  13437. /// 0a12 <receiver>.24B <success>.B (ZC_ACK_OPEN_WRITE_MAIL)
  13438. void clif_send_Mail_beginwrite_ack( map_session_data *sd, char* name, bool success ){
  13439. PACKET_ZC_ACK_OPEN_WRITE_MAIL p = { 0 };
  13440. p.PacketType = rodexopenwrite;
  13441. safestrncpy(p.receiveName, name, NAME_LENGTH);
  13442. p.result = success;
  13443. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13444. }
  13445. /// Request to start writing a mail
  13446. /// 0a08 <receiver>.24B (CZ_REQ_OPEN_WRITE_MAIL)
  13447. void clif_parse_Mail_beginwrite( int32 fd, map_session_data *sd ){
  13448. char name[NAME_LENGTH];
  13449. safestrncpy(name, RFIFOCP(fd, 2), NAME_LENGTH);
  13450. if( mail_invalid_operation( sd ) ){
  13451. return;
  13452. }
  13453. if( sd->state.storage_flag || sd->state.mail_writing || sd->state.trading ){
  13454. clif_send_Mail_beginwrite_ack(sd, name, false);
  13455. return;
  13456. }
  13457. mail_clear(sd);
  13458. sd->state.mail_writing = true;
  13459. clif_send_Mail_beginwrite_ack(sd, name,true);
  13460. }
  13461. /// Notification that the client cancelled writing a mail
  13462. /// 0a03 (CZ_REQ_CANCEL_WRITE_MAIL)
  13463. void clif_parse_Mail_cancelwrite( int32 fd, map_session_data *sd ){
  13464. sd->state.mail_writing = false;
  13465. }
  13466. /// Give the client information about the recipient, if available
  13467. /// 0a14 <char id>.L <class>.W <base level>.W (ZC_CHECK_RECEIVE_CHARACTER_NAME)
  13468. /// 0a51 <char id>.L <class>.W <base level>.W <name>.24B (ZC_CHECK_RECEIVE_CHARACTER_NAME2)
  13469. void clif_Mail_Receiver_Ack( map_session_data* sd, uint32 char_id, int16 class_, uint32 level, const char* name ){
  13470. #if PACKETVER >= 20141119
  13471. PACKET_ZC_CHECKNAME p = { 0 };
  13472. p.PacketType = HEADER_ZC_CHECKNAME;
  13473. p.CharId = char_id;
  13474. p.Class = class_;
  13475. p.BaseLevel = level;
  13476. #if PACKETVER >= 20160302
  13477. strncpy(p.Name, name, NAME_LENGTH);
  13478. #endif
  13479. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13480. #endif
  13481. }
  13482. /// Request information about the recipient
  13483. /// 0a13 <name>.24B (CZ_CHECK_RECEIVE_CHARACTER_NAME)
  13484. void clif_parse_Mail_Receiver_Check(int32 fd, map_session_data *sd) {
  13485. #if PACKETVER >= 20140423
  13486. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13487. const PACKET_CZ_CHECKNAME2* p = reinterpret_cast<PACKET_CZ_CHECKNAME2*>( RFIFOP( fd, 0 ) );
  13488. #else
  13489. const PACKET_CZ_CHECKNAME1* p = reinterpret_cast<PACKET_CZ_CHECKNAME1*>( RFIFOP( fd, 0 ) );
  13490. #endif
  13491. static char name[NAME_LENGTH];
  13492. if( mail_invalid_operation( sd ) ){
  13493. return;
  13494. }
  13495. safestrncpy(name, p->Name, NAME_LENGTH);
  13496. intif_mail_checkreceiver(sd, name);
  13497. #endif
  13498. }
  13499. /// Request to receive mail's attachment.
  13500. /// 0244 <mail id>.L (CZ_MAIL_GET_ITEM)
  13501. /// 09f1 <mail id>.Q <mail tab>.B (CZ_REQ_ZENY_FROM_MAIL)
  13502. /// 09f3 <mail id>.Q <mail tab>.B (CZ_REQ_ITEM_FROM_MAIL)
  13503. void clif_parse_Mail_getattach( int32 fd, map_session_data *sd ){
  13504. int32 i;
  13505. struct mail_message* msg;
  13506. #if PACKETVER < 20150513
  13507. int32 mail_id = RFIFOL(fd, packet_db[RFIFOW(fd, 0)].pos[0]);
  13508. int32 attachment = MAIL_ATT_ALL;
  13509. #else
  13510. uint16 packet_id = RFIFOW(fd, 0);
  13511. int32 mail_id = (int32)RFIFOQ(fd, 2);
  13512. //int32 openType = RFIFOB(fd, 10);
  13513. int32 attachment = packet_id == 0x9f1 ? MAIL_ATT_ZENY : packet_id == 0x9f3 ? MAIL_ATT_ITEM : MAIL_ATT_NONE;
  13514. #endif
  13515. if( !chrif_isconnected() )
  13516. return;
  13517. if( mail_id <= 0 )
  13518. return;
  13519. if( mail_invalid_operation(sd) )
  13520. return;
  13521. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13522. if( i == MAIL_MAX_INBOX )
  13523. return;
  13524. msg = &sd->mail.inbox.msg[i];
  13525. if( attachment&MAIL_ATT_ZENY && msg->zeny < 1 ){
  13526. attachment &= ~MAIL_ATT_ZENY;
  13527. }
  13528. if( attachment&MAIL_ATT_ITEM ){
  13529. ARR_FIND(0, MAIL_MAX_ITEM, i, msg->item[i].nameid > 0 || msg->item[i].amount > 0);
  13530. // No items were found
  13531. if( i == MAIL_MAX_ITEM ){
  13532. attachment &= ~MAIL_ATT_ITEM;
  13533. }
  13534. }
  13535. // Either no attachment requested at all or there are no zeny or items in the mail
  13536. if( attachment == MAIL_ATT_NONE ){
  13537. return;
  13538. }
  13539. if( attachment&MAIL_ATT_ZENY ){
  13540. if( ( msg->zeny + sd->status.zeny + sd->mail.pending_zeny ) > MAX_ZENY ){
  13541. clif_mail_getattachment(sd, msg, 1, MAIL_ATT_ZENY); //too many zeny
  13542. return;
  13543. }else{
  13544. // Store the pending zeny (required for the "retrieve all" feature)
  13545. sd->mail.pending_zeny += msg->zeny;
  13546. // To make sure another request fails
  13547. msg->zeny = 0;
  13548. }
  13549. }
  13550. if( attachment&MAIL_ATT_ITEM ){
  13551. int32 slots = 0, totalweight = 0;
  13552. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  13553. struct item* item = &msg->item[i];
  13554. if( item->nameid > 0 && item->amount > 0 ){
  13555. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13556. if(data == nullptr)
  13557. continue;
  13558. switch( pc_checkadditem(sd, item->nameid, item->amount) ){
  13559. case CHKADDITEM_NEW:
  13560. slots += data->inventorySlotNeeded( item->amount );
  13561. break;
  13562. case CHKADDITEM_OVERAMOUNT:
  13563. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13564. return;
  13565. }
  13566. totalweight += data->weight * item->amount;
  13567. }
  13568. }
  13569. if( ( totalweight + sd->weight + sd->mail.pending_weight ) > sd->max_weight ){
  13570. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13571. return;
  13572. }else if( pc_inventoryblank(sd) < ( slots + sd->mail.pending_slots ) ){
  13573. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13574. return;
  13575. }
  13576. // To make sure another request fails
  13577. memset(msg->item, 0, MAIL_MAX_ITEM*sizeof(struct item));
  13578. // Store the pending weight (required for the "retrieve all" feature)
  13579. sd->mail.pending_weight += totalweight;
  13580. // Store the pending slots (required for the "retrieve all" feature)
  13581. sd->mail.pending_slots += slots;
  13582. }
  13583. intif_mail_getattach(sd,msg, (enum mail_attachment_type)attachment);
  13584. }
  13585. /// Request to delete a mail.
  13586. /// 0243 <mail id>.L (CZ_MAIL_DELETE)
  13587. /// 09f5 <mail tab>.B <mail id>.Q (CZ_REQ_DELETE_MAIL)
  13588. void clif_parse_Mail_delete(int32 fd, map_session_data *sd){
  13589. #if PACKETVER < 20150513
  13590. int32 mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13591. #else
  13592. //int32 openType = RFIFOB(fd, 2);
  13593. int32 mail_id = (int32)RFIFOQ(fd, 3);
  13594. #endif
  13595. int32 i, j;
  13596. if( !chrif_isconnected() )
  13597. return;
  13598. if( mail_id <= 0 )
  13599. return;
  13600. if( mail_invalid_operation(sd) )
  13601. return;
  13602. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13603. if (i < MAIL_MAX_INBOX) {
  13604. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13605. // can't delete mail without removing zeny first
  13606. if( msg->zeny > 0 ){
  13607. clif_mail_delete(sd, msg, false);
  13608. return;
  13609. }
  13610. // can't delete mail without removing attachment(s) first
  13611. for( j = 0; j < MAIL_MAX_ITEM; j++ ){
  13612. if( msg->item[j].nameid > 0 && msg->item[j].amount > 0 ){
  13613. clif_mail_delete(sd, msg, false);
  13614. return;
  13615. }
  13616. }
  13617. if( intif_Mail_delete(sd->status.char_id, mail_id) && msg->status == MAIL_UNREAD ){
  13618. sd->mail.inbox.unread--;
  13619. clif_Mail_new(sd,0,nullptr,nullptr);
  13620. }
  13621. }
  13622. }
  13623. /// Request to return a mail.
  13624. /// 0273 <mail id>.L <receive name>.24B (CZ_REQ_MAIL_RETURN)
  13625. void clif_parse_Mail_return(int32 fd, map_session_data *sd){
  13626. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13627. const PACKET_CZ_RODEX_RETURN* p = reinterpret_cast<PACKET_CZ_RODEX_RETURN*>( RFIFOP( fd, 0 ) );
  13628. //ShowDump( p, sizeof( p ) );
  13629. int32 mail_id = p->msgId;
  13630. // not supported for now
  13631. return;
  13632. #else
  13633. int32 mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13634. #endif
  13635. //char *rec_name = RFIFOP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  13636. int32 i;
  13637. if( mail_id <= 0 )
  13638. return;
  13639. if( mail_invalid_operation(sd) )
  13640. return;
  13641. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13642. if( i < MAIL_MAX_INBOX && sd->mail.inbox.msg[i].send_id != 0 )
  13643. intif_Mail_return(sd->status.char_id, mail_id);
  13644. else
  13645. clif_Mail_return(sd->fd, mail_id, 1);
  13646. }
  13647. /// Request to add an item or Zeny to mail.
  13648. /// 0247 <index>.W <amount>.L (CZ_MAIL_ADD_ITEM)
  13649. /// 0a04 <index>.W <amount>.W (CZ_REQ_ADD_ITEM_TO_MAIL)
  13650. void clif_parse_Mail_setattach(int32 fd, map_session_data *sd){
  13651. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13652. uint16 idx = RFIFOW(fd,info->pos[0]);
  13653. #if PACKETVER < 20150513
  13654. int32 amount = RFIFOL(fd,info->pos[1]);
  13655. #else
  13656. int32 amount = RFIFOW(fd,info->pos[1]);
  13657. #endif
  13658. unsigned char flag;
  13659. if( !chrif_isconnected() )
  13660. return;
  13661. if (amount < 0 || server_index(idx) >= MAX_INVENTORY)
  13662. return;
  13663. if (sd->inventory_data[server_index(idx)] == nullptr)
  13664. return;
  13665. if( mail_invalid_operation( sd ) ){
  13666. return;
  13667. }
  13668. flag = mail_setitem(sd, idx, amount);
  13669. if( flag == MAIL_ATTACH_EQUIPSWITCH ){
  13670. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  13671. }else{
  13672. clif_Mail_setattachment(sd,idx,amount,flag);
  13673. }
  13674. }
  13675. /// Remove an item from a mail
  13676. /// 0a07 <result>.B <index>.W <amount>.W <weight>.W
  13677. void clif_mail_removeitem( map_session_data* sd, bool success, int32 index, int32 amount ){
  13678. PACKET_ZC_ACK_REMOVE_ITEM_MAIL p = { 0 };
  13679. p.PacketType = rodexremoveitem;
  13680. p.result = success;
  13681. p.index = index;
  13682. p.cnt = amount;
  13683. int32 total = 0;
  13684. for( int32 i = 0; i < MAIL_MAX_ITEM; i++ ){
  13685. if( sd->mail.item[i].nameid == 0 ){
  13686. break;
  13687. }
  13688. total += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  13689. }
  13690. p.weight = total;
  13691. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13692. }
  13693. /// Request to reset mail item and/or Zeny
  13694. /// 0246 <type>.W (CZ_MAIL_RESET_ITEM)
  13695. /// type:
  13696. /// 0 = reset all
  13697. /// 1 = remove item
  13698. /// 2 = remove zeny
  13699. /// 0a06 <index>.W <amount>.W (CZ_REQ_REMOVE_ITEM_MAIL)
  13700. void clif_parse_Mail_winopen(int32 fd, map_session_data *sd)
  13701. {
  13702. #if PACKETVER < 20150513
  13703. int32 type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13704. if (type == 0 || type == 1)
  13705. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13706. if (type == 0 || type == 2)
  13707. mail_removezeny(sd, false);
  13708. #else
  13709. uint16 index = RFIFOW(fd, 2);
  13710. uint16 count = RFIFOW(fd, 4);
  13711. mail_removeitem(sd,0,index,count);
  13712. #endif
  13713. }
  13714. /// Request to send mail
  13715. /// 0248 <packet len>.W <recipient>.24B <title>.40B <body len>.B <body>.?B (CZ_MAIL_SEND)
  13716. /// 09ec <packet len>.W <recipient>.24B <sender>.24B <zeny>.Q <title length>.W <body length>.W <title>.?B <body>.?B (CZ_REQ_WRITE_MAIL)
  13717. /// 0a6e <packet len>.W <recipient>.24B <sender>.24B <zeny>.Q <title length>.W <body length>.W <char id>.L <title>.?B <body>.?B (CZ_REQ_WRITE_MAIL2)
  13718. void clif_parse_Mail_send(int32 fd, map_session_data *sd){
  13719. #if PACKETVER < 20150513
  13720. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13721. if( !chrif_isconnected() )
  13722. return;
  13723. if( RFIFOW(fd,info->pos[0]) < 69 ) {
  13724. ShowWarning("Invalid Msg Len from account %d.\n", sd->status.account_id);
  13725. return;
  13726. }
  13727. mail_send(sd, RFIFOCP(fd,info->pos[1]), RFIFOCP(fd,info->pos[2]), RFIFOCP(fd,info->pos[4]), RFIFOB(fd,info->pos[3]));
  13728. #else
  13729. uint16 length = RFIFOW(fd, 2);
  13730. if( length < 0x3e ){
  13731. ShowWarning("Too short...\n");
  13732. clif_Mail_send(sd, WRITE_MAIL_FAILED);
  13733. return;
  13734. }
  13735. if( mail_invalid_operation( sd ) ){
  13736. return;
  13737. }
  13738. // Forged request without a begin writing packet?
  13739. if( !sd->state.mail_writing ){
  13740. return; // Ignore it
  13741. }
  13742. char receiver[NAME_LENGTH];
  13743. safestrncpy(receiver, RFIFOCP(fd, 4), NAME_LENGTH);
  13744. // char sender[NAME_LENGTH];
  13745. // safestrncpy(sender, RFIFOCP(fd, 28), NAME_LENGTH);
  13746. uint64 zeny = RFIFOQ(fd, 52);
  13747. uint16 titleLength = RFIFOW(fd, 60);
  13748. uint16 textLength = RFIFOW(fd, 62);
  13749. uint16 realTitleLength = min(titleLength, MAIL_TITLE_LENGTH);
  13750. uint16 realTextLength = min(textLength, MAIL_BODY_LENGTH);
  13751. char title[MAIL_TITLE_LENGTH];
  13752. char text[MAIL_BODY_LENGTH];
  13753. #if PACKETVER <= 20160330
  13754. safestrncpy(title, RFIFOCP(fd, 64), realTitleLength);
  13755. safestrncpy(text, RFIFOCP(fd, 64 + titleLength), realTextLength);
  13756. #else
  13757. // 64 = <char id>.L
  13758. safestrncpy(title, RFIFOCP(fd, 68), realTitleLength);
  13759. safestrncpy(text, RFIFOCP(fd, 68 + titleLength), realTextLength);
  13760. #endif
  13761. if( zeny > 0 ){
  13762. if( mail_setitem(sd,0,(uint32)zeny) != MAIL_ATTACH_SUCCESS ){
  13763. clif_Mail_send(sd,WRITE_MAIL_FAILED);
  13764. return;
  13765. }
  13766. }
  13767. mail_send(sd, receiver, title, text, realTextLength);
  13768. #endif
  13769. }
  13770. /// AUCTION SYSTEM
  13771. /// By Zephyrus
  13772. ///
  13773. /// Opens/closes the auction window (ZC_AUCTION_WINDOWS).
  13774. /// 025f <type>.L
  13775. /// type:
  13776. /// 0 = open
  13777. /// 1 = close
  13778. void clif_Auction_openwindow(map_session_data *sd)
  13779. {
  13780. int32 fd = sd->fd;
  13781. if( sd->state.storage_flag || sd->state.vending || sd->state.buyingstore || sd->state.trading )
  13782. return;
  13783. if( !battle_config.feature_auction )
  13784. return;
  13785. WFIFOHEAD(fd,packet_len(0x25f));
  13786. WFIFOW(fd,0) = 0x25f;
  13787. WFIFOL(fd,2) = 0;
  13788. WFIFOSET(fd,packet_len(0x25f));
  13789. }
  13790. /// Returns auction item search results (ZC_AUCTION_ITEM_REQ_SEARCH).
  13791. /// 0252 <packet len>.W <pages>.L <count>.L { <auction id>.L <seller name>.24B <name id>.W <type>.L <amount>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W <now price>.L <max price>.L <buyer name>.24B <delete time>.L }*
  13792. void clif_Auction_results(map_session_data *sd, int16 count, int16 pages, uint8 *buf)
  13793. {
  13794. int32 i, fd = sd->fd, len = sizeof(struct auction_data);
  13795. struct auction_data auction;
  13796. WFIFOHEAD(fd,12 + (count * 83));
  13797. WFIFOW(fd,0) = 0x252;
  13798. WFIFOW(fd,2) = 12 + (count * 83);
  13799. WFIFOL(fd,4) = pages;
  13800. WFIFOL(fd,8) = count;
  13801. for( i = 0; i < count; i++ ) {
  13802. int32 k = 12 + (i * 83);
  13803. memcpy(&auction, RBUFP(buf,i * len), len);
  13804. WFIFOL(fd,k) = auction.auction_id;
  13805. safestrncpy(WFIFOCP(fd,4+k), auction.seller_name, NAME_LENGTH);
  13806. WFIFOW(fd,28+k) = client_nameid( auction.item.nameid );
  13807. WFIFOL(fd,30+k) = auction.type;
  13808. WFIFOW(fd,34+k) = auction.item.amount; // Always 1
  13809. WFIFOB(fd,36+k) = auction.item.identify;
  13810. WFIFOB(fd,37+k) = auction.item.attribute;
  13811. WFIFOB(fd,38+k) = auction.item.refine;
  13812. WFIFOW(fd,39+k) = auction.item.card[0];
  13813. WFIFOW(fd,41+k) = auction.item.card[1];
  13814. WFIFOW(fd,43+k) = auction.item.card[2];
  13815. WFIFOW(fd,45+k) = auction.item.card[3];
  13816. WFIFOL(fd,47+k) = auction.price;
  13817. WFIFOL(fd,51+k) = auction.buynow;
  13818. safestrncpy(WFIFOCP(fd,55+k), auction.buyer_name, NAME_LENGTH);
  13819. WFIFOL(fd,79+k) = (uint32)auction.timestamp;
  13820. }
  13821. WFIFOSET(fd,WFIFOW(fd,2));
  13822. }
  13823. /// Result from request to add an item (ZC_ACK_AUCTION_ADD_ITEM).
  13824. /// 0256 <index>.W <result>.B
  13825. /// result:
  13826. /// 0 = success
  13827. /// 1 = failure
  13828. static void clif_Auction_setitem(int32 fd, int32 index, bool fail)
  13829. {
  13830. WFIFOHEAD(fd,packet_len(0x256));
  13831. WFIFOW(fd,0) = 0x256;
  13832. WFIFOW(fd,2) = index;
  13833. WFIFOB(fd,4) = fail;
  13834. WFIFOSET(fd,packet_len(0x256));
  13835. }
  13836. /// Request to initialize 'new auction' data (CZ_AUCTION_CREATE).
  13837. /// 024b <type>.W
  13838. /// type:
  13839. /// 0 = create (any other action in auction window)
  13840. /// 1 = cancel (cancel pressed on register tab)
  13841. /// ? = junk, uninitialized value (ex. when switching between list filters)
  13842. void clif_parse_Auction_cancelreg(int32 fd, map_session_data *sd){
  13843. //int32 type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13844. if( sd->auction.amount > 0 )
  13845. clif_additem(sd, sd->auction.index, sd->auction.amount, 0);
  13846. sd->auction.amount = 0;
  13847. }
  13848. /// Request to add an item to the action (CZ_AUCTION_ADD_ITEM).
  13849. /// 024c <index>.W <count>.L
  13850. void clif_parse_Auction_setitem(int32 fd, map_session_data *sd){
  13851. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13852. int32 idx = RFIFOW(fd,info->pos[0]) - 2;
  13853. int32 amount = RFIFOL(fd,info->pos[1]); // Always 1
  13854. if( !battle_config.feature_auction )
  13855. return;
  13856. if( sd->auction.amount > 0 )
  13857. sd->auction.amount = 0;
  13858. if( idx < 0 || idx >= MAX_INVENTORY ) {
  13859. ShowWarning("Character %s trying to set invalid item index in auctions.\n", sd->status.name);
  13860. return;
  13861. }
  13862. if( amount != 1 || amount > sd->inventory.u.items_inventory[idx].amount ) { // By client, amount is always set to 1. Maybe this is a future implementation.
  13863. ShowWarning("Character %s trying to set invalid amount in auctions.\n", sd->status.name);
  13864. return;
  13865. }
  13866. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[idx].nameid);
  13867. if( id != nullptr && !(id->type == IT_ARMOR || id->type == IT_PETARMOR || id->type == IT_WEAPON || id->type == IT_CARD || id->type == IT_ETC || id->type == IT_SHADOWGEAR) )
  13868. { // Consumable or pets are not allowed
  13869. clif_Auction_setitem(sd->fd, idx, true);
  13870. return;
  13871. }
  13872. if( !pc_can_give_items(sd) || sd->inventory.u.items_inventory[idx].expire_time ||
  13873. !sd->inventory.u.items_inventory[idx].identify ||
  13874. (sd->inventory.u.items_inventory[idx].bound && !pc_can_give_bounded_items(sd)) ||
  13875. !itemdb_available(sd->inventory.u.items_inventory[idx].nameid) ||
  13876. !itemdb_canauction(&sd->inventory.u.items_inventory[idx],pc_get_group_level(sd)) ) { // Quest Item or something else
  13877. clif_Auction_setitem(sd->fd, idx, true);
  13878. return;
  13879. }
  13880. sd->auction.index = idx;
  13881. sd->auction.amount = amount;
  13882. clif_Auction_setitem(fd, idx + 2, false);
  13883. }
  13884. /// Result from an auction action (ZC_AUCTION_RESULT).
  13885. /// 0250 <result>.B
  13886. /// result:
  13887. /// 0 = You have failed to bid into the auction
  13888. /// 1 = You have successfully bid in the auction
  13889. /// 2 = The auction has been canceled
  13890. /// 3 = An auction with at least one bidder cannot be canceled
  13891. /// 4 = You cannot register more than 5 items in an auction at a time
  13892. /// 5 = You do not have enough Zeny to pay the Auction Fee
  13893. /// 6 = You have won the auction
  13894. /// 7 = You have failed to win the auction
  13895. /// 8 = You do not have enough Zeny
  13896. /// 9 = You cannot place more than 5 bids at a time
  13897. void clif_Auction_message(int32 fd, unsigned char flag)
  13898. {
  13899. WFIFOHEAD(fd,packet_len(0x250));
  13900. WFIFOW(fd,0) = 0x250;
  13901. WFIFOB(fd,2) = flag;
  13902. WFIFOSET(fd,packet_len(0x250));
  13903. }
  13904. /// Result of the auction close request (ZC_AUCTION_ACK_MY_SELL_STOP).
  13905. /// 025e <result>.W
  13906. /// result:
  13907. /// 0 = You have ended the auction
  13908. /// 1 = You cannot end the auction
  13909. /// 2 = Auction ID is incorrect
  13910. void clif_Auction_close(int32 fd, unsigned char flag)
  13911. {
  13912. WFIFOHEAD(fd,packet_len(0x25e));
  13913. WFIFOW(fd,0) = 0x25d; // BUG: The client identifies this packet as 0x25d (CZ_AUCTION_REQ_MY_SELL_STOP)
  13914. WFIFOW(fd,2) = flag;
  13915. WFIFOSET(fd,packet_len(0x25e));
  13916. }
  13917. /// Request to add an auction.
  13918. /// 024d <now money>.L <max money>.L <delete hour>.W (CZ_AUCTION_ADD)
  13919. void clif_parse_Auction_register( int32 fd, map_session_data* sd ){
  13920. #if PACKETVER >= 20050808
  13921. const PACKET_CZ_AUCTION_ADD* p = reinterpret_cast<PACKET_CZ_AUCTION_ADD*>( RFIFOP( fd, 0 ) );
  13922. struct auction_data auction;
  13923. if( !battle_config.feature_auction )
  13924. return;
  13925. auction.price = p->now_money;
  13926. auction.buynow = p->max_money;
  13927. auction.hours = p->hours;
  13928. // Invalid Situations...
  13929. if( sd->auction.amount < 1 ) {
  13930. ShowWarning("Character %s trying to register auction without item.\n", sd->status.name);
  13931. return;
  13932. }
  13933. if( auction.price >= auction.buynow ) {
  13934. ShowWarning("Character %s trying to alter auction prices.\n", sd->status.name);
  13935. return;
  13936. }
  13937. if( auction.hours < 1 || auction.hours > 48 ) {
  13938. ShowWarning("Character %s trying to enter an invalid time for auction.\n", sd->status.name);
  13939. return;
  13940. }
  13941. // Auction checks...
  13942. if( sd->inventory.u.items_inventory[sd->auction.index].bound && !pc_can_give_bounded_items(sd) ) {
  13943. clif_displaymessage(sd->fd, msg_txt(sd,293));
  13944. clif_Auction_message(fd, 2); // The auction has been canceled
  13945. return;
  13946. }
  13947. if( sd->status.zeny < (auction.hours * battle_config.auction_feeperhour) ) {
  13948. clif_Auction_message(fd, 5); // You do not have enough zeny to pay the Auction Fee.
  13949. return;
  13950. }
  13951. if( auction.buynow > battle_config.auction_maximumprice ) { // Zeny Limits
  13952. auction.buynow = battle_config.auction_maximumprice;
  13953. if( auction.price >= auction.buynow )
  13954. auction.price = auction.buynow - 1;
  13955. }
  13956. auction.auction_id = 0;
  13957. auction.seller_id = sd->status.char_id;
  13958. safestrncpy(auction.seller_name, sd->status.name, sizeof(auction.seller_name));
  13959. auction.buyer_id = 0;
  13960. memset(auction.buyer_name, '\0', sizeof(auction.buyer_name));
  13961. if( sd->inventory.u.items_inventory[sd->auction.index].nameid == 0 || sd->inventory.u.items_inventory[sd->auction.index].amount < sd->auction.amount )
  13962. {
  13963. clif_Auction_message(fd, 2); // The auction has been canceled
  13964. return;
  13965. }
  13966. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[sd->auction.index].nameid);
  13967. if( id == nullptr )
  13968. { // Just in case
  13969. clif_Auction_message(fd, 2); // The auction has been canceled
  13970. return;
  13971. }
  13972. safestrncpy(auction.item_name, id->ename.c_str(), sizeof(auction.item_name));
  13973. auction.type = id->type;
  13974. memcpy(&auction.item, &sd->inventory.u.items_inventory[sd->auction.index], sizeof(struct item));
  13975. auction.item.amount = 1;
  13976. auction.timestamp = 0;
  13977. if( !intif_Auction_register(&auction) )
  13978. clif_Auction_message(fd, 4); // No Char Server? lets say something to the client
  13979. else
  13980. {
  13981. int32 zeny = auction.hours*battle_config.auction_feeperhour;
  13982. pc_delitem(sd, sd->auction.index, sd->auction.amount, 1, 6, LOG_TYPE_AUCTION);
  13983. sd->auction.amount = 0;
  13984. pc_payzeny(sd, zeny, LOG_TYPE_AUCTION);
  13985. }
  13986. #endif
  13987. }
  13988. /// Cancels an auction (CZ_AUCTION_ADD_CANCEL).
  13989. /// 024e <auction id>.L
  13990. void clif_parse_Auction_cancel(int32 fd, map_session_data *sd){
  13991. uint32 auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13992. intif_Auction_cancel(sd->status.char_id, auction_id);
  13993. }
  13994. /// Closes an auction (CZ_AUCTION_REQ_MY_SELL_STOP).
  13995. /// 025d <auction id>.L
  13996. void clif_parse_Auction_close(int32 fd, map_session_data *sd){
  13997. uint32 auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13998. intif_Auction_close(sd->status.char_id, auction_id);
  13999. }
  14000. /// Places a bid on an auction.
  14001. /// 024f <auction id>.L <money>.L (CZ_AUCTION_BUY)
  14002. void clif_parse_Auction_bid( int32 fd, map_session_data* sd ){
  14003. #if PACKETVER >= 20050718
  14004. const PACKET_CZ_AUCTION_BUY* p = reinterpret_cast<PACKET_CZ_AUCTION_BUY*>( RFIFOP( fd, 0 ) );
  14005. if( !pc_can_give_items(sd) ){
  14006. clif_displaymessage( sd->fd, msg_txt( sd, 246 ) ); // Your GM level doesn't authorize you to perform this action.
  14007. return;
  14008. }
  14009. // Check if char server is down (bugreport:1138)
  14010. if( CheckForCharServer() ){
  14011. clif_Auction_message(fd, 0); // You have failed to bid into the auction
  14012. return;
  14013. }
  14014. if( pc_payzeny( sd, p->money, LOG_TYPE_AUCTION ) != 0 ){
  14015. clif_Auction_message( fd, 8 ); // You do not have enough zeny
  14016. return;
  14017. }
  14018. intif_Auction_bid( sd->status.char_id, sd->status.name, p->auction_id, p->money );
  14019. #endif
  14020. }
  14021. /// Auction Search.
  14022. /// 0251 <search type>.W <auction id>.L <search text>.24B <page number>.W (CZ_AUCTION_ITEM_SEARCH)
  14023. /// search type:
  14024. /// 0 = armor
  14025. /// 1 = weapon
  14026. /// 2 = card
  14027. /// 3 = misc
  14028. /// 4 = name search
  14029. /// 5 = auction id search
  14030. void clif_parse_Auction_search(int32 fd, map_session_data* sd){
  14031. #if PACKETVER >= 20051107
  14032. const PACKET_CZ_AUCTION_ITEM_SEARCH* p = reinterpret_cast<PACKET_CZ_AUCTION_ITEM_SEARCH*>( RFIFOP( fd, 0 ) );
  14033. if( !battle_config.feature_auction )
  14034. return;
  14035. clif_parse_Auction_cancelreg(fd, sd);
  14036. char search_text[NAME_LENGTH];
  14037. safestrncpy( search_text, p->text, sizeof( search_text ) );
  14038. intif_Auction_requestlist( sd->status.char_id, p->type, p->auction_id, search_text, p->page );
  14039. #endif
  14040. }
  14041. /// Requests list of own currently active bids or auctions (CZ_AUCTION_REQ_MY_INFO).
  14042. /// 025c <type>.W
  14043. /// type:
  14044. /// 0 = sell (own auctions)
  14045. /// 1 = buy (own bids)
  14046. void clif_parse_Auction_buysell(int32 fd, map_session_data* sd)
  14047. {
  14048. int16 type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) + 6;
  14049. if( !battle_config.feature_auction )
  14050. return;
  14051. clif_parse_Auction_cancelreg(fd, sd);
  14052. intif_Auction_requestlist(sd->status.char_id, type, 0, "", 1);
  14053. }
  14054. /// CASH/POINT SHOP
  14055. ///
  14056. void clif_cashshop_open( map_session_data* sd, int32 tab ){
  14057. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14058. nullpo_retv( sd );
  14059. PACKET_ZC_SE_CASHSHOP_OPEN p = {};
  14060. p.packetType = HEADER_ZC_SE_CASHSHOP_OPEN;
  14061. p.cashPoints = sd->cashPoints;
  14062. p.kafraPoints = sd->kafraPoints;
  14063. #if PACKETVER_MAIN_NUM >= 20200129 || PACKETVER_RE_NUM >= 20200205 || PACKETVER_ZERO_NUM >= 20191224
  14064. p.tab = tab;
  14065. #endif
  14066. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14067. #endif
  14068. }
  14069. void clif_parse_cashshop_open_request( int32 fd, map_session_data* sd ){
  14070. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14071. nullpo_retv( sd );
  14072. int32 tab = 0;
  14073. #if PACKETVER >= 20191224
  14074. const PACKET_CZ_SE_CASHSHOP_OPEN2* p = reinterpret_cast<PACKET_CZ_SE_CASHSHOP_OPEN2*>( RFIFOP( fd, 0 ) );
  14075. tab = p->tab;
  14076. #endif
  14077. if (map_getmapflag(sd->bl.m, MF_NOCASHSHOP)) {
  14078. clif_displaymessage(fd, msg_txt(sd, 451)); // Cash Shop is disabled on this map.
  14079. return;
  14080. }
  14081. sd->state.cashshop_open = true;
  14082. sd->npc_shopid = -1; // Set npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14083. clif_cashshop_open( sd, tab );
  14084. #endif
  14085. }
  14086. void clif_parse_cashshop_close( int32 fd, map_session_data* sd ){
  14087. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14088. sd->state.cashshop_open = false;
  14089. sd->npc_shopid = 0; // Reset npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14090. // No need to do anything here
  14091. #endif
  14092. }
  14093. //0846 <tabid>.W (CZ_REQ_SE_CASH_TAB_CODE))
  14094. //08c0 <len>.W <openIdentity>.L <itemcount>.W (ZC_ACK_SE_CASH_ITEM_LIST2)
  14095. void clif_parse_CashShopReqTab(int32 fd, map_session_data *sd) {
  14096. #if PACKETVER_MAIN_NUM >= 20100824 || PACKETVER_RE_NUM >= 20100824 || defined(PACKETVER_ZERO)
  14097. const PACKET_CZ_REQ_SE_CASH_TAB_CODE* p_in = reinterpret_cast<PACKET_CZ_REQ_SE_CASH_TAB_CODE*>( RFIFOP( fd, 0 ) );
  14098. // TODO: most likely wrong answer packet. Most likely HEADER_ZC_ACK_SE_CASH_ITEM_LIST (0x847) would be correct [Lemongrass]
  14099. // [4144] packet exists only in 2011 and was dropped after
  14100. #if PACKETVER >= 20110222 && PACKETVER < 20120000
  14101. std::shared_ptr<s_cash_item_tab> tab = cash_shop_db.find( static_cast<e_cash_shop_tab>( p_in->tab ) );
  14102. if( tab == nullptr ){
  14103. return;
  14104. }
  14105. // Skip empty tabs, the client only expects filled ones
  14106. if( tab->items.empty() ){
  14107. return;
  14108. }
  14109. #if !(PACKETVER_SUPPORTS_SALES)
  14110. if( tab->tab == CASHSHOP_TAB_SALE ){
  14111. return;
  14112. }
  14113. #endif
  14114. PACKET_ZC_ACK_SE_CASH_ITEM_LIST2* p = reinterpret_cast<PACKET_ZC_ACK_SE_CASH_ITEM_LIST2*>( packet_buffer );
  14115. p->packetType = HEADER_ZC_ACK_SE_CASH_ITEM_LIST2;
  14116. p->packetLength = sizeof( *p );
  14117. p->tab = tab->tab;
  14118. p->count = 0;
  14119. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14120. p->items[p->count].itemId = client_nameid( item->nameid );
  14121. p->items[p->count].price = item->price;
  14122. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14123. p->count++;
  14124. }
  14125. clif_send( p, p->packetLength, &sd->bl, SELF );
  14126. #endif
  14127. #endif
  14128. }
  14129. //08ca <len>.W <itemcount> W <tabcode>.W (ZC_ACK_SCHEDULER_CASHITEM)
  14130. void clif_cashshop_list( map_session_data& sd ){
  14131. for( const auto& pair : cash_shop_db ){
  14132. std::shared_ptr<s_cash_item_tab> tab = pair.second;
  14133. // Skip empty tabs, the client only expects filled ones
  14134. if( tab->items.empty() ){
  14135. continue;
  14136. }
  14137. #if !(PACKETVER_SUPPORTS_SALES)
  14138. if( tab->tab == CASHSHOP_TAB_SALE ){
  14139. continue;
  14140. }
  14141. #endif
  14142. PACKET_ZC_ACK_SCHEDULER_CASHITEM* p = reinterpret_cast<PACKET_ZC_ACK_SCHEDULER_CASHITEM*>( packet_buffer );
  14143. p->packetType = HEADER_ZC_ACK_SCHEDULER_CASHITEM;
  14144. p->packetLength = sizeof( *p );
  14145. p->count = 0;
  14146. p->tabNum = tab->tab;
  14147. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14148. p->items[p->count].itemId = client_nameid( item->nameid );
  14149. p->items[p->count].price = item->price;
  14150. #ifdef ENABLE_CASHSHOP_PREVIEW_PATCH
  14151. struct item_data* id = itemdb_search( item->nameid );
  14152. if( id == nullptr ){
  14153. p->items[p->count].location = 0;
  14154. p->items[p->count].viewSprite = 0;
  14155. }else{
  14156. p->items[p->count].location = pc_equippoint_sub( &sd, id );
  14157. p->items[p->count].viewSprite = id->look;
  14158. }
  14159. #endif
  14160. p->count++;
  14161. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14162. if( ( static_cast<size_t>( p->packetLength ) + sizeof( p->items[0] ) ) >= INT16_MAX ){
  14163. // Send current data
  14164. clif_send( p, p->packetLength, &sd.bl, SELF );
  14165. // Start a new packet
  14166. p->count = 0;
  14167. p->packetLength = sizeof( *p );
  14168. }
  14169. }
  14170. clif_send( p, p->packetLength, &sd.bl, SELF );
  14171. }
  14172. }
  14173. void clif_parse_cashshop_list_request( int32 fd, map_session_data* sd ){
  14174. if( !sd->status.cashshop_sent ) {
  14175. clif_cashshop_list( *sd );
  14176. #if PACKETVER_SUPPORTS_SALES
  14177. sale_notify_login(sd);
  14178. #endif
  14179. sd->status.cashshop_sent = true;
  14180. }
  14181. }
  14182. /// List of items offered in a cash shop (ZC_PC_CASH_POINT_ITEMLIST).
  14183. /// 0287 <packet len>.W <cash point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }*
  14184. /// 0287 <packet len>.W <cash point>.L <kafra point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }* (PACKETVER >= 20070711)
  14185. void clif_cashshop_show( map_session_data& sd, npc_data& nd ){
  14186. sd.npc_shopid = nd.bl.id;
  14187. int32 cost[2] = { 0, 0 };
  14188. npc_shop_currency_type( &sd, &nd, cost, true );
  14189. PACKET_ZC_PC_CASH_POINT_ITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_CASH_POINT_ITEMLIST*>( packet_buffer );
  14190. p->packetType = HEADER_ZC_PC_CASH_POINT_ITEMLIST;
  14191. p->packetLength = sizeof( *p );
  14192. p->cashPoints = cost[0];
  14193. #if PACKETVER >= 20070711
  14194. p->kafraPoints = cost[1];
  14195. #endif
  14196. for( int32 i = 0; i < nd.u.shop.count; i++ ) {
  14197. struct item_data* id = itemdb_search( nd.u.shop.shop_item[i].nameid );
  14198. p->items[i].price = nd.u.shop.shop_item[i].value;
  14199. p->items[i].discountPrice = nd.u.shop.shop_item[i].value; // Discount Price
  14200. p->items[i].itemType = itemtype( id->nameid );
  14201. p->items[i].itemId = client_nameid( id->nameid );
  14202. #ifdef ENABLE_OLD_CASHSHOP_PREVIEW_PATCH
  14203. p->items[i].location = pc_equippoint_sub( &sd, id );
  14204. p->items[i].viewSprite = id->look;
  14205. memset( p->items[i].unused, 0, sizeof( p->items[i].unused ) );
  14206. #endif
  14207. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14208. }
  14209. clif_send( p, p->packetLength, &sd.bl, SELF );
  14210. }
  14211. /// Cashshop Buy Ack (ZC_PC_CASH_POINT_UPDATE).
  14212. /// 0289 <cash point>.L <error>.W
  14213. /// 0289 <cash point>.L <kafra point>.L <error>.W (PACKETVER >= 20070711)
  14214. /// error:
  14215. /// 0 = The deal has successfully completed. (ERROR_TYPE_NONE)
  14216. /// 1 = The Purchase has failed because the NPC does not exist. (ERROR_TYPE_NPC)
  14217. /// 2 = The Purchase has failed because the Kafra Shop System is not working correctly. (ERROR_TYPE_SYSTEM)
  14218. /// 3 = You are over your Weight Limit. (ERROR_TYPE_INVENTORY_WEIGHT)
  14219. /// 4 = You cannot purchase items while you are in a trade. (ERROR_TYPE_EXCHANGE)
  14220. /// 5 = The Purchase has failed because the Item Information was incorrect. (ERROR_TYPE_ITEM_ID)
  14221. /// 6 = You do not have enough Kafra Credit Points. (ERROR_TYPE_MONEY)
  14222. /// 7 = You can purchase up to 10 items. (ERROR_TYPE_AMOUNT)
  14223. /// 8 = Some items could not be purchased. (ERROR_TYPE_PURCHASE_FAIL)
  14224. void clif_cashshop_ack(map_session_data* sd, int32 error)
  14225. {
  14226. int32 fd, cost[2] = { 0, 0 };
  14227. struct npc_data *nd;
  14228. nullpo_retv(sd);
  14229. fd = sd->fd;
  14230. nd = map_id2nd(sd->npc_shopid);
  14231. npc_shop_currency_type(sd, nd, cost, false);
  14232. WFIFOHEAD(fd, packet_len(0x289));
  14233. WFIFOW(fd,0) = 0x289;
  14234. WFIFOL(fd,2) = cost[0];
  14235. #if PACKETVER < 20070711
  14236. WFIFOW(fd,6) = TOW(error);
  14237. #else
  14238. WFIFOL(fd,6) = cost[1];
  14239. WFIFOW(fd,10) = TOW(error);
  14240. #endif
  14241. WFIFOSET(fd, packet_len(0x289));
  14242. }
  14243. void clif_cashshop_result( map_session_data *sd, t_itemid item_id, uint16 result ){
  14244. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined( PACKETVER_ZERO )
  14245. nullpo_retv( sd );
  14246. PACKET_ZC_SE_PC_BUY_CASHITEM_RESULT packet = {};
  14247. packet.packetType = HEADER_ZC_SE_PC_BUY_CASHITEM_RESULT;
  14248. if( item_id != 0 ){
  14249. packet.itemId = client_nameid( item_id );
  14250. }else{
  14251. packet.itemId = 0;
  14252. }
  14253. packet.result = result;
  14254. packet.cashPoints = sd->cashPoints;
  14255. packet.kafraPoints = sd->kafraPoints;
  14256. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  14257. #endif
  14258. }
  14259. /// Request to buy item(s) from cash shop.
  14260. /// 0288 <name id>.W <amount>.W (CZ_PC_BUY_CASH_POINT_ITEM)
  14261. /// 0288 <name id>.W <amount>.W <kafra points>.L (PACKETVER >= 20070711)
  14262. /// 0288 <packet len>.W <kafra points>.L <count>.W { <amount>.W <name id>.W }.4B*count (PACKETVER >= 20100803)
  14263. void clif_parse_npccashshop_buy( int32 fd, map_session_data *sd ){
  14264. nullpo_retv( sd );
  14265. if( sd->state.trading || !sd->npc_shopid ){
  14266. clif_cashshop_ack( sd, 1 );
  14267. return;
  14268. }
  14269. const PACKET_CZ_PC_BUY_CASH_POINT_ITEM* p = reinterpret_cast<PACKET_CZ_PC_BUY_CASH_POINT_ITEM*>( RFIFOP( fd, 0 ) );
  14270. #if PACKETVER < 20070711
  14271. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, 0 ) );
  14272. #elif PACKETVER < 20101116
  14273. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, p->kafraPoints ) );
  14274. #else
  14275. int32 s_itl = sizeof( p->items[0] );
  14276. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14277. ShowWarning( "Player %u sent incorrect cash shop buy packet (len %u:%" PRIdPTR ")!\n", sd->status.char_id, p->packetLength, sizeof( *p ) + p->count * s_itl );
  14278. return;
  14279. }
  14280. std::vector<s_npc_buy_list> item_list = {};
  14281. item_list.reserve( p->count );
  14282. for( int32 i = 0; i < p->count; i++ ){
  14283. s_npc_buy_list item = {};
  14284. item.nameid = p->items[i].itemId;
  14285. item.qty = p->items[i].amount;
  14286. item_list.push_back( item );
  14287. }
  14288. clif_cashshop_ack( sd, npc_cashshop_buylist( sd, p->kafraPoints, item_list ) );
  14289. #endif
  14290. }
  14291. /// Request to buy item(s) from cash shop.
  14292. /// 0848 <packet len>.W <count>.W <packet len>.W <kafra points>.L <count>.W { <amount>.W <name id>.W <tab>.W }.6B*count (CZ_SE_PC_BUY_CASHITEM_LIST)
  14293. void clif_parse_cashshop_buy( int32 fd, map_session_data *sd ){
  14294. const PACKET_CZ_SE_PC_BUY_CASHITEM_LIST* p = reinterpret_cast<PACKET_CZ_SE_PC_BUY_CASHITEM_LIST*>( RFIFOP( fd, 0 ) );
  14295. int32 s_itl = sizeof( p->items[0] );
  14296. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14297. ShowWarning( "Player %u sent incorrect cash shop buy packet (len %u:%" PRIdPTR ")!\n", sd->status.char_id, p->packetLength, sizeof( *p ) + p->count * s_itl );
  14298. return;
  14299. }
  14300. cashshop_buylist( sd, p->kafraPoints, p->count, p->items );
  14301. }
  14302. /// Adoption System
  14303. ///
  14304. /// Adoption message (ZC_BABYMSG).
  14305. /// 0216 <msg>.L
  14306. /// msg:
  14307. /// ADOPT_REPLY_MORE_CHILDREN = "You cannot adopt more than 1 child."
  14308. /// ADOPT_REPLY_LEVEL_70 = "You must be at least character level 70 in order to adopt someone."
  14309. /// ADOPT_REPLY_MARRIED = "You cannot adopt a married person."
  14310. void clif_Adopt_reply(map_session_data *sd, int32 type)
  14311. {
  14312. int32 fd = sd->fd;
  14313. WFIFOHEAD(fd,6);
  14314. WFIFOW(fd,0) = 0x216;
  14315. WFIFOL(fd,2) = type;
  14316. WFIFOSET(fd,6);
  14317. }
  14318. /// Adoption confirmation (ZC_REQ_BABY).
  14319. /// 01f6 <account id>.L <char id>.L <name>.B
  14320. void clif_Adopt_request(map_session_data *sd, map_session_data *src, int32 p_id)
  14321. {
  14322. int32 fd = sd->fd;
  14323. WFIFOHEAD(fd,34);
  14324. WFIFOW(fd,0) = 0x1f6;
  14325. WFIFOL(fd,2) = src->status.account_id;
  14326. WFIFOL(fd,6) = p_id;
  14327. safestrncpy(WFIFOCP(fd,10), src->status.name, NAME_LENGTH);
  14328. WFIFOSET(fd,34);
  14329. }
  14330. /// Request to adopt a player (CZ_REQ_JOIN_BABY).
  14331. /// 01f9 <account id>.L
  14332. void clif_parse_Adopt_request(int32 fd, map_session_data *sd)
  14333. {
  14334. TBL_PC *tsd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  14335. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  14336. if( pc_try_adopt(sd, p_sd, tsd) == ADOPT_ALLOWED )
  14337. {
  14338. tsd->adopt_invite = sd->status.account_id;
  14339. clif_Adopt_request(tsd, sd, p_sd->status.account_id);
  14340. }
  14341. }
  14342. /// Answer to adopt confirmation.
  14343. /// 01f7 <account id>.L <char id>.L <answer>.L (CZ_JOIN_BABY)
  14344. /// answer:
  14345. /// 0 = rejected
  14346. /// 1 = accepted
  14347. void clif_parse_Adopt_reply(int32 fd, map_session_data *sd){
  14348. const PACKET_CZ_JOIN_BABY* p = reinterpret_cast<PACKET_CZ_JOIN_BABY*>( RFIFOP( fd, 0 ) );
  14349. // Check if the adoption was accepted
  14350. if( p->accepted == 0 ){
  14351. sd->adopt_invite = 0;
  14352. return;
  14353. }
  14354. map_session_data* father_sd = map_id2sd( p->father_AID );
  14355. // The father has to be online
  14356. if( father_sd == nullptr ){
  14357. sd->adopt_invite = 0;
  14358. return;
  14359. }
  14360. map_session_data* mother_sd = map_id2sd( p->mother_AID );
  14361. // The mother has to be online
  14362. if( mother_sd == nullptr ){
  14363. sd->adopt_invite = 0;
  14364. return;
  14365. }
  14366. if( sd->adopt_invite != father_sd->status.account_id ){
  14367. sd->adopt_invite = 0;
  14368. return;
  14369. }
  14370. sd->adopt_invite = 0;
  14371. pc_adoption( father_sd, mother_sd, sd );
  14372. }
  14373. /// Convex Mirror
  14374. /// 0293 <infoType>.B <x>.L <y>.L <minHours>.W <minMinutes>.W <maxHours>.W <maxMinutes>.W <monster name>.51B (ZC_BOSS_INFO)
  14375. /// infoType:
  14376. /// BOSS_INFO_NOT = No boss on this map.
  14377. /// BOSS_INFO_ALIVE = Boss is alive (position update).
  14378. /// BOSS_INFO_ALIVE_WITHMSG = Boss is alive (initial announce).
  14379. /// BOSS_INFO_DEAD = Boss is dead.
  14380. void clif_bossmapinfo( map_session_data& sd, mob_data* md, e_bossmap_info flag ){
  14381. PACKET_ZC_BOSS_INFO p = {};
  14382. p.packetType = HEADER_ZC_BOSS_INFO;
  14383. p.type = flag;
  14384. switch (flag) {
  14385. case BOSS_INFO_NOT:
  14386. // No data required
  14387. break;
  14388. case BOSS_INFO_ALIVE_WITHMSG:
  14389. [[fallthrough]];
  14390. case BOSS_INFO_ALIVE:
  14391. p.x = md->bl.x;
  14392. p.y = md->bl.y;
  14393. break;
  14394. case BOSS_INFO_DEAD: {
  14395. const struct TimerData * timer_data = get_timer(md->spawn_timer);
  14396. uint32 seconds;
  14397. int32 hours, minutes;
  14398. seconds = (uint32)(DIFF_TICK(timer_data->tick, gettick()) / 1000 + 60);
  14399. hours = seconds / (60 * 60);
  14400. seconds = seconds - (60 * 60 * hours);
  14401. minutes = seconds / 60;
  14402. p.minHours = hours;
  14403. p.minMinutes = minutes;
  14404. break;
  14405. }
  14406. }
  14407. if( md != nullptr ){
  14408. safestrncpy( p.name, md->db->jname.c_str(), sizeof( p.name ) );
  14409. }
  14410. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  14411. }
  14412. /// Requesting equip of a player (CZ_EQUIPWIN_MICROSCOPE).
  14413. /// 02d6 <account id>.L
  14414. void clif_parse_ViewPlayerEquip(int32 fd, map_session_data* sd)
  14415. {
  14416. int32 aid = RFIFOL(fd, packet_db[RFIFOW(fd,0)].pos[0]);
  14417. map_session_data* tsd = map_id2sd(aid);
  14418. if (!tsd)
  14419. return;
  14420. if (sd->bl.m != tsd->bl.m)
  14421. return;
  14422. else if( tsd->status.show_equip || pc_has_permission(sd, PC_PERM_VIEW_EQUIPMENT) )
  14423. clif_viewequip_ack( *sd, *tsd );
  14424. else
  14425. clif_msg(sd, MSI_OPEN_EQUIPEDITEM_REFUSED);
  14426. }
  14427. /// Request to a configuration.
  14428. /// 02d8 <type>.L <flag>.L (CZ_CONFIG)
  14429. /// type: see enum e_config_type
  14430. /// flag:
  14431. /// false = disabled
  14432. /// true = enabled
  14433. void clif_parse_configuration( int32 fd, map_session_data* sd ){
  14434. int32 cmd = RFIFOW(fd,0);
  14435. int32 type = RFIFOL(fd,packet_db[cmd].pos[0]);
  14436. bool flag = RFIFOL(fd,packet_db[cmd].pos[1]) != 0;
  14437. switch( type ){
  14438. case CONFIG_OPEN_EQUIPMENT_WINDOW:
  14439. sd->status.show_equip = flag;
  14440. break;
  14441. case CONFIG_CALL:
  14442. sd->status.disable_call = flag;
  14443. break;
  14444. case CONFIG_PET_AUTOFEED:
  14445. // Player can not click this if he does not have a pet
  14446. if( sd->pd == nullptr || !battle_config.feature_pet_autofeed || !sd->pd->get_pet_db()->allow_autofeed ){
  14447. return;
  14448. }
  14449. sd->pd->pet.autofeed = flag;
  14450. break;
  14451. case CONFIG_HOMUNCULUS_AUTOFEED:
  14452. // Player can not click this if he does not have a homunculus or it is vaporized
  14453. if( sd->hd == nullptr || sd->hd->homunculus.vaporize || !battle_config.feature_homunculus_autofeed ){
  14454. return;
  14455. }
  14456. sd->hd->homunculus.autofeed = flag;
  14457. break;
  14458. default:
  14459. ShowWarning( "clif_parse_configuration: received unknown configuration type '%d'...\n", type );
  14460. return;
  14461. }
  14462. clif_configuration( sd, static_cast<e_config_type>(type), flag );
  14463. }
  14464. /// Request to change party invitation tick.
  14465. /// 02C8 <enabled>.B (CZ_PARTY_CONFIG)
  14466. /// value:
  14467. /// 0 = disabled (triggered by /accept)
  14468. /// 1 = enabled (triggered by /refuse)
  14469. void clif_parse_PartyTick( int32 fd, map_session_data* sd ){
  14470. const PACKET_CZ_PARTY_CONFIG* p = reinterpret_cast<PACKET_CZ_PARTY_CONFIG*>( RFIFOP( fd, 0 ) );
  14471. sd->status.disable_partyinvite = p->refuseInvite;
  14472. clif_partyinvitationstate( *sd );
  14473. }
  14474. /// Questlog System [Kevin] [Inkfish]
  14475. ///
  14476. /**
  14477. * Safe check to init packet len & quest num for player.
  14478. * @param def_len
  14479. * @param info_len Length of each quest info.
  14480. * @param avail_quests Avail quests that player has.
  14481. * @param limit_out Limit for quest num, to make sure the quest num is still in allowed value.
  14482. * @param len_out Packet length as initial set => def_len + limit_out * info_len.
  14483. **/
  14484. static void clif_quest_len(int32 def_len, int32 info_len, int32 avail_quests, int32 *limit_out, int32 *len_out) {
  14485. (*limit_out) = (0xFFFF - def_len) / info_len;
  14486. (*limit_out) = (avail_quests > (*limit_out)) ? (*limit_out) : avail_quests;
  14487. (*len_out) = ((*limit_out) * info_len) + def_len;
  14488. }
  14489. std::string clif_quest_string( std::shared_ptr<s_quest_objective> objective ){
  14490. std::string race_name;
  14491. switch( objective->race ){
  14492. case RC_FORMLESS: race_name = "Formless"; break;
  14493. case RC_UNDEAD: race_name = "Undead"; break;
  14494. case RC_BRUTE: race_name = "Brute"; break;
  14495. case RC_PLANT: race_name = "Plant"; break;
  14496. case RC_INSECT: race_name = "Insect"; break;
  14497. case RC_FISH: race_name = "Fish"; break;
  14498. case RC_DEMON: race_name = "Demon"; break;
  14499. case RC_DEMIHUMAN: race_name = "Demihuman"; break;
  14500. case RC_ANGEL: race_name = "Angel"; break;
  14501. case RC_DRAGON: race_name = "Dragon"; break;
  14502. default:
  14503. ShowWarning( "clif_quest_string: Unsupported race %d - using empty string...\n", objective->race );
  14504. [[fallthrough]];
  14505. case RC_ALL: race_name = ""; break;
  14506. }
  14507. std::string size_name;
  14508. switch( objective->size ){
  14509. case SZ_SMALL: size_name = "Small"; break;
  14510. case SZ_MEDIUM: size_name = "Medium"; break;
  14511. case SZ_BIG: size_name = "Large"; break;
  14512. default:
  14513. ShowWarning( "clif_quest_string: Unsupported size %d - using empty string...\n", objective->size );
  14514. [[fallthrough]];
  14515. case SZ_ALL: size_name = ""; break;
  14516. }
  14517. std::string ele_name;
  14518. switch( objective->element ){
  14519. case ELE_NEUTRAL: ele_name = "Neutral Element"; break;
  14520. case ELE_WATER: ele_name = "Water Element"; break;
  14521. case ELE_EARTH: ele_name = "Earth Element"; break;
  14522. case ELE_FIRE: ele_name = "Fire Element"; break;
  14523. case ELE_WIND: ele_name = "Wind Element"; break;
  14524. case ELE_POISON: ele_name = "Poison Element"; break;
  14525. case ELE_HOLY: ele_name = "Holy Element"; break;
  14526. case ELE_DARK: ele_name = "Shadow Element"; break;
  14527. case ELE_GHOST: ele_name = "Ghost Element"; break;
  14528. case ELE_UNDEAD: ele_name = "Undead Element"; break;
  14529. default:
  14530. ShowWarning( "clif_quest_string: Unsupported element %d - using empty string...\n", objective->element );
  14531. [[fallthrough]];
  14532. case ELE_ALL: ele_name = ""; break;
  14533. }
  14534. std::string str;
  14535. if( !objective->map_name.empty() ){
  14536. str += objective->map_name;
  14537. }
  14538. if( !race_name.empty() ){
  14539. if( !str.empty() ){
  14540. str += ", ";
  14541. }
  14542. str += race_name;
  14543. }
  14544. if( !size_name.empty() ){
  14545. if( !str.empty() ){
  14546. str += ", ";
  14547. }
  14548. str += size_name;
  14549. }
  14550. if( !ele_name.empty() ){
  14551. if( !str.empty() ){
  14552. str += ", ";
  14553. }
  14554. str += ele_name;
  14555. }
  14556. return str;
  14557. }
  14558. /// Sends list of all quest states
  14559. /// 02b1 <packet len>.W <num>.L { <quest id>.L <active>.B }*num (ZC_ALL_QUEST_LIST)
  14560. /// 097a <packet len>.W <num>.L { <quest id>.L <active>.B <remaining time>.L <time>.L <count>.W { <mob_id>.L <killed>.W <total>.W <mob name>.24B }*count }*num (ZC_ALL_QUEST_LIST2)
  14561. /// 09f8 <packet len>.W <num>.L { <quest id>.L <active>.B <remaining time>.L <time>.L <count>.W { <hunt identification>.L <mob type>.L <mob_id>.L <min level>.W <max level>.W <killed>.W <total>.W <mob name>.24B }*count }*num (ZC_ALL_QUEST_LIST3)
  14562. void clif_quest_send_list(map_session_data *sd)
  14563. {
  14564. nullpo_retv(sd);
  14565. int32 fd = sd->fd;
  14566. int32 i;
  14567. int32 offset = 8;
  14568. int32 limit = 0;
  14569. #if PACKETVER >= 20141022
  14570. #if PACKETVER >= 20150513
  14571. int32 size = 22 + NAME_LENGTH;
  14572. #else
  14573. int32 size = 10 + NAME_LENGTH;
  14574. #endif
  14575. clif_quest_len(offset, 15 + ( size * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &i);
  14576. WFIFOHEAD(fd,i);
  14577. #if PACKETVER >= 20150513
  14578. WFIFOW(fd, 0) = 0x9f8;
  14579. #else
  14580. WFIFOW(fd, 0) = 0x97a;
  14581. #endif
  14582. WFIFOL(fd, 4) = limit;
  14583. for (i = 0; i < limit; i++) {
  14584. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14585. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14586. offset += 4;
  14587. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14588. offset++;
  14589. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14590. offset += 4;
  14591. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time);
  14592. offset += 4;
  14593. WFIFOW(fd, offset) = static_cast<uint16>(qi->objectives.size());
  14594. offset += 2;
  14595. if (!qi->objectives.empty()) {
  14596. std::shared_ptr<s_mob_db> mob;
  14597. for (int32 j = 0; j < qi->objectives.size(); j++) {
  14598. mob = mob_db.find(qi->objectives[j]->mob_id);
  14599. e_race race = qi->objectives[j]->race;
  14600. e_size size = qi->objectives[j]->size;
  14601. e_element element = qi->objectives[j]->element;
  14602. #if PACKETVER >= 20150513
  14603. WFIFOL(fd, offset) = sd->quest_log[i].quest_id * 1000 + j;
  14604. offset += 4;
  14605. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0)));
  14606. offset += 4;
  14607. #endif
  14608. WFIFOL(fd, offset) = ((mob && qi->objectives[j]->mob_id > 0) ? qi->objectives[j]->mob_id : MOBID_PORING);
  14609. offset += 4;
  14610. #if PACKETVER >= 20150513
  14611. WFIFOW(fd, offset) = qi->objectives[j]->min_level;
  14612. offset += 2;
  14613. WFIFOW(fd, offset) = qi->objectives[j]->max_level;
  14614. offset += 2;
  14615. #endif
  14616. WFIFOW(fd, offset) = sd->quest_log[i].count[j];
  14617. offset += 2;
  14618. WFIFOW(fd, offset) = qi->objectives[j]->count;
  14619. offset += 2;
  14620. if (mob && qi->objectives[j]->mob_id > 0)
  14621. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14622. else
  14623. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14624. offset += NAME_LENGTH;
  14625. }
  14626. }
  14627. }
  14628. WFIFOW(fd, 2) = offset;
  14629. WFIFOSET(fd, offset);
  14630. #else
  14631. clif_quest_len(offset, 5, sd->avail_quests, &limit, &i);
  14632. WFIFOHEAD(fd,i);
  14633. WFIFOW(fd, 0) = 0x2b1;
  14634. WFIFOL(fd, 4) = limit;
  14635. for (i = 0; i < limit; i++) {
  14636. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14637. offset += 4;
  14638. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14639. offset += 1;
  14640. }
  14641. WFIFOW(fd, 2) = offset;
  14642. WFIFOSET(fd, offset);
  14643. #endif
  14644. }
  14645. /// Sends list of all quest missions (ZC_ALL_QUEST_MISSION).
  14646. /// 02b2 <packet len>.W <num>.L { <quest id>.L <start time>.L <expire time>.L <mobs>.W { <mob id>.L <mob count>.W <mob name>.24B }*3 }*num
  14647. void clif_quest_send_mission(map_session_data *sd)
  14648. {
  14649. nullpo_retv(sd);
  14650. int32 fd = sd->fd;
  14651. int32 limit = 0;
  14652. int32 len = sd->avail_quests*104+8;
  14653. clif_quest_len(8, 14 + ((6 + NAME_LENGTH) * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &len);
  14654. WFIFOHEAD(fd, len);
  14655. WFIFOW(fd, 0) = 0x2b2;
  14656. WFIFOW(fd, 2) = len;
  14657. WFIFOL(fd, 4) = limit;
  14658. for (int32 i = 0; i < limit; i++) {
  14659. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14660. WFIFOL(fd, i*104+8) = sd->quest_log[i].quest_id;
  14661. WFIFOL(fd, i*104+12) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14662. WFIFOL(fd, i*104+16) = static_cast<uint32>(sd->quest_log[i].time);
  14663. WFIFOW(fd, i*104+20) = static_cast<uint16>(qi->objectives.size());
  14664. for (int32 j = 0 ; j < qi->objectives.size(); j++) {
  14665. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[j]->mob_id);
  14666. WFIFOL(fd, i*104+22+j*30) = (mob ? qi->objectives[j]->mob_id : MOBID_PORING);
  14667. WFIFOW(fd, i*104+26+j*30) = sd->quest_log[i].count[j];
  14668. if (mob && qi->objectives[j]->mob_id > 0)
  14669. safestrncpy(WFIFOCP(fd, i*104+28+j*30), mob->jname.c_str(), NAME_LENGTH);
  14670. else
  14671. safestrncpy(WFIFOCP(fd, i*104+28+j*30), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14672. }
  14673. }
  14674. WFIFOSET(fd, len);
  14675. }
  14676. /// Notification about a new quest
  14677. /// 02b3 <quest id>.L <active>.B <start time>.L <expire time>.L <mobs>.W { <mob id>.L <mob count>.W <mob name>.24B }*3 (ZC_ADD_QUEST)
  14678. /// 08fe <packet len>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_HUNTING_QUEST_INFO)
  14679. /// 09f9 <quest id>.L <active>.B <start time>.L <expire time>.L <mobs>.W { <hunt identification>.L <mob type>.L <mob id>.L <min level>.W <max level>.W <mob count>.W <mob name>.24B }*3 (ZC_ADD_QUEST_EX)
  14680. void clif_quest_add(map_session_data *sd, struct quest *qd)
  14681. {
  14682. nullpo_retv(sd);
  14683. nullpo_retv(qd);
  14684. int32 fd = sd->fd;
  14685. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14686. if (!qi)
  14687. return;
  14688. #if PACKETVER >= 20150513
  14689. int32 cmd = 0x9f9;
  14690. #else
  14691. int32 cmd = 0x2b3;
  14692. #endif
  14693. WFIFOHEAD(fd, packet_len(cmd));
  14694. WFIFOW(fd, 0) = cmd;
  14695. WFIFOL(fd, 2) = qd->quest_id;
  14696. WFIFOB(fd, 6) = qd->state;
  14697. WFIFOB(fd, 7) = static_cast<uint8>(qd->time - qi->time);
  14698. WFIFOL(fd, 11) = static_cast<uint32>(qd->time);
  14699. WFIFOW(fd, 15) = static_cast<uint16>(qi->objectives.size());
  14700. for (int32 i = 0, offset = 17; i < qi->objectives.size(); i++) {
  14701. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[i]->mob_id);
  14702. e_race race = qi->objectives[i]->race;
  14703. e_size size = qi->objectives[i]->size;
  14704. e_element element = qi->objectives[i]->element;
  14705. #if PACKETVER >= 20150513
  14706. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14707. offset += 4;
  14708. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0))); // effect ?
  14709. offset += 4;
  14710. #endif
  14711. WFIFOL(fd, offset) = ((mob && qi->objectives[i]->mob_id > 0) ? qi->objectives[i]->mob_id : MOBID_PORING); // 0 can't be used as it displays "Novice" job regardless of the clif_mobtype_name
  14712. offset += 4;
  14713. #if PACKETVER >= 20150513
  14714. WFIFOW(fd, offset) = qi->objectives[i]->min_level;
  14715. offset += 2;
  14716. WFIFOW(fd, offset) = qi->objectives[i]->max_level;
  14717. offset += 2;
  14718. #endif
  14719. WFIFOW(fd, offset) = qd->count[i];
  14720. offset += 2;
  14721. if (mob && qi->objectives[i]->mob_id > 0)
  14722. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14723. else
  14724. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[i]).c_str(), NAME_LENGTH);
  14725. offset += NAME_LENGTH;
  14726. }
  14727. WFIFOSET(fd, packet_len(cmd));
  14728. #if PACKETVER >= 20150513
  14729. int16 len;
  14730. len = static_cast<decltype(len)>( 4 + qi->objectives.size() * 12 );
  14731. WFIFOHEAD(fd, len);
  14732. WFIFOW(fd, 0) = 0x8fe;
  14733. WFIFOW(fd, 2) = len;
  14734. for (int32 i = 0, offset = 4; i < qi->objectives.size(); i++, offset += 12) {
  14735. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14736. WFIFOL(fd, offset + 4) = qi->objectives[i]->mob_id;
  14737. WFIFOW(fd, offset + 8) = qi->objectives[i]->count;
  14738. WFIFOW(fd, offset + 10) = qd->count[i];
  14739. }
  14740. WFIFOSET(fd, len);
  14741. #endif
  14742. }
  14743. /// Notification about a quest being removed (ZC_DEL_QUEST).
  14744. /// 02b4 <quest id>.L
  14745. void clif_quest_delete(map_session_data *sd, int32 quest_id)
  14746. {
  14747. int32 fd = sd->fd;
  14748. WFIFOHEAD(fd, packet_len(0x2b4));
  14749. WFIFOW(fd, 0) = 0x2b4;
  14750. WFIFOL(fd, 2) = quest_id;
  14751. WFIFOSET(fd, packet_len(0x2b4));
  14752. }
  14753. /// Notification of an update to the hunting mission counter
  14754. /// 02b5 <packet len>.W <mobs>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT)
  14755. /// 09fa <packet len>.W <mobs>.W { <quest id>.L <hunt identification>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT_EX)
  14756. void clif_quest_update_objective(map_session_data *sd, struct quest *qd)
  14757. {
  14758. int32 fd = sd->fd;
  14759. int32 offset = 6;
  14760. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14761. int16 len;
  14762. len = static_cast<decltype(len)>( qi->objectives.size() * 12 + 6 );
  14763. #if PACKETVER >= 20150513
  14764. int32 cmd = 0x9fa;
  14765. #else
  14766. int32 cmd = 0x2b5;
  14767. #endif
  14768. WFIFOHEAD(fd, len);
  14769. WFIFOW(fd, 0) = cmd;
  14770. WFIFOW(fd, 4) = static_cast<uint16>(qi->objectives.size());
  14771. for (int32 i = 0; i < qi->objectives.size(); i++) {
  14772. WFIFOL(fd, offset) = qd->quest_id;
  14773. offset += 4;
  14774. #if PACKETVER >= 20150513
  14775. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14776. offset += 4;
  14777. #else
  14778. WFIFOL(fd, offset) = qi->objectives[i]->mob_id;
  14779. offset += 4;
  14780. #endif
  14781. WFIFOW(fd, offset) = qi->objectives[i]->count;
  14782. offset += 2;
  14783. WFIFOW(fd, offset) = qd->count[i];
  14784. offset += 2;
  14785. }
  14786. WFIFOW(fd, 2) = offset;
  14787. WFIFOSET(fd, offset);
  14788. }
  14789. /// Request to change the state of a quest.
  14790. /// 02b6 <quest id>.L <active>.B (CZ_ACTIVE_QUEST)
  14791. void clif_parse_questStateAck( int32 fd, map_session_data* sd ){
  14792. #if PACKETVER >= 20070622
  14793. const PACKET_CZ_ACTIVE_QUEST* p = reinterpret_cast<PACKET_CZ_ACTIVE_QUEST*>( RFIFOP( fd, 0 ) );
  14794. quest_update_status( sd, p->quest_id, ( p->active != 0 ) ? Q_ACTIVE : Q_INACTIVE );
  14795. #endif
  14796. }
  14797. /// Notification about the change of a quest state (ZC_ACTIVE_QUEST).
  14798. /// 02b7 <quest id>.L <active>.B
  14799. void clif_quest_update_status(map_session_data *sd, int32 quest_id, bool active)
  14800. {
  14801. int32 fd = sd->fd;
  14802. WFIFOHEAD(fd, packet_len(0x2b7));
  14803. WFIFOW(fd, 0) = 0x2b7;
  14804. WFIFOL(fd, 2) = quest_id;
  14805. WFIFOB(fd, 6) = active;
  14806. WFIFOSET(fd, packet_len(0x2b7));
  14807. }
  14808. /// Notification about an NPC's quest state (ZC_QUEST_NOTIFY_EFFECT).
  14809. /// 0446 <npc id>.L <x>.W <y>.W <effect>.W <color>.W
  14810. /// effect:
  14811. /// 0 = none
  14812. /// 1 = exclamation mark icon
  14813. /// 2 = question mark icon
  14814. /// color:
  14815. /// 0 = yellow
  14816. /// 1 = orange
  14817. /// 2 = green
  14818. /// 3 = purple
  14819. void clif_quest_show_event(map_session_data *sd, struct block_list *bl, e_questinfo_types effect, e_questinfo_markcolor color)
  14820. {
  14821. #if PACKETVER >= 20090218
  14822. nullpo_retv(sd);
  14823. nullpo_retv(bl);
  14824. int32 fd = sd->fd;
  14825. WFIFOHEAD(fd, packet_len(0x446));
  14826. WFIFOW(fd, 0) = 0x446;
  14827. WFIFOL(fd, 2) = bl->id;
  14828. WFIFOW(fd, 6) = bl->x;
  14829. WFIFOW(fd, 8) = bl->y;
  14830. WFIFOW(fd, 10) = effect;
  14831. WFIFOW(fd, 12) = color;
  14832. WFIFOSET(fd, packet_len(0x446));
  14833. #endif
  14834. }
  14835. /// Mercenary System
  14836. ///
  14837. /// Notification about a mercenary status parameter change (ZC_MER_PAR_CHANGE).
  14838. /// 02a2 <var id>.W <value>.L
  14839. void clif_mercenary_updatestatus(map_session_data *sd, int32 type)
  14840. {
  14841. s_mercenary_data *md;
  14842. struct status_data *status;
  14843. int32 fd;
  14844. if( !clif_session_isValid(sd) || (md = sd->md) == nullptr )
  14845. return;
  14846. fd = sd->fd;
  14847. status = &md->battle_status;
  14848. WFIFOHEAD(fd,packet_len(0x2a2));
  14849. WFIFOW(fd,0) = 0x2a2;
  14850. WFIFOW(fd,2) = type;
  14851. switch( type ) {
  14852. case SP_ATK1:
  14853. {
  14854. int32 atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14855. WFIFOL(fd,4) = cap_value(atk, 0, INT16_MAX);
  14856. }
  14857. break;
  14858. case SP_MATK1:
  14859. WFIFOL(fd,4) = min(status->matk_max, UINT16_MAX);
  14860. break;
  14861. case SP_HIT:
  14862. WFIFOL(fd,4) = status->hit;
  14863. break;
  14864. case SP_CRITICAL:
  14865. WFIFOL(fd,4) = status->cri/10;
  14866. break;
  14867. case SP_DEF1:
  14868. WFIFOL(fd,4) = status->def;
  14869. break;
  14870. case SP_MDEF1:
  14871. WFIFOL(fd,4) = status->mdef;
  14872. break;
  14873. case SP_MERCFLEE:
  14874. WFIFOL(fd,4) = status->flee;
  14875. break;
  14876. case SP_ASPD:
  14877. WFIFOL(fd,4) = status->amotion;
  14878. break;
  14879. case SP_HP:
  14880. WFIFOL(fd,4) = status->hp;
  14881. break;
  14882. case SP_MAXHP:
  14883. WFIFOL(fd,4) = status->max_hp;
  14884. break;
  14885. case SP_SP:
  14886. WFIFOL(fd,4) = status->sp;
  14887. break;
  14888. case SP_MAXSP:
  14889. WFIFOL(fd,4) = status->max_sp;
  14890. break;
  14891. case SP_MERCKILLS:
  14892. WFIFOL(fd,4) = md->mercenary.kill_count;
  14893. break;
  14894. case SP_MERCFAITH:
  14895. WFIFOL(fd,4) = mercenary_get_faith(md);
  14896. break;
  14897. }
  14898. WFIFOSET(fd,packet_len(0x2a2));
  14899. }
  14900. /// Mercenary base status data (ZC_MER_INIT).
  14901. /// 029b <id>.L <atk>.W <matk>.W <hit>.W <crit>.W <def>.W <mdef>.W <flee>.W <aspd>.W
  14902. /// <name>.24B <level>.W <hp>.L <maxhp>.L <sp>.L <maxsp>.L <expire time>.L <faith>.W
  14903. /// <calls>.L <kills>.L <atk range>.W
  14904. void clif_mercenary_info(map_session_data *sd)
  14905. {
  14906. int32 fd;
  14907. s_mercenary_data *md;
  14908. struct status_data *status;
  14909. int32 atk;
  14910. if( !clif_session_isValid(sd) || (md = sd->md) == nullptr )
  14911. return;
  14912. fd = sd->fd;
  14913. status = &md->battle_status;
  14914. WFIFOHEAD(fd,packet_len(0x29b));
  14915. WFIFOW(fd,0) = 0x29b;
  14916. WFIFOL(fd,2) = md->bl.id;
  14917. // Mercenary shows ATK as a random value between ATK ~ ATK2
  14918. atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14919. WFIFOW(fd,6) = cap_value(atk, 0, INT16_MAX);
  14920. WFIFOW(fd,8) = min(status->matk_max, UINT16_MAX);
  14921. WFIFOW(fd,10) = status->hit;
  14922. WFIFOW(fd,12) = status->cri/10;
  14923. WFIFOW(fd,14) = status->def;
  14924. WFIFOW(fd,16) = status->mdef;
  14925. WFIFOW(fd,18) = status->flee;
  14926. WFIFOW(fd,20) = status->amotion;
  14927. safestrncpy(WFIFOCP(fd,22), md->db->name.c_str(), NAME_LENGTH);
  14928. WFIFOW(fd,46) = md->db->lv;
  14929. WFIFOL(fd,48) = status->hp;
  14930. WFIFOL(fd,52) = status->max_hp;
  14931. WFIFOL(fd,56) = status->sp;
  14932. WFIFOL(fd,60) = status->max_sp;
  14933. WFIFOL(fd,64) = client_tick(time(nullptr) + (mercenary_get_lifetime(md) / 1000));
  14934. WFIFOW(fd,68) = mercenary_get_faith(md);
  14935. WFIFOL(fd,70) = mercenary_get_calls(md);
  14936. WFIFOL(fd,74) = md->mercenary.kill_count;
  14937. WFIFOW(fd,78) = md->battle_status.rhw.range;
  14938. WFIFOSET(fd,packet_len(0x29b));
  14939. }
  14940. /// Mercenary skill tree (ZC_MER_SKILLINFO_LIST).
  14941. /// 029d <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  14942. void clif_mercenary_skillblock(map_session_data *sd)
  14943. {
  14944. s_mercenary_data *md;
  14945. int32 fd, len = 4;
  14946. if( sd == nullptr || (md = sd->md) == nullptr )
  14947. return;
  14948. fd = sd->fd;
  14949. WFIFOHEAD(fd,4+37*MAX_MERCSKILL);
  14950. WFIFOW(fd,0) = 0x29d;
  14951. for (const auto &it : md->db->skill) {
  14952. uint16 id = it.first;
  14953. if (!SKILL_CHK_MERC(id))
  14954. continue;
  14955. uint16 lv = it.second;
  14956. WFIFOW(fd,len) = id;
  14957. WFIFOL(fd,len+2) = skill_get_inf(id);
  14958. WFIFOW(fd,len+6) = lv;
  14959. WFIFOW(fd,len+8) = skill_get_sp(id, lv);
  14960. WFIFOW(fd,len+10) = skill_get_range2(&md->bl, id, lv, false);
  14961. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  14962. WFIFOB(fd,len+36) = 0; // Skillable for Mercenary?
  14963. len += 37;
  14964. }
  14965. WFIFOW(fd,2) = len;
  14966. WFIFOSET(fd,len);
  14967. }
  14968. /// Request to invoke a mercenary menu action (CZ_MER_COMMAND).
  14969. /// 029f <command>.B
  14970. /// 1 = mercenary information
  14971. /// 2 = delete
  14972. void clif_parse_mercenary_action(int32 fd, map_session_data* sd)
  14973. {
  14974. int32 option = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  14975. if( sd->md == nullptr )
  14976. return;
  14977. if( option == 2 ) mercenary_delete(sd->md, 2);
  14978. }
  14979. /// Notification about the remaining time of a rental item.
  14980. /// 0298 <name id>.W <seconds>.L (ZC_CASH_TIME_COUNTER)
  14981. void clif_rental_time( map_session_data* sd, t_itemid nameid, int32 seconds ){
  14982. // '<ItemName>' item will disappear in <seconds/60> minutes.
  14983. PACKET_ZC_CASH_TIME_COUNTER p = {};
  14984. p.packetType = HEADER_ZC_CASH_TIME_COUNTER;
  14985. p.itemId = client_nameid( nameid );
  14986. p.seconds = seconds;
  14987. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14988. }
  14989. /// Deletes a rental item from client's inventory.
  14990. /// 0299 <index>.W <name id>.W (ZC_CASH_ITEM_DELETE)
  14991. void clif_rental_expired( map_session_data* sd, int32 index, t_itemid nameid ){
  14992. // '<ItemName>' item has been deleted from the Inventory
  14993. PACKET_ZC_CASH_ITEM_DELETE p = {};
  14994. p.packetType = HEADER_ZC_CASH_ITEM_DELETE;
  14995. p.index = client_index( index );
  14996. p.itemId = client_nameid( nameid );
  14997. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14998. }
  14999. /// Book Reading (ZC_READ_BOOK).
  15000. /// 0294 <book id>.L <page>.L
  15001. void clif_readbook(int32 fd, int32 book_id, int32 page)
  15002. {
  15003. WFIFOHEAD(fd,packet_len(0x294));
  15004. WFIFOW(fd,0) = 0x294;
  15005. WFIFOL(fd,2) = book_id;
  15006. WFIFOL(fd,6) = page;
  15007. WFIFOSET(fd,packet_len(0x294));
  15008. }
  15009. /// Battlegrounds
  15010. ///
  15011. /// Updates HP bar of a camp member.
  15012. /// 02e0 <account id>.L <name>.24B <hp>.W <max hp>.W (ZC_BATTLEFIELD_NOTIFY_HP)
  15013. /// 0a0e <account id>.L <hp>.L <max hp>.L (ZC_BATTLEFIELD_NOTIFY_HP2)
  15014. void clif_bg_hp(map_session_data *sd) {
  15015. #if PACKETVER < 20140613
  15016. unsigned char buf[34];
  15017. const int32 cmd = 0x2e0;
  15018. #else
  15019. unsigned char buf[14];
  15020. const int32 cmd = 0xa0e;
  15021. #endif
  15022. nullpo_retv(sd);
  15023. WBUFW(buf,0) = cmd;
  15024. WBUFL(buf,2) = sd->status.account_id;
  15025. #if PACKETVER < 20140613
  15026. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  15027. if( sd->battle_status.max_hp > INT16_MAX ) { // To correctly display the %hp bar. [Skotlex]
  15028. WBUFW(buf,30) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  15029. WBUFW(buf,32) = 100;
  15030. } else {
  15031. WBUFW(buf,30) = sd->battle_status.hp;
  15032. WBUFW(buf,32) = sd->battle_status.max_hp;
  15033. }
  15034. #else
  15035. if( sd->battle_status.max_hp > INT32_MAX ) {
  15036. WBUFL(buf,6) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  15037. WBUFL(buf,10) = 100;
  15038. } else {
  15039. WBUFL(buf,6) = sd->battle_status.hp;
  15040. WBUFL(buf,10) = sd->battle_status.max_hp;
  15041. }
  15042. #endif
  15043. clif_send(buf, packet_len(cmd), &sd->bl, BG_AREA_WOS);
  15044. }
  15045. /// Updates the position of a camp member on the minimap (ZC_BATTLEFIELD_NOTIFY_POSITION).
  15046. /// 02df <account id>.L <name>.24B <class>.W <x>.W <y>.W
  15047. void clif_bg_xy(map_session_data *sd)
  15048. {
  15049. unsigned char buf[36];
  15050. nullpo_retv(sd);
  15051. WBUFW(buf,0)=0x2df;
  15052. WBUFL(buf,2)=sd->status.account_id;
  15053. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  15054. WBUFW(buf,30)=sd->status.class_;
  15055. WBUFW(buf,32)=sd->bl.x;
  15056. WBUFW(buf,34)=sd->bl.y;
  15057. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  15058. }
  15059. void clif_bg_xy_remove(map_session_data *sd)
  15060. {
  15061. unsigned char buf[36];
  15062. nullpo_retv(sd);
  15063. WBUFW(buf,0)=0x2df;
  15064. WBUFL(buf,2)=sd->status.account_id;
  15065. memset(WBUFP(buf,6), 0, NAME_LENGTH);
  15066. WBUFW(buf,30)=0;
  15067. WBUFW(buf,32)=-1;
  15068. WBUFW(buf,34)=-1;
  15069. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  15070. }
  15071. /// Notifies clients of a battleground message.
  15072. /// 02DC <packet len>.W <account id>.L <name>.24B <message>.?B (ZC_BATTLEFIELD_CHAT)
  15073. void clif_bg_message( struct s_battleground_data *bg, int32 src_id, const char *name, const char *mes, size_t len ){
  15074. map_session_data *sd = bg_getavailablesd( bg );
  15075. if( sd == nullptr ){
  15076. return;
  15077. }
  15078. // limit length
  15079. len = zmin( len + 1, CHAT_SIZE_MAX );
  15080. unsigned char buf[8 + NAME_LENGTH + CHAT_SIZE_MAX];
  15081. WBUFW(buf,0) = 0x2dc;
  15082. WBUFW(buf,2) = static_cast<int16>( len + NAME_LENGTH + 8 );
  15083. WBUFL(buf,4) = src_id;
  15084. safestrncpy(WBUFCP(buf,8), name, NAME_LENGTH);
  15085. safestrncpy(WBUFCP(buf,8+NAME_LENGTH), mes, len );
  15086. clif_send(buf,WBUFW(buf,2), &sd->bl, BG);
  15087. }
  15088. /// Validates and processes battlechat messages.
  15089. /// All messages that are sent after enabling battleground chat with /battlechat.
  15090. /// 02DB <packet len>.W <text>.?B (CZ_BATTLEFIELD_CHAT)
  15091. void clif_parse_BattleChat(int32 fd, map_session_data* sd){
  15092. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  15093. if( !clif_process_message( sd, false, name, message, output ) )
  15094. return;
  15095. bg_send_message(sd, output, strlen(output) );
  15096. }
  15097. /// Notifies client of a battleground score change (ZC_BATTLEFIELD_NOTIFY_POINT).
  15098. /// 02de <camp A points>.W <camp B points>.W
  15099. void clif_bg_updatescore(int16 m)
  15100. {
  15101. struct block_list bl;
  15102. unsigned char buf[6];
  15103. struct map_data *mapdata = map_getmapdata(m);
  15104. bl.id = 0;
  15105. bl.type = BL_NUL;
  15106. bl.m = m;
  15107. WBUFW(buf,0) = 0x2de;
  15108. WBUFW(buf,2) = mapdata->bgscore_lion;
  15109. WBUFW(buf,4) = mapdata->bgscore_eagle;
  15110. clif_send(buf,packet_len(0x2de),&bl,ALL_SAMEMAP);
  15111. }
  15112. void clif_bg_updatescore_single(map_session_data *sd)
  15113. {
  15114. int32 fd;
  15115. nullpo_retv(sd);
  15116. fd = sd->fd;
  15117. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  15118. WFIFOHEAD(fd,packet_len(0x2de));
  15119. WFIFOW(fd,0) = 0x2de;
  15120. WFIFOW(fd,2) = mapdata->bgscore_lion;
  15121. WFIFOW(fd,4) = mapdata->bgscore_eagle;
  15122. WFIFOSET(fd,packet_len(0x2de));
  15123. }
  15124. /// Battleground camp belong-information (ZC_BATTLEFIELD_NOTIFY_CAMPINFO).
  15125. /// 02dd <account id>.L <name>.24B <camp>.W
  15126. void clif_sendbgemblem_area(map_session_data *sd)
  15127. {
  15128. unsigned char buf[33];
  15129. nullpo_retv(sd);
  15130. WBUFW(buf, 0) = 0x2dd;
  15131. WBUFL(buf,2) = sd->bl.id;
  15132. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH); // name don't show in screen.
  15133. WBUFW(buf,30) = sd->bg_id;
  15134. clif_send(buf,packet_len(0x2dd), &sd->bl, AREA);
  15135. }
  15136. void clif_sendbgemblem_single(int32 fd, map_session_data *sd)
  15137. {
  15138. nullpo_retv(sd);
  15139. WFIFOHEAD(fd,32);
  15140. WFIFOW(fd,0) = 0x2dd;
  15141. WFIFOL(fd,2) = sd->bl.id;
  15142. safestrncpy(WFIFOCP(fd,6), sd->status.name, NAME_LENGTH);
  15143. WFIFOW(fd,30) = sd->bg_id;
  15144. WFIFOSET(fd,packet_len(0x2dd));
  15145. }
  15146. /// Battlegrounds queue incoming apply request from client.
  15147. /// Queue types: 1 solo queue, 2 party queue, 4 guild queue.
  15148. /// 0x8d7 <queue type>.W <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_APPLY)
  15149. void clif_parse_bg_queue_apply_request(int32 fd, map_session_data *sd)
  15150. {
  15151. if (!battle_config.feature_bgqueue)
  15152. return;
  15153. nullpo_retv(sd);
  15154. int16 type = RFIFOW(fd,2);
  15155. char name[NAME_LENGTH];
  15156. safestrncpy(name, RFIFOCP(fd, 4), NAME_LENGTH);
  15157. if (sd->bg_queue_id > 0) {
  15158. //ShowWarning("clif_parse_bg_queue_apply_request: Received duplicate queue application: %d from player %s (AID:%d CID:%d).\n", type, sd->status.name, sd->status.account_id, sd->status.char_id);
  15159. clif_bg_queue_apply_result(BG_APPLY_DUPLICATE, name, sd); // Duplicate application warning
  15160. return;
  15161. } else if (type == 1) // Solo
  15162. bg_queue_join_solo(name, sd);
  15163. else if (type == 2) // Party
  15164. bg_queue_join_party(name, sd);
  15165. else if (type == 4) // Guild
  15166. bg_queue_join_guild(name, sd);
  15167. else {
  15168. ShowWarning("clif_parse_bg_queue_apply_request: Received invalid queue type: %d from player %s (AID:%d CID:%d).\n", type, sd->status.name, sd->status.account_id, sd->status.char_id);
  15169. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd); // Someone sent an invalid queue type packet
  15170. return;
  15171. }
  15172. }
  15173. /// Outgoing battlegrounds queue apply result.
  15174. /// Result types: @see e_bg_queue_apply_ack
  15175. /// 0x8d8 <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_APPLY)
  15176. void clif_bg_queue_apply_result(e_bg_queue_apply_ack result, const char *name, map_session_data *sd)
  15177. {
  15178. nullpo_retv(sd);
  15179. int32 fd = sd->fd;
  15180. WFIFOHEAD(fd, packet_len(0x8d8));
  15181. WFIFOW(fd,0) = 0x8d8;
  15182. WFIFOB(fd,2) = result;
  15183. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15184. WFIFOSET(fd, packet_len(0x8d8));
  15185. }
  15186. /// Outgoing battlegrounds queue apply notification.
  15187. /// Sends a notification at the start of the battlegrounds queue and is also used to update the queue number.
  15188. /// 0x8d9 <battleground name>.24B <queue number>.L (ZC_NOTIFY_ENTRY_QUEUE_APPLY)
  15189. void clif_bg_queue_apply_notify(const char *name, map_session_data *sd)
  15190. {
  15191. nullpo_retv(sd);
  15192. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15193. if (queue == nullptr) {
  15194. ShowError("clif_bg_queue_apply_notify: Player is not in a battleground queue.\n");
  15195. return;
  15196. }
  15197. int32 fd = sd->fd;
  15198. WFIFOHEAD(fd, packet_len(0x8d9));
  15199. WFIFOW(fd,0) = 0x8d9;
  15200. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15201. WFIFOL(fd,2+NAME_LENGTH) = static_cast<int32>( queue->teama_members.size() + queue->teamb_members.size() );
  15202. WFIFOSET(fd, packet_len(0x8d9));
  15203. }
  15204. /// Battlegrounds queue outgoing cancel result.
  15205. /// 0x8db <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_CANCEL)
  15206. void clif_bg_queue_cancel_result(bool success, const char *name, map_session_data *sd)
  15207. {
  15208. nullpo_retv(sd);
  15209. int32 fd = sd->fd;
  15210. WFIFOHEAD(fd, packet_len(0x8d8));
  15211. WFIFOW(fd,0) = 0x8db;
  15212. WFIFOB(fd,2) = success;
  15213. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15214. WFIFOSET(fd, packet_len(0x8d8));
  15215. }
  15216. /// Battlegrounds queue incoming cancel request from client.
  15217. /// 0x8da <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_CANCEL)
  15218. void clif_parse_bg_queue_cancel_request(int32 fd, map_session_data *sd)
  15219. {
  15220. if (!battle_config.feature_bgqueue)
  15221. return;
  15222. nullpo_retv(sd);
  15223. bool success;
  15224. if (sd->bg_queue_id > 0) {
  15225. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15226. if (queue && queue->state == QUEUE_STATE_SETUP_DELAY)
  15227. return; // Make the cancel button do nothing if the entry window is open. Otherwise it'll crash the game when you click on both the queue status and entry status window.
  15228. else
  15229. success = bg_queue_leave(sd);
  15230. } else {
  15231. ShowWarning("clif_parse_bg_queue_cancel_request: Player trying to request leaving non-existent queue with name: %s (AID:%d CID:%d).\n", sd->status.name, sd->status.account_id, sd->status.char_id);
  15232. success = false;
  15233. }
  15234. char name[NAME_LENGTH];
  15235. safestrncpy( name, RFIFOCP( fd, 2 ), NAME_LENGTH );
  15236. clif_bg_queue_cancel_result(success, name, sd);
  15237. }
  15238. /// Battleground is ready to be joined, send a window asking for players to accept or decline.
  15239. /// 0x8df <battleground name>.24B <lobby name>.24B (ZC_NOTIFY_LOBBY_ADMISSION)
  15240. void clif_bg_queue_lobby_notify(const char *name, map_session_data *sd)
  15241. {
  15242. nullpo_retv(sd);
  15243. int32 fd = sd->fd;
  15244. WFIFOHEAD(fd, packet_len(0x8df));
  15245. WFIFOW(fd,0) = 0x8df;
  15246. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15247. safestrncpy(WFIFOCP(fd,2+NAME_LENGTH), name, NAME_LENGTH);
  15248. WFIFOSET(fd, packet_len(0x8df));
  15249. }
  15250. /// Incoming packet from client telling server whether player wants to enter battleground or cancel.
  15251. /// Result types: 1(Accept), 2(Decline).
  15252. /// 0x8e0 <result>.B <battleground name>.24B <lobby name>.24B (CZ_REPLY_LOBBY_ADMISSION)
  15253. void clif_parse_bg_queue_lobby_reply(int32 fd, map_session_data *sd)
  15254. {
  15255. nullpo_retv(sd);
  15256. if(sd->bg_queue_id > 0) {
  15257. uint8 result = RFIFOB(fd, 2);
  15258. if(result == 1) { // Accept
  15259. bg_queue_on_accept_invite(sd);
  15260. } else if(result == 2) { // Decline
  15261. bg_queue_leave(sd);
  15262. clif_bg_queue_entry_init(sd);
  15263. }
  15264. }
  15265. }
  15266. /// Plays a gong sound, signaling that someone has accepted the invite to enter a battleground.
  15267. /// 0x8e1 <result>.B <battleground name>.24B <lobby name>.24B (ZC_REPLY_ACK_LOBBY_ADMISSION)
  15268. void clif_bg_queue_ack_lobby(bool result, const char *name, const char *lobbyname, map_session_data *sd)
  15269. {
  15270. nullpo_retv(sd);
  15271. int32 fd = sd->fd;
  15272. WFIFOHEAD(fd, packet_len(0x8e1));
  15273. WFIFOW(fd,0) = 0x8e1;
  15274. WFIFOB(fd,2) = result;
  15275. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15276. safestrncpy(WFIFOCP(fd,3+NAME_LENGTH), lobbyname, NAME_LENGTH);
  15277. WFIFOSET(fd, packet_len(0x8e1));
  15278. }
  15279. /// Battlegrounds queue incoming queue number request from client.
  15280. /// 0x90a <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_RANKING)
  15281. void clif_parse_bg_queue_request_queue_number(int32 fd, map_session_data *sd)
  15282. {
  15283. nullpo_retv(sd);
  15284. char name[NAME_LENGTH];
  15285. safestrncpy( name, RFIFOCP(fd, 2), NAME_LENGTH );
  15286. clif_bg_queue_apply_notify(name, sd);
  15287. }
  15288. /// Silently removes all the battlegrounds stuff client side so that you will open the first BG window when you press battle on the interface.
  15289. /// Send this when a player joins a battleground so that it will remove all the queue stuff upon warping in.
  15290. /// 0x90e (ZC_ENTRY_QUEUE_INIT)
  15291. void clif_bg_queue_entry_init(map_session_data *sd)
  15292. {
  15293. nullpo_retv(sd);
  15294. PACKET_ZC_ENTRY_QUEUE_INIT p = {};
  15295. p.packetType = HEADER_ZC_ENTRY_QUEUE_INIT;
  15296. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15297. }
  15298. /// Custom Fonts (ZC_NOTIFY_FONT).
  15299. /// 02ef <account_id>.L <font id>.W
  15300. void clif_font(map_session_data *sd)
  15301. {
  15302. #if PACKETVER >= 20080102
  15303. unsigned char buf[8];
  15304. nullpo_retv(sd);
  15305. WBUFW(buf,0) = 0x2ef;
  15306. WBUFL(buf,2) = sd->bl.id;
  15307. WBUFW(buf,6) = sd->status.font;
  15308. clif_send(buf, packet_len(0x2ef), &sd->bl, AREA);
  15309. #endif
  15310. }
  15311. /// Required to start the instancing information window on Client
  15312. /// This window re-appears each "refresh" of client automatically until the keep_limit reaches 0.
  15313. /// S 0x2cb <Instance name>.61B <Standby Position>.W
  15314. void clif_instance_create( int32 instance_id, size_t num ){
  15315. #if PACKETVER >= 20071128
  15316. map_session_data *sd = nullptr;
  15317. enum send_target target = PARTY;
  15318. unsigned char buf[65];
  15319. instance_getsd(instance_id, sd, &target);
  15320. if (!sd)
  15321. return;
  15322. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15323. if (!db)
  15324. return;
  15325. WBUFW(buf,0) = 0x2cb;
  15326. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15327. WBUFW( buf, 63 ) = static_cast<int16>( num );
  15328. clif_send(buf,packet_len(0x2cb),&sd->bl,target);
  15329. #endif
  15330. }
  15331. /// To announce Instancing queue creation if no maps available
  15332. /// S 0x2cc <Standby Position>.W
  15333. void clif_instance_changewait(int32 instance_id, int32 num)
  15334. {
  15335. #if PACKETVER >= 20071128
  15336. map_session_data *sd = nullptr;
  15337. enum send_target target = PARTY;
  15338. unsigned char buf[4];
  15339. instance_getsd(instance_id, sd, &target);
  15340. if (!sd)
  15341. return;
  15342. WBUFW(buf,0) = 0x2cc;
  15343. WBUFW(buf,2) = num;
  15344. clif_send(buf,packet_len(0x2cc),&sd->bl,target);
  15345. #endif
  15346. return;
  15347. }
  15348. /// Notify the current status to members
  15349. /// S 0x2cd <Instance Name>.61B <Instance Remaining Time>.L <Instance Noplayers close time>.L
  15350. void clif_instance_status(int32 instance_id, uint32 limit1, uint32 limit2)
  15351. {
  15352. #if PACKETVER >= 20071128
  15353. map_session_data *sd = nullptr;
  15354. enum send_target target = PARTY;
  15355. unsigned char buf[71];
  15356. instance_getsd(instance_id, sd, &target);
  15357. if (!sd)
  15358. return;
  15359. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15360. if (!db)
  15361. return;
  15362. WBUFW(buf,0) = 0x2cd;
  15363. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15364. WBUFL(buf,63) = limit1;
  15365. WBUFL(buf,67) = limit2;
  15366. clif_send(buf,packet_len(0x2cd),&sd->bl,target);
  15367. #endif
  15368. return;
  15369. }
  15370. /// Notify a status change to members
  15371. /// S 0x2ce <Message ID>.L
  15372. /// 0 = Notification (EnterLimitDate update?)
  15373. /// 1 = The Memorial Dungeon expired; it has been destroyed
  15374. /// 2 = The Memorial Dungeon's entry time limit expired; it has been destroyed
  15375. /// 3 = The Memorial Dungeon has been removed.
  15376. /// 4 = Create failure (removes the instance window)
  15377. void clif_instance_changestatus(int32 instance_id, e_instance_notify type, uint32 limit)
  15378. {
  15379. #if PACKETVER >= 20071128
  15380. map_session_data *sd = nullptr;
  15381. enum send_target target = PARTY;
  15382. unsigned char buf[10];
  15383. instance_getsd(instance_id, sd, &target);
  15384. if (!sd)
  15385. return;
  15386. WBUFW(buf,0) = 0x2ce;
  15387. WBUFL(buf,2) = type;
  15388. WBUFL(buf,6) = limit;
  15389. clif_send(buf,packet_len(0x2ce),&sd->bl,target);
  15390. #endif
  15391. return;
  15392. }
  15393. /// Destroy an instance from the status window
  15394. /// 02cf <command>.L (CZ_MEMORIALDUNGEON_COMMAND)
  15395. void clif_parse_MemorialDungeonCommand(int32 fd, map_session_data *sd)
  15396. {
  15397. if (pc_istrading(sd) || pc_isdead(sd) || map_getmapdata(sd->bl.m)->instance_id > 0)
  15398. return;
  15399. const PACKET_CZ_MEMORIALDUNGEON_COMMAND* p = reinterpret_cast<PACKET_CZ_MEMORIALDUNGEON_COMMAND*>( RFIFOP( fd, 0 ) );
  15400. switch (p->command) {
  15401. case COMMAND_MEMORIALDUNGEON_DESTROY_FORCE:
  15402. instance_destroy_command(sd);
  15403. break;
  15404. }
  15405. }
  15406. void clif_instance_info( map_session_data& sd ){
  15407. if( sd.instance_id > 0 ){
  15408. instance_reqinfo( &sd, sd.instance_id );
  15409. }
  15410. if( sd.status.party_id > 0 ){
  15411. struct party_data* p = party_search( sd.status.party_id );
  15412. if( p != nullptr && p->instance_id > 0 ){
  15413. instance_reqinfo( &sd, p->instance_id );
  15414. }
  15415. }
  15416. if( sd.guild != nullptr && sd.guild->instance_id > 0 ){
  15417. instance_reqinfo( &sd, sd.guild->instance_id );
  15418. }
  15419. if( sd.clan != nullptr && sd.clan->instance_id > 0 ){
  15420. instance_reqinfo( &sd, sd.clan->instance_id );
  15421. }
  15422. }
  15423. /// Notifies clients about item picked up by a party member.
  15424. /// 02b8 <account id>.L <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W <equip location>.W <item type>.B (ZC_ITEM_PICKUP_PARTY)
  15425. void clif_party_show_picker( map_session_data* sd, struct item* item_data ){
  15426. #if PACKETVER >= 20071002
  15427. nullpo_retv( sd );
  15428. nullpo_retv( item_data );
  15429. struct item_data* id = itemdb_search( item_data->nameid );
  15430. PACKET_ZC_ITEM_PICKUP_PARTY p = {};
  15431. p.packetType = HEADER_ZC_ITEM_PICKUP_PARTY;
  15432. p.AID = sd->status.account_id;
  15433. p.itemId = client_nameid( item_data->nameid );
  15434. p.identified = item_data->identify;
  15435. p.damaged = item_data->attribute;
  15436. p.refine = item_data->refine;
  15437. clif_addcards( &p.slot, item_data );
  15438. p.location = id->equip; // equip location
  15439. p.itemType = itemtype( id->nameid ); // item type
  15440. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15441. p.grade = item_data->enchantgrade;
  15442. #endif
  15443. clif_send( &p, sizeof( p ), &sd->bl, PARTY_SAMEMAP_WOS );
  15444. #endif
  15445. }
  15446. /** Display gained exp.
  15447. * 07f6 <account id>.L <amount>.L <var id>.W <exp type>.W (ZC_NOTIFY_EXP)
  15448. * 0acc <account id>.L <amount>.Q <var id>.W <exp type>.W (ZC_NOTIFY_EXP2)
  15449. * amount: INT32_MIN ~ INT32_MAX
  15450. * var id:
  15451. * SP_BASEEXP, SP_JOBEXP
  15452. * exp type:
  15453. * 0 = normal exp gained/lost
  15454. * 1 = quest exp gained/lost
  15455. * @param sd Player
  15456. * @param exp EXP value gained/loss
  15457. * @param type SP_BASEEXP, SP_JOBEXP
  15458. * @param quest False:Normal EXP; True:Quest EXP (displayed in purple color)
  15459. * @param lost True:if losing EXP
  15460. */
  15461. void clif_displayexp(map_session_data *sd, t_exp exp, char type, bool quest, bool lost)
  15462. {
  15463. int32 fd;
  15464. int32 offset;
  15465. #if PACKETVER >= 20170830
  15466. int32 cmd = 0xacc;
  15467. #else
  15468. int32 cmd = 0x7f6;
  15469. #endif
  15470. nullpo_retv(sd);
  15471. fd = sd->fd;
  15472. WFIFOHEAD(fd, packet_len(cmd));
  15473. WFIFOW(fd,0) = cmd;
  15474. WFIFOL(fd,2) = sd->bl.id;
  15475. #if PACKETVER >= 20170830
  15476. WFIFOQ(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15477. offset = 4;
  15478. #else
  15479. WFIFOL(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15480. offset = 0;
  15481. #endif
  15482. WFIFOW(fd,10+offset) = type;
  15483. WFIFOW(fd,12+offset) = (quest && type != SP_JOBEXP) ? 1 : 0; // NOTE: Somehow JobEXP always in yellow color
  15484. WFIFOSET(fd,packet_len(cmd));
  15485. }
  15486. /// Displays digital clock digits on top of the screen (ZC_SHOWDIGIT).
  15487. /// type:
  15488. /// 0 = Displays 'value' for 5 seconds.
  15489. /// 1 = Incremental counter (1 tick/second), negated 'value' specifies start value (e.g. using -10 lets the counter start at 10).
  15490. /// 2 = Decremental counter (1 tick/second), negated 'value' specifies start value (does not stop when reaching 0, but overflows).
  15491. /// 3 = Decremental counter (1 tick/second), 'value' specifies start value (stops when reaching 0, displays at most 2 digits).
  15492. /// value:
  15493. /// Except for type 3 it is interpreted as seconds for displaying as DD:HH:MM:SS, HH:MM:SS, MM:SS or SS (leftmost '00' is not displayed).
  15494. void clif_showdigit(map_session_data* sd, unsigned char type, int32 value)
  15495. {
  15496. WFIFOHEAD(sd->fd, packet_len(0x1b1));
  15497. WFIFOW(sd->fd,0) = 0x1b1;
  15498. WFIFOB(sd->fd,2) = type;
  15499. WFIFOL(sd->fd,3) = value;
  15500. WFIFOSET(sd->fd, packet_len(0x1b1));
  15501. }
  15502. /// Notification of the state of client command /effect.
  15503. /// 021d <state>.L (CZ_LESSEFFECT)
  15504. /// state:
  15505. /// 0 = Full effects
  15506. /// 1 = Reduced effects
  15507. ///
  15508. /// NOTE: The state is used on Aegis for sending skill unit packet
  15509. /// 0x11f (ZC_SKILL_ENTRY) instead of 0x1c9 (ZC_SKILL_ENTRY2)
  15510. /// whenever possible. Due to the way the decision check is
  15511. /// constructed, this state tracking was rendered useless,
  15512. /// as the only skill unit, that is sent with 0x1c9 is
  15513. /// Graffiti.
  15514. void clif_parse_LessEffect(int32 fd, map_session_data* sd){
  15515. #if PACKETVER >= 20041115
  15516. const PACKET_CZ_LESSEFFECT* p = reinterpret_cast<PACKET_CZ_LESSEFFECT*>( RFIFOP( fd, 0 ) );
  15517. sd->state.lesseffect = p->state != 0;
  15518. #endif
  15519. }
  15520. /// 07e4 <length>.w <option>.l <val>.l {<index>.w <amount>.w).4b* (CZ_ITEMLISTWIN_RES)
  15521. void clif_parse_ItemListWindowSelected(int32 fd, map_session_data* sd) {
  15522. if( sd == nullptr ){
  15523. return;
  15524. }
  15525. // TODO: shuffle packet
  15526. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  15527. int32 n = (RFIFOW(fd,info->pos[0])-12) / 4;
  15528. int32 type = RFIFOL(fd,info->pos[1]);
  15529. int32 flag = RFIFOL(fd,info->pos[2]); // Button clicked: 0 = Cancel, 1 = OK
  15530. uint16* item_list = (uint16*)RFIFOP(fd,info->pos[3]);
  15531. if( sd->state.trading || sd->npc_shopid )
  15532. return;
  15533. if( flag == 0 || n == 0) {
  15534. clif_menuskill_clear(sd);
  15535. return; // Canceled by player.
  15536. }
  15537. if( sd->menuskill_id != SO_EL_ANALYSIS && sd->menuskill_id != GN_CHANGEMATERIAL ) {
  15538. clif_menuskill_clear(sd);
  15539. return; // Prevent hacking.
  15540. }
  15541. switch( type ) {
  15542. case 0: // Change Material
  15543. skill_changematerial(sd,n,item_list);
  15544. break;
  15545. case 1: // Level 1: Pure to Rough
  15546. case 2: // Level 2: Rough to Pure
  15547. skill_elementalanalysis(*sd,n,type,item_list);
  15548. break;
  15549. }
  15550. clif_menuskill_clear(sd);
  15551. return;
  15552. }
  15553. /*==========================================
  15554. * Elemental System
  15555. *==========================================*/
  15556. /// Notifies client of a change in an elemental's status parameter.
  15557. /// 0x81e <type>.W <value>.L (ZC_EL_PAR_CHANGE)
  15558. void clif_elemental_updatestatus(map_session_data& sd, _sp type) {
  15559. #if PACKETVER >= 20100309
  15560. if (sd.ed == nullptr)
  15561. return;
  15562. PACKET_ZC_EL_PAR_CHANGE p = {};
  15563. p.packetType = HEADER_ZC_EL_PAR_CHANGE;
  15564. p.type = static_cast<decltype(p.type)>(type);
  15565. status_data* status = &sd.ed->battle_status;
  15566. switch( type ) {
  15567. case SP_HP:
  15568. p.value = static_cast<decltype(p.value)>(status->hp);
  15569. break;
  15570. case SP_MAXHP:
  15571. p.value = static_cast<decltype(p.value)>(status->max_hp);
  15572. break;
  15573. case SP_SP:
  15574. p.value = static_cast<decltype(p.value)>(status->sp);
  15575. break;
  15576. case SP_MAXSP:
  15577. p.value = static_cast<decltype(p.value)>(status->max_sp);
  15578. break;
  15579. }
  15580. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  15581. #endif
  15582. }
  15583. void clif_elemental_info(map_session_data *sd) {
  15584. int32 fd;
  15585. s_elemental_data *ed;
  15586. struct status_data *status;
  15587. if( !clif_session_isValid(sd) || (ed = sd->ed) == nullptr )
  15588. return;
  15589. fd = sd->fd;
  15590. status = &ed->battle_status;
  15591. WFIFOHEAD(fd,22);
  15592. WFIFOW(fd, 0) = 0x81d;
  15593. WFIFOL(fd, 2) = ed->bl.id;
  15594. WFIFOL(fd, 6) = status->hp;
  15595. WFIFOL(fd,10) = status->max_hp;
  15596. WFIFOL(fd,14) = status->sp;
  15597. WFIFOL(fd,18) = status->max_sp;
  15598. WFIFOSET(fd,22);
  15599. }
  15600. /// Buying Store System
  15601. ///
  15602. /// Opens preparation window for buying store (ZC_OPEN_BUYING_STORE).
  15603. /// 0810 <slots>.B
  15604. void clif_buyingstore_open(map_session_data* sd)
  15605. {
  15606. int32 fd = sd->fd;
  15607. WFIFOHEAD(fd,packet_len(0x810));
  15608. WFIFOW(fd,0) = 0x810;
  15609. WFIFOB(fd,2) = sd->buyingstore.slots;
  15610. WFIFOSET(fd,packet_len(0x810));
  15611. }
  15612. /// Request to create a buying store.
  15613. /// 0811 <packet len>.W <limit zeny>.L <result>.B <store name>.80B { <name id>.W <amount>.W <price>.L }* (CZ_REQ_OPEN_BUYING_STORE)
  15614. /// result:
  15615. /// 0 = cancel
  15616. /// 1 = open
  15617. static void clif_parse_ReqOpenBuyingStore( int32 fd, map_session_data* sd ){
  15618. const PACKET_CZ_REQ_OPEN_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15619. // TODO: Make this check global for all variable length packets.
  15620. // minimum packet length
  15621. if( p->packetLength < sizeof( *p ) ){
  15622. ShowError( "clif_parse_ReqOpenBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15623. return;
  15624. }
  15625. if (map_getmapflag(sd->bl.m, MF_NOBUYINGSTORE)) {
  15626. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  15627. return;
  15628. }
  15629. if (map_getcell(sd->bl.m, sd->bl.x, sd->bl.y, CELL_CHKNOBUYINGSTORE)) {
  15630. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  15631. return;
  15632. }
  15633. char storename[MESSAGE_SIZE];
  15634. safestrncpy( storename, p->storeName, sizeof( storename ) );
  15635. // so that buyingstore_create knows, how many elements it has access to
  15636. int32 packet_len = p->packetLength - sizeof( *p );
  15637. if( packet_len % sizeof( p->items[0] ) ){
  15638. ShowError( "clif_parse_ReqOpenBuyingStore: Unexpected item list size %u (account_id=%d, block size=%" PRIdPTR ")\n", packet_len, sd->bl.id, sizeof( p->items[0] ) );
  15639. return;
  15640. }
  15641. buyingstore_create( sd, p->zenyLimit, p->result, storename, p->items, packet_len / sizeof( p->items[0] ), nullptr );
  15642. }
  15643. /// Notification, that the requested buying store could not be created (ZC_FAILED_OPEN_BUYING_STORE_TO_BUYER).
  15644. /// 0812 <result>.W <total weight>.L
  15645. /// result:
  15646. /// 1 = "Failed to open buying store." (0x6cd, MSI_BUYINGSTORE_OPEN_FAILED)
  15647. /// 2 = "Total amount of then possessed items exceeds the weight limit by <weight/10-maxweight*90%>. Please re-enter." (0x6ce, MSI_BUYINGSTORE_OVERWEIGHT)
  15648. /// 8 = "No sale (purchase) information available." (0x705)
  15649. /// ? = nothing
  15650. void clif_buyingstore_open_failed(map_session_data* sd, uint16 result, uint32 weight)
  15651. {
  15652. int32 fd = sd->fd;
  15653. WFIFOHEAD(fd,packet_len(0x812));
  15654. WFIFOW(fd,0) = 0x812;
  15655. WFIFOW(fd,2) = result;
  15656. WFIFOL(fd,4) = weight;
  15657. WFIFOSET(fd,packet_len(0x812));
  15658. }
  15659. /// Notification, that the requested buying store was created.
  15660. /// 0813 <packet len>.W <account id>.L <limit zeny>.L { <price>.L <count>.W <type>.B <name id>.W }* (ZC_MYITEMLIST_BUYING_STORE)
  15661. void clif_buyingstore_myitemlist( map_session_data& sd ){
  15662. #if PACKETVER >= 20100309
  15663. PACKET_ZC_MYITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_MYITEMLIST_BUYING_STORE*>( packet_buffer );
  15664. p->packetType = HEADER_ZC_MYITEMLIST_BUYING_STORE;
  15665. p->packetLength = sizeof( *p );
  15666. p->AID = sd.bl.id;
  15667. p->zenyLimit = sd.buyingstore.zenylimit;
  15668. for( int32 i = 0; i < sd.buyingstore.slots; i++ ){
  15669. PACKET_ZC_MYITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15670. entry.price = sd.buyingstore.items[i].price;
  15671. entry.amount = sd.buyingstore.items[i].amount;
  15672. entry.itemType = itemtype( sd.buyingstore.items[i].nameid );
  15673. entry.itemId = client_nameid( sd.buyingstore.items[i].nameid );
  15674. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15675. }
  15676. clif_send( p, p->packetLength, &sd.bl, SELF );
  15677. #endif
  15678. }
  15679. /// Notifies clients in area of a buying store (ZC_BUYING_STORE_ENTRY).
  15680. /// 0814 <account id>.L <store name>.80B
  15681. void clif_buyingstore_entry( map_session_data& sd, struct block_list* tbl ){
  15682. #if PACKETVER >= 20100420
  15683. enum send_target target;
  15684. if( tbl == nullptr ){
  15685. tbl = &sd.bl;
  15686. target = AREA_WOS;
  15687. }else{
  15688. target = SELF;
  15689. }
  15690. PACKET_ZC_BUYING_STORE_ENTRY p = {};
  15691. p.packetType = HEADER_ZC_BUYING_STORE_ENTRY;
  15692. p.makerAID = sd.status.account_id;
  15693. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  15694. clif_send( &p, sizeof( p ), tbl, target );
  15695. #endif
  15696. }
  15697. /// Request to close own buying store (CZ_REQ_CLOSE_BUYING_STORE).
  15698. /// 0815
  15699. static void clif_parse_ReqCloseBuyingStore(int32 fd, map_session_data* sd)
  15700. {
  15701. buyingstore_close(sd);
  15702. }
  15703. /// Notifies clients in area that a buying store was closed (ZC_DISAPPEAR_BUYING_STORE_ENTRY).
  15704. /// 0816 <account id>.L
  15705. void clif_buyingstore_disappear_entry( map_session_data& sd, struct block_list* tbl ){
  15706. #if PACKETVER >= 20100309
  15707. enum send_target target;
  15708. if( tbl == nullptr ){
  15709. tbl = &sd.bl;
  15710. target = AREA_WOS;
  15711. }else{
  15712. target = SELF;
  15713. }
  15714. PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY p = {};
  15715. p.packetType = HEADER_ZC_DISAPPEAR_BUYING_STORE_ENTRY;
  15716. p.makerAID = sd.status.account_id;
  15717. clif_send( &p, sizeof( p ), tbl, target );
  15718. #endif
  15719. }
  15720. /// Request to open someone else's buying store (CZ_REQ_CLICK_TO_BUYING_STORE).
  15721. /// 0817 <account id>.L
  15722. static void clif_parse_ReqClickBuyingStore(int32 fd, map_session_data* sd)
  15723. {
  15724. uint32 account_id;
  15725. // TODO: shuffle packet
  15726. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  15727. buyingstore_open(sd, account_id);
  15728. }
  15729. /// Sends buying store item list.
  15730. /// 0818 <packet len>.W <account id>.L <store id>.L <limit zeny>.L { <price>.L <amount>.W <type>.B <name id>.W }* (ZC_ACK_ITEMLIST_BUYING_STORE)
  15731. void clif_buyingstore_itemlist( map_session_data& sd, map_session_data& pl_sd ){
  15732. #if PACKETVER >= 20100309
  15733. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_ACK_ITEMLIST_BUYING_STORE*>( packet_buffer );
  15734. p->packetType = HEADER_ZC_ACK_ITEMLIST_BUYING_STORE;
  15735. p->packetLength = sizeof( *p );
  15736. p->AID = pl_sd.bl.id;
  15737. p->storeId = pl_sd.buyer_id;
  15738. p->zenyLimit = pl_sd.buyingstore.zenylimit;
  15739. for( int32 i = 0; i < pl_sd.buyingstore.slots; i++ ){
  15740. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15741. entry.price = pl_sd.buyingstore.items[i].price;
  15742. entry.amount = pl_sd.buyingstore.items[i].amount; // TODO: Figure out, if no longer needed items (amount == 0) are listed on official.
  15743. entry.itemType = itemtype(pl_sd.buyingstore.items[i].nameid);
  15744. entry.itemId = client_nameid( pl_sd.buyingstore.items[i].nameid );
  15745. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15746. }
  15747. clif_send( p, p->packetLength, &sd.bl, SELF );
  15748. #endif
  15749. }
  15750. /// Request to sell items to a buying store.
  15751. /// 0819 <packet len>.W <account id>.L <store id>.L { <index>.W <name id>.W <amount>.W }* (CZ_REQ_TRADE_BUYING_STORE)
  15752. static void clif_parse_ReqTradeBuyingStore( int32 fd, map_session_data* sd ){
  15753. const PACKET_CZ_REQ_TRADE_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_TRADE_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15754. // minimum packet length
  15755. if( p->packetLength < sizeof( *p ) ){
  15756. ShowError( "clif_parse_ReqTradeBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15757. return;
  15758. }
  15759. // so that buyingstore_trade knows, how many elements it has access to
  15760. int32 packet_len = p->packetLength - sizeof( *p );
  15761. if( packet_len % sizeof( p->items[0] ) ){
  15762. ShowError( "clif_parse_ReqTradeBuyingStore: Unexpected item list size %u (account_id=%d, buyer_id=%d, block size=%" PRIdPTR ")\n", packet_len, sd->bl.id, p->AID, sizeof( p->items[0] ) );
  15763. return;
  15764. }
  15765. buyingstore_trade( sd, p->AID, p->storeId, p->items, packet_len / sizeof( p->items[0] ) );
  15766. }
  15767. /// Notifies the buyer, that the buying store has been closed due to a post-trade condition (ZC_FAILED_TRADE_BUYING_STORE_TO_BUYER).
  15768. /// 081a <result>.W
  15769. /// result:
  15770. /// 3 = "All items within the buy limit were purchased." (0x6cf, MSI_BUYINGSTORE_TRADE_OVERLIMITZENY)
  15771. /// 4 = "All items were purchased." (0x6d0, MSI_BUYINGSTORE_TRADE_BUYCOMPLETE)
  15772. /// ? = nothing
  15773. void clif_buyingstore_trade_failed_buyer(map_session_data* sd, int16 result)
  15774. {
  15775. int32 fd = sd->fd;
  15776. WFIFOHEAD(fd,packet_len(0x81a));
  15777. WFIFOW(fd,0) = 0x81a;
  15778. WFIFOW(fd,2) = result;
  15779. WFIFOSET(fd,packet_len(0x81a));
  15780. }
  15781. /// Updates the zeny limit and an item in the buying store item list.
  15782. /// 081b <name id>.W <amount>.W <limit zeny>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE)
  15783. /// 09e6 <name id>.W <amount>.W <zeny>.L <limit zeny>.L <GID>.L <Date>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE2)
  15784. void clif_buyingstore_update_item( map_session_data* sd, t_itemid nameid, uint16 amount, uint32 char_id, int32 zeny ){
  15785. PACKET_ZC_UPDATE_ITEM_FROM_BUYING_STORE p = {};
  15786. p.packetType = buyingStoreUpdateItemType;
  15787. p.itemId = client_nameid( nameid );
  15788. p.amount = amount;
  15789. p.zenyLimit = sd->buyingstore.zenylimit;
  15790. #if PACKETVER >= 20141016
  15791. p.zeny = zeny;
  15792. p.charId = char_id; // GID
  15793. p.updateTime = (int32)time(nullptr);
  15794. #endif
  15795. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15796. }
  15797. /// Deletes item from inventory, that was sold to a buying store (ZC_ITEM_DELETE_BUYING_STORE).
  15798. /// 081c <index>.W <amount>.W <price>.L
  15799. /// message:
  15800. /// "%s (%d) were sold at %dz." (0x6d2, MSI_BUYINGSTORE_TRADE_SELLCOMPLETE)
  15801. ///
  15802. /// NOTE: This function has to be called _instead_ of clif_delitem/clif_dropitem.
  15803. void clif_buyingstore_delete_item(map_session_data* sd, int16 index, uint16 amount, int32 price)
  15804. {
  15805. int32 fd = sd->fd;
  15806. WFIFOHEAD(fd,packet_len(0x81c));
  15807. WFIFOW(fd,0) = 0x81c;
  15808. WFIFOW(fd,2) = client_index( index );
  15809. WFIFOW(fd,4) = amount;
  15810. WFIFOL(fd,6) = price; // price per item, client calculates total Zeny by itself
  15811. WFIFOSET(fd,packet_len(0x81c));
  15812. }
  15813. /// Notifies the seller, that a buying store trade failed.
  15814. /// 0824 <result>.W <name id>.W (ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER)
  15815. /// result:
  15816. /// 5 = "The deal has failed." (0x39, MSI_DEAL_FAIL)
  15817. /// 6 = "The trade failed, because the entered amount of item %s is higher, than the buyer is willing to buy." (0x6d3, MSI_BUYINGSTORE_TRADE_OVERCOUNT)
  15818. /// 7 = "The trade failed, because the buyer is lacking required balance." (0x6d1, MSI_BUYINGSTORE_TRADE_LACKBUYERZENY)
  15819. /// ? = nothing
  15820. void clif_buyingstore_trade_failed_seller( map_session_data* sd, int16 result, t_itemid nameid ){
  15821. PACKET_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER p = {};
  15822. p.packetType = HEADER_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER;
  15823. p.result = result;
  15824. p.itemId = client_nameid( nameid );
  15825. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15826. }
  15827. /// Search Store Info System
  15828. ///
  15829. /// Request to search for stores.
  15830. /// 0835 <packet len>.W <type>.B <max price>.L <min price>.L <name id count>.B <card count>.B { <name id>.W }* { <card>.W }* (CZ_SEARCH_STORE_INFO)
  15831. /// type:
  15832. /// 0 = Vending
  15833. /// 1 = Buying Store
  15834. ///
  15835. /// NOTE: The client determines the item ids by specifying a name and optionally,
  15836. /// amount of card slots. If the client does not know about the item it
  15837. /// cannot be searched.
  15838. static void clif_parse_SearchStoreInfo( int32 fd, map_session_data *sd ){
  15839. if (!battle_config.feature_search_stores)
  15840. return;
  15841. const PACKET_CZ_SEARCH_STORE_INFO* p = reinterpret_cast<PACKET_CZ_SEARCH_STORE_INFO*>( RFIFOP( fd, 0 ) );
  15842. // minimum packet length
  15843. if( p->packetLength < sizeof( *p ) ){
  15844. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15845. return;
  15846. }
  15847. // check, if there is enough data for the claimed count of items
  15848. int32 packet_len = p->packetLength - sizeof( *p );
  15849. if( packet_len % sizeof( p->items[0] ) ){
  15850. ShowError( "clif_parse_SearchStoreInfo: Unexpected item list size %u (account_id=%d, block size=%" PRIdPTR ")\n", packet_len, sd->bl.id, sizeof( p->items[0] ) );
  15851. return;
  15852. }
  15853. int32 count = packet_len / sizeof( p->items[0] );
  15854. if( count < p->itemsCount + p->cardsCount ){
  15855. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected count=%u, count=%u, account_id=%d).\n", p->itemsCount + p->cardsCount, count, sd->bl.id );
  15856. return;
  15857. }
  15858. if ( p->searchType > SEARCHTYPE_BUYING_STORE ) {
  15859. ShowError( "clif_parse_SearchStoreInfo: Invalid search type %u (account_id=%d).\n", p->searchType, sd->bl.id );
  15860. return;
  15861. }
  15862. if ( p->minPrice > battle_config.vending_max_value ) {
  15863. ShowError( "clif_parse_SearchStoreInfo: Invalid min price %u (account_id=%d).\n", p->minPrice, sd->bl.id );
  15864. return;
  15865. }
  15866. if ( p->maxPrice > battle_config.vending_max_value ) {
  15867. ShowError( "clif_parse_SearchStoreInfo: Invalid max price %u (account_id=%d).\n", p->maxPrice, sd->bl.id );
  15868. return;
  15869. }
  15870. searchstore_query( *sd, static_cast<e_searchstore_searchtype>(p->searchType), p->minPrice, p->maxPrice, &p->items[0], p->itemsCount, &p->items[p->itemsCount], p->cardsCount );
  15871. }
  15872. /// Results for a store search request.
  15873. /// 0836 <packet len>.W <is first page>.B <is next page>.B <remaining uses>.B { <store id>.L <account id>.L <shop name>.80B <nameid>.W <item type>.B <price>.L <amount>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }* (ZC_SEARCH_STORE_INFO_ACK)
  15874. /// is first page:
  15875. /// 0 = appends to existing results
  15876. /// 1 = clears previous results before displaying this result set
  15877. /// is next page:
  15878. /// 0 = no "next" label
  15879. /// 1 = "next" label to retrieve more results
  15880. void clif_search_store_info_ack( map_session_data& sd ){
  15881. #if PACKETVER_MAIN_NUM >= 20100817 || PACKETVER_RE_NUM >= 20100706 || defined(PACKETVER_ZERO)
  15882. if (!battle_config.feature_search_stores)
  15883. return;
  15884. uint32 start = sd.searchstore.pages * SEARCHSTORE_RESULTS_PER_PAGE ;
  15885. uint32 end = umin( static_cast<uint32>( sd.searchstore.items.size() ), start + SEARCHSTORE_RESULTS_PER_PAGE );
  15886. PACKET_ZC_SEARCH_STORE_INFO_ACK* p = reinterpret_cast<PACKET_ZC_SEARCH_STORE_INFO_ACK*>( packet_buffer );
  15887. p->packetType = HEADER_ZC_SEARCH_STORE_INFO_ACK;
  15888. p->packetLength = sizeof( *p );
  15889. p->firstPage = !sd.searchstore.pages;
  15890. p->nextPage = searchstore_querynext( sd );
  15891. p->usesCount = static_cast<decltype(p->usesCount)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(p->usesCount)>::max() ) );
  15892. for( int32 i = 0, count = 0; i < end - start; i++ ){
  15893. std::shared_ptr<s_search_store_info_item> ssitem = sd.searchstore.items[start + i];
  15894. if( ssitem == nullptr ){
  15895. continue;
  15896. }
  15897. PACKET_ZC_SEARCH_STORE_INFO_ACK_sub& entry = p->items[count];
  15898. entry.storeId = ssitem->store_id;
  15899. entry.AID = ssitem->account_id;
  15900. safestrncpy( entry.shopName, ssitem->store_name, MESSAGE_SIZE );
  15901. entry.itemId = client_nameid( ssitem->nameid );
  15902. entry.itemType = itemtype( ssitem->nameid );
  15903. entry.price = ssitem->price;
  15904. entry.amount = ssitem->amount;
  15905. entry.refine = ssitem->refine;
  15906. // make-up an item for clif_addcards
  15907. struct item it = {};
  15908. for( int32 j = 0; j < MAX_SLOTS; j++ ){
  15909. it.card[j] = ssitem->card[j];
  15910. }
  15911. it.nameid = ssitem->nameid;
  15912. it.amount = ssitem->amount;
  15913. clif_addcards( &entry.slot, &it );
  15914. #if PACKETVER >= 20150226
  15915. clif_add_random_options( entry.option_data, it );
  15916. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15917. entry.grade = ssitem->enchantgrade;
  15918. #endif
  15919. #endif
  15920. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15921. count++;
  15922. }
  15923. clif_send( p, p->packetLength, &sd.bl, SELF );
  15924. #endif
  15925. }
  15926. /// Notification of failure when searching for stores.
  15927. /// 0837 <reason>.B (ZC_SEARCH_STORE_INFO_FAILED)
  15928. void clif_search_store_info_failed(map_session_data& sd, e_searchstore_failure reason){
  15929. #if PACKETVER_MAIN_NUM >= 20100601 || PACKETVER_RE_NUM >= 20100601 || defined(PACKETVER_ZERO)
  15930. if (!battle_config.feature_search_stores)
  15931. return;
  15932. PACKET_ZC_SEARCH_STORE_INFO_FAILED packet{};
  15933. packet.packetType = HEADER_ZC_SEARCH_STORE_INFO_FAILED;
  15934. packet.reason = static_cast<decltype(packet.reason)>(reason);
  15935. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15936. #endif
  15937. }
  15938. /// Request to display next page of results.
  15939. /// 0838 (CZ_SEARCH_STORE_INFO_NEXT_PAGE)
  15940. static void clif_parse_SearchStoreInfoNextPage(int32 fd, map_session_data* sd){
  15941. if (!battle_config.feature_search_stores)
  15942. return;
  15943. searchstore_next(*sd);
  15944. }
  15945. /// Opens the search store window.
  15946. /// 083a <effect>.W <remaining uses>.B (ZC_OPEN_SEARCH_STORE_INFO)
  15947. void clif_open_search_store_info(map_session_data& sd){
  15948. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15949. if (!battle_config.feature_search_stores)
  15950. return;
  15951. PACKET_ZC_OPEN_SEARCH_STORE_INFO packet{};
  15952. packet.packetType = HEADER_ZC_OPEN_SEARCH_STORE_INFO;
  15953. packet.effect = static_cast<decltype(packet.effect)>(sd.searchstore.effect);
  15954. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15955. packet.remainingUses = static_cast<decltype(packet.remainingUses)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(packet.remainingUses)>::max() ) );
  15956. #endif
  15957. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15958. #endif
  15959. }
  15960. /// Request to close the store search window.
  15961. /// 083b (CZ_CLOSE_SEARCH_STORE_INFO)
  15962. static void clif_parse_CloseSearchStoreInfo(int32 fd, map_session_data* sd){
  15963. if (!battle_config.feature_search_stores)
  15964. return;
  15965. searchstore_close(*sd);
  15966. }
  15967. /// Request to invoke catalog effect on a store from search results.
  15968. /// 083c <account id>.L <store id>.L <nameid>.W (CZ_SSILIST_ITEM_CLICK)
  15969. static void clif_parse_SearchStoreInfoListItemClick( int32 fd, map_session_data* sd ){
  15970. if (!battle_config.feature_search_stores)
  15971. return;
  15972. const PACKET_CZ_SSILIST_ITEM_CLICK* p = reinterpret_cast<PACKET_CZ_SSILIST_ITEM_CLICK*>( RFIFOP( fd, 0 ) );
  15973. searchstore_click( *sd, p->AID, p->storeId, p->itemId );
  15974. }
  15975. /// Notification of the store position on current map.
  15976. /// 083d <xPos>.W <yPos>.W (ZC_SSILIST_ITEM_CLICK_ACK)
  15977. void clif_search_store_info_click_ack(map_session_data& sd, int16 x, int16 y){
  15978. #if PACKETVER_MAIN_NUM >= 20100608 || PACKETVER_RE_NUM >= 20100608 || defined(PACKETVER_ZERO)
  15979. if (!battle_config.feature_search_stores)
  15980. return;
  15981. PACKET_ZC_SSILIST_ITEM_CLICK_ACK packet{};
  15982. packet.packetType = HEADER_ZC_SSILIST_ITEM_CLICK_ACK;
  15983. packet.x = x;
  15984. packet.y = y;
  15985. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15986. #endif
  15987. }
  15988. /// Parse function for packet debugging.
  15989. void clif_parse_debug(int32 fd,map_session_data *sd)
  15990. {
  15991. int16 packet_len;
  15992. // clif_parse ensures, that there is at least 2 bytes of data
  15993. uint16 cmd = RFIFOW(fd,0);
  15994. if( sd ) {
  15995. packet_len = packet_db[cmd].len;
  15996. if( packet_len == 0 )
  15997. {// unknown
  15998. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  15999. }
  16000. else if( packet_len == -1 )
  16001. {// variable length
  16002. packet_len = RFIFOW(fd,2); // clif_parse ensures, that this amount of data is already received
  16003. }
  16004. ShowDebug("Packet debug of 0x%04X (length %d), %s session #%d, %d/%d (AID/CID)\n", cmd, packet_len, sd->state.active ? "authed" : "unauthed", fd, sd->status.account_id, sd->status.char_id);
  16005. }
  16006. else
  16007. {
  16008. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  16009. ShowDebug("Packet debug of 0x%04X (length %d), session #%d\n", cmd, packet_len, fd);
  16010. }
  16011. ShowDump(RFIFOP(fd,0), packet_len);
  16012. }
  16013. /*==========================================
  16014. * Server tells client to display a window similar to Magnifier (item) one
  16015. * Server populates the window with avilable elemental converter options according to player's inventory
  16016. *------------------------------------------*/
  16017. void clif_elementalconverter_list( map_session_data& sd ){
  16018. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16019. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16020. p->packetLength = sizeof( *p );
  16021. int32 count = 0;
  16022. for( int32 i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  16023. if( skill_can_produce_mix( &sd, skill_produce_db[i].nameid, 23, 1 ) ){
  16024. p->items[count].itemId = client_nameid( skill_produce_db[i].nameid );
  16025. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16026. count++;
  16027. }
  16028. }
  16029. clif_send( p, p->packetLength, &sd.bl, SELF );
  16030. if( count > 0 ){
  16031. sd.menuskill_id = SA_CREATECON;
  16032. sd.menuskill_val = count;
  16033. }
  16034. }
  16035. /**
  16036. * Rune Knight
  16037. **/
  16038. void clif_millenniumshield( block_list& bl, int16 shields ){
  16039. #if PACKETVER >= 20081126
  16040. PACKET_ZC_MILLENNIUMSHIELD packet{};
  16041. packet.packetType = HEADER_ZC_MILLENNIUMSHIELD;
  16042. packet.aid = bl.id;
  16043. packet.num = shields;
  16044. packet.state = 0;
  16045. clif_send( &packet, sizeof( packet ), &bl, AREA );
  16046. #endif
  16047. }
  16048. /**
  16049. * Mechanic
  16050. **/
  16051. /*==========================================
  16052. * Magic Decoy Material List
  16053. *------------------------------------------*/
  16054. void clif_magicdecoy_list( map_session_data& sd, uint16 skill_lv, int16 x, int16 y ){
  16055. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16056. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16057. p->packetLength = sizeof( *p );
  16058. size_t count = 0;
  16059. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  16060. if( itemdb_group.item_exists( IG_ELEMENT, sd.inventory.u.items_inventory[i].nameid ) ){
  16061. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  16062. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16063. count++;
  16064. }
  16065. }
  16066. if( count > 0 ){
  16067. clif_send( p, p->packetLength, &sd.bl, SELF );
  16068. sd.menuskill_id = NC_MAGICDECOY;
  16069. sd.menuskill_val = skill_lv;
  16070. sd.sc.comet_x = x;
  16071. sd.sc.comet_y = y;
  16072. }else{
  16073. clif_skill_fail( sd, NC_MAGICDECOY );
  16074. }
  16075. }
  16076. /*==========================================
  16077. * Guillotine Cross Poisons List
  16078. *------------------------------------------*/
  16079. void clif_poison_list( map_session_data& sd, uint16 skill_lv ){
  16080. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16081. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16082. p->packetLength = sizeof( *p );
  16083. size_t count = 0;
  16084. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  16085. if( itemdb_group.item_exists( IG_POISON, sd.inventory.u.items_inventory[i].nameid ) ){
  16086. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  16087. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16088. count++;
  16089. }
  16090. }
  16091. if( count > 0 ){
  16092. clif_send( p, p->packetLength, &sd.bl, SELF );
  16093. sd.menuskill_id = GC_POISONINGWEAPON;
  16094. sd.menuskill_val = skill_lv;
  16095. }else{
  16096. clif_skill_fail( sd, GC_POISONINGWEAPON, USESKILL_FAIL_GUILLONTINE_POISON );
  16097. }
  16098. }
  16099. /// 0442 <Length>.W <why>.L { <Skill_Id>.W }* (ZC_SKILL_SELECT_REQUEST).
  16100. void clif_autoshadowspell_list( map_session_data& sd ){
  16101. #if PACKETVER >= 20081210
  16102. if( sd.menuskill_id == SC_AUTOSHADOWSPELL )
  16103. return;
  16104. PACKET_ZC_SKILL_SELECT_REQUEST* p = reinterpret_cast<PACKET_ZC_SKILL_SELECT_REQUEST*>( packet_buffer );
  16105. p->packetType = HEADER_ZC_SKILL_SELECT_REQUEST;
  16106. p->packetLength = sizeof( *p );
  16107. p->flag = 1; // enum PACKET_ZC_SKILL_SELECT_REQUEST::enumWHY::WHY_SC_AUTOSHADOWSPELL = 0x1
  16108. size_t count = 0;
  16109. for( size_t i = 0; i < MAX_SKILL; i++ ){
  16110. if( sd.status.skill[i].flag == SKILL_FLAG_PLAGIARIZED && sd.status.skill[i].id > 0 &&
  16111. skill_get_inf2(sd.status.skill[i].id, INF2_ISAUTOSHADOWSPELL))
  16112. {
  16113. p->skillIds[count] = sd.status.skill[i].id;
  16114. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skillIds[0] ) );
  16115. count++;
  16116. }
  16117. }
  16118. if( count > 0 ) {
  16119. clif_send( p, p->packetLength, &sd.bl, SELF );
  16120. sd.menuskill_id = SC_AUTOSHADOWSPELL;
  16121. sd.menuskill_val = static_cast<decltype(sd.menuskill_val)>( count );
  16122. } else {
  16123. status_change_end( &sd.bl, SC_STOP );
  16124. clif_skill_fail( sd, SC_AUTOSHADOWSPELL, USESKILL_FAIL_IMITATION_SKILL_NONE );
  16125. }
  16126. #endif
  16127. }
  16128. /*===========================================
  16129. * Skill list for Four Elemental Analysis
  16130. * and Change Material skills.
  16131. * 07e3 <type>.L (ZC_ITEMLISTWIN_OPEN).
  16132. *------------------------------------------*/
  16133. int32 clif_skill_itemlistwindow( map_session_data *sd, uint16 skill_id, uint16 skill_lv ) {
  16134. #if PACKETVER >= 20090922
  16135. int32 fd = sd->fd;
  16136. nullpo_ret(sd);
  16137. sd->menuskill_id = skill_id; // To prevent hacking.
  16138. sd->menuskill_val = skill_lv;
  16139. if( skill_id == GN_CHANGEMATERIAL )
  16140. skill_lv = 0; // Changematerial
  16141. WFIFOHEAD(fd, packet_len(0x7e3));
  16142. WFIFOW(fd,0) = 0x7e3;
  16143. WFIFOL(fd,2) = skill_lv;
  16144. WFIFOL(fd,4) = 0;
  16145. WFIFOSET(fd, packet_len(0x7e3));
  16146. #endif
  16147. return 1;
  16148. }
  16149. /*==========================================
  16150. * Select a skill into a given list (used by SA_AUTOSPELL/SC_AUTOSHADOWSPELL)
  16151. * 0443 <type>.L <skill_id>.W (CZ_SKILL_SELECT_RESPONSE)
  16152. *------------------------------------------*/
  16153. void clif_parse_SkillSelectMenu(int32 fd, map_session_data *sd) {
  16154. #if PACKETVER >= 20081210
  16155. if( sd == nullptr ){
  16156. return;
  16157. }
  16158. const PACKET_CZ_SKILL_SELECT_RESPONSE* p = reinterpret_cast<PACKET_CZ_SKILL_SELECT_RESPONSE*>( RFIFOP( fd, 0 ) );
  16159. if (sd->menuskill_id == SA_AUTOSPELL) {
  16160. sd->state.workinprogress = WIP_DISABLE_NONE;
  16161. skill_autospell(sd, p->selectedSkillId);
  16162. } else if (sd->menuskill_id == SC_AUTOSHADOWSPELL) {
  16163. if (pc_istrading(sd)) {
  16164. clif_skill_fail( *sd, sd->ud.skill_id );
  16165. clif_menuskill_clear(sd);
  16166. return;
  16167. }
  16168. skill_select_menu(*sd, p->selectedSkillId);
  16169. } else
  16170. return;
  16171. clif_menuskill_clear(sd);
  16172. #endif
  16173. }
  16174. /// Kagerou/Oboro amulet spirit.
  16175. /// 08cf <id>.L <type>.W <num>.W (ZC_SPIRITS_ATTRIBUTE)
  16176. void clif_spiritcharm( map_session_data& sd ){
  16177. #if PACKETVER >= 20111102
  16178. PACKET_ZC_SPIRITS_ATTRIBUTE packet{};
  16179. packet.packetType = HEADER_ZC_SPIRITS_ATTRIBUTE;
  16180. packet.aid = sd.bl.id;
  16181. packet.spiritsType = static_cast<decltype(packet.spiritsType)>(sd.spiritcharm_type);
  16182. packet.num = static_cast<decltype(packet.num)>(sd.spiritcharm);
  16183. clif_send( &packet, sizeof( packet ), &sd.bl, AREA );
  16184. #endif
  16185. }
  16186. /// Move Item from or to Personal Tab
  16187. /// 0907 <index>.W <type>.B (CZ_INVENTORY_TAB)
  16188. /// type:
  16189. /// 0 = move item to personal tab
  16190. /// 1 = move item to normal tab
  16191. void clif_parse_MoveItem( int32 fd, map_session_data* sd ){
  16192. // TODO: Check for correct packet version
  16193. #if PACKETVER >= 20120410
  16194. /* can't move while dead. */
  16195. if(pc_isdead(sd)) {
  16196. return;
  16197. }
  16198. const PACKET_CZ_INVENTORY_TAB* p = reinterpret_cast<PACKET_CZ_INVENTORY_TAB*>( RFIFOP( fd, 0 ) );
  16199. uint16 index = server_index( p->index );
  16200. if( index >= MAX_INVENTORY ){
  16201. return;
  16202. }
  16203. if ( sd->inventory.u.items_inventory[index].favorite != 0 && p->favorite == true )
  16204. sd->inventory.u.items_inventory[index].favorite = 0;
  16205. else if( p->favorite == false )
  16206. sd->inventory.u.items_inventory[index].favorite = 1;
  16207. else
  16208. return;/* nothing to do. */
  16209. clif_favorite_item( *sd, index );
  16210. #endif
  16211. }
  16212. /// Items that are in favorite tab of inventory.
  16213. /// 0908 <index>.W <favorite>.B (ZC_INVENTORY_TAB)
  16214. static void clif_favorite_item( map_session_data& sd, uint16 index ){
  16215. // TODO: Check for correct packet version
  16216. #if PACKETVER >= 20111122
  16217. PACKET_ZC_INVENTORY_TAB p = {};
  16218. p.packetType = HEADER_ZC_INVENTORY_TAB;
  16219. p.index = client_index( index );
  16220. p.favorite = ( sd.inventory.u.items_inventory[index].favorite == 1 ) ? false : true;
  16221. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16222. #endif
  16223. }
  16224. /// 08d2 <id>.L <Pos X>.W <Pos Y>.W (ZC_FASTMOVE).
  16225. void clif_snap( struct block_list *bl, int16 x, int16 y ) {
  16226. unsigned char buf[10];
  16227. WBUFW(buf,0) = 0x8d2;
  16228. WBUFL(buf,2) = bl->id;
  16229. WBUFW(buf,6) = x;
  16230. WBUFW(buf,8) = y;
  16231. if( disguised(bl) )
  16232. {
  16233. clif_send(buf, packet_len(0x8d2), bl, AREA_WOS);
  16234. WBUFL(buf,2) = disguised_bl_id(bl->id);
  16235. clif_send(buf, packet_len(0x8d2), bl, SELF);
  16236. } else
  16237. clif_send(buf,packet_len(0x8d2),bl, AREA);
  16238. }
  16239. /// 0977 <id>.L <HP>.L <maxHP>.L (ZC_HP_INFO).
  16240. void clif_monster_hp_bar( struct mob_data* md, int32 fd ) {
  16241. #if PACKETVER >= 20120404
  16242. WFIFOHEAD(fd,packet_len(0x977));
  16243. WFIFOW(fd,0) = 0x977;
  16244. WFIFOL(fd,2) = md->bl.id;
  16245. WFIFOL(fd,6) = md->status.hp;
  16246. WFIFOL(fd,10) = md->status.max_hp;
  16247. WFIFOSET(fd,packet_len(0x977));
  16248. #endif
  16249. }
  16250. /* [Ind] placeholder for unsupported incoming packets (avoids server disconnecting client) */
  16251. void __attribute__ ((unused)) clif_parse_dull(int32 fd, map_session_data *sd) {
  16252. return;
  16253. }
  16254. /// Ack world info (ZC_ACK_BEFORE_WORLD_INFO)
  16255. /// 0979 <world name>.24B <char name>.24B
  16256. void clif_ackworldinfo(map_session_data* sd) {
  16257. int32 fd;
  16258. nullpo_retv(sd);
  16259. fd = sd->fd;
  16260. WFIFOHEAD(fd,packet_len(0x979));
  16261. WFIFOW(fd,0)=0x979;
  16262. //AID -> world name ?
  16263. safestrncpy(WFIFOCP(fd,2), "" /* World name */, 24);
  16264. safestrncpy(WFIFOCP(fd,26), sd->status.name, NAME_LENGTH);
  16265. WFIFOSET(fd,packet_len(0x979));
  16266. }
  16267. /// req world info (CZ_REQ_BEFORE_WORLD_INFO)
  16268. /// 0978 <AID>.L
  16269. void clif_parse_reqworldinfo(int32 fd,map_session_data *sd) {
  16270. //uint32 aid = RFIFOL(fd,2); //should we trust client ?
  16271. if(sd)
  16272. clif_ackworldinfo(sd);
  16273. }
  16274. static void clif_loadConfirm( map_session_data *sd ){
  16275. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410
  16276. nullpo_retv( sd );
  16277. PACKET_ZC_NOTIFY_ACTORINIT p = {};
  16278. p.packetType = HEADER_ZC_NOTIFY_ACTORINIT;
  16279. clif_send( &p, sizeof(p), &sd->bl, SELF );
  16280. clif_instance_info( *sd );
  16281. #endif
  16282. }
  16283. /// unknown usage (CZ_BLOCKING_PLAY_CANCEL)
  16284. /// 0447
  16285. void clif_parse_blocking_playcancel( int32 fd, map_session_data *sd ){
  16286. clif_loadConfirm( sd );
  16287. int32 mf = map_getmapflag(sd->bl.m, MF_SPECIALPOPUP);
  16288. if (mf > 0) {
  16289. clif_specialpopup(*sd, mf);
  16290. }
  16291. }
  16292. /// req world info (CZ_CLIENT_VERSION)
  16293. /// 044A <version>.L
  16294. void clif_parse_client_version(int32 fd, map_session_data *sd) {
  16295. //if(sd)
  16296. ;
  16297. }
  16298. /// Ranking list
  16299. /// ranking pointlist { <name>.24B }*10 { <point>.L }*10
  16300. void clif_sub_ranklist( RANKLIST& ranklist, e_rank rankingtype ){
  16301. struct fame_list* list;
  16302. size_t i;
  16303. size_t size = std::min<size_t>( ARRAYLENGTH( ranklist.names ), MAX_FAME_LIST );
  16304. switch( rankingtype ){
  16305. case RANK_BLACKSMITH:
  16306. list = smith_fame_list;
  16307. break;
  16308. case RANK_ALCHEMIST:
  16309. list = chemist_fame_list;
  16310. break;
  16311. case RANK_TAEKWON:
  16312. list = taekwon_fame_list;
  16313. break;
  16314. case RANK_KILLER:
  16315. // Currently not supported
  16316. list = nullptr;
  16317. size = 0;
  16318. break;
  16319. default:
  16320. ShowError( "clif_sub_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16321. return;
  16322. }
  16323. for( i = 0; i < size; i++ ){
  16324. ranklist.points[i] = list[i].fame;
  16325. if( list[i].id == 0 ){
  16326. safestrncpy( ranklist.names[i], "None", sizeof( ranklist.names[0] ) );
  16327. continue;
  16328. }
  16329. if( strcmp( list[i].name, "-" ) != 0 ){
  16330. safestrncpy( ranklist.names[i], list[i].name, sizeof( ranklist.names[0] ) );
  16331. continue;
  16332. }
  16333. const char* name = map_charid2nick( list[i].id );
  16334. if( name != nullptr ){
  16335. safestrncpy( ranklist.names[i], name, sizeof( ranklist.names[0] ) );
  16336. }else{
  16337. safestrncpy( ranklist.names[i], list[i].name, sizeof( ranklist.names[0] ) );
  16338. }
  16339. }
  16340. size = ARRAYLENGTH( ranklist.names );
  16341. for( ; i < size; i++ ){
  16342. safestrncpy( ranklist.names[i], "Unavailable", sizeof( ranklist.names[0] ) );
  16343. ranklist.points[i] = 0;
  16344. }
  16345. }
  16346. /// 0219 { <name>.24B }*10 { <point>.L }*10 (ZC_BLACKSMITH_RANK)
  16347. /// 021a { <name>.24B }*10 { <point>.L }*10 (ZC_ALCHEMIST_RANK)
  16348. /// 0226 { <name>.24B }*10 { <point>.L }*10 (ZC_TAEKWON_RANK)
  16349. /// 0238 { <name>.24B }*10 { <point>.L }*10 (ZC_KILLER_RANK)
  16350. /// 097d <RankingType>.W { <name>.24B }*10 { <point>.L }*10 <mypoint>L (ZC_ACK_RANKING)
  16351. /// 0af6 <RankingType>.W { <CID>.L }*10 { <point>.L }*10 <mypoint>L (ZC_ACK_RANKING2)
  16352. void clif_ranklist( map_session_data& sd, e_rank rankingtype ){
  16353. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190703 || PACKETVER_ZERO_NUM >= 20190724
  16354. PACKET_ZC_ACK_RANKING2 p = {};
  16355. p.packetType = HEADER_ZC_ACK_RANKING2;
  16356. p.type = rankingtype;
  16357. struct fame_list* list;
  16358. size_t i;
  16359. size_t size = std::min<size_t>( ARRAYLENGTH( p.CIDs ), MAX_FAME_LIST );
  16360. switch( rankingtype ){
  16361. case RANK_BLACKSMITH:
  16362. list = smith_fame_list;
  16363. break;
  16364. case RANK_ALCHEMIST:
  16365. list = chemist_fame_list;
  16366. break;
  16367. case RANK_TAEKWON:
  16368. list = taekwon_fame_list;
  16369. break;
  16370. case RANK_KILLER:
  16371. // Currently not supported
  16372. list = nullptr;
  16373. size = 0;
  16374. break;
  16375. default:
  16376. ShowError( "clif_sub_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16377. return;
  16378. }
  16379. for( i = 0; i < size; i++ ){
  16380. p.CIDs[i] = list[i].id;
  16381. p.points[i] = list[i].fame;
  16382. }
  16383. size = ARRAYLENGTH( p.CIDs );
  16384. for( ; i < size; i++ ){
  16385. p.CIDs[i] = 0;
  16386. p.points[i] = 0;
  16387. }
  16388. switch( sd.class_&MAPID_UPPERMASK ){
  16389. case MAPID_BLACKSMITH:
  16390. case MAPID_ALCHEMIST:
  16391. case MAPID_TAEKWON:
  16392. p.mypoints = sd.status.fame;
  16393. break;
  16394. default:
  16395. p.mypoints = 0;
  16396. break;
  16397. }
  16398. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16399. #elif PACKETVER_MAIN_NUM >= 20130605 || PACKETVER_RE_NUM >= 20130529 || defined(PACKETVER_ZERO)
  16400. PACKET_ZC_ACK_RANKING p = {};
  16401. p.packetType = HEADER_ZC_ACK_RANKING;
  16402. p.type = rankingtype;
  16403. clif_sub_ranklist( p.list, rankingtype );
  16404. switch( sd.class_&MAPID_UPPERMASK ){
  16405. case MAPID_BLACKSMITH:
  16406. case MAPID_ALCHEMIST:
  16407. case MAPID_TAEKWON:
  16408. p.mypoints = sd.status.fame;
  16409. break;
  16410. default:
  16411. p.mypoints = 0;
  16412. break;
  16413. }
  16414. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16415. #else
  16416. switch( rankingtype ){
  16417. case RANK_BLACKSMITH: {
  16418. #if PACKETVER >= 20041108
  16419. PACKET_ZC_BLACKSMITH_RANK p = {};
  16420. p.packetType = HEADER_ZC_BLACKSMITH_RANK;
  16421. clif_sub_ranklist( p.list, RANK_BLACKSMITH );
  16422. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16423. #endif
  16424. } break;
  16425. case RANK_ALCHEMIST: {
  16426. #if PACKETVER >= 20041108
  16427. PACKET_ZC_ALCHEMIST_RANK p = {};
  16428. p.packetType = HEADER_ZC_ALCHEMIST_RANK;
  16429. clif_sub_ranklist( p.list, RANK_ALCHEMIST );
  16430. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16431. #endif
  16432. } break;
  16433. case RANK_TAEKWON: {
  16434. #if PACKETVER >= 20050328
  16435. PACKET_ZC_TAEKWON_RANK p = {};
  16436. p.packetType = HEADER_ZC_TAEKWON_RANK;
  16437. clif_sub_ranklist( p.list, RANK_TAEKWON );
  16438. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16439. #endif
  16440. } break;
  16441. case RANK_KILLER: {
  16442. #if PACKETVER >= 20050328
  16443. PACKET_ZC_KILLER_RANK p = {};
  16444. p.packetType = HEADER_ZC_KILLER_RANK;
  16445. clif_sub_ranklist( p.list, RANK_KILLER );
  16446. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16447. #endif
  16448. } break;
  16449. default:
  16450. ShowError( "clif_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16451. return;
  16452. }
  16453. #endif
  16454. }
  16455. /// Request for the blacksmith ranklist.
  16456. /// /blacksmith command sends this packet to the server.
  16457. /// 0217 (CZ_BLACKSMITH_RANK)
  16458. void clif_parse_ranklist_blacksmith( int32 fd, map_session_data* sd ){
  16459. #if PACKETVER >= 20041108
  16460. const PACKET_CZ_BLACKSMITH_RANK* p = reinterpret_cast<PACKET_CZ_BLACKSMITH_RANK*>( RFIFOP( fd, 0 ) );
  16461. clif_ranklist( *sd, RANK_BLACKSMITH );
  16462. #endif
  16463. }
  16464. /// Request for the alchemist ranklist.
  16465. /// /alchemist command sends this packet to the server.
  16466. /// 0218 (CZ_ALCHEMIST_RANK)
  16467. void clif_parse_ranklist_alchemist( int32 fd, map_session_data* sd ){
  16468. #if PACKETVER >= 20041108
  16469. const PACKET_CZ_ALCHEMIST_RANK* p = reinterpret_cast<PACKET_CZ_ALCHEMIST_RANK*>( RFIFOP( fd, 0 ) );
  16470. clif_ranklist( *sd, RANK_ALCHEMIST );
  16471. #endif
  16472. }
  16473. /// Request for the taekwon ranklist.
  16474. /// /taekwon command sends this packet to the server.
  16475. /// 0225 (CZ_TAEKWON_RANK)
  16476. void clif_parse_ranklist_taekwon( int32 fd, map_session_data* sd ){
  16477. #if PACKETVER >= 20050328
  16478. const PACKET_CZ_TAEKWON_RANK* p = reinterpret_cast<PACKET_CZ_TAEKWON_RANK*>( RFIFOP( fd, 0 ) );
  16479. clif_ranklist( *sd, RANK_TAEKWON );
  16480. #endif
  16481. }
  16482. /// Request for the killer ranklist.
  16483. /// /pk command sends this packet to the server.
  16484. /// 0237 (CZ_KILLER_RANK)
  16485. void clif_parse_ranklist_killer( int32 fd, map_session_data* sd ){
  16486. #if PACKETVER >= 20050530
  16487. const PACKET_CZ_KILLER_RANK* p = reinterpret_cast<PACKET_CZ_KILLER_RANK*>( RFIFOP( fd, 0 ) );
  16488. clif_ranklist( *sd, RANK_KILLER );
  16489. #endif
  16490. }
  16491. ///
  16492. /// 097c <type>.W (CZ_REQ_RANKING)
  16493. /// type
  16494. /// 0: /blacksmith
  16495. /// 1: /alchemist
  16496. /// 2: /taekwon
  16497. /// 3: /pk
  16498. void clif_parse_ranklist( int fd, map_session_data* sd ){
  16499. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502
  16500. const PACKET_CZ_REQ_RANKING* p = reinterpret_cast<PACKET_CZ_REQ_RANKING*>( RFIFOP( fd, 0 ) );
  16501. switch( p->type ){
  16502. case RANK_BLACKSMITH:
  16503. case RANK_ALCHEMIST:
  16504. case RANK_TAEKWON:
  16505. case RANK_KILLER:
  16506. break;
  16507. default:
  16508. return;
  16509. }
  16510. clif_ranklist( *sd, static_cast<e_rank>( p->type ) );
  16511. #endif
  16512. }
  16513. /// Updates the fame rank points for the given ranking.
  16514. /// 021b <points>.L <total points>.L (ZC_BLACKSMITH_POINT)
  16515. /// 021c <points>.L <total points>.L (ZC_ALCHEMIST_POINT)
  16516. /// 0224 <points>.L <total points>.L (ZC_TAEKWON_POINT)
  16517. /// 0236 <points>.L <total points>.L (ZC_KILLER_POINT)
  16518. /// 097e <RankingType>.W <point>.L <TotalPoint>.L (ZC_UPDATE_RANKING_POINT)
  16519. void clif_update_rankingpoint( map_session_data& sd, e_rank rankingtype, uint32 point ){
  16520. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502
  16521. PACKET_ZC_UPDATE_RANKING_POINT p = {};
  16522. p.packetType = HEADER_ZC_UPDATE_RANKING_POINT;
  16523. p.type = rankingtype;
  16524. p.points = point;
  16525. p.points_total = sd.status.fame;
  16526. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16527. #else
  16528. switch(rankingtype){
  16529. case RANK_BLACKSMITH: {
  16530. #if PACKETVER >= 20041108
  16531. PACKET_ZC_BLACKSMITH_POINT p = {};
  16532. p.packetType = HEADER_ZC_BLACKSMITH_POINT;
  16533. p.points = point;
  16534. p.points_total = sd.status.fame;
  16535. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16536. #endif
  16537. } break;
  16538. case RANK_ALCHEMIST: {
  16539. #if PACKETVER >= 20041108
  16540. PACKET_ZC_ALCHEMIST_POINT p = {};
  16541. p.packetType = HEADER_ZC_ALCHEMIST_POINT;
  16542. p.points = point;
  16543. p.points_total = sd.status.fame;
  16544. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16545. #endif
  16546. } break;
  16547. case RANK_TAEKWON: {
  16548. #if PACKETVER >= 20050328
  16549. PACKET_ZC_TAEKWON_POINT p = {};
  16550. p.packetType = HEADER_ZC_TAEKWON_POINT;
  16551. p.points = point;
  16552. p.points_total = sd.status.fame;
  16553. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16554. #endif
  16555. } break;
  16556. case RANK_KILLER: {
  16557. #if PACKETVER >= 20050530
  16558. PACKET_ZC_KILLER_POINT p = {};
  16559. p.packetType = HEADER_ZC_KILLER_POINT;
  16560. p.points = point;
  16561. p.points_total = sd.status.fame;
  16562. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16563. #endif
  16564. } break;
  16565. default:
  16566. ShowError( "clif_update_rankingpoint: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16567. return;
  16568. }
  16569. #endif
  16570. }
  16571. /**
  16572. * Transmit personal information to player. (rates)
  16573. * 0x08cb <packet len>.W <exp>.W <death>.W <drop>.W <DETAIL_EXP_INFO>7B (ZC_PERSONAL_INFOMATION)
  16574. * <InfoType>.B <Exp>.W <Death>.W <Drop>.W (DETAIL_EXP_INFO 0x08cb)
  16575. * 0x097b <packet len>.W <exp>.L <death>.L <drop>.L <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION2)
  16576. * 0x0981 <packet len>.W <exp>.W <death>.W <drop>.W <activity rate>.W <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION_CHN)
  16577. * <InfoType>.B <Exp>.L <Death>.L <Drop>.L (DETAIL_EXP_INFO 0x97b|0981)
  16578. * FIXME!
  16579. * - Find/decide for details of EXP, Drop, and Death penalty rates
  16580. * - For now, we're assuming values for DETAIL_EXP_INFO are:
  16581. * 0 - map adjustment (bexp mapflag), 1 - Premium/VIP adjustment, 2 - Server rate adjustment, 3 - None
  16582. */
  16583. #ifdef VIP_ENABLE
  16584. void clif_display_pinfo( map_session_data& sd ){
  16585. #if PACKETVER_MAIN_NUM >= 20110627 || PACKETVER_RE_NUM >= 20110628 || defined(PACKETVER_ZERO)
  16586. // TODO: Whoever wants to take the blame, fix this crappy logic below - and yes I know indentation is wrong, but CANT TOUCH THIS! *sings* [Lemongrass]
  16587. int32 details_bexp[PINFO_MAX], details_drop[PINFO_MAX], details_penalty[PINFO_MAX];
  16588. /**
  16589. * EXP
  16590. */
  16591. //0:PCRoom
  16592. details_bexp[PINFO_BASIC] = map_getmapflag( sd.bl.m, MF_BEXP );
  16593. if (details_bexp[PINFO_BASIC] == 100 || !details_bexp[PINFO_BASIC])
  16594. details_bexp[PINFO_BASIC] = 0;
  16595. else {
  16596. if (details_bexp[PINFO_BASIC] < 100) {
  16597. details_bexp[PINFO_BASIC] = 100 - details_bexp[PINFO_BASIC];
  16598. details_bexp[PINFO_BASIC] = 0 - details_bexp[PINFO_BASIC];
  16599. } else
  16600. details_bexp[PINFO_BASIC] = details_bexp[PINFO_BASIC] - 100;
  16601. }
  16602. //1:Premium
  16603. if( pc_isvip( &sd ) ){
  16604. details_bexp[PINFO_PREMIUM] = battle_config.vip_base_exp_increase * battle_config.base_exp_rate / 100;
  16605. if (details_bexp[PINFO_PREMIUM] < 0)
  16606. details_bexp[PINFO_PREMIUM] = 0 - details_bexp[PINFO_PREMIUM];
  16607. } else
  16608. details_bexp[PINFO_PREMIUM] = 0;
  16609. //2:Server
  16610. details_bexp[PINFO_SERVER] = battle_config.base_exp_rate;
  16611. if (details_bexp[PINFO_SERVER] == 100)
  16612. details_bexp[PINFO_SERVER] = 0;
  16613. else {
  16614. if (details_bexp[PINFO_SERVER] < 100) {
  16615. details_bexp[PINFO_SERVER] = 100 - details_bexp[PINFO_SERVER];
  16616. details_bexp[PINFO_SERVER] = 0 - details_bexp[PINFO_SERVER];
  16617. } else
  16618. details_bexp[PINFO_SERVER] = details_bexp[PINFO_SERVER] - 100;
  16619. }
  16620. //3:TPLUS
  16621. details_bexp[PINFO_CAFE] = 0;
  16622. /**
  16623. * Drop rate
  16624. */
  16625. //0:PCRoom
  16626. details_drop[PINFO_BASIC] = 0;
  16627. //1:Premium
  16628. if( pc_isvip( &sd ) ){
  16629. details_drop[PINFO_PREMIUM] = (battle_config.vip_drop_increase * battle_config.item_rate_common) / 100;
  16630. if (details_drop[PINFO_PREMIUM] < 0)
  16631. details_drop[PINFO_PREMIUM] = 0 - details_drop[PINFO_PREMIUM];
  16632. } else
  16633. details_drop[PINFO_PREMIUM] = 0;
  16634. //2:Server
  16635. details_drop[PINFO_SERVER] = battle_config.item_rate_common;
  16636. if (details_drop[PINFO_SERVER] == 100)
  16637. details_drop[PINFO_SERVER] = 0;
  16638. else {
  16639. if (details_drop[PINFO_SERVER] < 100) {
  16640. details_drop[PINFO_SERVER] = 100 - details_drop[PINFO_SERVER];
  16641. details_drop[PINFO_SERVER] = 0 - details_drop[PINFO_SERVER];
  16642. } else
  16643. details_drop[PINFO_SERVER] = details_drop[PINFO_SERVER] - 100;
  16644. }
  16645. //3:TPLUS
  16646. details_drop[PINFO_CAFE] = 0;
  16647. /**
  16648. * Penalty rate
  16649. */
  16650. //! FIXME: Current penalty system makes this announcement unable to give info on + or - rate
  16651. //0:PCRoom
  16652. details_penalty[PINFO_BASIC] = 0;
  16653. //1:Premium
  16654. if( pc_isvip( &sd ) ){
  16655. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base;
  16656. if (details_penalty[PINFO_PREMIUM] == 100)
  16657. details_penalty[PINFO_PREMIUM] = 0;
  16658. else {
  16659. if (details_penalty[PINFO_PREMIUM] < 100) {
  16660. details_penalty[PINFO_PREMIUM] = 100 - details_penalty[PINFO_PREMIUM];
  16661. details_penalty[PINFO_PREMIUM] = 0 - details_penalty[PINFO_PREMIUM];
  16662. } else
  16663. details_penalty[PINFO_PREMIUM] = details_penalty[PINFO_PREMIUM] - 100;
  16664. }
  16665. if (battle_config.death_penalty_base > battle_config.vip_exp_penalty_base)
  16666. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base - battle_config.death_penalty_base;
  16667. } else
  16668. details_penalty[PINFO_PREMIUM] = 0;
  16669. //2:Server
  16670. details_penalty[PINFO_SERVER] = battle_config.death_penalty_base;
  16671. if (details_penalty[PINFO_SERVER] == 100)
  16672. details_penalty[PINFO_SERVER] = 0;
  16673. else {
  16674. if (details_penalty[PINFO_SERVER] < 100) {
  16675. details_penalty[PINFO_SERVER] = 100 - details_penalty[PINFO_SERVER];
  16676. details_penalty[PINFO_SERVER] = 0 - details_penalty[PINFO_SERVER];
  16677. } else
  16678. details_penalty[PINFO_SERVER] = details_penalty[PINFO_SERVER] - 100;
  16679. }
  16680. //3:TPLUS
  16681. details_penalty[PINFO_CAFE] = 0;
  16682. PACKET_ZC_PERSONAL_INFOMATION* p = reinterpret_cast<PACKET_ZC_PERSONAL_INFOMATION*>( packet_buffer );
  16683. p->packetType = HEADER_ZC_PERSONAL_INFOMATION;
  16684. p->length = sizeof( *p );
  16685. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16686. p->total_exp = 100 * 1000;
  16687. p->total_death = 100 * 1000;
  16688. p->total_drop = 100 * 1000;
  16689. #else
  16690. p->total_exp = 100;
  16691. p->total_death = 100;
  16692. p->total_drop = 100;
  16693. #endif
  16694. for( int32 i = PINFO_BASIC; i < PINFO_MAX; i++ ){
  16695. p->details[i].type = i; // infotype 0 PCRoom, 1 Premium, 2 Server, 3 TPlus
  16696. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16697. p->details[i].exp = details_bexp[i] * 1000;
  16698. p->details[i].death = details_penalty[i] * 1000;
  16699. p->details[i].drop = details_drop[i] * 1000;
  16700. #else
  16701. p->details[i].exp = details_bexp[i];
  16702. p->details[i].death = details_penalty[i];
  16703. p->details[i].drop = details_drop[i];
  16704. #endif
  16705. p->total_exp += p->details[i].exp;
  16706. p->total_death += p->details[i].death;
  16707. p->total_drop += p->details[i].drop;
  16708. p->length += static_cast<decltype(p->length)>( sizeof( p->details[0] ) );
  16709. }
  16710. clif_send( p, p->length, &sd.bl, SELF );
  16711. #endif
  16712. }
  16713. #endif
  16714. void clif_parse_GMFullStrip(int32 fd, map_session_data *sd) {
  16715. char cmd[30];
  16716. int32 t_aid = RFIFOL(fd,2);
  16717. safesnprintf(cmd,sizeof(cmd),"%cfullstrip %d",atcommand_symbol,t_aid);
  16718. is_atcommand(fd, sd, cmd, 1);
  16719. }
  16720. /**
  16721. * Marks Crimson Marker target on mini-map to the caster
  16722. * 09C1 <id>.L <x>.W <y>.W (ZC_C_MARKERINFO)
  16723. * @param fd
  16724. * @param bl Crimson Marker target
  16725. **/
  16726. void clif_crimson_marker( map_session_data& sd, struct block_list& bl, bool remove ){
  16727. #if PACKETVER_MAIN_NUM >= 20130731 || PACKETVER_RE_NUM >= 20130707 || defined(PACKETVER_ZERO)
  16728. PACKET_ZC_C_MARKERINFO p = {};
  16729. p.PacketType = HEADER_ZC_C_MARKERINFO;
  16730. p.AID = bl.id;
  16731. if( remove ){
  16732. p.xPos = -1;
  16733. p.yPos = -1;
  16734. }else{
  16735. p.xPos = bl.x;
  16736. p.yPos = bl.y;
  16737. }
  16738. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16739. #endif
  16740. }
  16741. /**
  16742. * 02d3 <index>.W (ZC_NOTIFY_BIND_ON_EQUIP)
  16743. **/
  16744. void clif_notify_bindOnEquip( map_session_data& sd, int16 index ){
  16745. #if PACKETVER >= 20070227
  16746. PACKET_ZC_NOTIFY_BIND_ON_EQUIP p = {};
  16747. p.packetType = HEADER_ZC_NOTIFY_BIND_ON_EQUIP;
  16748. p.index = client_index( index );
  16749. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16750. #endif
  16751. }
  16752. /**
  16753. * [Ind/Hercules]
  16754. * 08b3 <Length>.W <id>.L <message>.?B (ZC_SHOWSCRIPT)
  16755. **/
  16756. void clif_showscript(struct block_list* bl, const char* message, enum send_target flag) {
  16757. char buf[256];
  16758. size_t len;
  16759. nullpo_retv(bl);
  16760. if(!message)
  16761. return;
  16762. len = strlen(message)+1;
  16763. if( len > sizeof(buf)-8 ) {
  16764. ShowWarning("clif_showscript: Truncating too long message '%s' (len=%" PRIuPTR ").\n", message, len);
  16765. len = sizeof(buf)-8;
  16766. }
  16767. WBUFW(buf,0) = 0x8b3;
  16768. WBUFW(buf,2) = (uint16)(len+8);
  16769. WBUFL(buf,4) = bl->id;
  16770. safestrncpy(WBUFCP(buf,8), message, len);
  16771. clif_send((unsigned char *) buf, WBUFW(buf,2), bl, flag);
  16772. }
  16773. /**
  16774. * [Ind/Hercules]
  16775. * 07fc <OldMasterAID>.L <NewMasterAID>.L (ZC_CHANGE_GROUP_MASTER)
  16776. **/
  16777. void clif_party_leaderchanged(map_session_data *sd, int32 prev_leader_aid, int32 new_leader_aid) {
  16778. unsigned char buf[10];
  16779. nullpo_retv(sd);
  16780. WBUFW(buf,0) = 0x7fc;
  16781. WBUFL(buf,2) = prev_leader_aid;
  16782. WBUFL(buf,6) = new_leader_aid;
  16783. clif_send(buf, packet_len(0x7fc), &sd->bl, PARTY);
  16784. }
  16785. /**
  16786. * Sends a clan message to a player
  16787. * 098e <length>.W <name>.24B <message>.?B (ZC_NOTIFY_CLAN_CHAT)
  16788. **/
  16789. void clif_clan_message( struct clan& clan, const char *mes, size_t len ){
  16790. #if PACKETVER >= 20131223
  16791. map_session_data* sd = clan_getavailablesd( clan );
  16792. if( len == 0 ){
  16793. return;
  16794. }
  16795. PACKET_ZC_NOTIFY_CLAN_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CLAN_CHAT*>( packet_buffer );
  16796. // Maximum message length = size of our packet buffer minus fixed size of the packet and 1 byte zero termination
  16797. static size_t len_max = sizeof( packet_buffer ) - sizeof( *p ) - 1;
  16798. // Is the length bigger than the maximum message length
  16799. if( len > len_max ){
  16800. ShowWarning( "clif_clan_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", clan_id=%d).\n", mes, len, len_max, clan.id );
  16801. len = len_max;
  16802. }
  16803. p->PacketType = HEADER_ZC_NOTIFY_CLAN_CHAT;
  16804. p->PacketLength = sizeof( *p );
  16805. // Offially the sender name should also be filled here, but it is not required by the client and since it's in the message too we do not fill it
  16806. safestrncpy( p->MemberName, "", sizeof( p->MemberName ) );
  16807. safestrncpy( p->Message, mes, len + 1 );
  16808. p->PacketLength += static_cast<decltype(p->PacketLength)>( len + 1 );
  16809. clif_send( p, p->PacketLength, &sd->bl, CLAN );
  16810. #endif
  16811. }
  16812. /**
  16813. * Parses a clan message from a player.
  16814. * 098d <length>.W <text>.?B (<name> : <message>) (CZ_CLAN_CHAT)
  16815. **/
  16816. void clif_parse_clan_chat( int32 fd, map_session_data* sd ){
  16817. #if PACKETVER >= 20131223
  16818. if( sd == nullptr ){
  16819. return;
  16820. }
  16821. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  16822. // validate packet and retrieve name and message
  16823. if( !clif_process_message(sd, false, name, message, output ) )
  16824. return;
  16825. clan_send_message( *sd, RFIFOCP(fd,4), RFIFOW(fd,2) - 4 );
  16826. #endif
  16827. }
  16828. /**
  16829. * Sends the basic clan informations to the client.
  16830. * 098a <length>.W <clan id>.L <clan name>.24B <clan master>.24B <clan map>.16B <alliance count>.B
  16831. * <antagonist count>.B { <alliance>.24B } * alliance count { <antagonist>.24B } * antagonist count (ZC_CLANINFO)
  16832. **/
  16833. void clif_clan_basicinfo( map_session_data& sd ){
  16834. #if PACKETVER_MAIN_NUM >= 20130626 || PACKETVER_RE_NUM >= 20130605 || defined(PACKETVER_ZERO)
  16835. struct clan* clan = sd.clan;
  16836. if( clan == nullptr ){
  16837. return;
  16838. }
  16839. PACKET_ZC_CLANINFO* p = reinterpret_cast<PACKET_ZC_CLANINFO*>( packet_buffer );
  16840. p->PacketType = HEADER_ZC_CLANINFO;
  16841. p->PacketLength = sizeof( *p );
  16842. p->ClanID = clan->id;
  16843. safestrncpy( p->ClanName, clan->name, sizeof( p->ClanName ) );
  16844. safestrncpy( p->MasterName, clan->master, sizeof( p->MasterName ) );
  16845. mapindex_getmapname_ext( clan->map, p->Map );
  16846. p->AllyCount = clan_get_alliance_count( *clan, 0 );
  16847. p->AntagonistCount = clan_get_alliance_count( *clan, 1 );
  16848. for( int32 flag = 0; flag < 2; flag++ ){
  16849. for( int32 i = 0; i < MAX_CLANALLIANCE; i++ ){
  16850. if( clan->alliance[i].clan_id > 0 && clan->alliance[i].opposition == flag ){
  16851. char* name = reinterpret_cast<char*>( WBUFP( p, p->PacketLength ) );
  16852. safestrncpy( name, clan->alliance[i].name, NAME_LENGTH );
  16853. p->PacketLength += static_cast<decltype(p->PacketLength)>( NAME_LENGTH );
  16854. }
  16855. }
  16856. }
  16857. clif_send( p, p->PacketLength, &sd.bl, SELF );
  16858. #endif
  16859. }
  16860. /**
  16861. * Updates the online and maximum player count of a clan.
  16862. * 0988 <online count>.W <maximum member amount>.W (ZC_NOTIFY_CLAN_CONNECTINFO)
  16863. **/
  16864. void clif_clan_onlinecount( struct clan& clan ){
  16865. #if PACKETVER >= 20131223
  16866. map_session_data* sd = clan_getavailablesd( clan );
  16867. if( sd == nullptr){
  16868. return;
  16869. }
  16870. PACKET_ZC_NOTIFY_CLAN_CONNECTINFO p = {};
  16871. p.PacketType = HEADER_ZC_NOTIFY_CLAN_CONNECTINFO;
  16872. p.NumConnect = clan.connect_member;
  16873. p.NumTotal = clan.max_member;
  16874. clif_send( &p, sizeof( p ), &sd->bl, CLAN );
  16875. #endif
  16876. }
  16877. /**
  16878. * Notifies the client that the player has left his clan.
  16879. * 0989 (ZC_ACK_CLAN_LEAVE)
  16880. **/
  16881. void clif_clan_leave( map_session_data& sd ){
  16882. #if PACKETVER >= 20131223
  16883. PACKET_ZC_ACK_CLAN_LEAVE p = {};
  16884. p.PacketType = HEADER_ZC_ACK_CLAN_LEAVE;
  16885. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16886. #endif
  16887. }
  16888. /**
  16889. * Acknowledge the client about change title result (ZC_ACK_CHANGE_TITLE).
  16890. * 0A2F <result>.B <title_id>.L
  16891. */
  16892. void clif_change_title_ack(map_session_data *sd, unsigned char result, unsigned long title_id)
  16893. {
  16894. #if PACKETVER >= 20150513
  16895. int32 fd;
  16896. nullpo_retv(sd);
  16897. if (!clif_session_isValid(sd))
  16898. return;
  16899. fd = sd->fd;
  16900. WFIFOHEAD(fd, packet_len(0xa2f));
  16901. WFIFOW(fd, 0) = 0xa2f;
  16902. WFIFOB(fd, 2) = result;
  16903. WFIFOL(fd, 3) = title_id;
  16904. WFIFOSET(fd, packet_len(0xa2f));
  16905. #endif
  16906. }
  16907. /**
  16908. * Parsing a request from the client change title (CZ_REQ_CHANGE_TITLE).
  16909. * 0A2E <title_id>.L
  16910. */
  16911. void clif_parse_change_title(int32 fd, map_session_data *sd)
  16912. {
  16913. int32 title_id;
  16914. nullpo_retv(sd);
  16915. title_id = RFIFOL(fd, 2);
  16916. if( title_id == sd->status.title_id ){
  16917. // It is exactly the same as the old one
  16918. return;
  16919. }else if( title_id <= 0 ){
  16920. sd->status.title_id = 0;
  16921. }else{
  16922. if (std::find(sd->titles.begin(), sd->titles.end(), title_id) == sd->titles.end()) {
  16923. clif_change_title_ack(sd, 1, title_id);
  16924. return;
  16925. }
  16926. sd->status.title_id = title_id;
  16927. }
  16928. clif_name_area(&sd->bl);
  16929. clif_change_title_ack(sd, 0, title_id);
  16930. }
  16931. #ifdef DUMP_UNKNOWN_PACKET
  16932. void DumpUnknown(int32 fd,TBL_PC *sd,int32 cmd,int32 packet_len)
  16933. {
  16934. const char* packet_txt = "save/packet.txt";
  16935. FILE* fp;
  16936. time_t time_server;
  16937. struct tm *datetime;
  16938. char datestr[512];
  16939. time(&time_server); // get time in seconds since 1/1/1970
  16940. datetime = localtime(&time_server); // convert seconds in structure
  16941. // like sprintf, but only for date/time (Sunday, November 02 2003 15:12:52)
  16942. strftime(datestr, sizeof(datestr)-1, "%A, %B %d %Y %X.", datetime); // Server time (normal time): %A, %B %d %Y %X.
  16943. if( ( fp = fopen( packet_txt , "a" ) ) != nullptr ) {
  16944. if( sd ) {
  16945. fprintf(fp, "Unknown packet 0x%04X (length %d), %s session #%d, %d/%d (AID/CID) at %s \n", cmd, packet_len, sd->state.active ? "authed" : "unauthed", fd, sd->status.account_id, sd->status.char_id,datestr);
  16946. } else {
  16947. fprintf(fp, "Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16948. }
  16949. WriteDump(fp, RFIFOP(fd,0), packet_len);
  16950. fprintf(fp, "\n");
  16951. fclose(fp);
  16952. } else {
  16953. ShowError("Failed to write '%s'.\n", packet_txt);
  16954. // Dump on console instead
  16955. if( sd ) {
  16956. ShowDebug("Unknown packet 0x%04X (length %d), %s session #%d, %d/%d (AID/CID) at %s\n", cmd, packet_len, sd->state.active ? "authed" : "unauthed", fd, sd->status.account_id, sd->status.char_id,datestr);
  16957. } else {
  16958. ShowDebug("Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16959. }
  16960. ShowDump(RFIFOP(fd,0), packet_len);
  16961. }
  16962. }
  16963. #endif
  16964. /// Roulette System
  16965. /// Author: Yommy
  16966. void roulette_generate_bonus( map_session_data& sd ){
  16967. if( battle_config.feature_roulette_bonus_reward && sd.roulette.bonusItemID == 0 ){
  16968. int32 next_stage = 0;
  16969. if( sd.roulette_point.bronze > 0 ){
  16970. next_stage = 0;
  16971. }else if( sd.roulette_point.silver > 9 ){
  16972. next_stage = 2;
  16973. }else if( sd.roulette_point.gold > 9 ){
  16974. next_stage = 4;
  16975. }
  16976. // Get bonus item stage only from current stage or higher
  16977. int32 reward_stage = rnd_value( next_stage, MAX_ROULETTE_LEVEL - 1 );
  16978. sd.roulette.bonusItemID = rd.nameid[reward_stage][rnd()%rd.items[reward_stage]];
  16979. }else{
  16980. sd.roulette.bonusItemID = 0;
  16981. }
  16982. }
  16983. /// Opens the roulette window
  16984. /// 0A1A <result>.B <serial>.L <stage>.B <price index>.B <additional item id>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_OPEN_ROULETTE)
  16985. void clif_roulette_open( map_session_data* sd ){
  16986. nullpo_retv( sd );
  16987. roulette_generate_bonus( *sd );
  16988. struct packet_roulette_open_ack p;
  16989. p.PacketType = 0xa1a;
  16990. p.Result = 0; // result
  16991. p.Serial = 0; // serial
  16992. p.Step = (sd->roulette.claimPrize) ? sd->roulette.stage - 1 : 0;
  16993. p.Idx = (sd->roulette.claimPrize) ? sd->roulette.prizeIdx : -1;
  16994. p.AdditionItemID = sd->roulette.bonusItemID;
  16995. p.GoldPoint = sd->roulette_point.gold;
  16996. p.SilverPoint = sd->roulette_point.silver;
  16997. p.BronzePoint = sd->roulette_point.bronze;
  16998. sd->state.roulette_open = true;
  16999. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17000. }
  17001. /// Request to open the roulette window
  17002. /// 0A19 (CZ_REQ_OPEN_ROULETTE)
  17003. void clif_parse_roulette_open( int32 fd, map_session_data* sd ){
  17004. nullpo_retv(sd);
  17005. if (!battle_config.feature_roulette) {
  17006. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17007. return;
  17008. }
  17009. clif_roulette_open(sd);
  17010. }
  17011. /// Sends the info about the available roulette rewards to the client
  17012. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.W <amount>.W } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO)
  17013. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.L <amount>.L } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO) >= 20180516
  17014. void clif_roulette_info( map_session_data* sd ){
  17015. nullpo_retv(sd);
  17016. struct packet_roulette_info_ack p;
  17017. p.PacketType = rouletteinfoackType;
  17018. p.PacketLength = sizeof(p);
  17019. p.RouletteSerial = 1;
  17020. for( int32 i = 0, count = 0; i < MAX_ROULETTE_LEVEL; i++ ){
  17021. for( int32 j = 0; j < MAX_ROULETTE_COLUMNS - i; j++ ){
  17022. p.ItemInfo[count].Row = i;
  17023. p.ItemInfo[count].Position = j;
  17024. p.ItemInfo[count].ItemId = client_nameid( rd.nameid[i][j] );
  17025. p.ItemInfo[count].Count = rd.qty[i][j];
  17026. #if PACKETVER >= 20180511
  17027. p.ItemInfo[count].unused = 0;
  17028. #endif
  17029. count++;
  17030. }
  17031. }
  17032. clif_send( &p, sizeof(p), &sd->bl, SELF );
  17033. }
  17034. /// Request the roulette reward data
  17035. /// 0A1B (CZ_REQ_ROULETTE_INFO)
  17036. void clif_parse_roulette_info( int32 fd, map_session_data* sd ){
  17037. nullpo_retv(sd);
  17038. if (!battle_config.feature_roulette) {
  17039. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17040. return;
  17041. }
  17042. clif_roulette_info(sd);
  17043. }
  17044. /// Notification of the client that the roulette window was closed
  17045. /// 0A1D (CZ_REQ_CLOSE_ROULETTE)
  17046. void clif_parse_roulette_close( int32 fd, map_session_data* sd ){
  17047. nullpo_retv(sd);
  17048. if (!battle_config.feature_roulette) {
  17049. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17050. return;
  17051. }
  17052. sd->state.roulette_open = false;
  17053. }
  17054. /// Response to a item reward request
  17055. /// 0A22 <type>.B <bonus item>.W (ZC_RECV_ROULETTE_ITEM)
  17056. static void clif_roulette_recvitem_ack(map_session_data *sd, enum RECV_ROULETTE_ITEM_REQ type) {
  17057. #if PACKETVER >= 20141016
  17058. nullpo_retv(sd);
  17059. struct packet_roulette_itemrecv_ack p;
  17060. p.PacketType = roulettercvitemackType;
  17061. p.Result = type;
  17062. p.AdditionItemID = sd->roulette.bonusItemID;
  17063. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17064. #endif
  17065. }
  17066. /**
  17067. * Claim roulette reward
  17068. * @param sd Player
  17069. * @return 0:Success
  17070. **/
  17071. static uint8 clif_roulette_getitem(map_session_data *sd) {
  17072. struct item it;
  17073. uint8 res = 1;
  17074. uint16 factor = 1;
  17075. nullpo_retr(1, sd);
  17076. if (sd->roulette.prizeIdx < 0)
  17077. return RECV_ITEM_FAILED;
  17078. memset(&it, 0, sizeof(it));
  17079. it.nameid = rd.nameid[sd->roulette.prizeStage][sd->roulette.prizeIdx];
  17080. it.identify = 1;
  17081. if( sd->roulette.bonusItemID != 0 ){
  17082. if( sd->roulette.bonusItemID == it.nameid && battle_config.feature_roulette_bonus_reward && !( rd.flag[sd->roulette.prizeStage][sd->roulette.prizeIdx]&1 ) ){
  17083. factor = 2;
  17084. // Reset the Bonus Item to trigger new calculation
  17085. sd->roulette.bonusItemID = 0;
  17086. }else{
  17087. for( int32 i = 0; i < MAX_ROULETTE_LEVEL && sd->roulette.bonusItemID != 0; i++ ){
  17088. for( int32 j = 0; j < MAX_ROULETTE_COLUMNS - i && sd->roulette.bonusItemID != 0; j++ ){
  17089. if( rd.nameid[i][j] == sd->roulette.bonusItemID ){
  17090. // If you reached a equal or higher level than the first level with the bonus item in it, you missed your chance and a new bonus item will be calculated
  17091. if( sd->roulette.prizeStage >= i ){
  17092. // Reset the Bonus Item to trigger new calculation and to cancel the loops
  17093. sd->roulette.bonusItemID = 0;
  17094. }
  17095. }
  17096. }
  17097. }
  17098. }
  17099. }
  17100. if ((res = pc_additem(sd, &it, rd.qty[sd->roulette.prizeStage][sd->roulette.prizeIdx] * factor, LOG_TYPE_ROULETTE)) == 0) {
  17101. ; // onSuccess
  17102. }
  17103. if( sd->roulette.bonusItemID == 0 ){
  17104. roulette_generate_bonus( *sd );
  17105. }
  17106. sd->roulette.claimPrize = false;
  17107. sd->roulette.prizeStage = 0;
  17108. sd->roulette.prizeIdx = -1;
  17109. sd->roulette.stage = 0;
  17110. return res;
  17111. }
  17112. /// Update Roulette window with current stats
  17113. /// 0A20 <result>.B <stage>.W <price index>.W <bonus item>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_GENERATE_ROULETTE)
  17114. void clif_roulette_generate( map_session_data *sd, unsigned char result, int16 stage, int16 prizeIdx, t_itemid bonusItemID ){
  17115. nullpo_retv( sd );
  17116. struct packet_roulette_generate_ack p;
  17117. p.PacketType = roulettgenerateackType;
  17118. p.Result = result;
  17119. p.Step = stage;
  17120. p.Idx = prizeIdx;
  17121. p.AdditionItemID = battle_config.feature_roulette_bonus_reward ? bonusItemID : 0;
  17122. p.RemainGold = sd->roulette_point.gold;
  17123. p.RemainSilver = sd->roulette_point.silver;
  17124. p.RemainBronze = sd->roulette_point.bronze;
  17125. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17126. }
  17127. /// Request to start the roulette
  17128. /// 0A1F (CZ_REQ_GENERATE_ROULETTE)
  17129. void clif_parse_roulette_generate( int32 fd, map_session_data* sd ){
  17130. enum GENERATE_ROULETTE_ACK result;
  17131. nullpo_retv(sd);
  17132. if (!battle_config.feature_roulette) {
  17133. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17134. return;
  17135. }
  17136. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  17137. return;
  17138. }
  17139. // Player has not claimed his prize yet
  17140. if( sd->roulette.claimPrize ){
  17141. clif_roulette_getitem( sd );
  17142. }
  17143. if (sd->roulette.stage >= MAX_ROULETTE_LEVEL){
  17144. // Make sure everything is reset
  17145. sd->roulette.stage = 0;
  17146. sd->roulette.claimPrize = false;
  17147. sd->roulette.prizeStage = 0;
  17148. sd->roulette.prizeIdx = -1;
  17149. }
  17150. if( !sd->roulette.stage && sd->roulette_point.bronze <= 0 && sd->roulette_point.silver < 10 && sd->roulette_point.gold < 10 ){
  17151. result = GENERATE_ROULETTE_NO_ENOUGH_POINT;
  17152. }else{
  17153. if (!sd->roulette.stage) {
  17154. if (sd->roulette_point.bronze > 0) {
  17155. sd->roulette_point.bronze -= 1;
  17156. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  17157. } else if (sd->roulette_point.silver > 9) {
  17158. sd->roulette_point.silver -= 10;
  17159. sd->roulette.stage = 2;
  17160. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  17161. } else if (sd->roulette_point.gold > 9) {
  17162. sd->roulette_point.gold -= 10;
  17163. sd->roulette.stage = 4;
  17164. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  17165. }
  17166. }
  17167. sd->roulette.prizeStage = sd->roulette.stage;
  17168. sd->roulette.prizeIdx = rnd()%rd.items[sd->roulette.stage];
  17169. sd->roulette.claimPrize = true;
  17170. sd->roulette.tick = gettick() + i64max( 1, ( MAX_ROULETTE_COLUMNS - sd->roulette.prizeStage - 3 ) ) * 1000;
  17171. if( rd.flag[sd->roulette.stage][sd->roulette.prizeIdx]&1 ){
  17172. result = GENERATE_ROULETTE_LOSING;
  17173. sd->roulette.stage = 0;
  17174. }else{
  17175. result = GENERATE_ROULETTE_SUCCESS;
  17176. sd->roulette.stage++;
  17177. }
  17178. }
  17179. clif_roulette_generate(sd,result,sd->roulette.prizeStage,(sd->roulette.prizeIdx == -1 ? 0 : sd->roulette.prizeIdx), sd->roulette.bonusItemID);
  17180. }
  17181. /// Request to claim a prize
  17182. /// 0A21 (CZ_RECV_ROULETTE_ITEM)
  17183. void clif_parse_roulette_item( int32 fd, map_session_data* sd ){
  17184. enum RECV_ROULETTE_ITEM_REQ type = RECV_ITEM_FAILED;
  17185. nullpo_retv(sd);
  17186. if (!battle_config.feature_roulette) {
  17187. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17188. return;
  17189. }
  17190. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  17191. return;
  17192. }
  17193. if (sd->roulette.claimPrize && sd->roulette.prizeIdx != -1) {
  17194. switch (clif_roulette_getitem(sd)) {
  17195. case ADDITEM_SUCCESS:
  17196. type = RECV_ITEM_SUCCESS;
  17197. break;
  17198. case ADDITEM_INVALID:
  17199. case ADDITEM_OVERITEM:
  17200. case ADDITEM_OVERAMOUNT:
  17201. type = RECV_ITEM_OVERCOUNT;
  17202. break;
  17203. case ADDITEM_OVERWEIGHT:
  17204. type = RECV_ITEM_OVERWEIGHT;
  17205. break;
  17206. case ADDITEM_STACKLIMIT:
  17207. default:
  17208. type = RECV_ITEM_FAILED;
  17209. break;
  17210. }
  17211. }
  17212. clif_roulette_recvitem_ack(sd,type);
  17213. }
  17214. /// MERGE ITEM
  17215. /**
  17216. * Acknowledge the client about item merger result
  17217. * ZC 096F <index>.W <total>.W <result>.B (ZC_ACK_MERGE_ITEM)
  17218. * @param fd
  17219. * @param sd
  17220. **/
  17221. void clif_merge_item_ack( map_session_data &sd, enum MERGE_ITEM_ACK type, uint16 index = 0, uint16 count = 0 ){
  17222. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17223. PACKET_ZC_ACK_MERGE_ITEM p = {};
  17224. p.packetType = HEADER_ZC_ACK_MERGE_ITEM;
  17225. if( type == MERGE_ITEM_SUCCESS ){
  17226. p.index = client_index( index );
  17227. }else{
  17228. p.index = index;
  17229. }
  17230. p.amount = count;
  17231. p.reason = type;
  17232. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17233. #endif
  17234. }
  17235. /**
  17236. * Check if item has the pair to be merged
  17237. * @param sd
  17238. * @param it Item
  17239. * @return True if has pair, False if not
  17240. **/
  17241. static bool clif_merge_item_has_pair(map_session_data *sd, struct item *it) {
  17242. struct item *it_;
  17243. uint16 i;
  17244. nullpo_retr(false, sd);
  17245. ARR_FIND(0, MAX_INVENTORY, i, (it_ = &sd->inventory.u.items_inventory[i]) && it->nameid == it_->nameid && it->bound == it_->bound && memcmp(it_, it, sizeof(struct item)) != 0);
  17246. if (i < MAX_INVENTORY)
  17247. return true;
  17248. return false;
  17249. }
  17250. /**
  17251. * Check if item can be merged
  17252. * @param id Item Data
  17253. * @param it Item
  17254. * @return True if can be merged, False if not
  17255. **/
  17256. static bool clif_merge_item_check(struct item_data *id, struct item *it) {
  17257. if (!id || !it)
  17258. return false;
  17259. if (id->type == IT_CASH)
  17260. return false;
  17261. if (!itemdb_isstackable2(id))
  17262. return false;
  17263. if (itemdb_isspecial(it->card[0]))
  17264. return false;
  17265. if (!it->unique_id)
  17266. return false;
  17267. return true;
  17268. }
  17269. /**
  17270. * Open available item to be merged.
  17271. * Only show 1 item in 1 process.
  17272. * ZC 096D <size>.W { <index>.W }* (ZC_MERGE_ITEM_OPEN)
  17273. * @param sd
  17274. **/
  17275. void clif_merge_item_open( map_session_data& sd ){
  17276. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17277. PACKET_ZC_MERGE_ITEM_OPEN* p = reinterpret_cast<PACKET_ZC_MERGE_ITEM_OPEN*>( packet_buffer );
  17278. p->packetType = HEADER_ZC_MERGE_ITEM_OPEN;
  17279. p->packetLen = sizeof( *p );
  17280. int32 n = 0;
  17281. // Get entries
  17282. for( int32 i = 0; i < MAX_INVENTORY; i++ ){
  17283. struct item* it = &sd.inventory.u.items_inventory[i];
  17284. if( !clif_merge_item_check( sd.inventory_data[i], it ) ){
  17285. continue;
  17286. }
  17287. if( clif_merge_item_has_pair( &sd, it ) ){
  17288. p->items[n++].index = client_index( i );
  17289. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( p->items[0] ) );
  17290. }
  17291. }
  17292. // No item need to be merged
  17293. if( n < 2 ){
  17294. clif_msg( &sd, MSI_NOT_EXIST_MERGE_ITEM );
  17295. return;
  17296. }
  17297. clif_send( p, p->packetLen, &sd.bl, SELF );
  17298. #endif
  17299. }
  17300. /**
  17301. * Process item merge request.
  17302. * 096E <size>.W { <index>.W }* (CZ_REQ_MERGE_ITEM)
  17303. * @param fd
  17304. * @param sd
  17305. **/
  17306. void clif_parse_merge_item_req( int32 fd, map_session_data* sd ){
  17307. const PACKET_CZ_REQ_MERGE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_MERGE_ITEM*>( RFIFOP( fd, 0 ) );
  17308. int32 count = ( p->packetLength - sizeof( *p ) ) / sizeof( p->indices[0] );
  17309. // No item need to be merged
  17310. if( count < 2 ){
  17311. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17312. return;
  17313. }
  17314. uint16 idx_main = server_index( p->indices[0] );
  17315. if( idx_main >= MAX_INVENTORY ){
  17316. return;
  17317. }
  17318. if( !clif_merge_item_check( sd->inventory_data[idx_main], &sd->inventory.u.items_inventory[idx_main] ) ){
  17319. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17320. return;
  17321. }
  17322. // Ensure each index only comes once
  17323. std::unordered_set<uint16> indices;
  17324. for( int32 i = 1; i < count; i++ ){
  17325. uint16 idx = server_index( p->indices[i] );
  17326. if( idx >= MAX_INVENTORY ){
  17327. return;
  17328. }
  17329. if( sd->inventory_data[idx] == nullptr ){
  17330. return;
  17331. }
  17332. // Check if it is the same item
  17333. if( sd->inventory_data[idx]->nameid != sd->inventory_data[idx_main]->nameid ){
  17334. return;
  17335. }
  17336. if( !clif_merge_item_check( sd->inventory_data[idx], &sd->inventory.u.items_inventory[idx] ) ){
  17337. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17338. return;
  17339. }
  17340. indices.insert( idx );
  17341. }
  17342. if( indices.empty() ){
  17343. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17344. return;
  17345. }
  17346. uint32 total_amount = sd->inventory.u.items_inventory[idx_main].amount;
  17347. for( uint16 idx : indices ){
  17348. total_amount += sd->inventory.u.items_inventory[idx].amount;
  17349. }
  17350. uint16 stack = sd->inventory_data[idx_main]->stack.amount;
  17351. if( stack == 0 ){
  17352. stack = MAX_AMOUNT;
  17353. }
  17354. if( total_amount >= stack ){
  17355. clif_merge_item_ack( *sd, MERGE_ITEM_FAILED_MAX_COUNT );
  17356. return;
  17357. }
  17358. // Merrrrge!!!!
  17359. for( uint16 idx : indices ){
  17360. uint16 amount = sd->inventory.u.items_inventory[idx].amount;
  17361. log_pick_pc( sd, LOG_TYPE_MERGE_ITEM, -amount, &sd->inventory.u.items_inventory[idx] );
  17362. memset( &sd->inventory.u.items_inventory[idx], 0, sizeof( sd->inventory.u.items_inventory[0] ) );
  17363. sd->inventory_data[idx] = nullptr;
  17364. clif_delitem( *sd, idx, amount, 0 );
  17365. }
  17366. sd->inventory.u.items_inventory[idx_main].amount = total_amount;
  17367. clif_merge_item_ack( *sd, MERGE_ITEM_SUCCESS, idx_main, total_amount );
  17368. }
  17369. /**
  17370. * Cancel item merge
  17371. * CZ 0974 (CZ_CANCEL_MERGE_ITEM)
  17372. * @param fd
  17373. * @param sd
  17374. **/
  17375. void clif_parse_merge_item_cancel(int32 fd, map_session_data* sd) {
  17376. return; // Nothing todo yet
  17377. }
  17378. static std::string clif_hide_name(const char* original_name)
  17379. {
  17380. std::string censored(original_name);
  17381. size_t hide = zmin(battle_config.broadcast_hide_name, censored.length() - 1);
  17382. censored.replace(censored.length() - hide, hide, hide, '*');
  17383. return censored;
  17384. }
  17385. /**
  17386. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <containerid>.W (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17387. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <srcname>.24B (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17388. * type: ITEMOBTAIN_TYPE_BOXITEM & ITEMOBTAIN_TYPE_MONSTER_ITEM "[playername] ... [sourcename] ... [itemname]" -> MsgStringTable[1629]
  17389. * type: ITEMOBTAIN_TYPE_NPC "[playername] ... [itemname]" -> MsgStringTable[1870]
  17390. **/
  17391. void clif_broadcast_obtain_special_item( const char *char_name, t_itemid nameid, t_itemid container, enum BROADCASTING_SPECIAL_ITEM_OBTAIN type ){
  17392. char name[NAME_LENGTH];
  17393. if( battle_config.broadcast_hide_name ){
  17394. std::string dispname = clif_hide_name(char_name);
  17395. safestrncpy(name, dispname.c_str(), sizeof(name));
  17396. }else{
  17397. safestrncpy( name, char_name, sizeof( name ) );
  17398. }
  17399. switch( type ){
  17400. case ITEMOBTAIN_TYPE_BOXITEM:
  17401. #if PACKETVER >= 20091201
  17402. {
  17403. PACKET_ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN_item p = {};
  17404. p.PacketType = package_item_announceType;
  17405. p.PacketLength = sizeof( p );
  17406. p.type = type;
  17407. p.ItemID = client_nameid( nameid );
  17408. p.len = NAME_LENGTH;
  17409. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17410. p.boxItemID_len = (int8)sizeof( p.BoxItemID );
  17411. p.BoxItemID = client_nameid( container );
  17412. #if PACKETVER_MAIN_NUM >= 20220518 || PACKETVER_ZERO_NUM >= 20220518
  17413. p.refineLevel_len = (int8)sizeof( p.refineLevel );
  17414. p.refineLevel = 0; // TODO: implement
  17415. #endif
  17416. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17417. }
  17418. #endif
  17419. break;
  17420. case ITEMOBTAIN_TYPE_MONSTER_ITEM:
  17421. case ITEMOBTAIN_TYPE_NPC:
  17422. {
  17423. struct packet_item_drop_announce p;
  17424. p.PacketType = item_drop_announceType;
  17425. p.type = type;
  17426. p.ItemID = client_nameid( nameid );
  17427. p.len = NAME_LENGTH;
  17428. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17429. if( type == ITEMOBTAIN_TYPE_MONSTER_ITEM ){
  17430. std::shared_ptr<s_mob_db> db = mob_db.find( container );
  17431. p.monsterNameLen = NAME_LENGTH;
  17432. safestrncpy( p.monsterName, db->name.c_str(), NAME_LENGTH );
  17433. }else{
  17434. p.monsterNameLen = 0;
  17435. p.monsterName[0] = '\0';
  17436. }
  17437. #if PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  17438. p.PacketLength = 12 + p.len + p.monsterNameLen;
  17439. #else
  17440. p.PacketLength = 8 + p.len + p.monsterNameLen;
  17441. #endif
  17442. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17443. }
  17444. break;
  17445. }
  17446. }
  17447. /// Open the dress room window.
  17448. /// 0A02 <view>.W (ZC_DRESSROOM_OPEN)
  17449. void clif_dressing_room( map_session_data& sd ){
  17450. #if PACKETVER >= 20140212
  17451. PACKET_ZC_DRESSROOM_OPEN p = {};
  17452. p.packetType = HEADER_ZC_DRESSROOM_OPEN;
  17453. p.view = 0; // Ignored
  17454. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17455. #endif
  17456. }
  17457. /// Parsing a request from the client item identify oneclick (CZ_REQ_ONECLICK_ITEMIDENTIFY).
  17458. /// 0A35 <result>.W
  17459. void clif_parse_Oneclick_Itemidentify(int32 fd, map_session_data *sd) {
  17460. #if PACKETVER >= 20150513
  17461. int16 idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2, magnifier_idx;
  17462. // Ignore the request
  17463. // - Invalid item index
  17464. // - Invalid item ID or item doesn't exist
  17465. // - Item is already identified
  17466. if (idx < 0 || idx >= MAX_INVENTORY ||
  17467. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  17468. sd->inventory.u.items_inventory[idx].identify)
  17469. return;
  17470. // Ignore the request - No magnifiers in inventory
  17471. if ((magnifier_idx = pc_search_inventory(sd, ITEMID_MAGNIFIER)) == -1 &&
  17472. (magnifier_idx = pc_search_inventory(sd, ITEMID_NOVICE_MAGNIFIER)) == -1)
  17473. return;
  17474. if (pc_delitem(sd, magnifier_idx, 1, 0, 0, LOG_TYPE_OTHER) != 0) // Deleting of magnifier failed, for whatever reason...
  17475. return;
  17476. skill_identify(sd, idx);
  17477. #endif
  17478. }
  17479. /// Starts navigation to the given target on client side
  17480. void clif_navigateTo(map_session_data *sd, const char* mapname, uint16 x, uint16 y, uint8 flag, bool hideWindow, uint16 mob_id ){
  17481. #if PACKETVER >= 20111010
  17482. int32 fd = sd->fd;
  17483. WFIFOHEAD(fd,27);
  17484. WFIFOW(fd,0) = 0x08e2;
  17485. // How detailed will our navigation be?
  17486. if( mob_id > 0 ){
  17487. x = 0;
  17488. y = 0;
  17489. WFIFOB(fd,2) = 3; // monster with destination field
  17490. }else if( x > 0 && y > 0 ){
  17491. WFIFOB(fd,2) = 0; // with coordinates
  17492. }else{
  17493. x = 0;
  17494. y = 0;
  17495. WFIFOB(fd,2) = 1; // without coordinates(will fail if you are already on the map)
  17496. }
  17497. // Which services can be used for transportation?
  17498. WFIFOB(fd,3) = flag;
  17499. // If this flag is set, the navigation window will not be opened up
  17500. WFIFOB(fd,4) = hideWindow;
  17501. // Target map
  17502. safestrncpy( WFIFOCP(fd,5),mapname,MAP_NAME_LENGTH_EXT);
  17503. // Target x
  17504. WFIFOW(fd,21) = x;
  17505. // Target y
  17506. WFIFOW(fd,23) = y;
  17507. // Target monster ID
  17508. WFIFOW(fd,25) = mob_id;
  17509. WFIFOSET(fd,27);
  17510. #endif
  17511. }
  17512. /// Send hat effects to the client.
  17513. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17514. void clif_hat_effects( map_session_data& sd, block_list& bl, enum send_target target ){
  17515. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17516. map_session_data *tsd;
  17517. block_list* tbl;
  17518. if( target == SELF ){
  17519. tsd = BL_CAST(BL_PC,&bl);
  17520. tbl = &sd.bl;
  17521. }else{
  17522. tsd = &sd;
  17523. tbl = &bl;
  17524. }
  17525. if( tsd == nullptr ){
  17526. return;
  17527. }
  17528. if( tsd->hatEffects.empty() || map_getmapdata(tbl->m)->getMapFlag(MF_NOCOSTUME) ){
  17529. return;
  17530. }
  17531. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17532. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17533. p->packetLength = sizeof( *p );
  17534. p->aid = tsd->bl.id;
  17535. p->status = 1;
  17536. for( size_t i = 0; i < tsd->hatEffects.size(); i++ ){
  17537. p->effects[i] = tsd->hatEffects[i];
  17538. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17539. }
  17540. clif_send( p, p->packetLength, tbl, target );
  17541. #endif
  17542. }
  17543. /// Send a single hat effect to the client.
  17544. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17545. void clif_hat_effect_single( map_session_data& sd, uint16 effectId, bool enable ){
  17546. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17547. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17548. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17549. p->packetLength = sizeof( *p );
  17550. p->aid = sd.bl.id;
  17551. p->status = enable;
  17552. p->effects[0] = effectId;
  17553. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17554. clif_send( p, p->packetLength, &sd.bl, AREA );
  17555. #endif
  17556. }
  17557. /// Notify the client that a sale has started
  17558. /// 09b2 <item id>.W <remaining time>.L (ZC_NOTIFY_BARGAIN_SALE_SELLING)
  17559. void clif_sale_start( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17560. #if PACKETVER_SUPPORTS_SALES
  17561. PACKET_ZC_NOTIFY_BARGAIN_SALE_SELLING p = {};
  17562. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_SELLING;
  17563. p.itemId = client_nameid( sale_item->nameid );
  17564. p.remainingTime = (uint32)(sale_item->end - time(nullptr)); // time in S
  17565. clif_send( &p, sizeof( p ), bl, target );
  17566. #endif
  17567. }
  17568. /// Notify the client that a sale has ended
  17569. /// 09b3 <item id>.W (ZC_NOTIFY_BARGAIN_SALE_CLOSE)
  17570. void clif_sale_end( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17571. #if PACKETVER_SUPPORTS_SALES
  17572. PACKET_ZC_NOTIFY_BARGAIN_SALE_CLOSE p = {};
  17573. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_CLOSE;
  17574. p.itemId = client_nameid( sale_item->nameid );
  17575. clif_send( &p, sizeof( p ), bl, target );
  17576. #endif
  17577. }
  17578. /// Update the remaining amount of a sale item.
  17579. /// 09c4 <item id>.W <amount>.L (ZC_ACK_COUNT_BARGAIN_SALE_ITEM)
  17580. void clif_sale_amount( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17581. #if PACKETVER_SUPPORTS_SALES
  17582. PACKET_ZC_ACK_COUNT_BARGAIN_SALE_ITEM p = {};
  17583. p.packetType = HEADER_ZC_ACK_COUNT_BARGAIN_SALE_ITEM;
  17584. p.itemId = client_nameid( sale_item->nameid );
  17585. p.amount = sale_item->amount;
  17586. clif_send( &p, sizeof( p ), bl, target );
  17587. #endif
  17588. }
  17589. /// The client requested a refresh of the current remaining count of a sale item
  17590. /// 09ac <account id>.L <item id>.W (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17591. void clif_parse_sale_refresh( int32 fd, map_session_data* sd ){
  17592. #if PACKETVER_SUPPORTS_SALES
  17593. const PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO* p = reinterpret_cast<PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO*>( RFIFOP( fd, 0 ) );
  17594. struct sale_item_data* sale;
  17595. if( p->AID != sd->status.account_id ){
  17596. return;
  17597. }
  17598. sale = sale_find_item( p->itemId, true );
  17599. if( sale == nullptr ){
  17600. return;
  17601. }
  17602. clif_sale_amount(sale, &sd->bl, SELF);
  17603. #endif
  17604. }
  17605. /// Opens the sale administration window on the client
  17606. /// 09b5 (ZC_OPEN_BARGAIN_SALE_TOOL)
  17607. void clif_sale_open( map_session_data* sd ){
  17608. #if PACKETVER_SUPPORTS_SALES
  17609. nullpo_retv(sd);
  17610. if( sd->state.sale_open ){
  17611. return;
  17612. }
  17613. sd->state.sale_open = true;
  17614. int32 fd = sd->fd;
  17615. WFIFOHEAD(fd, 2);
  17616. WFIFOW(fd, 0) = 0x9b5;
  17617. WFIFOSET(fd, 2);
  17618. #endif
  17619. }
  17620. /// Client request to open the sale administration window.
  17621. /// This is sent by /limitedsale
  17622. /// 09b4 <account id>.L (CZ_OPEN_BARGAIN_SALE_TOOL)
  17623. void clif_parse_sale_open( int32 fd, map_session_data* sd ){
  17624. #if PACKETVER_SUPPORTS_SALES
  17625. nullpo_retv(sd);
  17626. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17627. return;
  17628. }
  17629. char command[CHAT_SIZE_MAX];
  17630. safesnprintf( command, sizeof(command), "%climitedsale", atcommand_symbol );
  17631. is_atcommand(fd, sd, command, 1);
  17632. #endif
  17633. }
  17634. /// Closes the sale administration window on the client.
  17635. /// 09bd (ZC_CLOSE_BARGAIN_SALE_TOOL)
  17636. void clif_sale_close(map_session_data* sd) {
  17637. #if PACKETVER_SUPPORTS_SALES
  17638. nullpo_retv(sd);
  17639. if( !sd->state.sale_open ){
  17640. return;
  17641. }
  17642. sd->state.sale_open = false;
  17643. int32 fd = sd->fd;
  17644. WFIFOHEAD(fd, 2);
  17645. WFIFOW(fd, 0) = 0x9bd;
  17646. WFIFOSET(fd, 2);
  17647. #endif
  17648. }
  17649. /// Client request to close the sale administration window.
  17650. /// 09bc (CZ_CLOSE_BARGAIN_SALE_TOOL)
  17651. void clif_parse_sale_close(int32 fd, map_session_data* sd) {
  17652. #if PACKETVER_SUPPORTS_SALES
  17653. nullpo_retv(sd);
  17654. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17655. return;
  17656. }
  17657. clif_sale_close(sd);
  17658. #endif
  17659. }
  17660. /// Reply to a item search request for item sale administration.
  17661. /// 09ad <result>.W <item id>.W <price>.L (ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO)
  17662. void clif_sale_search_reply( map_session_data* sd, std::shared_ptr<s_cash_item> item ){
  17663. #if PACKETVER_SUPPORTS_SALES
  17664. PACKET_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO p = {};
  17665. p.packetType = HEADER_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO;
  17666. if( item != nullptr ){
  17667. p.result = 0;
  17668. p.itemId = client_nameid( item->nameid );
  17669. p.price = item->price;
  17670. }else{
  17671. p.result = 1;
  17672. p.itemId = 0;
  17673. p.price = 0;
  17674. }
  17675. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17676. #endif
  17677. }
  17678. /// Search request for an item sale administration.
  17679. /// 09ac <length>.W <account id>.L <item name>.?B (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17680. void clif_parse_sale_search( int32 fd, map_session_data* sd ){
  17681. #if PACKETVER_SUPPORTS_SALES
  17682. char item_name[ITEM_NAME_LENGTH];
  17683. nullpo_retv(sd);
  17684. if( RFIFOL(fd, 4) != sd->status.account_id ){
  17685. return;
  17686. }
  17687. if( !sd->state.sale_open ){
  17688. return;
  17689. }
  17690. safestrncpy( item_name, RFIFOCP(fd, 8), min(RFIFOW(fd, 2) - 7, ITEM_NAME_LENGTH) );
  17691. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  17692. // not found
  17693. if( id == nullptr ){
  17694. clif_sale_search_reply( sd, nullptr );
  17695. return;
  17696. }
  17697. clif_sale_search_reply( sd, cash_shop_db.findItemInTab( CASHSHOP_TAB_SALE, id->nameid ) );
  17698. #endif
  17699. }
  17700. /// Reply if an item was successfully put on sale or not.
  17701. /// 09af <result>.W (ZC_ACK_APPLY_BARGAIN_SALE_ITEM)
  17702. void clif_sale_add_reply( map_session_data* sd, enum e_sale_add_result result ){
  17703. #if PACKETVER_SUPPORTS_SALES
  17704. int32 fd = sd->fd;
  17705. WFIFOHEAD(fd, 4);
  17706. WFIFOW(fd, 0) = 0x9af;
  17707. WFIFOW(fd, 2) = (uint16)result;
  17708. WFIFOSET(fd, 4);
  17709. #endif
  17710. }
  17711. /// A client request to put an item on sale.
  17712. /// 09ae <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.B (CZ_REQ_APPLY_BARGAIN_SALE_ITEM)
  17713. /// 0a3d <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.W (CZ_REQ_APPLY_BARGAIN_SALE_ITEM2)
  17714. void clif_parse_sale_add( int32 fd, map_session_data* sd ){
  17715. #if PACKETVER_SUPPORTS_SALES
  17716. nullpo_retv( sd );
  17717. const PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17718. if( p->AID != sd->status.account_id ){
  17719. return;
  17720. }
  17721. if( !sd->state.sale_open ){
  17722. return;
  17723. }
  17724. time_t endTime = p->startTime + p->hours * 60 * 60;
  17725. clif_sale_add_reply( sd, sale_add_item( p->itemId, p->amount, p->startTime, endTime ) );
  17726. #endif
  17727. }
  17728. /// Reply to an item removal from sale.
  17729. /// 09b1 <result>.W (ZC_ACK_REMOVE_BARGAIN_SALE_ITEM)
  17730. void clif_sale_remove_reply( map_session_data* sd, bool failed ){
  17731. #if PACKETVER_SUPPORTS_SALES
  17732. int32 fd = sd->fd;
  17733. WFIFOHEAD(fd, 4);
  17734. WFIFOW(fd, 0) = 0x9b1;
  17735. WFIFOW(fd, 2) = failed;
  17736. WFIFOSET(fd, 4);
  17737. #endif
  17738. }
  17739. /// Request to remove an item from sale.
  17740. /// 09b0 <account id>.L <item id>.W (CZ_REQ_REMOVE_BARGAIN_SALE_ITEM)
  17741. void clif_parse_sale_remove( int32 fd, map_session_data* sd ){
  17742. #if PACKETVER_SUPPORTS_SALES
  17743. nullpo_retv( sd );
  17744. const PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17745. if( p->AID != sd->status.account_id ){
  17746. return;
  17747. }
  17748. if( !sd->state.sale_open ){
  17749. return;
  17750. }
  17751. clif_sale_remove_reply( sd, !sale_remove_item( p->itemId ) );
  17752. #endif
  17753. }
  17754. /// Achievement System
  17755. /// Author: Luxuri, Aleos
  17756. /**
  17757. * Sends all achievement data to the client (ZC_ALL_ACH_LIST).
  17758. * 0a23 <packetLength>.W <ACHCount>.L <ACHPoint>.L
  17759. */
  17760. void clif_achievement_list_all(map_session_data *sd)
  17761. {
  17762. nullpo_retv(sd);
  17763. if (!battle_config.feature_achievement) {
  17764. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17765. return;
  17766. }
  17767. int32 count = sd->achievement_data.count; // All achievements should be sent to the client
  17768. if (count == 0)
  17769. return;
  17770. int32 len = (50 * count) + 22;
  17771. int32 fd = sd->fd;
  17772. int32 *info = achievement_level(sd, true);
  17773. WFIFOHEAD(fd,len);
  17774. WFIFOW(fd, 0) = 0xa23;
  17775. WFIFOW(fd, 2) = len;
  17776. WFIFOL(fd, 4) = count; // Amount of achievements the player has in their list (started/completed)
  17777. WFIFOL(fd, 8) = sd->achievement_data.total_score; // Top number
  17778. WFIFOW(fd, 12) = sd->achievement_data.level; // Achievement Level (gold circle)
  17779. WFIFOL(fd, 14) = info[0]; // Achievement EXP (left number in bar)
  17780. WFIFOL(fd, 18) = info[1]; // Achievement EXP TNL (right number in bar)
  17781. for (int32 i = 0; i < count; i++) {
  17782. WFIFOL(fd, i * 50 + 22) = (uint32)sd->achievement_data.achievements[i].achievement_id;
  17783. WFIFOB(fd, i * 50 + 26) = (uint32)sd->achievement_data.achievements[i].completed > 0;
  17784. for (int32 j = 0; j < MAX_ACHIEVEMENT_OBJECTIVES; j++)
  17785. WFIFOL(fd, (i * 50) + 27 + (j * 4)) = (uint32)sd->achievement_data.achievements[i].count[j];
  17786. WFIFOL(fd, i * 50 + 67) = (uint32)sd->achievement_data.achievements[i].completed;
  17787. WFIFOB(fd, i * 50 + 71) = sd->achievement_data.achievements[i].rewarded > 0;
  17788. }
  17789. WFIFOSET(fd, len);
  17790. }
  17791. /**
  17792. * Sends a single achievement's data to the client (ZC_ACH_UPDATE).
  17793. * 0a24 <ACHPoint>.L
  17794. */
  17795. void clif_achievement_update(map_session_data *sd, struct achievement *ach, int32 count)
  17796. {
  17797. nullpo_retv(sd);
  17798. if (!battle_config.feature_achievement) {
  17799. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17800. return;
  17801. }
  17802. int32 fd = sd->fd;
  17803. int32 *info = achievement_level(sd, true);
  17804. WFIFOHEAD(fd, packet_len(0xa24));
  17805. WFIFOW(fd, 0) = 0xa24;
  17806. WFIFOL(fd, 2) = sd->achievement_data.total_score; // Total Achievement Points (top of screen)
  17807. WFIFOW(fd, 6) = sd->achievement_data.level; // Achievement Level (gold circle)
  17808. WFIFOL(fd, 8) = info[0]; // Achievement EXP (left number in bar)
  17809. WFIFOL(fd, 12) = info[1]; // Achievement EXP TNL (right number in bar)
  17810. if (ach) {
  17811. WFIFOL(fd, 16) = ach->achievement_id; // Achievement ID
  17812. WFIFOB(fd, 20) = ach->completed > 0; // Is it complete?
  17813. for (int32 i = 0; i < MAX_ACHIEVEMENT_OBJECTIVES; i++)
  17814. WFIFOL(fd, 21 + (i * 4)) = (uint32)ach->count[i]; // 1~10 pre-reqs
  17815. WFIFOL(fd, 61) = (uint32)ach->completed; // Epoch time
  17816. WFIFOB(fd, 65) = ach->rewarded > 0; // Got reward?
  17817. } else
  17818. memset(WFIFOP(fd, 16), 0, 40);
  17819. WFIFOSET(fd, packet_len(0xa24));
  17820. }
  17821. /**
  17822. * Checks if an achievement reward can be rewarded (CZ_REQ_ACH_REWARD).
  17823. * 0a25 <achievementID>.L
  17824. */
  17825. void clif_parse_AchievementCheckReward(int32 fd, map_session_data *sd)
  17826. {
  17827. nullpo_retv(sd);
  17828. if( sd->achievement_data.save )
  17829. intif_achievement_save(sd);
  17830. achievement_check_reward(sd, RFIFOL(fd,2));
  17831. }
  17832. /**
  17833. * Returns the result of achievement_check_reward (ZC_REQ_ACH_REWARD_ACK).
  17834. * 0a26 <result>.W <achievementID>.L
  17835. */
  17836. void clif_achievement_reward_ack(int32 fd, unsigned char result, int32 achievement_id)
  17837. {
  17838. WFIFOHEAD(fd, packet_len(0xa26));
  17839. WFIFOW(fd, 0) = 0xa26;
  17840. WFIFOB(fd, 2) = result;
  17841. WFIFOL(fd, 3) = achievement_id;
  17842. WFIFOSET(fd, packet_len(0xa26));
  17843. }
  17844. /// Process the pet evolution request
  17845. /// 09fb <packetType>.W <packetLength>.W <evolutionPetEggITID>.W (CZ_PET_EVOLUTION)
  17846. void clif_parse_pet_evolution( int32 fd, map_session_data *sd ){
  17847. #if PACKETVER >= 20141008
  17848. const PACKET_CZ_PET_EVOLUTION* p = reinterpret_cast<PACKET_CZ_PET_EVOLUTION*>( RFIFOP( fd, 0 ) );
  17849. auto pet = pet_db_search( p->EvolvedPetEggID, PET_EGG );
  17850. if (!pet) {
  17851. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_NOT_PETEGG);
  17852. return;
  17853. }
  17854. pet_evolution(sd, pet->class_);
  17855. #endif
  17856. }
  17857. /// Sends the result of the evolution to the client.
  17858. /// 09fc <result>.L (ZC_PET_EVOLUTION_RESULT)
  17859. void clif_pet_evolution_result( map_session_data* sd, e_pet_evolution_result result ){
  17860. #if PACKETVER >= 20141008
  17861. int32 fd = sd->fd;
  17862. WFIFOHEAD(fd, packet_len(0x9fc));
  17863. WFIFOW(fd, 0) = 0x9fc;
  17864. WFIFOL(fd, 2) = static_cast<uint32>(result);
  17865. WFIFOSET(fd, packet_len(0x9fc));
  17866. #endif
  17867. }
  17868. /*
  17869. * This packet is sent by /changedress or /nocosplay
  17870. *
  17871. * 0ae8
  17872. */
  17873. void clif_parse_changedress( int32 fd, map_session_data* sd ){
  17874. #if PACKETVER >= 20180103
  17875. char command[CHAT_SIZE_MAX];
  17876. safesnprintf( command, sizeof(command), "%cchangedress", atcommand_symbol );
  17877. is_atcommand( fd, sd, command, 1 );
  17878. #endif
  17879. }
  17880. /// Opens an UI window of the given type and initializes it with the given data
  17881. /// 0AE2 <type>.B <data>.L
  17882. void clif_ui_open( map_session_data& sd, enum out_ui_type ui_type, int32 data ){
  17883. #if PACKETVER >= 20151202
  17884. // If the UI requires state tracking
  17885. switch( ui_type ){
  17886. case OUT_UI_STYLIST:
  17887. sd.state.stylist_open = true;
  17888. break;
  17889. case OUT_UI_ENCHANTGRADE:
  17890. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  17891. sd.state.enchantgrade_open = true;
  17892. break;
  17893. #else
  17894. return;
  17895. #endif
  17896. }
  17897. PACKET_ZC_UI_OPEN p = {};
  17898. p.PacketType = HEADER_ZC_UI_OPEN;
  17899. p.UIType = ui_type;
  17900. #if PACKETVER >= 20171122
  17901. p.data = data;
  17902. #endif
  17903. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17904. #endif
  17905. }
  17906. /// Request to open an UI window of the given type
  17907. /// 0A68 <type>.B
  17908. void clif_parse_open_ui( int32 fd, map_session_data* sd ){
  17909. switch( RFIFOB(fd,2) ){
  17910. case IN_UI_ATTENDANCE:
  17911. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  17912. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 791 ), false, SELF ); // You are not allowed to use the attendance system.
  17913. }else if( pc_attendance_enabled() ){
  17914. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  17915. }else{
  17916. clif_msg_color( sd, MSI_CHECK_ATTENDANCE_NOT_EVENT, color_table[COLOR_RED] );
  17917. }
  17918. break;
  17919. #if PACKETVER >= 20160316
  17920. case IN_UI_MACRO_REGISTER:
  17921. clif_ui_open(*sd, OUT_UI_CAPTCHA, 0);
  17922. break;
  17923. case IN_UI_MACRO_DETECTOR:
  17924. clif_ui_open(*sd, OUT_UI_MACRO, 0);
  17925. break;
  17926. #endif
  17927. }
  17928. }
  17929. /// Response for attedance request
  17930. /// 0AF0 <unknown>.L <data>.L
  17931. void clif_attendence_response( map_session_data *sd, int32 data ){
  17932. nullpo_retv(sd);
  17933. int32 fd = sd->fd;
  17934. WFIFOHEAD(fd,packet_len(0xAF0));
  17935. WFIFOW(fd,0) = 0xAF0;
  17936. WFIFOL(fd,2) = 0;
  17937. WFIFOL(fd,6) = data;
  17938. WFIFOSET(fd,packet_len(0xAF0));
  17939. }
  17940. /// Request from the client to retrieve today's attendance reward
  17941. /// 0AEF
  17942. void clif_parse_attendance_request( int32 fd, map_session_data* sd ){
  17943. pc_attendance_claim_reward(sd);
  17944. }
  17945. /// Send out the percentage of weight that causes it to be displayed in red.
  17946. /// 0ADE <percentage>.L
  17947. void clif_weight_limit( map_session_data* sd ){
  17948. #if PACKETVER >= 20171025
  17949. nullpo_retv(sd);
  17950. int32 fd = sd->fd;
  17951. WFIFOHEAD(fd, packet_len(0xADE));
  17952. WFIFOW(fd, 0) = 0xADE;
  17953. #ifdef RENEWAL
  17954. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate_renewal;
  17955. #else
  17956. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate;
  17957. #endif
  17958. WFIFOSET(fd, packet_len(0xADE));
  17959. #endif
  17960. }
  17961. enum e_private_airship_response : uint32{
  17962. PRIVATEAIRSHIP_OK,
  17963. PRIVATEAIRSHIP_RETRY,
  17964. PRIVATEAIRSHIP_ITEM_NOT_ENOUGH,
  17965. PRIVATEAIRSHIP_DESTINATION_MAP_INVALID,
  17966. PRIVATEAIRSHIP_SOURCE_MAP_INVALID,
  17967. PRIVATEAIRSHIP_ITEM_UNAVAILABLE
  17968. };
  17969. /// Send out the response to a private airship request
  17970. /// 0A4A <response>.L
  17971. void clif_private_airship_response( map_session_data* sd, enum e_private_airship_response response ){
  17972. #if PACKETVER >= 20180321
  17973. nullpo_retv( sd );
  17974. int32 fd = sd->fd;
  17975. WFIFOHEAD( fd, packet_len( 0xA4A ) );
  17976. WFIFOW( fd, 0 ) = 0xA4A;
  17977. WFIFOL( fd, 2 ) = response;
  17978. WFIFOSET( fd, packet_len( 0xA4A ) );
  17979. #endif
  17980. }
  17981. /// Parses a request for a private airship
  17982. /// 0A49 <mapname>.16B <itemid>.W
  17983. void clif_parse_private_airship_request( int32 fd, map_session_data* sd ){
  17984. #if PACKETVER >= 20180321
  17985. // Check if the feature is enabled
  17986. if( !battle_config.feature_privateairship ){
  17987. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 792 ), false, SELF ); // The private airship system is disabled.
  17988. return;
  17989. }
  17990. // Check if the player is allowed to warp from the source map
  17991. if( !map_getmapflag( sd->bl.m, MF_PRIVATEAIRSHIP_SOURCE ) ){
  17992. clif_private_airship_response( sd, PRIVATEAIRSHIP_SOURCE_MAP_INVALID );
  17993. return;
  17994. }
  17995. const PACKET_CZ_PRIVATE_AIRSHIP_REQUEST* p = reinterpret_cast<PACKET_CZ_PRIVATE_AIRSHIP_REQUEST*>( RFIFOP( fd, 0 ) );
  17996. char mapname[MAP_NAME_LENGTH_EXT];
  17997. safestrncpy( mapname, p->mapName, MAP_NAME_LENGTH_EXT );
  17998. int16 mapindex = mapindex_name2id( mapname );
  17999. // Check if we know the mapname
  18000. if( mapindex < 0 ){
  18001. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  18002. return;
  18003. }
  18004. int16 mapid = map_mapindex2mapid( mapindex );
  18005. // Check if the map is available on this server
  18006. if( mapid < 0 ){
  18007. // TODO: add multi map-server support, cant validate the mapflags of the other server
  18008. return;
  18009. }
  18010. // Check if the player tried to warp to the map he is on
  18011. if( sd->bl.m == mapid ){
  18012. // This is blocked by the client, but just to be sure
  18013. return;
  18014. }
  18015. // This can only be a hack, so we prevent it
  18016. if( map_getmapdata( mapid )->instance_id ){
  18017. // Ignore requests to warp directly into a running instance
  18018. return;
  18019. }
  18020. // Check if the player is allowed to warp to the target map
  18021. if( !map_getmapflag( mapid, MF_PRIVATEAIRSHIP_DESTINATION ) ){
  18022. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  18023. return;
  18024. }
  18025. t_itemid item_id = p->ItemID;
  18026. // Check if the item sent by the client is known to us
  18027. if( !itemdb_group.item_exists(IG_PRIVATE_AIRSHIP, item_id) ){
  18028. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_UNAVAILABLE );
  18029. return;
  18030. }
  18031. int32 idx = pc_search_inventory( sd, item_id );
  18032. // Check if the player has the item at all
  18033. if( idx < 0 ){
  18034. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_NOT_ENOUGH );
  18035. return;
  18036. }
  18037. // Delete the chosen item
  18038. if( pc_delitem( sd, idx, 1, 0, 0, LOG_TYPE_PRIVATE_AIRSHIP ) ){
  18039. clif_private_airship_response( sd, PRIVATEAIRSHIP_RETRY );
  18040. return;
  18041. }
  18042. // Warp the player to a random spot on the destination map
  18043. pc_setpos( sd, mapindex, 0, 0, CLR_TELEPORT );
  18044. #endif
  18045. }
  18046. /// Sends out the usage history of the guild storage
  18047. /// 09DA <size>.W <result>.W <count>.W { <id>.L <item id>.W <amount>.L <action>.B <refine>.L <unique id>.Q <identify>.B <item type>.W
  18048. /// { <card item id>.W }*4 <name>.24B <time>.24B <attribute>.B }*count (ZC_ACK_GUILDSTORAGE_LOG)
  18049. void clif_guild_storage_log( map_session_data& sd, std::vector<struct guild_log_entry>& log, enum e_guild_storage_log result ){
  18050. #if PACKETVER >= 20140205
  18051. PACKET_ZC_ACK_GUILDSTORAGE_LOG* p = reinterpret_cast<PACKET_ZC_ACK_GUILDSTORAGE_LOG*>( packet_buffer );
  18052. p->packetType = HEADER_ZC_ACK_GUILDSTORAGE_LOG;
  18053. p->PacketLength = sizeof( *p );
  18054. p->result = result;
  18055. if( result == GUILDSTORAGE_LOG_FINAL_SUCCESS ){
  18056. p->amount = (uint16)log.size();
  18057. for( int32 i = 0; i < log.size(); i++ ){
  18058. PACKET_ZC_ACK_GUILDSTORAGE_LOG_sub& item = p->items[i];
  18059. struct guild_log_entry& entry = log[i];
  18060. item.id = entry.id;
  18061. item.itemId = client_nameid( entry.item.nameid );
  18062. item.amount = (uint16)( entry.amount > 0 ? entry.amount : ( entry.amount * -1 ) );
  18063. item.action = entry.amount > 0; // action = true(put), false(get)
  18064. item.refine = entry.item.refine;
  18065. item.uniqueId = entry.item.unique_id;
  18066. item.IsIdentified = entry.item.identify;
  18067. item.itemType = itemtype( entry.item.nameid );
  18068. clif_addcards( &item.slot, &entry.item );
  18069. safestrncpy( item.name, entry.name, NAME_LENGTH );
  18070. safestrncpy( item.time, entry.time, NAME_LENGTH );
  18071. item.attribute = entry.item.attribute;
  18072. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof ( item ) );
  18073. }
  18074. }else{
  18075. p->amount = 0;
  18076. }
  18077. clif_send( p, p->PacketLength, &sd.bl, SELF );
  18078. #endif
  18079. }
  18080. /// Activates the client camera info or updates the client camera with the given values.
  18081. /// 0A78 <type>.B <range>.F <rotation>.F <latitude>.F
  18082. void clif_camerainfo( map_session_data* sd, bool show, float range, float rotation, float latitude ){
  18083. #if PACKETVER >= 20160525
  18084. int32 fd = sd->fd;
  18085. WFIFOHEAD(fd, packet_len(0xa78));
  18086. WFIFOW(fd, 0) = 0xa78;
  18087. WFIFOB(fd, 2) = show;
  18088. WFIFOF(fd, 3) = range;
  18089. WFIFOF(fd, 7) = rotation;
  18090. WFIFOF(fd, 11) = latitude;
  18091. WFIFOSET(fd, packet_len(0xa78));
  18092. #endif
  18093. }
  18094. /// Activates or deactives the client camera info or updates the camera settings.
  18095. /// This packet is triggered by /viewpointvalue or /setcamera
  18096. /// 0A77 <type>.B <range>.F <rotation>.F <latitude>.F
  18097. void clif_parse_camerainfo( int32 fd, map_session_data* sd ){
  18098. char command[CHAT_SIZE_MAX];
  18099. // /viewpointvalue
  18100. if( RFIFOB( fd, 2 ) == 1 ){
  18101. safesnprintf( command, sizeof( command ), "%ccamerainfo", atcommand_symbol );
  18102. // /setcamera
  18103. }else{
  18104. safesnprintf( command, sizeof( command ), "%ccamerainfo %03.03f %03.03f %03.03f", atcommand_symbol, RFIFOF( fd, 3 ), RFIFOF( fd, 7 ), RFIFOF( fd, 11 ) );
  18105. }
  18106. is_atcommand( fd, sd, command, 1 );
  18107. }
  18108. /// Send the full list of items in the equip switch window
  18109. /// 0a9b <length>.W { <index>.W <position>.L }*
  18110. void clif_equipswitch_list( map_session_data* sd ){
  18111. #if PACKETVER >= 20170208
  18112. int32 fd = sd->fd;
  18113. int32 offset, i, position;
  18114. WFIFOW(fd, 0) = 0xa9b;
  18115. for( i = 0, offset = 4, position = 0; i < EQI_MAX; i++ ){
  18116. int16 index = sd->equip_switch_index[i];
  18117. if( index >= 0 && !( position & equip_bitmask[i] ) ){
  18118. WFIFOW(fd, offset) = index + 2;
  18119. WFIFOL(fd, offset + 2) = sd->inventory.u.items_inventory[index].equipSwitch;
  18120. position |= sd->inventory.u.items_inventory[index].equipSwitch;
  18121. offset += 6;
  18122. }
  18123. }
  18124. WFIFOW(fd, 2) = offset;
  18125. WFIFOSET(fd, offset);
  18126. #endif
  18127. }
  18128. /// Acknowledgement for removing an equip to the equip switch window
  18129. /// 0a9a <index>.W <position.>.L <failure>.W
  18130. void clif_equipswitch_remove( map_session_data* sd, uint16 index, uint32 pos, bool failed ){
  18131. #if PACKETVER >= 20170208
  18132. int32 fd = sd->fd;
  18133. WFIFOHEAD(fd, packet_len(0xa9a));
  18134. WFIFOW(fd, 0) = 0xa9a;
  18135. WFIFOW(fd, 2) = index + 2;
  18136. WFIFOL(fd, 4) = pos;
  18137. WFIFOW(fd, 8) = failed;
  18138. WFIFOSET(fd, packet_len(0xa9a));
  18139. #endif
  18140. }
  18141. /// Request to remove an equip from the equip switch window
  18142. /// 0a99 <index>.W <position>.L <= 20170502
  18143. /// 0a99 <index>.W
  18144. void clif_parse_equipswitch_remove( int32 fd, map_session_data* sd ){
  18145. #if PACKETVER >= 20170208
  18146. uint16 index = RFIFOW(fd, 2) - 2;
  18147. bool removed = false;
  18148. if( !battle_config.feature_equipswitch ){
  18149. return;
  18150. }
  18151. // Check if the index is valid
  18152. if( index >= MAX_INVENTORY ){
  18153. return;
  18154. }
  18155. pc_equipswitch_remove( sd, index );
  18156. #endif
  18157. }
  18158. /// Acknowledgement for adding an equip to the equip switch window
  18159. /// 0a98 <index>.W <position.>.L <flag>.L <= 20170502
  18160. /// 0a98 <index>.W <position.>.L <flag>.W
  18161. void clif_equipswitch_add( map_session_data* sd, uint16 index, uint32 pos, uint8 flag ){
  18162. #if PACKETVER >= 20170208
  18163. int32 fd = sd->fd;
  18164. WFIFOHEAD(fd, packet_len(0xa98));
  18165. WFIFOW(fd, 0) = 0xa98;
  18166. WFIFOW(fd, 2) = index + 2;
  18167. WFIFOL(fd, 4) = pos;
  18168. #if PACKETVER <= 20170502
  18169. WFIFOL(fd, 8) = flag;
  18170. #else
  18171. WFIFOW(fd, 8) = flag;
  18172. #endif
  18173. WFIFOSET(fd,packet_len(0xa98));
  18174. #endif
  18175. }
  18176. /// Request to add an equip to the equip switch window
  18177. /// 0a97 <index>.W <position>.L
  18178. void clif_parse_equipswitch_add( int32 fd, map_session_data* sd ){
  18179. #if PACKETVER >= 20170208
  18180. uint16 index = RFIFOW(fd, 2) - 2;
  18181. uint32 position = RFIFOL(fd, 4);
  18182. if( !battle_config.feature_equipswitch ){
  18183. return;
  18184. }
  18185. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  18186. return;
  18187. }
  18188. if( sd->state.trading || sd->npc_shopid ){
  18189. clif_equipswitch_add( sd, index, position, ITEM_EQUIP_ACK_OK );
  18190. return;
  18191. }
  18192. if( sd->inventory_data[index]->type == IT_AMMO ){
  18193. position = EQP_AMMO;
  18194. }
  18195. pc_equipitem( sd, index, position, true );
  18196. #endif
  18197. }
  18198. /// Acknowledgement packet for the full equip switch
  18199. /// 0a9d <failed>.W
  18200. void clif_equipswitch_reply( map_session_data* sd, bool failed ){
  18201. #if PACKETVER >= 20170208
  18202. int32 fd = sd->fd;
  18203. WFIFOHEAD(fd, packet_len(0xa9d));
  18204. WFIFOW(fd, 0) = 0xa9d;
  18205. WFIFOW(fd, 2) = failed;
  18206. WFIFOSET(fd, packet_len(0xa9d));
  18207. #endif
  18208. }
  18209. /// Request to do a full equip switch
  18210. /// 0a9c
  18211. void clif_parse_equipswitch_request( int32 fd, map_session_data* sd ){
  18212. #if PACKETVER >= 20170208
  18213. int32 i;
  18214. t_tick tick = gettick();
  18215. uint16 skill_id = ALL_EQSWITCH, skill_lv = 1;
  18216. if( DIFF_TICK(tick, sd->equipswitch_tick) < 0 ) {
  18217. // Client will not let you send a request
  18218. return;
  18219. }
  18220. sd->equipswitch_tick = tick + skill_get_cooldown( skill_id, skill_lv );
  18221. if( !battle_config.feature_equipswitch ){
  18222. return;
  18223. }
  18224. ARR_FIND( 0, EQI_MAX, i, sd->equip_switch_index[i] >= 0 );
  18225. if( i == EQI_MAX ){
  18226. // client will show: "There is no item to replace." and should not even come here
  18227. clif_equipswitch_reply( sd, false );
  18228. return;
  18229. }
  18230. if( pc_issit(sd) && !pc_setstand( sd, false ) ){
  18231. return;
  18232. }
  18233. clif_parse_skill_toid( sd, skill_id, skill_lv, sd->bl.id );
  18234. #endif
  18235. }
  18236. /// Request to do a single equip switch
  18237. /// 0ace <index>.W
  18238. void clif_parse_equipswitch_request_single( int32 fd, map_session_data* sd ){
  18239. #if PACKETVER >= 20170502
  18240. uint16 index = RFIFOW(fd, 2) - 2;
  18241. if( !battle_config.feature_equipswitch ){
  18242. return;
  18243. }
  18244. // Check if the index is valid
  18245. if( index >= MAX_INVENTORY ){
  18246. return;
  18247. }
  18248. // Check if the item exists
  18249. if (sd->inventory_data[index] == nullptr)
  18250. return;
  18251. // Check if the item was already added to equip switch
  18252. if( sd->inventory.u.items_inventory[index].equipSwitch ){
  18253. if( sd->npc_id ){
  18254. #ifdef RENEWAL
  18255. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) ){
  18256. clif_msg( sd, MSI_BUSY);
  18257. return;
  18258. }
  18259. #endif
  18260. if( !sd->npc_item_flag ){
  18261. return;
  18262. }
  18263. }
  18264. pc_equipswitch( sd, index );
  18265. return;
  18266. }
  18267. pc_equipitem( sd, index, pc_equippoint(sd, index), true );
  18268. #endif
  18269. }
  18270. void clif_parse_StartUseSkillToId( int32 fd, map_session_data* sd ){
  18271. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18272. const PACKET_CZ_USE_SKILL_START* p = reinterpret_cast<PACKET_CZ_USE_SKILL_START*>( RFIFOP( fd, 0 ) );
  18273. // Only rolling cutter is supported for now
  18274. if( p->skillId != GC_ROLLINGCUTTER ){
  18275. return;
  18276. }
  18277. // Already running - cant happen on officials, since only one skill is supported
  18278. if( sd->skill_keep_using.skill_id != 0 ){
  18279. return;
  18280. }
  18281. sd->skill_keep_using.tid = INVALID_TIMER;
  18282. sd->skill_keep_using.skill_id = p->skillId;
  18283. sd->skill_keep_using.level = p->skillLv;
  18284. sd->skill_keep_using.target = p->targetId;
  18285. clif_parse_skill_toid( sd, sd->skill_keep_using.skill_id, sd->skill_keep_using.level, sd->skill_keep_using.target );
  18286. #endif
  18287. }
  18288. void clif_parse_StopUseSkillToId( int32 fd, map_session_data* sd ){
  18289. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18290. const PACKET_CZ_USE_SKILL_END* p = reinterpret_cast<PACKET_CZ_USE_SKILL_END*>( RFIFOP( fd, 0 ) );
  18291. // Not running
  18292. if( sd->skill_keep_using.skill_id == 0 ){
  18293. return;
  18294. }
  18295. #if 0
  18296. // Hack
  18297. if( p->skillId != sd->skill_keep_using.skill_id ){
  18298. return;
  18299. }
  18300. #endif
  18301. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  18302. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  18303. sd->skill_keep_using.tid = INVALID_TIMER;
  18304. }
  18305. sd->skill_keep_using.skill_id = 0;
  18306. sd->skill_keep_using.level = 0;
  18307. sd->skill_keep_using.target = 0;
  18308. #endif
  18309. }
  18310. void clif_ping( map_session_data* sd ){
  18311. #if PACKETVER_MAIN_NUM >= 20190213 || PACKETVER_RE_NUM >= 20190213 || PACKETVER_ZERO_NUM >= 20190130
  18312. nullpo_retv( sd );
  18313. int32 fd = sd->fd;
  18314. if( !session_isActive( fd ) ){
  18315. return;
  18316. }
  18317. PACKET_ZC_PING_LIVE p = {};
  18318. p.packetType = HEADER_ZC_PING_LIVE;
  18319. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18320. #endif
  18321. }
  18322. int32 clif_ping_timer_sub( map_session_data *sd, va_list ap ){
  18323. nullpo_ret( sd );
  18324. int32 fd = sd->fd;
  18325. if( !session_isActive( fd ) ){
  18326. return 0;
  18327. }
  18328. t_tick tick = va_arg( ap, t_tick );
  18329. if( session[fd]->wdata_tick + battle_config.ping_time < tick ){
  18330. clif_ping( sd );
  18331. }
  18332. return 0;
  18333. }
  18334. TIMER_FUNC( clif_ping_timer ){
  18335. map_foreachpc( clif_ping_timer_sub, gettick() );
  18336. return 0;
  18337. }
  18338. /**
  18339. * Opens the refine UI on the designated client.
  18340. * 0aa0
  18341. */
  18342. void clif_refineui_open( map_session_data* sd ){
  18343. #if PACKETVER >= 20161012
  18344. nullpo_retv( sd );
  18345. PACKET_ZC_OPEN_REFINING_UI p = {};
  18346. p.packetType = HEADER_ZC_OPEN_REFINING_UI;
  18347. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18348. sd->state.refineui_open = true;
  18349. #endif
  18350. }
  18351. /**
  18352. * Parses cancel/close of the refine UI on client side.
  18353. * 0aa4
  18354. */
  18355. void clif_parse_refineui_close( int32 fd, map_session_data* sd ){
  18356. #if PACKETVER >= 20161012
  18357. sd->state.refineui_open = false;
  18358. #endif
  18359. }
  18360. /**
  18361. * Adds the selected item into the refine UI and sends the possible materials
  18362. * to the client.
  18363. * 0aa2 <length>.W <index>.W <catalyst count>.B { <material>.W <chance>.B <price>.L }*
  18364. */
  18365. void clif_refineui_info( map_session_data* sd, uint16 index ){
  18366. #if PACKETVER >= 20161012
  18367. int32 fd = sd->fd;
  18368. // Get the item db reference
  18369. struct item_data* id = sd->inventory_data[index];
  18370. // No item data was found
  18371. if( id == nullptr ){
  18372. return;
  18373. }
  18374. // Check the inventory
  18375. struct item* item = &sd->inventory.u.items_inventory[index];
  18376. // No item was found at the given index
  18377. if( item == nullptr ){
  18378. return;
  18379. }
  18380. // Check if the item is identified
  18381. if( !item->identify ){
  18382. return;
  18383. }
  18384. // Check if the item is broken
  18385. if( item->attribute ){
  18386. return;
  18387. }
  18388. PACKET_ZC_REFINING_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_REFINING_MATERIAL_LIST*>( packet_buffer );
  18389. p->packetType = HEADER_ZC_REFINING_MATERIAL_LIST;
  18390. p->packetLength = sizeof( *p );
  18391. p->itemIndex = client_index( index );
  18392. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18393. // No possibilities were found
  18394. if( info == nullptr ){
  18395. p->blacksmithBlessing = 0;
  18396. }else{
  18397. p->blacksmithBlessing = static_cast<decltype(p->blacksmithBlessing)>( info->blessing_amount );
  18398. for( uint16 i = REFINE_COST_NORMAL, count = 0; i < REFINE_COST_MAX; i++ ){
  18399. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, i );
  18400. if( cost == nullptr ){
  18401. continue;
  18402. }
  18403. PACKET_ZC_REFINING_MATERIAL_LIST_SUB& entry = p->req[count];
  18404. entry.itemId = client_nameid( cost->nameid );
  18405. entry.chance = static_cast<decltype(entry.chance)>( cost->chance / 100 );
  18406. entry.zeny = cost->zeny;
  18407. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  18408. count++;
  18409. }
  18410. }
  18411. clif_send( p, p->packetLength, &sd->bl, SELF );
  18412. #endif
  18413. }
  18414. /**
  18415. * Client request to add an item to the refine UI.
  18416. * 0aa1 <index>.W
  18417. */
  18418. void clif_parse_refineui_add( int32 fd, map_session_data* sd ){
  18419. #if PACKETVER >= 20161012
  18420. const PACKET_CZ_REFINING_SELECT_ITEM* p = reinterpret_cast<PACKET_CZ_REFINING_SELECT_ITEM*>( RFIFOP( fd, 0 ) );
  18421. uint16 index = server_index( p->index );
  18422. // Check if the refine UI is open
  18423. if( !sd->state.refineui_open ){
  18424. return;
  18425. }
  18426. // Check if the index is valid
  18427. if( index >= MAX_INVENTORY ){
  18428. return;
  18429. }
  18430. // Send out the requirements for the refine process
  18431. clif_refineui_info( sd, index );
  18432. #endif
  18433. }
  18434. /**
  18435. * Client requests to try to refine an item.
  18436. * 0aa3 <index>.W <material>.W <catalyst>.B
  18437. */
  18438. void clif_parse_refineui_refine( int32 fd, map_session_data* sd ){
  18439. #if PACKETVER >= 20161012
  18440. const PACKET_CZ_REQ_REFINING* p = reinterpret_cast<PACKET_CZ_REQ_REFINING*>( RFIFOP( fd, 0 ) );
  18441. uint16 index = server_index( p->index );
  18442. t_itemid material = p->itemId;
  18443. int16 j;
  18444. // Check if the refine UI is open
  18445. if( !sd->state.refineui_open ){
  18446. return;
  18447. }
  18448. // Check if the index is valid
  18449. if( index >= MAX_INVENTORY ){
  18450. return;
  18451. }
  18452. // Get the item db reference
  18453. struct item_data* id = sd->inventory_data[index];
  18454. // No item data was found
  18455. if( id == nullptr ){
  18456. return;
  18457. }
  18458. // Check the inventory
  18459. struct item* item = &sd->inventory.u.items_inventory[index];
  18460. // No item was found at the given index
  18461. if( item == nullptr ){
  18462. return;
  18463. }
  18464. // Check if the item is identified
  18465. if( !item->identify ){
  18466. return;
  18467. }
  18468. // Check if the item is broken
  18469. if( item->attribute ){
  18470. return;
  18471. }
  18472. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18473. // No refine possible
  18474. if( info == nullptr ){
  18475. return;
  18476. }
  18477. // No possibilities were found
  18478. if( info->costs.empty() ){
  18479. return;
  18480. }
  18481. // Check if the player has the selected material
  18482. if( ( j = pc_search_inventory( sd, material ) ) < 0 ){
  18483. return;
  18484. }
  18485. int16 blacksmith_index = -1;
  18486. uint16 blacksmith_amount = 0;
  18487. // Check if the player has enough blacksmith blessings
  18488. if( p->blacksmithBlessing != 0 ){
  18489. blacksmith_amount = info->blessing_amount;
  18490. // Player tried to use blacksmith blessing on a refine level, where it is not available
  18491. if( blacksmith_amount == 0 ){
  18492. return;
  18493. }
  18494. // Check if the player has blacksmith blessings
  18495. if( ( blacksmith_index = pc_search_inventory( sd, ITEMID_BLACKSMITH_BLESSING ) ) < 0 ){
  18496. return;
  18497. }
  18498. // Check if the player has enough blacksmith blessings
  18499. if( sd->inventory.u.items_inventory[blacksmith_index].amount < blacksmith_amount ){
  18500. return;
  18501. }
  18502. }
  18503. std::shared_ptr<s_refine_cost> cost = nullptr;
  18504. for( const auto& pair : info->costs ){
  18505. if( pair.second->nameid == material ){
  18506. cost = pair.second;
  18507. break;
  18508. }
  18509. }
  18510. // The material was not in the list of possible materials
  18511. if( cost == nullptr ){
  18512. return;
  18513. }
  18514. // Try to pay for the refine
  18515. if( pc_payzeny( sd, cost->zeny, LOG_TYPE_CONSUME ) ){
  18516. clif_npc_buy_result( sd, e_purchase_result::PURCHASE_FAIL_MONEY ); // "You do not have enough zeny."
  18517. return;
  18518. }
  18519. // Delete the required material
  18520. if( pc_delitem( sd, j, 1, 0, 0, LOG_TYPE_CONSUME ) ){
  18521. return;
  18522. }
  18523. // Delete the required blacksmith blessings
  18524. if( blacksmith_amount > 0 && pc_delitem( sd, blacksmith_index, blacksmith_amount, 0, 0, LOG_TYPE_CONSUME ) ){
  18525. return;
  18526. }
  18527. // Try to refine the item
  18528. if( cost->chance >= ( rnd() % 10000 ) ){
  18529. log_pick_pc( sd, LOG_TYPE_OTHER, -1, item );
  18530. // Success
  18531. item->refine = cap_value( item->refine + 1, 0, MAX_REFINE );
  18532. log_pick_pc( sd, LOG_TYPE_OTHER, 1, item );
  18533. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_SUCCESS );
  18534. clif_refine( *sd, index, ITEMREFINING_SUCCESS );
  18535. if (info->broadcast_success) {
  18536. clif_broadcast_refine_result(*sd, item->nameid, item->refine, true);
  18537. }
  18538. if( id->type == IT_WEAPON ){
  18539. achievement_update_objective( sd, AG_ENCHANT_SUCCESS, 2, id->weapon_level, item->refine );
  18540. }
  18541. clif_refineui_info( sd, index );
  18542. }else{
  18543. // Failure
  18544. if (info->broadcast_failure) {
  18545. clif_broadcast_refine_result(*sd, item->nameid, item->refine, false);
  18546. }
  18547. // Blacksmith blessings were used to prevent breaking and downgrading
  18548. if( blacksmith_amount > 0 ){
  18549. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18550. clif_refineui_info( sd, index );
  18551. // Delete the item if it is breakable
  18552. }else if( cost->breaking_rate > 0 && ( rnd() % 10000 ) < cost->breaking_rate ){
  18553. clif_refine( *sd, index, ITEMREFINING_FAILURE );
  18554. pc_delitem( sd, index, 1, 0, 2, LOG_TYPE_CONSUME );
  18555. // Downgrade the item if necessary
  18556. }else if( cost->downgrade_amount > 0 ){
  18557. item->refine = cap_value( item->refine - cost->downgrade_amount, 0, MAX_REFINE );
  18558. clif_refine( *sd, index, ITEMREFINING_DOWNGRADE );
  18559. clif_refineui_info(sd, index);
  18560. // Only show failure, but dont do anything
  18561. }else{
  18562. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18563. clif_refineui_info( sd, index );
  18564. }
  18565. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_FAILURE );
  18566. achievement_update_objective( sd, AG_ENCHANT_FAIL, 1, 1 );
  18567. }
  18568. #endif
  18569. }
  18570. void clif_unequipall_reply( map_session_data* sd, bool failed ){
  18571. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18572. PACKET_ZC_ACK_TAKEOFF_EQUIP_ALL p = {};
  18573. p.PacketType = HEADER_ZC_ACK_TAKEOFF_EQUIP_ALL;
  18574. p.result = failed;
  18575. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18576. #endif // PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18577. }
  18578. void clif_parse_unequipall( int32 fd, map_session_data* sd ){
  18579. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20210818
  18580. if( pc_cant_act( sd ) ){
  18581. clif_unequipall_reply( sd, true );
  18582. return;
  18583. }
  18584. const PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL* p = reinterpret_cast<PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL*>( RFIFOP( fd, 0 ) );
  18585. #if PACKETVER_MAIN_NUM >= 20230906
  18586. uint32 location = p->location;
  18587. int32 max = EQI_MAX;
  18588. #else
  18589. uint32 location = 0xFFFFFFFF;
  18590. int32 max = EQI_COSTUME_HEAD_TOP;
  18591. #endif
  18592. for( int32 i = 0; i < max; i++ ){
  18593. if( sd->equip_index[i] >= 0 && ( location & equip_bitmask[i] ) != 0 ){
  18594. pc_unequipitem( sd, sd->equip_index[i], 1 );
  18595. }
  18596. }
  18597. clif_unequipall_reply( sd, false );
  18598. #endif
  18599. }
  18600. void clif_stylist_response( map_session_data* sd, bool failed ){
  18601. #if PACKETVER >= 20151104
  18602. PACKET_ZC_STYLE_CHANGE_RES p = {};
  18603. p.PacketType = HEADER_ZC_STYLE_CHANGE_RES;
  18604. p.flag = failed;
  18605. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18606. if( !failed ){
  18607. sd->state.stylist_open = false;
  18608. }
  18609. #endif
  18610. }
  18611. bool clif_parse_stylist_buy_sub( map_session_data* sd, _look look, int16 index ){
  18612. std::shared_ptr<s_stylist_list> list = stylist_db.find( look );
  18613. if( list == nullptr ){
  18614. return false;
  18615. }
  18616. std::shared_ptr<s_stylist_entry> entry = util::umap_find( list->entries, index );
  18617. if( entry == nullptr ){
  18618. return false;
  18619. }
  18620. std::shared_ptr<s_stylist_costs> costs;
  18621. if( ( sd->class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  18622. costs = entry->doram;
  18623. }else{
  18624. costs = entry->human;
  18625. }
  18626. if( costs == nullptr ){
  18627. return false;
  18628. }
  18629. if( sd->status.zeny < costs->price ){
  18630. return false;
  18631. }
  18632. int16 inventoryIndex = -1;
  18633. if( costs->requiredItem != 0 ){
  18634. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18635. if( inventoryIndex < 0 ){
  18636. // No other option
  18637. if( costs->requiredItemBox == 0 ){
  18638. return false;
  18639. }
  18640. // Check if the box that contains the item is in the inventory
  18641. inventoryIndex = pc_search_inventory( sd, costs->requiredItemBox );
  18642. // The box containing the item also does not exist
  18643. if( inventoryIndex < 0 ){
  18644. return false;
  18645. }
  18646. }
  18647. }else if( costs->requiredItemBox != 0 ){
  18648. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18649. if( inventoryIndex < 0 ){
  18650. return false;
  18651. }
  18652. }
  18653. if( inventoryIndex >= 0 && pc_delitem( sd, inventoryIndex, 1, 0, 0, LOG_TYPE_OTHER ) != 0 ){
  18654. return false;
  18655. }
  18656. if( costs->price > 0 && pc_payzeny( sd, costs->price, LOG_TYPE_OTHER ) != 0 ){
  18657. return false;
  18658. }
  18659. switch( look ){
  18660. case LOOK_HAIR:
  18661. case LOOK_HAIR_COLOR:
  18662. case LOOK_CLOTHES_COLOR:
  18663. case LOOK_BODY2:
  18664. pc_changelook( sd, look, entry->value );
  18665. break;
  18666. case LOOK_HEAD_BOTTOM:
  18667. case LOOK_HEAD_MID:
  18668. case LOOK_HEAD_TOP: {
  18669. struct mail_message msg = {};
  18670. msg.dest_id = sd->status.char_id;
  18671. safestrncpy( msg.send_name, "Styling Shop", NAME_LENGTH );
  18672. safestrncpy( msg.title, "<MSG>2949</MSG>", MAIL_TITLE_LENGTH );
  18673. safestrncpy( msg.body, "<MSG>2950</MSG>", MAIL_BODY_LENGTH );
  18674. msg.item[0].nameid = entry->value;
  18675. msg.item[0].identify = 1;
  18676. msg.item[0].amount = 1;
  18677. msg.status = MAIL_NEW;
  18678. msg.type = MAIL_INBOX_NORMAL;
  18679. msg.timestamp = time( nullptr );
  18680. intif_Mail_send( 0, &msg );
  18681. } break;
  18682. }
  18683. return true;
  18684. }
  18685. void clif_parse_stylist_buy( int32 fd, map_session_data* sd ){
  18686. #if PACKETVER >= 20151104
  18687. #if PACKETVER >= 20180516
  18688. const PACKET_CZ_REQ_STYLE_CHANGE2* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE2*>( RFIFOP( fd, 0 ) );
  18689. #else
  18690. const PACKET_CZ_REQ_STYLE_CHANGE* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE*>( RFIFOP( fd, 0 ) );
  18691. #endif
  18692. if( p->HeadPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR_COLOR, p->HeadPalette ) ){
  18693. clif_stylist_response( sd, true );
  18694. return;
  18695. }
  18696. if( p->HeadStyle != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR, p->HeadStyle ) ){
  18697. clif_stylist_response( sd, true );
  18698. return;
  18699. }
  18700. if( p->BodyPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_CLOTHES_COLOR, p->BodyPalette ) ){
  18701. clif_stylist_response( sd, true );
  18702. return;
  18703. }
  18704. if( p->TopAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_TOP, p->TopAccessory ) ){
  18705. clif_stylist_response( sd, true );
  18706. return;
  18707. }
  18708. if( p->MidAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_MID, p->MidAccessory ) ){
  18709. clif_stylist_response( sd, true );
  18710. return;
  18711. }
  18712. if( p->BottomAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_BOTTOM, p->BottomAccessory ) ){
  18713. clif_stylist_response( sd, true );
  18714. return;
  18715. }
  18716. #if PACKETVER >= 20180516
  18717. if( p->BodyStyle != 0 && ( sd->class_ & JOBL_THIRD ) != 0 && ( sd->class_ & JOBL_FOURTH ) == 0 && !clif_parse_stylist_buy_sub( sd, LOOK_BODY2, p->BodyStyle ) ){
  18718. clif_stylist_response( sd, true );
  18719. return;
  18720. }
  18721. #endif
  18722. clif_stylist_response( sd, false );
  18723. #endif
  18724. }
  18725. void clif_parse_stylist_close( int32 fd, map_session_data* sd ){
  18726. #if PACKETVER >= 20151104
  18727. sd->state.stylist_open = false;
  18728. #endif
  18729. }
  18730. void clif_inventory_expansion_info( map_session_data* sd ){
  18731. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18732. nullpo_retv( sd );
  18733. PACKET_ZC_EXTEND_BODYITEM_SIZE p = {};
  18734. p.packetType = HEADER_ZC_EXTEND_BODYITEM_SIZE;
  18735. p.expansionSize = sd->status.inventory_slots - INVENTORY_BASE_SIZE;
  18736. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18737. #endif
  18738. }
  18739. enum class e_inventory_expansion_response : uint8{
  18740. ASK_CONFIRMATION = 0,
  18741. FAILED,
  18742. BUSY,
  18743. MISSING_ITEM,
  18744. MAXIMUM_REACHED
  18745. };
  18746. void clif_inventory_expansion_response( map_session_data* sd, e_inventory_expansion_response response ){
  18747. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18748. nullpo_retv( sd );
  18749. PACKET_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE p = {};
  18750. p.packetType = HEADER_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE;
  18751. p.result = (uint8)response;
  18752. p.itemId = sd->state.inventory_expansion_confirmation;
  18753. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18754. #endif
  18755. }
  18756. void clif_parse_inventory_expansion_request( int32 fd, map_session_data* sd ){
  18757. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18758. // Check if player is dead or busy with other stuff
  18759. if( pc_isdead( sd ) || pc_cant_act( sd ) ){
  18760. clif_inventory_expansion_response( sd, e_inventory_expansion_response::BUSY );
  18761. return;
  18762. }
  18763. // Check if the player already reached the maximum
  18764. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18765. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18766. return;
  18767. }
  18768. static std::map<t_itemid, uint16> items = {
  18769. // The order of entries in this list defines which will be used first
  18770. // This order and the usable items are hardcoded into the client
  18771. // The number of increased slots is "hardcoded" in the message of the client and cannot be sent per item
  18772. { ITEMID_INVENTORY_EX_EVT, 10 },
  18773. { ITEMID_INVENTORY_EX_DIS, 10 },
  18774. { ITEMID_INVENTORY_EX, 10 },
  18775. };
  18776. int16 index = -1;
  18777. bool found_over_limit = false;
  18778. for( const auto& entry : items ){
  18779. // Check if the player has the required item
  18780. index = pc_search_inventory( sd, entry.first );
  18781. // Found an item
  18782. if( index >= 0 ){
  18783. // Check if the player would exceed the maximum
  18784. if( sd->status.inventory_slots + entry.second > MAX_INVENTORY ){
  18785. found_over_limit = true;
  18786. }else{
  18787. found_over_limit = false;
  18788. sd->state.inventory_expansion_confirmation = entry.first;
  18789. sd->state.inventory_expansion_amount = entry.second;
  18790. break;
  18791. }
  18792. }
  18793. }
  18794. // Check if an item was found
  18795. if( sd->state.inventory_expansion_confirmation == 0 ){
  18796. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MISSING_ITEM );
  18797. return;
  18798. }
  18799. // Check if an item would have been found, but the player would exceed the maximum
  18800. if( found_over_limit ){
  18801. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18802. return;
  18803. }
  18804. // The player met all requirements => ask him for confirmation
  18805. clif_inventory_expansion_response( sd, e_inventory_expansion_response::ASK_CONFIRMATION );
  18806. #endif
  18807. }
  18808. enum class e_inventory_expansion_result : uint8{
  18809. SUCCESS = 0,
  18810. FAILED,
  18811. BUSY,
  18812. MISSING_ITEM,
  18813. MAXIMUM_REACHED
  18814. };
  18815. void clif_inventory_expansion_result( map_session_data* sd, e_inventory_expansion_result result ){
  18816. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18817. nullpo_retv( sd );
  18818. PACKET_ZC_ACK_EXTEND_BODYITEM_SIZE p = {};
  18819. p.packetType = HEADER_ZC_ACK_EXTEND_BODYITEM_SIZE;
  18820. p.result = (uint8)result;
  18821. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18822. // Reset the state tracking
  18823. sd->state.inventory_expansion_confirmation = 0;
  18824. sd->state.inventory_expansion_amount = 0;
  18825. #endif
  18826. }
  18827. void clif_parse_inventory_expansion_confirm( int32 fd, map_session_data* sd ){
  18828. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18829. if( sd->state.inventory_expansion_confirmation == 0 ){
  18830. return;
  18831. }
  18832. // Check if player is dead
  18833. if( pc_isdead( sd ) ){
  18834. clif_inventory_expansion_result( sd, e_inventory_expansion_result::BUSY );
  18835. return;
  18836. }
  18837. // Check if the player already reached the maximum
  18838. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18839. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18840. return;
  18841. }
  18842. // Check if the player has the required item
  18843. int32 index = pc_search_inventory( sd, sd->state.inventory_expansion_confirmation );
  18844. // The player did not have the item anymore
  18845. if( index < 0 ){
  18846. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MISSING_ITEM );
  18847. return;
  18848. }
  18849. // Check if the player would exceed the maximum
  18850. if( sd->status.inventory_slots + sd->state.inventory_expansion_amount > MAX_INVENTORY ){
  18851. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18852. return;
  18853. }
  18854. // Delete the required item
  18855. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_OTHER ) ){
  18856. clif_inventory_expansion_result( sd, e_inventory_expansion_result::FAILED );
  18857. return;
  18858. }
  18859. // Increase the slots
  18860. sd->status.inventory_slots += sd->state.inventory_expansion_amount;
  18861. // Save player data (slots) and inventory data (removed item)
  18862. chrif_save( sd, CSAVE_NORMAL | CSAVE_INVENTORY );
  18863. // Inform the player of success
  18864. clif_inventory_expansion_result( sd, e_inventory_expansion_result::SUCCESS );
  18865. clif_inventory_expansion_info( sd );
  18866. #endif
  18867. }
  18868. void clif_parse_inventory_expansion_reject( int32 fd, map_session_data* sd ){
  18869. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18870. sd->state.inventory_expansion_confirmation = 0;
  18871. sd->state.inventory_expansion_amount = 0;
  18872. #endif
  18873. }
  18874. void clif_barter_open( map_session_data& sd, struct npc_data& nd ){
  18875. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18876. if( nd.subtype != NPCTYPE_BARTER || nd.u.barter.extended || sd.state.barter_open ){
  18877. return;
  18878. }
  18879. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18880. if( barter == nullptr ){
  18881. return;
  18882. }
  18883. sd.state.barter_open = true;
  18884. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  18885. p->packetType = HEADER_ZC_NPC_BARTER_MARKET_ITEMINFO;
  18886. p->packetLength = sizeof( *p );
  18887. int16 count = 0;
  18888. for( const auto& itemPair : barter->items ){
  18889. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  18890. if( id == nullptr ){
  18891. continue;
  18892. }
  18893. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO_sub& item = p->list[count];
  18894. item.nameid = client_nameid( id->nameid );
  18895. item.type = itemtype( id->nameid );
  18896. if( itemPair.second->stockLimited ){
  18897. item.amount = itemPair.second->stock;
  18898. }else{
  18899. item.amount = -1;
  18900. }
  18901. item.weight = id->weight;
  18902. item.index = itemPair.second->index;
  18903. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  18904. item.viewSprite = id->look;
  18905. item.location = pc_equippoint_sub( &sd, id.get() );
  18906. #endif
  18907. // Use a loop if someone did not start with index 0
  18908. for( const auto& requirementPair : itemPair.second->requirements ){
  18909. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  18910. item.currencyNameid = client_nameid( requirement->nameid );
  18911. item.currencyAmount = requirement->amount;
  18912. // It is a normal barter, cancel after first entry
  18913. break;
  18914. }
  18915. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( item ) );
  18916. count++;
  18917. }
  18918. clif_send( p, p->packetLength, &sd.bl, SELF );
  18919. #endif
  18920. }
  18921. void clif_parse_barter_close( int32 fd, map_session_data* sd ){
  18922. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18923. if( sd->state.barter_open ){
  18924. sd->npc_shopid = 0;
  18925. sd->state.barter_open = false;
  18926. }
  18927. #endif
  18928. }
  18929. void clif_parse_barter_buy( int32 fd, map_session_data* sd ){
  18930. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18931. // No shop open
  18932. if( sd->npc_shopid == 0 || !sd->state.barter_open ){
  18933. return;
  18934. }
  18935. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  18936. // Unknown shop
  18937. if( nd == nullptr ){
  18938. return;
  18939. }
  18940. // Not a barter
  18941. if( nd->subtype != NPCTYPE_BARTER ){
  18942. return;
  18943. }
  18944. // It is an extended barter
  18945. if( nd->u.barter.extended ){
  18946. return;
  18947. }
  18948. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  18949. if( barter == nullptr ){
  18950. return;
  18951. }
  18952. const PACKET_CZ_NPC_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  18953. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  18954. // Empty purchase list
  18955. if( entries == 0 ){
  18956. return;
  18957. }
  18958. std::vector<s_barter_purchase> purchases;
  18959. purchases.reserve( entries );
  18960. // Make sure each shop index and target item id is only used once
  18961. for( int32 i = 0; i < entries; i++ ){
  18962. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  18963. // Invalid shop index
  18964. if( item == nullptr ){
  18965. return;
  18966. }
  18967. for( int32 j = i + 1; j < entries; j++ ){
  18968. // Same shop index
  18969. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  18970. return;
  18971. }
  18972. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  18973. // Invalid shop index
  18974. if( item2 == nullptr ){
  18975. return;
  18976. }
  18977. // Same target item id
  18978. if( item->nameid == item2->nameid ){
  18979. return;
  18980. }
  18981. }
  18982. s_barter_purchase purchase = {};
  18983. purchase.item = item;
  18984. purchase.amount = p->list[i].amount;
  18985. purchases.push_back( purchase );
  18986. }
  18987. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  18988. #endif
  18989. }
  18990. void clif_barter_extended_open( map_session_data& sd, struct npc_data& nd ){
  18991. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18992. if( nd.subtype != NPCTYPE_BARTER || !nd.u.barter.extended || sd.state.barter_extended_open ){
  18993. return;
  18994. }
  18995. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18996. if( barter == nullptr ){
  18997. return;
  18998. }
  18999. sd.state.barter_extended_open = true;
  19000. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  19001. p->packetType = HEADER_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO;
  19002. p->packetLength = sizeof( *p );
  19003. p->items_count = 0;
  19004. for( const auto& itemPair : barter->items ){
  19005. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  19006. if( id == nullptr ){
  19007. continue;
  19008. }
  19009. // Needs dynamic calculation, because of variable currencies
  19010. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub* item = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub*>( WBUFP( p, p->packetLength ) );
  19011. item->nameid = client_nameid( id->nameid );
  19012. item->type = itemtype( id->nameid );
  19013. if( itemPair.second->stockLimited ){
  19014. item->amount = itemPair.second->stock;
  19015. }else{
  19016. item->amount = -1;
  19017. }
  19018. item->weight = id->weight;
  19019. item->index = itemPair.second->index;
  19020. item->zeny = itemPair.second->price;
  19021. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  19022. item->viewSprite = id->look;
  19023. item->location = pc_equippoint_sub( &sd, id.get() );
  19024. #endif
  19025. // Because of a MSVS bug, the currencies have been defined with a fixed array entry, which has to be substracted
  19026. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *item ) - sizeof( item->currencies ) );
  19027. p->items_count++;
  19028. item->currency_count = 0;
  19029. for( const auto& requirementPair : itemPair.second->requirements ){
  19030. // Needs dynamic calculation, because of variable currencies
  19031. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2* req = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2*>( WBUFP( p, p->packetLength ) );
  19032. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  19033. req->nameid = requirement->nameid;
  19034. if( requirement->refine >= 0 ){
  19035. req->refine_level = requirement->refine;
  19036. }else{
  19037. req->refine_level = 0;
  19038. }
  19039. req->amount = requirement->amount;
  19040. req->type = itemtype( requirement->nameid );
  19041. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *req ) );
  19042. item->currency_count++;
  19043. }
  19044. }
  19045. clif_send( p, p->packetLength, &sd.bl, SELF );
  19046. #endif
  19047. }
  19048. void clif_parse_barter_extended_close( int32 fd, map_session_data* sd ){
  19049. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  19050. if( sd->state.barter_extended_open ){
  19051. sd->npc_shopid = 0;
  19052. sd->state.barter_extended_open = false;
  19053. }
  19054. #endif
  19055. }
  19056. void clif_parse_barter_extended_buy( int32 fd, map_session_data* sd ){
  19057. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  19058. // No shop open
  19059. if( sd->npc_shopid == 0 || !sd->state.barter_extended_open ){
  19060. return;
  19061. }
  19062. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  19063. // Unknown shop
  19064. if( nd == nullptr ){
  19065. return;
  19066. }
  19067. // Not a barter
  19068. if( nd->subtype != NPCTYPE_BARTER ){
  19069. return;
  19070. }
  19071. // Not an extended barter
  19072. if( !nd->u.barter.extended ){
  19073. return;
  19074. }
  19075. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  19076. if( barter == nullptr ){
  19077. return;
  19078. }
  19079. const PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  19080. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  19081. // Empty purchase list
  19082. if( entries == 0 ){
  19083. return;
  19084. }
  19085. std::vector<s_barter_purchase> purchases;
  19086. purchases.reserve( entries );
  19087. // Make sure each shop index and target item id is only used once
  19088. for( int32 i = 0; i < entries; i++ ){
  19089. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  19090. // Invalid shop index
  19091. if( item == nullptr ){
  19092. return;
  19093. }
  19094. for( int32 j = i + 1; j < entries; j++ ){
  19095. // Same shop index
  19096. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  19097. return;
  19098. }
  19099. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  19100. // Invalid shop index
  19101. if( item2 == nullptr ){
  19102. return;
  19103. }
  19104. // Same target item id
  19105. if( item->nameid == item2->nameid ){
  19106. return;
  19107. }
  19108. }
  19109. s_barter_purchase purchase = {};
  19110. purchase.item = item;
  19111. purchase.amount = p->list[i].amount;
  19112. purchases.push_back( purchase );
  19113. }
  19114. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  19115. #endif
  19116. }
  19117. void clif_summon_init(struct mob_data& md) {
  19118. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19119. struct block_list* master_bl = battle_get_master(&md.bl);
  19120. if( master_bl == nullptr ){
  19121. return;
  19122. }
  19123. PACKET_ZC_SUMMON_HP_INIT p = {};
  19124. p.PacketType = HEADER_ZC_SUMMON_HP_INIT;
  19125. p.summonAID = md.bl.id;
  19126. p.CurrentHP = md.status.hp;
  19127. p.MaxHP = md.status.max_hp;
  19128. clif_send( &p, sizeof( p ), master_bl, SELF );
  19129. #endif
  19130. }
  19131. void clif_summon_hp_bar(struct mob_data& md) {
  19132. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19133. struct block_list* master_bl = battle_get_master(&md.bl);
  19134. if( master_bl == nullptr ){
  19135. return;
  19136. }
  19137. PACKET_ZC_SUMMON_HP_UPDATE p = {};
  19138. p.PacketType = HEADER_ZC_SUMMON_HP_UPDATE;
  19139. p.summonAID = md.bl.id;
  19140. p.VarId = SP_HP; // HP parameter
  19141. p.Value = md.status.hp;
  19142. clif_send( &p, sizeof( p ), master_bl, SELF );
  19143. #endif
  19144. }
  19145. void clif_laphine_synthesis_open( map_session_data *sd, std::shared_ptr<s_laphine_synthesis> synthesis ){
  19146. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19147. nullpo_retv( sd );
  19148. sd->state.laphine_synthesis = synthesis->item_id;
  19149. PACKET_ZC_RANDOM_COMBINE_ITEM_UI_OPEN p = {};
  19150. p.packetType = HEADER_ZC_RANDOM_COMBINE_ITEM_UI_OPEN;
  19151. p.itemId = client_nameid( synthesis->item_id );
  19152. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19153. #endif
  19154. }
  19155. void clif_parse_laphine_synthesis_close( int32 fd, map_session_data* sd ){
  19156. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19157. sd->state.laphine_synthesis = 0;
  19158. #endif
  19159. }
  19160. enum e_laphine_synthesis_result : int16{
  19161. LAPHINE_SYNTHESIS_SUCCESS = 0,
  19162. LAPHINE_SYNTHESIS_AMOUNT = 5,
  19163. LAPHINE_SYNTHESIS_ITEM = 7
  19164. };
  19165. void clif_laphine_synthesis_result( map_session_data* sd, enum e_laphine_synthesis_result result ){
  19166. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19167. nullpo_retv( sd );
  19168. PACKET_ZC_ACK_RANDOM_COMBINE_ITEM p = {};
  19169. p.packetType = HEADER_ZC_ACK_RANDOM_COMBINE_ITEM;
  19170. p.result = result;
  19171. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19172. #endif
  19173. }
  19174. void clif_parse_laphine_synthesis( int32 fd, map_session_data* sd ){
  19175. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19176. if( sd->state.laphine_synthesis == 0 ){
  19177. return;
  19178. }
  19179. const PACKET_CZ_REQ_RANDOM_COMBINE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_COMBINE_ITEM*>( RFIFOP( fd, 0 ) );
  19180. if( sd->state.laphine_synthesis != p->itemId ){
  19181. return;
  19182. }
  19183. std::shared_ptr<s_laphine_synthesis> synthesis = laphine_synthesis_db.find( sd->state.laphine_synthesis );
  19184. if( synthesis == nullptr ){
  19185. return;
  19186. }
  19187. size_t count = ( p->packetLength - sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM ) ) / sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM_sub );
  19188. // Player sent more or less than actually required
  19189. if( count != synthesis->requiredRequirements ){
  19190. return;
  19191. }
  19192. // Check for duplicates
  19193. for( size_t i = 0; i < count; i++ ){
  19194. for( size_t j = i + 1; j < count; j++ ){
  19195. if( p->items[i].index == p->items[j].index ){
  19196. return;
  19197. }
  19198. }
  19199. }
  19200. for( size_t i = 0; i < count; i++ ){
  19201. int16 index = server_index( p->items[i].index );
  19202. if( index >= MAX_INVENTORY ){
  19203. return;
  19204. }
  19205. if( sd->inventory_data[index] == nullptr ){
  19206. return;
  19207. }
  19208. struct item* item = &sd->inventory.u.items_inventory[index];
  19209. std::shared_ptr<s_laphine_synthesis_requirement> requirement = util::umap_find( synthesis->requirements, item->nameid );
  19210. if( requirement == nullptr ){
  19211. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19212. return;
  19213. }
  19214. if( p->items[i].count != requirement->amount ){
  19215. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19216. return;
  19217. }
  19218. if( item->amount < requirement->amount ){
  19219. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19220. return;
  19221. }
  19222. if( item->refine < synthesis->minimumRefine ){
  19223. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19224. return;
  19225. }
  19226. if( item->refine > synthesis->maximumRefine ){
  19227. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19228. return;
  19229. }
  19230. if( item->equip != 0 ){
  19231. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19232. return;
  19233. }
  19234. if( item->equipSwitch != 0 ){
  19235. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19236. return;
  19237. }
  19238. }
  19239. // If triggered from item
  19240. if( sd->itemid == sd->state.laphine_synthesis ){
  19241. int16 index = pc_search_inventory( sd, sd->state.laphine_synthesis );
  19242. if( index < 0 ){
  19243. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19244. return;
  19245. }
  19246. if( ( sd->inventory_data[index]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19247. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19248. return;
  19249. }
  19250. }
  19251. }
  19252. for( size_t i = 0; i < count; i++ ){
  19253. int16 index = server_index( p->items[i].index );
  19254. if( pc_delitem( sd, index, p->items[i].count, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19255. return;
  19256. }
  19257. }
  19258. itemdb_group.pc_get_itemgroup( synthesis->rewardGroupId, true, *sd );
  19259. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_SUCCESS );
  19260. #endif
  19261. }
  19262. void clif_laphine_upgrade_open( map_session_data* sd, std::shared_ptr<s_laphine_upgrade> upgrade ){
  19263. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19264. nullpo_retv( sd );
  19265. sd->state.laphine_upgrade = upgrade->item_id;
  19266. PACKET_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN p = {};
  19267. p.packetType = HEADER_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN;
  19268. p.itemId = client_nameid( upgrade->item_id );
  19269. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19270. #endif
  19271. }
  19272. void clif_parse_laphine_upgrade_close( int32 fd, map_session_data* sd ){
  19273. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19274. sd->state.laphine_upgrade = 0;
  19275. #endif
  19276. }
  19277. void clif_laphine_upgrade_result( map_session_data *sd, bool failed ){
  19278. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19279. PACKET_ZC_ACK_RANDOM_UPGRADE_ITEM p = {};
  19280. p.packetType = HEADER_ZC_ACK_RANDOM_UPGRADE_ITEM;
  19281. p.result = failed;
  19282. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19283. #endif
  19284. }
  19285. static void clif_item_preview( map_session_data *sd, int16 index ){
  19286. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19287. nullpo_retv( sd );
  19288. struct item* item = &sd->inventory.u.items_inventory[index];
  19289. PACKET_ZC_CHANGE_ITEM_OPTION p = {};
  19290. p.packetType = HEADER_ZC_CHANGE_ITEM_OPTION;
  19291. p.index = client_index( index );
  19292. #if PACKETVER_MAIN_NUM >= 20181017 || PACKETVER_RE_NUM >= 20181017 || PACKETVER_ZERO_NUM >= 20181024
  19293. p.isDamaged = item->attribute != 0;
  19294. #endif
  19295. p.refiningLevel = item->refine;
  19296. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723
  19297. p.grade = item->enchantgrade;
  19298. #endif
  19299. clif_addcards( &p.slot, item );
  19300. clif_add_random_options( p.option_data, *item );
  19301. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19302. #endif
  19303. }
  19304. void clif_parse_laphine_upgrade( int32 fd, map_session_data* sd ){
  19305. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19306. if( sd->state.laphine_upgrade == 0 ){
  19307. return;
  19308. }
  19309. const PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM*>( RFIFOP( fd, 0 ) );
  19310. if( sd->state.laphine_upgrade != p->itemId ){
  19311. return;
  19312. }
  19313. std::shared_ptr<s_laphine_upgrade> upgrade = laphine_upgrade_db.find( sd->state.laphine_upgrade );
  19314. if( upgrade == nullptr ){
  19315. return;
  19316. }
  19317. uint16 index = server_index( p->index );
  19318. if( index >= MAX_INVENTORY ){
  19319. return;
  19320. }
  19321. if( sd->inventory_data[index] == nullptr ){
  19322. return;
  19323. }
  19324. struct item* item = &sd->inventory.u.items_inventory[index];
  19325. // Not a valid target item
  19326. if( !util::vector_exists( upgrade->target_item_ids, item->nameid ) ){
  19327. clif_laphine_upgrade_result( sd, true );
  19328. return;
  19329. }
  19330. // If target item is not identified
  19331. if( item->identify == 0 ){
  19332. clif_laphine_upgrade_result( sd, true );
  19333. return;
  19334. }
  19335. // If target item is equipped
  19336. if( item->equip != 0 ){
  19337. clif_laphine_upgrade_result( sd, true );
  19338. return;
  19339. }
  19340. // If target item is in equipswitch
  19341. if( item->equipSwitch != 0 ){
  19342. clif_laphine_upgrade_result( sd, true );
  19343. return;
  19344. }
  19345. // Check minimum refine requirement
  19346. if( item->refine < upgrade->minimumRefine ){
  19347. clif_laphine_upgrade_result( sd, true );
  19348. return;
  19349. }
  19350. // Check maximum refine requirement
  19351. if( item->refine > upgrade->maximumRefine ){
  19352. clif_laphine_upgrade_result( sd, true );
  19353. return;
  19354. }
  19355. // If no cards are allowed
  19356. if( !upgrade->cardsAllowed ){
  19357. for( int32 i = 0; i < MAX_SLOTS; i++ ){
  19358. if( item->card[i] != 0 ){
  19359. clif_laphine_upgrade_result( sd, true );
  19360. return;
  19361. }
  19362. }
  19363. }
  19364. // If random options are required
  19365. if( upgrade->requiredRandomOptions > 0 ){
  19366. int32 i;
  19367. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19368. if( item->option[i].id != 0 ){
  19369. break;
  19370. }
  19371. }
  19372. if( ( i + 1 ) < upgrade->requiredRandomOptions ){
  19373. clif_laphine_upgrade_result( sd, true );
  19374. return;
  19375. }
  19376. }
  19377. int16 index2 = pc_search_inventory( sd, sd->state.laphine_upgrade );
  19378. if( index2 < 0 ){
  19379. clif_laphine_upgrade_result( sd, true );
  19380. return;
  19381. }
  19382. if( ( sd->inventory_data[index2]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19383. if( pc_delitem( sd, index2, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19384. return;
  19385. }
  19386. }
  19387. // Log removal of item
  19388. log_pick_pc( sd, LOG_TYPE_LAPHINE, -1, item );
  19389. // Visually remove it from the client
  19390. clif_delitem( *sd, index, 1, 0 );
  19391. // Apply the random options
  19392. if( upgrade->randomOptionGroup != nullptr ){
  19393. upgrade->randomOptionGroup->apply( *item );
  19394. }
  19395. // Change the refine rate if needed
  19396. if( upgrade->resultRefine > 0 ){
  19397. // Absolute refine level change
  19398. item->refine = max( item->refine, upgrade->resultRefine );
  19399. }else if( upgrade->resultRefineMaximum > 0 ){
  19400. // If a minimum is specified it can also downgrade
  19401. if( upgrade->resultRefineMinimum ){
  19402. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, upgrade->resultRefineMaximum ) );
  19403. }else{
  19404. // Otherwise it can only be upgraded until the maximum, but not downgraded
  19405. item->refine = static_cast<uint8>( rnd_value<uint16>( item->refine, upgrade->resultRefineMaximum ) );
  19406. }
  19407. }else if( upgrade->resultRefineMinimum > 0 ){
  19408. // No maximum has been specified, so it can be anything between minimum and MAX_REFINE
  19409. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, MAX_REFINE ) );
  19410. }
  19411. // Log retrieving the item again -> with the new options
  19412. log_pick_pc( sd, LOG_TYPE_LAPHINE, 1, item );
  19413. // Make it visible for the client again
  19414. clif_additem( sd, index, 1, 0 );
  19415. // Open a preview of the item
  19416. clif_item_preview( sd, index );
  19417. // Tell the client we are done
  19418. clif_laphine_upgrade_result( sd, false );
  19419. #endif
  19420. }
  19421. void clif_enchantgrade_add( map_session_data& sd, uint16 index = UINT16_MAX, std::shared_ptr<s_enchantgradelevel> gradeLevel = nullptr ){
  19422. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19423. PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST*>( packet_buffer );
  19424. p->PacketType = HEADER_ZC_GRADE_ENCHANT_MATERIAL_LIST;
  19425. p->PacketLength = sizeof( *p );
  19426. if( index < UINT16_MAX ){
  19427. p->index = client_index( index );
  19428. p->success_chance = gradeLevel->chances[sd.inventory.u.items_inventory[index].refine] / 100;
  19429. p->blessing_info.id = client_nameid( gradeLevel->catalyst.item );
  19430. p->blessing_info.amount = gradeLevel->catalyst.amountPerStep;
  19431. p->blessing_info.max_blessing = gradeLevel->catalyst.maximumSteps;
  19432. p->blessing_info.bonus = gradeLevel->catalyst.chanceIncrease / 100;
  19433. // Not displayed by client
  19434. p->protect_itemid = 0;
  19435. p->protect_amount = 0;
  19436. int32 i = 0;
  19437. for( const auto& pair : gradeLevel->options ){
  19438. GRADE_ENCHANT_MATERIAL& mat = p->material_info[i];
  19439. std::shared_ptr<s_enchantgradeoption> option = pair.second;
  19440. mat.nameid = client_nameid( option->item );
  19441. mat.amount = option->amount;
  19442. mat.price = option->zeny;
  19443. mat.downgrade = option->downgrade_amount > 0;
  19444. mat.breakable = option->breaking_rate > 0;
  19445. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( mat ) );
  19446. i++;
  19447. }
  19448. }else{
  19449. p->index = -1;
  19450. p->success_chance = 0;
  19451. p->blessing_info.id = 0;
  19452. p->blessing_info.amount = 0;
  19453. p->blessing_info.max_blessing = 0;
  19454. p->blessing_info.bonus = 0;
  19455. p->protect_itemid = 0;
  19456. p->protect_amount = 0;
  19457. }
  19458. clif_send( p, p->PacketLength, &sd.bl, SELF );
  19459. #endif
  19460. }
  19461. void clif_parse_enchantgrade_add( int32 fd, map_session_data* sd ){
  19462. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19463. nullpo_retv( sd );
  19464. if( !sd->state.enchantgrade_open ){
  19465. return;
  19466. }
  19467. const PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT*>( RFIFOP( fd, 0 ) );
  19468. uint16 index = server_index( p->index );
  19469. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19470. return;
  19471. }
  19472. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19473. // Unsupported item type - no answer, because client should have actually prevented this request
  19474. if( enchantgrade == nullptr ){
  19475. return;
  19476. }
  19477. // Item can't be enhanced
  19478. if( !sd->inventory_data[index]->flag.gradable ){
  19479. return;
  19480. }
  19481. uint16 level = 0;
  19482. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19483. level = sd->inventory_data[index]->weapon_level;
  19484. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19485. level = sd->inventory_data[index]->armor_level;
  19486. }
  19487. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19488. // Cannot upgrade this weapon or armor level
  19489. if( enchantgradelevels == enchantgrade->levels.end() ){
  19490. clif_enchantgrade_add( *sd );
  19491. return;
  19492. }
  19493. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19494. // Cannot increase enchantgrade any further - no answer, because client should have actually prevented this request
  19495. if( enchantgradelevel == nullptr ){
  19496. return;
  19497. }
  19498. clif_enchantgrade_add( *sd, index, enchantgradelevel );
  19499. #endif
  19500. }
  19501. /// <summary>
  19502. /// Sends the result for trying to enchant an item
  19503. /// </summary>
  19504. /// <param name="sd">The player session</param>
  19505. /// <param name="index">The target item</param>
  19506. /// <param name="result">
  19507. /// 0= The grade has been successfully upgraded.
  19508. /// 1= Refinement failed.
  19509. /// 2= The refine level has decreased.
  19510. /// 3= Equipment destroyed.
  19511. /// 4= The equipment is protected.
  19512. /// </param>
  19513. void clif_enchantgrade_result( map_session_data& sd, uint16 index, e_enchantgrade_result result ){
  19514. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19515. PACKET_ZC_GRADE_ENCHANT_ACK p = {};
  19516. p.PacketType = HEADER_ZC_GRADE_ENCHANT_ACK;
  19517. p.index = client_index( index );
  19518. p.grade = sd.inventory.u.items_inventory[index].enchantgrade;
  19519. p.result = result;
  19520. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19521. #endif
  19522. }
  19523. void clif_enchantgrade_announce( map_session_data& sd, struct item& item, bool success ){
  19524. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19525. PACKET_ZC_GRADE_ENCHANT_BROADCAST_RESULT p = {};
  19526. p.packetType = HEADER_ZC_GRADE_ENCHANT_BROADCAST_RESULT;
  19527. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  19528. p.itemId = client_nameid( item.nameid );
  19529. p.grade = item.enchantgrade;
  19530. p.status = success;
  19531. clif_send( &p, sizeof( p ), nullptr, ALL_CLIENT );
  19532. #endif
  19533. }
  19534. void clif_parse_enchantgrade_start( int32 fd, map_session_data* sd ){
  19535. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19536. nullpo_retv( sd );
  19537. if( !sd->state.enchantgrade_open ){
  19538. return;
  19539. }
  19540. const PACKET_CZ_GRADE_ENCHANT_REQUEST* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_REQUEST*>( RFIFOP( fd, 0 ) );
  19541. uint16 index = server_index( p->index );
  19542. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19543. return;
  19544. }
  19545. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19546. // Unsupported item type - no answer
  19547. if( enchantgrade == nullptr ){
  19548. return;
  19549. }
  19550. uint16 level = 0;
  19551. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19552. level = sd->inventory_data[index]->weapon_level;
  19553. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19554. level = sd->inventory_data[index]->armor_level;
  19555. }
  19556. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19557. // Cannot upgrade this weapon or armor level - no answer
  19558. if( enchantgradelevels == enchantgrade->levels.end() ){
  19559. return;
  19560. }
  19561. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19562. // Cannot increase enchantgrade any further - no answer
  19563. if( enchantgradelevel == nullptr ){
  19564. return;
  19565. }
  19566. uint16 totalChance = enchantgradelevel->chances[sd->inventory.u.items_inventory[index].refine];
  19567. // No chance to increase the enchantgrade
  19568. if( totalChance == 0 ){
  19569. return;
  19570. }
  19571. std::shared_ptr<s_enchantgradeoption> option = util::map_find( enchantgradelevel->options, (uint16)p->material_index );
  19572. // Unknown option id - no answer
  19573. if( option == nullptr ){
  19574. return;
  19575. }
  19576. // Not enough zeny
  19577. if( sd->status.zeny < option->zeny ){
  19578. return;
  19579. }
  19580. uint16 steps = min( p->blessing_amount, enchantgradelevel->catalyst.maximumSteps );
  19581. std::unordered_map<uint16, uint16> requiredItems;
  19582. if( p->blessing_flag ){
  19583. // If the catalysator item is the same as the option item build the sum of amounts
  19584. if( enchantgradelevel->catalyst.item == option->item ){
  19585. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps + option->amount;
  19586. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19587. if( index < 0 ){
  19588. return;
  19589. }
  19590. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19591. return;
  19592. }
  19593. requiredItems[index] = amount;
  19594. }else{
  19595. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps;
  19596. // Check catalysator item
  19597. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19598. if( index < 0 ){
  19599. return;
  19600. }
  19601. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19602. return;
  19603. }
  19604. requiredItems[index] = amount;
  19605. // Check option item
  19606. index = pc_search_inventory( sd, option->item );
  19607. if( index < 0 ){
  19608. return;
  19609. }
  19610. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19611. return;
  19612. }
  19613. requiredItems[index] = option->amount;
  19614. }
  19615. totalChance += steps * enchantgradelevel->catalyst.chanceIncrease;
  19616. }else{
  19617. // Check option item
  19618. int16 index = pc_search_inventory( sd, option->item );
  19619. if( index < 0 ){
  19620. return;
  19621. }
  19622. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19623. return;
  19624. }
  19625. requiredItems[index] = option->amount;
  19626. }
  19627. // All items should be there, start deleting
  19628. for( const auto& pair : requiredItems ){
  19629. if( pc_delitem( sd, pair.first, pair.second, 0, 0, LOG_TYPE_ENCHANTGRADE ) != 0 ){
  19630. return;
  19631. }
  19632. }
  19633. if( pc_payzeny( sd, option->zeny, LOG_TYPE_ENCHANTGRADE ) > 0 ){
  19634. return;
  19635. }
  19636. if( rnd()%10000 < totalChance ){
  19637. // Log removal of item
  19638. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19639. // Increase enchantgrade
  19640. sd->inventory.u.items_inventory[index].enchantgrade = min( sd->inventory.u.items_inventory[index].enchantgrade + 1, MAX_ENCHANTGRADE );
  19641. // On successful enchantgrade increase the refine is reset
  19642. sd->inventory.u.items_inventory[index].refine = 0;
  19643. // Log retrieving the item again -> with the new refine and enchantgrade
  19644. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19645. // Show success
  19646. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_SUCCESS );
  19647. // Check if it has to be announced
  19648. if( enchantgradelevel->announceSuccess ){
  19649. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], true );
  19650. }
  19651. }else{
  19652. // Check if it has to be announced (has to be done before deleting the item from inventory)
  19653. if( enchantgradelevel->announceFail ){
  19654. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], false );
  19655. }
  19656. // Delete the item if it is breakable
  19657. if( option->breaking_rate > 0 && ( rnd() % 10000 ) < option->breaking_rate ){
  19658. // Delete the item
  19659. pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_ENCHANTGRADE );
  19660. // Show failure
  19661. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_BREAK );
  19662. // Downgrade the item if necessary
  19663. }else if( option->downgrade_amount > 0 ){
  19664. // Log removal of item
  19665. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19666. // Decrease refine level
  19667. sd->inventory.u.items_inventory[index].refine = cap_value( sd->inventory.u.items_inventory[index].refine - option->downgrade_amount, 0, MAX_REFINE );
  19668. // Log retrieving the item again -> with the new refine
  19669. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19670. // Show downgrade
  19671. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_DOWNGRADE );
  19672. // Only show failure, but dont do anything
  19673. }else{
  19674. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_FAILED );
  19675. }
  19676. }
  19677. #endif
  19678. }
  19679. void clif_parse_enchantgrade_close( int32 fd, map_session_data* sd ){
  19680. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19681. nullpo_retv( sd );
  19682. sd->state.enchantgrade_open = false;
  19683. #endif
  19684. }
  19685. void clif_reputation_type( map_session_data& sd, int64 type, int64 points ){
  19686. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19687. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19688. p->packetType = HEADER_ZC_REPUTE_INFO;
  19689. p->packetLength = sizeof( *p );
  19690. p->success = true;
  19691. PACKET_ZC_REPUTE_INFO_sub& entry = p->list[0];
  19692. entry.type = type;
  19693. entry.points = points;
  19694. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  19695. clif_send( p, p->packetLength, &sd.bl, SELF );
  19696. #endif
  19697. }
  19698. void clif_reputation_list( map_session_data& sd ){
  19699. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19700. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19701. p->packetType = HEADER_ZC_REPUTE_INFO;
  19702. p->packetLength = sizeof( *p );
  19703. p->success = true;
  19704. size_t index = 0;
  19705. for( const auto& entry : reputation_db ){
  19706. std::shared_ptr<s_reputation> reputation = entry.second;
  19707. PACKET_ZC_REPUTE_INFO_sub& list_entry = p->list[index];
  19708. list_entry.type = reputation->id;
  19709. list_entry.points = pc_readreg2( &sd, reputation->variable.c_str() );
  19710. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( list_entry ) );
  19711. index++;
  19712. }
  19713. clif_send( p, p->packetLength, &sd.bl, SELF );
  19714. #endif
  19715. }
  19716. void clif_item_reform_open( map_session_data& sd, t_itemid item ){
  19717. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19718. PACKET_ZC_OPEN_REFORM_UI p = {};
  19719. p.PacketType = HEADER_ZC_OPEN_REFORM_UI;
  19720. p.ITID = item;
  19721. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19722. sd.state.item_reform = item;
  19723. #endif
  19724. }
  19725. void clif_parse_item_reform_close( int32 fd, map_session_data* sd ){
  19726. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19727. sd->state.item_reform = 0;
  19728. #endif
  19729. }
  19730. void clif_item_reform_result( map_session_data& sd, uint16 index, uint8 result ){
  19731. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19732. PACKET_ZC_ITEM_REFORM_ACK p = {};
  19733. p.PacketType = HEADER_ZC_ITEM_REFORM_ACK;
  19734. p.index = client_index( index );
  19735. p.result = result;
  19736. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19737. if( result == 0 ){
  19738. // Client closes the window on success
  19739. sd.state.item_reform = 0;
  19740. }
  19741. #endif
  19742. }
  19743. void clif_parse_item_reform_start( int32 fd, map_session_data* sd ){
  19744. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19745. // Not opened
  19746. if( sd->state.item_reform == 0 ){
  19747. return;
  19748. }
  19749. const PACKET_CZ_ITEM_REFORM* p = reinterpret_cast<PACKET_CZ_ITEM_REFORM*>( RFIFOP( fd, 0 ) );
  19750. // Item mismatch
  19751. if( p->ITID != sd->state.item_reform ){
  19752. return;
  19753. }
  19754. uint16 index = server_index( p->index );
  19755. if( index >= MAX_INVENTORY ){
  19756. return;
  19757. }
  19758. if( sd->inventory_data[index] == nullptr ){
  19759. return;
  19760. }
  19761. std::shared_ptr<s_item_reform> reform = item_reform_db.find( sd->state.item_reform );
  19762. if( reform == nullptr ){
  19763. return;
  19764. }
  19765. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19766. std::shared_ptr<s_item_reform_base> base = util::umap_find( reform->base_items, selected_item.nameid );
  19767. if( base == nullptr ){
  19768. return;
  19769. }
  19770. // If target item is not identified
  19771. if( selected_item.identify == 0 ){
  19772. return;
  19773. }
  19774. // If target item is equipped
  19775. if( selected_item.equip != 0 ){
  19776. return;
  19777. }
  19778. // Check minimum refine requirement
  19779. if( selected_item.refine < base->minimumRefine ){
  19780. return;
  19781. }
  19782. // Check maximum refine requirement
  19783. if( selected_item.refine > base->maximumRefine ){
  19784. return;
  19785. }
  19786. // If no cards are allowed
  19787. if( !base->cardsAllowed ){
  19788. for( int32 i = 0; i < MAX_SLOTS; i++ ){
  19789. if( selected_item.card[i] != 0 ){
  19790. return;
  19791. }
  19792. }
  19793. }
  19794. // If random options are required
  19795. if( base->requiredRandomOptions > 0 ){
  19796. int32 i;
  19797. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19798. if( selected_item.option[i].id != 0 ){
  19799. break;
  19800. }
  19801. }
  19802. if( ( i + 1 ) < base->requiredRandomOptions ){
  19803. return;
  19804. }
  19805. }
  19806. std::unordered_map<uint16, uint16> materials;
  19807. // Check if all materials exist
  19808. for( const auto& material : base->materials ){
  19809. int16 material_index = pc_search_inventory( sd, material.first );
  19810. if( material_index < 0 ){
  19811. return;
  19812. }
  19813. if( sd->inventory.u.items_inventory[material_index].amount < material.second ){
  19814. return;
  19815. }
  19816. materials[material_index] = material.second;
  19817. }
  19818. // Remove the material
  19819. for( const auto& material : materials ){
  19820. if( pc_delitem( sd, material.first, material.second, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19821. return;
  19822. }
  19823. }
  19824. // If triggered from item
  19825. if( sd->itemid == sd->state.item_reform && pc_delitem( sd, sd->itemindex, 1, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19826. return;
  19827. }
  19828. // Log removal of item
  19829. log_pick_pc( sd, LOG_TYPE_REFORM, -1, &selected_item );
  19830. // Visually remove it from the client
  19831. clif_delitem( *sd, index, 1, 0 );
  19832. // Apply the random options
  19833. if( base->randomOptionGroup != nullptr ){
  19834. base->randomOptionGroup->apply( selected_item );
  19835. }
  19836. // Change the refine rate if needed
  19837. if( base->refineChange != 0 ){
  19838. selected_item.refine = cap_value( selected_item.refine + base->refineChange, 0, MAX_REFINE );
  19839. }
  19840. // Remove all cards and socket enchants
  19841. if( base->clearSlots ){
  19842. for( int32 i = 0; i < MAX_SLOTS; i++ ){
  19843. selected_item.card[i] = 0;
  19844. }
  19845. }
  19846. // Remove the current enchantgrade
  19847. if( base->removeEnchantgrade ){
  19848. selected_item.enchantgrade = 0;
  19849. }
  19850. // Finally change the item id
  19851. selected_item.nameid = base->resultItemId;
  19852. // Link inventory data cache to the new item
  19853. sd->inventory_data[index] = itemdb_search( base->resultItemId );
  19854. // Log retrieving the item again -> with the new options
  19855. log_pick_pc( sd, LOG_TYPE_REFORM, 1, &selected_item );
  19856. // Make it visible for the client again
  19857. clif_additem( sd, index, 1, 0 );
  19858. clif_item_reform_result( *sd, index, 0 );
  19859. #endif
  19860. }
  19861. void clif_enchantwindow_open( map_session_data& sd, uint64 clientLuaIndex ){
  19862. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19863. // Hardcoded clientside check
  19864. if( sd.weight > ( ( sd.max_weight * 70 ) / 100 ) ){
  19865. clif_msg_color( &sd, MSI_ENCHANT_FAILED_OVER_WEIGHT, color_table[COLOR_RED] );
  19866. sd.state.item_enchant_index = 0;
  19867. return;
  19868. }
  19869. PACKET_ZC_UI_OPEN_V3 p = {};
  19870. p.packetType = HEADER_ZC_UI_OPEN_V3;
  19871. p.type = OUT_UI_ENCHANT;
  19872. p.data = clientLuaIndex;
  19873. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19874. sd.state.item_enchant_index = clientLuaIndex;
  19875. #endif
  19876. }
  19877. void clif_enchantwindow_result( map_session_data& sd, bool success, t_itemid enchant = 0 ){
  19878. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19879. PACKET_ZC_RESPONSE_ENCHANT p = {};
  19880. p.PacketType = HEADER_ZC_RESPONSE_ENCHANT;
  19881. if( success ){
  19882. p.msgId = MSI_ENCHANT_SUCCESS;
  19883. }else{
  19884. p.msgId = MSI_ENCHANT_FAILED;
  19885. }
  19886. p.ITID = enchant;
  19887. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19888. sd.state.item_enchant_index = 0;
  19889. #endif
  19890. }
  19891. bool clif_parse_enchant_basecheck( struct item& selected_item, std::shared_ptr<s_item_enchant> enchant ){
  19892. if( selected_item.equip != 0 ){
  19893. return false;
  19894. }
  19895. if( selected_item.equipSwitch != 0 ){
  19896. return false;
  19897. }
  19898. if( selected_item.attribute != 0 ){
  19899. return false;
  19900. }
  19901. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  19902. return false;
  19903. }
  19904. if( selected_item.refine < enchant->minimumRefine ){
  19905. return false;
  19906. }
  19907. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  19908. return false;
  19909. }
  19910. if( !enchant->allowRandomOptions ){
  19911. for( const s_item_randomoption& option : selected_item.option ){
  19912. if( option.id != 0 ){
  19913. return false;
  19914. }
  19915. }
  19916. }
  19917. return true;
  19918. }
  19919. void clif_parse_enchantwindow_general( int32 fd, map_session_data* sd ){
  19920. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19921. const PACKET_CZ_REQUEST_RANDOM_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RANDOM_ENCHANT*>( RFIFOP( fd, 0 ) );
  19922. if( sd->state.item_enchant_index != p->enchant_group ){
  19923. return;
  19924. }
  19925. uint16 index = server_index( p->index );
  19926. if( index >= MAX_INVENTORY ){
  19927. return;
  19928. }
  19929. if( sd->inventory_data[index] == nullptr ){
  19930. return;
  19931. }
  19932. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19933. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19934. if( enchant == nullptr ){
  19935. return;
  19936. }
  19937. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19938. return;
  19939. }
  19940. uint16 slot = MAX_SLOTS;
  19941. for( uint16 nextSlot : enchant->order ){
  19942. if( selected_item.card[nextSlot] == 0 ){
  19943. slot = nextSlot;
  19944. break;
  19945. }
  19946. }
  19947. if( slot == MAX_SLOTS ){
  19948. return;
  19949. }
  19950. if( slot < sd->inventory_data[index]->slots ){
  19951. return;
  19952. }
  19953. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19954. if( enchant_slot == nullptr ){
  19955. return;
  19956. }
  19957. std::shared_ptr<s_item_enchant_normal> enchants_for_enchantgrade = util::umap_find( enchant_slot->normal.enchants, (uint16)selected_item.enchantgrade );
  19958. if( enchants_for_enchantgrade == nullptr ){
  19959. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 829 ), false, SELF); // Enchanting is not possible for your item's enchant grade.
  19960. clif_enchantwindow_result( *sd, false );
  19961. return;
  19962. }
  19963. if( sd->status.zeny < enchant_slot->normal.zeny ){
  19964. return;
  19965. }
  19966. std::unordered_map<uint16, uint16> materials;
  19967. for( const auto& entry : enchant_slot->normal.materials ){
  19968. int16 idx = pc_search_inventory( sd, entry.first );
  19969. if( idx < 0 ){
  19970. return;
  19971. }
  19972. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19973. return;
  19974. }
  19975. materials[idx] = entry.second;
  19976. }
  19977. if( pc_payzeny( sd, enchant_slot->normal.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19978. return;
  19979. }
  19980. for( const auto& entry : materials ){
  19981. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19982. return;
  19983. }
  19984. }
  19985. uint32 chance = enchant_slot->normal.chance;
  19986. for( int32 i = 0; i <= MAX_ENCHANTGRADE; i++ ){
  19987. chance += enchant_slot->normal.enchantgradeChanceIncrease[i];
  19988. }
  19989. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  19990. clif_enchantwindow_result( *sd, false );
  19991. return;
  19992. }
  19993. // Log removal of item
  19994. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19995. size_t maximum = 3 * enchant_slot->normal.enchants.size();
  19996. bool enchanted = false;
  19997. for( int32 i = 0; i < maximum; i++ ){
  19998. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  19999. if( rnd_value( 0, 10000 ) < normal_enchant->chance ){
  20000. selected_item.card[slot] = normal_enchant->item_id;
  20001. enchanted = true;
  20002. break;
  20003. }
  20004. }
  20005. if( !enchanted ){
  20006. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  20007. selected_item.card[slot] = normal_enchant->item_id;
  20008. }
  20009. // Log retrieving the item again -> with the new enchant
  20010. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20011. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  20012. #endif
  20013. }
  20014. void clif_parse_enchantwindow_perfect( int32 fd, map_session_data* sd ){
  20015. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20016. const PACKET_CZ_REQUEST_PERFECT_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_PERFECT_ENCHANT*>( RFIFOP( fd, 0 ) );
  20017. if( sd->state.item_enchant_index != p->enchant_group ){
  20018. return;
  20019. }
  20020. uint16 index = server_index( p->index );
  20021. if( index >= MAX_INVENTORY ){
  20022. return;
  20023. }
  20024. if( sd->inventory_data[index] == nullptr ){
  20025. return;
  20026. }
  20027. struct item& selected_item = sd->inventory.u.items_inventory[index];
  20028. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  20029. if( enchant == nullptr ){
  20030. return;
  20031. }
  20032. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  20033. return;
  20034. }
  20035. uint16 slot = MAX_SLOTS;
  20036. for( uint16 nextSlot : enchant->order ){
  20037. if( selected_item.card[nextSlot] == 0 ){
  20038. slot = nextSlot;
  20039. break;
  20040. }
  20041. }
  20042. if( slot == MAX_SLOTS ){
  20043. return;
  20044. }
  20045. if( slot < sd->inventory_data[index]->slots ){
  20046. return;
  20047. }
  20048. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  20049. if( enchant_slot == nullptr ){
  20050. return;
  20051. }
  20052. std::shared_ptr<s_item_enchant_perfect> perfect_enchant = util::umap_find( enchant_slot->perfect.enchants, p->ITID );
  20053. if( perfect_enchant == nullptr ){
  20054. return;
  20055. }
  20056. if( sd->status.zeny < perfect_enchant->zeny ){
  20057. return;
  20058. }
  20059. std::unordered_map<uint16, uint16> materials;
  20060. for( const auto& entry : perfect_enchant->materials ){
  20061. int16 idx = pc_search_inventory( sd, entry.first );
  20062. if( idx < 0 ){
  20063. return;
  20064. }
  20065. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20066. return;
  20067. }
  20068. materials[idx] = entry.second;
  20069. }
  20070. if( pc_payzeny( sd, perfect_enchant->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20071. return;
  20072. }
  20073. for( const auto& entry : materials ){
  20074. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20075. return;
  20076. }
  20077. }
  20078. // Log removal of item
  20079. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20080. selected_item.card[slot] = perfect_enchant->item_id;
  20081. // Log retrieving the item again -> with the new enchant
  20082. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20083. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  20084. #endif
  20085. }
  20086. void clif_parse_enchantwindow_upgrade( int32 fd, map_session_data* sd ){
  20087. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20088. const PACKET_CZ_REQUEST_UPGRADE_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_UPGRADE_ENCHANT*>( RFIFOP( fd, 0 ) );
  20089. if( sd->state.item_enchant_index != p->enchant_group ){
  20090. return;
  20091. }
  20092. uint16 index = server_index( p->index );
  20093. if( index >= MAX_INVENTORY ){
  20094. return;
  20095. }
  20096. if( sd->inventory_data[index] == nullptr ){
  20097. return;
  20098. }
  20099. struct item& selected_item = sd->inventory.u.items_inventory[index];
  20100. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  20101. if( enchant == nullptr ){
  20102. return;
  20103. }
  20104. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  20105. return;
  20106. }
  20107. uint16 slot = p->slot;
  20108. if( slot >= MAX_SLOTS ){
  20109. return;
  20110. }
  20111. if( slot < sd->inventory_data[index]->slots ){
  20112. return;
  20113. }
  20114. if( selected_item.card[slot] == 0 ){
  20115. return;
  20116. }
  20117. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  20118. if( enchant_slot == nullptr ){
  20119. return;
  20120. }
  20121. std::shared_ptr<s_item_enchant_upgrade> upgrade = util::umap_find( enchant_slot->upgrade.enchants, selected_item.card[slot] );
  20122. if( upgrade == nullptr ){
  20123. return;
  20124. }
  20125. if( sd->status.zeny < upgrade->zeny ){
  20126. return;
  20127. }
  20128. std::unordered_map<uint16, uint16> materials;
  20129. for( const auto& entry : upgrade->materials ){
  20130. int16 idx = pc_search_inventory( sd, entry.first );
  20131. if( idx < 0 ){
  20132. return;
  20133. }
  20134. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20135. return;
  20136. }
  20137. materials[idx] = entry.second;
  20138. }
  20139. if( pc_payzeny( sd, upgrade->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20140. return;
  20141. }
  20142. for( const auto& entry : materials ){
  20143. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20144. return;
  20145. }
  20146. }
  20147. // Log removal of item
  20148. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20149. selected_item.card[slot] = upgrade->upgrade_item_id;
  20150. // Log retrieving the item again -> with the new enchant
  20151. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20152. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  20153. #endif
  20154. }
  20155. void clif_parse_enchantwindow_reset( int32 fd, map_session_data* sd ){
  20156. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20157. const PACKET_CZ_REQUEST_RESET_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RESET_ENCHANT*>( RFIFOP( fd, 0 ) );
  20158. if( sd->state.item_enchant_index != p->enchant_group ){
  20159. return;
  20160. }
  20161. uint16 index = server_index( p->index );
  20162. if( index >= MAX_INVENTORY ){
  20163. return;
  20164. }
  20165. if( sd->inventory_data[index] == nullptr ){
  20166. return;
  20167. }
  20168. struct item& selected_item = sd->inventory.u.items_inventory[index];
  20169. if( selected_item.equip != 0 ){
  20170. return;
  20171. }
  20172. if( selected_item.equipSwitch != 0 ){
  20173. return;
  20174. }
  20175. if( selected_item.attribute != 0 ){
  20176. return;
  20177. }
  20178. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  20179. if( enchant == nullptr ){
  20180. return;
  20181. }
  20182. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  20183. return;
  20184. }
  20185. if( selected_item.refine < enchant->minimumRefine ){
  20186. return;
  20187. }
  20188. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  20189. return;
  20190. }
  20191. bool is_enchanted = false;
  20192. for( int32 i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20193. if( selected_item.card[i] != 0 ){
  20194. is_enchanted = true;
  20195. break;
  20196. }
  20197. }
  20198. if( !is_enchanted ){
  20199. return;
  20200. }
  20201. if( sd->status.zeny < enchant->reset.zeny ){
  20202. return;
  20203. }
  20204. std::unordered_map<uint16, uint16> materials;
  20205. for( const auto& entry : enchant->reset.materials ){
  20206. int16 idx = pc_search_inventory( sd, entry.first );
  20207. if( idx < 0 ){
  20208. return;
  20209. }
  20210. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20211. return;
  20212. }
  20213. materials[idx] = entry.second;
  20214. }
  20215. if( pc_payzeny( sd, enchant->reset.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20216. return;
  20217. }
  20218. for( const auto& entry : materials ){
  20219. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20220. return;
  20221. }
  20222. }
  20223. uint32 chance = enchant->reset.chance;
  20224. if( chance == 0 ){
  20225. return;
  20226. }
  20227. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  20228. clif_enchantwindow_result( *sd, false );
  20229. return;
  20230. }
  20231. // Log removal of item
  20232. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20233. for( int32 i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20234. selected_item.card[i] = 0;
  20235. }
  20236. // Log retrieving the item again -> with the new enchant
  20237. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20238. clif_enchantwindow_result( *sd, true );
  20239. #endif
  20240. }
  20241. void clif_parse_enchantwindow_close( int32 fd, map_session_data* sd ){
  20242. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20243. sd->state.item_enchant_index = 0;
  20244. #endif
  20245. }
  20246. void clif_parse_itempackage_select( int32 fd, map_session_data* sd ){
  20247. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220316
  20248. const PACKET_CZ_USE_PACKAGEITEM* p = reinterpret_cast<PACKET_CZ_USE_PACKAGEITEM*>( RFIFOP( fd, 0 ) );
  20249. if( p->AID != sd->status.account_id ){
  20250. return;
  20251. }
  20252. uint16 index = server_index( p->index );
  20253. if( index >= MAX_INVENTORY ){
  20254. return;
  20255. }
  20256. if( sd->inventory_data[index] == nullptr ){
  20257. return;
  20258. }
  20259. if( sd->inventory_data[index]->nameid != p->itemID ){
  20260. return;
  20261. }
  20262. if( sd->inventory_data[index]->elv > sd->status.base_level ){
  20263. return;
  20264. }
  20265. std::shared_ptr<s_item_package> package = item_package_db.find( p->itemID );
  20266. if( package == nullptr ){
  20267. return;
  20268. }
  20269. std::shared_ptr<s_item_package_group> group = util::umap_find( package->groups, p->BoxIndex );
  20270. if( group == nullptr ){
  20271. return;
  20272. }
  20273. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_PACKAGE ) != 0 ){
  20274. return;
  20275. }
  20276. for( const auto& entry : group->items ){
  20277. struct item item = {};
  20278. item.nameid = entry.second->item_id;
  20279. item.identify = 1;
  20280. item.refine = (char)entry.second->refine;
  20281. if( entry.second->rentalhours ){
  20282. item.expire_time = (uint32)( time( nullptr ) + entry.second->rentalhours * 3600 );
  20283. }
  20284. // Check if it is a pet egg
  20285. std::shared_ptr<s_pet_db> pet = pet_db_search( item.nameid, PET_EGG );
  20286. if( pet != nullptr ){
  20287. for( int32 i = 0; i < entry.second->amount; i++ ){
  20288. pet_create_egg( sd, item.nameid );
  20289. }
  20290. }else if( entry.second->amount > 1 && ( !itemdb_isstackable( item.nameid ) || item.expire_time > 0 ) ){
  20291. for( int32 i = 0; i < entry.second->amount; i++ ){
  20292. // New random options on each iteration
  20293. if( entry.second->randomOptionGroup != nullptr ){
  20294. entry.second->randomOptionGroup->apply( item );
  20295. }
  20296. pc_additem( sd, &item, 1, LOG_TYPE_PACKAGE );
  20297. }
  20298. }else{
  20299. if( entry.second->randomOptionGroup != nullptr ){
  20300. entry.second->randomOptionGroup->apply( item );
  20301. }
  20302. pc_additem( sd, &item, entry.second->amount, LOG_TYPE_PACKAGE );
  20303. }
  20304. }
  20305. #endif
  20306. }
  20307. void clif_broadcast_refine_result(map_session_data& sd, t_itemid itemId, int8 level, bool success)
  20308. {
  20309. #if PACKETVER_MAIN_NUM >= 20170906 || PACKETVER_RE_NUM >= 20170830 || defined(PACKETVER_ZERO)
  20310. PACKET_ZC_BROADCAST_ITEMREFINING_RESULT p{};
  20311. p.packetType = HEADER_ZC_BROADCAST_ITEMREFINING_RESULT;
  20312. p.itemId = itemId;
  20313. p.refine_level = level;
  20314. p.status = (int8)success;
  20315. if (battle_config.broadcast_hide_name) {
  20316. std::string dispname = clif_hide_name(sd.status.name);
  20317. safestrncpy(p.name, dispname.c_str(), sizeof(p.name));
  20318. }
  20319. else {
  20320. safestrncpy(p.name, sd.status.name, sizeof(p.name));
  20321. }
  20322. clif_send(&p, sizeof(p), &sd.bl, ALL_CLIENT);
  20323. #endif
  20324. }
  20325. void clif_parse_captcha_register(int32 fd, map_session_data *sd) {
  20326. #if PACKETVER >= 20160316
  20327. nullpo_retv(sd);
  20328. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20329. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20330. return;
  20331. }
  20332. const PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20333. pc_macro_captcha_register(*sd, p->imageSize, p->answer);
  20334. #endif
  20335. }
  20336. void clif_captcha_upload_request(map_session_data &sd) {
  20337. #if PACKETVER >= 20160330
  20338. PACKET_ZC_ACK_UPLOAD_MACRO_DETECTOR p = {};
  20339. p.PacketType = HEADER_ZC_ACK_UPLOAD_MACRO_DETECTOR;
  20340. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20341. if (sd.captcha_upload.cd != nullptr) {
  20342. p.captchaFlag = 0;
  20343. } else {
  20344. p.captchaFlag = 1;
  20345. }
  20346. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20347. #endif
  20348. }
  20349. void clif_parse_captcha_upload(int32 fd, map_session_data *sd) {
  20350. #if PACKETVER >= 20160316
  20351. nullpo_retv(sd);
  20352. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20353. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20354. return;
  20355. }
  20356. const PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20357. int16 upload_size = p->PacketLength - sizeof( * p );
  20358. if (upload_size < 1 || upload_size > MAX_CAPTCHA_CHUNK_SIZE)
  20359. return;
  20360. if (sd->captcha_upload.upload_size + upload_size > sd->captcha_upload.cd->image_size)
  20361. return;
  20362. pc_macro_captcha_register_upload(*sd, upload_size, p->imageData);
  20363. #endif
  20364. }
  20365. void clif_captcha_upload_end(map_session_data &sd) {
  20366. #if PACKETVER >= 20160330
  20367. PACKET_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA p = {};
  20368. p.PacketType = HEADER_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA;
  20369. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20370. #endif
  20371. }
  20372. void clif_parse_captcha_preview_request(int32 fd, map_session_data *sd) {
  20373. #if PACKETVER >= 20160323
  20374. nullpo_retv(sd);
  20375. if (!(pc_has_permission(sd, PC_PERM_MACRO_REGISTER) && pc_has_permission(sd, PC_PERM_MACRO_DETECT))) {
  20376. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20377. return;
  20378. }
  20379. const PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20380. clif_captcha_preview_response(*sd, captcha_db.find(p->captchaID));
  20381. #endif
  20382. }
  20383. void clif_captcha_preview_response(map_session_data &sd, std::shared_ptr<s_captcha_data> cd) {
  20384. #if PACKETVER >= 20160330
  20385. PACKET_ZC_ACK_PREVIEW_MACRO_DETECTOR p = {};
  20386. p.PacketType = HEADER_ZC_ACK_PREVIEW_MACRO_DETECTOR;
  20387. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20388. if (cd == nullptr) {
  20389. p.captchaFlag = 1;
  20390. p.imageSize = 0;
  20391. } else {
  20392. p.captchaFlag = 0;
  20393. p.imageSize = cd->image_size;
  20394. }
  20395. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20396. if (cd != nullptr) {
  20397. for (uint16 offset = 0; offset < cd->image_size;) {
  20398. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20399. PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20400. p2->PacketType = HEADER_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA;
  20401. p2->PacketLength = sizeof( *p2 );
  20402. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20403. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20404. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20405. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20406. offset += chunk_size;
  20407. }
  20408. }
  20409. #endif
  20410. }
  20411. void clif_macro_detector_request(map_session_data &sd) {
  20412. #if PACKETVER >= 20160330
  20413. std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  20414. if (cd == nullptr) {
  20415. return;
  20416. }
  20417. // Send preview initialization request to the client.
  20418. PACKET_ZC_APPLY_MACRO_DETECTOR p = {};
  20419. p.PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR;
  20420. p.imageSize = cd->image_size;
  20421. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20422. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20423. for (uint16 offset = 0; offset < cd->image_size;) {
  20424. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20425. PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20426. p2->PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR_CAPTCHA;
  20427. p2->PacketLength = sizeof( *p2 );
  20428. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20429. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20430. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20431. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20432. offset += chunk_size;
  20433. }
  20434. #endif
  20435. }
  20436. void clif_macro_detector_request_show(map_session_data &sd) {
  20437. #if PACKETVER >= 20160330
  20438. PACKET_ZC_REQ_ANSWER_MACRO_DETECTOR p = {};
  20439. p.PacketType = HEADER_ZC_REQ_ANSWER_MACRO_DETECTOR;
  20440. p.retryCount = sd.macro_detect.retry;
  20441. p.timeout = battle_config.macro_detection_timeout;
  20442. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20443. #endif
  20444. }
  20445. void clif_parse_macro_detector_download_ack(int32 fd, map_session_data *sd) {
  20446. #if PACKETVER >= 20160316
  20447. nullpo_retv(sd);
  20448. if (sd->macro_detect.retry != 0) {
  20449. //const PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20450. clif_macro_detector_request_show(*sd);
  20451. }
  20452. #endif
  20453. }
  20454. void clif_parse_macro_detector_answer(int32 fd, map_session_data *sd) {
  20455. #if PACKETVER >= 20160316
  20456. nullpo_retv(sd);
  20457. const PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20458. pc_macro_detector_process_answer(*sd, p->answer);
  20459. #endif
  20460. }
  20461. void clif_macro_detector_status(map_session_data &sd, e_macro_detect_status stype) {
  20462. #if PACKETVER >= 20160330
  20463. PACKET_ZC_CLOSE_MACRO_DETECTOR p = {};
  20464. p.PacketType = HEADER_ZC_CLOSE_MACRO_DETECTOR;
  20465. p.status = stype;
  20466. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20467. #endif
  20468. }
  20469. void clif_parse_macro_reporter_select(int32 fd, map_session_data *sd) {
  20470. #if PACKETVER >= 20160330
  20471. nullpo_retv(sd);
  20472. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20473. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20474. return;
  20475. }
  20476. const PACKET_CZ_REQ_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_CZ_REQ_PLAYER_AID_IN_RANGE*>( RFIFOP( fd, 0 ) );
  20477. pc_macro_reporter_area_select(*sd, p->xPos, p->yPos, p->RadiusRange);
  20478. #endif
  20479. }
  20480. void clif_macro_reporter_select(map_session_data &sd, const std::vector<uint32> &aid_list) {
  20481. #if PACKETVER >= 20160330
  20482. PACKET_ZC_ACK_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_ZC_ACK_PLAYER_AID_IN_RANGE*>( packet_buffer );
  20483. p->PacketType = HEADER_ZC_ACK_PLAYER_AID_IN_RANGE;
  20484. p->PacketLength = sizeof( *p );
  20485. for (size_t i = 0; i < aid_list.size(); i++){
  20486. p->AID[i] = aid_list[static_cast<int32>(i)];
  20487. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->AID[0] ) );
  20488. }
  20489. clif_send(p, p->PacketLength, &sd.bl, SELF);
  20490. #endif
  20491. }
  20492. void clif_parse_macro_reporter_ack(int32 fd, map_session_data *sd) {
  20493. #if PACKETVER >= 20160316
  20494. nullpo_retv(sd);
  20495. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20496. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20497. return;
  20498. }
  20499. const PACKET_CZ_REQ_APPLY_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20500. map_session_data *tsd = map_id2sd(p->AID);
  20501. if (tsd == nullptr) {
  20502. clif_displaymessage(fd, msg_txt(sd, 3)); // Character not found.
  20503. return;
  20504. }
  20505. if (tsd->macro_detect.retry != 0) {
  20506. clif_macro_reporter_status(*sd, MCR_INPROGRESS);
  20507. return;
  20508. }
  20509. if (captcha_db.empty()) {
  20510. clif_macro_reporter_status(*sd, MCR_NO_DATA);
  20511. return;
  20512. }
  20513. pc_macro_reporter_process(*tsd, sd->status.account_id);
  20514. clif_macro_reporter_status(*sd, MCR_MONITORING);
  20515. #endif
  20516. }
  20517. void clif_macro_reporter_status(map_session_data &sd, e_macro_report_status stype) {
  20518. #if PACKETVER >= 20160330
  20519. PACKET_ZC_ACK_APPLY_MACRO_DETECTOR p = {};
  20520. p.PacketType = HEADER_ZC_ACK_APPLY_MACRO_DETECTOR;
  20521. p.status = stype;
  20522. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20523. #endif
  20524. }
  20525. void clif_dynamicnpc_result( map_session_data& sd, e_dynamicnpc_result result ){
  20526. #if PACKETVER_MAIN_NUM >= 20140430 || PACKETVER_RE_NUM >= 20140430 || defined(PACKETVER_ZERO)
  20527. PACKET_ZC_DYNAMICNPC_CREATE_RESULT p = {};
  20528. p.PacketType = HEADER_ZC_DYNAMICNPC_CREATE_RESULT;
  20529. p.result = result;
  20530. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20531. #endif
  20532. }
  20533. void clif_partybooking_ask( map_session_data* sd, map_session_data* joining_sd ){
  20534. #if PACKETVER >= 20191204
  20535. PACKET_ZC_PARTY_REQ_MASTER_TO_JOIN p = {};
  20536. p.packetType = HEADER_ZC_PARTY_REQ_MASTER_TO_JOIN;
  20537. p.CID = joining_sd->status.char_id;
  20538. p.AID = joining_sd->status.account_id;
  20539. safestrncpy( p.name, joining_sd->status.name, NAME_LENGTH );
  20540. p.x = joining_sd->status.base_level;
  20541. p.y = joining_sd->status.class_;
  20542. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20543. #endif
  20544. }
  20545. void clif_parse_partybooking_join( int32 fd, map_session_data* sd ){
  20546. #if PACKETVER >= 20191204
  20547. const PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20548. // Character is already in a party
  20549. if( sd->status.party_id != 0 ){
  20550. return;
  20551. }
  20552. map_session_data* tsd = map_charid2sd( p->CID );
  20553. // Target player is offline
  20554. if( tsd == nullptr ){
  20555. return;
  20556. }
  20557. if( tsd->status.account_id != p->AID ){
  20558. return;
  20559. }
  20560. struct s_party_booking_requirement requirement;
  20561. if( !party_booking_load( tsd->status.account_id, tsd->status.char_id, &requirement ) ){
  20562. return;
  20563. }
  20564. if( sd->status.base_level < requirement.minimum_level ){
  20565. return;
  20566. }
  20567. if( sd->status.base_level > requirement.maximum_level ){
  20568. return;
  20569. }
  20570. // Already requested to join this party
  20571. if( util::vector_exists( tsd->party_booking_requests, sd->status.char_id ) ){
  20572. return;
  20573. }
  20574. // Store information that the player tried to join the party
  20575. tsd->party_booking_requests.push_back( sd->status.char_id );
  20576. clif_partybooking_ask( tsd, sd );
  20577. #endif
  20578. }
  20579. void clif_partybooking_reply( map_session_data* sd, map_session_data* party_leader_sd, bool accepted ){
  20580. #if PACKETVER >= 20191204
  20581. PACKET_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER p = {};
  20582. if( party_leader_sd->status.party_id == 0 ){
  20583. return;
  20584. }
  20585. struct party_data* party = party_search( party_leader_sd->status.party_id );
  20586. if( party == nullptr ){
  20587. return;
  20588. }
  20589. p.packetType = HEADER_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER;
  20590. safestrncpy( p.player_name, party_leader_sd->status.name, NAME_LENGTH );
  20591. safestrncpy( p.party_name, party->party.name, NAME_LENGTH );
  20592. p.AID = party_leader_sd->status.account_id;
  20593. p.refused = !accepted;
  20594. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20595. #endif
  20596. }
  20597. void clif_parse_partybooking_reply( int32 fd, map_session_data* sd ){
  20598. #if PACKETVER >= 20191204
  20599. const PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20600. map_session_data* tsd = map_charid2sd( p->CID );
  20601. // Target player is offline
  20602. if( tsd == nullptr ){
  20603. return;
  20604. }
  20605. if( tsd->status.account_id != p->AID ){
  20606. return;
  20607. }
  20608. // Check if the player even requested to join the party
  20609. if( !util::vector_exists( sd->party_booking_requests, p->CID ) ){
  20610. return;
  20611. }
  20612. util::vector_erase_if_exists( sd->party_booking_requests, p->CID );
  20613. // Only party leaders can reply
  20614. if( !party_isleader( sd ) ){
  20615. return;
  20616. }
  20617. struct party_data* party = party_search( sd->status.party_id );
  20618. if( party == nullptr ){
  20619. return;
  20620. }
  20621. if( party->instance_id > 0 && battle_config.instance_block_invite ){
  20622. return;
  20623. }
  20624. if( p->accept ){
  20625. party_join( *tsd, sd->status.party_id );
  20626. }
  20627. clif_partybooking_reply( tsd, sd, p->accept );
  20628. #endif
  20629. }
  20630. void clif_parse_reset_skill( int32 fd, map_session_data* sd ){
  20631. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220203
  20632. const PACKET_CZ_RESET_SKILL* p = reinterpret_cast<PACKET_CZ_RESET_SKILL*>( RFIFOP( fd, 0 ) );
  20633. #endif
  20634. }
  20635. void clif_set_dialog_align(map_session_data& sd, int32 npcid, e_say_dialog_align align)
  20636. {
  20637. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20638. PACKET_ZC_DIALOG_TEXT_ALIGN p = {};
  20639. p.PacketType = HEADER_ZC_DIALOG_TEXT_ALIGN;
  20640. p.align = align;
  20641. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20642. #endif // PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20643. }
  20644. void clif_set_npc_window_size(map_session_data& sd, int32 width, int32 height)
  20645. {
  20646. #if PACKETVER_MAIN_NUM >= 20220504
  20647. PACKET_ZC_DIALOG_WINDOW_SIZE p = {};
  20648. p.PacketType = HEADER_ZC_DIALOG_WINDOW_SIZE;
  20649. p.width = width;
  20650. p.height = height;
  20651. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20652. #endif // PACKETVER_MAIN_NUM >= 20220504
  20653. }
  20654. void clif_set_npc_window_pos(map_session_data& sd, int32 x, int32 y)
  20655. {
  20656. #if PACKETVER_MAIN_NUM >= 20220504
  20657. PACKET_ZC_DIALOG_WINDOW_POS p = {};
  20658. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS;
  20659. p.x = x;
  20660. p.y = y;
  20661. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20662. #endif // PACKETVER_MAIN_NUM >= 20220504
  20663. }
  20664. void clif_set_npc_window_pos_percent(map_session_data& sd, int32 x, int32 y)
  20665. {
  20666. #if PACKETVER_MAIN_NUM >= 20220504
  20667. PACKET_ZC_DIALOG_WINDOW_POS2 p = {};
  20668. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS2;
  20669. p.x = x;
  20670. p.y = y;
  20671. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20672. #endif // PACKETVER_MAIN_NUM >= 20220504
  20673. }
  20674. /// Displays a special popup.
  20675. /// Works only if player moved from one map to another.
  20676. /// 0bbe <popup id>.L (ZC_SPECIALPOPUP)
  20677. void clif_specialpopup(map_session_data& sd, int32 id ){
  20678. #if PACKETVER >= 20221005
  20679. PACKET_ZC_SPECIALPOPUP p = {};
  20680. p.PacketType = HEADER_ZC_SPECIALPOPUP;
  20681. p.ppId = id;
  20682. clif_send( &p, sizeof( p ), &sd.bl, SELF);
  20683. #endif
  20684. }
  20685. /*==========================================
  20686. * Main client packet processing function
  20687. *------------------------------------------*/
  20688. static int32 clif_parse(int32 fd)
  20689. {
  20690. int32 cmd, packet_len;
  20691. TBL_PC* sd;
  20692. int32 pnum;
  20693. #ifdef PACKET_OBFUSCATION
  20694. int32 cmd2;
  20695. #endif
  20696. //TODO apply delays or disconnect based on packet throughput [FlavioJS]
  20697. // Note: "click masters" can do 80+ clicks in 10 seconds
  20698. for( pnum = 0; pnum < 3; ++pnum )// Limit max packets per cycle to 3 (delay packet spammers) [FlavioJS] -- This actually aids packet spammers, but stuff like /str+ gets slow without it [Ai4rei]
  20699. { // begin main client packet processing loop
  20700. sd = (TBL_PC *)session[fd]->session_data;
  20701. if (session[fd]->flag.eof) {
  20702. if (sd) {
  20703. if (sd->state.autotrade) {
  20704. //Disassociate character from the socket connection.
  20705. session[fd]->session_data = nullptr;
  20706. sd->fd = 0;
  20707. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off (using @autotrade).\n", sd->status.name);
  20708. } else
  20709. if (sd->state.active) {
  20710. // Player logout display [Valaris]
  20711. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off.\n", sd->status.name);
  20712. clif_quitsave(fd, sd);
  20713. } else {
  20714. //Unusual logout (during log on/off/map-changer procedure)
  20715. ShowInfo("Player AID:%d/CID:%d logged off.\n", sd->status.account_id, sd->status.char_id);
  20716. map_quit(sd);
  20717. }
  20718. } else {
  20719. ShowInfo("Closed connection from '" CL_WHITE "%s" CL_RESET "'.\n", ip2str(session[fd]->client_addr, nullptr));
  20720. }
  20721. do_close(fd);
  20722. return 0;
  20723. }
  20724. if (RFIFOREST(fd) < 2)
  20725. return 0;
  20726. cmd = RFIFOW(fd, 0);
  20727. #ifdef PACKET_OBFUSCATION
  20728. // Check if it is a player that tries to connect to the map server.
  20729. if( sd ){
  20730. // Decrypt the current packet ID with the last key stored in the session.
  20731. cmd = (cmd ^ ((sd->cryptKey >> 16) & 0x7FFF));
  20732. }else{
  20733. // Store the original value for checking
  20734. cmd2 = cmd;
  20735. // A player tries to connect - use the initial keys for the decryption of the packet ID.
  20736. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20737. }
  20738. #endif
  20739. // filter out invalid / unsupported packets
  20740. if (cmd > MAX_PACKET_DB || cmd < MIN_PACKET_DB || packet_db[cmd].len == 0) {
  20741. ShowWarning("clif_parse: Received unsupported packet (packet 0x%04x, %d bytes received), disconnecting session #%d.\n", cmd, RFIFOREST(fd), fd);
  20742. #ifdef DUMP_INVALID_PACKET
  20743. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20744. #endif
  20745. #ifdef PACKET_OBFUSCATION_WARN
  20746. // If it is a connection attempt, check if the packet would have been valid without decrypting it
  20747. if( !sd ){
  20748. #ifdef PACKET_OBFUSCATION
  20749. if( cmd2 >= MIN_PACKET_DB && cmd2 < MAX_PACKET_DB && packet_db[cmd2].len != 0 && packet_db[cmd2].func == clif_parse_WantToConnection ){
  20750. ShowWarning( "clif_parse: It looks like you have enabled PACKET_OBFUSCATION on server side, but disabled it on client side.\n" );
  20751. }
  20752. #else
  20753. // Try to use the initial keys for the decryption of the packet ID.
  20754. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20755. if( cmd >= MIN_PACKET_DB && cmd < MAX_PACKET_DB && packet_db[cmd].len != 0 && packet_db[cmd].func == clif_parse_WantToConnection ){
  20756. ShowWarning( "clif_parse: It looks like you have disabled PACKET_OBFUSCATION on server side, but enabled it on client side.\n" );
  20757. }
  20758. #endif
  20759. }
  20760. #endif
  20761. set_eof(fd);
  20762. return 0;
  20763. }
  20764. // determine real packet length
  20765. packet_len = packet_db[cmd].len;
  20766. if (packet_len == -1) { // variable-length packet
  20767. if (RFIFOREST(fd) < 4)
  20768. return 0;
  20769. packet_len = RFIFOW(fd,2);
  20770. if (packet_len < 4 || packet_len > 32768) {
  20771. ShowWarning("clif_parse: Received packet 0x%04x specifies invalid packet_len (%d), disconnecting session #%d.\n", cmd, packet_len, fd);
  20772. #ifdef DUMP_INVALID_PACKET
  20773. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20774. #endif
  20775. set_eof(fd);
  20776. return 0;
  20777. }
  20778. }
  20779. if ((int32)RFIFOREST(fd) < packet_len){
  20780. ShowWarning( "clif_parse: Received packet 0x%04x with expected packet length %d, but only %d bytes remaining, disconnecting session #%d.\n", cmd, packet_len, RFIFOREST( fd ), fd );
  20781. #ifdef DUMP_INVALID_PACKET
  20782. ShowDump( RFIFOP( fd, 0 ), RFIFOREST( fd ) );
  20783. #endif
  20784. set_eof( fd );
  20785. return 0; // not enough data received to form the packet
  20786. }
  20787. #ifdef PACKET_OBFUSCATION
  20788. RFIFOW(fd, 0) = cmd;
  20789. if (sd)
  20790. sd->cryptKey = ((sd->cryptKey * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF; // Update key for the next packet
  20791. #endif
  20792. if( packet_db[cmd].func == clif_parse_debug )
  20793. packet_db[cmd].func(fd, sd);
  20794. else if( packet_db[cmd].func != nullptr ) {
  20795. if( !sd && packet_db[cmd].func != clif_parse_WantToConnection )
  20796. ; //Only valid packet when there is no session
  20797. else
  20798. if( sd && sd->bl.prev == nullptr && packet_db[cmd].func != clif_parse_LoadEndAck )
  20799. ; //Only valid packet when player is not on a map
  20800. else
  20801. packet_db[cmd].func(fd, sd);
  20802. }
  20803. #ifdef DUMP_UNKNOWN_PACKET
  20804. else DumpUnknown(fd,sd,cmd,packet_len);
  20805. #endif
  20806. RFIFOSKIP(fd, packet_len);
  20807. }; // main loop end
  20808. return 0;
  20809. }
  20810. void packetdb_addpacket( uint16 cmd, uint16 length, void (*func)(int32, map_session_data *), ... ){
  20811. va_list argp;
  20812. int32 i;
  20813. if(cmd <= 0 || cmd > MAX_PACKET_DB)
  20814. return;
  20815. packet_db[cmd].len = length;
  20816. packet_db[cmd].func = func;
  20817. va_start(argp, func);
  20818. for( i = 0; i < MAX_PACKET_POS; i++ ){
  20819. int32 offset = va_arg(argp, int32);
  20820. if( offset == 0 ){
  20821. break;
  20822. }
  20823. packet_db[cmd].pos[i] = offset;
  20824. }
  20825. if( i == MAX_PACKET_POS ){
  20826. ShowError( "Too many positions found for packet 0x%04x (max=%d).\n", cmd, MAX_PACKET_POS );
  20827. }
  20828. va_end(argp);
  20829. }
  20830. /*==========================================
  20831. * Reads packets and setups its array reference
  20832. *------------------------------------------*/
  20833. void packetdb_readdb(){
  20834. memset(packet_db,0,sizeof(packet_db));
  20835. #include "clif_packetdb.hpp"
  20836. #include "clif_shuffle.hpp"
  20837. ShowStatus("Using packet version: " CL_WHITE "%d" CL_RESET ".\n", PACKETVER);
  20838. #ifdef PACKET_OBFUSCATION
  20839. ShowStatus("Packet Obfuscation: " CL_GREEN "Enabled" CL_RESET ". Keys: " CL_WHITE "0x%08X, 0x%08X, 0x%08X" CL_RESET "\n", clif_cryptKey[0], clif_cryptKey[1], clif_cryptKey[2]);
  20840. #else
  20841. ShowStatus("Packet Obfuscation: " CL_RED "Disabled" CL_RESET ".\n");
  20842. #endif
  20843. }
  20844. /*==========================================
  20845. *
  20846. *------------------------------------------*/
  20847. void do_init_clif(void) {
  20848. const int32 colors[COLOR_MAX] = {
  20849. 0x00FF00, // COLOR_DEFAULT
  20850. 0xFF0000, // COLOR_RED
  20851. 0xFFFFFF, // COLOR_WHITE
  20852. 0xFFFF00, // COLOR_YELLOW
  20853. 0x00FFFF, // COLOR_CYAN
  20854. 0xB5FFB5, // COLOR_LIGHT_GREEN
  20855. 0xFFFF63, // COLOR_LIGHT_YELLOW
  20856. };
  20857. /**
  20858. * Setup Color Table (saves unnecessary load of strtoul on every call)
  20859. **/
  20860. for( int32 i = 0; i < COLOR_MAX; i++ ){
  20861. color_table[i] = ( colors[i] & 0x0000FF ) << 16 | ( colors[i] & 0x00FF00 ) | ( colors[i] & 0xFF0000 ) >> 16; //RGB to BGR
  20862. }
  20863. packetdb_readdb();
  20864. set_defaultparse(clif_parse);
  20865. if( make_listen_bind(bind_ip,map_port) == -1 ) {
  20866. ShowFatalError("Failed to bind to port '" CL_WHITE "%d" CL_RESET "'\n",map_port);
  20867. exit(EXIT_FAILURE);
  20868. }
  20869. add_timer_func_list(clif_clearunit_delayed_sub, "clif_clearunit_delayed_sub");
  20870. add_timer_func_list(clif_delayquit, "clif_delayquit");
  20871. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320
  20872. add_timer_func_list( clif_ping_timer, "clif_ping_timer" );
  20873. if( battle_config.ping_timer_interval > 0 ){
  20874. add_timer_interval( gettick() + battle_config.ping_timer_interval * 1000, clif_ping_timer, 0, 0, battle_config.ping_timer_interval * 1000 );
  20875. }
  20876. #endif
  20877. delay_clearunit_ers = ers_new(sizeof(struct block_list),"clif.cpp::delay_clearunit_ers",ERS_OPT_CLEAR);
  20878. }
  20879. void do_final_clif(void) {
  20880. ers_destroy(delay_clearunit_ers);
  20881. }