clif.cpp 740 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390
  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 int 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. int 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, unsigned short pos, short x, short 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, unsigned short pos, short x0, short y0, short x1, short 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(int fd, unsigned short pos, short x, short y, unsigned char dir) {
  166. WBUFPOS(WFIFOP(fd,pos), 0, x, y, dir);
  167. }
  168. static inline void RBUFPOS(const uint8* p, unsigned short pos, short* x, short* 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, unsigned short pos, short* x0, short* y0, short* x1, short* 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(int fd, unsigned short pos, short* x, short* y, unsigned char* dir) {
  202. RBUFPOS(RFIFOP(fd,pos), 0, x, y, dir);
  203. }
  204. static inline void RFIFOPOS2(int fd, unsigned short pos, short* x0, short* y0, short* x1, short* 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 unsigned int mes_len_check(char* mes, unsigned int len, unsigned int 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. int map_fd;
  224. static int clif_parse (int fd);
  225. /*==========================================
  226. * Ip setting of map-server
  227. *------------------------------------------*/
  228. int 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 int 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. int 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,int);
  352. nullpo_ret(src_bl = va_arg(ap,struct block_list*));
  353. type = va_arg(ap,int);
  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. int clif_send(const void* buf, int len, struct block_list* bl, enum send_target type)
  405. {
  406. int i;
  407. map_session_data *sd, *tsd;
  408. struct party_data *p = nullptr;
  409. std::shared_ptr<s_battleground_data> bg;
  410. int 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(int 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(int fd, int 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(int 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, (int)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 int clif_setlevel_sub(int 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 int clif_setlevel(struct block_list* bl) {
  908. int 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. int 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. int 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. int 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 an NPC object (ZC_NPCSPRITE_CHANGE).
  1303. /// 01b0 <id>.L <type>.B <value>.L
  1304. /// type:
  1305. /// unused
  1306. void clif_class_change_target(struct block_list *bl,int class_,int type, enum send_target target, map_session_data *sd)
  1307. {
  1308. nullpo_retv(bl);
  1309. if(!pcdb_checkid(class_))
  1310. {// player classes yield missing sprites
  1311. unsigned char buf[16];
  1312. WBUFW(buf,0)=0x1b0;
  1313. WBUFL(buf,2)=bl->id;
  1314. WBUFB(buf,6)=type;
  1315. WBUFL(buf,7)=class_;
  1316. clif_send(buf,packet_len(0x1b0),(sd == nullptr ? bl : &(sd->bl)),target);
  1317. }
  1318. }
  1319. void clif_servantball( map_session_data& sd, struct block_list* target, enum send_target send_target ){
  1320. PACKET_ZC_SPIRITS p = {};
  1321. p.PacketType = HEADER_ZC_SPIRITS;
  1322. p.AID = sd.bl.id;
  1323. p.num = sd.servantball;
  1324. if( target == nullptr ){
  1325. target = &sd.bl;
  1326. }
  1327. clif_send( &p, sizeof( p ), target, send_target );
  1328. }
  1329. void clif_abyssball( map_session_data& sd, struct block_list* target, enum send_target send_target ){
  1330. PACKET_ZC_SPIRITS p = {};
  1331. p.PacketType = HEADER_ZC_SPIRITS;
  1332. p.AID = sd.bl.id;
  1333. p.num = sd.abyssball;
  1334. if( target == nullptr ){
  1335. target = &sd.bl;
  1336. }
  1337. clif_send( &p, sizeof( p ), target, send_target );
  1338. }
  1339. /// Notifies the client of an object's Millenium Shields.
  1340. static void clif_millenniumshield_single( map_session_data& sd, map_session_data& tsd ){
  1341. #if PACKETVER >= 20081126
  1342. status_change_entry* sce = sd.sc.getSCE( SC_MILLENNIUMSHIELD );
  1343. if( sce == nullptr ){
  1344. return;
  1345. }
  1346. PACKET_ZC_MILLENNIUMSHIELD packet{};
  1347. packet.packetType = HEADER_ZC_MILLENNIUMSHIELD;
  1348. packet.aid = sd.bl.id;
  1349. packet.num = sce->val2;
  1350. packet.state = 0;
  1351. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  1352. #endif
  1353. }
  1354. /*==========================================
  1355. * Kagerou/Oboro amulet spirit
  1356. *------------------------------------------*/
  1357. static void clif_spiritcharm_single( map_session_data& sd, map_session_data& tsd ){
  1358. #if PACKETVER >= 20111102
  1359. if( sd.spiritcharm_type == CHARM_TYPE_NONE ){
  1360. return;
  1361. }
  1362. if( sd.spiritcharm <= 0 ){
  1363. return;
  1364. }
  1365. PACKET_ZC_SPIRITS_ATTRIBUTE packet{};
  1366. packet.packetType = HEADER_ZC_SPIRITS_ATTRIBUTE;
  1367. packet.aid = sd.bl.id;
  1368. packet.spiritsType = static_cast<decltype(packet.spiritsType)>(sd.spiritcharm_type);
  1369. packet.num = static_cast<decltype(packet.num)>(sd.spiritcharm);
  1370. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  1371. #endif
  1372. }
  1373. /*==========================================
  1374. * Enchanting Shadow / Shadow Scar Spirit
  1375. *------------------------------------------*/
  1376. void clif_enchantingshadow_spirit(unit_data &ud) {
  1377. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191120 || PACKETVER_ZERO_NUM >= 20191127
  1378. PACKET_ZC_TARGET_SPIRITS p = {};
  1379. p.packetType = HEADER_ZC_TARGET_SPIRITS;
  1380. p.GID = ud.bl->id;
  1381. p.unknown_val = 0;
  1382. p.amount = static_cast<uint16>(ud.shadow_scar_timer.size());
  1383. clif_send(&p, sizeof(p), ud.bl, AREA);
  1384. #endif
  1385. }
  1386. /*==========================================
  1387. * Run when player changes map / refreshes
  1388. * Tells its client to display all weather settings being used by this map
  1389. *------------------------------------------*/
  1390. static void clif_weather_check(map_session_data *sd)
  1391. {
  1392. int16 m = sd->bl.m;
  1393. int fd = sd->fd;
  1394. if (map_getmapflag(m, MF_SNOW)
  1395. || map_getmapflag(m, MF_CLOUDS)
  1396. || map_getmapflag(m, MF_FOG)
  1397. || map_getmapflag(m, MF_FIREWORKS)
  1398. || map_getmapflag(m, MF_SAKURA)
  1399. || map_getmapflag(m, MF_LEAVES)
  1400. || map_getmapflag(m, MF_CLOUDS2))
  1401. {
  1402. if (map_getmapflag(m, MF_SNOW))
  1403. clif_specialeffect_single(&sd->bl, EF_SNOW, fd);
  1404. if (map_getmapflag(m, MF_CLOUDS))
  1405. clif_specialeffect_single(&sd->bl, EF_CLOUD3, fd);
  1406. if (map_getmapflag(m, MF_CLOUDS2))
  1407. clif_specialeffect_single(&sd->bl, EF_CLOUD5, fd);
  1408. if (map_getmapflag(m, MF_FOG))
  1409. clif_specialeffect_single(&sd->bl, EF_CLOUD4, fd);
  1410. if (map_getmapflag(m, MF_FIREWORKS)) {
  1411. clif_specialeffect_single(&sd->bl, EF_POKJUK, fd);
  1412. clif_specialeffect_single(&sd->bl, EF_THROWITEM2, fd);
  1413. clif_specialeffect_single(&sd->bl, EF_POKJUK_SOUND, fd);
  1414. }
  1415. if (map_getmapflag(m, MF_SAKURA))
  1416. clif_specialeffect_single(&sd->bl, EF_SAKURA, fd);
  1417. if (map_getmapflag(m, MF_LEAVES))
  1418. clif_specialeffect_single(&sd->bl, EF_MAPLE, fd);
  1419. }
  1420. }
  1421. /**
  1422. * Run when the weather on a map changes, throws all players in map id 'm' to clif_weather_check function
  1423. **/
  1424. void clif_weather(int16 m)
  1425. {
  1426. struct s_mapiterator* iter;
  1427. map_session_data *sd=nullptr;
  1428. iter = mapit_getallusers();
  1429. for( sd = (map_session_data*)mapit_first(iter); mapit_exists(iter); sd = (map_session_data*)mapit_next(iter) )
  1430. {
  1431. if( sd->bl.m == m )
  1432. clif_weather_check(sd);
  1433. }
  1434. mapit_free(iter);
  1435. }
  1436. /**
  1437. * Hide a NPC from the effects of Maya Purple card.
  1438. * @param bl: Block data
  1439. * @return True if NPC is disabled or false otherwise
  1440. */
  1441. static inline bool clif_npc_mayapurple(block_list *bl) {
  1442. nullpo_retr(false, bl);
  1443. if (bl->type == BL_NPC) {
  1444. npc_data *nd = map_id2nd(bl->id);
  1445. // TODO: Confirm if waitingroom cause any special cases
  1446. if (/* nd->chat_id == 0 && */ nd->is_invisible)
  1447. return true;
  1448. }
  1449. return false;
  1450. }
  1451. /**
  1452. * Main function to spawn a unit on the client (player/mob/pet/etc)
  1453. **/
  1454. int clif_spawn( struct block_list *bl, bool walking ){
  1455. struct view_data *vd;
  1456. vd = status_get_viewdata(bl);
  1457. if( !vd || vd->class_ == JT_INVISIBLE )
  1458. return 0;
  1459. // Hide NPC from Maya Purple card
  1460. if (clif_npc_mayapurple(bl))
  1461. return 0;
  1462. if( bl->type == BL_NPC && !vd->dead_sit ){
  1463. clif_set_unit_idle( bl, walking, AREA_WOS, bl );
  1464. }else{
  1465. clif_spawn_unit( bl, AREA_WOS );
  1466. }
  1467. if (vd->cloth_color)
  1468. clif_refreshlook(bl,bl->id,LOOK_CLOTHES_COLOR,vd->cloth_color,AREA_WOS);
  1469. if (vd->body_style)
  1470. clif_refreshlook(bl,bl->id,LOOK_BODY2,vd->body_style,AREA_WOS);
  1471. switch (bl->type)
  1472. {
  1473. case BL_PC:
  1474. {
  1475. TBL_PC *sd = ((TBL_PC*)bl);
  1476. if (sd->spiritball > 0)
  1477. clif_spiritball(&sd->bl);
  1478. if (sd->sc.getSCE(SC_MILLENNIUMSHIELD))
  1479. clif_millenniumshield( sd->bl, sd->sc.getSCE( SC_MILLENNIUMSHIELD )->val2 );
  1480. if (sd->soulball > 0)
  1481. clif_soulball(sd);
  1482. if (sd->servantball > 0)
  1483. clif_servantball( *sd );
  1484. if (sd->abyssball > 0)
  1485. clif_abyssball( *sd );
  1486. if(sd->state.size==SZ_BIG) // tiny/big players [Valaris]
  1487. clif_specialeffect(bl,EF_GIANTBODY2,AREA);
  1488. else if(sd->state.size==SZ_MEDIUM)
  1489. clif_specialeffect(bl,EF_BABYBODY2,AREA);
  1490. if( sd->bg_id && map_getmapflag(sd->bl.m, MF_BATTLEGROUND) )
  1491. clif_sendbgemblem_area(sd);
  1492. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  1493. clif_spiritcharm( *sd );
  1494. if (sd->status.robe)
  1495. clif_refreshlook(bl,bl->id,LOOK_ROBE,sd->status.robe,AREA);
  1496. clif_efst_status_change_sub(bl, bl, AREA);
  1497. clif_hat_effects( *sd, sd->bl, AREA );
  1498. }
  1499. break;
  1500. case BL_MOB:
  1501. {
  1502. TBL_MOB *md = ((TBL_MOB*)bl);
  1503. if(md->special_state.size==SZ_BIG) // tiny/big mobs [Valaris]
  1504. clif_specialeffect(&md->bl,EF_GIANTBODY2,AREA);
  1505. else if(md->special_state.size==SZ_MEDIUM)
  1506. clif_specialeffect(&md->bl,EF_BABYBODY2,AREA);
  1507. if ( md->special_state.ai == AI_ABR || md->special_state.ai == AI_BIONIC )
  1508. clif_summon_init(*md);
  1509. }
  1510. break;
  1511. case BL_NPC:
  1512. {
  1513. TBL_NPC *nd = ((TBL_NPC*)bl);
  1514. if( nd->size == SZ_BIG )
  1515. clif_specialeffect(&nd->bl,EF_GIANTBODY2,AREA);
  1516. else if( nd->size == SZ_MEDIUM )
  1517. clif_specialeffect(&nd->bl,EF_BABYBODY2,AREA);
  1518. clif_efst_status_change_sub(bl, bl, AREA);
  1519. clif_progressbar_npc_area(nd);
  1520. }
  1521. break;
  1522. case BL_PET:
  1523. if (vd->head_bottom)
  1524. clif_pet_equip_area((TBL_PET*)bl); // needed to display pet equip properly
  1525. break;
  1526. }
  1527. return 0;
  1528. }
  1529. /// Sends information about owned homunculus to the client . [orn]
  1530. /// 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)
  1531. /// 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)
  1532. void clif_hominfo( map_session_data *sd, struct homun_data *hd, int flag ){
  1533. #if PACKETVER_MAIN_NUM >= 20101005 || PACKETVER_RE_NUM >= 20080827 || defined(PACKETVER_ZERO)
  1534. nullpo_retv( sd );
  1535. nullpo_retv( hd );
  1536. struct status_data *status = &hd->battle_status;
  1537. PACKET_ZC_PROPERTY_HOMUN p = {};
  1538. p.packetType = HEADER_ZC_PROPERTY_HOMUN;
  1539. safestrncpy( p.name, hd->homunculus.name, sizeof( p.name ) );
  1540. // Bit field, bit 0 : rename_flag (1 = already renamed), bit 1 : homunc vaporized (1 = true), bit 2 : homunc dead (1 = true)
  1541. 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 );
  1542. p.level = hd->homunculus.level;
  1543. p.hunger = hd->homunculus.hunger;
  1544. p.intimacy = hd->homunculus.intimacy / 100;
  1545. #if !(PACKETVER_MAIN_NUM >= 20190619 || PACKETVER_RE_NUM >= 20190605 || PACKETVER_ZERO_NUM >= 20190626)
  1546. p.itemId = 0; // equip id
  1547. #endif
  1548. p.atk2 = cap_value( status->rhw.atk2 + status->batk, 0, INT16_MAX );
  1549. p.matk = i16min( status->matk_max, INT16_MAX ); //FIXME capping to INT16 here is too late
  1550. p.hit = status->hit;
  1551. if( battle_config.hom_setting&HOMSET_DISPLAY_LUK ){
  1552. p.crit = status->luk / 3 + 1; //crit is a +1 decimal value! Just display purpose.[Vicious]
  1553. }else{
  1554. p.crit = status->cri / 10;
  1555. }
  1556. #ifdef RENEWAL
  1557. p.def = status->def + status->def2;
  1558. p.mdef = status->mdef + status->mdef2;
  1559. #else
  1560. p.def = status->def + status->vit;
  1561. p.mdef = status->mdef;
  1562. #endif
  1563. p.flee = status->flee;
  1564. p.amotion = (flag) ? 0 : status->amotion;
  1565. #if PACKETVER >= 20141016
  1566. // Homunculus HP bar will screw up if the percentage calculation exceeds signed values
  1567. // Tested maximum: 21474836(=INT32_MAX/100), any value above will screw up the HP bar
  1568. if( status->max_hp > ( INT32_MAX / 100 ) ){
  1569. p.hp = status->hp / ( status->max_hp / 100 );
  1570. p.maxHp = 100;
  1571. }else{
  1572. p.hp = status->hp;
  1573. p.maxHp = status->max_hp;
  1574. }
  1575. #else
  1576. if( status->max_hp > INT16_MAX ){
  1577. p.hp = status->hp / ( status->max_hp / 100 );
  1578. p.maxHp = 100;
  1579. }else{
  1580. p.hp = status->hp;
  1581. p.maxHp = status->max_hp;
  1582. }
  1583. #endif
  1584. if( status->max_sp > INT16_MAX ){
  1585. p.sp = status->sp / ( status->max_sp / 100 );
  1586. p.maxSp = 100;
  1587. }else{
  1588. p.sp = status->sp;
  1589. p.maxSp = status->max_sp;
  1590. }
  1591. #if PACKETVER_MAIN_NUM >= 20210303 || PACKETVER_RE_NUM >= 20211103
  1592. p.exp = hd->homunculus.exp;
  1593. p.expNext = hd->exp_next;
  1594. #else
  1595. p.exp = (uint32)hd->homunculus.exp;
  1596. p.expNext = (uint32)hd->exp_next;
  1597. #endif
  1598. switch( hom_class2type( hd->homunculus.class_ ) ){
  1599. case HT_REG:
  1600. case HT_EVO:
  1601. if( hd->homunculus.level >= battle_config.hom_max_level ){
  1602. p.expNext = 0;
  1603. }
  1604. break;
  1605. case HT_S:
  1606. if( hd->homunculus.level >= battle_config.hom_S_max_level ){
  1607. p.expNext = 0;
  1608. }
  1609. break;
  1610. }
  1611. p.skillPoints = hd->homunculus.skillpts;
  1612. p.range = status_get_range( &hd->bl );
  1613. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  1614. #endif
  1615. }
  1616. /// Notification about a change in homunuculus' state.
  1617. /// 0230 <type>.B <state>.B <id>.L <data>.L (ZC_CHANGESTATE_MER)
  1618. /// type:
  1619. /// unused
  1620. /// state:
  1621. /// 0 = pre-init
  1622. /// 1 = intimacy
  1623. /// 2 = hunger
  1624. /// 3 = accessory?
  1625. /// ? = ignored
  1626. void clif_send_homdata( homun_data& hd, e_hom_state2 state ){
  1627. #if PACKETVER >= 20050523
  1628. if( hd.master == nullptr ){
  1629. return;
  1630. }
  1631. uint32 param;
  1632. switch( state ){
  1633. case SP_ACK:
  1634. param = 0;
  1635. break;
  1636. case SP_INTIMATE:
  1637. // TODO: Why is something like this here? [Lemongrass]
  1638. if( hd.homunculus.class_ == hd.homunculusDB->evo_class && hom_intimacy_intimacy2grade( hd.homunculus.intimacy ) >= HOMGRADE_LOYAL ){
  1639. hom_calc_skilltree( &hd );
  1640. }
  1641. param = hd.homunculus.intimacy / 100;
  1642. break;
  1643. case SP_HUNGRY:
  1644. param = hd.homunculus.hunger;
  1645. break;
  1646. default:
  1647. return;
  1648. }
  1649. PACKET_ZC_CHANGESTATE_MER packet{};
  1650. packet.packetType = HEADER_ZC_CHANGESTATE_MER;
  1651. packet.type = 0;
  1652. packet.state = state;
  1653. packet.gid = hd.bl.id;
  1654. packet.data = param;
  1655. clif_send( &packet, sizeof( packet ), &hd.master->bl, SELF );
  1656. #endif
  1657. }
  1658. void clif_homskillinfoblock( homun_data& hd ){
  1659. #if PACKETVER >= 20050530
  1660. map_session_data* sd = hd.master;
  1661. if( sd == nullptr ){
  1662. return;
  1663. }
  1664. PACKET_ZC_HOSKILLINFO_LIST* packet = reinterpret_cast<PACKET_ZC_HOSKILLINFO_LIST*>(packet_buffer);
  1665. packet->packetType = HEADER_ZC_HOSKILLINFO_LIST;
  1666. packet->packetLength = sizeof( *packet );
  1667. for(int i = 0, count = 0; i < MAX_HOMUNSKILL; i++) {
  1668. int id = hd.homunculus.hskill[i].id;
  1669. if (id != 0) {
  1670. int combo = (hd.homunculus.hskill[i].flag)&SKILL_FLAG_TMP_COMBO;
  1671. short idx = hom_skill_get_index(id);
  1672. if (idx == -1)
  1673. continue;
  1674. packet->skills[count].id = id;
  1675. packet->skills[count].inf = (combo) ? INF_SELF_SKILL : skill_get_inf(id);
  1676. packet->skills[count].unknown = 0;
  1677. packet->skills[count].level = hd.homunculus.hskill[idx].lv;
  1678. packet->skills[count].sp = skill_get_sp(id,hd.homunculus.hskill[idx].lv);
  1679. packet->skills[count].range = skill_get_range2(&hd.bl,id,hd.homunculus.hskill[idx].lv,false);
  1680. safestrncpy(packet->skills[count].name, skill_get_name(id), NAME_LENGTH);
  1681. 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;
  1682. packet->packetLength += sizeof( packet->skills[0] );
  1683. count++;
  1684. }
  1685. }
  1686. clif_send( packet, packet->packetLength, &sd->bl, SELF );
  1687. #endif
  1688. }
  1689. void clif_homskillup( homun_data& hd, uint16 skill_id ){
  1690. #if PACKETVER >= 20050531
  1691. short idx = hom_skill_get_index( skill_id );
  1692. if( idx == -1 ){
  1693. return;
  1694. }
  1695. map_session_data* sd = hd.master;
  1696. if( sd == nullptr ){
  1697. return;
  1698. }
  1699. PACKET_ZC_HOSKILLINFO_UPDATE packet{};
  1700. packet.packetType = HEADER_ZC_HOSKILLINFO_UPDATE;
  1701. packet.skill_id = skill_id;
  1702. packet.Level = hd.homunculus.hskill[idx].lv;
  1703. packet.SP = skill_get_sp(skill_id,hd.homunculus.hskill[idx].lv);
  1704. packet.AttackRange = skill_get_range2(&hd.bl,skill_id,hd.homunculus.hskill[idx].lv,false);
  1705. 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;
  1706. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  1707. #endif
  1708. }
  1709. /// Result of request to feed a homun/merc.
  1710. /// 022f <result>.B <name id>.W (ZC_FEED_MER)
  1711. /// result:
  1712. /// 0 = failure
  1713. /// 1 = success
  1714. void clif_hom_food( map_session_data& sd, int32 foodid, bool success ){
  1715. PACKET_ZC_FEED_MER packet{};
  1716. packet.packetType = HEADER_ZC_FEED_MER;
  1717. packet.result = success;
  1718. packet.itemId = client_nameid( foodid );
  1719. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1720. }
  1721. /// Notifies the client, that it is walking.
  1722. /// 0087 <walk start time>.L <walk data>.6B (ZC_NOTIFY_PLAYERMOVE)
  1723. void clif_walkok( map_session_data& sd ){
  1724. PACKET_ZC_NOTIFY_PLAYERMOVE packet{};
  1725. packet.packetType = HEADER_ZC_NOTIFY_PLAYERMOVE;
  1726. packet.moveStartTime = client_tick(gettick());
  1727. WBUFPOS2(packet.moveData, 0, sd.bl.x, sd.bl.y, sd.ud.to_x, sd.ud.to_y, 8, 8);
  1728. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1729. }
  1730. /// Notifies clients in an area, that an other visible object is walking.
  1731. /// Note: unit must not be self
  1732. void clif_move( struct unit_data& ud )
  1733. {
  1734. struct block_list* bl = ud.bl;
  1735. struct view_data* vd = status_get_viewdata(bl);
  1736. if (bl == nullptr || vd == nullptr)
  1737. return;
  1738. // This performance check is needed to keep GM-hidden objects from being notified to bots.
  1739. if (vd->class_ == JT_INVISIBLE)
  1740. return;
  1741. // Hide NPC from Maya Purple card
  1742. if (clif_npc_mayapurple(bl))
  1743. return;
  1744. status_change* sc = nullptr;
  1745. if ((sc = status_get_sc(bl)) && sc->option & (OPTION_HIDE | OPTION_CLOAK | OPTION_INVISIBLE | OPTION_CHASEWALK))
  1746. clif_ally_only = true;
  1747. clif_set_unit_walking( *bl, nullptr, ud, AREA_WOS );
  1748. if (vd->cloth_color)
  1749. clif_refreshlook(bl, bl->id, LOOK_CLOTHES_COLOR, vd->cloth_color, AREA_WOS);
  1750. if (vd->body_style)
  1751. clif_refreshlook(bl, bl->id, LOOK_BODY2, vd->body_style, AREA_WOS);
  1752. switch (bl->type) {
  1753. case BL_PC:
  1754. {
  1755. map_session_data* sd = reinterpret_cast<map_session_data*>( bl );
  1756. if (sd->state.size == SZ_BIG) // tiny/big players [Valaris]
  1757. clif_specialeffect(&sd->bl, EF_GIANTBODY2, AREA);
  1758. else if (sd->state.size == SZ_MEDIUM)
  1759. clif_specialeffect(&sd->bl, EF_BABYBODY2, AREA);
  1760. if (sd->status.robe)
  1761. clif_refreshlook(bl, bl->id, LOOK_ROBE, sd->status.robe, AREA);
  1762. }
  1763. break;
  1764. case BL_MOB:
  1765. {
  1766. mob_data* md = reinterpret_cast<mob_data*>( bl );
  1767. if (md->special_state.size == SZ_BIG) // tiny/big mobs [Valaris]
  1768. clif_specialeffect(&md->bl, EF_GIANTBODY2, AREA);
  1769. else if (md->special_state.size == SZ_MEDIUM)
  1770. clif_specialeffect(&md->bl, EF_BABYBODY2, AREA);
  1771. }
  1772. break;
  1773. case BL_PET:
  1774. if (vd->head_bottom) // needed to display pet equip properly
  1775. clif_pet_equip_area(BL_CAST(BL_PET, bl));
  1776. break;
  1777. }
  1778. clif_ally_only = false;
  1779. }
  1780. /*==========================================
  1781. * Delays the map_quit of a player after they are disconnected. [Skotlex]
  1782. *------------------------------------------*/
  1783. static TIMER_FUNC(clif_delayquit){
  1784. map_session_data *sd = nullptr;
  1785. //Remove player from map server
  1786. if ((sd = map_id2sd(id)) != nullptr && sd->fd == 0) //Should be a disconnected player.
  1787. map_quit(sd);
  1788. return 0;
  1789. }
  1790. /*==========================================
  1791. *
  1792. *------------------------------------------*/
  1793. void clif_quitsave(int fd,map_session_data *sd) {
  1794. if (!battle_config.prevent_logout ||
  1795. sd->canlog_tick == 0 ||
  1796. DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout)
  1797. map_quit(sd);
  1798. else if (session_isValid(sd->fd)) {
  1799. //Disassociate session from player (session is deleted after this function was called)
  1800. //And set a timer to make him quit later.
  1801. session[sd->fd]->session_data = nullptr;
  1802. sd->fd = 0;
  1803. add_timer(gettick() + 10000, clif_delayquit, sd->bl.id, 0);
  1804. }
  1805. }
  1806. /// Notifies the client of a position change to coordinates on given map (ZC_NPCACK_MAPMOVE).
  1807. /// 0091 <map name>.16B <x>.W <y>.W
  1808. void clif_changemap( map_session_data& sd, short m, uint16 x, uint16 y ){
  1809. PACKET_ZC_NPCACK_MAPMOVE packet{};
  1810. packet.packetType = HEADER_ZC_NPCACK_MAPMOVE;
  1811. mapindex_getmapname_ext(map_mapid2mapname(m), packet.mapName);
  1812. packet.xPos = x;
  1813. packet.yPos = y;
  1814. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1815. }
  1816. /// Notifies the client of a position change to coordinates on given map, which is on another map-server.
  1817. /// 0092 <map name>.16B <x>.W <y>.W <ip>.L <port>.W (ZC_NPCACK_SERVERMOVE)
  1818. /// 0ac7 <map name>.16B <x>.W <y>.W <ip>.L <port>.W <unknown>.128B (ZC_NPCACK_SERVERMOVE_DOMAIN)
  1819. void clif_changemapserver( map_session_data& sd, const char* map, uint16 x, uint16 y, uint32 ip, uint16 port ){
  1820. PACKET_ZC_NPCACK_SERVERMOVE packet{};
  1821. packet.packetType = HEADER_ZC_NPCACK_SERVERMOVE;
  1822. mapindex_getmapname_ext(map, packet.mapName);
  1823. packet.xPos = x;
  1824. packet.yPos = y;
  1825. packet.ip = htonl(ip);
  1826. packet.port = ntows(htons(port)); // [!] LE byte order here [!]
  1827. #if PACKETVER >= 20170315
  1828. safestrncpy( packet.domain, "", sizeof( packet.domain ) );
  1829. #endif
  1830. #ifdef DEBUG
  1831. 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 );
  1832. #endif
  1833. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1834. }
  1835. /// In many situations (knockback, backslide, etc.) Aegis sends both clif_slide and clif_fixpos
  1836. /// This function combines both calls and allows to simplify the calling code
  1837. void clif_blown(struct block_list *bl)
  1838. {
  1839. clif_slide(*bl, bl->x, bl->y);
  1840. clif_fixpos( *bl );
  1841. }
  1842. /// Visually moves(slides) a character to x,y. If the target cell
  1843. /// isn't walkable, the char doesn't move at all. If the char is
  1844. /// sitting it will stand up.
  1845. /// 0088 <id>.L <x>.W <y>.W (ZC_STOPMOVE)
  1846. void clif_fixpos( block_list& bl ){
  1847. PACKET_ZC_STOPMOVE packet = {};
  1848. packet.packetType = HEADER_ZC_STOPMOVE;
  1849. packet.AID = bl.id;
  1850. packet.xPos = bl.x;
  1851. packet.yPos = bl.y;
  1852. clif_send( &packet, sizeof( packet ), &bl, AREA);
  1853. if( disguised( &bl ) ){
  1854. packet.AID = disguised_bl_id( bl.id );
  1855. clif_send( &packet, sizeof( packet ), &bl, SELF );
  1856. }
  1857. }
  1858. /// Displays the buy/sell dialog of an NPC shop.
  1859. /// 00c4 <shop id>.L (ZC_SELECT_DEALTYPE)
  1860. void clif_npcbuysell( map_session_data& sd, npc_data& nd ){
  1861. PACKET_ZC_SELECT_DEALTYPE packet{};
  1862. packet.packetType = HEADER_ZC_SELECT_DEALTYPE;
  1863. packet.npcId = nd.bl.id;
  1864. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1865. }
  1866. /// Presents list of items, that can be bought in an NPC shop.
  1867. /// 00c6 <packet len>.W { <price>.L <discount price>.L <item type>.B <name id>.W }* (ZC_PC_PURCHASE_ITEMLIST)
  1868. void clif_buylist( map_session_data& sd, npc_data& nd ){
  1869. PACKET_ZC_PC_PURCHASE_ITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_ITEMLIST*>( packet_buffer );
  1870. p->packetType = HEADER_ZC_PC_PURCHASE_ITEMLIST;
  1871. p->packetLength = sizeof( *p );
  1872. for( int i = 0, count = 0, discount = npc_shop_discount( &nd ); i < nd.u.shop.count; i++ ){
  1873. int val = nd.u.shop.shop_item[i].value;
  1874. p->items[count].price = val;
  1875. p->items[count].discountPrice = ( discount ) ? pc_modifybuyvalue( &sd, val ) : val;
  1876. p->items[count].itemType = itemtype( nd.u.shop.shop_item[i].nameid );
  1877. p->items[count].itemId = client_nameid( nd.u.shop.shop_item[i].nameid );
  1878. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  1879. std::shared_ptr<item_data> id = item_db.find(nd.u.shop.shop_item[i].nameid);
  1880. p->items[count].viewSprite = id->look;
  1881. p->items[count].location = pc_equippoint_sub( &sd, id.get() );
  1882. #endif
  1883. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  1884. count++;
  1885. }
  1886. clif_send( p, p->packetLength, &sd.bl, SELF );
  1887. }
  1888. /// Presents list of items, that can be sold to an NPC shop.
  1889. /// 00c7 <packet len>.W { <index>.W <price>.L <overcharge price>.L }* (ZC_PC_SELL_ITEMLIST)
  1890. void clif_selllist( map_session_data& sd){
  1891. if( !sd.npc_shopid ){
  1892. return;
  1893. }
  1894. npc_data* nd = map_id2nd( sd.npc_shopid );
  1895. if( nd == nullptr ){
  1896. return;
  1897. }
  1898. PACKET_ZC_PC_SELL_ITEMLIST *packet = reinterpret_cast<PACKET_ZC_PC_SELL_ITEMLIST*>(packet_buffer);
  1899. packet->packetType = HEADER_ZC_PC_SELL_ITEMLIST;
  1900. packet->packetLength = sizeof( *packet );
  1901. for( int i = 0, c = 0; i < MAX_INVENTORY; i++ ){
  1902. if( sd.inventory.u.items_inventory[i].nameid <= 0 ){
  1903. continue;
  1904. }
  1905. if( sd.inventory_data[i] == nullptr ){
  1906. continue;
  1907. }
  1908. if( !pc_can_sell_item( &sd, &sd.inventory.u.items_inventory[i], nd->subtype ) ){
  1909. continue;
  1910. }
  1911. int price;
  1912. if( battle_config.rental_item_novalue && sd.inventory.u.items_inventory[i].expire_time ){
  1913. price = 0;
  1914. }else{
  1915. price = sd.inventory_data[i]->value_sell;
  1916. if( price < 0 ){
  1917. continue;
  1918. }
  1919. }
  1920. packet->items[c].index = client_index(i);
  1921. packet->items[c].price = price;
  1922. packet->items[c].overcharge = pc_modifysellvalue( &sd, price );
  1923. packet->packetLength += sizeof( packet->items[0] );
  1924. c++;
  1925. }
  1926. clif_send( packet, packet->packetLength, &sd.bl, SELF );
  1927. }
  1928. /// Closes shop (CZ_NPC_TRADE_QUIT).
  1929. /// 09d4
  1930. void clif_parse_NPCShopClosed(int fd, map_session_data *sd) {
  1931. // TODO: State tracking?
  1932. sd->npc_shopid = 0;
  1933. }
  1934. /**
  1935. * Presents list of items, that can be sold to a Market shop.
  1936. * @author: Ind and Yommy
  1937. **/
  1938. /// 0x9d5 <packet len>.W { <name id>.W <type>.B <price>.L <qty>.L <weight>.W }* (ZC_NPC_MARKET_OPEN)
  1939. void clif_npc_market_open( map_session_data& sd, npc_data& nd ){
  1940. #if PACKETVER >= 20131223
  1941. if( sd.state.trading ){
  1942. return;
  1943. }
  1944. PACKET_ZC_NPC_MARKET_OPEN* p = reinterpret_cast<PACKET_ZC_NPC_MARKET_OPEN*>( packet_buffer );
  1945. p->packetType = HEADER_ZC_NPC_MARKET_OPEN;
  1946. p->packetLength = sizeof( *p );
  1947. for( int i = 0, count = 0; i < nd.u.shop.count; i++ ){
  1948. struct npc_item_list *item = &nd.u.shop.shop_item[i];
  1949. if( !item->nameid ){
  1950. continue;
  1951. }
  1952. std::shared_ptr<item_data> id = item_db.find(item->nameid);
  1953. if( !id ){
  1954. continue;
  1955. }
  1956. PACKET_ZC_NPC_MARKET_OPEN_sub& entry = p->list[count];
  1957. entry.nameid = client_nameid( item->nameid );
  1958. entry.type = itemtype( item->nameid );
  1959. entry.price = item->value;
  1960. entry.qty = item->qty;
  1961. entry.weight = id->weight;
  1962. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  1963. entry.location = pc_equippoint_sub( &sd, id.get() );
  1964. #endif
  1965. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  1966. count++;
  1967. }
  1968. clif_send( p, p->packetLength, &sd.bl, SELF );
  1969. sd.state.trading = 1;
  1970. #endif
  1971. }
  1972. /// Closes the Market shop window.
  1973. void clif_parse_NPCMarketClosed(int fd, map_session_data *sd) {
  1974. nullpo_retv(sd);
  1975. sd->npc_shopid = 0;
  1976. sd->state.trading = 0;
  1977. }
  1978. /// Purchase item from Market shop.
  1979. /// 0x9d7 <packet len>.W <count>.B { <name id>.W <qty>.W <price>.L }* (ZC_NPC_MARKET_PURCHASE_RESULT)
  1980. void clif_npc_market_purchase_ack( map_session_data& sd, e_purchase_result res, std::vector<s_npc_buy_list>& list ){
  1981. #if PACKETVER >= 20131223
  1982. struct npc_data *nd = map_id2nd( sd.npc_shopid );
  1983. if( nd == nullptr ){
  1984. return;
  1985. }
  1986. PACKET_ZC_NPC_MARKET_PURCHASE_RESULT* p = reinterpret_cast<PACKET_ZC_NPC_MARKET_PURCHASE_RESULT*>( packet_buffer );
  1987. p->PacketType = HEADER_ZC_NPC_MARKET_PURCHASE_RESULT;
  1988. p->PacketLength = sizeof( *p );
  1989. #if PACKETVER_MAIN_NUM >= 20190807 || PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190814
  1990. p->result = ( res == e_purchase_result::PURCHASE_SUCCEED ? 0 : -1 );
  1991. #else
  1992. p->result = ( res == e_purchase_result::PURCHASE_SUCCEED ? 1 : 0 );
  1993. #endif
  1994. if( res == e_purchase_result::PURCHASE_SUCCEED ){
  1995. for( int i = 0, j, count = 0; i < list.size(); i++ ){
  1996. ARR_FIND( 0, nd->u.shop.count, j, list[i].nameid == nd->u.shop.shop_item[j].nameid );
  1997. // Not found
  1998. if( j == nd->u.shop.count ){
  1999. continue;
  2000. }
  2001. p->list[count].ITID = client_nameid( list[i].nameid );
  2002. p->list[count].qty = list[i].qty;
  2003. p->list[count].price = nd->u.shop.shop_item[j].value;
  2004. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->list[0] ) );
  2005. count++;
  2006. }
  2007. }
  2008. clif_send( p, p->PacketLength, &sd.bl, SELF );
  2009. #endif
  2010. }
  2011. /// Purchase item from Market shop.
  2012. /// 0x9d6 <len>.W { <name id>.W <qty>.L } (CZ_NPC_MARKET_PURCHASE)
  2013. void clif_parse_NPCMarketPurchase(int fd, map_session_data *sd) {
  2014. #if PACKETVER >= 20131223
  2015. if( sd == nullptr ){
  2016. return;
  2017. }
  2018. if( !sd->npc_shopid ){
  2019. return;
  2020. }
  2021. const PACKET_CZ_NPC_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  2022. int count = ( p->PacketLength - sizeof( *p ) ) / sizeof( p->list[0] );
  2023. std::vector<s_npc_buy_list> items;
  2024. items.reserve( count );
  2025. for( int i = 0; i < count; i++ ){
  2026. s_npc_buy_list item = {};
  2027. item.nameid = p->list[i].ITID;
  2028. item.qty = p->list[i].qty;
  2029. items.push_back( item );
  2030. }
  2031. e_purchase_result res = npc_buylist( sd, items );
  2032. clif_npc_market_purchase_ack( *sd, res, items );
  2033. #endif
  2034. }
  2035. /// Displays an NPC dialog message.
  2036. /// 00b4 <packet len>.W <npc id>.L <message>.?B (ZC_SAY_DIALOG)
  2037. /// Client behavior (dialog window):
  2038. /// - disable mouse targeting
  2039. /// - open the dialog window
  2040. /// - set npcid of dialog window (0 by default)
  2041. /// - if set to clear on next mes, clear contents
  2042. /// - append this text
  2043. void clif_scriptmes( map_session_data& sd, uint32 npcid, const char *mes ){
  2044. PACKET_ZC_SAY_DIALOG* p = reinterpret_cast<PACKET_ZC_SAY_DIALOG*>( packet_buffer );
  2045. int16 length = (int16)( strlen( mes ) + 1 );
  2046. p->PacketType = HEADER_ZC_SAY_DIALOG;
  2047. p->PacketLength = sizeof( *p ) + length;
  2048. p->NpcID = npcid;
  2049. safestrncpy( p->message, mes, length );
  2050. clif_send( p, p->PacketLength, &sd.bl, SELF );
  2051. }
  2052. /// Adds a 'next' button to an NPC dialog.
  2053. /// 00b5 <npc id>.L (ZC_WAIT_DIALOG)
  2054. /// Client behavior (dialog window):
  2055. /// - disable mouse targeting
  2056. /// - open the dialog window
  2057. /// - add 'next' button
  2058. /// When 'next' is pressed:
  2059. /// - 00B9 <npcid of dialog window>.L
  2060. /// - set to clear on next mes
  2061. /// - remove 'next' button
  2062. void clif_scriptnext( map_session_data& sd, uint32 npcid ){
  2063. PACKET_ZC_WAIT_DIALOG p = {};
  2064. p.PacketType = HEADER_ZC_WAIT_DIALOG;
  2065. p.NpcID = npcid;
  2066. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  2067. }
  2068. /// Adds a 'close' button to an NPC dialog.
  2069. /// 00b6 <npc id>.L (ZC_CLOSE_DIALOG)
  2070. /// Client behavior:
  2071. /// - if dialog window is open:
  2072. /// - remove 'next' button
  2073. /// - add 'close' button
  2074. /// - else:
  2075. /// - enable mouse targeting
  2076. /// - close the dialog window
  2077. /// - close the menu window
  2078. /// When 'close' is pressed:
  2079. /// - enable mouse targeting
  2080. /// - close the dialog window
  2081. /// - close the menu window
  2082. /// - 0146 <npcid of dialog window>.L
  2083. void clif_scriptclose( map_session_data& sd, uint32 npcid ){
  2084. PACKET_ZC_CLOSE_DIALOG packet{};
  2085. packet.packetType = HEADER_ZC_CLOSE_DIALOG;
  2086. packet.npcId = npcid;
  2087. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2088. }
  2089. /**
  2090. * Close script when player is idle
  2091. * 08d6 <npc id>.L (ZC_CLEAR_DIALOG)
  2092. * @author [Ind/Hercules]
  2093. * @param sd : player pointer
  2094. * @param npcid : npc gid to close
  2095. */
  2096. void clif_scriptclear( map_session_data& sd, int npcid ){
  2097. PACKET_ZC_CLEAR_DIALOG p = {};
  2098. p.packetType = HEADER_ZC_CLEAR_DIALOG;
  2099. p.GID = npcid;
  2100. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  2101. }
  2102. void clif_sendfakenpc( map_session_data& sd, uint32 npcid ){
  2103. sd.state.using_fake_npc = 1;
  2104. // Code below is a copy of clif_set_unit_idle
  2105. struct packet_idle_unit p;
  2106. p.PacketType = idle_unitType;
  2107. #if PACKETVER >= 20091103
  2108. p.PacketLength = sizeof(p);
  2109. p.objecttype = 0x1; // NPC_TYPE
  2110. #endif
  2111. #if PACKETVER >= 20131223
  2112. p.AID = npcid;
  2113. p.GID = 0;
  2114. #else
  2115. p.GID = npcid;
  2116. #endif
  2117. p.speed = 0;
  2118. p.bodyState = 0;
  2119. p.healthState = 0;
  2120. p.effectState = 0;
  2121. p.job = JT_HIDDEN_NPC;
  2122. p.head = 0;
  2123. p.weapon = 0;
  2124. #if PACKETVER < 7 || PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  2125. p.shield = 0;
  2126. #endif
  2127. p.accessory = 0;
  2128. p.accessory2 = 0;
  2129. p.accessory3 = 0;
  2130. p.headpalette = 0;
  2131. p.bodypalette = 0;
  2132. p.headDir = 0;
  2133. #if PACKETVER >= 20101124
  2134. p.robe = 0;
  2135. #endif
  2136. p.GUID = 0;
  2137. p.GEmblemVer = 0;
  2138. p.honor = 0;
  2139. p.virtue = 0;
  2140. p.isPKModeON = 0;
  2141. p.sex = SEX_FEMALE;
  2142. WBUFPOS( &p.PosDir[0], 0, sd.bl.x, sd.bl.y, sd.ud.dir );
  2143. p.xSize = p.ySize = 5;
  2144. p.state = 0;
  2145. p.clevel = 0;
  2146. #if PACKETVER >= 20080102
  2147. p.font = 0;
  2148. #endif
  2149. #if PACKETVER >= 20120221
  2150. p.maxHP = -1;
  2151. p.HP = -1;
  2152. #endif
  2153. #if PACKETVER >= 20150513
  2154. p.body = 0;
  2155. #endif
  2156. /* Might be earlier, this is when the named item bug began */
  2157. #if PACKETVER >= 20131223
  2158. safestrncpy( p.name, "", NAME_LENGTH);
  2159. #endif
  2160. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  2161. }
  2162. /// Displays an NPC dialog menu.
  2163. /// 00b7 <packet len>.W <npc id>.L <menu items>.?B (ZC_MENU_LIST)
  2164. /// Client behavior:
  2165. /// - disable mouse targeting
  2166. /// - close the menu window
  2167. /// - open the menu window
  2168. /// - add options to the menu (separated in the text by ":")
  2169. /// - set npcid of menu window
  2170. /// - if dialog window is open:
  2171. /// - remove 'next' button
  2172. /// When 'ok' is pressed:
  2173. /// - 00B8 <npcid of menu window>.L <selected option>.B
  2174. /// - close the menu window
  2175. /// When 'cancel' is pressed:
  2176. /// - 00B8 <npcid of menu window>.L <-1>.B
  2177. /// - enable mouse targeting
  2178. /// - close a bunch of windows...
  2179. /// WARNING: the 'cancel' button closes other windows besides the dialog window and the menu window.
  2180. /// Which suggests their have intertwined behavior. (probably the mouse targeting)
  2181. /// TODO investigate behavior of other windows [FlavioJS]
  2182. void clif_scriptmenu( map_session_data& sd, uint32 npcid, const char* mes ){
  2183. struct block_list *bl = nullptr;
  2184. if (!sd.state.using_fake_npc && (npcid == fake_nd->bl.id || ((bl = map_id2bl(npcid)) && (bl->m!=sd.bl.m ||
  2185. bl->x<sd.bl.x-AREA_SIZE-1 || bl->x>sd.bl.x+AREA_SIZE+1 ||
  2186. bl->y<sd.bl.y-AREA_SIZE-1 || bl->y>sd.bl.y+AREA_SIZE+1))))
  2187. clif_sendfakenpc( sd, npcid );
  2188. // String length + 1 byte for zero termination
  2189. size_t mes_length = strlen( mes ) + 1;
  2190. PACKET_ZC_MENU_LIST* packet = reinterpret_cast<PACKET_ZC_MENU_LIST*>( packet_buffer );
  2191. packet->packetType = HEADER_ZC_MENU_LIST;
  2192. packet->packetLength = sizeof( PACKET_ZC_MENU_LIST );
  2193. packet->npcId = npcid;
  2194. safestrncpy( packet->menu, mes, mes_length );
  2195. packet->packetLength += static_cast<decltype(packet->packetLength)>( mes_length );
  2196. clif_send( packet, packet->packetLength, &sd.bl, SELF );
  2197. }
  2198. /// Displays an NPC dialog input box for numbers.
  2199. /// 0142 <npc id>.L (ZC_OPEN_EDITDLG)
  2200. /// Client behavior (inputnum window):
  2201. /// - if npcid exists in the client:
  2202. /// - open the inputnum window
  2203. /// - set npcid of inputnum window
  2204. /// When 'ok' is pressed:
  2205. /// - if inputnum window has text:
  2206. /// - if npcid exists in the client:
  2207. /// - 0143 <npcid of inputnum window>.L <atoi(text)>.L
  2208. /// - close inputnum window
  2209. void clif_scriptinput( map_session_data& sd, uint32 npcid ){
  2210. struct block_list *bl = nullptr;
  2211. if (!sd.state.using_fake_npc && (npcid == fake_nd->bl.id || ((bl = map_id2bl(npcid)) && (bl->m!=sd.bl.m ||
  2212. bl->x<sd.bl.x-AREA_SIZE-1 || bl->x>sd.bl.x+AREA_SIZE+1 ||
  2213. bl->y<sd.bl.y-AREA_SIZE-1 || bl->y>sd.bl.y+AREA_SIZE+1))))
  2214. clif_sendfakenpc( sd, npcid );
  2215. PACKET_ZC_OPEN_EDITDLG packet{};
  2216. packet.packetType = HEADER_ZC_OPEN_EDITDLG;
  2217. packet.npcId = npcid;
  2218. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2219. }
  2220. /// Displays an NPC dialog input box for strings.
  2221. /// 01d4 <npc id>.L (ZC_OPEN_EDITDLGSTR)
  2222. /// Client behavior (inputstr window):
  2223. /// - if npcid is 0 or npcid exists in the client:
  2224. /// - open the inputstr window
  2225. /// - set npcid of inputstr window
  2226. /// When 'ok' is pressed:
  2227. /// - if inputstr window has text and isn't an insult(manner.txt):
  2228. /// - if npcid is 0 or npcid exists in the client:
  2229. /// - 01d5 <packetlen>.W <npcid of inputstr window>.L <text>.?B
  2230. /// - close inputstr window
  2231. void clif_scriptinputstr( map_session_data& sd, uint32 npcid ){
  2232. struct block_list *bl = nullptr;
  2233. if (!sd.state.using_fake_npc && (npcid == fake_nd->bl.id || ((bl = map_id2bl(npcid)) && (bl->m!=sd.bl.m ||
  2234. bl->x<sd.bl.x-AREA_SIZE-1 || bl->x>sd.bl.x+AREA_SIZE+1 ||
  2235. bl->y<sd.bl.y-AREA_SIZE-1 || bl->y>sd.bl.y+AREA_SIZE+1))))
  2236. clif_sendfakenpc( sd, npcid );
  2237. PACKET_ZC_OPEN_EDITDLGSTR packet{};
  2238. packet.packetType = HEADER_ZC_OPEN_EDITDLGSTR;
  2239. packet.npcId = npcid;
  2240. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2241. }
  2242. /// Marks a position on client's minimap.
  2243. /// 0144 <npc id>.L <type>.L <x>.L <y>.L <id>.B <color>.L (ZC_COMPASS)
  2244. /// npc id:
  2245. /// is ignored in the client
  2246. /// type:
  2247. /// 0 = display mark for 15 seconds
  2248. /// 1 = display mark until dead or teleported
  2249. /// 2 = remove mark
  2250. /// color:
  2251. /// 0x00RRGGBB
  2252. void clif_viewpoint( map_session_data& sd, uint32 npc_id, int type, uint16 x, uint16 y, int id, uint32 color ){
  2253. PACKET_ZC_COMPASS packet{};
  2254. packet.packetType = HEADER_ZC_COMPASS;
  2255. packet.npcId = npc_id;
  2256. packet.type = type;
  2257. packet.xPos = x;
  2258. packet.yPos = y;
  2259. packet.id = id;
  2260. packet.color = color;
  2261. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2262. }
  2263. /// Displays an illustration image.
  2264. /// 0145 <image name>.16B <type>.B (ZC_SHOW_IMAGE)
  2265. /// 01b3 <image name>.64B <type>.B (ZC_SHOW_IMAGE2)
  2266. /// type:
  2267. /// 0 = bottom left corner
  2268. /// 1 = bottom middle
  2269. /// 2 = bottom right corner
  2270. /// 3 = middle of screen, inside a movable window
  2271. /// 4 = middle of screen, movable with a close button, chrome-less
  2272. /// 255 = clear all displayed cutins
  2273. void clif_cutin( map_session_data& sd, const char* image, int type ){
  2274. PACKET_ZC_SHOW_IMAGE packet{};
  2275. packet.packetType = HEADER_ZC_SHOW_IMAGE;
  2276. safestrncpy(packet.image, image, sizeof(packet.image));
  2277. packet.type = type;
  2278. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2279. }
  2280. /*==========================================
  2281. * Fills in card data from the given item and into the buffer. [Skotlex]
  2282. *------------------------------------------*/
  2283. static void clif_addcards( struct EQUIPSLOTINFO* buf, struct item* item ){
  2284. nullpo_retv( buf );
  2285. // Blank data
  2286. if( item == nullptr ){
  2287. buf->card[0] = 0;
  2288. buf->card[1] = 0;
  2289. buf->card[2] = 0;
  2290. buf->card[3] = 0;
  2291. return;
  2292. }
  2293. // Pet eggs
  2294. if( item->card[0] == CARD0_PET ){
  2295. buf->card[0] = 0;
  2296. buf->card[1] = 0;
  2297. // Pet intimacy
  2298. // No idea when this was added exactly, but older clients have no problem if we send it anyway
  2299. buf->card[2] = item->card[3] >> 1;
  2300. // Pet renamed flag
  2301. buf->card[3] = item->card[3] & 1;
  2302. return;
  2303. }
  2304. // Forged/created items
  2305. if( item->card[0] == CARD0_FORGE || item->card[0] == CARD0_CREATE ){
  2306. buf->card[0] = item->card[0];
  2307. buf->card[1] = item->card[1];
  2308. buf->card[2] = item->card[2];
  2309. buf->card[3] = item->card[3];
  2310. return;
  2311. }
  2312. int i = 0, j;
  2313. // Client only receives four cards.. so randomly send them a set of cards. [Skotlex]
  2314. if( MAX_SLOTS > 4 && ( j = itemdb_slots( item->nameid ) ) > 4 ){
  2315. i = rnd() % ( j - 3 ); //eg: 6 slots, possible i values: 0->3, 1->4, 2->5 => i = rnd()%3;
  2316. }
  2317. // Normal items.
  2318. for( int k = 0; k < 4; k++, i++ ){
  2319. if( item->card[i] > 0 ){
  2320. buf->card[k] = client_nameid( item->card[i] );
  2321. }else{
  2322. buf->card[k] = 0;
  2323. }
  2324. }
  2325. }
  2326. /// Fills in part of the item buffers that calls for variable bonuses data. [Napster]
  2327. /// A maximum of 5 random options can be supported.
  2328. static void clif_add_random_options( struct ItemOptions buf[MAX_ITEM_RDM_OPT], struct item& it ){
  2329. for( int i = 0; i < MAX_ITEM_RDM_OPT; i++ ){
  2330. if( it.option[i].id ){
  2331. buf[i].index = it.option[i].id; // OptIndex
  2332. buf[i].value = it.option[i].value; // Value
  2333. buf[i].param = it.option[i].param; // Param1
  2334. }else{
  2335. buf[i].index = 0;
  2336. buf[i].value = 0;
  2337. buf[i].param = 0;
  2338. }
  2339. }
  2340. #if MAX_ITEM_RDM_OPT < 5
  2341. for( ; i < 5; i++ ){
  2342. buf[i].index = 0; // OptIndex
  2343. buf[i].value = 0; // Value
  2344. buf[i].param = 0; // Param1
  2345. }
  2346. #endif
  2347. }
  2348. /// Notifies the client, about a received inventory item or the result of a pick-up request.
  2349. /// 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)
  2350. /// 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)
  2351. /// 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)
  2352. /// 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)
  2353. /// 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)
  2354. /// 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)
  2355. void clif_additem( map_session_data *sd, int n, int amount, unsigned char fail ){
  2356. nullpo_retv( sd );
  2357. int fd = sd->fd;
  2358. if( !session_isActive( fd ) ){
  2359. return;
  2360. }
  2361. PACKET_ZC_ITEM_PICKUP_ACK p = {};
  2362. if( fail ){
  2363. p = {};
  2364. }else{
  2365. if( n < 0 || n >= MAX_INVENTORY || sd->inventory.u.items_inventory[n].nameid == 0 || sd->inventory_data[n] == nullptr ){
  2366. return;
  2367. }
  2368. p.nameid = client_nameid( sd->inventory.u.items_inventory[n].nameid );
  2369. p.IsIdentified = sd->inventory.u.items_inventory[n].identify ? 1 : 0;
  2370. p.IsDamaged = ( sd->inventory.u.items_inventory[n].attribute ) != 0 ? 1 : 0;
  2371. p.refiningLevel =sd->inventory.u.items_inventory[n].refine;
  2372. clif_addcards( &p.slot, &sd->inventory.u.items_inventory[n] );
  2373. p.location = pc_equippoint(sd,n);
  2374. p.type = itemtype( sd->inventory.u.items_inventory[n].nameid );
  2375. #if PACKETVER >= 20061218
  2376. p.HireExpireDate = sd->inventory.u.items_inventory[n].expire_time;
  2377. #if PACKETVER >= 20071002
  2378. /* why restrict the flag to non-stackable? because this is the only packet allows stackable to,
  2379. * show the color, and therefore it'd be inconsistent with the rest (aka it'd show yellow, you relog/refresh and boom its gone)
  2380. */
  2381. p.bindOnEquipType = sd->inventory.u.items_inventory[n].bound && !itemdb_isstackable2( sd->inventory_data[n] ) ? 2 : sd->inventory_data[n]->flag.bindOnEquip ? 1 : 0;
  2382. #if PACKETVER >= 20150226
  2383. clif_add_random_options( p.option_data, sd->inventory.u.items_inventory[n] );
  2384. #if PACKETVER >= 20160921
  2385. p.favorite = ( sd->inventory.u.items_inventory[n].favorite != 0 ) ? 1 : 0;
  2386. p.look = sd->inventory_data[n]->look;
  2387. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  2388. p.grade = sd->inventory.u.items_inventory[n].enchantgrade;
  2389. #endif
  2390. #endif
  2391. #endif
  2392. #endif
  2393. #endif
  2394. }
  2395. p.PacketType = HEADER_ZC_ITEM_PICKUP_ACK;
  2396. p.Index = client_index( n );
  2397. p.count = amount;
  2398. p.result = fail;
  2399. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  2400. }
  2401. /// Notifies the client, that an inventory item was deleted or dropped.
  2402. /// 00af <index>.W <amount>.W (ZC_ITEM_THROW_ACK)
  2403. void clif_dropitem( map_session_data& sd, int index, int amount ){
  2404. PACKET_ZC_ITEM_THROW_ACK packet{};
  2405. packet.packetType = HEADER_ZC_ITEM_THROW_ACK;
  2406. packet.index = client_index( index );
  2407. packet.count = amount;
  2408. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2409. }
  2410. /// Notifies the client, that an inventory item was deleted.
  2411. /// 07fa <delete type>.W <index>.W <amount>.W (ZC_DELETE_ITEM_FROM_BODY)
  2412. /// delete type:
  2413. /// 0 = Normal
  2414. /// 1 = Item used for a skill
  2415. /// 2 = Refine failed
  2416. /// 3 = Material changed
  2417. /// 4 = Moved to storage
  2418. /// 5 = Moved to cart
  2419. /// 6 = Item sold
  2420. /// 7 = Consumed by Four Spirit Analysis (SO_EL_ANALYSIS) skill
  2421. void clif_delitem( map_session_data& sd, int index, int amount, short reason ){
  2422. #if PACKETVER >= 20091117
  2423. PACKET_ZC_DELETE_ITEM_FROM_BODY packet{};
  2424. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_BODY;
  2425. packet.deleteType = reason;
  2426. packet.index = client_index( index );
  2427. packet.count = amount;
  2428. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2429. #else
  2430. clif_dropitem( sd, index, amount );
  2431. #endif
  2432. }
  2433. static void clif_item_equip( short idx, struct EQUIPITEM_INFO *p, struct item *it, struct item_data *id, int eqp_pos ){
  2434. nullpo_retv( p );
  2435. nullpo_retv( it );
  2436. nullpo_retv( id );
  2437. p->index = idx;
  2438. p->ITID = client_nameid( it->nameid );
  2439. p->type = itemtype( it->nameid );
  2440. #if PACKETVER < 20120925
  2441. p->IsIdentified = it->identify ? 1 : 0;
  2442. #endif
  2443. p->location = eqp_pos;
  2444. p->WearState = it->equip;
  2445. #if PACKETVER < 20120925
  2446. p->IsDamaged = it->attribute != 0 ? 1 : 0;
  2447. #endif
  2448. p->RefiningLevel = it->refine;
  2449. clif_addcards( &p->slot, it );
  2450. #if PACKETVER >= 20071002
  2451. p->HireExpireDate = it->expire_time;
  2452. #endif
  2453. #if PACKETVER >= 20080102
  2454. p->bindOnEquipType = it->bound ? 2 : id->flag.bindOnEquip ? 1 : 0;
  2455. #endif
  2456. #if PACKETVER >= 20100629
  2457. // TODO: WBUFW(buf,n+8) = (equip == -2 && id->equip == EQP_AMMO) ? id->equip : 0;
  2458. p->wItemSpriteNumber = ( id->equip&EQP_VISIBLE ) ? id->look : 0;
  2459. #endif
  2460. #if PACKETVER >= 20120925
  2461. p->Flag.IsIdentified = it->identify ? 1 : 0;
  2462. p->Flag.IsDamaged = it->attribute ? 1 : 0;
  2463. p->Flag.PlaceETCTab = it->favorite ? 1 : 0;
  2464. p->Flag.SpareBits = 0;
  2465. #endif
  2466. #if PACKETVER >= 20150226
  2467. clif_add_random_options( p->option_data, *it );
  2468. // Client ingores the value anyway and recalculates it
  2469. p->option_count = 0;
  2470. #endif
  2471. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  2472. p->grade = it->enchantgrade;
  2473. #endif
  2474. }
  2475. static void clif_item_normal( short idx, struct NORMALITEM_INFO *p, struct item *i, struct item_data *id ){
  2476. nullpo_retv(p);
  2477. nullpo_retv(i);
  2478. nullpo_retv(id);
  2479. p->index = idx;
  2480. p->ITID = client_nameid( i->nameid );
  2481. p->type = itemtype( i->nameid );
  2482. #if PACKETVER < 20120925
  2483. p->IsIdentified = i->identify ? 1 : 0;
  2484. #endif
  2485. p->count = i->amount;
  2486. p->WearState = id->equip;
  2487. #if PACKETVER >= 5
  2488. clif_addcards( &p->slot, i );
  2489. #endif
  2490. #if PACKETVER >= 20080102
  2491. p->HireExpireDate = i->expire_time;
  2492. #endif
  2493. #if PACKETVER >= 20120925
  2494. p->Flag.IsIdentified = i->identify ? 1 : 0;
  2495. p->Flag.PlaceETCTab = ( i->favorite != 0 ) ? 1 : 0;
  2496. p->Flag.SpareBits = 0;
  2497. #endif
  2498. }
  2499. static void clif_inventoryStart( map_session_data*sd, e_inventory_type type, const char *name ){
  2500. #if PACKETVER_RE_NUM >= 20180829 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2501. nullpo_retv(sd);
  2502. nullpo_retv(name);
  2503. char buf[sizeof(PACKET_ZC_INVENTORY_START) + 24];
  2504. memset(buf, 0, sizeof(buf));
  2505. PACKET_ZC_INVENTORY_START *p = (PACKET_ZC_INVENTORY_START *)buf;
  2506. p->packetType = HEADER_ZC_INVENTORY_START;
  2507. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2508. p->invType = type;
  2509. #endif
  2510. #if PACKETVER_RE_NUM >= 20180919 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2511. int strLen = (int)safestrnlen(name, 24) + 1;
  2512. if (strLen > 24)
  2513. strLen = 24;
  2514. const int len = sizeof(PACKET_ZC_INVENTORY_START) + strLen;
  2515. p->packetLength = len;
  2516. safestrncpy(p->name, name, strLen);
  2517. #else
  2518. const int len = sizeof(PACKET_ZC_INVENTORY_START);
  2519. safestrncpy(p->name, name, NAME_LENGTH);
  2520. #endif
  2521. clif_send( p, len, &sd->bl, SELF );
  2522. #endif
  2523. }
  2524. static void clif_inventoryEnd( map_session_data *sd, e_inventory_type type ){
  2525. #if PACKETVER_RE_NUM >= 20180829 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2526. nullpo_retv(sd);
  2527. PACKET_ZC_INVENTORY_END p = {};
  2528. p.packetType = HEADER_ZC_INVENTORY_END;
  2529. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2530. p.invType = type;
  2531. #endif
  2532. p.flag = 0;
  2533. clif_send( &p, sizeof(p), &sd->bl, SELF );
  2534. #endif
  2535. }
  2536. //Unified inventory function which sends all of the inventory (requires two packets, one for equipable items and one for stackable ones. [Skotlex]
  2537. void clif_inventorylist( map_session_data *sd ){
  2538. nullpo_retv( sd );
  2539. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2540. e_inventory_type type = INVTYPE_INVENTORY;
  2541. clif_inventoryStart( sd, type, "" );
  2542. #endif
  2543. static struct packet_itemlist_normal itemlist_normal;
  2544. static struct packet_itemlist_equip itemlist_equip;
  2545. int equip = 0;
  2546. int normal = 0;
  2547. for( int i = 0; i < MAX_INVENTORY; i++ ){
  2548. if( sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr ){
  2549. continue;
  2550. }
  2551. // Non-stackable (Equippable)
  2552. if( !itemdb_isstackable2( sd->inventory_data[i] ) ){
  2553. clif_item_equip( client_index( i ), &itemlist_equip.list[equip++], &sd->inventory.u.items_inventory[i], sd->inventory_data[i], pc_equippoint( sd, i ) );
  2554. if( equip == MAX_INVENTORY_ITEM_PACKET_NORMAL ){
  2555. itemlist_equip.PacketType = inventorylistequipType;
  2556. itemlist_equip.PacketLength = static_cast<decltype(itemlist_equip.PacketLength)>( ( sizeof( itemlist_equip ) - sizeof( itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2557. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2558. itemlist_equip.invType = type;
  2559. #endif
  2560. clif_send( &itemlist_equip, itemlist_equip.PacketLength, &sd->bl, SELF );
  2561. equip = 0;
  2562. }
  2563. // Stackable (Normal)
  2564. }else{
  2565. clif_item_normal( client_index( i ), &itemlist_normal.list[normal++], &sd->inventory.u.items_inventory[i], sd->inventory_data[i] );
  2566. if( normal == MAX_INVENTORY_ITEM_PACKET_NORMAL ){
  2567. itemlist_normal.PacketType = inventorylistnormalType;
  2568. itemlist_normal.PacketLength = static_cast<decltype(itemlist_normal.PacketLength)>( ( sizeof( itemlist_normal ) - sizeof( itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2569. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2570. itemlist_normal.invType = type;
  2571. #endif
  2572. clif_send( &itemlist_normal, itemlist_normal.PacketLength, &sd->bl, SELF );
  2573. normal = 0;
  2574. }
  2575. }
  2576. }
  2577. if( normal ){
  2578. itemlist_normal.PacketType = inventorylistnormalType;
  2579. itemlist_normal.PacketLength = static_cast<decltype(itemlist_normal.PacketLength)>( ( sizeof( itemlist_normal ) - sizeof( itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2580. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2581. itemlist_normal.invType = type;
  2582. #endif
  2583. clif_send( &itemlist_normal, itemlist_normal.PacketLength, &sd->bl, SELF );
  2584. }
  2585. if( sd->equip_index[EQI_AMMO] >= 0 )
  2586. clif_arrowequip( *sd );
  2587. if( equip ) {
  2588. itemlist_equip.PacketType = inventorylistequipType;
  2589. itemlist_equip.PacketLength = static_cast<decltype(itemlist_equip.PacketLength)>( ( sizeof( itemlist_equip ) - sizeof( itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2590. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2591. itemlist_equip.invType = type;
  2592. #endif
  2593. clif_send( &itemlist_equip, itemlist_equip.PacketLength, &sd->bl, SELF );
  2594. }
  2595. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2596. clif_inventoryEnd( sd, type );
  2597. #endif
  2598. /* on 20120925 onwards this is a field on clif_item_equip/normal */
  2599. #if PACKETVER >= 20111122 && PACKETVER < 20120925
  2600. for( int i = 0; i < MAX_INVENTORY; i++ ){
  2601. if( sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr )
  2602. continue;
  2603. if ( sd->inventory.u.items_inventory[i].favorite != 0 )
  2604. clif_favorite_item( *sd, i );
  2605. }
  2606. #endif
  2607. }
  2608. //Required when items break/get-repaired. Only sends equippable item list.
  2609. void clif_equiplist( map_session_data *sd ){
  2610. // TODO: implement again => only send equip part
  2611. clif_inventorylist( sd );
  2612. }
  2613. void clif_storagelist(map_session_data* sd, struct item* items, int items_length, const char *storename){
  2614. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2615. e_inventory_type type = INVTYPE_STORAGE;
  2616. clif_inventoryStart( sd, type, storename );
  2617. #endif
  2618. static struct ZC_STORE_ITEMLIST_NORMAL storage_itemlist_normal;
  2619. static struct ZC_STORE_ITEMLIST_EQUIP storage_itemlist_equip;
  2620. int equip = 0;
  2621. int normal = 0;
  2622. for( int i = 0; i < items_length; i++ ){
  2623. if( items[i].nameid == 0 ){
  2624. continue;
  2625. }
  2626. struct item_data* id = itemdb_search( items[i].nameid );
  2627. // Non-stackable (Equippable)
  2628. if( !itemdb_isstackable2( id ) ){
  2629. clif_item_equip( client_storage_index( i ), &storage_itemlist_equip.list[equip++], &items[i], id, pc_equippoint_sub( sd, id ) );
  2630. if( equip == MAX_STORAGE_ITEM_PACKET_EQUIP ){
  2631. storage_itemlist_equip.PacketType = storageListEquipType;
  2632. 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 ) );
  2633. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2634. storage_itemlist_equip.invType = type;
  2635. #elif PACKETVER >= 20120925
  2636. safestrncpy( storage_itemlist_equip.name, storename, NAME_LENGTH );
  2637. #endif
  2638. clif_send( &storage_itemlist_equip, storage_itemlist_equip.PacketLength, &sd->bl, SELF );
  2639. equip = 0;
  2640. }
  2641. // Stackable (Normal)
  2642. }else{
  2643. clif_item_normal( client_storage_index( i ), &storage_itemlist_normal.list[normal++], &items[i], id );
  2644. if( normal == MAX_STORAGE_ITEM_PACKET_NORMAL ){
  2645. storage_itemlist_normal.PacketType = storageListNormalType;
  2646. 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 ) );
  2647. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2648. storage_itemlist_normal.invType = type;
  2649. #elif PACKETVER >= 20120925
  2650. safestrncpy( storage_itemlist_normal.name, storename, NAME_LENGTH );
  2651. #endif
  2652. clif_send( &storage_itemlist_normal, storage_itemlist_normal.PacketLength, &sd->bl, SELF );
  2653. normal = 0;
  2654. }
  2655. }
  2656. }
  2657. if( normal ){
  2658. storage_itemlist_normal.PacketType = storageListNormalType;
  2659. 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 ) );
  2660. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2661. storage_itemlist_normal.invType = type;
  2662. #elif PACKETVER >= 20120925
  2663. safestrncpy( storage_itemlist_normal.name, storename, NAME_LENGTH );
  2664. #endif
  2665. clif_send( &storage_itemlist_normal, storage_itemlist_normal.PacketLength, &sd->bl, SELF );
  2666. }
  2667. if( equip ) {
  2668. storage_itemlist_equip.PacketType = storageListEquipType;
  2669. 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 ) );
  2670. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2671. storage_itemlist_equip.invType = type;
  2672. #elif PACKETVER >= 20120925
  2673. safestrncpy( storage_itemlist_equip.name, storename, NAME_LENGTH );
  2674. #endif
  2675. clif_send( &storage_itemlist_equip, storage_itemlist_equip.PacketLength, &sd->bl, SELF );
  2676. }
  2677. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2678. clif_inventoryEnd( sd, type );
  2679. #endif
  2680. }
  2681. void clif_cartlist( map_session_data *sd ){
  2682. nullpo_retv( sd );
  2683. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2684. e_inventory_type type = INVTYPE_CART;
  2685. clif_inventoryStart( sd, type, "" );
  2686. #endif
  2687. static struct packet_itemlist_normal itemlist_normal;
  2688. static struct packet_itemlist_equip itemlist_equip;
  2689. int normal = 0;
  2690. int equip = 0;
  2691. for( int i = 0; i < MAX_CART; i++ ){
  2692. if( sd->cart.u.items_cart[i].nameid == 0 ){
  2693. continue;
  2694. }
  2695. struct item_data* id = itemdb_search( sd->cart.u.items_cart[i].nameid );
  2696. // Non-stackable (Equippable)
  2697. if( !itemdb_isstackable2(id) ){
  2698. clif_item_equip( client_index( i ), &itemlist_equip.list[equip++], &sd->cart.u.items_cart[i], id, id->equip );
  2699. // Stackable (Normal)
  2700. }else{
  2701. clif_item_normal( client_index( i ), &itemlist_normal.list[normal++], &sd->cart.u.items_cart[i], id );
  2702. }
  2703. }
  2704. if( normal ){
  2705. itemlist_normal.PacketType = cartlistnormalType;
  2706. itemlist_normal.PacketLength = static_cast<decltype(itemlist_normal.PacketLength)>( ( sizeof( itemlist_normal ) - sizeof( itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2707. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2708. itemlist_normal.invType = type;
  2709. #endif
  2710. clif_send( &itemlist_normal, itemlist_normal.PacketLength, &sd->bl, SELF );
  2711. }
  2712. if( equip ){
  2713. itemlist_equip.PacketType = cartlistequipType;
  2714. itemlist_equip.PacketLength = static_cast<decltype(itemlist_equip.PacketLength)>( ( sizeof( itemlist_equip ) - sizeof( itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2715. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2716. itemlist_equip.invType = type;
  2717. #endif
  2718. clif_send( &itemlist_equip, itemlist_equip.PacketLength, &sd->bl, SELF );
  2719. }
  2720. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2721. clif_inventoryEnd( sd, type );
  2722. #endif
  2723. }
  2724. /// Removes cart.
  2725. /// 012b (ZC_CARTOFF)
  2726. /// Client behaviour:
  2727. /// Closes the cart storage and removes all it's items from memory.
  2728. /// The Num & Weight values of the cart are left untouched and the cart is NOT removed.
  2729. void clif_clearcart(int fd)
  2730. {
  2731. PACKET_ZC_CARTOFF packet{};
  2732. packet.packetType = HEADER_ZC_CARTOFF;
  2733. socket_send<PACKET_ZC_CARTOFF>(fd, packet);
  2734. }
  2735. /// Sends XY location to all other guild members
  2736. /// 01eb <account id>.L <x>.W <y>.W (ZC_NOTIFY_POSITION_TO_GUILDM)
  2737. void clif_guild_xy( map_session_data& sd ){
  2738. PACKET_ZC_NOTIFY_POSITION_TO_GUILDM packet{};
  2739. packet.packetType = HEADER_ZC_NOTIFY_POSITION_TO_GUILDM;
  2740. packet.aid = sd.status.account_id;
  2741. packet.xPos = sd.bl.x;
  2742. packet.yPos = sd.bl.y;
  2743. clif_send(&packet, sizeof(packet), &sd.bl, GUILD_SAMEMAP_WOS);
  2744. }
  2745. /// Sends XY location to a specific guild member
  2746. /// 01eb <account id>.L <x>.W <y>.W (ZC_NOTIFY_POSITION_TO_GUILDM)
  2747. void clif_guild_xy_single( map_session_data& sd, map_session_data& tsd ){
  2748. if( sd.bg_id ){
  2749. return;
  2750. }
  2751. PACKET_ZC_NOTIFY_POSITION_TO_GUILDM packet{};
  2752. packet.packetType = HEADER_ZC_NOTIFY_POSITION_TO_GUILDM;
  2753. packet.aid = sd.status.account_id;
  2754. packet.xPos = sd.bl.x;
  2755. packet.yPos = sd.bl.y;
  2756. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  2757. }
  2758. /// Removes XY location from all other guild members
  2759. /// 01eb <account id>.L <x>.W <y>.W (ZC_NOTIFY_POSITION_TO_GUILDM)
  2760. void clif_guild_xy_remove( map_session_data& sd ){
  2761. PACKET_ZC_NOTIFY_POSITION_TO_GUILDM packet{};
  2762. packet.packetType = HEADER_ZC_NOTIFY_POSITION_TO_GUILDM;
  2763. packet.aid = sd.status.account_id;
  2764. packet.xPos = -1;
  2765. packet.yPos = -1;
  2766. clif_send( &packet, sizeof( packet ), &sd.bl, GUILD_SAMEMAP_WOS );
  2767. }
  2768. /*==========================================
  2769. * Load castle list for guild UI. [Asheraf] / [Balfear]
  2770. *------------------------------------------*/
  2771. void clif_guild_castle_list(map_session_data& sd){
  2772. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190717 || PACKETVER_ZERO_NUM >= 20190814
  2773. auto &g = sd.guild;
  2774. if (g == nullptr)
  2775. return;
  2776. int castle_count = guild_checkcastles(g->guild);
  2777. if( castle_count <= 0 ){
  2778. return;
  2779. }
  2780. PACKET_ZC_GUILD_AGIT_INFO* p = reinterpret_cast<PACKET_ZC_GUILD_AGIT_INFO*>( packet_buffer );
  2781. p->packetType = HEADER_ZC_GUILD_AGIT_INFO;
  2782. p->packetLength = sizeof( *p );
  2783. int i = 0;
  2784. for (const auto& gc : castle_db) {
  2785. if (gc.second->guild_id == g->guild.guild_id && gc.second->client_id) {
  2786. p->castle_list[i] = static_cast<int8>( gc.second->client_id );
  2787. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->castle_list[0] ) );
  2788. ++i;
  2789. }
  2790. }
  2791. clif_send(p, p->packetLength, &sd.bl, SELF);
  2792. #endif
  2793. }
  2794. /*==========================================
  2795. * Send castle info Economy/Defence. [Asheraf] / [Balfear]
  2796. *------------------------------------------*/
  2797. void clif_guild_castleinfo(map_session_data& sd, std::shared_ptr<guild_castle> castle ){
  2798. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190717 || PACKETVER_ZERO_NUM >= 20190814
  2799. if( castle->client_id == 0 ){
  2800. return;
  2801. }
  2802. PACKET_ZC_REQ_ACK_AGIT_INVESTMENT p = {};
  2803. p.packetType = HEADER_ZC_REQ_ACK_AGIT_INVESTMENT;
  2804. p.castle_id = static_cast<int8>( castle->client_id );
  2805. p.economy = castle->economy;
  2806. p.defense = castle->defense;
  2807. clif_send(&p, sizeof(p), &sd.bl, SELF);
  2808. #endif
  2809. }
  2810. /*==========================================
  2811. * Show teleport request result. [Asheraf] / [Balfear]
  2812. *------------------------------------------*/
  2813. void clif_guild_castle_teleport_res(map_session_data& sd, enum e_siege_teleport_result result){
  2814. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190717 || PACKETVER_ZERO_NUM >= 20190814
  2815. PACKET_ZC_REQ_ACK_MOVE_GUILD_AGIT p = {};
  2816. p.packetType = HEADER_ZC_REQ_ACK_MOVE_GUILD_AGIT;
  2817. p.result = static_cast<int16>( result );
  2818. clif_send(&p, sizeof(p), &sd.bl, SELF);
  2819. #endif
  2820. }
  2821. /*==========================================
  2822. * Request castle info. [Asheraf] / [Balfear]
  2823. *------------------------------------------*/
  2824. void clif_parse_guild_castle_info_request(int fd, map_session_data* sd){
  2825. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190522 || PACKETVER_ZERO_NUM >= 20190515
  2826. if( sd == nullptr ){
  2827. return;
  2828. }
  2829. auto &g = sd->guild;
  2830. if (g == nullptr)
  2831. return;
  2832. const PACKET_CZ_REQ_AGIT_INVESTMENT* p = reinterpret_cast<PACKET_CZ_REQ_AGIT_INVESTMENT*>( RFIFOP( fd, 0 ) );
  2833. std::shared_ptr<guild_castle> gc = castle_db.find_by_clientid( p->castle_id );
  2834. if (gc == nullptr)
  2835. return;
  2836. if (gc->guild_id != g->guild.guild_id)
  2837. return;
  2838. clif_guild_castleinfo(*sd, gc);
  2839. #endif
  2840. }
  2841. /*==========================================
  2842. * Teleport to castle. [Asheraf] / [Balfear]
  2843. *------------------------------------------*/
  2844. void clif_parse_guild_castle_teleport_request(int fd, map_session_data* sd){
  2845. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190522 || PACKETVER_ZERO_NUM >= 20190515
  2846. const PACKET_CZ_REQ_MOVE_GUILD_AGIT* p = reinterpret_cast<PACKET_CZ_REQ_MOVE_GUILD_AGIT*>RFIFOP( fd, 0 );
  2847. auto &g = sd->guild;
  2848. if (g == nullptr)
  2849. return;
  2850. std::shared_ptr<guild_castle> gc = castle_db.find_by_clientid( p->castle_id );
  2851. if (gc == nullptr)
  2852. return;
  2853. if (!gc->warp_enabled)
  2854. return;
  2855. if (gc->guild_id != g->guild.guild_id)
  2856. return;
  2857. if (map_getmapflag(sd->bl.m, MF_GVG_CASTLE)
  2858. || map_getmapflag(sd->bl.m, MF_NOTELEPORT)
  2859. || map_getmapflag(sd->bl.m, MF_NOWARP))
  2860. return;
  2861. uint32 zeny = gc->zeny;
  2862. switch( gc->type ){
  2863. case WOE_FIRST_EDITION:
  2864. if( agit_flag ){
  2865. zeny = gc->zeny_siege;
  2866. }
  2867. break;
  2868. case WOE_SECOND_EDITION:
  2869. if( agit2_flag ){
  2870. zeny = gc->zeny_siege;
  2871. }
  2872. break;
  2873. case WOE_THIRD_EDITION:
  2874. if( agit3_flag ){
  2875. zeny = gc->zeny_siege;
  2876. }
  2877. break;
  2878. }
  2879. if (zeny && pc_payzeny(sd, zeny, LOG_TYPE_OTHER)) {
  2880. clif_guild_castle_teleport_res(*sd, SIEGE_TP_NOT_ENOUGH_ZENY);
  2881. return;
  2882. }
  2883. pc_setpos(sd, gc->mapindex, gc->warp_x, gc->warp_y, CLR_OUTSIGHT);
  2884. #endif
  2885. }
  2886. /*==========================================
  2887. *
  2888. *------------------------------------------*/
  2889. static int clif_hpmeter_sub( struct block_list *bl, va_list ap ){
  2890. map_session_data* sd = va_arg( ap, map_session_data* );
  2891. map_session_data* tsd = BL_CAST( BL_PC, bl );
  2892. nullpo_ret(sd);
  2893. nullpo_ret(tsd);
  2894. if( pc_has_permission( tsd, PC_PERM_VIEW_HPMETER ) ){
  2895. clif_hpmeter_single( *tsd, sd->status.account_id, sd->battle_status.hp, sd->battle_status.max_hp );
  2896. }
  2897. return 0;
  2898. }
  2899. /*==========================================
  2900. * Server tells all players that are allowed to view HP bars
  2901. * and are nearby 'sd' that 'sd' hp bar was updated.
  2902. *------------------------------------------*/
  2903. static int clif_hpmeter(map_session_data *sd)
  2904. {
  2905. nullpo_ret(sd);
  2906. 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);
  2907. return 0;
  2908. }
  2909. /**
  2910. * Send HP bar update to others.
  2911. * @param sd: Player invoking update
  2912. */
  2913. void clif_update_hp(map_session_data &sd) {
  2914. if (map_getmapdata(sd.bl.m)->hpmeter_visible)
  2915. clif_hpmeter(&sd);
  2916. if (!battle_config.party_hp_mode && sd.status.party_id)
  2917. clif_party_hp( sd );
  2918. if (sd.bg_id)
  2919. clif_bg_hp(&sd);
  2920. }
  2921. /// Notifies client of a character parameter change.
  2922. /// 00b0 <var id>.W <value>.L (ZC_PAR_CHANGE)
  2923. static void clif_par_change(map_session_data& sd, uint16 varId, int count) {
  2924. PACKET_ZC_PAR_CHANGE packet{};
  2925. packet.PacketType = HEADER_ZC_PAR_CHANGE;
  2926. packet.varID = varId;
  2927. packet.count = count;
  2928. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2929. }
  2930. /// Notifies client of a character parameter change.
  2931. /// 00b1 <var id>.W <value>.L (ZC_LONGPAR_CHANGE)
  2932. static void clif_longpar_change(map_session_data& sd, uint16 varId, int amount) {
  2933. PACKET_ZC_LONGPAR_CHANGE packet{};
  2934. packet.PacketType = HEADER_ZC_LONGPAR_CHANGE;
  2935. packet.varID = varId;
  2936. packet.amount = amount;
  2937. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2938. }
  2939. /// Notifies client of a character parameter change.
  2940. /// 00be <status id>.W <value>.B (ZC_STATUS_CHANGE)
  2941. static void clif_zc_status_change(map_session_data& sd, uint16 status_id, uint8 value) {
  2942. PACKET_ZC_STATUS_CHANGE packet{};
  2943. packet.PacketType = HEADER_ZC_STATUS_CHANGE;
  2944. packet.statusID = status_id;
  2945. packet.value = value;
  2946. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2947. }
  2948. /// Notifies client of a character parameter change.
  2949. /// 0121 <current count>.W <max count>.W <current weight>.L <max weight>.L (ZC_NOTIFY_CARTITEM_COUNTINFO)
  2950. static void clif_cartcount( map_session_data& sd ){
  2951. PACKET_ZC_NOTIFY_CARTITEM_COUNTINFO packet{};
  2952. packet.PacketType = HEADER_ZC_NOTIFY_CARTITEM_COUNTINFO;
  2953. packet.curCount = sd.cart_num;
  2954. packet.maxCount = MAX_CART;
  2955. packet.curWeight = sd.cart_weight;
  2956. packet.maxWeight = sd.cart_weight_max;
  2957. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2958. }
  2959. /// Tells the client how far it is allowed to attack (weapon range)
  2960. /// 013a <atk range>.W (ZC_ATTACK_RANGE)
  2961. static void clif_attackrange( map_session_data& sd, int16 range ){
  2962. PACKET_ZC_ATTACK_RANGE packet{};
  2963. packet.PacketType = HEADER_ZC_ATTACK_RANGE;
  2964. packet.currentAttRange = range;
  2965. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2966. }
  2967. /// Notifies client of a character parameter change.
  2968. /// 0141 <status id>.L <base status>.L <plus status>.L (ZC_COUPLESTATUS)
  2969. static void clif_couplestatus(map_session_data& sd, uint32 status_type, int32 defaultStatus, int32 plusStatus) {
  2970. PACKET_ZC_COUPLESTATUS packet{};
  2971. packet.PacketType = HEADER_ZC_COUPLESTATUS;
  2972. packet.statusType = status_type;
  2973. packet.defaultStatus = defaultStatus;
  2974. packet.plusStatus = plusStatus;
  2975. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  2976. }
  2977. /// Notifies client of a character parameter change.
  2978. /// 0acb <var id>.W <value>.Q (ZC_LONGLONGPAR_CHANGE)
  2979. static void clif_longlongpar_change(map_session_data& sd, uint16 varId, int64 amount) {
  2980. #if PACKETVER_MAIN_NUM >= 20170906 || PACKETVER_RE_NUM >= 20170830 || defined(PACKETVER_ZERO)
  2981. PACKET_ZC_LONGLONGPAR_CHANGE packet{};
  2982. packet.PacketType = HEADER_ZC_LONGLONGPAR_CHANGE;
  2983. packet.varID = varId;
  2984. packet.amount = amount;
  2985. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2986. #endif
  2987. }
  2988. /// Notifies client of a character parameter change.
  2989. void clif_updatestatus( map_session_data& sd, enum _sp type ){
  2990. switch(type){
  2991. case SP_WEIGHT:
  2992. pc_updateweightstatus(&sd);
  2993. clif_par_change(sd, type, sd.weight);
  2994. break;
  2995. case SP_MAXWEIGHT:
  2996. clif_par_change(sd, type, sd.max_weight);
  2997. break;
  2998. case SP_SPEED:
  2999. clif_par_change(sd, type, sd.battle_status.speed);
  3000. break;
  3001. case SP_BASELEVEL:
  3002. clif_par_change(sd, type, sd.status.base_level);
  3003. break;
  3004. case SP_JOBLEVEL:
  3005. clif_par_change(sd, type, sd.status.job_level);
  3006. break;
  3007. case SP_KARMA:
  3008. // Adding this back, I wonder if the client intercepts this - [Lance]
  3009. clif_par_change(sd, type, sd.status.karma);
  3010. break;
  3011. case SP_MANNER:
  3012. clif_par_change(sd, type, sd.status.manner);
  3013. break;
  3014. case SP_STATUSPOINT:
  3015. clif_par_change(sd, type, sd.status.status_point);
  3016. break;
  3017. case SP_SKILLPOINT:
  3018. clif_par_change(sd, type, sd.status.skill_point);
  3019. break;
  3020. case SP_HIT:
  3021. clif_par_change(sd, type, sd.battle_status.hit);
  3022. break;
  3023. case SP_FLEE1:
  3024. clif_par_change(sd, type, sd.battle_status.flee);
  3025. break;
  3026. case SP_FLEE2:
  3027. clif_par_change(sd, type, sd.battle_status.flee2/10);
  3028. break;
  3029. case SP_MAXHP:
  3030. clif_par_change(sd, type, sd.battle_status.max_hp);
  3031. break;
  3032. case SP_MAXSP:
  3033. clif_par_change(sd, type, sd.battle_status.max_sp);
  3034. break;
  3035. case SP_HP:
  3036. // On officials the HP never go below 1, even if you die [Lemongrass]
  3037. // On officials the HP Novice class never go below 50%, even if you die [Napster]
  3038. if (sd.battle_status.hp == 0) {
  3039. clif_par_change(sd, type, (sd.class_&MAPID_UPPERMASK) != MAPID_NOVICE ? 1 : sd.battle_status.max_hp/2);
  3040. } else {
  3041. clif_par_change(sd, type, sd.battle_status.hp);
  3042. }
  3043. break;
  3044. case SP_SP:
  3045. clif_par_change(sd, type, sd.battle_status.sp);
  3046. break;
  3047. case SP_ASPD:
  3048. clif_par_change(sd, type, sd.battle_status.amotion);
  3049. break;
  3050. case SP_ATK1:
  3051. clif_par_change(sd, type, pc_leftside_atk(&sd));
  3052. break;
  3053. case SP_DEF1:
  3054. clif_par_change(sd, type, pc_leftside_def(&sd));
  3055. break;
  3056. case SP_MDEF1:
  3057. clif_par_change(sd, type, pc_leftside_mdef(&sd));
  3058. break;
  3059. case SP_ATK2:
  3060. clif_par_change(sd, type, pc_rightside_atk(&sd));
  3061. break;
  3062. case SP_DEF2:
  3063. clif_par_change(sd, type, pc_rightside_def(&sd));
  3064. break;
  3065. case SP_MDEF2: {
  3066. //negative check (in case you have something like Berserk active)
  3067. int mdef2 = pc_rightside_mdef(&sd);
  3068. #ifndef RENEWAL
  3069. clif_par_change(sd, type, mdef2 < 0 ? 0 : mdef2);
  3070. #else
  3071. clif_par_change(sd, type, mdef2);
  3072. #endif
  3073. }
  3074. break;
  3075. case SP_CRITICAL:
  3076. clif_par_change(sd, type, sd.battle_status.cri/10);
  3077. break;
  3078. case SP_MATK1:
  3079. clif_par_change(sd, type, pc_rightside_matk(&sd));
  3080. break;
  3081. case SP_MATK2:
  3082. clif_par_change(sd, type, pc_leftside_matk(&sd));
  3083. break;
  3084. case SP_ZENY:
  3085. clif_longpar_change(sd, type, sd.status.zeny);
  3086. break;
  3087. #if PACKETVER >= 20170830
  3088. case SP_BASEEXP:
  3089. clif_longlongpar_change(sd, type, client_exp(sd.status.base_exp));
  3090. break;
  3091. case SP_JOBEXP:
  3092. clif_longlongpar_change(sd, type, client_exp(sd.status.job_exp));
  3093. break;
  3094. case SP_NEXTBASEEXP:
  3095. clif_longlongpar_change(sd, type, client_exp(pc_nextbaseexp(&sd)));
  3096. break;
  3097. case SP_NEXTJOBEXP:
  3098. clif_longlongpar_change(sd, type, client_exp(pc_nextjobexp(&sd)));
  3099. break;
  3100. #else
  3101. case SP_BASEEXP:
  3102. clif_longpar_change(sd, type, client_exp(sd.status.base_exp));
  3103. break;
  3104. case SP_JOBEXP:
  3105. clif_longpar_change(sd, type, client_exp(sd.status.job_exp));
  3106. break;
  3107. case SP_NEXTBASEEXP:
  3108. clif_longpar_change(sd, type, client_exp(pc_nextbaseexp(&sd)));
  3109. break;
  3110. case SP_NEXTJOBEXP:
  3111. clif_longpar_change(sd, type, client_exp(pc_nextjobexp(&sd)));
  3112. break;
  3113. #endif
  3114. /**
  3115. * SP_U<STAT> are used to update the amount of points necessary to increase that stat
  3116. **/
  3117. case SP_USTR:
  3118. case SP_UAGI:
  3119. case SP_UVIT:
  3120. case SP_UINT:
  3121. case SP_UDEX:
  3122. case SP_ULUK:
  3123. clif_zc_status_change(sd, static_cast<uint16>(type), static_cast<uint8>(pc_need_status_point(&sd, type-SP_USTR+SP_STR, 1)));
  3124. break;
  3125. case SP_ATTACKRANGE:
  3126. clif_attackrange( sd, sd.battle_status.rhw.range );
  3127. break;
  3128. case SP_STR:
  3129. clif_couplestatus(sd, type, sd.status.str, sd.battle_status.str - sd.status.str);
  3130. break;
  3131. case SP_AGI:
  3132. clif_couplestatus(sd, type, sd.status.agi, sd.battle_status.agi - sd.status.agi);
  3133. break;
  3134. case SP_VIT:
  3135. clif_couplestatus(sd, type, sd.status.vit, sd.battle_status.vit - sd.status.vit);
  3136. break;
  3137. case SP_INT:
  3138. clif_couplestatus(sd, type, sd.status.int_, sd.battle_status.int_ - sd.status.int_);
  3139. break;
  3140. case SP_DEX:
  3141. clif_couplestatus(sd, type, sd.status.dex, sd.battle_status.dex - sd.status.dex);
  3142. break;
  3143. case SP_LUK:
  3144. clif_couplestatus(sd, type, sd.status.luk, sd.battle_status.luk - sd.status.luk);
  3145. break;
  3146. case SP_CARTINFO:
  3147. clif_cartcount( sd );
  3148. break;
  3149. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  3150. case SP_AP:
  3151. clif_par_change( sd, SP_AP, sd.battle_status.ap );
  3152. break;
  3153. case SP_TRAITPOINT:
  3154. clif_par_change( sd, SP_TRAITPOINT, sd.status.trait_point );
  3155. break;
  3156. case SP_MAXAP:
  3157. clif_par_change( sd, SP_MAXAP, sd.battle_status.max_ap );
  3158. break;
  3159. case SP_POW:
  3160. clif_couplestatus( sd, SP_POW, sd.status.pow, sd.battle_status.pow - sd.status.pow );
  3161. break;
  3162. case SP_STA:
  3163. clif_couplestatus( sd, SP_STA, sd.status.sta, sd.battle_status.sta - sd.status.sta );
  3164. break;
  3165. case SP_WIS:
  3166. clif_couplestatus( sd, SP_WIS, sd.status.wis, sd.battle_status.wis - sd.status.wis );
  3167. break;
  3168. case SP_SPL:
  3169. clif_couplestatus( sd, SP_SPL, sd.status.spl, sd.battle_status.spl - sd.status.spl );
  3170. break;
  3171. case SP_CON:
  3172. clif_couplestatus( sd, SP_CON, sd.status.con, sd.battle_status.con - sd.status.con );
  3173. break;
  3174. case SP_CRT:
  3175. clif_couplestatus( sd, SP_CRT, sd.status.crt, sd.battle_status.crt - sd.status.crt );
  3176. break;
  3177. case SP_UPOW:
  3178. case SP_USTA:
  3179. case SP_UWIS:
  3180. case SP_USPL:
  3181. case SP_UCON:
  3182. case SP_UCRT:
  3183. clif_zc_status_change( sd, static_cast<uint16>( type ), static_cast<uint8>( pc_need_trait_point( &sd, type - SP_UPOW + SP_POW, 1 ) ) );
  3184. break;
  3185. case SP_PATK:
  3186. clif_par_change( sd, SP_PATK, sd.battle_status.patk );
  3187. break;
  3188. case SP_SMATK:
  3189. clif_par_change( sd, SP_SMATK, sd.battle_status.smatk );
  3190. break;
  3191. case SP_RES:
  3192. clif_par_change( sd, SP_RES, sd.battle_status.res );
  3193. break;
  3194. case SP_MRES:
  3195. clif_par_change( sd, SP_MRES, sd.battle_status.mres );
  3196. break;
  3197. case SP_HPLUS:
  3198. clif_par_change( sd, SP_HPLUS, sd.battle_status.hplus );
  3199. break;
  3200. case SP_CRATE:
  3201. clif_par_change( sd, SP_CRATE, sd.battle_status.crate );
  3202. break;
  3203. #else
  3204. case SP_AP:
  3205. case SP_TRAITPOINT:
  3206. case SP_MAXAP:
  3207. case SP_POW:
  3208. case SP_STA:
  3209. case SP_WIS:
  3210. case SP_SPL:
  3211. case SP_CON:
  3212. case SP_CRT:
  3213. case SP_UPOW:
  3214. case SP_USTA:
  3215. case SP_UWIS:
  3216. case SP_USPL:
  3217. case SP_UCON:
  3218. case SP_UCRT:
  3219. case SP_PATK:
  3220. case SP_SMATK:
  3221. case SP_RES:
  3222. case SP_MRES:
  3223. case SP_HPLUS:
  3224. case SP_CRATE:
  3225. // 4th job status are not supported by older clients
  3226. return;
  3227. #endif
  3228. default:
  3229. ShowError("clif_updatestatus : unrecognized type %d\n",type);
  3230. return;
  3231. }
  3232. // Additional update packets that should be sent right after
  3233. switch( type ){
  3234. case SP_BASELEVEL:
  3235. if( sd.status.party_id ){
  3236. struct party_data* p;
  3237. int i;
  3238. if( ( p = party_search( sd.status.party_id ) ) != nullptr ){
  3239. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd.status.char_id);
  3240. if( i < MAX_PARTY ){
  3241. p->party.member[i].lv = sd.status.base_level;
  3242. clif_party_job_and_level( sd );
  3243. }
  3244. }
  3245. }
  3246. break;
  3247. case SP_HP:
  3248. clif_update_hp( sd );
  3249. break;
  3250. }
  3251. }
  3252. /// Notifies client of a parameter change of an another player.
  3253. /// 01ab <account id>.L <var id>.W <value>.L (ZC_PAR_CHANGE_USER)
  3254. /// var id:
  3255. /// SP_MANNER
  3256. /// ?
  3257. void clif_changemanner( map_session_data& sd ) {
  3258. PACKET_ZC_PAR_CHANGE_USER packet{};
  3259. packet.packetType = HEADER_ZC_PAR_CHANGE_USER;
  3260. packet.gid = sd.bl.id;
  3261. packet.type = static_cast<decltype(packet.type)>(SP_MANNER);
  3262. packet.value = sd.status.manner;
  3263. clif_send( &packet, sizeof( packet ), &sd.bl, AREA_WOS );
  3264. }
  3265. /// 00c3 <id>.L <type>.B <value>.B (ZC_SPRITE_CHANGE)
  3266. /// 01d7 <id>.L <type>.B <value1>.W <value2>.W (ZC_SPRITE_CHANGE2)
  3267. void clif_sprite_change( struct block_list *bl, int id, int type, int val, int val2, enum send_target target ){
  3268. PACKET_ZC_SPRITE_CHANGE p = {};
  3269. p.packetType = sendLookType;
  3270. p.AID = id;
  3271. p.type = type;
  3272. p.val = val;
  3273. #if PACKETVER >= 4
  3274. p.val2 = val2;
  3275. #endif
  3276. clif_send( &p, sizeof( p ), bl, target );
  3277. }
  3278. /// Updates sprite/style properties of an object.
  3279. void clif_changelook(struct block_list *bl, int type, int val) {
  3280. map_session_data* sd;
  3281. status_change* sc;
  3282. struct view_data* vd;
  3283. enum send_target target = AREA;
  3284. int val2 = 0;
  3285. nullpo_retv(bl);
  3286. sd = BL_CAST(BL_PC, bl);
  3287. sc = status_get_sc(bl);
  3288. vd = status_get_viewdata(bl);
  3289. if( vd ) //temp hack to let Warp Portal change appearance
  3290. switch(type) {
  3291. case LOOK_WEAPON:
  3292. if (sd) {
  3293. clif_get_weapon_view(sd, &vd->weapon, &vd->shield);
  3294. val = vd->weapon;
  3295. }
  3296. else
  3297. vd->weapon = val;
  3298. break;
  3299. case LOOK_SHIELD:
  3300. if (sd) {
  3301. clif_get_weapon_view(sd, &vd->weapon, &vd->shield);
  3302. val = vd->shield;
  3303. }
  3304. else
  3305. vd->shield = val;
  3306. break;
  3307. case LOOK_BASE:
  3308. if (!sd)
  3309. break;
  3310. if ( val == JT_INVISIBLE )
  3311. return;
  3312. if (sd->sc.option&OPTION_COSTUME)
  3313. vd->weapon = vd->shield = 0;
  3314. if (!vd->cloth_color)
  3315. break;
  3316. if (sd) {
  3317. if (sd->sc.option&OPTION_WEDDING && battle_config.wedding_ignorepalette)
  3318. vd->cloth_color = 0;
  3319. if (sd->sc.option&OPTION_XMAS && battle_config.xmas_ignorepalette)
  3320. vd->cloth_color = 0;
  3321. if (sd->sc.option&(OPTION_SUMMER|OPTION_SUMMER2) && battle_config.summer_ignorepalette)
  3322. vd->cloth_color = 0;
  3323. if (sd->sc.option&OPTION_HANBOK && battle_config.hanbok_ignorepalette)
  3324. vd->cloth_color = 0;
  3325. if (sd->sc.option&OPTION_OKTOBERFEST && battle_config.oktoberfest_ignorepalette)
  3326. vd->cloth_color = 0;
  3327. if (vd->body_style && sd->sc.option&OPTION_COSTUME)
  3328. vd->body_style = 0;
  3329. }
  3330. break;
  3331. case LOOK_HAIR:
  3332. vd->hair_style = val;
  3333. break;
  3334. case LOOK_HEAD_BOTTOM:
  3335. vd->head_bottom = val;
  3336. break;
  3337. case LOOK_HEAD_TOP:
  3338. vd->head_top = val;
  3339. break;
  3340. case LOOK_HEAD_MID:
  3341. vd->head_mid = val;
  3342. break;
  3343. case LOOK_HAIR_COLOR:
  3344. vd->hair_color = val;
  3345. break;
  3346. case LOOK_CLOTHES_COLOR:
  3347. if (val && sd) {
  3348. if (sd->sc.option&OPTION_WEDDING && battle_config.wedding_ignorepalette)
  3349. val = 0;
  3350. if (sd->sc.option&OPTION_XMAS && battle_config.xmas_ignorepalette)
  3351. val = 0;
  3352. if (sd->sc.option&(OPTION_SUMMER|OPTION_SUMMER2) && battle_config.summer_ignorepalette)
  3353. val = 0;
  3354. if (sd->sc.option&OPTION_HANBOK && battle_config.hanbok_ignorepalette)
  3355. val = 0;
  3356. if (sd->sc.option&OPTION_OKTOBERFEST && battle_config.oktoberfest_ignorepalette)
  3357. val = 0;
  3358. }
  3359. vd->cloth_color = val;
  3360. break;
  3361. case LOOK_SHOES:
  3362. #if PACKETVER > 3
  3363. if (sd) {
  3364. int n;
  3365. if((n = sd->equip_index[EQI_SHOES]) >= 0 && sd->inventory_data[n]) {
  3366. if(sd->inventory_data[n]->view_id > 0)
  3367. val = sd->inventory_data[n]->view_id;
  3368. else
  3369. val = sd->inventory.u.items_inventory[n].nameid;
  3370. } else
  3371. val = 0;
  3372. }
  3373. #endif
  3374. //Shoes? No packet uses this....
  3375. break;
  3376. case LOOK_BODY:
  3377. // unknown purpose
  3378. break;
  3379. case LOOK_ROBE:
  3380. #if PACKETVER < 20110111
  3381. return;
  3382. #else
  3383. vd->robe = val;
  3384. #endif
  3385. break;
  3386. case LOOK_BODY2:
  3387. #if PACKETVER < 20150513
  3388. return;
  3389. #else
  3390. if (val && sd && sd->sc.option&OPTION_COSTUME)
  3391. val = 0;
  3392. vd->body_style = val;
  3393. #endif
  3394. break;
  3395. }
  3396. // prevent leaking the presence of GM-hidden objects
  3397. if( sc && sc->option&OPTION_INVISIBLE )
  3398. target = SELF;
  3399. #if PACKETVER < 4
  3400. if (target != SELF && bl->type == BL_NPC && (((TBL_NPC*)bl)->is_invisible))
  3401. target = SELF;
  3402. clif_sprite_change(bl, bl->id, type, val, 0, target);
  3403. #else
  3404. if (bl->type != BL_NPC) {
  3405. if (type == LOOK_WEAPON || type == LOOK_SHIELD) {
  3406. type = LOOK_WEAPON;
  3407. val = (vd ? vd->weapon : 0);
  3408. val2 = (vd ? vd->shield : 0);
  3409. }
  3410. if (disguised(bl)) {
  3411. clif_sprite_change(bl, bl->id, type, val, val2, AREA_WOS);
  3412. clif_sprite_change(bl, disguised_bl_id(bl->id), type, val, val2, SELF);
  3413. } else
  3414. clif_sprite_change(bl, bl->id, type, val, val2, target);
  3415. } else
  3416. unit_refresh(bl);
  3417. #endif
  3418. }
  3419. //Sends a change-base-look packet required for traps as they are triggered.
  3420. void clif_changetraplook(struct block_list *bl,int val) {
  3421. clif_sprite_change(bl, bl->id, LOOK_BASE, val, 0, AREA);
  3422. }
  3423. /// For the stupid cloth-dye bug. Resends the given view data to the area specified by bl.
  3424. void clif_refreshlook(struct block_list *bl, int id, int type, int val, enum send_target target) {
  3425. clif_sprite_change(bl, id, type, val, 0, target);
  3426. }
  3427. /// Character status (ZC_STATUS).
  3428. /// 00bd <stpoint>.W <str>.B <need str>.B <agi>.B <need agi>.B <vit>.B <need vit>.B
  3429. /// <int>.B <need int>.B <dex>.B <need dex>.B <luk>.B <need luk>.B <atk>.W <atk2>.W
  3430. /// <matk min>.W <matk max>.W <def>.W <def2>.W <mdef>.W <mdef2>.W <hit>.W
  3431. /// <flee>.W <flee2>.W <crit>.W <aspd>.W <aspd2>.W
  3432. void clif_initialstatus( map_session_data& sd ) {
  3433. PACKET_ZC_STATUS packet{};
  3434. packet.packetType = HEADER_ZC_STATUS;
  3435. packet.point = min(sd.status.status_point, INT16_MAX);
  3436. packet.str = min(sd.status.str, UINT8_MAX);
  3437. packet.standardStr = pc_need_status_point( &sd, SP_STR, 1 );
  3438. packet.agi = min(sd.status.agi, UINT8_MAX);
  3439. packet.standardAgi = pc_need_status_point( &sd, SP_AGI,1 );
  3440. packet.vit = min(sd.status.vit, UINT8_MAX);
  3441. packet.standardVit = pc_need_status_point( &sd, SP_VIT,1 );
  3442. packet.int_ = min(sd.status.int_, UINT8_MAX);
  3443. packet.standardInt = pc_need_status_point( &sd, SP_INT,1 );
  3444. packet.dex = min(sd.status.dex, UINT8_MAX);
  3445. packet.standardDex = pc_need_status_point( &sd, SP_DEX,1 );
  3446. packet.luk = min(sd.status.luk, UINT8_MAX);
  3447. packet.standardLuk = pc_need_status_point( &sd, SP_LUK,1 );
  3448. packet.attPower = pc_leftside_atk( &sd );
  3449. packet.refiningPower = pc_rightside_atk( &sd );
  3450. packet.max_mattPower = pc_rightside_matk( &sd );
  3451. packet.min_mattPower = pc_leftside_matk( &sd );
  3452. packet.itemdefPower = pc_leftside_def( &sd );
  3453. packet.plusdefPower = pc_rightside_def( &sd );
  3454. packet.mdefPower = pc_leftside_mdef( &sd );
  3455. #ifdef RENEWAL
  3456. packet.plusmdefPower = pc_rightside_mdef( &sd );
  3457. #else
  3458. // Negative check for Frenzy'ed characters.
  3459. packet.plusmdefPower = std::max( pc_rightside_mdef( &sd ), 0 );
  3460. #endif
  3461. packet.hitSuccessValue = sd.battle_status.hit;
  3462. packet.avoidSuccessValue = sd.battle_status.flee;
  3463. packet.plusAvoidSuccessValue = sd.battle_status.flee2 / 10;
  3464. packet.criticalSuccessValue = sd.battle_status.cri / 10;
  3465. packet.ASPD = sd.battle_status.amotion;
  3466. packet.plusASPD = 0;
  3467. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3468. clif_updatestatus(sd, SP_STR);
  3469. clif_updatestatus(sd, SP_AGI);
  3470. clif_updatestatus(sd, SP_VIT);
  3471. clif_updatestatus(sd, SP_INT);
  3472. clif_updatestatus(sd, SP_DEX);
  3473. clif_updatestatus(sd, SP_LUK);
  3474. clif_updatestatus(sd, SP_ATTACKRANGE);
  3475. clif_updatestatus(sd, SP_ASPD);
  3476. #ifdef RENEWAL
  3477. clif_updatestatus(sd, SP_POW);
  3478. clif_updatestatus(sd, SP_STA);
  3479. clif_updatestatus(sd, SP_WIS);
  3480. clif_updatestatus(sd, SP_SPL);
  3481. clif_updatestatus(sd, SP_CON);
  3482. clif_updatestatus(sd, SP_CRT);
  3483. clif_updatestatus(sd, SP_PATK);
  3484. clif_updatestatus(sd, SP_SMATK);
  3485. clif_updatestatus(sd, SP_RES);
  3486. clif_updatestatus(sd, SP_MRES);
  3487. clif_updatestatus(sd, SP_HPLUS);
  3488. clif_updatestatus(sd, SP_CRATE);
  3489. clif_updatestatus(sd, SP_TRAITPOINT);
  3490. clif_updatestatus(sd, SP_AP);
  3491. clif_updatestatus(sd, SP_MAXAP);
  3492. clif_updatestatus(sd, SP_UPOW);
  3493. clif_updatestatus(sd, SP_USTA);
  3494. clif_updatestatus(sd, SP_UWIS);
  3495. clif_updatestatus(sd, SP_USPL);
  3496. clif_updatestatus(sd, SP_UCON);
  3497. clif_updatestatus(sd, SP_UCRT);
  3498. #endif
  3499. }
  3500. /// Marks an ammunition item in inventory as equipped.
  3501. /// 013c <index>.W (ZC_EQUIP_ARROW)
  3502. void clif_arrowequip( map_session_data& sd ) {
  3503. #if PACKETVER >= 20121128
  3504. clif_status_change(&sd.bl, EFST_CLIENT_ONLY_EQUIP_ARROW, 1, INFINITE_TICK, 0, 0, 0);
  3505. #endif
  3506. PACKET_ZC_EQUIP_ARROW packet{};
  3507. packet.packetType = HEADER_ZC_EQUIP_ARROW;
  3508. // Inventory index of the arrow
  3509. packet.index = client_index( sd.equip_index[EQI_AMMO] );
  3510. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3511. }
  3512. /// Ammunition action message.
  3513. /// 013b <type>.W (ZC_ACTION_FAILURE)
  3514. /// type:
  3515. /// 0 = MsgStringTable[242]="Please equip the proper ammunition first."
  3516. /// 1 = MsgStringTable[243]="You can't Attack or use Skills because your Weight Limit has been exceeded."
  3517. /// 2 = MsgStringTable[244]="You can't use Skills because Weight Limit has been exceeded."
  3518. /// 3 = assassin, baby_assassin, assassin_cross => MsgStringTable[1040]="You have equipped throwing daggers."
  3519. /// gunslinger => MsgStringTable[1175]="Bullets have been equipped."
  3520. /// NOT ninja => MsgStringTable[245]="Ammunition has been equipped."
  3521. void clif_arrow_fail( map_session_data& sd, e_action_failure type ) {
  3522. PACKET_ZC_ACTION_FAILURE packet{};
  3523. packet.packetType = HEADER_ZC_ACTION_FAILURE;
  3524. packet.type = static_cast<decltype(packet.type)>(type);
  3525. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3526. }
  3527. /// Presents a list of items, that can be processed by Arrow Crafting (ZC_MAKINGARROW_LIST).
  3528. /// 01ad <packet len>.W { <name id>.W }*
  3529. void clif_arrow_create_list( map_session_data& sd ){
  3530. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  3531. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  3532. p->packetLength = sizeof( *p );
  3533. int count = 0;
  3534. for (const auto &it : skill_arrow_db) {
  3535. t_itemid nameid = it.second->nameid;
  3536. if( !item_db.exists( nameid ) ){
  3537. continue;
  3538. }
  3539. int index = pc_search_inventory( &sd, nameid );
  3540. if( index < 0 ){
  3541. continue;
  3542. }
  3543. if( sd.inventory.u.items_inventory[index].equip ){
  3544. continue;
  3545. }
  3546. if( !sd.inventory.u.items_inventory[index].identify ){
  3547. continue;
  3548. }
  3549. p->items[count].itemId = client_nameid( nameid );
  3550. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  3551. count++;
  3552. }
  3553. clif_send( p, p->packetLength, &sd.bl, SELF );
  3554. if( count > 0 ){
  3555. sd.menuskill_id = AC_MAKINGARROW;
  3556. sd.menuskill_val = count;
  3557. }
  3558. }
  3559. /// Notifies the client, about the result of an status change request.
  3560. /// 00bc <status id>.W <result>.B <value>.B (ZC_STATUS_CHANGE_ACK)
  3561. /// status id:
  3562. /// SP_STR ~ SP_LUK and SP_POW ~ SP_CRT
  3563. /// result:
  3564. /// 0 = failure
  3565. /// 1 = success
  3566. void clif_statusupack( map_session_data& sd, int32 type, bool success, int32 val ) {
  3567. PACKET_ZC_STATUS_CHANGE_ACK packet{};
  3568. packet.packetType = HEADER_ZC_STATUS_CHANGE_ACK;
  3569. packet.sp = static_cast<decltype(packet.sp)>(type);
  3570. packet.ok = success;
  3571. packet.value = cap_value(val, 0, UINT8_MAX);
  3572. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3573. }
  3574. /// Notifies the client about the result of a request to equip an item.
  3575. /// 00aa <index>.W <equip location>.W <result>.B (ZC_REQ_WEAR_EQUIP_ACK)
  3576. /// 00aa <index>.W <equip location>.W <view id>.W <result>.B (PACKETVER >= 20100629)
  3577. /// 08d0 <index>.W <equip location>.W <view id>.W <result>.B (ZC_REQ_WEAR_EQUIP_ACK2)
  3578. /// 0999 <index>.W <equip location>.L <view id>.W <result>.B (ZC_ACK_WEAR_EQUIP_V5)
  3579. /// @ok: //inversed forv2 v5
  3580. /// 0 = failure
  3581. /// 1 = success
  3582. /// 2 = failure due to low level
  3583. void clif_equipitemack( map_session_data& sd, uint8 flag, int index, int pos ){
  3584. PACKET_ZC_REQ_WEAR_EQUIP_ACK p = {};
  3585. p.PacketType = HEADER_ZC_REQ_WEAR_EQUIP_ACK;
  3586. p.index = client_index( index );
  3587. p.wearLocation = pos;
  3588. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20100629
  3589. if( flag == ITEM_EQUIP_ACK_OK && sd.inventory_data[index]->equip&EQP_VISIBLE ){
  3590. p.wItemSpriteNumber = sd.inventory_data[index]->look;
  3591. }else{
  3592. p.wItemSpriteNumber = 0;
  3593. }
  3594. #endif
  3595. p.result = flag;
  3596. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  3597. }
  3598. /// Notifies the client about the result of a request to take off an item.
  3599. /// 00ac <index>.W <equip location>.W <result>.B (ZC_REQ_TAKEOFF_EQUIP_ACK)
  3600. /// 08d1 <index>.W <equip location>.W <result>.B (ZC_REQ_TAKEOFF_EQUIP_ACK2)
  3601. /// 099a <index>.W <equip location>.L <result>.B (ZC_ACK_TAKEOFF_EQUIP_V5)
  3602. /// @success : //inversed for v2 v5
  3603. /// 0 = failure
  3604. /// 1 = success
  3605. void clif_unequipitemack( map_session_data& sd, uint16 server_index, int32 pos, bool success ){
  3606. #if PACKETVER >= 20110824
  3607. success = !success;
  3608. #endif
  3609. PACKET_ZC_REQ_TAKEOFF_EQUIP_ACK p{};
  3610. p.packetType = HEADER_ZC_REQ_TAKEOFF_EQUIP_ACK;
  3611. p.index = client_index(server_index);
  3612. p.wearLocation = static_cast<decltype(p.wearLocation)>(pos);
  3613. p.flag = success;
  3614. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3615. }
  3616. /// Notifies clients in the area about an special/visual effect.
  3617. /// 019b <id>.L <effect id>.L (ZC_NOTIFY_EFFECT)
  3618. /// effect id:
  3619. /// 0 = base level up
  3620. /// 1 = job level up
  3621. /// 2 = refine failure
  3622. /// 3 = refine success
  3623. /// 4 = game over
  3624. /// 5 = pharmacy success
  3625. /// 6 = pharmacy failure
  3626. /// 7 = base level up (super novice)
  3627. /// 8 = job level up (super novice)
  3628. /// 9 = base level up (taekwon)
  3629. void clif_misceffect( block_list& bl, e_notify_effect type ){
  3630. PACKET_ZC_NOTIFY_EFFECT packet{};
  3631. packet.packetType = HEADER_ZC_NOTIFY_EFFECT;
  3632. packet.aid = bl.id;
  3633. packet.effectId = static_cast<decltype(packet.effectId)>(type);
  3634. clif_send( &packet, sizeof( packet ), &bl, AREA );
  3635. }
  3636. /// Notifies clients in the area of a state change.
  3637. /// 0119 <id>.L <body state>.W <health state>.W <effect state>.W <pk mode>.B (ZC_STATE_CHANGE)
  3638. /// 0229 <id>.L <body state>.W <health state>.W <effect state>.L <pk mode>.B (ZC_STATE_CHANGE3)
  3639. void clif_changeoption_target( struct block_list* bl, struct block_list* target ){
  3640. nullpo_retv( bl );
  3641. status_change* sc = status_get_sc( bl );
  3642. //How can an option change if there's no sc?
  3643. if( sc == nullptr ){
  3644. return;
  3645. }
  3646. map_session_data* sd = BL_CAST( BL_PC, bl );
  3647. PACKET_ZC_STATE_CHANGE p = {};
  3648. p.packetType = HEADER_ZC_STATE_CHANGE;
  3649. p.AID = bl->id;
  3650. p.bodyState = sc->opt1;
  3651. p.healthState = sc->opt2;
  3652. p.effectState = sc->option;
  3653. p.isPKModeON = sd ? sd->status.karma : false;
  3654. if( target == nullptr ){
  3655. if( disguised( bl ) ){
  3656. clif_send( &p, sizeof( p ), bl, AREA_WOS );
  3657. p.AID = disguised_bl_id( p.AID );
  3658. clif_send( &p, sizeof( p ), bl, SELF );
  3659. p.AID = disguised_bl_id( p.AID );
  3660. p.effectState = OPTION_INVISIBLE;
  3661. clif_send( &p, sizeof( p ), bl, SELF );
  3662. }else{
  3663. clif_send( &p, sizeof( p ), bl, AREA );
  3664. }
  3665. //Whenever we send "changeoption" to the client, the provoke icon is lost
  3666. //There is probably an option for the provoke icon, but as we don't know it, we have to do this for now
  3667. if( sc->getSCE(SC_PROVOKE) ){
  3668. const struct TimerData *td = get_timer( sc->getSCE(SC_PROVOKE)->timer );
  3669. clif_status_change( bl, status_db.getIcon(SC_PROVOKE), 1, ( !td ? INFINITE_TICK : DIFF_TICK( td->tick, gettick() ) ), 0, 0, 0 );
  3670. }
  3671. }else{
  3672. if( disguised( bl ) ){
  3673. p.AID = disguised_bl_id( p.AID );
  3674. clif_send( &p, sizeof( p ), target, SELF );
  3675. p.AID = disguised_bl_id( p.AID );
  3676. p.effectState = OPTION_INVISIBLE;
  3677. clif_send( &p, sizeof( p ), target, SELF );
  3678. }else{
  3679. clif_send( &p, sizeof( p ), target, SELF );
  3680. }
  3681. }
  3682. }
  3683. /// Displays status change effects on NPCs/monsters.
  3684. /// 028a <id>.L <effect state>.L <level>.L <showEFST>.L (ZC_NPC_SHOWEFST_UPDATE)
  3685. void clif_changeoption2( block_list& bl ){
  3686. status_change *sc = status_get_sc(&bl);
  3687. if (!sc)
  3688. return; //How can an option change if there's no sc?
  3689. PACKET_ZC_NPC_SHOWEFST_UPDATE packet{};
  3690. packet.packetType = HEADER_ZC_NPC_SHOWEFST_UPDATE;
  3691. packet.gid = bl.id;
  3692. packet.effectState = sc->option;
  3693. packet.level = clif_setlevel(&bl);
  3694. packet.showEFST = sc->opt3;
  3695. if (disguised(&bl)) {
  3696. clif_send( &packet, sizeof( packet ), &bl, AREA_WOS );
  3697. packet.gid = disguised_bl_id( bl.id );
  3698. clif_send( &packet, sizeof( packet ), &bl, SELF );
  3699. packet.gid = bl.id;
  3700. packet.effectState = OPTION_INVISIBLE;
  3701. clif_send( &packet, sizeof( packet ), &bl, SELF );
  3702. } else {
  3703. clif_send( &packet, sizeof( packet ), &bl, AREA );
  3704. }
  3705. }
  3706. /// Notifies the client about the result of an item use request.
  3707. /// 00a8 <index>.W <amount>.W <result>.B (ZC_USE_ITEM_ACK)
  3708. /// 01c8 <index>.W <name id>.W <id>.L <amount>.W <result>.B (ZC_USE_ITEM_ACK2)
  3709. void clif_useitemack( map_session_data *sd, int index, int amount, bool ok ){
  3710. nullpo_retv( sd );
  3711. int fd = sd->fd;
  3712. if( !session_isActive( fd ) ){
  3713. return;
  3714. }
  3715. if( index < 0 || index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0 || sd->inventory_data[index] == nullptr ){
  3716. return;
  3717. }
  3718. PACKET_ZC_USE_ITEM_ACK p = {};
  3719. p.packetType = useItemAckType;
  3720. p.index = index + 2;
  3721. #if PACKETVER > 3
  3722. p.itemId = client_nameid( sd->inventory.u.items_inventory[index].nameid );
  3723. p.AID = sd->bl.id;
  3724. #endif
  3725. p.amount = amount;
  3726. p.result = ok;
  3727. if( !ok ){
  3728. clif_send( &p, sizeof(p), &sd->bl, SELF );
  3729. }else{
  3730. clif_send( &p, sizeof(p), &sd->bl, AREA );
  3731. }
  3732. }
  3733. /// Inform client whether chatroom creation was successful or not.
  3734. /// 00d6 <flag>.B (ZC_ACK_CREATE_CHATROOM)
  3735. /// flag:
  3736. /// 0 = Room has been successfully created (opens chat room)
  3737. /// 1 = Room limit exceeded
  3738. /// 2 = Same room already exists
  3739. void clif_createchat( map_session_data& sd, e_create_chatroom flag ){
  3740. PACKET_ZC_ACK_CREATE_CHATROOM packet{};
  3741. packet.packetType = HEADER_ZC_ACK_CREATE_CHATROOM;
  3742. packet.flag = static_cast<decltype(packet.flag)>(flag);
  3743. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3744. }
  3745. /// Display a chat above the owner (ZC_ROOM_NEWENTRY).
  3746. /// 00d7 <packet len>.W <owner id>.L <char id>.L <limit>.W <users>.W <type>.B <title>.?B
  3747. /// type:
  3748. /// 0 = private (password protected)
  3749. /// 1 = public
  3750. /// 2 = arena (npc waiting room)
  3751. /// 3 = PK zone (non-clickable)
  3752. void clif_dispchat(struct chat_data* cd, int fd)
  3753. {
  3754. unsigned char buf[128];
  3755. uint8 type;
  3756. if( cd == nullptr || cd->owner == nullptr )
  3757. return;
  3758. type = (cd->owner->type == BL_PC ) ? (cd->pub) ? 1 : 0
  3759. : (cd->owner->type == BL_NPC) ? (cd->limit) ? 2 : 3
  3760. : 1;
  3761. WBUFW(buf, 0) = 0xd7;
  3762. WBUFW(buf, 2) = (uint16)(17 + strlen(cd->title));
  3763. WBUFL(buf, 4) = cd->owner->id;
  3764. WBUFL(buf, 8) = cd->bl.id;
  3765. WBUFW(buf,12) = cd->limit;
  3766. WBUFW(buf,14) = (cd->owner->type == BL_NPC) ? cd->users+1 : cd->users;
  3767. WBUFB(buf,16) = type;
  3768. memcpy(WBUFCP(buf,17), cd->title, strlen(cd->title)); // not zero-terminated
  3769. if( session_isActive(fd) ) {
  3770. WFIFOHEAD(fd,WBUFW(buf,2));
  3771. memcpy(WFIFOP(fd,0),buf,WBUFW(buf,2));
  3772. WFIFOSET(fd,WBUFW(buf,2));
  3773. } else {
  3774. clif_send(buf,WBUFW(buf,2),cd->owner,AREA_WOSC);
  3775. }
  3776. }
  3777. /// Chatroom properties adjustment (ZC_CHANGE_CHATROOM).
  3778. /// 00df <packet len>.W <owner id>.L <chat id>.L <limit>.W <users>.W <type>.B <title>.?B
  3779. /// type:
  3780. /// 0 = private (password protected)
  3781. /// 1 = public
  3782. /// 2 = arena (npc waiting room)
  3783. /// 3 = PK zone (non-clickable)
  3784. void clif_changechatstatus(chat_data& cd) {
  3785. if(cd.usersd[0] == nullptr )
  3786. return;
  3787. enum e_chat_flags:uint8 {
  3788. CHAT_PRIVATE = 0,
  3789. CHAT_PUBLIC,
  3790. CHAT_ARENA,
  3791. CHAT_PK
  3792. };
  3793. PACKET_ZC_CHANGE_CHATROOM* p = reinterpret_cast<PACKET_ZC_CHANGE_CHATROOM*>( packet_buffer );
  3794. p->packetType = HEADER_ZC_CHANGE_CHATROOM;
  3795. p->packetSize = static_cast<decltype(p->packetSize)>(sizeof(*p) + strlen(cd.title));
  3796. p->ownerId = cd.owner->id;
  3797. p->chatId = cd.bl.id;
  3798. p->limit = cd.limit;
  3799. p->users = cd.users;
  3800. // not zero-terminated
  3801. strncpy(p->title, cd.title, strlen(cd.title));
  3802. if(cd.owner->type == BL_NPC){
  3803. // NPC itself counts as additional chat user
  3804. p->users++;
  3805. if(cd.limit)
  3806. p->flag = CHAT_ARENA;
  3807. else
  3808. p->flag = CHAT_PK;
  3809. }else if(cd.owner->type == BL_PC && cd.pub == false){
  3810. p->flag = CHAT_PRIVATE;
  3811. }else{
  3812. p->flag = CHAT_PUBLIC;
  3813. }
  3814. clif_send(p,p->packetSize,cd.owner,CHAT);
  3815. }
  3816. /// Removes the chatroom
  3817. /// 00d8 <chat id>.L (ZC_DESTROY_ROOM)
  3818. void clif_clearchat(chat_data &cd){
  3819. PACKET_ZC_DESTROY_ROOM p{};
  3820. p.packetType = HEADER_ZC_DESTROY_ROOM;
  3821. p.chatId = cd.bl.id;
  3822. clif_send(&p,sizeof(p),cd.owner,AREA_WOSC);
  3823. }
  3824. /// Displays messages regarding join chat failures.
  3825. /// 00da <result>.B (ZC_REFUSE_ENTER_ROOM)
  3826. /// result:
  3827. /// 0 = room full
  3828. /// 1 = wrong password
  3829. /// 2 = kicked
  3830. /// 3 = success (no message)
  3831. /// 4 = no enough zeny
  3832. /// 5 = too low level
  3833. /// 6 = too high level
  3834. /// 7 = unsuitable job class
  3835. void clif_joinchatfail( map_session_data& sd, e_refuse_enter_room result ){
  3836. PACKET_ZC_REFUSE_ENTER_ROOM packet{};
  3837. packet.packetType = HEADER_ZC_REFUSE_ENTER_ROOM;
  3838. packet.result = static_cast<decltype(packet.result)>(result);
  3839. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3840. }
  3841. /// Notifies the client about entering a chatroom (ZC_ENTER_ROOM).
  3842. /// 00db <packet len>.W <chat id>.L { <role>.L <name>.24B }*
  3843. /// role:
  3844. /// 0 = owner (menu)
  3845. /// 1 = normal
  3846. void clif_joinchatok(map_session_data& sd, chat_data& cd){
  3847. PACKET_ZC_ENTER_ROOM* p = reinterpret_cast<PACKET_ZC_ENTER_ROOM*>( packet_buffer );
  3848. p->packetType = HEADER_ZC_ENTER_ROOM;
  3849. p->packetSize = sizeof(*p);
  3850. p->chatId = cd.bl.id;
  3851. if(cd.owner->type == BL_NPC){
  3852. PACKET_ZC_ENTER_ROOM_sub& owner = p->members[0];
  3853. owner.flag = 0;
  3854. safestrncpy(owner.name, reinterpret_cast<npc_data*>(cd.owner)->name, sizeof(owner.name));
  3855. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( owner ) );
  3856. for (size_t i = 0; i < cd.users; i++) {
  3857. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i + 1];
  3858. member.flag = 1;
  3859. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3860. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3861. }
  3862. }else{
  3863. for (size_t i = 0; i < cd.users; i++) {
  3864. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i];
  3865. member.flag = i > 0;
  3866. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3867. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3868. }
  3869. }
  3870. clif_send(p,p->packetSize,&sd.bl,SELF);
  3871. }
  3872. /// Notifies clients in a chat about a new member (ZC_MEMBER_NEWENTRY).
  3873. /// 00dc <users>.W <name>.24B
  3874. void clif_addchat(struct chat_data* cd,map_session_data *sd)
  3875. {
  3876. unsigned char buf[29];
  3877. nullpo_retv(sd);
  3878. nullpo_retv(cd);
  3879. WBUFW(buf, 0) = 0xdc;
  3880. WBUFW(buf, 2) = cd->users;
  3881. safestrncpy(WBUFCP(buf, 4),sd->status.name,NAME_LENGTH);
  3882. clif_send(buf,packet_len(0xdc),&sd->bl,CHAT_WOS);
  3883. }
  3884. /// Announce the new owner (ZC_ROLE_CHANGE).
  3885. /// 00e1 <role>.L <nick>.24B
  3886. /// role:
  3887. /// 0 = owner (menu)
  3888. /// 1 = normal
  3889. void clif_changechatowner(struct chat_data* cd, map_session_data* sd)
  3890. {
  3891. unsigned char buf[64];
  3892. nullpo_retv(sd);
  3893. nullpo_retv(cd);
  3894. WBUFW(buf, 0) = 0xe1;
  3895. WBUFL(buf, 2) = 1;
  3896. safestrncpy(WBUFCP(buf,6),cd->usersd[0]->status.name,NAME_LENGTH);
  3897. WBUFW(buf,30) = 0xe1;
  3898. WBUFL(buf,32) = 0;
  3899. safestrncpy(WBUFCP(buf,36),sd->status.name,NAME_LENGTH);
  3900. clif_send(buf,packet_len(0xe1)*2,&sd->bl,CHAT);
  3901. }
  3902. /// Notify about user leaving the chatroom (ZC_MEMBER_EXIT).
  3903. /// 00dd <users>.W <nick>.24B <flag>.B
  3904. /// flag:
  3905. /// 0 = left
  3906. /// 1 = kicked
  3907. void clif_leavechat(struct chat_data* cd, map_session_data* sd, bool flag)
  3908. {
  3909. unsigned char buf[32];
  3910. nullpo_retv(sd);
  3911. nullpo_retv(cd);
  3912. WBUFW(buf, 0) = 0xdd;
  3913. WBUFW(buf, 2) = cd->users-1;
  3914. safestrncpy(WBUFCP(buf,4),sd->status.name,NAME_LENGTH);
  3915. WBUFB(buf,28) = flag;
  3916. clif_send(buf,packet_len(0xdd),&sd->bl,CHAT);
  3917. }
  3918. /// Opens a trade request window from char 'name'.
  3919. /// 00e5 <nick>.24B (ZC_REQ_EXCHANGE_ITEM)
  3920. /// 01f4 <nick>.24B <targetid>.L <baselvl>.W (ZC_REQ_EXCHANGE_ITEM2)
  3921. void clif_traderequest(map_session_data& sd, const char* name){
  3922. PACKET_ZC_REQ_EXCHANGE_ITEM p{};
  3923. p.packetType = HEADER_ZC_REQ_EXCHANGE_ITEM;
  3924. safestrncpy(p.requesterName, name, sizeof(p.requesterName));
  3925. #if PACKETVER > 6
  3926. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  3927. p.targetLv = sd.trade_partner.lv;
  3928. #endif
  3929. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3930. }
  3931. /// Reply to a trade-request.
  3932. /// 00e7 <result>.B (ZC_ACK_EXCHANGE_ITEM)
  3933. /// 01f5 <result>.B <targetid>.L <baselvl>.W (ZC_ACK_EXCHANGE_ITEM2)
  3934. /// result:
  3935. /// 0 = Char is too far
  3936. /// 1 = Character does not exist
  3937. /// 2 = Trade failed
  3938. /// 3 = Accept
  3939. /// 4 = Cancel
  3940. /// 5 = Busy
  3941. void clif_traderesponse( map_session_data& sd, e_ack_trade_response result ){
  3942. PACKET_ZC_ACK_EXCHANGE_ITEM p{};
  3943. p.packetType = HEADER_ZC_ACK_EXCHANGE_ITEM;
  3944. p.result = static_cast<decltype(p.result)>( result );
  3945. #if PACKETVER > 6
  3946. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  3947. p.targetLv = sd.trade_partner.lv;
  3948. #endif
  3949. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3950. }
  3951. /// Notifies the client about an item from other player in current trade.
  3952. /// 00e9 <amount>.L <nameid>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_EXCHANGE_ITEM)
  3953. /// 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)
  3954. /// 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)
  3955. void clif_tradeadditem( map_session_data* sd, map_session_data* tsd, int index, int amount ){
  3956. nullpo_retv( sd );
  3957. nullpo_retv( tsd );
  3958. int fd = tsd->fd;
  3959. if( !session_isActive( fd ) ){
  3960. return;
  3961. }
  3962. PACKET_ZC_ADD_EXCHANGE_ITEM p = {};
  3963. if( index ){
  3964. index = server_index( index );
  3965. if( index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0 || sd->inventory_data[index] == nullptr ){
  3966. return;
  3967. }
  3968. p.itemId = client_nameid( sd->inventory.u.items_inventory[index].nameid );
  3969. #if PACKETVER >= 20100223
  3970. p.itemType = sd->inventory_data[index]->type;
  3971. #endif
  3972. p.identified = sd->inventory.u.items_inventory[index].identify;
  3973. p.damaged = sd->inventory.u.items_inventory[index].attribute;
  3974. p.refine = sd->inventory.u.items_inventory[index].refine;
  3975. clif_addcards( &p.slot, &sd->inventory.u.items_inventory[index] );
  3976. #if PACKETVER >= 20150226
  3977. clif_add_random_options( p.option_data, sd->inventory.u.items_inventory[index] );
  3978. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  3979. p.location = pc_equippoint_sub( sd, sd->inventory_data[index] );
  3980. p.look = sd->inventory_data[index]->look;
  3981. p.grade = sd->inventory.u.items_inventory[index].enchantgrade;
  3982. #endif
  3983. #endif
  3984. }else{
  3985. p = {};
  3986. }
  3987. p.packetType = HEADER_ZC_ADD_EXCHANGE_ITEM;
  3988. p.amount = amount;
  3989. clif_send( &p, sizeof( p ), &tsd->bl, SELF );
  3990. }
  3991. /// Notifies the client about the result of request to add an item to the current trade (ZC_ACK_ADD_EXCHANGE_ITEM).
  3992. /// 00ea <index>.W <result>.B
  3993. /// result:
  3994. /// 0 = success
  3995. /// 1 = overweight
  3996. /// 2 = trade canceled
  3997. void clif_tradeitemok(map_session_data& sd, int index, e_exitem_add_result result)
  3998. {
  3999. PACKET_ZC_ACK_ADD_EXCHANGE_ITEM p = {};
  4000. p.packetType = HEADER_ZC_ACK_ADD_EXCHANGE_ITEM;
  4001. p.index = client_index(index);
  4002. p.result = static_cast<uint8>(result);
  4003. clif_send(&p, sizeof(p), &sd.bl, SELF);
  4004. }
  4005. /// Notifies the client about finishing one side of the current trade.
  4006. /// 00ec <who>.B (ZC_CONCLUDE_EXCHANGE_ITEM)
  4007. /// who:
  4008. /// 0 = self
  4009. /// 1 = other player
  4010. void clif_tradedeal_lock( map_session_data& sd, bool who ){
  4011. PACKET_ZC_CONCLUDE_EXCHANGE_ITEM packet{};
  4012. packet.packetType = HEADER_ZC_CONCLUDE_EXCHANGE_ITEM;
  4013. packet.who = who;
  4014. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4015. }
  4016. /// Notifies the client about the trade being canceled.
  4017. /// 00ee (ZC_CANCEL_EXCHANGE_ITEM)
  4018. void clif_tradecancelled( map_session_data& sd ){
  4019. PACKET_ZC_CANCEL_EXCHANGE_ITEM packet{};
  4020. packet.packetType = HEADER_ZC_CANCEL_EXCHANGE_ITEM;
  4021. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4022. }
  4023. /// Result of a trade.
  4024. /// 00f0 <result>.B (ZC_EXEC_EXCHANGE_ITEM)
  4025. /// result:
  4026. /// 0 = success
  4027. /// 1 = failure
  4028. void clif_tradecompleted( map_session_data& sd ){
  4029. PACKET_ZC_EXEC_EXCHANGE_ITEM packet{};
  4030. packet.packetType = HEADER_ZC_EXEC_EXCHANGE_ITEM;
  4031. packet.result = 0;
  4032. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4033. }
  4034. /// Resets the trade window on the send side.
  4035. /// 00f1 (ZC_EXCHANGEITEM_UNDO)
  4036. /// NOTE: Unknown purpose. Items are not removed until the window is
  4037. /// refreshed (ex. by putting another item in there).
  4038. void clif_tradeundo( map_session_data& sd ){
  4039. PACKET_ZC_EXCHANGEITEM_UNDO packet{};
  4040. packet.packetType = HEADER_ZC_EXCHANGEITEM_UNDO;
  4041. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4042. }
  4043. /// Updates storage total amount.
  4044. /// 00f2 <current count>.W <max count>.W (ZC_NOTIFY_STOREITEM_COUNTINFO)
  4045. void clif_updatestorageamount( map_session_data& sd, uint16 amount, uint16 max_amount ){
  4046. PACKET_ZC_NOTIFY_STOREITEM_COUNTINFO packet{};
  4047. packet.packetType = HEADER_ZC_NOTIFY_STOREITEM_COUNTINFO;
  4048. packet.amount = amount;
  4049. packet.max_amount = max_amount;
  4050. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4051. }
  4052. /// Notifies the client of an item being added to the storage.
  4053. /// 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)
  4054. /// 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)
  4055. /// 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)
  4056. void clif_storageitemadded( map_session_data* sd, struct item* i, int index, int amount ){
  4057. nullpo_retv( sd );
  4058. nullpo_retv( i );
  4059. int fd = sd->fd;
  4060. if( !session_isActive( fd ) ){
  4061. return;
  4062. }
  4063. PACKET_ZC_ADD_ITEM_TO_STORE p = {};
  4064. p.packetType = HEADER_ZC_ADD_ITEM_TO_STORE; // Storage item added
  4065. p.index = client_storage_index( index ); // index
  4066. p.amount = amount; // amount
  4067. p.itemId = client_nameid( i->nameid ); // id
  4068. #if PACKETVER >= 5
  4069. p.itemType = itemtype( i->nameid ); //type
  4070. #endif
  4071. p.identified = i->identify; //identify flag
  4072. p.damaged = i->attribute; // attribute
  4073. p.refine = i->refine; //refine
  4074. clif_addcards( &p.slot, i );
  4075. #if PACKETVER_MAIN_NUM >= 20140813 || PACKETVER_RE_NUM >= 20140402 || defined(PACKETVER_ZERO)
  4076. clif_add_random_options( p.option_data, *i );
  4077. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20221024
  4078. p.grade = i->enchantgrade;
  4079. #endif
  4080. #endif
  4081. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  4082. }
  4083. /// Notifies the client of an item being deleted from the storage.
  4084. /// 00f6 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_STORE)
  4085. void clif_storageitemremoved( map_session_data& sd, uint16 index, uint32 amount ){
  4086. PACKET_ZC_DELETE_ITEM_FROM_STORE packet{};
  4087. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_STORE;
  4088. packet.index = client_storage_index(index);
  4089. packet.amount = amount;
  4090. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4091. }
  4092. /// Closes storage.
  4093. /// 00f8 (ZC_CLOSE_STORE)
  4094. void clif_storageclose( map_session_data& sd ){
  4095. PACKET_ZC_CLOSE_STORE packet{};
  4096. packet.packetType = HEADER_ZC_CLOSE_STORE;
  4097. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4098. }
  4099. /// Notifies clients in an area of a player's souls.
  4100. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  4101. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  4102. /// 0b73 <id>.L <amount>.W (ZC_SOULENERGY)
  4103. void clif_soulball( map_session_data *sd, struct block_list* target, enum send_target send_target ){
  4104. #if PACKETVER_MAIN_NUM >= 20200414 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20200506
  4105. PACKET_ZC_SOULENERGY p = {};
  4106. p.PacketType = HEADER_ZC_SOULENERGY;
  4107. #else
  4108. PACKET_ZC_SPIRITS p = {};
  4109. p.PacketType = HEADER_ZC_SPIRITS;
  4110. #endif
  4111. p.AID = sd->bl.id;
  4112. p.num = sd->soulball;
  4113. clif_send( &p, sizeof( p ), target == nullptr ? &sd->bl : target, send_target );
  4114. }
  4115. /*==========================================
  4116. * Server tells 'sd' player client the abouts of 'dstsd' player
  4117. *------------------------------------------*/
  4118. static void clif_getareachar_pc(map_session_data* sd,map_session_data* dstsd)
  4119. {
  4120. struct block_list *d_bl;
  4121. int i;
  4122. if( dstsd->chatID ) {
  4123. struct chat_data *cd = nullptr;
  4124. if( (cd = (struct chat_data*)map_id2bl(dstsd->chatID)) && cd->usersd[0]==dstsd)
  4125. clif_dispchat(cd,sd->fd);
  4126. } else if( dstsd->state.vending )
  4127. clif_showvendingboard( *dstsd, SELF, &sd->bl );
  4128. else if( dstsd->state.buyingstore )
  4129. clif_buyingstore_entry( *dstsd, &sd->bl );
  4130. if(dstsd->spiritball > 0)
  4131. clif_spiritball( &dstsd->bl, &sd->bl, SELF );
  4132. clif_millenniumshield_single( *dstsd, *sd );
  4133. clif_spiritcharm_single( *dstsd, *sd );
  4134. if (dstsd->soulball > 0)
  4135. clif_soulball( dstsd, &sd->bl, SELF );
  4136. if (dstsd->servantball > 0)
  4137. clif_servantball( *dstsd, &sd->bl, SELF );
  4138. if (dstsd->abyssball > 0)
  4139. clif_abyssball( *dstsd, &sd->bl, SELF );
  4140. if( (sd->status.party_id && dstsd->status.party_id == sd->status.party_id) || //Party-mate, or hpdisp setting.
  4141. (sd->bg_id && sd->bg_id == dstsd->bg_id) || //BattleGround
  4142. pc_has_permission(sd, PC_PERM_VIEW_HPMETER)
  4143. )
  4144. clif_hpmeter_single( *sd, dstsd->bl.id, dstsd->battle_status.hp, dstsd->battle_status.max_hp );
  4145. // display link (sd - dstsd) to sd
  4146. ARR_FIND( 0, MAX_DEVOTION, i, sd->devotion[i] == dstsd->bl.id );
  4147. if( i < MAX_DEVOTION )
  4148. clif_devotion(&sd->bl, sd);
  4149. // display links (dstsd - devotees) to sd
  4150. ARR_FIND( 0, MAX_DEVOTION, i, dstsd->devotion[i] > 0 );
  4151. if( i < MAX_DEVOTION )
  4152. clif_devotion(&dstsd->bl, sd);
  4153. // display link (dstsd - crusader) to sd
  4154. if( dstsd->sc.getSCE(SC_DEVOTION) && (d_bl = map_id2bl(dstsd->sc.getSCE(SC_DEVOTION)->val1)) != nullptr )
  4155. clif_devotion(d_bl, sd);
  4156. }
  4157. void clif_getareachar_unit( map_session_data* sd,struct block_list *bl ){
  4158. struct unit_data *ud;
  4159. struct view_data *vd;
  4160. bool option = false;
  4161. unsigned int option_val = 0;
  4162. vd = status_get_viewdata(bl);
  4163. if (!vd || vd->class_ == JT_INVISIBLE)
  4164. return;
  4165. // Hide NPC from Maya Purple card
  4166. if (clif_npc_mayapurple(bl))
  4167. return;
  4168. if( bl->type == BL_NPC && npc_is_hidden_dynamicnpc( *( (struct npc_data*)bl ), *sd ) ){
  4169. // Do not send anything
  4170. return;
  4171. }
  4172. ud = unit_bl2ud(bl);
  4173. if( ud && ud->walktimer != INVALID_TIMER ){
  4174. clif_set_unit_walking( *bl, sd, *ud, SELF );
  4175. }else{
  4176. clif_set_unit_idle( bl, false, SELF, &sd->bl );
  4177. }
  4178. if (vd->cloth_color)
  4179. clif_refreshlook(&sd->bl,bl->id,LOOK_CLOTHES_COLOR,vd->cloth_color,SELF);
  4180. if (vd->body_style)
  4181. clif_refreshlook(&sd->bl,bl->id,LOOK_BODY2,vd->body_style,SELF);
  4182. switch (bl->type)
  4183. {
  4184. case BL_PC:
  4185. {
  4186. TBL_PC* tsd = (TBL_PC*)bl;
  4187. clif_getareachar_pc(sd, tsd);
  4188. if(tsd->state.size==SZ_BIG) // tiny/big players [Valaris]
  4189. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4190. else if(tsd->state.size==SZ_MEDIUM)
  4191. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4192. if( tsd->bg_id && map_getmapflag(tsd->bl.m, MF_BATTLEGROUND) )
  4193. clif_sendbgemblem_single(sd->fd,tsd);
  4194. if ( tsd->status.robe )
  4195. clif_refreshlook(&sd->bl,bl->id,LOOK_ROBE,tsd->status.robe,SELF);
  4196. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4197. clif_hat_effects( *sd, tsd->bl, SELF );
  4198. }
  4199. break;
  4200. case BL_MER: // Devotion Effects
  4201. if( ((TBL_MER*)bl)->devotion_flag )
  4202. clif_devotion(bl, sd);
  4203. break;
  4204. case BL_NPC:
  4205. {
  4206. TBL_NPC* nd = (TBL_NPC*)bl;
  4207. if( nd->chat_id )
  4208. clif_dispchat((struct chat_data*)map_id2bl(nd->chat_id),sd->fd);
  4209. if( nd->size == SZ_BIG )
  4210. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4211. else if( nd->size == SZ_MEDIUM )
  4212. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4213. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4214. clif_progressbar_npc(nd, sd);
  4215. }
  4216. break;
  4217. case BL_MOB:
  4218. {
  4219. TBL_MOB* md = (TBL_MOB*)bl;
  4220. if(md->special_state.size==SZ_BIG) // tiny/big mobs [Valaris]
  4221. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4222. else if(md->special_state.size==SZ_MEDIUM)
  4223. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4224. #if PACKETVER >= 20120404
  4225. if (battle_config.monster_hp_bars_info && !map_getmapflag(bl->m, MF_HIDEMOBHPBAR)) {
  4226. int i;
  4227. for(i = 0; i < DAMAGELOG_SIZE; i++)// must show hp bar to all char who already hit the mob.
  4228. if( md->dmglog[i].id == sd->status.char_id )
  4229. clif_monster_hp_bar(md, sd->fd);
  4230. }
  4231. #endif
  4232. }
  4233. break;
  4234. case BL_PET:
  4235. if (vd->head_bottom)
  4236. clif_pet_equip(sd, (TBL_PET*)bl); // needed to display pet equip properly
  4237. break;
  4238. }
  4239. }
  4240. //Modifies the type of damage according to status changes [Skotlex]
  4241. //Aegis data specifies that: 4 endure against single hit sources, 9 against multi-hit.
  4242. static enum e_damage_type clif_calc_delay(enum e_damage_type type, int div, int64 damage, int delay)
  4243. {
  4244. return ( delay == 0 && damage > 0 ) ? ( div > 1 ? DMG_MULTI_HIT_ENDURE : DMG_ENDURE ) : type;
  4245. }
  4246. /*==========================================
  4247. * Estimates walk delay based on the damage criteria. [Skotlex]
  4248. *------------------------------------------*/
  4249. static int clif_calc_walkdelay(struct block_list *bl,int delay, char type, int64 damage, int div_)
  4250. {
  4251. if (type == DMG_ENDURE || type == DMG_MULTI_HIT_ENDURE || damage <= 0)
  4252. return 0;
  4253. if (bl->type == BL_PC) {
  4254. if (battle_config.pc_walk_delay_rate != 100)
  4255. delay = delay*battle_config.pc_walk_delay_rate/100;
  4256. } else
  4257. if (battle_config.walk_delay_rate != 100)
  4258. delay = delay*battle_config.walk_delay_rate/100;
  4259. if (div_ > 1) //Multi-hit skills mean higher delays.
  4260. delay += battle_config.multihit_delay*(div_-1);
  4261. return (delay > 0) ? delay:1; //Return 1 to specify there should be no noticeable delay, but you should stop walking.
  4262. }
  4263. /*========================================== [Playtester]
  4264. * Returns hallucination damage the client displays
  4265. *------------------------------------------*/
  4266. static int clif_hallucination_damage()
  4267. {
  4268. int digit = rnd() % 5 + 1;
  4269. switch (digit)
  4270. {
  4271. case 1:
  4272. return (rnd() % 10);
  4273. case 2:
  4274. return (rnd() % 100);
  4275. case 3:
  4276. return (rnd() % 1000);
  4277. case 4:
  4278. return (rnd() % 10000);
  4279. }
  4280. return (rnd() % 32767);
  4281. }
  4282. #define DEFAULT_ANIMATION_SPEED 432
  4283. /// Sends a 'damage' packet (src performs action on dst)
  4284. /// 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)
  4285. /// 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)
  4286. /// 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)
  4287. /// type:
  4288. /// 0 = damage [ damage: total damage, div: amount of hits, damage2: assassin dual-wield damage ]
  4289. /// 1 = pick up item
  4290. /// 2 = sit down
  4291. /// 3 = stand up
  4292. /// 4 = damage (endure)
  4293. /// 5 = (splash?)
  4294. /// 6 = (skill?)
  4295. /// 7 = (repeat damage?)
  4296. /// 8 = multi-hit damage
  4297. /// 9 = multi-hit damage (endure)
  4298. /// 10 = critical hit
  4299. /// 11 = lucky dodge
  4300. /// 12 = (touch skill?)
  4301. /// 13 = multi-hit critical
  4302. int clif_damage(block_list& src, block_list& dst, t_tick tick, int sdelay, int ddelay, int64 sdamage, int div, enum e_damage_type type, int64 sdamage2, bool spdamage){
  4303. int damage = (int)cap_value(sdamage,INT_MIN,INT_MAX);
  4304. int damage2 = (int)cap_value(sdamage2,INT_MIN,INT_MAX);
  4305. if (type != DMG_MULTI_HIT_CRITICAL)
  4306. type = clif_calc_delay(type,div,damage+damage2,ddelay);
  4307. status_change *sc = status_get_sc(&dst);
  4308. if(sc && sc->count) {
  4309. if(sc->getSCE(SC_HALLUCINATION)) {
  4310. damage = clif_hallucination_damage();
  4311. if(damage2)
  4312. damage2 = clif_hallucination_damage();
  4313. }
  4314. }
  4315. // Calculate what sdelay to send to the client so it applies damage at the same time as the server
  4316. if (battle_config.synchronize_damage && src.type == BL_MOB) {
  4317. // When a clif_damage packet is sent to the client it will also send "sdelay" (amotion) as value.
  4318. // The client however does not interpret this value as AttackMotion but incorrectly as an inverted
  4319. // animation speed modifier, with 432 standing for 1x animation speed.
  4320. // The client will ignore all values above 432, but lower values will speed up the animation.
  4321. // 216 for example means play the animation at double the speed. 108 is quadruple speed.
  4322. // Each monster has an attack animation and may define the frame in the attack animation on which
  4323. // it displays the damage and makes the target flinch / stop. If the damage frame is undefined,
  4324. // it instead displays the damage / flinch / stop at the beginning of the second to last frame.
  4325. // We define the time after which the damage frame shows at 1x speed as clientamotion.
  4326. uint16 clientamotion = std::max((uint16)1, status_get_clientamotion(&src));
  4327. // Knowing when the damage frame happens in the animation allows us to synchronize the timing
  4328. // between client and server using the formula below.
  4329. sdelay = sdelay * DEFAULT_ANIMATION_SPEED / clientamotion;
  4330. // Hint: If amotion is larger than clientamotion this results in a value above 432 which makes the
  4331. // client display the attack at 1x speed. In this case we need to shorten the delay damage timer
  4332. // on the server to clientamotion ms instead (see battle_delay_damage).
  4333. sdelay = std::min(sdelay, DEFAULT_ANIMATION_SPEED);
  4334. }
  4335. PACKET_ZC_NOTIFY_ACT p{};
  4336. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4337. p.srcID = src.id;
  4338. p.targetID = dst.id;
  4339. p.serverTick = client_tick(tick);
  4340. p.srcSpeed = sdelay;
  4341. p.dmgSpeed = ddelay;
  4342. if (battle_config.hide_woe_damage && map_flag_gvg(src.m)) {
  4343. p.damage = damage ? div : 0;
  4344. p.damage2 = damage2 ? div : 0;
  4345. } else {
  4346. p.damage = static_cast<decltype(p.damage)>( std::min( damage, static_cast<decltype(damage)>( std::numeric_limits<decltype(p.damage)>::max() ) ) );
  4347. p.damage2 = static_cast<decltype(p.damage2)>( std::min( damage2, static_cast<decltype(damage2)>( std::numeric_limits<decltype(p.damage2)>::max() ) ) );
  4348. }
  4349. #if PACKETVER >= 20131223
  4350. p.isSPDamage = (spdamage) ? 1 : 0; // IsSPDamage - Displays blue digits.
  4351. #endif
  4352. p.div = div;
  4353. p.type = type;
  4354. if(disguised(&dst)) {
  4355. clif_send( &p, sizeof(p), &dst, AREA_WOS);
  4356. p.targetID = disguised_bl_id( dst.id );
  4357. clif_send( &p, sizeof(p), &dst, SELF);
  4358. } else
  4359. clif_send(&p, sizeof(p), &dst, AREA);
  4360. if(disguised(&src)) {
  4361. p.srcID = disguised_bl_id( src.id );
  4362. if(damage > 0)
  4363. p.damage = -1;
  4364. if(damage2 > 0)
  4365. p.damage2 = -1;
  4366. clif_send( &p, sizeof(p), &src, SELF);
  4367. }
  4368. if(&src == &dst)
  4369. unit_setdir(&src, unit_getdir(&src));
  4370. // In case this assignment is bypassed by DMG_MULTI_HIT_CRITICAL
  4371. type = clif_calc_delay(type, div, damage + damage2, ddelay);
  4372. //Return adjusted can't walk delay for further processing.
  4373. return clif_calc_walkdelay(&dst, ddelay, type, damage+damage2, div);
  4374. }
  4375. /*==========================================
  4376. * src picks up dst
  4377. *------------------------------------------*/
  4378. void clif_takeitem(block_list& src, block_list& dst){
  4379. PACKET_ZC_NOTIFY_ACT p{};
  4380. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4381. p.srcID = src.id;
  4382. p.targetID = dst.id;
  4383. p.type = DMG_PICKUP_ITEM;
  4384. clif_send(&p, sizeof(p), &src, AREA);
  4385. }
  4386. /*==========================================
  4387. * inform clients in area that `bl` is sitting
  4388. *------------------------------------------*/
  4389. void clif_sitting(block_list& bl){
  4390. PACKET_ZC_NOTIFY_ACT p{};
  4391. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4392. p.srcID = bl.id;
  4393. p.type = DMG_SIT_DOWN;
  4394. clif_send(&p, sizeof(p), &bl, AREA);
  4395. if(disguised(&bl)) {
  4396. p.srcID = disguised_bl_id( bl.id );
  4397. clif_send(&p, sizeof(p), &bl, SELF);
  4398. }
  4399. }
  4400. /*==========================================
  4401. * inform clients in area that `bl` is standing
  4402. *------------------------------------------*/
  4403. void clif_standing(block_list& bl){
  4404. PACKET_ZC_NOTIFY_ACT p{};
  4405. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4406. p.srcID = bl.id;
  4407. p.type = DMG_STAND_UP;
  4408. clif_send(&p, sizeof(p), &bl, AREA);
  4409. if(disguised(&bl)) {
  4410. p.srcID = disguised_bl_id( bl.id );
  4411. clif_send(&p, sizeof(p), &bl, SELF);
  4412. }
  4413. }
  4414. /// Inform client(s) about a map-cell change (ZC_UPDATE_MAPINFO).
  4415. /// 0192 <x>.W <y>.W <type>.W <map name>.16B
  4416. void clif_changemapcell(int fd, int16 m, int x, int y, int type, enum send_target target)
  4417. {
  4418. unsigned char buf[32];
  4419. WBUFW(buf,0) = 0x192;
  4420. WBUFW(buf,2) = x;
  4421. WBUFW(buf,4) = y;
  4422. WBUFW(buf,6) = type;
  4423. mapindex_getmapname_ext(map_mapid2mapname(m),WBUFCP(buf,8));
  4424. if( session_isActive(fd) )
  4425. {
  4426. WFIFOHEAD(fd,packet_len(0x192));
  4427. memcpy(WFIFOP(fd,0), buf, packet_len(0x192));
  4428. WFIFOSET(fd,packet_len(0x192));
  4429. }
  4430. else
  4431. {
  4432. struct block_list dummy_bl;
  4433. dummy_bl.type = BL_NUL;
  4434. dummy_bl.x = x;
  4435. dummy_bl.y = y;
  4436. dummy_bl.m = m;
  4437. clif_send(buf,packet_len(0x192),&dummy_bl,target);
  4438. }
  4439. }
  4440. /// Notifies the client about an item on floor (ZC_ITEM_ENTRY).
  4441. /// 009d <id>.L <name id>.W <identified>.B <x>.W <y>.W <amount>.W <subX>.B <subY>.B
  4442. void clif_getareachar_item( map_session_data* sd,struct flooritem_data* fitem ){
  4443. nullpo_retv( sd );
  4444. nullpo_retv( fitem );
  4445. PACKET_ZC_ITEM_ENTRY p = {};
  4446. p.packetType = HEADER_ZC_ITEM_ENTRY;
  4447. p.AID = fitem->bl.id;
  4448. p.itemId = client_nameid( fitem->item.nameid );
  4449. p.identify = fitem->item.identify;
  4450. p.x = fitem->bl.x;
  4451. p.y = fitem->bl.y;
  4452. p.amount = fitem->item.amount;
  4453. p.subX = fitem->subx;
  4454. p.subY = fitem->suby;
  4455. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  4456. }
  4457. /// Notifes client about Graffiti
  4458. /// 01c9 <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B <has msg>.B <msg>.80B (ZC_SKILL_ENTRY2)
  4459. static void clif_graffiti(struct block_list *bl, struct skill_unit *unit, enum send_target target) {
  4460. unsigned char buf[128];
  4461. nullpo_retv(bl);
  4462. nullpo_retv(unit);
  4463. WBUFW(buf, 0) = 0x1c9;
  4464. WBUFL(buf, 2) = unit->bl.id;
  4465. WBUFL(buf, 6) = unit->group->src_id;
  4466. WBUFW(buf,10) = unit->bl.x;
  4467. WBUFW(buf,12) = unit->bl.y;
  4468. WBUFB(buf,14) = unit->group->unit_id;
  4469. WBUFB(buf,15) = 1;
  4470. WBUFB(buf,16) = 1;
  4471. safestrncpy(WBUFCP(buf,17),unit->group->valstr,MESSAGE_SIZE);
  4472. clif_send(buf,packet_len(0x1c9),bl,target);
  4473. }
  4474. /// Notifies the client of a skill unit.
  4475. /// 011f <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B (ZC_SKILL_ENTRY)
  4476. /// 08c7 <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.B <range>.W <visible>.B (ZC_SKILL_ENTRY3)
  4477. /// 099f <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.L <range>.W <visible>.B (ZC_SKILL_ENTRY4)
  4478. /// 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)
  4479. void clif_getareachar_skillunit(struct block_list *bl, struct skill_unit *unit, enum send_target target, bool visible) {
  4480. int header = 0, unit_id = 0, pos = 0, fd = 0, len = -1;
  4481. unsigned char buf[128];
  4482. nullpo_retv(bl);
  4483. nullpo_retv(unit);
  4484. if (bl->type == BL_PC)
  4485. fd = ((TBL_PC*)bl)->fd;
  4486. if (unit->group->state.guildaura)
  4487. return;
  4488. if (unit->group->state.song_dance&0x1 && unit->val2&(1 << UF_ENSEMBLE))
  4489. unit_id = unit->val2&(1 << UF_SONG) ? UNT_DISSONANCE : UNT_UGLYDANCE;
  4490. else if (skill_get_unit_flag(unit->group->skill_id, UF_RANGEDSINGLEUNIT) && !(unit->val2 & (1 << UF_RANGEDSINGLEUNIT)))
  4491. unit_id = UNT_DUMMYSKILL; // Use invisible unit id for other case of rangedsingle unit
  4492. else
  4493. unit_id = unit->group->unit_id;
  4494. if (!visible)
  4495. unit_id = UNT_DUMMYSKILL; // Hack to makes hidden trap really hidden!
  4496. #if PACKETVER >= 3
  4497. if (unit_id == UNT_GRAFFITI) { // Graffiti [Valaris]
  4498. clif_graffiti(bl, unit, target);
  4499. return;
  4500. }
  4501. #endif
  4502. #if PACKETVER <= 20120702
  4503. header = 0x011f;
  4504. //#if PACKETVER < 20110718
  4505. // header = 0x011f;
  4506. //#elif PACKETVER < 20121212
  4507. // header = 0x08c7;
  4508. #elif PACKETVER < 20130731
  4509. header = 0x099f;
  4510. #else
  4511. header = 0x09ca;
  4512. #endif
  4513. len = packet_len(header);
  4514. WBUFW(buf,pos) = header;
  4515. if (header != 0x011f) {
  4516. WBUFW(buf, pos+2) = len;
  4517. pos += 2;
  4518. }
  4519. WBUFL(buf,pos+2) = unit->bl.id;
  4520. WBUFL(buf,pos+6) = unit->group->src_id;
  4521. WBUFW(buf,pos+10) = unit->bl.x;
  4522. WBUFW(buf,pos+12) = unit->bl.y;
  4523. switch (header) {
  4524. case 0x011f:
  4525. WBUFB(buf,pos+14) = unit_id;
  4526. WBUFB(buf,pos+15) = visible;
  4527. break;
  4528. case 0x08c7:
  4529. WBUFB(buf,pos+14) = unit_id;
  4530. WBUFW(buf,pos+15) = unit->range;
  4531. WBUFB(buf,pos+17) = visible;
  4532. break;
  4533. case 0x099f:
  4534. WBUFL(buf,pos+14) = unit_id;
  4535. WBUFW(buf,pos+18) = unit->range;
  4536. WBUFB(buf,pos+20) = visible;
  4537. break;
  4538. case 0x09ca:
  4539. WBUFL(buf,pos+14) = unit_id;
  4540. WBUFB(buf,pos+18) = (unsigned char)unit->range;
  4541. WBUFB(buf,pos+19) = visible;
  4542. WBUFB(buf,pos+20) = (unsigned char)unit->group->skill_lv;
  4543. break;
  4544. }
  4545. clif_send(buf, len, bl, target);
  4546. if (unit->group->skill_id == WZ_ICEWALL)
  4547. clif_changemapcell(fd, unit->bl.m, unit->bl.x, unit->bl.y, 5, SELF);
  4548. }
  4549. /// 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)
  4550. void clif_skill_unit_test(struct block_list *bl, short x, short y, int unit_id, short range, short skill_lv) {
  4551. unsigned char buf[128];
  4552. nullpo_retv(bl);
  4553. WBUFW(buf, 0) = 0x09ca;
  4554. WBUFW(buf, 2) = packet_len(0x09ca);
  4555. WBUFL(buf, 4) = 1000;
  4556. WBUFL(buf, 8) = 2000;
  4557. WBUFW(buf, 12) = x;
  4558. WBUFW(buf, 14) = y;
  4559. WBUFL(buf, 16) = unit_id;
  4560. WBUFB(buf, 20) = (unsigned char)range;
  4561. WBUFB(buf, 21) = 1;
  4562. WBUFB(buf, 22) = (unsigned char)skill_lv;
  4563. clif_send(buf, packet_len(0x09ca), bl, AREA);
  4564. }
  4565. /// Server tells client to remove unit of id 'unit->bl.id'
  4566. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4567. static void clif_clearchar_skillunit( skill_unit& unit, map_session_data& sd ){
  4568. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4569. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4570. packet.GID = unit.bl.id;
  4571. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4572. if( unit.group && unit.group->skill_id == WZ_ICEWALL ){
  4573. clif_changemapcell( sd.fd, unit.bl.m, unit.bl.x, unit.bl.y, unit.val2, SELF );
  4574. }
  4575. }
  4576. /// Removes a skill unit.
  4577. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4578. void clif_skill_delunit( skill_unit& unit ){
  4579. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4580. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4581. packet.GID = unit.bl.id;
  4582. clif_send( &packet, sizeof( packet ), &unit.bl, AREA );
  4583. }
  4584. /// Sent when an object gets ankle-snared.
  4585. /// 01ac <id>.L (ZC_SKILL_UPDATE)
  4586. /// Only affects units with class [139,153] client-side.
  4587. void clif_skillunit_update( block_list& bl ){
  4588. PACKET_ZC_SKILL_UPDATE packet{};
  4589. packet.packetType = HEADER_ZC_SKILL_UPDATE;
  4590. packet.GID = bl.id;
  4591. clif_send( &packet, sizeof( packet ), &bl, AREA );
  4592. }
  4593. /*==========================================
  4594. *
  4595. *------------------------------------------*/
  4596. static int clif_getareachar(struct block_list* bl,va_list ap)
  4597. {
  4598. map_session_data *sd;
  4599. nullpo_ret(bl);
  4600. sd=va_arg(ap,map_session_data*);
  4601. if (!clif_session_isValid(sd))
  4602. return 0;
  4603. switch(bl->type){
  4604. case BL_ITEM:
  4605. clif_getareachar_item(sd,(struct flooritem_data*) bl);
  4606. break;
  4607. case BL_SKILL:
  4608. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &sd->bl);
  4609. break;
  4610. default:
  4611. if(&sd->bl == bl)
  4612. break;
  4613. clif_getareachar_unit(sd,bl);
  4614. break;
  4615. }
  4616. return 0;
  4617. }
  4618. /*==========================================
  4619. * tbl has gone out of view-size of bl
  4620. *------------------------------------------*/
  4621. int clif_outsight(struct block_list *bl,va_list ap)
  4622. {
  4623. struct block_list *tbl;
  4624. struct view_data *vd;
  4625. TBL_PC *sd, *tsd;
  4626. tbl=va_arg(ap,struct block_list*);
  4627. if(bl == tbl) return 0;
  4628. sd = BL_CAST(BL_PC, bl);
  4629. tsd = BL_CAST(BL_PC, tbl);
  4630. if (clif_session_isValid(tsd)) { //tsd has lost sight of the bl object.
  4631. nullpo_ret(bl);
  4632. switch(bl->type){
  4633. case BL_PC:
  4634. if(sd->vd.class_ != JT_INVISIBLE)
  4635. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4636. if(sd->chatID){
  4637. struct chat_data *cd;
  4638. cd=(struct chat_data*)map_id2bl(sd->chatID);
  4639. if(cd->usersd[0]==sd)
  4640. clif_dispchat(cd,tsd->fd);
  4641. }
  4642. if(sd->state.vending)
  4643. clif_closevendingboard(bl,tsd->fd);
  4644. if(sd->state.buyingstore)
  4645. clif_buyingstore_disappear_entry( *sd, &tsd->bl );
  4646. break;
  4647. case BL_ITEM:
  4648. clif_clearflooritem( *reinterpret_cast<flooritem_data*>( bl ), tsd );
  4649. break;
  4650. case BL_SKILL:
  4651. clif_clearchar_skillunit( *((skill_unit *)bl), *tsd );
  4652. break;
  4653. case BL_NPC:
  4654. if(!(((TBL_NPC*)bl)->is_invisible))
  4655. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4656. break;
  4657. default:
  4658. if((vd=status_get_viewdata(bl)) && vd->class_ != JT_INVISIBLE)
  4659. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4660. break;
  4661. }
  4662. }
  4663. if (clif_session_isValid(sd)) { //sd is watching tbl go out of view.
  4664. nullpo_ret(tbl);
  4665. if(tbl->type == BL_SKILL) //Trap knocked out of sight
  4666. clif_clearchar_skillunit( *((skill_unit *)tbl), *sd );
  4667. else if(((vd=status_get_viewdata(tbl)) && vd->class_ != JT_INVISIBLE) &&
  4668. !(tbl->type == BL_NPC && (((TBL_NPC*)tbl)->is_invisible)))
  4669. clif_clearunit_single( tbl->id, CLR_OUTSIGHT, *sd );
  4670. }
  4671. return 0;
  4672. }
  4673. /*==========================================
  4674. * tbl has come into view of bl
  4675. *------------------------------------------*/
  4676. int clif_insight(struct block_list *bl,va_list ap)
  4677. {
  4678. struct block_list *tbl;
  4679. TBL_PC *sd, *tsd;
  4680. tbl=va_arg(ap,struct block_list*);
  4681. if (bl == tbl) return 0;
  4682. sd = BL_CAST(BL_PC, bl);
  4683. tsd = BL_CAST(BL_PC, tbl);
  4684. if (clif_session_isValid(tsd)) { //Tell tsd that bl entered into his view
  4685. switch(bl->type){
  4686. case BL_ITEM:
  4687. clif_getareachar_item(tsd,(struct flooritem_data*)bl);
  4688. break;
  4689. case BL_SKILL:
  4690. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &tsd->bl);
  4691. break;
  4692. default:
  4693. clif_getareachar_unit(tsd,bl);
  4694. break;
  4695. }
  4696. }
  4697. if (clif_session_isValid(sd)) { //Tell sd that tbl walked into his view
  4698. clif_getareachar_unit(sd,tbl);
  4699. }
  4700. return 0;
  4701. }
  4702. /// Updates whole skill tree (ZC_SKILLINFO_LIST).
  4703. /// 010f <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  4704. void clif_skillinfoblock(map_session_data *sd)
  4705. {
  4706. int fd;
  4707. int i,len,id;
  4708. nullpo_retv(sd);
  4709. fd = sd->fd;
  4710. if (!session_isActive(fd))
  4711. return;
  4712. WFIFOHEAD(fd, MAX_SKILL * 37 + 4);
  4713. WFIFOW(fd,0) = 0x10f;
  4714. bool haveCallPartnerSkill = false;
  4715. for ( i = 0, len = 4; i < MAX_SKILL; i++)
  4716. {
  4717. if( (id = sd->status.skill[i].id) != 0 )
  4718. {
  4719. // workaround for bugreport:5348
  4720. if (len + 37 > 8192)
  4721. break;
  4722. // skip WE_CALLPARTNER and send it in special way
  4723. if (id == WE_CALLPARTNER) {
  4724. haveCallPartnerSkill = true;
  4725. continue;
  4726. }
  4727. WFIFOW(fd,len) = id;
  4728. WFIFOL(fd,len+2) = skill_get_inf(id);
  4729. WFIFOW(fd,len+6) = sd->status.skill[i].lv;
  4730. WFIFOW(fd,len+8) = skill_get_sp(id,sd->status.skill[i].lv);
  4731. WFIFOW(fd,len+10)= skill_get_range2(&sd->bl,id,sd->status.skill[i].lv,false);
  4732. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  4733. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  4734. WFIFOB(fd,len+36) = (sd->status.skill[i].lv < skill_tree_get_max(id, sd->status.class_))? 1:0;
  4735. else
  4736. WFIFOB(fd,len+36) = 0;
  4737. len += 37;
  4738. }
  4739. }
  4740. WFIFOW(fd,2)=len;
  4741. WFIFOSET(fd,len);
  4742. // adoption fix
  4743. if (haveCallPartnerSkill) {
  4744. clif_addskill(sd, WE_CALLPARTNER);
  4745. clif_skillinfo(sd, WE_CALLPARTNER, 0);
  4746. }
  4747. // workaround for bugreport:5348; send the remaining skills one by one to bypass packet size limit
  4748. for ( ; i < MAX_SKILL; i++)
  4749. {
  4750. if( (id = sd->status.skill[i].id) != 0 && ( id != WE_CALLPARTNER || !haveCallPartnerSkill ) )
  4751. {
  4752. clif_addskill(sd, id);
  4753. clif_skillinfo(sd, id, 0);
  4754. }
  4755. }
  4756. }
  4757. /**
  4758. * Server tells client 'sd' to add skill of id 'id' to it's skill tree (e.g. with Ice Falcion item)
  4759. **/
  4760. /// Adds new skill to the skill tree (ZC_ADD_SKILL).
  4761. /// 0111 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B
  4762. void clif_addskill(map_session_data *sd, int skill_id)
  4763. {
  4764. nullpo_retv(sd);
  4765. int fd = sd->fd;
  4766. uint16 idx = skill_get_index(skill_id);
  4767. if (!session_isActive(fd) || !idx)
  4768. return;
  4769. if( sd->status.skill[idx].id <= 0 )
  4770. return;
  4771. WFIFOHEAD(fd, packet_len(0x111));
  4772. WFIFOW(fd,0) = 0x111;
  4773. WFIFOW(fd,2) = skill_id;
  4774. WFIFOL(fd,4) = skill_get_inf(skill_id);
  4775. WFIFOW(fd,8) = sd->status.skill[idx].lv;
  4776. WFIFOW(fd,10) = skill_get_sp(skill_id,sd->status.skill[idx].lv);
  4777. WFIFOW(fd,12)= skill_get_range2(&sd->bl,skill_id,sd->status.skill[idx].lv,false);
  4778. safestrncpy(WFIFOCP(fd,14), skill_get_name(skill_id), NAME_LENGTH);
  4779. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  4780. WFIFOB(fd,38) = (sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_))? 1:0;
  4781. else
  4782. WFIFOB(fd,38) = 0;
  4783. WFIFOSET(fd,packet_len(0x111));
  4784. }
  4785. /// Deletes a skill from the skill tree (ZC_SKILLINFO_DELETE).
  4786. /// 0441 <skill id>.W
  4787. void clif_deleteskill(map_session_data *sd, int skill_id, bool skip_infoblock)
  4788. {
  4789. #if PACKETVER >= 20081217
  4790. nullpo_retv(sd);
  4791. int fd = sd->fd;
  4792. uint16 idx = skill_get_index(skill_id);
  4793. if (!session_isActive(fd) || !idx)
  4794. return;
  4795. WFIFOHEAD(fd,packet_len(0x441));
  4796. WFIFOW(fd,0) = 0x441;
  4797. WFIFOW(fd,2) = skill_id;
  4798. WFIFOSET(fd,packet_len(0x441));
  4799. #endif
  4800. #if PACKETVER_MAIN_NUM >= 20190807 || PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190918
  4801. if (!skip_infoblock)
  4802. #endif
  4803. clif_skillinfoblock(sd);
  4804. }
  4805. /// Updates a skill in the skill tree (ZC_SKILLINFO_UPDATE).
  4806. /// 010e <skill id>.W <level>.W <sp cost>.W <attack range>.W <upgradable>.B
  4807. void clif_skillup(map_session_data *sd, uint16 skill_id, int lv, int range, int upgradable) {
  4808. nullpo_retv(sd);
  4809. int fd = sd->fd;
  4810. uint16 idx = skill_get_index(skill_id);
  4811. if (!session_isActive(fd) || !idx)
  4812. return;
  4813. WFIFOHEAD(fd, packet_len(0x10e));
  4814. WFIFOW(fd, 0) = 0x10e;
  4815. WFIFOW(fd, 2) = skill_id;
  4816. WFIFOW(fd, 4) = lv;
  4817. WFIFOW(fd, 6) = skill_get_sp(skill_id, lv);
  4818. WFIFOW(fd, 8) = range;
  4819. WFIFOB(fd, 10) = upgradable;
  4820. WFIFOSET(fd, packet_len(0x10e));
  4821. }
  4822. /// Updates a skill in the skill tree (ZC_SKILLINFO_UPDATE2).
  4823. /// 07e1 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <upgradable>.B
  4824. void clif_skillinfo(map_session_data *sd,int skill_id, int inf)
  4825. {
  4826. nullpo_retv(sd);
  4827. const int fd = sd->fd;
  4828. uint16 idx = skill_get_index(skill_id);
  4829. if (!session_isActive(fd) || !idx)
  4830. return;
  4831. WFIFOHEAD(fd,packet_len(0x7e1));
  4832. WFIFOW(fd,0) = 0x7e1;
  4833. WFIFOW(fd,2) = skill_id;
  4834. WFIFOL(fd,4) = inf?inf:skill_get_inf(skill_id);
  4835. WFIFOW(fd,8) = sd->status.skill[idx].lv;
  4836. WFIFOW(fd,10) = skill_get_sp(skill_id,sd->status.skill[idx].lv);
  4837. WFIFOW(fd,12) = skill_get_range2(&sd->bl,skill_id,sd->status.skill[idx].lv,false);
  4838. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  4839. WFIFOB(fd,14) = (sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_))? 1:0;
  4840. else
  4841. WFIFOB(fd,14) = 0;
  4842. WFIFOSET(fd,packet_len(0x7e1));
  4843. }
  4844. void clif_skill_scale( struct block_list *bl, int src_id, int x, int y, uint16 skill_id, uint16 skill_lv, int casttime ){
  4845. #if PACKETVER >= 20151223
  4846. if( !battle_config.show_skill_scale ){
  4847. return;
  4848. }
  4849. PACKET_ZC_SKILL_SCALE p = {};
  4850. p.PacketType = skillscale;
  4851. p.AID = src_id;
  4852. p.skill_id = skill_id;
  4853. p.skill_lv = skill_lv;
  4854. p.x = x;
  4855. p.y = y;
  4856. p.casttime = casttime;
  4857. if( disguised( bl ) ){
  4858. clif_send( &p, sizeof( p ), bl, AREA_WOS );
  4859. p.AID = disguised_bl_id( bl->id );
  4860. clif_send( &p, sizeof( p ), bl, SELF );
  4861. }else{
  4862. clif_send( &p, sizeof( p ), bl, AREA );
  4863. }
  4864. #endif
  4865. }
  4866. /// Notifies clients in area, that an object is about to use a skill.
  4867. /// 013e <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L (ZC_USESKILL_ACK)
  4868. /// 07fb <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L <is disposable>.B (ZC_USESKILL_ACK2)
  4869. /// property:
  4870. /// 0 = Yellow cast aura
  4871. /// 1 = Water elemental cast aura
  4872. /// 2 = Earth elemental cast aura
  4873. /// 3 = Fire elemental cast aura
  4874. /// 4 = Wind elemental cast aura
  4875. /// 5 = Poison elemental cast aura
  4876. /// 6 = Holy elemental cast aura
  4877. /// ? = like 0
  4878. /// is disposable:
  4879. /// 0 = yellow chat text "[src name] will use skill [skill name]."
  4880. /// 1 = no text
  4881. void clif_skillcasting(struct block_list* bl, int src_id, int dst_id, int dst_x, int dst_y, uint16 skill_id, uint16 skill_lv, int property, int casttime)
  4882. {
  4883. #if PACKETVER < 20091124
  4884. const int cmd = 0x13e;
  4885. #else
  4886. const int cmd = 0x7fb;
  4887. #endif
  4888. unsigned char buf[32];
  4889. WBUFW(buf,0) = cmd;
  4890. WBUFL(buf,2) = src_id;
  4891. WBUFL(buf,6) = dst_id;
  4892. WBUFW(buf,10) = dst_x;
  4893. WBUFW(buf,12) = dst_y;
  4894. WBUFW(buf,14) = skill_id;
  4895. WBUFL(buf,16) = property<0?0:property; //Avoid sending negatives as element [Skotlex]
  4896. WBUFL(buf,20) = casttime;
  4897. #if PACKETVER >= 20091124
  4898. WBUFB(buf,24) = 0; // isDisposable
  4899. #endif
  4900. if (disguised(bl)) {
  4901. clif_send(buf,packet_len(cmd), bl, AREA_WOS);
  4902. WBUFL(buf,2) = disguised_bl_id( src_id );
  4903. clif_send(buf,packet_len(cmd), bl, SELF);
  4904. } else
  4905. clif_send(buf,packet_len(cmd), bl, AREA);
  4906. if( skill_get_inf2( skill_id, INF2_SHOWSCALE ) ){
  4907. clif_skill_scale( bl, src_id, bl->x, bl->y, skill_id, skill_lv, casttime );
  4908. }
  4909. }
  4910. /// Notifies clients in area, that an object canceled casting.
  4911. /// 01b9 <id>.L (ZC_DISPEL)
  4912. void clif_skillcastcancel( block_list& bl ){
  4913. PACKET_ZC_DISPEL packet{};
  4914. packet.packetType = HEADER_ZC_DISPEL;
  4915. packet.gid = bl.id;
  4916. clif_send( &packet, sizeof(PACKET_ZC_DISPEL), &bl, AREA );
  4917. }
  4918. /// Notifies the client about the result of a skill use request (ZC_ACK_TOUSESKILL).
  4919. /// 0110 <skill id>.W <num>.L <result>.B <cause>.B
  4920. /// num (only used when skill id = NV_BASIC and cause = 0):
  4921. /// 0 = "skill failed" MsgStringTable[159]
  4922. /// 1 = "no emotions" MsgStringTable[160]
  4923. /// 2 = "no sit" MsgStringTable[161]
  4924. /// 3 = "no chat" MsgStringTable[162]
  4925. /// 4 = "no party" MsgStringTable[163]
  4926. /// 5 = "no shout" MsgStringTable[164]
  4927. /// 6 = "no PKing" MsgStringTable[165]
  4928. /// 7 = "no alligning" MsgStringTable[383]
  4929. /// ? = ignored
  4930. /// cause:
  4931. /// 0 = "not enough skill level" MsgStringTable[214] (AL_WARP)
  4932. /// "steal failed" MsgStringTable[205] (TF_STEAL)
  4933. /// "envenom failed" MsgStringTable[207] (TF_POISON)
  4934. /// "skill failed" MsgStringTable[204] (otherwise)
  4935. /// ... = @see enum useskill_fail_cause
  4936. /// ? = ignored
  4937. ///
  4938. /// if(result!=0) doesn't display any of the previous messages
  4939. /// Note: when this packet is received an unknown flag is always set to 0,
  4940. /// suggesting this is an ACK packet for the UseSkill packets and should be sent on success too [FlavioJS]
  4941. void clif_skill_fail( map_session_data& sd, uint16 skill_id, enum useskill_fail_cause cause, int btype, t_itemid itemId ){
  4942. if(battle_config.display_skill_fail&1)
  4943. return; //Disable all skill failed messages
  4944. if(cause==USESKILL_FAIL_SKILLINTERVAL && !sd.state.showdelay)
  4945. return; //Disable delay failed messages
  4946. if(skill_id == RG_SNATCHER && battle_config.display_skill_fail&4)
  4947. return;
  4948. if(skill_id == TF_POISON && battle_config.display_skill_fail&8)
  4949. return;
  4950. PACKET_ZC_ACK_TOUSESKILL p = {};
  4951. p.packetType = HEADER_ZC_ACK_TOUSESKILL;
  4952. p.skillId = skill_id;
  4953. p.btype = btype;
  4954. p.itemId = itemId;
  4955. p.flag = 0; // 0 - failed
  4956. p.cause = cause;
  4957. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  4958. }
  4959. /// Skill cooldown display icon.
  4960. /// 043d <skill ID>.W <tick>.L (ZC_SKILL_POSTDELAY)
  4961. void clif_skill_cooldown( map_session_data &sd, uint16 skill_id, t_tick tick ){
  4962. #if PACKETVER_MAIN_NUM >= 20081112 || PACKETVER_RE_NUM >= 20081111 || defined(PACKETVER_ZERO)
  4963. PACKET_ZC_SKILL_POSTDELAY packet{};
  4964. packet.PacketType = HEADER_ZC_SKILL_POSTDELAY;
  4965. packet.SKID = skill_id;
  4966. packet.DelayTM = client_tick(tick);
  4967. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4968. #endif
  4969. }
  4970. /// Skill attack effect and damage.
  4971. /// 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)
  4972. /// 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)
  4973. int clif_skill_damage(struct block_list *src,struct block_list *dst,t_tick tick,int sdelay,int ddelay,int64 sdamage,int div,uint16 skill_id,uint16 skill_lv,enum e_damage_type type)
  4974. {
  4975. unsigned char buf[64];
  4976. status_change *sc;
  4977. int damage = (int)cap_value(sdamage,INT_MIN,INT_MAX);
  4978. nullpo_ret(src);
  4979. nullpo_ret(dst);
  4980. type = clif_calc_delay(type,div,damage,ddelay);
  4981. if( ( sc = status_get_sc(dst) ) && sc->count ) {
  4982. if(sc->getSCE(SC_HALLUCINATION) && damage)
  4983. damage = clif_hallucination_damage();
  4984. }
  4985. #if PACKETVER < 3
  4986. WBUFW(buf,0)=0x114;
  4987. WBUFW(buf,2)=skill_id;
  4988. WBUFL(buf,4)=src->id;
  4989. WBUFL(buf,8)=dst->id;
  4990. WBUFL(buf,12)=client_tick(tick);
  4991. WBUFL(buf,16)=sdelay;
  4992. WBUFL(buf,20)=ddelay;
  4993. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  4994. WBUFW(buf,24)=damage?div:0;
  4995. } else {
  4996. WBUFW(buf,24)=damage;
  4997. }
  4998. WBUFW(buf,26)=skill_lv;
  4999. WBUFW(buf,28)=div;
  5000. WBUFB(buf,30)=type;
  5001. if (disguised(dst)) {
  5002. clif_send(buf,packet_len(0x114),dst,AREA_WOS);
  5003. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5004. clif_send(buf,packet_len(0x114),dst,SELF);
  5005. } else
  5006. clif_send(buf,packet_len(0x114),dst,AREA);
  5007. if(disguised(src)) {
  5008. WBUFL(buf,4)=disguised_bl_id(src->id);
  5009. if (disguised(dst))
  5010. WBUFL(buf,8)=dst->id;
  5011. if(damage > 0)
  5012. WBUFW(buf,24)=-1;
  5013. clif_send(buf,packet_len(0x114),src,SELF);
  5014. }
  5015. #else
  5016. WBUFW(buf,0)=0x1de;
  5017. WBUFW(buf,2)=skill_id;
  5018. WBUFL(buf,4)=src->id;
  5019. WBUFL(buf,8)=dst->id;
  5020. WBUFL(buf,12)=client_tick(tick);
  5021. WBUFL(buf,16)=sdelay;
  5022. WBUFL(buf,20)=ddelay;
  5023. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5024. WBUFL(buf,24)=damage?div:0;
  5025. } else {
  5026. WBUFL(buf,24)=damage;
  5027. }
  5028. WBUFW(buf,28)=skill_lv;
  5029. WBUFW(buf,30)=div;
  5030. // For some reason, late 2013 and newer clients have
  5031. // a issue that causes players and monsters to endure
  5032. // type 6 (ACTION_SKILL) skills. So we have to do a small
  5033. // hack to set all type 6 to be sent as type 8 ACTION_ATTACK_MULTIPLE
  5034. #if PACKETVER < 20131223
  5035. WBUFB(buf,32)=type;
  5036. #else
  5037. WBUFB(buf,32)=( type == DMG_SINGLE ) ? DMG_MULTI_HIT : type;
  5038. #endif
  5039. if (disguised(dst)) {
  5040. clif_send(buf,packet_len(0x1de),dst,AREA_WOS);
  5041. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5042. clif_send(buf,packet_len(0x1de),dst,SELF);
  5043. } else
  5044. clif_send(buf,packet_len(0x1de),dst,AREA);
  5045. if(disguised(src)) {
  5046. WBUFL(buf,4)=disguised_bl_id(src->id);
  5047. if (disguised(dst))
  5048. WBUFL(buf,8)=dst->id;
  5049. if(damage > 0)
  5050. WBUFL(buf,24)=-1;
  5051. clif_send(buf,packet_len(0x1de),src,SELF);
  5052. }
  5053. #endif
  5054. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5055. return clif_calc_walkdelay(dst,ddelay,type,damage,div);
  5056. }
  5057. /// Ground skill attack effect and damage (ZC_NOTIFY_SKILL_POSITION).
  5058. /// 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
  5059. /*
  5060. int clif_skill_damage2(struct block_list *src,struct block_list *dst,t_tick tick,int sdelay,int ddelay,int damage,int div,uint16 skill_id,uint16 skill_lv,enum e_damage_type type)
  5061. {
  5062. unsigned char buf[64];
  5063. status_change *sc;
  5064. nullpo_ret(src);
  5065. nullpo_ret(dst);
  5066. type = (type>DMG_NORMAL)?type:skill_get_hit(skill_id);
  5067. type = clif_calc_delay(type,div,damage,ddelay);
  5068. sc = status_get_sc(dst);
  5069. if(sc && sc->count) {
  5070. if(sc->getSCE(SC_HALLUCINATION) && damage)
  5071. damage = clif_hallucination_damage();
  5072. }
  5073. WBUFW(buf,0)=0x115;
  5074. WBUFW(buf,2)=skill_id;
  5075. WBUFL(buf,4)=src->id;
  5076. WBUFL(buf,8)=dst->id;
  5077. WBUFL(buf,12)=client_tick(tick);
  5078. WBUFL(buf,16)=sdelay;
  5079. WBUFL(buf,20)=ddelay;
  5080. WBUFW(buf,24)=dst->x;
  5081. WBUFW(buf,26)=dst->y;
  5082. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5083. WBUFW(buf,28)=damage?div:0;
  5084. } else {
  5085. WBUFW(buf,28)=damage;
  5086. }
  5087. WBUFW(buf,30)=skill_lv;
  5088. WBUFW(buf,32)=div;
  5089. WBUFB(buf,34)=type;
  5090. clif_send(buf,packet_len(0x115),src,AREA);
  5091. if(disguised(src)) {
  5092. WBUFL(buf,4)=disguised_bl_id(src->id);
  5093. if(damage > 0)
  5094. WBUFW(buf,28)=-1;
  5095. clif_send(buf,packet_len(0x115),src,SELF);
  5096. }
  5097. if (disguised(dst)) {
  5098. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5099. if (disguised(src))
  5100. WBUFL(buf,4)=src->id;
  5101. else if(damage > 0)
  5102. WBUFW(buf,28)=-1;
  5103. clif_send(buf,packet_len(0x115),dst,SELF);
  5104. }
  5105. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5106. return clif_calc_walkdelay(dst,ddelay,type,damage,div);
  5107. }
  5108. */
  5109. /// Non-damaging skill effect
  5110. /// 011a <skill id>.W <heal>.W <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL).
  5111. /// 09cb <skill id>.W <heal>.L <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL2).
  5112. bool clif_skill_nodamage(struct block_list *src,struct block_list *dst, uint16 skill_id, int heal, t_tick tick)
  5113. {
  5114. unsigned char buf[17];
  5115. #if PACKETVER < 20130731
  5116. const int cmd = 0x11a;
  5117. #else
  5118. const int cmd = 0x9cb;
  5119. #endif
  5120. int offset = 0;
  5121. bool success = ( tick != 0 );
  5122. nullpo_ret(dst);
  5123. WBUFW(buf,0) = cmd;
  5124. WBUFW(buf,2) = skill_id;
  5125. #if PACKETVER < 20130731
  5126. WBUFW(buf,4) = min(heal, INT16_MAX);
  5127. #else
  5128. WBUFL(buf,4) = min(heal, INT32_MAX);
  5129. offset += 2;
  5130. #endif
  5131. WBUFL(buf,6+offset) = dst->id;
  5132. WBUFL(buf,10+offset) = src ? src->id : 0;
  5133. WBUFB(buf,14+offset) = success;
  5134. if (disguised(dst)) {
  5135. clif_send(buf, packet_len(cmd), dst, AREA_WOS);
  5136. WBUFL(buf,6+offset) = disguised_bl_id(dst->id);
  5137. clif_send(buf, packet_len(cmd), dst, SELF);
  5138. } else
  5139. clif_send(buf, packet_len(cmd), dst, AREA);
  5140. if(src && disguised(src)) {
  5141. WBUFL(buf,10+offset) = disguised_bl_id(src->id);
  5142. if (disguised(dst))
  5143. WBUFL(buf,6+offset) = dst->id;
  5144. clif_send(buf, packet_len(cmd), src, SELF);
  5145. }
  5146. return success;
  5147. }
  5148. /// Non-damaging ground skill effect (ZC_NOTIFY_GROUNDSKILL).
  5149. /// 0117 <skill id>.W <src id>.L <level>.W <x>.W <y>.W <tick>.L
  5150. void clif_skill_poseffect(struct block_list *src,uint16 skill_id,int val,int x,int y,t_tick tick)
  5151. {
  5152. unsigned char buf[32];
  5153. nullpo_retv(src);
  5154. WBUFW(buf,0)=0x117;
  5155. WBUFW(buf,2)=skill_id;
  5156. WBUFL(buf,4)=src->id;
  5157. WBUFW(buf,8)=val;
  5158. WBUFW(buf,10)=x;
  5159. WBUFW(buf,12)=y;
  5160. WBUFL(buf,14)=client_tick(tick);
  5161. if(disguised(src)) {
  5162. clif_send(buf,packet_len(0x117),src,AREA_WOS);
  5163. WBUFL(buf,4)=disguised_bl_id(src->id);
  5164. clif_send(buf,packet_len(0x117),src,SELF);
  5165. } else
  5166. clif_send(buf,packet_len(0x117),src,AREA);
  5167. }
  5168. /// Presents a list of available warp destinations.
  5169. /// 011c <skill id>.W { <map name>.16B }*4 (ZC_WARPLIST)
  5170. /// 0abe <lenght>.W <skill id>.W { <map name>.16B }*? (ZC_WARPLIST2)
  5171. void clif_skill_warppoint( map_session_data& sd, uint16 skill_id, uint16 skill_lv, std::vector<std::string>& maps ){
  5172. if(maps.empty())
  5173. return;
  5174. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  5175. PACKET_ZC_WARPLIST* p = reinterpret_cast<PACKET_ZC_WARPLIST*>( packet_buffer );
  5176. p->packetType = HEADER_ZC_WARPLIST;
  5177. p->packetLength = sizeof( *p );
  5178. p->skillId = skill_id;
  5179. size_t memoCount = 0;
  5180. for( std::string& map : maps ){
  5181. if( map.empty() ){
  5182. continue;
  5183. }
  5184. PACKET_ZC_WARPLIST_sub& warp = p->maps[memoCount];
  5185. mapindex_getmapname_ext( map.c_str(), warp.map );
  5186. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( warp ) );
  5187. memoCount++;
  5188. }
  5189. clif_send( p, p->packetLength, &sd.bl, SELF );
  5190. #else
  5191. PACKET_ZC_WARPLIST p = {};
  5192. p.packetType = HEADER_ZC_WARPLIST;
  5193. p.skillId = skill_id;
  5194. size_t memoCount = 0, max = 4;
  5195. for( std::string& map : maps ){
  5196. if( map.empty() ){
  5197. continue;
  5198. }
  5199. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5200. mapindex_getmapname_ext( map.c_str(), warp.map );
  5201. if( memoCount++ == max ){
  5202. break;
  5203. }
  5204. }
  5205. for( ; memoCount < max; memoCount++ ){
  5206. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5207. strcpy( warp.map, "" );
  5208. }
  5209. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  5210. #endif
  5211. sd.menuskill_id = skill_id;
  5212. if (skill_id == AL_WARP) {
  5213. sd.menuskill_val = (sd.ud.skillx<<16)|sd.ud.skilly; //Store warp position here.
  5214. sd.state.workinprogress = WIP_DISABLE_ALL;
  5215. } else
  5216. sd.menuskill_val = skill_lv;
  5217. }
  5218. /// Memo message.
  5219. /// 011e <type>.B (ZC_ACK_REMEMBER_WARPPOINT)
  5220. /// type:
  5221. /// 0 = "Saved location as a Memo Point for Warp skill." in color 0xFFFF00 (cyan)
  5222. /// 1 = "Skill Level is not high enough." in color 0x0000FF (red)
  5223. /// 2 = "You haven't learned Warp." in color 0x0000FF (red)
  5224. ///
  5225. /// @param sd Who receives the message
  5226. /// @param type What message
  5227. void clif_skill_memomessage( map_session_data& sd, e_ack_remember_warppoint_result result ){
  5228. PACKET_ZC_ACK_REMEMBER_WARPPOINT packet{};
  5229. packet.packetType = HEADER_ZC_ACK_REMEMBER_WARPPOINT;
  5230. packet.type = static_cast<decltype(packet.type)>(result);
  5231. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5232. }
  5233. /// Teleport message.
  5234. /// 0189 <type>.W (ZC_NOTIFY_MAPINFO)
  5235. /// type:
  5236. /// 0 = "Unable to Teleport in this area" in color 0xFFFF00 (cyan)
  5237. /// 1 = "Saved point cannot be memorized." in color 0x0000FF (red)
  5238. /// 2 = "This skill cannot be used in this area"
  5239. /// 3 = "This item cannot be used in this area"
  5240. ///
  5241. /// @param sd Who receives the message
  5242. /// @param type What message
  5243. void clif_skill_teleportmessage( map_session_data& sd, e_notify_mapinfo_result result ){
  5244. PACKET_ZC_NOTIFY_MAPINFO packet{};
  5245. packet.packetType = HEADER_ZC_NOTIFY_MAPINFO;
  5246. packet.type = static_cast<decltype(packet.type)>(result);
  5247. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5248. }
  5249. /// Displays Sense (WZ_ESTIMATION) information window (ZC_MONSTER_INFO).
  5250. /// 018c <class>.W <level>.W <size>.W <hp>.L <def>.W <race>.W <mdef>.W <element>.W
  5251. /// <water%>.B <earth%>.B <fire%>.B <wind%>.B <poison%>.B <holy%>.B <shadow%>.B <ghost%>.B <undead%>.B
  5252. void clif_skill_estimation(map_session_data *sd,struct block_list *dst)
  5253. {
  5254. unsigned char buf[64];
  5255. int i, fix;
  5256. nullpo_retv(sd);
  5257. nullpo_retv(dst);
  5258. if( dst->type != BL_MOB )
  5259. return;
  5260. status_data* status = status_get_status_data(*dst);
  5261. WBUFW(buf, 0)=0x18c;
  5262. WBUFW(buf, 2)=status_get_class(dst);
  5263. WBUFW(buf, 4)=status_get_lv(dst);
  5264. WBUFW(buf, 6)=status->size;
  5265. WBUFL(buf, 8)=status->hp;
  5266. WBUFW(buf,12)= (battle_config.estimation_type&1?status->def:0)
  5267. +(battle_config.estimation_type&2?status->def2:0);
  5268. WBUFW(buf,14)=status->race;
  5269. WBUFW(buf,16)= (battle_config.estimation_type&1?status->mdef:0)
  5270. +(battle_config.estimation_type&2?status->mdef2:0);
  5271. WBUFW(buf,18)= status->def_ele;
  5272. for(i=0;i<9;i++)
  5273. // The following caps negative attributes to 0 since the client displays them as 255-fix. [Skotlex]
  5274. WBUFB(buf,20+i)= (unsigned char)((fix=elemental_attribute_db.getAttribute(status->ele_lv, i+1, status->def_ele))<0?0:fix);
  5275. clif_send(buf,packet_len(0x18c),&sd->bl,sd->status.party_id>0?PARTY_SAMEMAP:SELF);
  5276. }
  5277. /// Presents a textual list of producable items.
  5278. /// 018d <packet len>.W { <name id>.W { <material id>.W }*3 }* (ZC_MAKABLEITEMLIST)
  5279. /// material id:
  5280. /// unused by the client
  5281. void clif_skill_produce_mix_list( map_session_data& sd, int skill_id, int trigger ){
  5282. // Avoid resending the menu
  5283. if( sd.menuskill_id == skill_id ){
  5284. return;
  5285. }
  5286. if (skill_id == GC_CREATENEWPOISON)
  5287. skill_id = GC_RESEARCHNEWPOISON;
  5288. PACKET_ZC_MAKABLEITEMLIST* p = reinterpret_cast<PACKET_ZC_MAKABLEITEMLIST*>( packet_buffer );
  5289. p->packetType = HEADER_ZC_MAKABLEITEMLIST;
  5290. p->packetLength = sizeof( *p );
  5291. int count = 0;
  5292. for( int i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5293. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, 1 ) ){
  5294. continue;
  5295. }
  5296. if( skill_id > 0 && skill_produce_db[i].req_skill != skill_id ){
  5297. continue;
  5298. }
  5299. PACKET_ZC_MAKABLEITEMLIST_sub& entry = p->items[count];
  5300. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5301. entry.material[0] = 0;
  5302. entry.material[1] = 0;
  5303. entry.material[2] = 0;
  5304. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5305. count++;
  5306. }
  5307. clif_send( p, p->packetLength, &sd.bl, SELF );
  5308. if( count > 0 ){
  5309. sd.menuskill_id = skill_id;
  5310. sd.menuskill_val = trigger;
  5311. }
  5312. }
  5313. /// Present a list of producable items.
  5314. /// 025a <packet len>.W <mk type>.W { <name id>.W }* (ZC_MAKINGITEM_LIST)
  5315. /// mk type:
  5316. /// 1 = cooking
  5317. /// 2 = arrow
  5318. /// 3 = elemental
  5319. /// 4 = GN_MIX_COOKING
  5320. /// 5 = GN_MAKEBOMB
  5321. /// 6 = GN_S_PHARMACY
  5322. /// 7 = MT_M_MACHINE
  5323. /// 8 = BO_BIONIC_PHARMACY
  5324. void clif_cooking_list( map_session_data& sd, int trigger, uint16 skill_id, int qty, int list_type ){
  5325. #if PACKETVER >= 20051010
  5326. // Avoid resending the menu
  5327. if( sd.menuskill_id == skill_id ){
  5328. return;
  5329. }
  5330. PACKET_ZC_MAKINGITEM_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGITEM_LIST*>( packet_buffer );
  5331. p->packetType = HEADER_ZC_MAKINGITEM_LIST;
  5332. p->packetLength = sizeof( *p );
  5333. p->makeItem = list_type;
  5334. int count = 0;
  5335. for( int i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5336. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, qty ) ){
  5337. continue;
  5338. }
  5339. PACKET_ZC_MAKINGITEM_LIST_sub& entry = p->items[count];
  5340. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5341. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5342. count++;
  5343. }
  5344. if( count > 0 || skill_id == AM_PHARMACY ){
  5345. clif_send( p, p->packetLength, &sd.bl, SELF );
  5346. sd.menuskill_id = skill_id;
  5347. sd.menuskill_val = trigger;
  5348. sd.menuskill_val2 = qty; // amount.
  5349. }else{
  5350. clif_menuskill_clear( &sd );
  5351. #if PACKETVER >= 20090922
  5352. clif_msg_skill( &sd, skill_id, MSI_SKILL_INVENTORY_KINDCNT_OVER );
  5353. #else
  5354. clif_send( p, p->packetLength, &sd.bl, SELF );
  5355. #endif
  5356. }
  5357. #endif
  5358. }
  5359. /// Notifies clients of a status change.
  5360. /// 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)]
  5361. /// 043f <index>.W <id>.L <state>.B <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE2) [used exclusively for starting statuses on pcs]
  5362. /// 0983 <index>.W <id>.L <state>.B <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE3) (PACKETVER >= 20120618)
  5363. /// @param bl Sends packet to clients around this object
  5364. /// @param id ID of object that has this effect
  5365. /// @param type Status icon see enum efst_type
  5366. /// @param flag 1:Active, 0:Deactive
  5367. /// @param tick Duration in ms
  5368. /// @param val1
  5369. /// @param val2
  5370. /// @param val3
  5371. void clif_status_change_sub(struct block_list *bl, int id, int type, int flag, t_tick tick, int val1, int val2, int val3, enum send_target target_type)
  5372. {
  5373. unsigned char buf[32];
  5374. if (type == EFST_BLANK) //It shows nothing on the client...
  5375. return;
  5376. if (type == EFST_POSTDELAY && tick == 0)
  5377. return;
  5378. nullpo_retv(bl);
  5379. // Statuses with an infinite duration, but still needs a duration sent to display properly.
  5380. if (type == EFST_LUNARSTANCE || type == EFST_UNIVERSESTANCE || type == EFST_SUNSTANCE || type == EFST_STARSTANCE)
  5381. tick = 200;
  5382. #if PACKETVER >= 20120618
  5383. if (flag && battle_config.display_status_timers)
  5384. WBUFW(buf,0) = 0x983;
  5385. else
  5386. #elif PACKETVER >= 20090121
  5387. if (flag && battle_config.display_status_timers)
  5388. WBUFW(buf,0) = 0x43f;
  5389. else
  5390. #endif
  5391. WBUFW(buf,0) = 0x196;
  5392. WBUFW(buf,2) = type;
  5393. WBUFL(buf,4) = id;
  5394. WBUFB(buf,8) = flag;
  5395. #if PACKETVER >= 20120618
  5396. if (flag && battle_config.display_status_timers) {
  5397. if (tick <= 0)
  5398. tick = 9999; // this is indeed what official servers do
  5399. WBUFL(buf,9) = client_tick(tick);/* at this stage remain and total are the same value I believe */
  5400. WBUFL(buf,13) = client_tick(tick);
  5401. WBUFL(buf,17) = val1;
  5402. WBUFL(buf,21) = val2;
  5403. WBUFL(buf,25) = val3;
  5404. }
  5405. #elif PACKETVER >= 20090121
  5406. if (flag && battle_config.display_status_timers) {
  5407. if (tick <= 0)
  5408. tick = 9999; // this is indeed what official servers do
  5409. WBUFL(buf,9) = client_tick(tick);
  5410. WBUFL(buf,13) = val1;
  5411. WBUFL(buf,17) = val2;
  5412. WBUFL(buf,21) = val3;
  5413. }
  5414. #endif
  5415. clif_send(buf, packet_len(WBUFW(buf,0)), bl, target_type);
  5416. }
  5417. /* Sends status effect to clients around the bl
  5418. * @param bl Object that has the effect
  5419. * @param type Status icon see enum efst_type
  5420. * @param flag 1:Active, 0:Deactive
  5421. * @param tick Duration in ms
  5422. * @param val1
  5423. * @param val2
  5424. * @param val3
  5425. */
  5426. void clif_status_change(struct block_list *bl, int type, int flag, t_tick tick, int val1, int val2, int val3) {
  5427. map_session_data *sd = nullptr;
  5428. if (type == EFST_BLANK) //It shows nothing on the client...
  5429. return;
  5430. if (type == EFST_POSTDELAY && tick == 0)
  5431. return;
  5432. if (type == EFST_ILLUSION && !battle_config.display_hallucination) // Disable Hallucination.
  5433. return;
  5434. #if !( PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 )
  5435. // Older clients display normal riding icon.
  5436. if (type == EFST_MADOGEAR_TYPE)
  5437. type = EFST_RIDING;
  5438. #endif
  5439. nullpo_retv(bl);
  5440. sd = BL_CAST(BL_PC, bl);
  5441. // Check if current bl type is in the returned bitmask and only send status changes that actually matter to the client
  5442. if (!(status_efst_get_bl_type(static_cast<efst_type>(type)) & bl->type))
  5443. return;
  5444. clif_status_change_sub(bl, bl->id, type, flag, tick, val1, val2, val3, ((sd ? (pc_isinvisible(sd) ? SELF : AREA) : AREA_WOS)));
  5445. }
  5446. /**
  5447. * Send any active EFST to those around.
  5448. * @param tbl: Unit to send the packet to
  5449. * @param bl: Objects walking into view
  5450. * @param target: Client send type
  5451. */
  5452. void clif_efst_status_change_sub(struct block_list *tbl, struct block_list *bl, enum send_target target) {
  5453. unsigned char i;
  5454. struct sc_display_entry **sc_display;
  5455. unsigned char sc_display_count;
  5456. bool spheres_sent;
  5457. nullpo_retv(bl);
  5458. switch( bl->type ){
  5459. case BL_PC: {
  5460. map_session_data* sd = (map_session_data*)bl;
  5461. sc_display = sd->sc_display;
  5462. sc_display_count = sd->sc_display_count;
  5463. spheres_sent = !sd->state.connect_new;
  5464. }
  5465. break;
  5466. case BL_NPC: {
  5467. struct npc_data* nd = (struct npc_data*)bl;
  5468. sc_display = nd->sc_display;
  5469. sc_display_count = nd->sc_display_count;
  5470. spheres_sent = true;
  5471. }
  5472. break;
  5473. default:
  5474. return;
  5475. }
  5476. for (i = 0; i < sc_display_count; i++) {
  5477. enum sc_type type = sc_display[i]->type;
  5478. status_change *sc = status_get_sc(bl);
  5479. const struct TimerData *td = (sc && sc->getSCE(type) ? get_timer(sc->getSCE(type)->timer) : nullptr);
  5480. t_tick tick = 0;
  5481. if (td)
  5482. tick = DIFF_TICK(td->tick, gettick());
  5483. // Status changes that need special handling
  5484. switch( type ){
  5485. case SC_SPHERE_1:
  5486. case SC_SPHERE_2:
  5487. case SC_SPHERE_3:
  5488. case SC_SPHERE_4:
  5489. case SC_SPHERE_5:
  5490. if( spheres_sent ){
  5491. target = AREA_WOS;
  5492. }
  5493. break;
  5494. case SC_HELLS_PLANT:
  5495. if( sc && sc->getSCE(type) ){
  5496. tick = sc->getSCE(type)->val4;
  5497. }
  5498. break;
  5499. }
  5500. #if PACKETVER > 20120418
  5501. 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);
  5502. #else
  5503. 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);
  5504. #endif
  5505. }
  5506. }
  5507. /// Notifies the client when a player enters the screen with an active EFST.
  5508. /// 08ff <id>.L <index>.W <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER) (PACKETVER >= 20111108)
  5509. /// 0984 <id>.L <index>.W <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER2) (PACKETVER >= 20120618)
  5510. void clif_efst_status_change(struct block_list *bl, int tid, enum send_target target, int type, t_tick tick, int val1, int val2, int val3) {
  5511. #if PACKETVER >= 20111108
  5512. unsigned char buf[32];
  5513. #if PACKETVER >= 20120618
  5514. const int cmd = 0x984;
  5515. #elif PACKETVER >= 20111108
  5516. const int cmd = 0x8ff;
  5517. #endif
  5518. int offset = 0;
  5519. if (type == EFST_BLANK)
  5520. return;
  5521. nullpo_retv(bl);
  5522. if (tick <= 0)
  5523. tick = 9999;
  5524. WBUFW(buf,offset + 0) = cmd;
  5525. WBUFL(buf,offset + 2) = tid;
  5526. WBUFW(buf,offset + 6) = type;
  5527. #if PACKETVER >= 20111108
  5528. WBUFL(buf,offset + 8) = client_tick(tick); // Set remaining status duration [exneval]
  5529. #if PACKETVER >= 20120618
  5530. WBUFL(buf,offset + 12) = client_tick(tick);
  5531. offset += 4;
  5532. #endif
  5533. WBUFL(buf,offset + 12) = val1;
  5534. WBUFL(buf,offset + 16) = val2;
  5535. WBUFL(buf,offset + 20) = val3;
  5536. #endif
  5537. clif_send(buf,packet_len(cmd),bl,target);
  5538. #endif
  5539. }
  5540. /// Send message (modified by [Yor]) (ZC_NOTIFY_PLAYERCHAT).
  5541. /// 008e <packet len>.W <message>.?B
  5542. void clif_displaymessage(const int fd, const char* mes)
  5543. {
  5544. nullpo_retv(mes);
  5545. if( session_isActive( fd ) ){
  5546. char *message, *line;
  5547. message = aStrdup(mes);
  5548. line = strtok(message, "\n");
  5549. while(line != nullptr) {
  5550. #if PACKETVER == 20141022
  5551. /** for some reason game client crashes depending on message pattern (only for this packet) **/
  5552. /** so we redirect to ZC_NPC_CHAT **/
  5553. //clif_messagecolor(&sd->bl, color_table[COLOR_DEFAULT], mes, false, SELF);
  5554. unsigned long color = (color_table[COLOR_DEFAULT] & 0x0000FF) << 16 | (color_table[COLOR_DEFAULT] & 0x00FF00) | (color_table[COLOR_DEFAULT] & 0xFF0000) >> 16; // RGB to BGR
  5555. unsigned short len = strnlen(line, CHAT_SIZE_MAX);
  5556. if (len > 0) {
  5557. WFIFOHEAD(fd, 13 + len);
  5558. WFIFOW(fd, 0) = 0x2C1;
  5559. WFIFOW(fd, 2) = 13 + len;
  5560. WFIFOL(fd, 4) = 0;
  5561. WFIFOL(fd, 8) = color;
  5562. safestrncpy(WFIFOCP(fd, 12), line, len + 1);
  5563. WFIFOSET(fd, WFIFOW(fd, 2));
  5564. }
  5565. #else
  5566. // Limit message to 255+1 characters (otherwise it causes a buffer overflow in the client)
  5567. int16 len;
  5568. len = static_cast<decltype(len)>( strnlen(line, CHAT_SIZE_MAX) );
  5569. if (len > 0) { // don't send a void message (it's not displaying on the client chat). @help can send void line.
  5570. WFIFOHEAD(fd, 5 + len);
  5571. WFIFOW(fd,0) = 0x8e;
  5572. WFIFOW(fd,2) = 5 + len; // 4 + len + nullptr teminate
  5573. safestrncpy(WFIFOCP(fd,4), line, len + 1);
  5574. WFIFOSET(fd, 5 + len);
  5575. }
  5576. #endif
  5577. line = strtok(nullptr, "\n");
  5578. }
  5579. aFree(message);
  5580. }
  5581. }
  5582. /// Send broadcast message in yellow or blue without font formatting.
  5583. /// 009a <packet len>.W <message>.?B (ZC_BROADCAST)
  5584. void clif_broadcast(struct block_list* bl, const char* mes, size_t len, int type, enum send_target target)
  5585. {
  5586. nullpo_retv(mes);
  5587. if (len < 2)
  5588. return;
  5589. PACKET_ZC_BROADCAST* p = reinterpret_cast<PACKET_ZC_BROADCAST*>( packet_buffer );
  5590. p->packetType = HEADER_ZC_BROADCAST;
  5591. p->PacketLength = sizeof( *p );
  5592. if( ( type&BC_BLUE ) != 0 ){
  5593. // If there's "blue" at the beginning of the message, game client will display it in blue instead of yellow.
  5594. const char* color = "blue";
  5595. size_t length = strlen( color );
  5596. strcpy( p->message, color );
  5597. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  5598. strncpy( &p->message[length], mes, len );
  5599. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5600. }else if( ( type&BC_WOE ) != 0 ){
  5601. // If there's "ssss", game client will recognize message as 'WoE broadcast'.
  5602. const char* color = "ssss";
  5603. size_t length = strlen( color );
  5604. strcpy( p->message, color );
  5605. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  5606. strncpy( &p->message[length], mes, len );
  5607. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5608. }else{
  5609. strncpy( p->message, mes, len );
  5610. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5611. }
  5612. clif_send( p, p->PacketLength, bl, target );
  5613. }
  5614. /*==========================================
  5615. * Displays a message on a 'bl' to all it's nearby clients
  5616. * 008d <PacketLength>.W <GID>.L <message>.?B (ZC_NOTIFY_CHAT)
  5617. *------------------------------------------*/
  5618. void clif_GlobalMessage( block_list& bl, const char* message, enum send_target target ){
  5619. nullpo_retv(message);
  5620. int16 len = (int16)( strlen( message ) + 1 );
  5621. if( len > CHAT_SIZE_MAX ) {
  5622. ShowWarning("clif_GlobalMessage: Truncating too long message '%s' (len=%" PRId16 ").\n", message, len);
  5623. len = CHAT_SIZE_MAX;
  5624. }
  5625. PACKET_ZC_NOTIFY_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT*>( packet_buffer );
  5626. p->PacketType = HEADER_ZC_NOTIFY_CHAT;
  5627. p->PacketLength = sizeof( *p );
  5628. p->GID = bl.id;
  5629. safestrncpy( p->Message, message, len );
  5630. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5631. clif_send( p, p->PacketLength, &bl, target );
  5632. }
  5633. /// Send broadcast message with font formatting.
  5634. /// 01c3 <packet len>.W <fontColor>.L <fontType>.W <fontSize>.W <fontAlign>.W <fontY>.W <message>.?B (ZC_BROADCAST2)
  5635. void clif_broadcast2(struct block_list* bl, const char* mes, size_t len, unsigned long fontColor, short fontType, short fontSize, short fontAlign, short fontY, enum send_target target)
  5636. {
  5637. nullpo_retv(mes);
  5638. if (len < 2)
  5639. return;
  5640. PACKET_ZC_BROADCAST2* p = reinterpret_cast<PACKET_ZC_BROADCAST2*>( packet_buffer );
  5641. p->packetType = HEADER_ZC_BROADCAST2;
  5642. p->PacketLength = sizeof( *p );
  5643. p->fontColor = fontColor;
  5644. p->fontType = fontType;
  5645. p->fontSize = fontSize;
  5646. p->fontAlign = fontAlign;
  5647. p->fontY = fontY;
  5648. strncpy( p->message, mes, len );
  5649. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5650. clif_send( p, p->PacketLength, bl, target );
  5651. }
  5652. /*
  5653. * Display *msg to all *users in channel
  5654. */
  5655. void clif_channel_msg(struct Channel *channel, const char *msg, unsigned long color) {
  5656. DBIterator *iter;
  5657. map_session_data *user;
  5658. unsigned short msg_len = 0, len = 0;
  5659. unsigned char buf[CHAT_SIZE_MAX];
  5660. if (!channel || !msg)
  5661. return;
  5662. msg_len = (unsigned short)(strlen(msg) + 1);
  5663. if( msg_len > sizeof(buf)-12 ) {
  5664. ShowWarning("clif_channel_msg: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  5665. msg_len = sizeof(buf)-12;
  5666. }
  5667. WBUFW(buf,0) = 0x2C1;
  5668. WBUFW(buf,2) = (len = msg_len + 12);
  5669. WBUFL(buf,4) = 0;
  5670. WBUFL(buf,8) = color;
  5671. safestrncpy(WBUFCP(buf,12), msg, msg_len);
  5672. iter = db_iterator(channel->users);
  5673. for( user = (map_session_data *)dbi_first(iter); dbi_exists(iter); user = (map_session_data *)dbi_next(iter) ) {
  5674. clif_send(buf, len, &user->bl, SELF);
  5675. }
  5676. dbi_destroy(iter);
  5677. }
  5678. /// Displays heal effect.
  5679. /// 013d <var id>.W <amount>.W (ZC_RECOVERY)
  5680. /// 0a27 <var id>.W <amount>.L (ZC_RECOVERY2)
  5681. /// var id:
  5682. /// 5 = HP (SP_HP)
  5683. /// 7 = SP (SP_SP)
  5684. /// ? = ignored
  5685. void clif_heal( map_session_data& sd, int32 type, uint32 val ) {
  5686. PACKET_ZC_RECOVERY packet{};
  5687. packet.packetType = HEADER_ZC_RECOVERY;
  5688. packet.type = static_cast<decltype(packet.type)>(type);
  5689. packet.amount = std::min( static_cast<decltype(packet.amount)>( val ), std::numeric_limits<decltype(packet.amount)>::max() );
  5690. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5691. }
  5692. /// Displays resurrection effect.
  5693. /// 0148 <id>.L <type>.W (ZC_RESURRECTION)
  5694. /// type:
  5695. /// ignored
  5696. void clif_resurrection( block_list& bl ){
  5697. PACKET_ZC_RESURRECTION packet{};
  5698. packet.packetType = HEADER_ZC_RESURRECTION;
  5699. packet.gid = bl.id;
  5700. packet.type = 0;
  5701. clif_send( &packet, sizeof( packet ), &bl, AREA );
  5702. if (disguised(&bl)) {
  5703. clif_spawn(&bl);
  5704. }
  5705. }
  5706. /// Sets the map property
  5707. /// 0199 <type>.W (ZC_NOTIFY_MAPPROPERTY)
  5708. /// 099b <type>.W <flags>.L (ZC_MAPPROPERTY_R2)
  5709. void clif_map_property(struct block_list *bl, enum map_property property, enum send_target t)
  5710. {
  5711. #if PACKETVER >= 20121010
  5712. short cmd = 0x99b;
  5713. unsigned char buf[8];
  5714. #else
  5715. short cmd = 0x199;
  5716. unsigned char buf[4];
  5717. #endif
  5718. WBUFW(buf,0)=cmd;
  5719. WBUFW(buf,2)=property;
  5720. #if PACKETVER >= 20121010
  5721. struct map_data *mapdata = map_getmapdata(bl->m);
  5722. map_session_data *sd = BL_CAST(BL_PC, bl);
  5723. WBUFL(buf,4) = ((mapdata->getMapFlag(MF_PVP) || (sd && sd->duel_group > 0))<<0)| // PARTY - Show attack cursor on non-party members (PvP)
  5724. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<1)|// GUILD - Show attack cursor on non-guild members (GvG)
  5725. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<2)|// SIEGE - Show emblem over characters heads when in GvG (WoE castle)
  5726. ((mapdata->getMapFlag(MF_FORCEMINEFFECT) || mapdata_flag_gvg2(mapdata))<<3)| // USE_SIMPLE_EFFECT - Forces simpler skill effects, like /mineffect command
  5727. ((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
  5728. ((mapdata->getMapFlag(MF_PVP))<<5)| // COUNT_PK - Show the PvP counter
  5729. ((mapdata->getMapFlag(MF_PARTYLOCK))<<6)| // NO_PARTY_FORMATION - Prevents party creation/modification (Might be used for instance dungeons)
  5730. ((mapdata->getMapFlag(MF_BATTLEGROUND))<<7)| // BATTLEFIELD - Unknown (Does something for battlegrounds areas)
  5731. ((mapdata->getMapFlag(MF_NOCOSTUME))<<8)| // DISABLE_COSTUMEITEM - Disable costume sprites
  5732. ((!mapdata->getMapFlag(MF_NOUSECART))<<9)| // USECART - Allow opening cart inventory (Well force it to always allow it)
  5733. ((!mapdata->getMapFlag(MF_NOSUNMOONSTARMIRACLE))<<10); // SUNMOONSTAR_MIRACLE - Allows Star Gladiator's Miracle to activate
  5734. //(1<<11); // Unused bits. 1 - 10 is 0x1 length and 11 is 0x15 length. May be used for future settings.
  5735. #endif
  5736. clif_send(buf,packet_len(cmd),bl,t);
  5737. }
  5738. /// Set the map type.
  5739. /// 01d6 <type>.W (ZC_NOTIFY_MAPPROPERTY2)
  5740. void clif_map_type( map_session_data& sd, e_map_type type ){
  5741. PACKET_ZC_NOTIFY_MAPPROPERTY2 packet{};
  5742. packet.packetType = HEADER_ZC_NOTIFY_MAPPROPERTY2;
  5743. packet.type = static_cast<decltype(packet.type)>(type);
  5744. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5745. }
  5746. /// Updates PvP ranking (ZC_NOTIFY_RANKING).
  5747. /// 019a <id>.L <ranking>.L <total>.L
  5748. void clif_pvpset(map_session_data *sd,int pvprank,int pvpnum,int type)
  5749. {
  5750. if(type == 2) {
  5751. int fd = sd->fd;
  5752. WFIFOHEAD(fd,packet_len(0x19a));
  5753. WFIFOW(fd,0) = 0x19a;
  5754. WFIFOL(fd,2) = sd->bl.id;
  5755. WFIFOL(fd,6) = pvprank;
  5756. WFIFOL(fd,10) = pvpnum;
  5757. WFIFOSET(fd,packet_len(0x19a));
  5758. } else {
  5759. unsigned char buf[32];
  5760. WBUFW(buf,0) = 0x19a;
  5761. WBUFL(buf,2) = sd->bl.id;
  5762. if(sd->sc.option&(OPTION_HIDE|OPTION_CLOAK))
  5763. WBUFL(buf,6) = UINT32_MAX; //On client displays as --
  5764. else
  5765. WBUFL(buf,6) = pvprank;
  5766. WBUFL(buf,10) = pvpnum;
  5767. if(pc_isinvisible(sd) || sd->disguise) //Causes crashes when a 'mob' with pvp info dies.
  5768. clif_send(buf,packet_len(0x19a),&sd->bl,SELF);
  5769. else if(!type)
  5770. clif_send(buf,packet_len(0x19a),&sd->bl,AREA);
  5771. else
  5772. clif_send(buf,packet_len(0x19a),&sd->bl,ALL_SAMEMAP);
  5773. }
  5774. }
  5775. /*==========================================
  5776. *
  5777. *------------------------------------------*/
  5778. void clif_map_property_mapall(int map_idx, enum map_property property)
  5779. {
  5780. struct block_list bl;
  5781. bl.id = 0;
  5782. bl.type = BL_NUL;
  5783. bl.m = map_idx;
  5784. clif_map_property( &bl, property, ALL_SAMEMAP );
  5785. }
  5786. /// Notifies the client about the result of a refine attempt.
  5787. /// 0188 <result>.W <index>.W <refine>.W (ZC_ACK_ITEMREFINING)
  5788. /// result:
  5789. /// 0 = success
  5790. /// 1 = failure
  5791. /// 2 = downgrade
  5792. /// 3 = failure (?)
  5793. void clif_refine( map_session_data& sd, uint16 index, e_ack_itemrefining result ){
  5794. PACKET_ZC_ACK_ITEMREFINING packet{};
  5795. packet.packetType = HEADER_ZC_ACK_ITEMREFINING;
  5796. packet.result = static_cast<decltype(packet.result)>(result);
  5797. packet.index = client_index( index );
  5798. packet.value = static_cast<decltype(packet.value)>( sd.inventory.u.items_inventory[index].refine );
  5799. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5800. }
  5801. /// Notifies the client about the result of a weapon refine attempt (ZC_ACK_WEAPONREFINE).
  5802. /// 0223 <result>.L <nameid>.W
  5803. /// result:
  5804. /// 0 = "weapon upgraded: %s" MsgStringTable[911] in rgb(0,255,255)
  5805. /// 1 = "weapon upgraded: %s" MsgStringTable[912] in rgb(0,205,205)
  5806. /// 2 = "cannot upgrade %s until you level up the upgrade weapon skill" MsgStringTable[913] in rgb(255,200,200)
  5807. /// 3 = "you lack the item %s to upgrade the weapon" MsgStringTable[914] in rgb(255,200,200)
  5808. void clif_upgrademessage( map_session_data* sd, int result, t_itemid item_id ){
  5809. PACKET_ZC_ACK_WEAPONREFINE p = {};
  5810. p.packetType = HEADER_ZC_ACK_WEAPONREFINE;
  5811. p.result = result;
  5812. p.itemId = client_nameid( item_id );
  5813. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  5814. }
  5815. /// Whisper is transmitted to the destination player (ZC_WHISPER).
  5816. /// 0097 <packet len>.W <nick>.24B <message>.?B
  5817. /// 0097 <packet len>.W <nick>.24B <isAdmin>.L <message>.?B (PACKETVER >= 20091104)
  5818. void clif_wis_message(map_session_data* sd, const char* nick, const char* mes, size_t mes_len, int gmlvl){
  5819. PACKET_ZC_WHISPER* p = reinterpret_cast<PACKET_ZC_WHISPER*>( packet_buffer );
  5820. p->PacketType = HEADER_ZC_WHISPER;
  5821. p->PacketLength = sizeof( *p );
  5822. safestrncpy( p->sender, nick, NAME_LENGTH );
  5823. safestrncpy( p->message, mes, mes_len );
  5824. p->PacketLength += static_cast<decltype( p->PacketLength )>( mes_len );
  5825. #if PACKETVER >= 20091104
  5826. map_session_data* ssd = map_nick2sd(nick, false);
  5827. // If it is not a message from the server or a player from another map-server
  5828. if( ssd != nullptr ){
  5829. gmlvl = pc_get_group_level(ssd);
  5830. }
  5831. p->isAdmin = (gmlvl == 99) ? 1 : 0;
  5832. #if PACKETVER_MAIN_NUM >= 20131204 || PACKETVER_RE_NUM >= 20131120 || defined(PACKETVER_ZERO)
  5833. if( ssd != nullptr ){
  5834. p->senderGID = ssd->bl.id;
  5835. }else{
  5836. p->senderGID = 0;
  5837. }
  5838. #endif
  5839. #endif
  5840. clif_send( p, p->PacketLength, &sd->bl, SELF );
  5841. }
  5842. /// Inform the player about the result of his whisper action
  5843. /// 0098 <result>.B (ZC_ACK_WHISPER).
  5844. /// 09df <result>.B <CID>.L (ZC_ACK_WHISPER02).
  5845. /// result:
  5846. /// 0 = success to send whisper
  5847. /// 1 = target character is not loged in
  5848. /// 2 = ignored by target
  5849. /// 3 = everyone ignored by target
  5850. void clif_wis_end( map_session_data& sd, e_ack_whisper result ){
  5851. PACKET_ZC_ACK_WHISPER packet{};
  5852. packet.packetType = HEADER_ZC_ACK_WHISPER;
  5853. packet.result = static_cast<decltype(packet.result)>(result);
  5854. #if PACKETVER >= 20131223
  5855. packet.CID = sd.status.char_id;
  5856. #endif
  5857. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5858. }
  5859. /// Returns character name requested by char_id (ZC_ACK_REQNAME_BYGID).
  5860. /// 0194 <char id>.L <name>.24B
  5861. /// 0af7 <flag>.W <char id>.L <name>.24B
  5862. void clif_solved_charname(int fd, int charid, const char* name)
  5863. {
  5864. #if PACKETVER >= 20180221
  5865. WFIFOHEAD(fd,packet_len(0xaf7));
  5866. WFIFOW(fd,0) = 0xaf7;
  5867. WFIFOW(fd,2) = name[0] ? 3 : 2;
  5868. WFIFOL(fd,4) = charid;
  5869. safestrncpy(WFIFOCP(fd, 8), name, NAME_LENGTH);
  5870. WFIFOSET(fd,packet_len(0x0af7));
  5871. #else
  5872. WFIFOHEAD(fd,packet_len(0x194));
  5873. WFIFOW(fd,0)=0x194;
  5874. WFIFOL(fd,2)=charid;
  5875. safestrncpy(WFIFOCP(fd,6), name, NAME_LENGTH);
  5876. WFIFOSET(fd,packet_len(0x194));
  5877. #endif
  5878. }
  5879. /// Presents a list of items that can be carded/composed (ZC_ITEMCOMPOSITION_LIST).
  5880. /// 017b <packet len>.W { <name id>.W }*
  5881. void clif_use_card(map_session_data *sd,int idx)
  5882. {
  5883. int i,c,ep;
  5884. int fd=sd->fd;
  5885. nullpo_retv(sd);
  5886. if (idx < 0 || idx >= MAX_INVENTORY) //Crash-fix from bad packets.
  5887. return;
  5888. if (!sd->inventory_data[idx] || sd->inventory_data[idx]->type != IT_CARD)
  5889. return; //Avoid parsing invalid item indexes (no card/no item)
  5890. ep=sd->inventory_data[idx]->equip;
  5891. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5892. WFIFOW(fd,0)=0x17b;
  5893. for(i=c=0;i<MAX_INVENTORY;i++){
  5894. int j;
  5895. if(sd->inventory_data[i] == nullptr)
  5896. continue;
  5897. if(sd->inventory_data[i]->type!=IT_WEAPON && sd->inventory_data[i]->type!=IT_ARMOR)
  5898. continue;
  5899. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0])) //Can't slot it
  5900. continue;
  5901. if(sd->inventory.u.items_inventory[i].identify==0 ) //Not identified
  5902. continue;
  5903. if((sd->inventory_data[i]->equip&ep)==0) //Not equippable on this part.
  5904. continue;
  5905. if(sd->inventory_data[i]->type==IT_WEAPON && ep==EQP_SHIELD) //Shield card won't go on left weapon.
  5906. continue;
  5907. 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.
  5908. continue;
  5909. ARR_FIND( 0, sd->inventory_data[i]->slots, j, sd->inventory.u.items_inventory[i].card[j] == 0 );
  5910. if( j == sd->inventory_data[i]->slots ) // No room
  5911. continue;
  5912. if( sd->inventory.u.items_inventory[i].equip > 0 ) // Do not check items that are already equipped
  5913. continue;
  5914. WFIFOW(fd,4+c*2)=i+2;
  5915. c++;
  5916. }
  5917. if( !c ) return; // no item is available for card insertion
  5918. WFIFOW(fd,2)=4+c*2;
  5919. WFIFOSET(fd,WFIFOW(fd,2));
  5920. }
  5921. /// Notifies the client about the result of item carding/composition.
  5922. /// 017d <equip index>.W <card index>.W <result>.B (ZC_ACK_ITEMCOMPOSITION)
  5923. /// result:
  5924. /// 0 = success
  5925. /// 1 = failure
  5926. void clif_insert_card( map_session_data& sd, int32 idx_equip, int32 idx_card, bool failure ){
  5927. PACKET_ZC_ACK_ITEMCOMPOSITION packet{};
  5928. packet.packetType = HEADER_ZC_ACK_ITEMCOMPOSITION;
  5929. packet.equipIndex = client_index( idx_equip );
  5930. packet.cardIndex = client_index( idx_card );
  5931. packet.result = failure;
  5932. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5933. }
  5934. /// Presents a list of items that can be identified (ZC_ITEMIDENTIFY_LIST).
  5935. /// 0177 <packet len>.W { <index>.W }*
  5936. void clif_item_identify_list(map_session_data *sd)
  5937. {
  5938. int i,c;
  5939. int fd;
  5940. nullpo_retv(sd);
  5941. fd=sd->fd;
  5942. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5943. WFIFOW(fd,0)=0x177;
  5944. for(i=c=0;i<MAX_INVENTORY;i++){
  5945. if(sd->inventory.u.items_inventory[i].nameid > 0 && !sd->inventory.u.items_inventory[i].identify){
  5946. WFIFOW(fd,c*2+4)=client_index(i);
  5947. c++;
  5948. }
  5949. }
  5950. if(c > 0) {
  5951. WFIFOW(fd,2)=c*2+4;
  5952. WFIFOSET(fd,WFIFOW(fd,2));
  5953. sd->menuskill_id = MC_IDENTIFY;
  5954. sd->menuskill_val = c;
  5955. sd->state.workinprogress = WIP_DISABLE_ALL;
  5956. }
  5957. }
  5958. /// Notifies the client about the result of a item identify request.
  5959. /// 0179 <index>.W <result>.B (ZC_ACK_ITEMIDENTIFY)
  5960. /// result:
  5961. /// 0 = success
  5962. /// 1 = failure
  5963. void clif_item_identified( map_session_data& sd, int32 idx, bool failure ){
  5964. PACKET_ZC_ACK_ITEMIDENTIFY packet{};
  5965. packet.packetType = HEADER_ZC_ACK_ITEMIDENTIFY;
  5966. packet.index = client_index( idx );
  5967. packet.result = failure;
  5968. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5969. }
  5970. /// Presents a list of items that can be repaired.
  5971. /// 01fc <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }* (ZC_REPAIRITEMLIST)
  5972. void clif_item_repair_list( map_session_data& sd, map_session_data& dstsd, uint16 lv ){
  5973. PACKET_ZC_REPAIRITEMLIST* p = reinterpret_cast<PACKET_ZC_REPAIRITEMLIST*>( packet_buffer );
  5974. p->packetType = HEADER_ZC_REPAIRITEMLIST;
  5975. p->packetLength = sizeof( *p );
  5976. size_t c = 0;
  5977. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  5978. 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)){
  5979. p->items[c].index = static_cast<decltype( p->items[0].index )>( i );
  5980. p->items[c].itemId = client_nameid( dstsd.inventory.u.items_inventory[i].nameid );
  5981. p->items[c].refine = dstsd.inventory.u.items_inventory[i].refine;
  5982. clif_addcards( &p->items[c].slot, &dstsd.inventory.u.items_inventory[i] );
  5983. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  5984. c++;
  5985. }
  5986. }
  5987. if( c > 0 ){
  5988. clif_send( p, p->packetLength, &sd.bl, SELF );
  5989. sd.menuskill_id = BS_REPAIRWEAPON;
  5990. sd.menuskill_val = dstsd.bl.id;
  5991. sd.menuskill_val2 = lv;
  5992. }else{
  5993. clif_skill_fail( sd, sd.ud.skill_id );
  5994. }
  5995. }
  5996. /// Notifies the client about the result of a item repair request.
  5997. /// 01fe <index>.W <result>.B (ZC_ACK_ITEMREPAIR)
  5998. /// index:
  5999. /// ignored (inventory index)
  6000. /// result:
  6001. /// 0 = Item repair success.
  6002. /// 1 = Item repair failure.
  6003. void clif_item_repaireffect( map_session_data& sd, int32 idx, bool failure ){
  6004. PACKET_ZC_ACK_ITEMREPAIR packet{};
  6005. packet.packetType = HEADER_ZC_ACK_ITEMREPAIR;
  6006. packet.index = client_index( idx );
  6007. packet.result = failure;
  6008. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6009. }
  6010. /// Displays a message, that an equipment got damaged.
  6011. /// 02bb <equip location>.W <account id>.L (ZC_EQUIPITEM_DAMAGED)
  6012. void clif_item_damaged( map_session_data& sd, uint16 position ){
  6013. PACKET_ZC_EQUIPITEM_DAMAGED packet{};
  6014. packet.packetType = HEADER_ZC_EQUIPITEM_DAMAGED;
  6015. packet.equipLocation = position;
  6016. packet.GID = sd.bl.id;
  6017. // TODO: the packet seems to be sent to other people as well, probably party and/or guild.
  6018. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6019. }
  6020. /// Presents a list of weapon items that can be refined [Taken from jAthena] (ZC_NOTIFY_WEAPONITEMLIST).
  6021. /// 0221 <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }*
  6022. void clif_item_refine_list( map_session_data& sd ){
  6023. PACKET_ZC_NOTIFY_WEAPONITEMLIST* p = reinterpret_cast<PACKET_ZC_NOTIFY_WEAPONITEMLIST*>( packet_buffer );
  6024. p->packetType = HEADER_ZC_NOTIFY_WEAPONITEMLIST;
  6025. p->packetLength = sizeof( *p );
  6026. uint16 skill_lv = pc_checkskill( &sd, WS_WEAPONREFINE );
  6027. int refine_item[MAX_WEAPON_LEVEL];
  6028. refine_item[0] = pc_search_inventory( &sd, ITEMID_PHRACON );
  6029. refine_item[1] = pc_search_inventory( &sd, ITEMID_EMVERETARCON );
  6030. refine_item[2] = refine_item[3] = pc_search_inventory( &sd, ITEMID_ORIDECON );
  6031. #ifdef RENEWAL
  6032. refine_item[4] = -1;
  6033. #endif
  6034. int count = 0;
  6035. for( int i = 0; i < MAX_INVENTORY; i++ ){
  6036. if( sd.inventory.u.items_inventory[i].nameid > 0 && sd.inventory.u.items_inventory[i].refine < skill_lv &&
  6037. sd.inventory_data[i] != nullptr && sd.inventory_data[i]->type == IT_WEAPON &&
  6038. sd.inventory.u.items_inventory[i].identify && sd.inventory_data[i]->weapon_level >= 1 &&
  6039. refine_item[sd.inventory_data[i]->weapon_level - 1] != -1 && !( sd.inventory.u.items_inventory[i].equip & EQP_ARMS ) ){
  6040. p->items[count].index = client_index( i );
  6041. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  6042. p->items[count].refine = sd.inventory.u.items_inventory[i].refine;
  6043. clif_addcards( &p->items[count].slot, &sd.inventory.u.items_inventory[i] );
  6044. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  6045. count++;
  6046. }
  6047. }
  6048. clif_send( p, p->packetLength, &sd.bl, SELF );
  6049. if( count > 0 ){
  6050. sd.menuskill_id = WS_WEAPONREFINE;
  6051. sd.menuskill_val = skill_lv;
  6052. }
  6053. }
  6054. /// Notification of an auto-casted skill (ZC_AUTORUN_SKILL).
  6055. /// 0147 <skill id>.W <type>.L <level>.W <sp cost>.W <atk range>.W <skill name>.24B <upgradable>.B
  6056. void clif_item_skill( map_session_data *sd, uint16 skill_id, uint16 skill_lv ){
  6057. nullpo_retv( sd );
  6058. PACKET_ZC_AUTORUN_SKILL p = {};
  6059. p.packetType = HEADER_ZC_AUTORUN_SKILL;
  6060. p.skill_id = skill_id;
  6061. p.skill_type = skill_get_inf( skill_id );
  6062. p.skill_lv = skill_lv;
  6063. p.skill_sp = skill_get_sp( skill_id, skill_lv );
  6064. p.skill_range = skill_get_range2( &sd->bl, skill_id, skill_lv, false );
  6065. safestrncpy( p.skill_name, skill_get_name( skill_id ), NAME_LENGTH );
  6066. p.up_flag = false;
  6067. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6068. }
  6069. /// Adds an item to character's cart.
  6070. /// 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)
  6071. /// 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)
  6072. /// 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)
  6073. void clif_cart_additem( map_session_data *sd, int n, int amount ){
  6074. nullpo_retv( sd );
  6075. int fd = sd->fd;
  6076. if( !session_isActive( fd ) ){
  6077. return;
  6078. }
  6079. if( n < 0 || n >= MAX_CART || sd->cart.u.items_cart[n].nameid == 0 ){
  6080. return;
  6081. }
  6082. PACKET_ZC_ADD_ITEM_TO_CART p = {};
  6083. p.packetType = HEADER_ZC_ADD_ITEM_TO_CART;
  6084. p.index = client_index( n );
  6085. p.amount = amount;
  6086. p.itemId = client_nameid( sd->cart.u.items_cart[n].nameid );
  6087. #if PACKETVER >= 5
  6088. p.itemType = itemdb_type( sd->cart.u.items_cart[n].nameid );
  6089. #endif
  6090. p.identified = sd->cart.u.items_cart[n].identify;
  6091. p.damaged = sd->cart.u.items_cart[n].attribute;
  6092. p.refine = sd->cart.u.items_cart[n].refine;
  6093. clif_addcards( &p.slot, &sd->cart.u.items_cart[n] );
  6094. #if PACKETVER >= 20150226
  6095. clif_add_random_options( p.option_data, sd->cart.u.items_cart[n] );
  6096. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6097. p.grade = sd->cart.u.items_cart[n].enchantgrade;
  6098. #endif
  6099. #endif
  6100. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6101. }
  6102. /// Acknowledge an item have been added to cart
  6103. /// 012c <result>.B (ZC_ACK_ADDITEM_TO_CART)
  6104. /// result:
  6105. /// 0 = ADDITEM_TO_CART_FAIL_WEIGHT
  6106. /// 1 = ADDITEM_TO_CART_FAIL_COUNT
  6107. void clif_cart_additem_ack( map_session_data& sd, e_ack_additem_to_cart flag ){
  6108. PACKET_ZC_ACK_ADDITEM_TO_CART packet{};
  6109. packet.packetType = HEADER_ZC_ACK_ADDITEM_TO_CART;
  6110. packet.result = static_cast<decltype(packet.result)>(flag);
  6111. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6112. }
  6113. // 09B7 <unknow data> (ZC_ACK_OPEN_BANKING)
  6114. void clif_bank_open( map_session_data& sd ){
  6115. #if PACKETVER >= 20130717
  6116. PACKET_ZC_ACK_OPEN_BANKING p = {};
  6117. p.packetType = HEADER_ZC_ACK_OPEN_BANKING;
  6118. p.unknown = 0;
  6119. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6120. #endif
  6121. }
  6122. /*
  6123. * Request to Open the banking system
  6124. * 09B6 <aid>L (CZ_REQ_OPEN_BANKING)
  6125. */
  6126. void clif_parse_BankOpen(int fd, map_session_data* sd) {
  6127. #if PACKETVER >= 20130717
  6128. const PACKET_CZ_REQ_OPEN_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BANKING*>( RFIFOP( fd, 0 ) );
  6129. //TODO check if preventing trade or stuff like that
  6130. //also mark something in case char ain't available for saving, should we check now ?
  6131. nullpo_retv(sd);
  6132. if( !battle_config.feature_banking ) {
  6133. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6134. return;
  6135. }
  6136. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6137. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6138. return;
  6139. }
  6140. else {
  6141. if(sd->status.account_id == p->AID){
  6142. sd->state.banking = 1;
  6143. //request save ?
  6144. //chrif_bankdata_request(sd->status.account_id, sd->status.char_id);
  6145. //on succes open bank ?
  6146. clif_bank_open( *sd );
  6147. }
  6148. }
  6149. #endif
  6150. }
  6151. // 09B9 <unknow data> (ZC_ACK_CLOSE_BANKING)
  6152. void clif_bank_close( map_session_data& sd ){
  6153. #if PACKETVER >= 20130717
  6154. PACKET_ZC_ACK_CLOSE_BANKING p = {};
  6155. p.packetType = HEADER_ZC_ACK_CLOSE_BANKING;
  6156. p.unknown = 0;
  6157. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6158. #endif
  6159. }
  6160. /*
  6161. * Request to close the banking system
  6162. * 09B8 <aid>L (CZ_REQ_CLOSE_BANKING)
  6163. */
  6164. void clif_parse_BankClose(int fd, map_session_data* sd) {
  6165. #if PACKETVER >= 20130717
  6166. const PACKET_CZ_REQ_CLOSE_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_CLOSE_BANKING*>( RFIFOP( fd, 0 ) );
  6167. nullpo_retv(sd);
  6168. if( !battle_config.feature_banking ) {
  6169. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6170. //still allow to go trough to not stuck player if we have disable it while they was in
  6171. }
  6172. if(sd->status.account_id == p->AID){
  6173. sd->state.banking = 0;
  6174. clif_bank_close( *sd );
  6175. }
  6176. #endif
  6177. }
  6178. /*
  6179. * Display how much we got in bank (I suppose)
  6180. * 09A6 <Bank_Vault>Q <Reason>W (ZC_BANKING_CHECK)
  6181. */
  6182. void clif_Bank_Check( map_session_data& sd ){
  6183. #if PACKETVER >= 20130717
  6184. PACKET_ZC_BANKING_CHECK p = {};
  6185. p.packetType = HEADER_ZC_BANKING_CHECK;
  6186. p.money = sd.bank_vault;
  6187. p.reason = 0;
  6188. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6189. #endif
  6190. }
  6191. /*
  6192. * Requesting the data in bank
  6193. * 09AB <aid>L (CZ_REQ_BANKING_CHECK)
  6194. */
  6195. void clif_parse_BankCheck(int fd, map_session_data* sd) {
  6196. #if PACKETVER >= 20130717
  6197. const PACKET_CZ_REQ_BANKING_CHECK* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_CHECK*>( RFIFOP( fd, 0 ) );
  6198. nullpo_retv(sd);
  6199. if( !battle_config.feature_banking ) {
  6200. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6201. return;
  6202. }
  6203. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6204. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6205. return;
  6206. }
  6207. else {
  6208. if(sd->status.account_id == p->AID) //since we have it let check it for extra security
  6209. clif_Bank_Check( *sd );
  6210. }
  6211. #endif
  6212. }
  6213. /*
  6214. * Acknowledge of deposit some money in bank
  6215. 09A8 <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_DEPOSIT)
  6216. */
  6217. void clif_bank_deposit( map_session_data& sd, enum e_BANKING_DEPOSIT_ACK reason ){
  6218. #if PACKETVER >= 20130717
  6219. PACKET_ZC_ACK_BANKING_DEPOSIT p = {};
  6220. p.packetType = HEADER_ZC_ACK_BANKING_DEPOSIT;
  6221. p.money = sd.bank_vault;
  6222. p.zeny = sd.status.zeny;
  6223. p.reason = reason;
  6224. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6225. #endif
  6226. }
  6227. /*
  6228. * Request saving some money in bank
  6229. * @author : original [Yommy/Hercules]
  6230. * 09A7 <AID>L <Money>L (CZ_REQ_BANKING_DEPOSIT)
  6231. */
  6232. void clif_parse_BankDeposit(int fd, map_session_data* sd) {
  6233. #if PACKETVER >= 20130717
  6234. const PACKET_CZ_REQ_BANKING_DEPOSIT* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_DEPOSIT*>( RFIFOP( fd, 0 ) );
  6235. nullpo_retv(sd);
  6236. if( !battle_config.feature_banking ) {
  6237. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6238. return;
  6239. }
  6240. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6241. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6242. return;
  6243. }
  6244. else {
  6245. if(sd->status.account_id == p->AID){
  6246. enum e_BANKING_DEPOSIT_ACK reason = pc_bank_deposit(sd,max(0,p->zeny));
  6247. clif_bank_deposit( *sd, reason );
  6248. }
  6249. }
  6250. #endif
  6251. }
  6252. /*
  6253. * Acknowledge of withdrawing some money from bank
  6254. * 09AA <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_WITHDRAW)
  6255. */
  6256. void clif_bank_withdraw( map_session_data& sd, enum e_BANKING_WITHDRAW_ACK reason ){
  6257. #if PACKETVER >= 20130717
  6258. PACKET_ZC_ACK_BANKING_WITHDRAW p = {};
  6259. p.packetType = HEADER_ZC_ACK_BANKING_WITHDRAW;
  6260. p.reason = reason;
  6261. p.money = sd.bank_vault;
  6262. p.zeny = sd.status.zeny;
  6263. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6264. #endif
  6265. }
  6266. /*
  6267. * Request Withdrawing some money from bank
  6268. * 09A9 <AID>L <Money>L (CZ_REQ_BANKING_WITHDRAW)
  6269. */
  6270. void clif_parse_BankWithdraw(int fd, map_session_data* sd) {
  6271. #if PACKETVER >= 20130717
  6272. const PACKET_CZ_REQ_BANKING_WITHDRAW* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_WITHDRAW*>( RFIFOP( fd, 0 ) );
  6273. nullpo_retv(sd);
  6274. if( !battle_config.feature_banking ) {
  6275. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6276. return;
  6277. }
  6278. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6279. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6280. return;
  6281. }
  6282. else {
  6283. if(sd->status.account_id == p->AID){
  6284. enum e_BANKING_WITHDRAW_ACK reason = pc_bank_withdraw(sd,max(0,p->zeny));
  6285. clif_bank_withdraw( *sd, reason );
  6286. }
  6287. }
  6288. #endif
  6289. }
  6290. /// Deletes an item from character's cart.
  6291. /// 0125 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_CART)
  6292. void clif_cart_delitem( map_session_data& sd, int32 index, int32 amount ){
  6293. PACKET_ZC_DELETE_ITEM_FROM_CART packet{};
  6294. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_CART;
  6295. packet.index = client_index( index );
  6296. packet.amount = amount;
  6297. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6298. }
  6299. /// Opens the shop creation menu.
  6300. /// 012d <num>.W (ZC_OPENSTORE)
  6301. /// num:
  6302. /// number of allowed item slots
  6303. void clif_openvendingreq( map_session_data& sd, uint16 num ){
  6304. PACKET_ZC_OPENSTORE packet{};
  6305. packet.packetType = HEADER_ZC_OPENSTORE;
  6306. packet.num = num;
  6307. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6308. }
  6309. /// Displays a vending board to target/area (ZC_STORE_ENTRY).
  6310. /// 0131 <owner id>.L <message>.80B
  6311. void clif_showvendingboard( map_session_data& sd, enum send_target target, struct block_list* tbl ){
  6312. if( tbl == nullptr ){
  6313. tbl = &sd.bl;
  6314. target = AREA_WOS;
  6315. }
  6316. PACKET_ZC_STORE_ENTRY p = {};
  6317. p.packetType = HEADER_ZC_STORE_ENTRY;
  6318. p.makerAID = sd.status.account_id;
  6319. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  6320. clif_send( &p, sizeof( p ), tbl, target );
  6321. }
  6322. /// Removes a vending board from screen (ZC_DISAPPEAR_ENTRY).
  6323. /// 0132 <owner id>.L
  6324. void clif_closevendingboard(struct block_list* bl, int fd)
  6325. {
  6326. unsigned char buf[16];
  6327. nullpo_retv(bl);
  6328. WBUFW(buf,0) = 0x132;
  6329. WBUFL(buf,2) = bl->id;
  6330. if( session_isActive(fd) ) {
  6331. WFIFOHEAD(fd,packet_len(0x132));
  6332. memcpy(WFIFOP(fd,0),buf,packet_len(0x132));
  6333. WFIFOSET(fd,packet_len(0x132));
  6334. } else {
  6335. clif_send(buf,packet_len(0x132),bl,AREA_WOS);
  6336. }
  6337. }
  6338. /// Sends a list of items in a shop.
  6339. /// 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)
  6340. /// 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)
  6341. void clif_vendinglist( map_session_data& sd, map_session_data& vsd ){
  6342. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC*>( packet_buffer );
  6343. p->packetType = HEADER_ZC_PC_PURCHASE_ITEMLIST_FROMMC;
  6344. p->packetLength = sizeof( *p );
  6345. p->AID = vsd.status.account_id;
  6346. #if PACKETVER >= 20100105
  6347. p->venderId = vsd.vender_id;
  6348. #endif
  6349. for( int i = 0, count = 0; i < vsd.vend_num; i++ ){
  6350. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC_sub& entry = p->items[count];
  6351. int index = vsd.vending[i].index;
  6352. struct item_data* data = itemdb_search( vsd.cart.u.items_cart[index].nameid );
  6353. if( data == nullptr ){
  6354. continue;
  6355. }
  6356. entry.price = vsd.vending[i].value;
  6357. entry.amount = vsd.vending[i].amount;
  6358. entry.index = client_index( index );
  6359. entry.itemType = itemtype( vsd.cart.u.items_cart[index].nameid );
  6360. entry.itemId = client_nameid( vsd.cart.u.items_cart[index].nameid );
  6361. entry.identified = vsd.cart.u.items_cart[index].identify;
  6362. entry.damaged = vsd.cart.u.items_cart[index].attribute;
  6363. entry.refine = vsd.cart.u.items_cart[index].refine;
  6364. clif_addcards( &entry.slot, &vsd.cart.u.items_cart[index] );
  6365. #if PACKETVER >= 20150226
  6366. clif_add_random_options( entry.option_data, vsd.cart.u.items_cart[index] );
  6367. #if PACKETVER >= 20160921
  6368. entry.location = pc_equippoint_sub( &sd, data );
  6369. entry.viewSprite = data->look;
  6370. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6371. entry.grade = vsd.cart.u.items_cart[index].enchantgrade;
  6372. #endif
  6373. #endif
  6374. #endif
  6375. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6376. count++;
  6377. }
  6378. clif_send( p, p->packetLength, &sd.bl, SELF );
  6379. }
  6380. /// Shop purchase failure.
  6381. /// 0135 <index>.W <amount>.W <result>.B (ZC_PC_PURCHASE_RESULT_FROMMC)
  6382. /// result:
  6383. /// 0 = success
  6384. /// 1 = not enough zeny
  6385. /// 2 = overweight
  6386. /// 4 = out of stock
  6387. /// 5 = "cannot use an npc shop while in a trade"
  6388. /// 6 = Because the store information was incorrect the item was not purchased.
  6389. /// 7 = No sales information.
  6390. void clif_buyvending( map_session_data& sd, uint16 index, uint16 amount, e_pc_purchase_result_frommc result ){
  6391. PACKET_ZC_PC_PURCHASE_RESULT_FROMMC packet{};
  6392. packet.packetType = HEADER_ZC_PC_PURCHASE_RESULT_FROMMC;
  6393. packet.index = client_index( index );
  6394. packet.amount = amount;
  6395. packet.result = static_cast<decltype(packet.result)>(result);
  6396. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6397. }
  6398. /// Show's vending player its list of items for sale.
  6399. /// 0a28 <result>.B (ZC_ACK_OPENSTORE2)
  6400. void clif_openvending_ack( map_session_data& sd, e_ack_openstore2 result ){
  6401. #if PACKETVER >= 20141022
  6402. PACKET_ZC_ACK_OPENSTORE2 packet{};
  6403. packet.packetType = HEADER_ZC_ACK_OPENSTORE2;
  6404. packet.result = static_cast<decltype(packet.result)>(result);
  6405. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6406. #endif
  6407. }
  6408. /// Shop creation success.
  6409. /// 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)
  6410. void clif_openvending( map_session_data& sd ){
  6411. PACKET_ZC_PC_PURCHASE_MYITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_MYITEMLIST*>( packet_buffer );
  6412. p->packetType = HEADER_ZC_PC_PURCHASE_MYITEMLIST;
  6413. p->packetLength = sizeof( *p );
  6414. p->AID = sd.bl.id;
  6415. for( int i = 0; i < sd.vend_num; i++ ) {
  6416. PACKET_ZC_PC_PURCHASE_MYITEMLIST_sub& entry = p->items[i];
  6417. int16 index = sd.vending[i].index;
  6418. entry.price = sd.vending[i].value;
  6419. entry.index = client_index( index );
  6420. entry.amount = sd.vending[i].amount;
  6421. entry.itemType = itemtype( sd.cart.u.items_cart[index].nameid );
  6422. entry.itemId = client_nameid( sd.cart.u.items_cart[index].nameid );
  6423. entry.identified = sd.cart.u.items_cart[index].identify;
  6424. entry.damaged = sd.cart.u.items_cart[index].attribute;
  6425. entry.refine = sd.cart.u.items_cart[index].refine;
  6426. clif_addcards( &entry.slot, &sd.cart.u.items_cart[index] );
  6427. #if PACKETVER >= 20150226
  6428. clif_add_random_options( entry.option_data, sd.cart.u.items_cart[index] );
  6429. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6430. entry.grade = sd.cart.u.items_cart[index].enchantgrade;
  6431. #endif
  6432. #endif
  6433. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6434. }
  6435. clif_send( p, p->packetLength, &sd.bl, SELF );
  6436. clif_openvending_ack( sd, OPENSTORE2_SUCCESS );
  6437. }
  6438. /// Inform merchant that someone has bought an item.
  6439. /// 0137 <index>.W <amount>.W (ZC_DELETEITEM_FROM_MCSTORE)
  6440. /// 09e5 <index>.W <amount>.W <GID>.L <Date>.L <zeny>.L (ZC_DELETEITEM_FROM_MCSTORE2)
  6441. void clif_vendingreport( map_session_data& sd, uint16 index, uint16 amount, uint32 char_id, int32 zeny ){
  6442. PACKET_ZC_DELETEITEM_FROM_MCSTORE packet{};
  6443. packet.packetType = HEADER_ZC_DELETEITEM_FROM_MCSTORE;
  6444. packet.index = client_index( index );
  6445. packet.amount = amount;
  6446. // TODO : not sure for client date [Napster]
  6447. #if PACKETVER >= 20141016
  6448. packet.buyerCID = char_id;
  6449. packet.date = client_tick( time(nullptr) );
  6450. packet.zeny = zeny;
  6451. #endif
  6452. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6453. }
  6454. /// Result of organizing a party (ZC_ACK_MAKE_GROUP).
  6455. /// 00fa <result>.B
  6456. /// result:
  6457. /// 0 = opens party window and shows MsgStringTable[77]="party successfully organized"
  6458. /// 1 = MsgStringTable[78]="party name already exists"
  6459. /// 2 = MsgStringTable[79]="already in a party"
  6460. /// 3 = cannot organize parties on this map
  6461. /// ? = nothing
  6462. void clif_party_created( map_session_data& sd, int result ){
  6463. PACKET_ZC_ACK_MAKE_GROUP p = {};
  6464. p.PacketType = HEADER_ZC_ACK_MAKE_GROUP;
  6465. p.result = result;
  6466. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6467. }
  6468. /// Adds new member to a party.
  6469. /// 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)
  6470. /// 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)
  6471. /// 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)
  6472. /// role:
  6473. /// 0 = leader
  6474. /// 1 = normal
  6475. /// state:
  6476. /// 0 = connected
  6477. /// 1 = disconnected
  6478. void clif_party_member_info( struct party_data& party, map_session_data& sd ){
  6479. int id = party_getmemberid( &party, &sd );
  6480. if( id < 0 ){
  6481. return;
  6482. }
  6483. PACKET_ZC_ADD_MEMBER_TO_GROUP p = {};
  6484. p.packetType = partymemberinfo;
  6485. p.AID = sd.status.account_id;
  6486. #if PACKETVER >= 20171207
  6487. p.GID = sd.status.char_id;
  6488. #endif
  6489. p.leader = ( party.party.member[id].leader != 0 ) ? 0 : 1;
  6490. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6491. p.class_ = sd.status.class_;
  6492. p.baseLevel = sd.status.base_level;
  6493. #endif
  6494. p.x = sd.bl.x;
  6495. p.y = sd.bl.y;
  6496. p.offline = ( party.party.member[id].online != 0 ) ? 0 : 1;
  6497. safestrncpy( p.partyName, party.party.name, sizeof( p.partyName ) );
  6498. safestrncpy( p.playerName, sd.status.name, sizeof( p.playerName ) );
  6499. mapindex_getmapname_ext( map_mapid2mapname( sd.bl.m ), p.mapName );
  6500. p.sharePickup = ( party.party.item&1 ) ? 1 : 0;
  6501. p.shareLoot = ( party.party.item&2 )? 1 : 0;
  6502. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6503. }
  6504. /// Sends party information (ZC_GROUP_LIST).
  6505. /// 00fb <packet len>.W <party name>.24B { <account id>.L <nick>.24B <map name>.16B <role>.B <state>.B }*
  6506. /// role:
  6507. /// 0 = leader
  6508. /// 1 = normal
  6509. /// state:
  6510. /// 0 = connected
  6511. /// 1 = disconnected
  6512. /// 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
  6513. void clif_party_info( struct party_data& party, map_session_data* sd ){
  6514. send_target target;
  6515. if( sd == nullptr ){
  6516. sd = party_getavailablesd( &party );
  6517. if( sd == nullptr ){
  6518. // Nobody is online to notify
  6519. return;
  6520. }
  6521. // Send to whole party
  6522. target = PARTY;
  6523. }else{
  6524. // Send only to self
  6525. target = SELF;
  6526. }
  6527. PACKET_ZC_GROUP_LIST* p = reinterpret_cast<PACKET_ZC_GROUP_LIST*>( packet_buffer );
  6528. p->packetType = partyinfo;
  6529. p->packetLen = sizeof( *p );
  6530. safestrncpy( p->partyName, party.party.name, sizeof( p->partyName ) );
  6531. for( int i = 0, c = 0; i < MAX_PARTY; i++ ){
  6532. struct party_member& m = party.party.member[i];
  6533. if( m.account_id == 0 ){
  6534. continue;
  6535. }
  6536. PACKET_ZC_GROUP_LIST_SUB& member = p->members[c];
  6537. member.AID = m.account_id;
  6538. #if PACKETVER >= 20171207
  6539. member.GID = m.char_id;
  6540. #endif
  6541. safestrncpy( member.playerName, m.name, sizeof( member.playerName ) );
  6542. mapindex_getmapname_ext( m.map, member.mapName );
  6543. member.leader = ( m.leader ) ? 0 : 1;
  6544. member.offline = ( m.online ) ? 0 : 1;
  6545. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6546. member.class_ = m.class_;
  6547. member.baseLevel = m.lv;
  6548. #endif
  6549. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( member ) );
  6550. c++;
  6551. }
  6552. clif_send( p, p->packetLen, &sd->bl, target );
  6553. }
  6554. /// The player's 'party invite' state, sent during login (ZC_PARTY_CONFIG).
  6555. /// 02c9 <flag>.B
  6556. /// flag:
  6557. /// 0 = allow party invites
  6558. /// 1 = auto-deny party invites
  6559. void clif_partyinvitationstate( map_session_data& sd ){
  6560. #if PACKETVER_MAIN_NUM >= 20070911 || defined(PACKETVER_RE) || PACKETVER_AD_NUM >= 20070911 || PACKETVER_SAK_NUM >= 20070904 || defined(PACKETVER_ZERO)
  6561. PACKET_ZC_PARTY_CONFIG p = {};
  6562. p.packetType = HEADER_ZC_PARTY_CONFIG;
  6563. p.denyPartyInvites = sd.status.disable_partyinvite;
  6564. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6565. #endif
  6566. }
  6567. /// Party invitation request.
  6568. /// 00fe <party id>.L <party name>.24B (ZC_REQ_JOIN_GROUP)
  6569. /// 02c6 <party id>.L <party name>.24B (ZC_PARTY_JOIN_REQ)
  6570. void clif_party_invite( map_session_data& sd, map_session_data& tsd ){
  6571. struct party_data* party = party_search( sd.status.party_id );
  6572. if( party == nullptr ){
  6573. return;
  6574. }
  6575. PACKET_ZC_PARTY_JOIN_REQ p = {};
  6576. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ;
  6577. p.GRID = party->party.party_id;
  6578. safestrncpy( p.groupName, party->party.name, sizeof( p.groupName ) );
  6579. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6580. }
  6581. /// Party invite result.
  6582. /// 00fd <nick>.24S <result>.B (ZC_ACK_REQ_JOIN_GROUP)
  6583. /// 02c5 <nick>.24S <result>.L (ZC_PARTY_JOIN_REQ_ACK)
  6584. /// result=0 : char is already in a party -> MsgStringTable[80]
  6585. /// result=1 : party invite was rejected -> MsgStringTable[81]
  6586. /// result=2 : party invite was accepted -> MsgStringTable[82]
  6587. /// result=3 : party is full -> MsgStringTable[83]
  6588. /// result=4 : char of the same account already joined the party -> MsgStringTable[608]
  6589. /// result=5 : char blocked party invite -> MsgStringTable[1324] (since 20070904)
  6590. /// result=7 : char is not online or doesn't exist -> MsgStringTable[71] (since 20070904)
  6591. /// result=8 : (%s) are currently in restricted map to join a party. -> MsgStringTable[1388] (since 20080527)
  6592. /// result=9 : Cannot join a party in this map. -> MsgStringTable[1871] (since 20110215)
  6593. /// result=10 : You cannot invite or withdraw while in memorial dungeon -> message: MSG_ID_BD3 (since 20161130)
  6594. /// result=11 : The character is a level that can not join the party -> message: MSG_ID_C9A (since 20170412)
  6595. void clif_party_invite_reply( map_session_data& sd, const char* nick, enum e_party_invite_reply reply ){
  6596. #if PACKETVER < 20070904
  6597. if( reply == PARTY_REPLY_OFFLINE ){
  6598. clif_displaymessage( sd.fd, msg_txt( &sd, 3 ) ); // Character not found.
  6599. return;
  6600. }
  6601. #endif
  6602. PACKET_ZC_PARTY_JOIN_REQ_ACK p = {};
  6603. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ_ACK;
  6604. safestrncpy( p.characterName, nick, sizeof( p.characterName ) );
  6605. p.result = reply;
  6606. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6607. }
  6608. /// Updates party settings.
  6609. /// 0101 <exp option>.L (ZC_GROUPINFO_CHANGE)
  6610. /// 07d8 <exp option>.L <item pick rule>.B <item share rule>.B (ZC_REQ_GROUPINFO_CHANGE_V2)
  6611. /// exp option:
  6612. /// 0 = exp sharing disabled
  6613. /// 1 = exp sharing enabled
  6614. /// 2 = cannot change exp sharing
  6615. ///
  6616. /// flag:
  6617. /// 0 = send to party
  6618. /// 1 = send to sd
  6619. void clif_party_option(struct party_data *p,map_session_data *sd,int flag)
  6620. {
  6621. unsigned char buf[16];
  6622. #if PACKETVER < 20090603
  6623. const int cmd = 0x101;
  6624. #else
  6625. const int cmd = 0x7d8;
  6626. #endif
  6627. nullpo_retv(p);
  6628. if(!sd && flag==0){
  6629. int i;
  6630. ARR_FIND(0,MAX_PARTY,i,p->data[i].sd);
  6631. if (i < MAX_PARTY)
  6632. sd = p->data[i].sd;
  6633. }
  6634. if(!sd) return;
  6635. WBUFW(buf,0)=cmd;
  6636. WBUFL(buf,2)=((flag&0x01)?2:p->party.exp);
  6637. #if PACKETVER >= 20090603
  6638. WBUFB(buf,6)=(p->party.item&1)?1:0;
  6639. WBUFB(buf,7)=(p->party.item&2)?1:0;
  6640. #endif
  6641. if(flag==0)
  6642. clif_send(buf,packet_len(cmd),&sd->bl,PARTY);
  6643. else
  6644. clif_send(buf,packet_len(cmd),&sd->bl,SELF);
  6645. }
  6646. /**
  6647. * 0105 <account id>.L <char name>.24B <result>.B (ZC_DELETE_MEMBER_FROM_GROUP).
  6648. * @param sd Send the notification for this player
  6649. * @param account_id Account ID of kicked member
  6650. * @param name Name of kicked member
  6651. * @param result Party leave result @see PARTY_MEMBER_WITHDRAW
  6652. * @param target Send target
  6653. **/
  6654. void clif_party_withdraw( map_session_data& sd, uint32 account_id, const char* name, enum e_party_member_withdraw result, enum send_target target ){
  6655. PACKET_ZC_DELETE_MEMBER_FROM_GROUP p = {};
  6656. p.PacketType = HEADER_ZC_DELETE_MEMBER_FROM_GROUP;
  6657. p.AID = account_id;
  6658. safestrncpy( p.characterName, name, sizeof( p.characterName ) );
  6659. p.result = result;
  6660. clif_send( &p, sizeof( p ), &sd.bl, target );
  6661. }
  6662. /// Party chat message (ZC_NOTIFY_CHAT_PARTY).
  6663. /// 0109 <packet len>.W <account id>.L <message>.?B
  6664. void clif_party_message( struct party_data& party, uint32 account_id, const char* mes, size_t len ){
  6665. map_session_data* sd = party_getavailablesd( &party );
  6666. if( sd == nullptr ){
  6667. return;
  6668. }
  6669. if (len > CHAT_SIZE_MAX) {
  6670. ShowWarning( "clif_party_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", party_id=%d).\n", mes, len, CHAT_SIZE_MAX, party.party.party_id );
  6671. len = CHAT_SIZE_MAX;
  6672. }
  6673. PACKET_ZC_NOTIFY_CHAT_PARTY* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT_PARTY*>( packet_buffer );
  6674. p->PacketType = HEADER_ZC_NOTIFY_CHAT_PARTY;
  6675. p->PacketLength = sizeof( *p );
  6676. p->AID = account_id;
  6677. safestrncpy( p->chatMsg, mes, len );
  6678. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  6679. clif_send( p, p->PacketLength, &sd->bl, PARTY );
  6680. }
  6681. /// Updates the position of a party member on the minimap (ZC_NOTIFY_POSITION_TO_GROUPM).
  6682. /// 0107 <account id>.L <x>.W <y>.W
  6683. void clif_party_xy( map_session_data& sd ){
  6684. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6685. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6686. p.AID = sd.status.account_id;
  6687. p.xPos = sd.bl.x;
  6688. p.yPos = sd.bl.y;
  6689. clif_send( &p, sizeof( p ), &sd.bl, PARTY_SAMEMAP_WOS );
  6690. }
  6691. /*==========================================
  6692. * Sends x/y dot to a single fd. [Skotlex]
  6693. *------------------------------------------*/
  6694. void clif_party_xy_single( map_session_data& sd, map_session_data& tsd ){
  6695. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6696. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6697. p.AID = sd.status.account_id;
  6698. p.xPos = sd.bl.x;
  6699. p.yPos = sd.bl.y;
  6700. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6701. }
  6702. /// Updates HP bar of a party member.
  6703. /// 0106 <account id>.L <hp>.W <max hp>.W (ZC_NOTIFY_HP_TO_GROUPM)
  6704. /// 080e <account id>.L <hp>.L <max hp>.L (ZC_NOTIFY_HP_TO_GROUPM_R2)
  6705. void clif_party_hp( map_session_data& sd ){
  6706. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6707. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6708. p.AID = sd.status.account_id;
  6709. #if PACKETVER < 20100119
  6710. // To correctly display the %hp bar. [Skotlex]
  6711. if( sd.battle_status.max_hp > INT16_MAX ){
  6712. p.hp = sd.battle_status.hp / ( sd.battle_status.max_hp / 100 );
  6713. p.maxhp = 100;
  6714. }else{
  6715. p.hp = sd.battle_status.hp;
  6716. p.maxhp = sd.battle_status.max_hp;
  6717. }
  6718. #else
  6719. p.hp = sd.battle_status.hp;
  6720. p.maxhp = sd.battle_status.max_hp;
  6721. #endif
  6722. clif_send( &p, sizeof( p ), &sd.bl, PARTY_AREA_WOS );
  6723. }
  6724. /// Notifies the party members of a character's death or revival.
  6725. /// 0AB2 <GID>.L <dead>.B
  6726. void clif_party_dead( map_session_data& sd ){
  6727. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6728. PACKET_ZC_GROUP_ISALIVE p = {};
  6729. p.packetType = HEADER_ZC_GROUP_ISALIVE;
  6730. p.AID = sd.status.account_id;
  6731. p.isDead = pc_isdead( &sd );
  6732. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6733. #endif
  6734. }
  6735. /// Updates the job and level of a party member
  6736. /// 0abd <account id>.L <job>.W <level>.W
  6737. void clif_party_job_and_level( map_session_data& sd ){
  6738. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  6739. PACKET_ZC_NOTIFY_MEMBERINFO_TO_GROUPM p = {};
  6740. p.PacketType = HEADER_ZC_NOTIFY_MEMBERINFO_TO_GROUPM;
  6741. p.AID = sd.status.account_id;
  6742. p.job = sd.status.class_;
  6743. p.level = sd.status.base_level;
  6744. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6745. #endif
  6746. }
  6747. /*==========================================
  6748. * Sends HP bar to a single fd. [Skotlex]
  6749. *------------------------------------------*/
  6750. void clif_hpmeter_single( map_session_data& sd, uint32 id, uint32 hp, uint32 maxhp ){
  6751. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6752. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6753. p.AID = id;
  6754. #if PACKETVER < 20100119
  6755. // To correctly display the %hp bar. [Skotlex]
  6756. if( maxhp > INT16_MAX ){
  6757. p.hp = hp / ( maxhp / 100 );
  6758. p.maxhp = 100;
  6759. }else{
  6760. p.hp = hp;
  6761. p.maxhp = maxhp;
  6762. }
  6763. #else
  6764. p.hp = hp;
  6765. p.maxhp = maxhp;
  6766. #endif
  6767. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6768. }
  6769. /// Notifies the client, that it's attack target is too far.
  6770. /// 0139 <target id>.L <target x>.W <target y>.W <x>.W <y>.W <atk range>.W (ZC_ATTACK_FAILURE_FOR_DISTANCE)
  6771. void clif_movetoattack( map_session_data& sd, block_list& bl ){
  6772. PACKET_ZC_ATTACK_FAILURE_FOR_DISTANCE packet{};
  6773. packet.PacketType = HEADER_ZC_ATTACK_FAILURE_FOR_DISTANCE;
  6774. packet.targetAID = bl.id;
  6775. packet.targetXPos = bl.x;
  6776. packet.targetYPos = bl.y;
  6777. packet.xPos = sd.bl.x;
  6778. packet.yPos = sd.bl.y;
  6779. packet.currentAttRange = sd.battle_status.rhw.range;
  6780. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6781. }
  6782. /// Notifies the client about the result of an item produce request (ZC_ACK_REQMAKINGITEM).
  6783. /// 018f <result>.W <name id>.W
  6784. /// result:
  6785. /// 0 = success
  6786. /// 1 = failure
  6787. /// 2 = success (alchemist)
  6788. /// 3 = failure (alchemist)
  6789. void clif_produceeffect(map_session_data* sd,int flag, t_itemid nameid){
  6790. nullpo_retv( sd );
  6791. clif_solved_charname( sd->fd, sd->status.char_id, sd->status.name );
  6792. PACKET_ZC_ACK_REQMAKINGITEM p = {};
  6793. p.packetType = HEADER_ZC_ACK_REQMAKINGITEM;
  6794. p.result = flag;
  6795. p.itemId = client_nameid( nameid );
  6796. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6797. }
  6798. /// Initiates the pet taming process.
  6799. /// 019e (ZC_START_CAPTURE)
  6800. void clif_catch_process( map_session_data& sd ){
  6801. PACKET_ZC_START_CAPTURE packet{};
  6802. packet.PacketType = HEADER_ZC_START_CAPTURE;
  6803. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6804. }
  6805. /// Displays the result of a pet taming attempt.
  6806. /// 01a0 <result>.B (ZC_TRYCAPTURE_MONSTER)
  6807. /// 0 = failure
  6808. /// 1 = success
  6809. void clif_pet_roulette( map_session_data& sd, bool success ){
  6810. PACKET_ZC_TRYCAPTURE_MONSTER packet{};
  6811. packet.PacketType = HEADER_ZC_TRYCAPTURE_MONSTER;
  6812. packet.result = success;
  6813. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6814. }
  6815. /// Presents a list of pet eggs that can be hatched (ZC_PETEGG_LIST).
  6816. /// 01a6 <packet len>.W { <index>.W }*
  6817. void clif_sendegg(map_session_data *sd)
  6818. {
  6819. int i,n=0,fd;
  6820. nullpo_retv(sd);
  6821. fd=sd->fd;
  6822. if (battle_config.pet_no_gvg && map_flag_gvg2(sd->bl.m)) { //Disable pet hatching in GvG grounds during Guild Wars [Skotlex]
  6823. clif_displaymessage(fd, msg_txt(sd,666));
  6824. return;
  6825. }
  6826. WFIFOHEAD(fd, MAX_INVENTORY * 2 + 4);
  6827. WFIFOW(fd,0)=0x1a6;
  6828. for(i=0,n=0;i<MAX_INVENTORY;i++){
  6829. if(sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr ||
  6830. sd->inventory_data[i]->type != IT_PETEGG ||
  6831. sd->inventory.u.items_inventory[i].amount <= 0)
  6832. continue;
  6833. WFIFOW(fd,n*2+4)=client_index(i);
  6834. n++;
  6835. }
  6836. WFIFOW(fd,2)=4+n*2;
  6837. WFIFOSET(fd,WFIFOW(fd,2));
  6838. sd->menuskill_id = SA_TAMINGMONSTER;
  6839. sd->menuskill_val = -1;
  6840. }
  6841. /// Sends a specific pet data update (ZC_CHANGESTATE_PET).
  6842. /// 01a4 <type>.B <id>.L <data>.L
  6843. /// type:
  6844. /// 0 = pre-init (data = 0)
  6845. /// 1 = intimacy (data = 0~4)
  6846. /// 2 = hunger (data = 0~4)
  6847. /// 3 = accessory
  6848. /// 4 = performance (data = 1~3: normal, 4: special)
  6849. /// 5 = hairstyle
  6850. /// 6 = close egg selection ui and update egg in inventory (PACKETVER >= 20180704)
  6851. ///
  6852. /// If sd is null, the update is sent to nearby objects, otherwise it is sent only to that player.
  6853. void clif_send_petdata(map_session_data* sd, struct pet_data* pd, int type, int param)
  6854. {
  6855. uint8 buf[16];
  6856. nullpo_retv(pd);
  6857. WBUFW(buf,0) = 0x1a4;
  6858. WBUFB(buf,2) = type;
  6859. WBUFL(buf,3) = pd->bl.id;
  6860. WBUFL(buf,7) = param;
  6861. if (sd)
  6862. clif_send(buf, packet_len(0x1a4), &sd->bl, SELF);
  6863. else
  6864. clif_send(buf, packet_len(0x1a4), &pd->bl, AREA);
  6865. }
  6866. /// Pet's base data (ZC_PROPERTY_PET).
  6867. /// 01a2 <name>.24B <renamed>.B <level>.W <hunger>.W <intimacy>.W <accessory id>.W <class>.W
  6868. void clif_send_petstatus(map_session_data *sd)
  6869. {
  6870. int fd;
  6871. struct s_pet *pet;
  6872. nullpo_retv(sd);
  6873. nullpo_retv(sd->pd);
  6874. fd=sd->fd;
  6875. pet = &sd->pd->pet;
  6876. WFIFOHEAD(fd,packet_len(0x1a2));
  6877. WFIFOW(fd,0)=0x1a2;
  6878. safestrncpy(WFIFOCP(fd,2),pet->name,NAME_LENGTH);
  6879. WFIFOB(fd,26)=battle_config.pet_rename?0:pet->rename_flag;
  6880. WFIFOW(fd,27)=pet->level;
  6881. WFIFOW(fd,29)=pet->hungry;
  6882. WFIFOW(fd,31)=pet->intimate;
  6883. WFIFOW(fd,33)=pet->equip;
  6884. #if PACKETVER >= 20081126
  6885. WFIFOW(fd,35)=pet->class_;
  6886. #endif
  6887. WFIFOSET(fd,packet_len(0x1a2));
  6888. }
  6889. /// Notification about a pet's emotion/talk (ZC_PET_ACT).
  6890. /// 01aa <id>.L <data>.L
  6891. /// data:
  6892. /// @see CZ_PET_ACT.
  6893. void clif_pet_emotion(struct pet_data *pd,int param)
  6894. {
  6895. unsigned char buf[16];
  6896. nullpo_retv(pd);
  6897. memset(buf,0,packet_len(0x1aa));
  6898. WBUFW(buf,0)=0x1aa;
  6899. WBUFL(buf,2)=pd->bl.id;
  6900. WBUFL(buf,6)=param;
  6901. clif_send(buf,packet_len(0x1aa),&pd->bl,AREA);
  6902. }
  6903. /// Result of request to feed a pet.
  6904. /// 01a3 <result>.B <name id>.W (ZC_FEED_PET)
  6905. /// result:
  6906. /// 0 = failure
  6907. /// 1 = success
  6908. void clif_pet_food( map_session_data& sd, int32 foodid, bool success ){
  6909. PACKET_ZC_FEED_PET packet{};
  6910. packet.packetType = HEADER_ZC_FEED_PET;
  6911. packet.result = success;
  6912. packet.itemId = client_nameid( foodid );
  6913. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6914. }
  6915. /// Send pet auto feed info.
  6916. void clif_pet_autofeed_status(map_session_data* sd, bool force) {
  6917. #if PACKETVER >= 20141008
  6918. nullpo_retv(sd);
  6919. if (force || battle_config.pet_autofeed_always) {
  6920. // Always send ON or OFF
  6921. if (sd->pd && battle_config.feature_pet_autofeed) {
  6922. clif_configuration(sd, CONFIG_PET_AUTOFEED, sd->pd->pet.autofeed);
  6923. }
  6924. else {
  6925. clif_configuration(sd, CONFIG_PET_AUTOFEED, false);
  6926. }
  6927. }
  6928. else {
  6929. // Only send when enabled
  6930. if (sd->pd && battle_config.feature_pet_autofeed && sd->pd->pet.autofeed) {
  6931. clif_configuration(sd, CONFIG_PET_AUTOFEED, true);
  6932. }
  6933. }
  6934. #endif
  6935. }
  6936. /// Presents a list of skills that can be auto-spelled.
  6937. /// 01cd { <skill id>.L }*7 (ZC_AUTOSPELLLIST)
  6938. void clif_autospell( map_session_data& sd, uint16 skill_lv ){
  6939. struct s_autospell_requirement{
  6940. uint16 skill_id;
  6941. uint16 required_autospell_skill_lv;
  6942. };
  6943. #ifdef RENEWAL
  6944. const std::vector<s_autospell_requirement> autospell_skills = {
  6945. { MG_FIREBOLT, 0 },
  6946. { MG_COLDBOLT, 0 },
  6947. { MG_LIGHTNINGBOLT, 0 },
  6948. { MG_SOULSTRIKE, 3 },
  6949. { MG_FIREBALL, 3 },
  6950. { WZ_EARTHSPIKE, 6 },
  6951. { MG_FROSTDIVER, 6 },
  6952. { MG_THUNDERSTORM, 9 },
  6953. { WZ_HEAVENDRIVE, 9 }
  6954. };
  6955. #else
  6956. const std::vector<s_autospell_requirement> autospell_skills = {
  6957. { MG_NAPALMBEAT, 0 },
  6958. { MG_COLDBOLT, 1 },
  6959. { MG_FIREBOLT, 1 },
  6960. { MG_LIGHTNINGBOLT, 1 },
  6961. { MG_SOULSTRIKE, 4 },
  6962. { MG_FIREBALL, 7 },
  6963. { MG_FROSTDIVER, 9 },
  6964. };
  6965. #endif
  6966. #if PACKETVER_MAIN_NUM >= 20181128 || PACKETVER_RE_NUM >= 20181031
  6967. PACKET_ZC_AUTOSPELLLIST* p = reinterpret_cast<PACKET_ZC_AUTOSPELLLIST*>( packet_buffer );
  6968. p->packetType = HEADER_ZC_AUTOSPELLLIST;
  6969. p->packetLength = sizeof( *p );
  6970. size_t count = 0;
  6971. for( const s_autospell_requirement& requirement : autospell_skills ){
  6972. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  6973. p->skills[count++] = requirement.skill_id;
  6974. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skills[0] ) );
  6975. }
  6976. }
  6977. clif_send( p, p->packetLength, &sd.bl, SELF );
  6978. #elif PACKETVER_MAIN_NUM >= 20090406 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_SAK_NUM >= 20080618
  6979. PACKET_ZC_AUTOSPELLLIST p = {};
  6980. p.packetType = HEADER_ZC_AUTOSPELLLIST;
  6981. size_t count = 0;
  6982. for( const s_autospell_requirement& requirement : autospell_skills ){
  6983. if( count == ARRAYLENGTH( p.skills ) ){
  6984. break;
  6985. }
  6986. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  6987. p.skills[count++] = requirement.skill_id;
  6988. }else{
  6989. p.skills[count++] = 0;
  6990. }
  6991. }
  6992. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6993. #endif
  6994. sd.menuskill_id = SA_AUTOSPELL;
  6995. sd.menuskill_val = skill_lv;
  6996. }
  6997. /// Devotion's visual effect (ZC_DEVOTIONLIST).
  6998. /// 01cf <devoter id>.L { <devotee id>.L }*5 <max distance>.W
  6999. void clif_devotion(struct block_list *src, map_session_data *tsd)
  7000. {
  7001. unsigned char buf[56];
  7002. nullpo_retv(src);
  7003. memset(buf,0,packet_len(0x1cf));
  7004. WBUFW(buf,0) = 0x1cf;
  7005. WBUFL(buf,2) = src->id;
  7006. if( src->type == BL_MER )
  7007. {
  7008. s_mercenary_data *md = BL_CAST(BL_MER,src);
  7009. if( md && md->master && md->devotion_flag )
  7010. WBUFL(buf,6) = md->master->bl.id;
  7011. WBUFW(buf,26) = skill_get_range2(src, ML_DEVOTION, mercenary_checkskill(md, ML_DEVOTION), false);
  7012. }
  7013. else
  7014. {
  7015. int i;
  7016. map_session_data *sd = BL_CAST(BL_PC,src);
  7017. if( sd == nullptr )
  7018. return;
  7019. for( i = 0; i < 5 /*MAX_DEVOTION*/; i++ ) // Client only able show to 5 links
  7020. WBUFL(buf,6+4*i) = sd->devotion[i];
  7021. WBUFW(buf,26) = skill_get_range2(src, CR_DEVOTION, pc_checkskill(sd, CR_DEVOTION), false);
  7022. }
  7023. if( tsd )
  7024. clif_send(buf, packet_len(0x1cf), &tsd->bl, SELF);
  7025. else
  7026. clif_send(buf, packet_len(0x1cf), src, AREA);
  7027. }
  7028. /// Notifies the client of an object's spirits.
  7029. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  7030. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  7031. void clif_spiritball( struct block_list *bl, struct block_list* target, enum send_target send_target ){
  7032. nullpo_retv( bl );
  7033. PACKET_ZC_SPIRITS p = {};
  7034. p.PacketType = HEADER_ZC_SPIRITS;
  7035. p.AID = bl->id;
  7036. switch( bl->type ){
  7037. case BL_PC:
  7038. p.num = ( (map_session_data*)bl )->spiritball;
  7039. break;
  7040. case BL_HOM:
  7041. p.num = ( (struct homun_data*)bl )->homunculus.spiritball;
  7042. break;
  7043. }
  7044. clif_send( &p, sizeof( p ), target == nullptr ? bl : target, send_target );
  7045. }
  7046. /// Notifies clients in area of a character's combo delay (ZC_COMBODELAY).
  7047. /// 01d2 <account id>.L <delay>.L
  7048. void clif_combo_delay(struct block_list *bl,t_tick wait)
  7049. {
  7050. unsigned char buf[32];
  7051. nullpo_retv(bl);
  7052. WBUFW(buf,0)=0x1d2;
  7053. WBUFL(buf,2)=bl->id;
  7054. WBUFL(buf,6)=client_tick(wait);
  7055. clif_send(buf,packet_len(0x1d2),bl,AREA);
  7056. }
  7057. /// Notifies clients in area that a character has blade-stopped another (ZC_BLADESTOP).
  7058. /// 01d1 <src id>.L <dst id>.L <flag>.L
  7059. /// flag:
  7060. /// 0 = inactive
  7061. /// 1 = active
  7062. void clif_bladestop(struct block_list *src, int dst_id, int active)
  7063. {
  7064. unsigned char buf[32];
  7065. nullpo_retv(src);
  7066. WBUFW(buf,0)=0x1d1;
  7067. WBUFL(buf,2)=src->id;
  7068. WBUFL(buf,6)=dst_id;
  7069. WBUFL(buf,10)=active;
  7070. clif_send(buf,packet_len(0x1d1),src,AREA);
  7071. }
  7072. /// MVP effect (ZC_MVP).
  7073. /// 010c <account id>.L
  7074. void clif_mvp_effect(map_session_data *sd)
  7075. {
  7076. unsigned char buf[16];
  7077. nullpo_retv(sd);
  7078. WBUFW(buf,0)=0x10c;
  7079. WBUFL(buf,2)=sd->bl.id;
  7080. clif_send(buf,packet_len(0x10c),&sd->bl,AREA);
  7081. }
  7082. /// MVP item reward message (ZC_MVP_GETTING_ITEM).
  7083. /// 010a <name id>.W
  7084. void clif_mvp_item( map_session_data *sd, t_itemid nameid ){
  7085. PACKET_ZC_MVP_GETTING_ITEM p = {};
  7086. p.packetType = HEADER_ZC_MVP_GETTING_ITEM;
  7087. p.itemId = client_nameid( nameid );
  7088. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  7089. }
  7090. /// MVP EXP reward message (ZC_MVP_GETTING_SPECIAL_EXP).
  7091. /// 010b <exp>.L
  7092. void clif_mvp_exp(map_session_data *sd, t_exp exp) {
  7093. #if PACKETVER >= 20131223 // Kro remove this packet [Napster]
  7094. if (battle_config.mvp_exp_reward_message) {
  7095. char e_msg[CHAT_SIZE_MAX];
  7096. sprintf(e_msg, msg_txt(sd, 717), exp);
  7097. clif_messagecolor(&sd->bl, color_table[COLOR_CYAN], e_msg, false, SELF); // Congratulations! You are the MVP! Your reward EXP Points are %u !!
  7098. }
  7099. #else
  7100. int fd;
  7101. nullpo_retv(sd);
  7102. fd = sd->fd;
  7103. WFIFOHEAD(fd, packet_len(0x10b));
  7104. WFIFOW(fd,0) = 0x10b;
  7105. WFIFOL(fd,2) = (uint32)u64min( exp, MAX_EXP );
  7106. WFIFOSET(fd, packet_len(0x10b));
  7107. #endif
  7108. }
  7109. /// Dropped MVP item reward message (ZC_THROW_MVPITEM).
  7110. /// 010d
  7111. ///
  7112. /// "You are the MVP, but cannot obtain the reward because
  7113. /// you are overweight."
  7114. void clif_mvp_noitem(map_session_data* sd)
  7115. {
  7116. int fd = sd->fd;
  7117. WFIFOHEAD(fd,packet_len(0x10d));
  7118. WFIFOW(fd,0) = 0x10d;
  7119. WFIFOSET(fd,packet_len(0x10d));
  7120. }
  7121. /// Guild creation result.
  7122. /// 0167 <result>.B (ZC_RESULT_MAKE_GUILD)
  7123. /// result:
  7124. /// 0 = "Guild has been created."
  7125. /// 1 = "You are already in a Guild."
  7126. /// 2 = "That Guild Name already exists."
  7127. /// 3 = "You need the neccessary item to create a Guild."
  7128. void clif_guild_created( map_session_data& sd, int flag ){
  7129. PACKET_ZC_RESULT_MAKE_GUILD p = {};
  7130. p.packetType = HEADER_ZC_RESULT_MAKE_GUILD;
  7131. p.result = static_cast<decltype(p.result)>( flag );
  7132. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7133. }
  7134. /// Notifies the client that it is belonging to a guild (ZC_UPDATE_GDID).
  7135. /// 016c <guild id>.L <emblem id>.L <mode>.L <ismaster>.B <inter sid>.L <guild name>.24B
  7136. /// mode:
  7137. /// &0x01 = allow invite
  7138. /// &0x10 = allow expel
  7139. void clif_guild_belonginfo( map_session_data& sd ){
  7140. if( sd.guild == nullptr ){
  7141. return;
  7142. }
  7143. const auto &guild = sd.guild->guild;
  7144. int position = guild_getposition(sd);
  7145. if( position < 0 ){
  7146. return;
  7147. }
  7148. PACKET_ZC_UPDATE_GDID p = {};
  7149. p.PacketType = HEADER_ZC_UPDATE_GDID;
  7150. p.guildId = guild.guild_id;
  7151. p.emblemVersion = guild.emblem_id;
  7152. p.mode = guild.position[position].mode;
  7153. p.isMaster = ( sd.state.gmaster_flag == 1 );
  7154. p.interSid = 0; // InterSID (unknown purpose)
  7155. safestrncpy( p.guildName, guild.name, sizeof( p.guildName ) );
  7156. #if PACKETVER_MAIN_NUM >= 20220216
  7157. p.masterGID = guild.member[0].char_id;
  7158. #endif
  7159. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7160. }
  7161. /// Guild member login notice.
  7162. /// 016d <account id>.L <char id>.L <status>.L (ZC_UPDATE_CHARSTAT)
  7163. /// 01f2 <account id>.L <char id>.L <status>.L <gender>.W <hair style>.W <hair color>.W (ZC_UPDATE_CHARSTAT2)
  7164. /// status:
  7165. /// 0 = offline
  7166. /// 1 = online
  7167. void clif_guild_memberlogin_notice(const struct mmo_guild &g,int idx,int flag)
  7168. {
  7169. unsigned char buf[64];
  7170. map_session_data* sd;
  7171. WBUFW(buf, 0)=0x1f2;
  7172. WBUFL(buf, 2)=g.member[idx].account_id;
  7173. WBUFL(buf, 6)=g.member[idx].char_id;
  7174. WBUFL(buf,10)=flag;
  7175. if( ( sd = g.member[idx].sd ) != nullptr )
  7176. {
  7177. WBUFW(buf,14) = sd->status.sex;
  7178. WBUFW(buf,16) = sd->status.hair;
  7179. WBUFW(buf,18) = sd->status.hair_color;
  7180. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD_WOS);
  7181. }
  7182. else if( ( sd = guild_getavailablesd(g) ) != nullptr )
  7183. {
  7184. WBUFW(buf,14) = 0;
  7185. WBUFW(buf,16) = 0;
  7186. WBUFW(buf,18) = 0;
  7187. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD);
  7188. }
  7189. }
  7190. // Function `clif_guild_memberlogin_notice` sends info about
  7191. // logins and logouts of a guild member to the rest members.
  7192. // But at the 1st time (after a player login or map changing)
  7193. // the client won't show the message.
  7194. // So I suggest use this function for sending "first-time-info"
  7195. // to some player on entering the game or changing location.
  7196. // At next time the client would always show the message.
  7197. // The function sends all the statuses in the single packet
  7198. // to economize traffic. [LuzZza]
  7199. void clif_guild_send_onlineinfo(map_session_data *sd)
  7200. {
  7201. unsigned char buf[14*128];
  7202. int i, count=0, p_len;
  7203. nullpo_retv(sd);
  7204. p_len = packet_len(0x16d);
  7205. auto &g = sd->guild;
  7206. if (!g)
  7207. return;
  7208. for(i=0; i<g->guild.max_member; i++) {
  7209. if(g->guild.member[i].account_id > 0 &&
  7210. g->guild.member[i].account_id != sd->status.account_id) {
  7211. WBUFW(buf,count*p_len) = 0x16d;
  7212. WBUFL(buf,count*p_len+2) = g->guild.member[i].account_id;
  7213. WBUFL(buf,count*p_len+6) = g->guild.member[i].char_id;
  7214. WBUFL(buf,count*p_len+10) = g->guild.member[i].online;
  7215. count++;
  7216. }
  7217. }
  7218. clif_send(buf, p_len*count, &sd->bl, SELF);
  7219. }
  7220. /// Bitmask of enabled guild window tabs
  7221. /// 014e <menu flag>.L (ZC_ACK_GUILD_MENUINTERFACE)
  7222. /// menu flag:
  7223. /// 0x00 = Basic Info (always on)
  7224. /// &0x01 = Member manager
  7225. /// &0x02 = Positions
  7226. /// &0x04 = Skills
  7227. /// &0x10 = Expulsion list
  7228. /// &0x40 = Unknown (GMENUFLAG_ALLGUILDLIST)
  7229. /// &0x80 = Notice
  7230. void clif_guild_masterormember(map_session_data& sd){
  7231. PACKET_ZC_ACK_GUILD_MENUINTERFACE p{};
  7232. p.packetType = HEADER_ZC_ACK_GUILD_MENUINTERFACE;
  7233. if(sd.state.gmaster_flag){
  7234. p.menuFlag = 0xd7;
  7235. }else{
  7236. p.menuFlag = 0x57;
  7237. }
  7238. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7239. }
  7240. /// Guild basic information (Territories [Valaris])
  7241. /// 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)
  7242. /// 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)
  7243. /// 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)
  7244. void clif_guild_basicinfo( map_session_data& sd ){
  7245. if( sd.guild == nullptr ){
  7246. return;
  7247. }
  7248. const auto &guild = sd.guild->guild;
  7249. PACKET_ZC_GUILD_INFO p = {};
  7250. p.PacketType = HEADER_ZC_GUILD_INFO;
  7251. p.GDID = guild.guild_id;
  7252. p.level = guild.guild_lv;
  7253. p.userNum = guild.connect_member;
  7254. p.maxUserNum = guild.max_member;
  7255. p.userAverageLevel = guild.average_lv;
  7256. p.exp = (uint32)cap_value( guild.exp, 0, MAX_GUILD_EXP );
  7257. p.maxExp = (uint32)cap_value( guild.next_exp, 0, MAX_GUILD_EXP );
  7258. p.point = 0; // Tax Points
  7259. p.honor = 0; // Honor: (left) Vulgar [-100,100] Famed (right)
  7260. p.virtue = 0; // Virtue: (down) Wicked [-100,100] Righteous (up)
  7261. p.emblemVersion = guild.emblem_id;
  7262. safestrncpy( p.guildname, guild.name, sizeof( p.guildname ) );
  7263. safestrncpy( p.manageLand, msg_txt( &sd, 300 + guild_checkcastles( guild ) ), sizeof( p.manageLand ) );
  7264. p.zeny = 0;
  7265. #if PACKETVER >= 20200902
  7266. p.masterGID = guild.member[0].char_id; // leader
  7267. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7268. #elif PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7269. p.masterGID = guild.member[0].char_id; // leader
  7270. #else
  7271. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7272. #endif
  7273. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7274. }
  7275. /// Guild alliance and opposition list
  7276. /// 014c <packet len>.W { <relation>.L <guild id>.L <guild name>.24B }* (ZC_MYGUILD_BASIC_INFO)
  7277. void clif_guild_allianceinfo(map_session_data& sd){
  7278. auto &g = sd.guild;
  7279. if (g == nullptr){
  7280. return;
  7281. }
  7282. PACKET_ZC_MYGUILD_BASIC_INFO* p = reinterpret_cast<PACKET_ZC_MYGUILD_BASIC_INFO*>( packet_buffer );
  7283. p->PacketType = HEADER_ZC_MYGUILD_BASIC_INFO;
  7284. p->PacketLength = sizeof(*p);
  7285. for(size_t i=0, c = 0;i<MAX_GUILDALLIANCE;i++){
  7286. guild_alliance &a = g->guild.alliance[i];
  7287. if(a.guild_id<=0){
  7288. continue;
  7289. }
  7290. RELATED_GUILD_INFO& info = p->rgInfo[c];
  7291. info.relation = a.opposition;
  7292. info.GDID = a.guild_id;
  7293. safestrncpy(info.guildname,a.name,sizeof(info.guildname));
  7294. p->PacketLength += static_cast<decltype(p->PacketLength)>(sizeof(info));
  7295. c++;
  7296. }
  7297. clif_send(p,p->PacketLength,&sd.bl,SELF);
  7298. }
  7299. /// Guild member manager information
  7300. /// 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)
  7301. /// state:
  7302. /// 0 = offline
  7303. /// 1 = online
  7304. /// memo:
  7305. /// probably member's self-introduction (unused, no client UI/packets for editing it)
  7306. /// 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)
  7307. void clif_guild_memberlist( map_session_data& sd ){
  7308. if( sd.guild == nullptr ){
  7309. return;
  7310. }
  7311. const auto &guild = sd.guild->guild;
  7312. PACKET_ZC_MEMBERMGR_INFO* p = reinterpret_cast<PACKET_ZC_MEMBERMGR_INFO*>( packet_buffer );
  7313. p->PacketType = HEADER_ZC_MEMBERMGR_INFO;
  7314. p->packetLength = sizeof( *p );
  7315. for( int i = 0, c = 0; i < guild.max_member; i++ ){
  7316. const auto &member = guild.member[i];
  7317. if( member.account_id == 0 ){
  7318. continue;
  7319. }
  7320. GUILD_MEMBER_INFO& member_info = p->guildMemberInfo[c];
  7321. member_info.AID = member.account_id;
  7322. member_info.GID = member.char_id;
  7323. member_info.head = member.hair;
  7324. member_info.headPalette = member.hair_color;
  7325. member_info.sex = member.gender;
  7326. member_info.job = member.class_;
  7327. member_info.level = member.lv;
  7328. member_info.contributionExp = (uint32)cap_value( member.exp, 0, MAX_GUILD_EXP );
  7329. member_info.currentState = member.online;
  7330. member_info.positionID = member.position;
  7331. #if PACKETVER >= 20200902
  7332. member_info.lastLoginTime = member.last_login;
  7333. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7334. #elif PACKETVER_MAIN_NUM >= 20161214 || PACKETVER_RE_NUM >= 20161130 || defined(PACKETVER_ZERO)
  7335. member_info.lastLoginTime = member.last_login;
  7336. #else
  7337. 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.
  7338. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7339. #endif
  7340. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( member_info ) );
  7341. c++;
  7342. }
  7343. clif_send( p, p->packetLength, &sd.bl, SELF );
  7344. }
  7345. /// Guild position name information (ZC_POSITION_ID_NAME_INFO).
  7346. /// 0166 <packet len>.W { <position id>.L <position name>.24B }*
  7347. void clif_guild_positionnamelist(map_session_data *sd)
  7348. {
  7349. int i,fd;
  7350. nullpo_retv(sd);
  7351. auto &g = sd->guild;
  7352. if (!g)
  7353. return;
  7354. fd = sd->fd;
  7355. WFIFOHEAD(fd, MAX_GUILDPOSITION * 28 + 4);
  7356. WFIFOW(fd, 0)=0x166;
  7357. for(i=0;i<MAX_GUILDPOSITION;i++){
  7358. WFIFOL(fd,i*28+4)=i;
  7359. safestrncpy(WFIFOCP(fd,i*28+8),g->guild.position[i].name,NAME_LENGTH);
  7360. }
  7361. WFIFOW(fd,2)=i*28+4;
  7362. WFIFOSET(fd,WFIFOW(fd,2));
  7363. }
  7364. /// Guild position information
  7365. /// 0160 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L }* (ZC_POSITION_INFO)
  7366. /// mode:
  7367. /// See enum e_guild_permission
  7368. /// ranking:
  7369. /// TODO
  7370. static void clif_guild_positioninfolist(map_session_data& sd){
  7371. auto &g = sd.guild;
  7372. if (g == nullptr){
  7373. return;
  7374. }
  7375. PACKET_ZC_POSITION_INFO* p = reinterpret_cast<PACKET_ZC_POSITION_INFO*>( packet_buffer );
  7376. p->PacketType = HEADER_ZC_POSITION_INFO;
  7377. p->PacketLength = sizeof(*p);
  7378. for(size_t i=0;i<MAX_GUILDPOSITION;i++){
  7379. guild_position& gp = g->guild.position[i];
  7380. p->posInfo[i].positionID = i;
  7381. p->posInfo[i].right = gp.mode;
  7382. p->posInfo[i].ranking = i;
  7383. p->posInfo[i].payRate = gp.exp_mode;
  7384. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->posInfo[0] ) );
  7385. }
  7386. clif_send(p,p->PacketLength,&sd.bl,SELF);
  7387. }
  7388. /// Notifies clients in a guild about updated position information.
  7389. /// 0174 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <position name>.24B }* (ZC_ACK_CHANGE_GUILD_POSITIONINFO)
  7390. /// mode:
  7391. /// &0x01 = allow invite
  7392. /// &0x10 = allow expel
  7393. /// ranking:
  7394. /// TODO
  7395. void clif_guild_positionchanged(const struct mmo_guild &g,int idx)
  7396. {
  7397. // FIXME: This packet is intended to update the clients after a
  7398. // commit of position info changes, not sending one packet per
  7399. // position.
  7400. map_session_data *sd;
  7401. unsigned char buf[128];
  7402. WBUFW(buf, 0)=0x174;
  7403. WBUFW(buf, 2)=44; // packet len
  7404. // GUILD_REG_POSITION_INFO{
  7405. WBUFL(buf, 4)=idx;
  7406. WBUFL(buf, 8)=g.position[idx].mode;
  7407. WBUFL(buf,12)=idx;
  7408. WBUFL(buf,16)=g.position[idx].exp_mode;
  7409. safestrncpy(WBUFCP(buf,20),g.position[idx].name,NAME_LENGTH);
  7410. // }*
  7411. if( (sd=guild_getavailablesd(g))!=nullptr )
  7412. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7413. }
  7414. /// Notifies clients in a guild about updated member position assignments.
  7415. /// 0156 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (ZC_ACK_REQ_CHANGE_MEMBERS)
  7416. void clif_guild_memberpositionchanged(const struct mmo_guild &g, int idx)
  7417. {
  7418. // FIXME: This packet is intended to update the clients after a
  7419. // commit of member position assignment changes, not sending one
  7420. // packet per position.
  7421. map_session_data *sd;
  7422. unsigned char buf[64];
  7423. WBUFW(buf, 0)=0x156;
  7424. WBUFW(buf, 2)=16; // packet len
  7425. // MEMBER_POSITION_INFO{
  7426. WBUFL(buf, 4)=g.member[idx].account_id;
  7427. WBUFL(buf, 8)=g.member[idx].char_id;
  7428. WBUFL(buf,12)=g.member[idx].position;
  7429. // }*
  7430. if( (sd=guild_getavailablesd(g))!=nullptr )
  7431. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7432. }
  7433. /// Sends emblems bitmap data to the client that requested it.
  7434. /// 0152 <packet len>.W <guild id>.L <emblem id>.L <emblem data>.?B (ZC_GUILD_EMBLEM_IMG)
  7435. void clif_guild_emblem(const map_session_data &sd, const struct mmo_guild &g)
  7436. {
  7437. int fd = sd.fd;
  7438. if( g.emblem_len <= 0 )
  7439. return;
  7440. WFIFOHEAD(fd,g.emblem_len+12);
  7441. WFIFOW(fd,0)=0x152;
  7442. WFIFOW(fd,2)=g.emblem_len+12;
  7443. WFIFOL(fd,4)=g.guild_id;
  7444. WFIFOL(fd,8)=g.emblem_id;
  7445. memcpy(WFIFOP(fd,12),g.emblem_data,g.emblem_len);
  7446. WFIFOSET(fd,WFIFOW(fd,2));
  7447. }
  7448. /// Sends update of the guild id/emblem id to everyone in the area.
  7449. /// 01b4 <id>.L <guild id>.L <emblem id>.W (ZC_CHANGE_GUILD)
  7450. /// 0b1f <guild id>.L <version>.L <AID>.L (ZC_NEW_EMBLEM_DOWNLOAD)
  7451. /// 0b47 <guild id>.L <version>.L <AID>.L (ZC_ACK_ADD_NEW_EMBLEM)
  7452. void clif_guild_emblem_area(struct block_list* bl)
  7453. {
  7454. // TODO this packet doesn't force the update of ui components that have the emblem visible
  7455. // (emblem in the flag npcs and emblem over the head in agit maps) [FlavioJS]
  7456. PACKET_ZC_CHANGE_GUILD p{};
  7457. p.packetType = HEADER_ZC_CHANGE_GUILD;
  7458. p.guild_id = status_get_guild_id(bl);
  7459. p.emblem_id = status_get_emblem_id(bl);
  7460. p.AID = bl->id;
  7461. clif_send(&p, sizeof(p), bl, AREA_WOS);
  7462. }
  7463. /// Sends guild skills.
  7464. /// 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)
  7465. void clif_guild_skillinfo( map_session_data& sd ){
  7466. auto& g = sd.guild;
  7467. if( g == nullptr ){
  7468. return;
  7469. }
  7470. PACKET_ZC_GUILD_SKILLINFO* p = reinterpret_cast<PACKET_ZC_GUILD_SKILLINFO*>( packet_buffer );
  7471. p->PacketType = HEADER_ZC_GUILD_SKILLINFO;
  7472. p->PacketLength = sizeof( *p );
  7473. p->skillPoint = g->guild.skill_point;
  7474. for( size_t i = 0, c = 0; i < MAX_GUILDSKILL; i++ ){
  7475. if( g->guild.skill[i].id <= 0 ){
  7476. continue;
  7477. }
  7478. if( !guild_check_skill_require( g->guild, g->guild.skill[i].id ) ){
  7479. continue;
  7480. }
  7481. GUILD_SKILLDATA& gs = p->skillInfo[c];
  7482. int skill_id = g->guild.skill[i].id;
  7483. gs.id = skill_id;
  7484. gs.inf = skill_get_inf(skill_id);
  7485. gs.level = g->guild.skill[i].lv;
  7486. if( g->guild.skill[i].lv > 0 ){
  7487. gs.sp = skill_get_sp( skill_id, g->guild.skill[i].lv );
  7488. gs.range2 = skill_get_range( skill_id, g->guild.skill[i].lv );
  7489. }else{
  7490. gs.sp = 0;
  7491. gs.range2 = 0;
  7492. }
  7493. safestrncpy( gs.name, skill_get_name( skill_id ), sizeof( gs.name ) );
  7494. gs.upFlag = ( g->guild.skill[i].lv < guild_skill_get_max( skill_id ) && &sd == g->guild.member[0].sd ) ? 1 : 0;
  7495. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( gs ) );
  7496. c++;
  7497. }
  7498. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7499. }
  7500. /// Sends guild notice to client.
  7501. /// 016f <subject>.60B <notice>.120B (ZC_GUILD_NOTICE)
  7502. void clif_guild_notice( map_session_data& sd ){
  7503. auto& g = sd.guild;
  7504. if( g == nullptr ){
  7505. return;
  7506. }
  7507. if( g->guild.mes1[0] == '\0' && g->guild.mes2[0] == '\0' ){
  7508. return;
  7509. }
  7510. PACKET_ZC_GUILD_NOTICE p = {};
  7511. p.packetType = HEADER_ZC_GUILD_NOTICE;
  7512. safestrncpy( p.subject, g->guild.mes1, sizeof( p.subject ) );
  7513. safestrncpy( p.notice, g->guild.mes2, sizeof( p.notice ) );
  7514. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7515. }
  7516. /// Guild invite.
  7517. /// 016a <guild id>.L <guild name>.24B (ZC_REQ_JOIN_GUILD)
  7518. void clif_guild_invite( map_session_data& sd, const struct mmo_guild& g ){
  7519. PACKET_ZC_REQ_JOIN_GUILD p = {};
  7520. p.packetType = HEADER_ZC_REQ_JOIN_GUILD;
  7521. p.guild_id = g.guild_id;
  7522. safestrncpy( p.guild_name, g.name, sizeof( p.guild_name ) );
  7523. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7524. }
  7525. /// Reply to invite request.
  7526. /// 0169 <answer>.B (ZC_ACK_REQ_JOIN_GUILD)
  7527. /// answer:
  7528. /// 0 = Already in guild.
  7529. /// 1 = Offer rejected.
  7530. /// 2 = Offer accepted.
  7531. /// 3 = Guild full.
  7532. void clif_guild_inviteack( map_session_data& sd, int flag ){
  7533. PACKET_ZC_ACK_REQ_JOIN_GUILD p = {};
  7534. p.packetType = HEADER_ZC_ACK_REQ_JOIN_GUILD;
  7535. p.result = static_cast<decltype(p.result)>( flag );
  7536. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7537. }
  7538. /// Notifies clients of a guild of a leaving member.
  7539. /// 015a <char name>.24B <reason>.40B (ZC_ACK_LEAVE_GUILD)
  7540. /// 0a83 <CID>.L <reason>.40B (ZC_ACK_LEAVE_GUILD_DELNAME)
  7541. void clif_guild_leave( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7542. PACKET_ZC_ACK_LEAVE_GUILD p = {};
  7543. p.packetType = guildLeave;
  7544. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7545. p.GID = char_id;
  7546. #else
  7547. safestrncpy( p.name, name, sizeof( p.name ) );
  7548. #endif
  7549. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7550. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7551. }
  7552. /// Notifies clients of a guild of an expelled member.
  7553. /// 015c <char name>.24B <reason>.40B <account name>.24B (ZC_ACK_BAN_GUILD)
  7554. /// 0839 <char name>.24B <reason>.40B (ZC_ACK_BAN_GUILD_SSO)
  7555. /// 0a82 <CID>.L <reason>.40B (ZC_ACK_BAN_GUILD_DELNAME)
  7556. void clif_guild_expulsion( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7557. PACKET_ZC_ACK_BAN_GUILD p = {};
  7558. p.packetType = guildExpulsion;
  7559. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7560. p.GID = char_id;
  7561. #else
  7562. safestrncpy( p.name, name, sizeof( p.name ) );
  7563. #endif
  7564. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7565. #if PACKETVER < 20100803
  7566. // account name is not sent for security reasons
  7567. safestrncpy( p.account_name, "", sizeof( p.account_name ) );
  7568. #endif
  7569. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7570. }
  7571. /// Guild expulsion list
  7572. /// 0163 <packet len>.W { <char name>.24B <account name>.24B <reason>.40B }* (ZC_BAN_LIST)
  7573. /// 0163 <packet len>.W { <char name>.24B <reason>.40B }* (PACKETVER >= 20100803) (ZC_BAN_LIST)
  7574. /// 0a87 <packet len>.W { <charid>.L <reason>.40B }* (ZC_BAN_LIST2)
  7575. /// 0b7c <packet len>.W { <charid>.L <reason>.40B <char name>.24B }* (ZC_BAN_LIST3)
  7576. static void clif_guild_expulsionlist(map_session_data& sd){
  7577. auto &g = sd.guild;
  7578. if (!g)
  7579. return;
  7580. PACKET_ZC_BAN_LIST* p = reinterpret_cast<PACKET_ZC_BAN_LIST*>( packet_buffer );
  7581. p->packetType = HEADER_ZC_BAN_LIST;
  7582. p->packetLen = sizeof(*p);
  7583. for( size_t i = 0, c = 0; i < MAX_GUILDEXPULSION; i++ ){
  7584. struct guild_expulsion& e = g->guild.expulsion[i];
  7585. if( e.account_id == 0 ){
  7586. continue;
  7587. }
  7588. PACKET_ZC_BAN_LIST_sub& banned = p->chars[c];
  7589. #if PACKETVER >= 20161019
  7590. banned.char_id = e.char_id;
  7591. #elif PACKETVER < 20100803
  7592. // account name (not used for security reasons)
  7593. safestrncpy(banned.account_name, "", sizeof(banned.account_name));
  7594. #endif
  7595. #if PACKETVER >= 20200902 || PACKETVER < 20161019
  7596. safestrncpy(banned.char_name, e.name, sizeof(banned.char_name));
  7597. #endif
  7598. safestrncpy(banned.message, e.mes, sizeof(banned.message));
  7599. p->packetLen += static_cast<decltype(p->packetLen)>(sizeof(banned));
  7600. c++;
  7601. }
  7602. clif_send(p,p->packetLen,&sd.bl,SELF);
  7603. }
  7604. /// Guild chat message
  7605. /// 017f <packet len>.W <message>.?B (ZC_GUILD_CHAT)
  7606. void clif_guild_message( const struct mmo_guild& g, const char* mes, size_t len ){
  7607. PACKET_ZC_GUILD_CHAT *p = reinterpret_cast<PACKET_ZC_GUILD_CHAT*>( packet_buffer );
  7608. // -1 for null terminator
  7609. static const size_t max_len = CHAT_SIZE_MAX - sizeof( *p ) - 1;
  7610. map_session_data* sd = guild_getavailablesd(g);
  7611. // Ignore this message, if no guildmember is available
  7612. if (sd == nullptr)
  7613. return;
  7614. if( len == 0 ){
  7615. return;
  7616. } else if( len > max_len ){
  7617. ShowWarning("clif_guild_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", guild_id=%u).\n", mes, len, max_len, g.guild_id);
  7618. len = max_len;
  7619. }
  7620. p->packetType = HEADER_ZC_GUILD_CHAT;
  7621. p->packetLength = sizeof(*p);
  7622. safestrncpy(p->message, mes, len+1);
  7623. p->packetLength += static_cast<decltype(p->packetLength)>( len + 1 );
  7624. clif_send(p, p->packetLength, &sd->bl, GUILD_NOBG);
  7625. }
  7626. /// Request for guild alliance
  7627. /// 0171 <inviter account id>.L <guild name>.24B (ZC_REQ_ALLY_GUILD).
  7628. void clif_guild_reqalliance(map_session_data& sd,uint32 account_id,const char *name)
  7629. {
  7630. PACKET_ZC_REQ_ALLY_GUILD p{};
  7631. p.packetType = HEADER_ZC_REQ_ALLY_GUILD;
  7632. p.inviterId = account_id;
  7633. safestrncpy(p.inviterGuildName,name,sizeof(p.inviterGuildName));
  7634. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7635. }
  7636. /// Notifies the client about the result of a alliance request.
  7637. /// 0173 <answer>.B (ZC_ACK_REQ_ALLY_GUILD)
  7638. /// answer:
  7639. /// 0 = Already allied.
  7640. /// 1 = You rejected the offer.
  7641. /// 2 = You accepted the offer.
  7642. /// 3 = They have too any alliances.
  7643. /// 4 = You have too many alliances.
  7644. /// 5 = Alliances are disabled.
  7645. void clif_guild_allianceack(map_session_data& sd,uint8 flag){
  7646. PACKET_ZC_ACK_REQ_ALLY_GUILD p{};
  7647. p.packetType = HEADER_ZC_ACK_REQ_ALLY_GUILD;
  7648. p.flag = flag;
  7649. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7650. }
  7651. /// Notifies the client that a alliance or opposition has been removed.
  7652. /// 0184 <other guild id>.L <relation>.L (ZC_DELETE_RELATED_GUILD)
  7653. /// relation:
  7654. /// 0 = Ally
  7655. /// 1 = Enemy
  7656. void clif_guild_delalliance(map_session_data& sd,uint32 guild_id,uint32 flag)
  7657. {
  7658. PACKET_ZC_DELETE_RELATED_GUILD p{};
  7659. p.packetType = HEADER_ZC_DELETE_RELATED_GUILD;
  7660. p.allyID = guild_id;
  7661. p.flag = flag;
  7662. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7663. }
  7664. /// Notifies the client about the result of a opposition request.
  7665. /// 0181 <result>.B (ZC_ACK_REQ_HOSTILE_GUILD)
  7666. /// result:
  7667. /// 0 = Antagonist has been set.
  7668. /// 1 = Guild has too many Antagonists.
  7669. /// 2 = Already set as an Antagonist.
  7670. /// 3 = Antagonists are disabled.
  7671. void clif_guild_oppositionack(map_session_data& sd,uint8 flag){
  7672. PACKET_ZC_ACK_REQ_HOSTILE_GUILD p{};
  7673. p.packetType = HEADER_ZC_ACK_REQ_HOSTILE_GUILD;
  7674. p.flag = flag;
  7675. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7676. }
  7677. /// Adds alliance or opposition (ZC_ADD_RELATED_GUILD).
  7678. /// 0185 <relation>.L <guild id>.L <guild name>.24B
  7679. /*
  7680. void clif_guild_allianceadded(struct guild *g,int idx)
  7681. {
  7682. unsigned char buf[64];
  7683. WBUFW(buf,0)=0x185;
  7684. WBUFL(buf,2)=g->alliance[idx].opposition;
  7685. WBUFL(buf,6)=g->alliance[idx].guild_id;
  7686. safestrncpy(WBUFP(buf,10),g->alliance[idx].name,NAME_LENGTH);
  7687. clif_send(buf,packet_len(0x185),guild_getavailablesd(g),GUILD);
  7688. }
  7689. */
  7690. /// Notifies the client about the result of a guild break.
  7691. /// 015e <reason>.L (ZC_ACK_DISORGANIZE_GUILD_RESULT)
  7692. /// 0 = success
  7693. /// 1 = invalid key (guild name, @see clif_parse_GuildBreak)
  7694. /// 2 = there are still members in the guild
  7695. void clif_guild_broken( map_session_data& sd, int flag ){
  7696. PACKET_ZC_ACK_DISORGANIZE_GUILD_RESULT p = {};
  7697. p.packetType = HEADER_ZC_ACK_DISORGANIZE_GUILD_RESULT;
  7698. p.result = flag;
  7699. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7700. }
  7701. /// Displays emotion on an object (ZC_EMOTION).
  7702. /// 00c0 <id>.L <type>.B
  7703. /// type:
  7704. /// enum emotion_type
  7705. void clif_emotion(struct block_list *bl,int type)
  7706. {
  7707. unsigned char buf[8];
  7708. nullpo_retv(bl);
  7709. WBUFW(buf,0)=0xc0;
  7710. WBUFL(buf,2)=bl->id;
  7711. WBUFB(buf,6)=type;
  7712. clif_send(buf,packet_len(0xc0),bl,AREA);
  7713. }
  7714. /// Displays the contents of a talkiebox trap.
  7715. /// 0191 <id>.L <contents>.80B (ZC_TALKBOX_CHATCONTENTS)
  7716. void clif_talkiebox( struct block_list* bl, const char* talkie ){
  7717. nullpo_retv( bl );
  7718. nullpo_retv( talkie );
  7719. PACKET_ZC_TALKBOX_CHATCONTENTS p = {};
  7720. p.PacketType = HEADER_ZC_TALKBOX_CHATCONTENTS;
  7721. p.aid = bl->id;
  7722. safestrncpy( p.message, talkie, TALKBOX_MESSAGE_SIZE );
  7723. clif_send( &p, sizeof( PACKET_ZC_TALKBOX_CHATCONTENTS ), bl, AREA );
  7724. }
  7725. /// Displays wedding effect centered on an object (ZC_CONGRATULATION).
  7726. /// 01ea <id>.L
  7727. void clif_wedding_effect(struct block_list *bl)
  7728. {
  7729. unsigned char buf[6];
  7730. nullpo_retv(bl);
  7731. WBUFW(buf,0) = 0x1ea;
  7732. WBUFL(buf,2) = bl->id;
  7733. clif_send(buf, packet_len(0x1ea), bl, AREA);
  7734. }
  7735. /// Notifies the client of the name of the partner character (ZC_COUPLENAME).
  7736. /// 01e6 <partner name>.24B
  7737. void clif_callpartner(map_session_data& sd)
  7738. {
  7739. PACKET_ZC_COUPLENAME p = { 0 };
  7740. p.packetType = HEADER_ZC_COUPLENAME;
  7741. if( sd.status.partner_id ) {
  7742. map_session_data *p_sd = pc_get_partner(&sd);
  7743. if (p_sd != nullptr && !p_sd->state.autotrade)
  7744. safestrncpy(p.name, p_sd->status.name, NAME_LENGTH);
  7745. else
  7746. p.name[0] = 0;
  7747. } else {// Send zero-length name if no partner, to initialize the client buffer.
  7748. p.name[0] = 0;
  7749. }
  7750. clif_send(&p, sizeof(p), &sd.bl, AREA);
  7751. }
  7752. /// Initiates the partner "taming" process [DracoRPG] (ZC_START_COUPLE).
  7753. /// 01e4
  7754. /// This packet while still implemented by the client is no longer being officially used.
  7755. /*
  7756. void clif_marriage_process(map_session_data *sd)
  7757. {
  7758. int fd;
  7759. nullpo_retv(sd);
  7760. fd=sd->fd;
  7761. WFIFOHEAD(fd,packet_len(0x1e4));
  7762. WFIFOW(fd,0)=0x1e4;
  7763. WFIFOSET(fd,packet_len(0x1e4));
  7764. }
  7765. */
  7766. /// Notice of divorce (ZC_DIVORCE).
  7767. /// 0205 <partner name>.24B
  7768. void clif_divorced(map_session_data* sd, const char* name)
  7769. {
  7770. int fd;
  7771. nullpo_retv(sd);
  7772. fd=sd->fd;
  7773. WFIFOHEAD(fd,packet_len(0x205));
  7774. WFIFOW(fd,0)=0x205;
  7775. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  7776. WFIFOSET(fd, packet_len(0x205));
  7777. }
  7778. /// Marriage proposal (ZC_REQ_COUPLE).
  7779. /// 01e2 <account id>.L <char id>.L <char name>.24B
  7780. /// This packet while still implemented by the client is no longer being officially used.
  7781. /*
  7782. void clif_marriage_proposal(int fd, map_session_data *sd, map_session_data* ssd)
  7783. {
  7784. nullpo_retv(sd);
  7785. WFIFOHEAD(fd,packet_len(0x1e2));
  7786. WFIFOW(fd,0) = 0x1e2;
  7787. WFIFOL(fd,2) = ssd->status.account_id;
  7788. WFIFOL(fd,6) = ssd->status.char_id;
  7789. safestrncpy(WFIFOCP(fd,10), ssd->status.name, NAME_LENGTH);
  7790. WFIFOSET(fd, packet_len(0x1e2));
  7791. }
  7792. */
  7793. /*==========================================
  7794. * Displays a message using the guild-chat colors to the specified targets. [Skotlex]
  7795. *------------------------------------------*/
  7796. void clif_disp_message(struct block_list* src, const char* mes, size_t len, enum send_target target)
  7797. {
  7798. unsigned char buf[256];
  7799. if( len == 0 ) {
  7800. return;
  7801. } else if( len > sizeof(buf)-5 ) {
  7802. ShowWarning("clif_disp_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", aid=%d).\n", mes, len, sizeof(buf)-5, src->id);
  7803. len = sizeof(buf)-5;
  7804. }
  7805. WBUFW(buf, 0) = 0x17f;
  7806. WBUFW(buf, 2) = static_cast<int16>( len + 5 );
  7807. safestrncpy(WBUFCP(buf,4), mes, len+1);
  7808. clif_send(buf, WBUFW(buf,2), src, target);
  7809. }
  7810. /// Notifies the client about the result of a request to disconnect another player (ZC_ACK_DISCONNECT_CHARACTER).
  7811. /// 00cd <result>.L (unknown packet version or invalid information at packet_len_table)
  7812. /// 00cd <result>.B
  7813. /// result:
  7814. /// 0 = failure
  7815. /// 1 = success
  7816. void clif_GM_kickack(map_session_data *sd, int id)
  7817. {
  7818. int fd;
  7819. nullpo_retv(sd);
  7820. fd = sd->fd;
  7821. WFIFOHEAD(fd,packet_len(0xcd));
  7822. WFIFOW(fd,0) = 0xcd;
  7823. WFIFOB(fd,2) = id; // FIXME: this is not account id
  7824. WFIFOSET(fd, packet_len(0xcd));
  7825. }
  7826. void clif_GM_kick(map_session_data *sd, map_session_data *tsd)
  7827. {
  7828. nullpo_retv(tsd);
  7829. if (sd == nullptr)
  7830. tsd->state.keepshop = true;
  7831. if (session_isActive(tsd->fd))
  7832. clif_authfail_fd(tsd->fd, 15);
  7833. else
  7834. map_quit(tsd);
  7835. if (sd)
  7836. clif_GM_kickack(sd, tsd->status.account_id);
  7837. }
  7838. /// Displays various manner-related status messages (ZC_ACK_GIVE_MANNER_POINT).
  7839. /// 014a <result>.L
  7840. /// result:
  7841. /// 0 = "A manner point has been successfully aligned."
  7842. /// 1 = MP_FAILURE_EXHAUST
  7843. /// 2 = MP_FAILURE_ALREADY_GIVING
  7844. /// 3 = "Chat Block has been applied by GM due to your ill-mannerous action."
  7845. /// 4 = "Automated Chat Block has been applied due to Anti-Spam System."
  7846. /// 5 = "You got a good point from %s."
  7847. void clif_manner_message(map_session_data* sd, uint32 type)
  7848. {
  7849. int fd;
  7850. nullpo_retv(sd);
  7851. fd = sd->fd;
  7852. WFIFOHEAD(fd,packet_len(0x14a));
  7853. WFIFOW(fd,0) = 0x14a;
  7854. WFIFOL(fd,2) = type;
  7855. WFIFOSET(fd, packet_len(0x14a));
  7856. }
  7857. /// Followup to 0x14a type 3/5, informs who did the manner adjustment action (ZC_NOTIFY_MANNER_POINT_GIVEN).
  7858. /// 014b <type>.B <GM name>.24B
  7859. /// type:
  7860. /// 0 = positive (unmute)
  7861. /// 1 = negative (mute)
  7862. void clif_GM_silence(map_session_data* sd, map_session_data* tsd, uint8 type)
  7863. {
  7864. int fd;
  7865. nullpo_retv(sd);
  7866. nullpo_retv(tsd);
  7867. fd = tsd->fd;
  7868. WFIFOHEAD(fd,packet_len(0x14b));
  7869. WFIFOW(fd,0) = 0x14b;
  7870. WFIFOB(fd,2) = type;
  7871. safestrncpy(WFIFOCP(fd,3), sd->status.name, NAME_LENGTH);
  7872. WFIFOSET(fd, packet_len(0x14b));
  7873. }
  7874. /// Notifies the client about the result of a request to allow/deny whispers from a player (ZC_SETTING_WHISPER_PC).
  7875. /// 00d1 <type>.B <result>.B
  7876. /// type:
  7877. /// 0 = /ex (deny)
  7878. /// 1 = /in (allow)
  7879. /// result:
  7880. /// 0 = success
  7881. /// 1 = failure
  7882. /// 2 = too many blocks
  7883. void clif_wisexin(map_session_data *sd,int type,int flag)
  7884. {
  7885. int fd;
  7886. nullpo_retv(sd);
  7887. fd=sd->fd;
  7888. WFIFOHEAD(fd,packet_len(0xd1));
  7889. WFIFOW(fd,0)=0xd1;
  7890. WFIFOB(fd,2)=type;
  7891. WFIFOB(fd,3)=flag;
  7892. WFIFOSET(fd,packet_len(0xd1));
  7893. }
  7894. /// Notifies the client about the result of a request to allow/deny whispers from anyone (ZC_SETTING_WHISPER_STATE).
  7895. /// 00d2 <type>.B <result>.B
  7896. /// type:
  7897. /// 0 = /exall (deny)
  7898. /// 1 = /inall (allow)
  7899. /// result:
  7900. /// 0 = success
  7901. /// 1 = failure
  7902. void clif_wisall(map_session_data *sd,int type,int flag)
  7903. {
  7904. int fd;
  7905. nullpo_retv(sd);
  7906. fd=sd->fd;
  7907. WFIFOHEAD(fd,packet_len(0xd2));
  7908. WFIFOW(fd,0)=0xd2;
  7909. WFIFOB(fd,2)=type;
  7910. WFIFOB(fd,3)=flag;
  7911. WFIFOSET(fd,packet_len(0xd2));
  7912. }
  7913. /// Play a BGM! [Rikter/Yommy] (ZC_PLAY_NPC_BGM).
  7914. /// 07fe <bgm>.24B
  7915. void clif_playBGM( map_session_data& sd, const char* name ){
  7916. #if PACKETVER_MAIN_NUM >= 20220504
  7917. PACKET_ZC_PLAY_NPC_BGM* p = reinterpret_cast<PACKET_ZC_PLAY_NPC_BGM*>( packet_buffer );
  7918. size_t length = strlen( name ) + 1;
  7919. p->PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7920. p->PacketLength = sizeof( *p );
  7921. p->playType = 0; // TODO: implement, send loop for the time being
  7922. safestrncpy( p->bgm, name, length );
  7923. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  7924. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7925. #elif PACKETVER >= 20091201
  7926. PACKET_ZC_PLAY_NPC_BGM p = {};
  7927. p.PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7928. safestrncpy( p.bgm, name, sizeof( p.bgm ) );
  7929. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7930. #endif
  7931. }
  7932. /// Plays/stops a wave sound (ZC_SOUND).
  7933. /// 01d3 <file name>.24B <act>.B <term>.L <npc id>.L
  7934. /// file name:
  7935. /// relative to data\wav
  7936. /// act:
  7937. /// 0 = play (once)
  7938. /// 1 = play (repeat, does not work)
  7939. /// 2 = stops all sound instances of file name (does not work)
  7940. /// term:
  7941. /// unknown purpose, only relevant to act = 1
  7942. /// npc id:
  7943. /// The accustic direction of the sound is determined by the
  7944. /// relative position of the NPC to the player (3D sound).
  7945. void clif_soundeffect( struct block_list& bl, const char* name, int type, enum send_target target ){
  7946. PACKET_ZC_SOUND p = {};
  7947. p.PacketType = HEADER_ZC_SOUND;
  7948. safestrncpy( p.name, name, sizeof( p.name ) );
  7949. p.act = type;
  7950. p.term = 0; // TODO: implement
  7951. p.AID = bl.id;
  7952. clif_send( &p, sizeof( p ), &bl, target );
  7953. }
  7954. /// Displays special effects (npcs, weather, etc) [Valaris] (ZC_NOTIFY_EFFECT2).
  7955. /// 01f3 <id>.L <effect id>.L
  7956. /// effect id:
  7957. /// @see doc/effect_list.txt
  7958. void clif_specialeffect(struct block_list* bl, int type, enum send_target target)
  7959. {
  7960. unsigned char buf[24];
  7961. nullpo_retv(bl);
  7962. memset(buf, 0, packet_len(0x1f3));
  7963. WBUFW(buf,0) = 0x1f3;
  7964. WBUFL(buf,2) = bl->id;
  7965. WBUFL(buf,6) = type;
  7966. clif_send(buf, packet_len(0x1f3), bl, target);
  7967. if (disguised(bl)) {
  7968. WBUFL(buf,2) = disguised_bl_id(bl->id);
  7969. clif_send(buf, packet_len(0x1f3), bl, SELF);
  7970. }
  7971. }
  7972. void clif_specialeffect_single(struct block_list* bl, int type, int fd)
  7973. {
  7974. WFIFOHEAD(fd,10);
  7975. WFIFOW(fd,0) = 0x1f3;
  7976. WFIFOL(fd,2) = bl->id;
  7977. WFIFOL(fd,6) = type;
  7978. WFIFOSET(fd,10);
  7979. }
  7980. /// Notifies clients of an special/visual effect that accepts an value (ZC_NOTIFY_EFFECT3).
  7981. /// 0284 <id>.L <effect id>.L <num data>.L
  7982. /// effect id:
  7983. /// @see doc/effect_list.txt
  7984. /// num data:
  7985. /// effect-dependent value
  7986. void clif_specialeffect_value(struct block_list* bl, int effect_id, int num, send_target target)
  7987. {
  7988. uint8 buf[14];
  7989. WBUFW(buf,0) = 0x284;
  7990. WBUFL(buf,2) = bl->id;
  7991. WBUFL(buf,6) = effect_id;
  7992. WBUFL(buf,10) = num;
  7993. clif_send(buf, packet_len(0x284), bl, target);
  7994. if( disguised(bl) )
  7995. {
  7996. WBUFL(buf,2) = disguised_bl_id(bl->id);
  7997. clif_send(buf, packet_len(0x284), bl, SELF);
  7998. }
  7999. }
  8000. void clif_specialeffect_remove(struct block_list* bl_src, int effect, enum send_target e_target, struct block_list* bl_target)
  8001. {
  8002. #if PACKETVER >= 20181002
  8003. nullpo_retv( bl_src );
  8004. nullpo_retv( bl_target );
  8005. PACKET_ZC_REMOVE_EFFECT p = {};
  8006. p.packetType = HEADER_ZC_REMOVE_EFFECT;
  8007. p.aid = bl_src->id;
  8008. p.effectId = effect;
  8009. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_target, e_target );
  8010. if( disguised(bl_src) )
  8011. {
  8012. p.aid = disguised_bl_id( bl_src->id );
  8013. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_src, SELF );
  8014. }
  8015. #endif
  8016. }
  8017. /// Monster/NPC color chat [SnakeDrak] (ZC_NPC_CHAT).
  8018. /// 02c1 <packet len>.W <id>.L <color>.L <message>.?B
  8019. void clif_messagecolor_target(struct block_list *bl, unsigned long color, const char *msg, bool rgb2bgr, enum send_target type, map_session_data *sd) {
  8020. unsigned short msg_len = (unsigned short)(strlen(msg) + 1);
  8021. uint8 buf[CHAT_SIZE_MAX];
  8022. nullpo_retv(bl);
  8023. if (msg_len > sizeof(buf) - 12) {
  8024. ShowWarning("clif_messagecolor: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  8025. msg_len = sizeof(buf) - 12;
  8026. }
  8027. if (rgb2bgr)
  8028. color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16; // RGB to BGR
  8029. WBUFW(buf,0) = 0x2C1;
  8030. WBUFW(buf,2) = msg_len + 12;
  8031. WBUFL(buf,4) = bl->id;
  8032. WBUFL(buf,8) = color;
  8033. memcpy(WBUFCP(buf,12), msg, msg_len);
  8034. clif_send(buf, WBUFW(buf,2), (sd == nullptr ? bl : &(sd->bl)), type);
  8035. }
  8036. /**
  8037. * Notifies the client that the storage window is still open
  8038. *
  8039. * Should only be used in cases where the client closed the
  8040. * storage window without server's consent
  8041. */
  8042. void clif_refresh_storagewindow(map_session_data *sd) {
  8043. // Notify the client that the storage is open
  8044. if( sd->state.storage_flag == 1 ) {
  8045. storage_sortitem(sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage));
  8046. clif_storagelist(sd, sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage), storage_getName(0));
  8047. clif_updatestorageamount(*sd, sd->storage.amount, sd->storage.max_amount);
  8048. }
  8049. // Notify the client that the gstorage is open otherwise it will
  8050. // remain locked forever and nobody will be able to access it
  8051. if( sd->state.storage_flag == 2 ) {
  8052. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  8053. if( !gstor ) // Shouldn't happen. The information should already be at the map-server
  8054. intif_request_guild_storage(sd->status.account_id, sd->status.guild_id);
  8055. else {
  8056. storage_sortitem(gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild));
  8057. clif_storagelist(sd, gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild), "Guild Storage");
  8058. clif_updatestorageamount(*sd, gstor->amount, gstor->max_amount);
  8059. }
  8060. }
  8061. // Notify the client that the premium storage is open
  8062. if (sd->state.storage_flag == 3) {
  8063. storage_sortitem(sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage));
  8064. clif_storagelist(sd, sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage), storage_getName(sd->premiumStorage.stor_id));
  8065. clif_updatestorageamount(*sd, sd->premiumStorage.amount, sd->premiumStorage.max_amount);
  8066. }
  8067. }
  8068. // refresh the client's screen, getting rid of any effects
  8069. void clif_refresh(map_session_data *sd)
  8070. {
  8071. nullpo_retv(sd);
  8072. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8073. clif_inventorylist(sd);
  8074. clif_equipswitch_list(sd);
  8075. if(pc_iscarton(sd)) {
  8076. clif_cartlist(sd);
  8077. clif_updatestatus(*sd,SP_CARTINFO);
  8078. }
  8079. clif_updatestatus(*sd,SP_WEIGHT);
  8080. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8081. clif_updatestatus(*sd,SP_STR);
  8082. clif_updatestatus(*sd,SP_AGI);
  8083. clif_updatestatus(*sd,SP_VIT);
  8084. clif_updatestatus(*sd,SP_INT);
  8085. clif_updatestatus(*sd,SP_DEX);
  8086. clif_updatestatus(*sd,SP_LUK);
  8087. #ifdef RENEWAL
  8088. clif_updatestatus(*sd,SP_POW);
  8089. clif_updatestatus(*sd,SP_STA);
  8090. clif_updatestatus(*sd,SP_WIS);
  8091. clif_updatestatus(*sd,SP_SPL);
  8092. clif_updatestatus(*sd,SP_CON);
  8093. clif_updatestatus(*sd,SP_CRT);
  8094. #endif
  8095. if (sd->spiritball)
  8096. clif_spiritball( &sd->bl, &sd->bl, SELF );
  8097. clif_millenniumshield_single( *sd, *sd );
  8098. clif_spiritcharm_single( *sd, *sd );
  8099. if (sd->soulball)
  8100. clif_soulball( sd, &sd->bl, SELF );
  8101. if (sd->servantball)
  8102. clif_servantball( *sd, &sd->bl, SELF );
  8103. if (sd->abyssball)
  8104. clif_abyssball( *sd, &sd->bl, SELF );
  8105. if (sd->vd.cloth_color)
  8106. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8107. if (sd->vd.body_style)
  8108. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8109. if(hom_is_active(sd->hd))
  8110. clif_send_homdata( *sd->hd, SP_ACK );
  8111. if( sd->md ) {
  8112. clif_mercenary_info(sd);
  8113. clif_mercenary_skillblock(sd);
  8114. }
  8115. if( sd->ed )
  8116. clif_elemental_info(sd);
  8117. map_foreachinallrange(clif_getareachar,&sd->bl,AREA_SIZE,BL_ALL,sd);
  8118. clif_weather_check(sd);
  8119. if( sd->chatID )
  8120. chat_leavechat(sd,0);
  8121. if( sd->state.vending )
  8122. clif_openvending( *sd );
  8123. if( pc_issit(sd) )
  8124. clif_sitting(sd->bl); // FIXME: just send to self, not area
  8125. if( pc_isdead(sd) ) // When you refresh, resend the death packet.
  8126. clif_clearunit_single( sd->bl.id, CLR_DEAD, *sd );
  8127. else
  8128. clif_changed_dir(sd->bl, SELF);
  8129. clif_efst_status_change_sub(&sd->bl,&sd->bl,SELF);
  8130. //Issue #2143
  8131. //Cancel Trading State
  8132. if (sd->state.trading)
  8133. trade_tradecancel(sd);
  8134. //Cancel Buying/Selling State
  8135. if (sd->state.buyingstore)
  8136. buyingstore_close(sd);
  8137. mail_clear(sd);
  8138. clif_loadConfirm( sd );
  8139. if( disguised(&sd->bl) ) {/* refresh-da */
  8140. short disguise = sd->disguise;
  8141. pc_disguise(sd, 0);
  8142. pc_disguise(sd, disguise);
  8143. }
  8144. clif_refresh_storagewindow(sd);
  8145. }
  8146. /// Updates the object's (bl) name on client.
  8147. /// Used to update when a char leaves a party/guild. [Skotlex]
  8148. /// Needed because when you send a 0x95 packet, the client will not remove the cached party/guild info that is not sent.
  8149. /// 0095 <id>.L <char name>.24B (ZC_ACK_REQNAME)
  8150. /// 0195 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B (ZC_ACK_REQNAMEALL)
  8151. /// 0a30 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B <title ID>.L (ZC_ACK_REQNAMEALL2)
  8152. void clif_name( struct block_list* src, struct block_list *bl, send_target target ){
  8153. nullpo_retv( src );
  8154. nullpo_retv( bl );
  8155. switch( bl->type ){
  8156. case BL_PC: {
  8157. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8158. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8159. packet.gid = bl->id;
  8160. map_session_data *sd = (map_session_data *)bl;
  8161. //Requesting your own "shadow" name. [Skotlex]
  8162. if( src == bl && target == SELF && sd->disguise ){
  8163. packet.gid = -bl->id;
  8164. }
  8165. if( sd->fakename[0] ) {
  8166. safestrncpy( packet.name, sd->fakename, NAME_LENGTH );
  8167. clif_send( &packet, sizeof(packet), src, target );
  8168. return;
  8169. }
  8170. safestrncpy( packet.name, sd->status.name, NAME_LENGTH );
  8171. party_data *p = nullptr;
  8172. if( sd->status.party_id ){
  8173. p = party_search( sd->status.party_id );
  8174. }
  8175. // do not display party unless the player is also in a guild
  8176. if( p && ( sd->guild || battle_config.display_party_name ) ){
  8177. safestrncpy( packet.party_name, p->party.name, NAME_LENGTH );
  8178. }
  8179. if( sd->guild ){
  8180. int position;
  8181. // Will get the position of the guild the player is in
  8182. position = guild_getposition(*sd);
  8183. safestrncpy( packet.guild_name, sd->guild->guild.name, NAME_LENGTH );
  8184. safestrncpy( packet.position_name, sd->guild->guild.position[position].name, NAME_LENGTH );
  8185. }else if( sd->clan ){
  8186. safestrncpy( packet.position_name, sd->clan->name, NAME_LENGTH );
  8187. }
  8188. #if PACKETVER_MAIN_NUM >= 20150225 || PACKETVER_RE_NUM >= 20141126 || defined( PACKETVER_ZERO )
  8189. packet.title_id = sd->status.title_id; // Title ID
  8190. #endif
  8191. clif_send(&packet, sizeof(packet), src, target);
  8192. }
  8193. break;
  8194. //[blackhole89]
  8195. case BL_HOM:
  8196. case BL_MER:
  8197. case BL_PET:
  8198. case BL_NPC:
  8199. case BL_ELEM: {
  8200. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8201. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8202. packet.gid = bl->id;
  8203. switch (bl->type) {
  8204. case BL_HOM:
  8205. memcpy(packet.name, ((TBL_HOM *)bl)->homunculus.name, NAME_LENGTH);
  8206. break;
  8207. case BL_MER:
  8208. memcpy(packet.name, ((TBL_MER *)bl)->db->name.c_str(), NAME_LENGTH);
  8209. break;
  8210. case BL_PET:
  8211. safestrncpy(packet.name, ((TBL_PET *)bl)->pet.name, NAME_LENGTH);
  8212. break;
  8213. case BL_NPC:
  8214. safestrncpy(packet.name, ((TBL_NPC *)bl)->name, NAME_LENGTH);
  8215. break;
  8216. case BL_ELEM:
  8217. safestrncpy(packet.name, ((TBL_ELEM *)bl)->db->name.c_str(), NAME_LENGTH);
  8218. break;
  8219. }
  8220. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8221. unit_data *ud = unit_bl2ud(bl);
  8222. if (ud != nullptr) {
  8223. memcpy(packet.title, ud->title, NAME_LENGTH);
  8224. packet.groupId = ud->group_id;
  8225. }
  8226. #endif
  8227. clif_send(&packet, sizeof(packet), src, target);
  8228. }
  8229. break;
  8230. case BL_MOB: {
  8231. mob_data *md = (mob_data *)bl;
  8232. if( md->guardian_data && md->guardian_data->guild_id ){
  8233. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8234. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8235. packet.gid = bl->id;
  8236. safestrncpy( packet.name, md->name, NAME_LENGTH );
  8237. safestrncpy( packet.guild_name, md->guardian_data->guild_name, NAME_LENGTH );
  8238. safestrncpy( packet.position_name, md->guardian_data->castle->castle_name, NAME_LENGTH );
  8239. clif_send(&packet, sizeof(packet), src, target);
  8240. }else if( battle_config.show_mob_info ){
  8241. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8242. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8243. packet.gid = bl->id;
  8244. safestrncpy( packet.name, md->name, NAME_LENGTH );
  8245. char mobhp[50], *str_p = mobhp;
  8246. if( battle_config.show_mob_info&4 ){
  8247. str_p += sprintf( str_p, "Lv. %d | ", md->level );
  8248. }
  8249. if( battle_config.show_mob_info&1 ){
  8250. str_p += sprintf( str_p, "HP: %u/%u | ", md->status.hp, md->status.max_hp );
  8251. }
  8252. if( battle_config.show_mob_info&2 ){
  8253. str_p += sprintf( str_p, "HP: %u%% | ", get_percentage( md->status.hp, md->status.max_hp ) );
  8254. }
  8255. // Even thought mobhp ain't a name, we send it as one so the client can parse it. [Skotlex]
  8256. if( str_p != mobhp ){
  8257. *(str_p-3) = '\0'; //Remove trailing space + pipe.
  8258. safestrncpy( packet.party_name, mobhp, NAME_LENGTH );
  8259. }
  8260. clif_send(&packet, sizeof(packet), src, target);
  8261. } else {
  8262. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8263. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8264. packet.gid = bl->id;
  8265. safestrncpy(packet.name, md->name, NAME_LENGTH);
  8266. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8267. unit_data *ud = unit_bl2ud(bl);
  8268. if (ud != nullptr) {
  8269. memcpy(packet.title, ud->title, NAME_LENGTH);
  8270. packet.groupId = ud->group_id;
  8271. }
  8272. #endif
  8273. clif_send(&packet, sizeof(packet), src, target);
  8274. }
  8275. }
  8276. break;
  8277. case BL_CHAT:
  8278. case BL_SKILL:
  8279. // Newer clients request this, but do not need an answer
  8280. return;
  8281. default:
  8282. ShowError("clif_name: bad type %d(%d)\n", bl->type, bl->id);
  8283. return;
  8284. }
  8285. }
  8286. /// Taekwon Jump (TK_HIGHJUMP) effect (ZC_HIGHJUMP).
  8287. /// 01ff <id>.L <x>.W <y>.W
  8288. ///
  8289. /// Visually moves(instant) a character to x,y. The char moves even
  8290. /// when the target cell isn't walkable. If the char is sitting it
  8291. /// stays that way.
  8292. void clif_slide(block_list& bl, int x, int y){
  8293. PACKET_ZC_HIGHJUMP p{};
  8294. p.packetType = HEADER_ZC_HIGHJUMP;
  8295. p.srcId = bl.id;
  8296. p.x = x;
  8297. p.y = y;
  8298. clif_send(&p, sizeof(p), &bl, AREA);
  8299. if( disguised(&bl) )
  8300. {
  8301. p.srcId = disguised_bl_id(bl.id);
  8302. clif_send(&p, sizeof(p), &bl, SELF);
  8303. }
  8304. }
  8305. /// Public chat message (ZC_NOTIFY_CHAT). lordalfa/Skotlex - used by @me as well
  8306. /// 008d <packet len>.W <id>.L <message>.?B
  8307. void clif_disp_overhead_(struct block_list *bl, const char* mes, enum send_target flag)
  8308. {
  8309. unsigned char buf[256]; //This should be more than sufficient, the theorical max is CHAT_SIZE + 8 (pads and extra inserted crap)
  8310. int16 len_mes;
  8311. len_mes = static_cast<decltype(len_mes)>( strlen(mes) + 1 ); //Account for \0
  8312. if (len_mes > sizeof(buf)-8) {
  8313. ShowError("clif_disp_overhead: Message too long (length %d)\n", len_mes);
  8314. len_mes = sizeof(buf)-8; //Trunk it to avoid problems.
  8315. }
  8316. // send message to others
  8317. if (flag == AREA) {
  8318. WBUFW(buf,0) = 0x8d;
  8319. WBUFW(buf,2) = len_mes + 8; // len of message + 8 (command+len+id)
  8320. WBUFL(buf,4) = bl->id;
  8321. safestrncpy(WBUFCP(buf,8), mes, len_mes);
  8322. clif_send(buf, WBUFW(buf,2), bl, AREA_CHAT_WOC);
  8323. }
  8324. // send back message to the speaker
  8325. if( bl->type == BL_PC ) {
  8326. WBUFW(buf,0) = 0x8e;
  8327. WBUFW(buf, 2) = len_mes + 4;
  8328. safestrncpy(WBUFCP(buf,4), mes, len_mes);
  8329. clif_send(buf, WBUFW(buf,2), bl, SELF);
  8330. }
  8331. }
  8332. /*==========================
  8333. * Minimap fix [Kevin]
  8334. * Remove dot from minimap
  8335. *--------------------------*/
  8336. void clif_party_xy_remove(map_session_data* sd)
  8337. {
  8338. nullpo_retv(sd);
  8339. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p{};
  8340. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  8341. p.AID = sd->status.account_id;
  8342. p.xPos = -1;
  8343. p.yPos = -1;
  8344. clif_send(&p, sizeof(p), &sd->bl, PARTY_SAMEMAP_WOS);
  8345. }
  8346. /// Displays a skill message (thanks to Rayce) (ZC_SKILLMSG).
  8347. /// 0215 <msg id>.L
  8348. /// msg id:
  8349. /// 0x15 = End all negative status (PA_GOSPEL)
  8350. /// 0x16 = Immunity to all status (PA_GOSPEL)
  8351. /// 0x17 = MaxHP +100% (PA_GOSPEL)
  8352. /// 0x18 = MaxSP +100% (PA_GOSPEL)
  8353. /// 0x19 = All stats +20 (PA_GOSPEL)
  8354. /// 0x1c = Enchant weapon with Holy element (PA_GOSPEL)
  8355. /// 0x1d = Enchant armor with Holy element (PA_GOSPEL)
  8356. /// 0x1e = DEF +25% (PA_GOSPEL)
  8357. /// 0x1f = ATK +100% (PA_GOSPEL)
  8358. /// 0x20 = HIT/Flee +50 (PA_GOSPEL)
  8359. /// 0x28 = Full strip failed because of coating (ST_FULLSTRIP)
  8360. /// ? = nothing
  8361. void clif_gospel_info(map_session_data *sd, int type)
  8362. {
  8363. int fd=sd->fd;
  8364. WFIFOHEAD(fd,packet_len(0x215));
  8365. WFIFOW(fd,0)=0x215;
  8366. WFIFOL(fd,2)=type;
  8367. WFIFOSET(fd, packet_len(0x215));
  8368. }
  8369. /// Multi-purpose mission information packet (ZC_STARSKILL).
  8370. /// 020e <mapname>.24B <monster_id>.L <star>.B <result>.B
  8371. /// result:
  8372. /// 0 = Star Gladiator %s has designed <mapname>'s as the %s.
  8373. /// star:
  8374. /// 0 = Place of the Sun
  8375. /// 1 = Place of the Moon
  8376. /// 2 = Place of the Stars
  8377. /// 1 = Star Gladiator %s's %s: <mapname>
  8378. /// star:
  8379. /// 0 = Place of the Sun
  8380. /// 1 = Place of the Moon
  8381. /// 2 = Place of the Stars
  8382. /// 10 = Star Gladiator %s has designed <mapname>'s as the %s.
  8383. /// star:
  8384. /// 0 = Target of the Sun
  8385. /// 1 = Target of the Moon
  8386. /// 2 = Target of the Stars
  8387. /// 11 = Star Gladiator %s's %s: <mapname used as monster name>
  8388. /// star:
  8389. /// 0 = Monster of the Sun
  8390. /// 1 = Monster of the Moon
  8391. /// 2 = Monster of the Stars
  8392. /// 20 = [TaeKwon Mission] Target Monster : <mapname used as monster name> (<star>%)
  8393. /// 21 = [Taming Mission] Target Monster : <mapname used as monster name>
  8394. /// 22 = [Collector Rank] Target Item : <monster_id used as item id>
  8395. /// 30 = [Sun, Moon and Stars Angel] Designed places and monsters have been reset.
  8396. /// 40 = Target HP : <monster_id used as HP>
  8397. void clif_starskill(map_session_data* sd, const char* mapname, int monster_id, unsigned char star, unsigned char result)
  8398. {
  8399. int fd = sd->fd;
  8400. WFIFOHEAD(fd,packet_len(0x20e));
  8401. WFIFOW(fd,0) = 0x20e;
  8402. safestrncpy(WFIFOCP(fd,2), mapname, NAME_LENGTH);
  8403. WFIFOL(fd,26) = monster_id;
  8404. WFIFOB(fd,30) = star;
  8405. WFIFOB(fd,31) = result;
  8406. WFIFOSET(fd,packet_len(0x20e));
  8407. }
  8408. /*==========================================
  8409. * Info about Star Glaldiator save map [Komurka]
  8410. * type: 1: Information, 0: Map registered
  8411. *------------------------------------------*/
  8412. void clif_feel_info(map_session_data* sd, unsigned char feel_level, unsigned char type)
  8413. {
  8414. char mapname[MAP_NAME_LENGTH_EXT];
  8415. mapindex_getmapname_ext(mapindex_id2name(sd->feel_map[feel_level].index), mapname);
  8416. clif_starskill(sd, mapname, 0, feel_level, type ? 1 : 0);
  8417. }
  8418. /*==========================================
  8419. * Info about Star Glaldiator hate mob [Komurka]
  8420. * type: 1: Register mob, 0: Information.
  8421. *------------------------------------------*/
  8422. void clif_hate_info(map_session_data *sd, unsigned char hate_level,int class_, unsigned char type)
  8423. {
  8424. if( pcdb_checkid(class_) ) {
  8425. clif_starskill(sd, job_name(class_), class_, hate_level, type ? 10 : 11);
  8426. } else if( mobdb_checkid(class_) ) {
  8427. clif_starskill(sd, mob_db.find(class_)->jname.c_str(), class_, hate_level, type ? 10 : 11);
  8428. } else {
  8429. 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, (unsigned int)hate_level, (unsigned int)type);
  8430. }
  8431. }
  8432. /*==========================================
  8433. * Info about TaeKwon Do TK_MISSION mob [Skotlex]
  8434. *------------------------------------------*/
  8435. void clif_mission_info(map_session_data *sd, int mob_id, unsigned char progress)
  8436. {
  8437. clif_starskill(sd, mob_db.find(mob_id)->jname.c_str(), mob_id, progress, 20);
  8438. }
  8439. /*==========================================
  8440. * Feel/Hate reset (thanks to Rayce) [Skotlex]
  8441. *------------------------------------------*/
  8442. void clif_feel_hate_reset(map_session_data *sd)
  8443. {
  8444. clif_starskill(sd, "", 0, 0, 30);
  8445. }
  8446. /// Send out reply to configuration change
  8447. /// 02d9 <type>.L <value>.L (ZC_CONFIG)
  8448. /// type: see enum e_config_type
  8449. /// value:
  8450. /// false = disabled
  8451. /// true = enabled
  8452. void clif_configuration( map_session_data* sd, enum e_config_type type, bool enabled ){
  8453. int fd;
  8454. nullpo_retv(sd);
  8455. fd = sd->fd;
  8456. WFIFOHEAD(fd, packet_len(0x2d9));
  8457. WFIFOW(fd, 0) = 0x2d9;
  8458. WFIFOL(fd, 2) = type;
  8459. WFIFOL(fd, 6) = enabled;
  8460. WFIFOSET(fd, packet_len(0x2d9));
  8461. }
  8462. /// The player's 'view equip' state, sent during login (ZC_CONFIG_NOTIFY).
  8463. /// 02da <open equip window>.B
  8464. /// open equip window:
  8465. /// 0 = disabled
  8466. /// 1 = enabled
  8467. void clif_equipcheckbox(map_session_data* sd)
  8468. {
  8469. int fd;
  8470. nullpo_retv(sd);
  8471. fd = sd->fd;
  8472. WFIFOHEAD(fd, packet_len(0x2da));
  8473. WFIFOW(fd, 0) = 0x2da;
  8474. WFIFOB(fd, 2) = (sd->status.show_equip ? 1 : 0);
  8475. WFIFOSET(fd, packet_len(0x2da));
  8476. }
  8477. /// Sends info about a player's equipped items.
  8478. /// 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)
  8479. /// 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)
  8480. /// 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)
  8481. /// 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)
  8482. /// 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)
  8483. /// 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)
  8484. void clif_viewequip_ack( map_session_data& sd, map_session_data& tsd ){
  8485. #if PACKETVER_AD_NUM >= 20071211 || PACKETVER_SAK_NUM >= 20071127 || PACKETVER_MAIN_NUM >= 20071211 || defined(PACKETVER_RE)
  8486. PACKET_ZC_EQUIPWIN_MICROSCOPE* p = reinterpret_cast<PACKET_ZC_EQUIPWIN_MICROSCOPE*>( packet_buffer );
  8487. p->PacketType = HEADER_ZC_EQUIPWIN_MICROSCOPE;
  8488. p->PacketLength = sizeof( *p );
  8489. safestrncpy( p->characterName, tsd.status.name, NAME_LENGTH );
  8490. p->job = tsd.status.class_;
  8491. p->head = tsd.vd.hair_style;
  8492. p->accessory = tsd.vd.head_bottom;
  8493. p->accessory2 = tsd.vd.head_mid;
  8494. p->accessory3 = tsd.vd.head_top;
  8495. #if PACKETVER >= 20110111
  8496. p->robe = tsd.vd.robe;
  8497. #endif
  8498. p->headpalette = tsd.vd.hair_color;
  8499. p->bodypalette = tsd.vd.cloth_color;
  8500. #if PACKETVER_MAIN_NUM >= 20180801 || PACKETVER_RE_NUM >= 20180801 || PACKETVER_ZERO_NUM >= 20180808
  8501. p->body2 = tsd.vd.body_style;
  8502. #endif
  8503. p->sex = tsd.vd.sex;
  8504. for( int i = 0, equip = 0; i < EQI_MAX; i++ ){
  8505. int k = tsd.equip_index[i];
  8506. if( k >= 0 ){
  8507. if( tsd.inventory.u.items_inventory[k].nameid == 0 || tsd.inventory_data[k] == nullptr ){ // Item doesn't exist
  8508. continue;
  8509. }
  8510. if( !tsd.inventory.u.items_inventory[k].equip ){
  8511. continue;
  8512. }
  8513. if( !itemdb_isequip2( tsd.inventory_data[k] ) ){ // Is not equippable
  8514. continue;
  8515. }
  8516. clif_item_equip( client_index( k ), &p->list[equip], &tsd.inventory.u.items_inventory[k], tsd.inventory_data[k], pc_equippoint( &tsd, k ) );
  8517. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->list[0] ) );
  8518. equip++;
  8519. }
  8520. }
  8521. clif_send( p, p->PacketLength, &sd.bl, SELF );
  8522. #endif
  8523. }
  8524. /// Display msgstringtable.txt string (ZC_MSG).
  8525. /// 0291 <message>.W
  8526. void clif_msg(map_session_data* sd, unsigned short id)
  8527. {
  8528. int fd;
  8529. nullpo_retv(sd);
  8530. fd = sd->fd;
  8531. WFIFOHEAD(fd, packet_len(0x291));
  8532. WFIFOW(fd, 0) = 0x291;
  8533. WFIFOW(fd, 2) = id; // zero-based msgstringtable.txt index
  8534. WFIFOSET(fd, packet_len(0x291));
  8535. }
  8536. /// Display msgstringtable.txt string and fill in a valid for %d format (ZC_MSG_VALUE).
  8537. /// 0x7e2 <message>.W <value>.L
  8538. void clif_msg_value(map_session_data* sd, unsigned short id, int value)
  8539. {
  8540. int fd = sd->fd;
  8541. WFIFOHEAD(fd, packet_len(0x7e2));
  8542. WFIFOW(fd,0) = 0x7e2;
  8543. WFIFOW(fd,2) = id;
  8544. WFIFOL(fd,4) = value;
  8545. WFIFOSET(fd, packet_len(0x7e2));
  8546. }
  8547. /// Displays msgstringtable.txt string, prefixed with a skill name. (ZC_MSG_SKILL).
  8548. /// 07e6 <skill id>.W <msg id>.L
  8549. ///
  8550. /// NOTE: Message has following format and is printed in color 0xCDCDFF (purple):
  8551. /// "[SkillName] Message"
  8552. void clif_msg_skill(map_session_data* sd, uint16 skill_id, int msg_id)
  8553. {
  8554. int fd = sd->fd;
  8555. WFIFOHEAD(fd, packet_len(0x7e6));
  8556. WFIFOW(fd,0) = 0x7e6;
  8557. WFIFOW(fd,2) = skill_id;
  8558. WFIFOL(fd,4) = msg_id;
  8559. WFIFOSET(fd, packet_len(0x7e6));
  8560. }
  8561. /// Displays msgstringtable.txt string in a color. (ZC_MSG_COLOR).
  8562. /// 09cd <msg id>.W <color>.L
  8563. void clif_msg_color( map_session_data *sd, uint16 msg_id, uint32 color ){
  8564. nullpo_retv(sd);
  8565. int fd = sd->fd;
  8566. WFIFOHEAD(fd, packet_len(0x9cd));
  8567. WFIFOW(fd, 0) = 0x9cd;
  8568. WFIFOW(fd, 2) = msg_id;
  8569. WFIFOL(fd, 4) = color;
  8570. WFIFOSET(fd, packet_len(0x9cd));
  8571. }
  8572. /// Validates one global/guild/party/whisper message packet and tries to recognize its components.
  8573. /// Returns true if the packet was parsed successfully.
  8574. /// Formats: false - <packet id>.w <packet len>.w (<name> : <message>).?B 00
  8575. /// true - <packet id>.w <packet len>.w <name>.24B <message>.?B 00
  8576. static bool clif_process_message(map_session_data* sd, bool whisperFormat, char* out_name, char* out_message, char* out_full_message ){
  8577. const char* seperator = " : ";
  8578. int fd;
  8579. struct s_packet_db* info;
  8580. uint16 packetLength, inputLength;
  8581. const char *input, *name, *message;
  8582. size_t nameLength, messageLength;
  8583. fd = sd->fd;
  8584. info = &packet_db[RFIFOW(fd,0)];
  8585. packetLength = RFIFOW(fd,info->pos[0]);
  8586. input = RFIFOCP(fd,info->pos[1]);
  8587. // basic structure check for the 4-byte header
  8588. if( packetLength < 4 ){
  8589. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no message data)!\n", sd->status.name);
  8590. return false;
  8591. }else{
  8592. inputLength = packetLength - 4;
  8593. }
  8594. // process <name> part of the packet
  8595. if( whisperFormat ) {
  8596. // name has fixed width
  8597. if( inputLength < NAME_LENGTH + 1 ) {
  8598. ShowWarning("clif_process_message: Received malformed packet from player '%s' (packet length is incorrect)!\n", sd->status.name);
  8599. return false;
  8600. }
  8601. name = input;
  8602. // validate name
  8603. nameLength = strnlen( name, NAME_LENGTH - 1 );
  8604. // only restriction is that the name must be zero-terminated
  8605. if( name[nameLength] != '\0' ) {
  8606. ShowWarning("clif_process_message: Player '%s' sent an unterminated name!\n", sd->status.name);
  8607. return false;
  8608. }
  8609. message = input + NAME_LENGTH;
  8610. messageLength = inputLength - NAME_LENGTH;
  8611. }else{
  8612. // name and message are separated by ' : '
  8613. size_t seperatorLength = strnlen( seperator, NAME_LENGTH );
  8614. nameLength = strnlen( sd->status.name, NAME_LENGTH - 1 ); // name length (w/o zero byte)
  8615. // check if there's enough data provided
  8616. if( inputLength < nameLength + seperatorLength + 1 ){
  8617. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no username data)!\n", sd->status.name);
  8618. return false;
  8619. }
  8620. name = input;
  8621. // validate name
  8622. if( strncmp( name, sd->status.name, nameLength ) || // the text must start with the speaker's name
  8623. strncmp( name + nameLength, seperator, seperatorLength ) ) // followed by the seperator
  8624. {
  8625. //Hacked message, or infamous "client desynch" issue where they pick one char while loading another.
  8626. ShowWarning("clif_process_message: Player '%s' sent a message using an incorrect name! Forcing a relog...\n", sd->status.name);
  8627. set_eof(sd->fd); // Just kick them out to correct it.
  8628. return false;
  8629. }
  8630. message = input + nameLength + seperatorLength;
  8631. messageLength = inputLength - nameLength - seperatorLength;
  8632. }
  8633. #if PACKETVER < 20151001
  8634. // the declared length must match real length
  8635. if( messageLength != strnlen(message, messageLength)+1 ) {
  8636. ShowWarning("clif_process_message: Received malformed packet from player '%s' (length is incorrect)!\n", sd->status.name);
  8637. return false;
  8638. }
  8639. // verify <message> part of the packet
  8640. if( message[messageLength-1] != '\0' ) { // message must be zero-terminated
  8641. ShowWarning("clif_process_message: Player '%s' sent an unterminated message string!\n", sd->status.name);
  8642. return false;
  8643. }
  8644. #else
  8645. // No zero termination anymore
  8646. messageLength += 1;
  8647. #endif
  8648. // messages mustn't be too long
  8649. if( messageLength > CHAT_SIZE_MAX-1 ) {
  8650. // Normally you can only enter CHATBOX_SIZE-1 letters into the chat box, but Frost Joke / Dazzler's text can be longer.
  8651. // Also, the physical size of strings that use multibyte encoding can go multiple times over the chatbox capacity.
  8652. // Neither the official client nor server place any restriction on the length of the data in the packet,
  8653. // but we'll only allow reasonably long strings here. This also makes sure that they fit into the `chatlog` table.
  8654. ShowWarning("clif_process_message: Player '%s' sent a message too long ('%.*s')!\n", sd->status.name, CHAT_SIZE_MAX-1, message);
  8655. return false;
  8656. }
  8657. // If it is not a whisper message, set the name to the fakename of the player
  8658. if( whisperFormat == false && sd->fakename[0] != '\0' ){
  8659. strcpy( out_name, sd->fakename );
  8660. }else{
  8661. safestrncpy( out_name, name, nameLength + 1 );
  8662. }
  8663. safestrncpy( out_message, message, messageLength );
  8664. if( whisperFormat ){
  8665. sprintf( out_full_message, "%-24s%s", out_name, out_message );
  8666. out_full_message[nameLength] = '\0';
  8667. }else{
  8668. sprintf( out_full_message, "%s%s%s", out_name, seperator, out_message );
  8669. }
  8670. if( is_atcommand( fd, sd, out_message, 1 ) )
  8671. return false;
  8672. if (sd->sc.cant.chat || (sd->state.block_action & PCBLOCK_CHAT))
  8673. return false; //no "chatting" while muted.
  8674. if( battle_config.min_chat_delay ) { //[Skotlex]
  8675. if (DIFF_TICK(sd->cantalk_tick, gettick()) > 0)
  8676. return false;
  8677. sd->cantalk_tick = gettick() + battle_config.min_chat_delay;
  8678. }
  8679. if (battle_config.idletime_option&IDLE_CHAT)
  8680. sd->idletime = last_tick;
  8681. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_CHAT)
  8682. sd->idletime_hom = last_tick;
  8683. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_CHAT)
  8684. sd->idletime_mer = last_tick;
  8685. //achievement_update_objective(sd, AG_CHATTING, 1, 1); // !TODO: Confirm how this achievement is triggered
  8686. return true;
  8687. }
  8688. /**
  8689. * Displays a message if the player enters a PK Zone (during pk_mode)
  8690. * @param sd: Player data
  8691. */
  8692. inline void clif_pk_mode_message(map_session_data * sd)
  8693. {
  8694. if (battle_config.pk_mode && battle_config.pk_mode_mes &&
  8695. sd && map_getmapflag(sd->bl.m, MF_PVP)) {
  8696. if( (int)sd->status.base_level < battle_config.pk_min_level ) {
  8697. char output[CHAT_SIZE_MAX];
  8698. // 1504: You've entered a PK Zone (safe until level %d).
  8699. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(sd,1504),
  8700. battle_config.pk_min_level);
  8701. clif_showscript(&sd->bl, output, SELF);
  8702. } else {
  8703. // 1503: You've entered a PK Zone.
  8704. clif_showscript(&sd->bl, msg_txt(sd,1503), SELF);
  8705. }
  8706. }
  8707. return;
  8708. }
  8709. // ---------------------
  8710. // clif_parse_wanttoconnect
  8711. // ---------------------
  8712. // Parses a WantToConnection packet to try to identify which is the packet version used. [Skotlex]
  8713. // error codes:
  8714. // 0 - Success
  8715. // 1 - Unknown packet_ver
  8716. // 2 - Invalid account_id
  8717. // 3 - Invalid char_id
  8718. // 4 - Invalid login_id1 (reserved)
  8719. // 5 - Invalid client_tick (reserved)
  8720. // 6 - Invalid sex
  8721. // Only the first 'invalid' error that appears is used.
  8722. static int clif_parse_WantToConnection_sub(int fd)
  8723. {
  8724. int value; //Value is used to temporarily store account/char_id/sex
  8725. //By default, start searching on the default one.
  8726. uint16 cmd = RFIFOW(fd, 0);
  8727. int16 packet_len;
  8728. packet_len = static_cast<decltype(packet_len)>( RFIFOREST( fd ) );
  8729. // FIXME: If the packet is not received at once, this will FAIL.
  8730. // Figure out, when it happens, that only part of the packet is
  8731. // received, or fix the function to be able to deal with that
  8732. // case.
  8733. if( packet_len != packet_db[cmd].len )
  8734. return 1; /* wrong length */
  8735. else if( (value=(int)RFIFOL(fd, packet_db[cmd].pos[0])) < START_ACCOUNT_NUM || value > END_ACCOUNT_NUM )
  8736. return 2; /* invalid account_id */
  8737. else if( (value=(int)RFIFOL(fd, packet_db[cmd].pos[1])) <= 0 )
  8738. return 3; /* invalid char_id */
  8739. /* RFIFOL(fd, packet_db[cmd].pos[2]) - don't care about login_id1 */
  8740. /* RFIFOL(fd, packet_db[cmd].pos[3]) - don't care about client_tick */
  8741. else if( (value=(int)RFIFOB(fd, packet_db[cmd].pos[4])) != 0 && value != 1 )
  8742. return 6; /* invalid sex */
  8743. else
  8744. return 0;
  8745. }
  8746. /// Request to connect to map-server.
  8747. /// 0072 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER)
  8748. /// 0436 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER2)
  8749. /// There are various variants of this packet, some of them have padding between fields.
  8750. void clif_parse_WantToConnection(int fd, map_session_data* sd)
  8751. {
  8752. struct block_list* bl;
  8753. struct auth_node* node;
  8754. int cmd, account_id, char_id, login_id1, sex, err;
  8755. t_tick client_tick; //The client tick is a tick, therefore it needs be unsigned. [Skotlex]
  8756. if (sd) {
  8757. ShowError("clif_parse_WantToConnection : invalid request (character already logged in)\n");
  8758. return;
  8759. }
  8760. cmd = RFIFOW(fd, 0);
  8761. account_id = RFIFOL(fd, packet_db[cmd].pos[0]);
  8762. char_id = RFIFOL(fd, packet_db[cmd].pos[1]);
  8763. login_id1 = RFIFOL(fd, packet_db[cmd].pos[2]);
  8764. client_tick = RFIFOL(fd, packet_db[cmd].pos[3]);
  8765. sex = RFIFOB(fd, packet_db[cmd].pos[4]);
  8766. err = clif_parse_WantToConnection_sub(fd);
  8767. if( err ){ // connection rejected
  8768. ShowInfo("clif_parse: Disconnecting session #%d with unknown connect packet 0x%04x(length:%d)%s\n", fd, cmd, RFIFOREST(fd), (
  8769. err == 1 ? "." :
  8770. err == 2 ? ", possibly for having an invalid account_id." :
  8771. err == 3 ? ", possibly for having an invalid char_id." :
  8772. /* Uncomment when checks are added in clif_parse_WantToConnection_sub. [FlavioJS]
  8773. err == 4 ? ", possibly for having an invalid login_id1." :
  8774. err == 5 ? ", possibly for having an invalid client_tick." :
  8775. */
  8776. err == 6 ? ", possibly for having an invalid sex." :
  8777. ". ERROR invalid error code"));
  8778. WFIFOHEAD(fd,packet_len(0x6a));
  8779. WFIFOW(fd,0) = 0x6a;
  8780. WFIFOB(fd,2) = err;
  8781. WFIFOSET(fd,packet_len(0x6a));
  8782. #ifdef DUMP_INVALID_PACKET
  8783. ShowDump(RFIFOP(fd, 0), RFIFOREST(fd));
  8784. #endif
  8785. RFIFOSKIP(fd, RFIFOREST(fd));
  8786. set_eof(fd);
  8787. return;
  8788. }
  8789. if( !global_core->is_running() ){ // not allowed
  8790. clif_authfail_fd(fd,1);// server closed
  8791. return;
  8792. }
  8793. //Check for double login.
  8794. bl = map_id2bl(account_id);
  8795. if(bl && bl->type != BL_PC) {
  8796. ShowError("clif_parse_WantToConnection: a non-player object already has id %d, please increase the starting account number\n", account_id);
  8797. WFIFOHEAD(fd,packet_len(0x6a));
  8798. WFIFOW(fd,0) = 0x6a;
  8799. WFIFOB(fd,2) = 3; // Rejected by server
  8800. WFIFOSET(fd,packet_len(0x6a));
  8801. set_eof(fd);
  8802. return;
  8803. }
  8804. if (bl ||
  8805. ((node=chrif_search(account_id)) && //An already existing node is valid only if it is for this login.
  8806. !(node->account_id == account_id && node->char_id == char_id && node->state == ST_LOGIN)))
  8807. {
  8808. clif_authfail_fd(fd, 8); //Still recognizes last connection
  8809. return;
  8810. }
  8811. CREATE(sd, TBL_PC, 1);
  8812. new(sd) map_session_data();
  8813. sd->fd = fd;
  8814. #ifdef PACKET_OBFUSCATION
  8815. sd->cryptKey = (((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF) * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF;
  8816. #endif
  8817. session[fd]->session_data = sd;
  8818. pc_setnewpc(sd, account_id, char_id, login_id1, client_tick, sex, fd);
  8819. #if PACKETVER < 20070521
  8820. WFIFOHEAD(fd,4);
  8821. WFIFOL(fd,0) = sd->bl.id;
  8822. WFIFOSET(fd,4);
  8823. #else
  8824. WFIFOHEAD(fd,packet_len(0x283));
  8825. WFIFOW(fd,0) = 0x283;
  8826. WFIFOL(fd,2) = sd->bl.id;
  8827. WFIFOSET(fd,packet_len(0x283));
  8828. #endif
  8829. chrif_authreq(sd,false);
  8830. }
  8831. /// Notification from the client, that it has finished map loading and is about to display player's character (CZ_NOTIFY_ACTORINIT).
  8832. /// 007d
  8833. void clif_parse_LoadEndAck(int fd,map_session_data *sd)
  8834. {
  8835. bool guild_notice = false;
  8836. if(sd->bl.prev != nullptr)
  8837. return;
  8838. // Autotraders should ignore this entirely, clif_parse_LoadEndAck is always invoked manually for them
  8839. if (!sd->state.active || (!sd->state.autotrade && !sd->state.pc_loaded)) { //Character loading is not complete yet!
  8840. //Let pc_reg_received or pc_scdata_received reinvoke this when ready.
  8841. sd->state.connect_new = 0;
  8842. return;
  8843. }
  8844. if (sd->state.rewarp) { //Rewarp player.
  8845. sd->state.rewarp = 0;
  8846. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8847. return;
  8848. }
  8849. sd->state.warping = 0;
  8850. // look
  8851. #if PACKETVER < 4
  8852. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8853. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8854. #else
  8855. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8856. #endif
  8857. pc_set_costume_view(sd);
  8858. if(sd->vd.cloth_color)
  8859. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8860. if(sd->vd.body_style)
  8861. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8862. // item
  8863. clif_inventorylist(sd); // inventory list first, otherwise deleted items in pc_checkitem show up as 'unknown item'
  8864. pc_checkitem(sd);
  8865. clif_equipswitch_list(sd);
  8866. // cart
  8867. if(pc_iscarton(sd)) {
  8868. clif_cartlist(sd);
  8869. clif_updatestatus(*sd,SP_CARTINFO);
  8870. }
  8871. // weight
  8872. clif_updatestatus(*sd,SP_WEIGHT);
  8873. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8874. // guild
  8875. // (needs to go before clif_spawn() to show guild emblems correctly)
  8876. if(sd->status.guild_id)
  8877. guild_send_memberinfoshort(sd,1);
  8878. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8879. if(battle_config.pc_invincible_time > 0) {
  8880. if(mapdata_flag_gvg(mapdata))
  8881. pc_setinvincibletimer(sd,battle_config.pc_invincible_time<<1);
  8882. else
  8883. pc_setinvincibletimer(sd,battle_config.pc_invincible_time);
  8884. }
  8885. if( mapdata->users++ == 0 && battle_config.dynamic_mobs )
  8886. map_spawnmobs(sd->bl.m);
  8887. if( !pc_isinvisible(sd) ) { // increment the number of pvp players on the map
  8888. mapdata->users_pvp++;
  8889. }
  8890. sd->state.debug_remove_map = 0; // temporary state to track double remove_map's [FlavioJS]
  8891. // reset the callshop flag if the player changes map
  8892. sd->state.callshop = 0;
  8893. if(map_addblock(&sd->bl))
  8894. return;
  8895. clif_spawn(&sd->bl);
  8896. // Party
  8897. // (needs to go after clif_spawn() to show hp bars correctly)
  8898. if(sd->status.party_id) {
  8899. party_send_movemap(sd);
  8900. clif_party_hp( *sd ); // Show hp after displacement [LuzZza]
  8901. }
  8902. if( sd->bg_id ) clif_bg_hp(sd); // BattleGround System
  8903. if(!pc_isinvisible(sd) && mapdata->getMapFlag(MF_PVP)) {
  8904. if(!battle_config.pk_mode) { // remove pvp stuff for pk_mode [Valaris]
  8905. if (!mapdata->getMapFlag(MF_PVP_NOCALCRANK))
  8906. sd->pvp_timer = add_timer(gettick()+200, pc_calc_pvprank_timer, sd->bl.id, 0);
  8907. sd->pvp_rank = 0;
  8908. sd->pvp_lastusers = 0;
  8909. sd->pvp_point = 5;
  8910. sd->pvp_won = 0;
  8911. sd->pvp_lost = 0;
  8912. }
  8913. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8914. } else if(sd->duel_group) // set flag, if it's a duel [LuzZza]
  8915. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8916. else if (mapdata->getMapFlag(MF_GVG_DUNGEON))
  8917. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF); //TODO: Figure out the real packet to send here.
  8918. else if( mapdata_flag_gvg(mapdata) )
  8919. clif_map_property(&sd->bl, MAPPROPERTY_AGITZONE, SELF);
  8920. else
  8921. clif_map_property(&sd->bl, MAPPROPERTY_NOTHING, SELF);
  8922. // info about nearby objects
  8923. // must use foreachinarea (CIRCULAR_AREA interferes with foreachinrange)
  8924. 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);
  8925. // pet
  8926. if( sd->pd ) {
  8927. if( battle_config.pet_no_gvg && mapdata_flag_gvg(mapdata) ) { //Return the pet to egg. [Skotlex]
  8928. clif_displaymessage(sd->fd, msg_txt(sd,666));
  8929. pet_return_egg( sd, sd->pd );
  8930. } else {
  8931. if(map_addblock(&sd->pd->bl))
  8932. return;
  8933. clif_spawn(&sd->pd->bl);
  8934. clif_send_petdata(sd,sd->pd,0,0);
  8935. clif_send_petstatus(sd);
  8936. // skill_unit_move(&sd->pd->bl,gettick(),1);
  8937. }
  8938. }
  8939. //homunculus [blackhole89]
  8940. if( hom_is_active(sd->hd) ) {
  8941. if(map_addblock(&sd->hd->bl))
  8942. return;
  8943. clif_spawn(&sd->hd->bl);
  8944. clif_send_homdata( *sd->hd, SP_ACK );
  8945. clif_hominfo(sd,sd->hd,1);
  8946. clif_hominfo(sd,sd->hd,0); //for some reason, at least older clients want this sent twice
  8947. clif_homskillinfoblock( *sd->hd );
  8948. status_calc_bl(&sd->hd->bl, { SCB_SPEED });
  8949. if( !(battle_config.hom_setting&HOMSET_NO_INSTANT_LAND_SKILL) )
  8950. skill_unit_move(&sd->hd->bl,gettick(),1); // apply land skills immediately
  8951. }
  8952. if( sd->md ) {
  8953. if(map_addblock(&sd->md->bl))
  8954. return;
  8955. clif_spawn(&sd->md->bl);
  8956. clif_mercenary_info(sd);
  8957. clif_mercenary_skillblock(sd);
  8958. status_calc_bl(&sd->md->bl, { SCB_SPEED }); // Mercenary mimic their master's speed on each map change
  8959. }
  8960. if( sd->ed ) {
  8961. if (map_addblock(&sd->ed->bl))
  8962. return;
  8963. clif_spawn(&sd->ed->bl);
  8964. clif_elemental_info(sd);
  8965. clif_elemental_updatestatus(*sd, SP_HP);
  8966. clif_hpmeter_single( *sd, sd->ed->bl.id, sd->ed->battle_status.hp, sd->ed->battle_status.max_hp );
  8967. clif_elemental_updatestatus(*sd, SP_SP);
  8968. status_calc_bl(&sd->ed->bl, { SCB_SPEED }); //Elemental mimic their master's speed on each map change
  8969. }
  8970. if(sd->state.connect_new) {
  8971. int lv;
  8972. guild_notice = true;
  8973. clif_skillinfoblock(sd);
  8974. clif_hotkeys_send(sd,0);
  8975. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  8976. clif_hotkeys_send(sd,1);
  8977. #endif
  8978. clif_updatestatus(*sd,SP_BASEEXP);
  8979. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  8980. clif_updatestatus(*sd,SP_JOBEXP);
  8981. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  8982. clif_updatestatus(*sd,SP_SKILLPOINT);
  8983. clif_initialstatus( *sd );
  8984. if (sd->sc.option&OPTION_FALCON)
  8985. clif_status_load(&sd->bl, EFST_FALCON, 1);
  8986. else if (sd->sc.option&(OPTION_RIDING|OPTION_DRAGON))
  8987. clif_status_load(&sd->bl, EFST_RIDING, 1);
  8988. else if (sd->sc.option&OPTION_WUGRIDER)
  8989. clif_status_load(&sd->bl, EFST_WUGRIDER, 1);
  8990. else if (sd->sc.getSCE(SC_ALL_RIDING))
  8991. clif_status_load(&sd->bl, EFST_ALL_RIDING, 1);
  8992. if(sd->status.manner < 0)
  8993. sc_start(&sd->bl,&sd->bl,SC_NOCHAT,100,0,0);
  8994. //Auron reported that This skill only triggers when you logon on the map o.O [Skotlex]
  8995. if ((lv = pc_checkskill(sd,SG_KNOWLEDGE)) > 0) {
  8996. if(sd->bl.m == sd->feel_map[0].m
  8997. || sd->bl.m == sd->feel_map[1].m
  8998. || sd->bl.m == sd->feel_map[2].m)
  8999. sc_start(&sd->bl,&sd->bl, SC_KNOWLEDGE, 100, lv, skill_get_time(SG_KNOWLEDGE, lv));
  9000. }
  9001. if(sd->pd && sd->pd->pet.intimate > 900)
  9002. clif_pet_emotion(sd->pd,(sd->pd->pet.class_ - 100)*100 + 50 + pet_hungry_val(sd->pd));
  9003. if(hom_is_active(sd->hd))
  9004. hom_init_timers(sd->hd);
  9005. if (night_flag && mapdata->getMapFlag(MF_NIGHTENABLED)) {
  9006. sd->state.night = 1;
  9007. clif_status_load(&sd->bl, EFST_SKE, 1);
  9008. }
  9009. // Notify everyone that this char logged in [Skotlex].
  9010. map_foreachpc(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 1);
  9011. if (!sd->state.autotrade) { // Don't trigger NPC event or opening vending/buyingstore will be failed
  9012. npc_script_event( *sd, NPCE_LOGIN );
  9013. }
  9014. // Set facing direction before check below to update client
  9015. if (battle_config.spawn_direction)
  9016. unit_setdir(&sd->bl, sd->status.body_direction, false);
  9017. } else {
  9018. //For some reason the client "loses" these on warp/map-change.
  9019. clif_updatestatus(*sd,SP_STR);
  9020. clif_updatestatus(*sd,SP_AGI);
  9021. clif_updatestatus(*sd,SP_VIT);
  9022. clif_updatestatus(*sd,SP_INT);
  9023. clif_updatestatus(*sd,SP_DEX);
  9024. clif_updatestatus(*sd,SP_LUK);
  9025. #ifdef RENEWAL
  9026. clif_updatestatus(*sd,SP_POW);
  9027. clif_updatestatus(*sd,SP_STA);
  9028. clif_updatestatus(*sd,SP_WIS);
  9029. clif_updatestatus(*sd,SP_SPL);
  9030. clif_updatestatus(*sd,SP_CON);
  9031. clif_updatestatus(*sd,SP_CRT);
  9032. #endif
  9033. // abort currently running script
  9034. sd->state.using_fake_npc = 0;
  9035. sd->state.menu_or_input = 0;
  9036. sd->npc_menu = 0;
  9037. if(sd->npc_id)
  9038. npc_event_dequeue(sd);
  9039. }
  9040. if( sd->state.changemap ) {// restore information that gets lost on map-change
  9041. clif_partyinvitationstate( *sd );
  9042. #if PACKETVER >= 20070918
  9043. clif_equipcheckbox(sd);
  9044. #endif
  9045. clif_pet_autofeed_status(sd,false);
  9046. clif_configuration( sd, CONFIG_CALL, sd->status.disable_call );
  9047. #if PACKETVER >= 20170920
  9048. if( battle_config.homunculus_autofeed_always ){
  9049. // Always send ON or OFF
  9050. if( sd->hd && battle_config.feature_homunculus_autofeed ){
  9051. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, sd->hd->homunculus.autofeed );
  9052. }else{
  9053. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, false );
  9054. }
  9055. }else{
  9056. // Only send when enabled
  9057. if( sd->hd && battle_config.feature_homunculus_autofeed && sd->hd->homunculus.autofeed ){
  9058. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, true );
  9059. }
  9060. }
  9061. #endif
  9062. clif_reputation_list( *sd );
  9063. if (sd->guild && battle_config.guild_notice_changemap == 1){
  9064. // Displays after VIP
  9065. clif_guild_notice( *sd );
  9066. guild_notice = false; // Do not display it twice
  9067. }
  9068. if (battle_config.bg_flee_penalty != 100 || battle_config.gvg_flee_penalty != 100) {
  9069. struct map_data *pmap = map_getmapdata(sd->state.pmap);
  9070. if ((pmap != nullptr && (mapdata_flag_gvg(pmap) || pmap->getMapFlag(MF_BATTLEGROUND))) || (mapdata != nullptr && (mapdata_flag_gvg(mapdata) || mapdata->getMapFlag(MF_BATTLEGROUND))))
  9071. status_calc_bl(&sd->bl, { SCB_FLEE }); //Refresh flee penalty
  9072. }
  9073. if( night_flag && mapdata->getMapFlag(MF_NIGHTENABLED) )
  9074. { //Display night.
  9075. if( !sd->state.night )
  9076. {
  9077. sd->state.night = 1;
  9078. clif_status_load(&sd->bl, EFST_SKE, 1);
  9079. }
  9080. }
  9081. else if( sd->state.night )
  9082. { //Clear night display.
  9083. sd->state.night = 0;
  9084. clif_status_load(&sd->bl, EFST_SKE, 0);
  9085. }
  9086. if( mapdata->getMapFlag(MF_BATTLEGROUND) )
  9087. {
  9088. clif_map_type( *sd, MAPTYPE_BATTLEFIELD ); // Battleground Mode
  9089. if( map_getmapflag(sd->bl.m, MF_BATTLEGROUND) == 2 )
  9090. clif_bg_updatescore_single(sd);
  9091. }
  9092. if( mapdata->getMapFlag(MF_ALLOWKS) && !mapdata_flag_ks(mapdata) )
  9093. {
  9094. char output[128];
  9095. sprintf(output, "[ Kill Steal Protection Disable. KS is allowed in this map ]");
  9096. clif_broadcast(&sd->bl, output, strlen(output) + 1, BC_BLUE, SELF);
  9097. }
  9098. if( pc_has_permission(sd,PC_PERM_VIEW_HPMETER) ) {
  9099. mapdata->hpmeter_visible++;
  9100. sd->state.hpmeter_visible = 1;
  9101. }
  9102. status_change_clear_onChangeMap(&sd->bl, &sd->sc);
  9103. map_iwall_get(sd); // Updates Walls Info on this Map to Client
  9104. status_calc_pc(sd, sd->state.autotrade ? SCO_FIRST : SCO_NONE); // Some conditions are map-dependent so we must recalculate
  9105. #ifdef VIP_ENABLE
  9106. if (!sd->state.connect_new &&
  9107. !sd->vip.disableshowrate &&
  9108. sd->state.pmap != sd->bl.m &&
  9109. map_getmapflag(sd->state.pmap, MF_BEXP) != mapdata->getMapFlag(MF_BEXP)
  9110. )
  9111. {
  9112. clif_display_pinfo( *sd );
  9113. }
  9114. #endif
  9115. // Instances do not need their own channels
  9116. if( channel_config.map_tmpl.name[0] && (channel_config.map_tmpl.opt&CHAN_OPT_AUTOJOIN) && !mapdata->instance_id && !mapdata->getMapFlag(MF_NOMAPCHANNELAUTOJOIN) )
  9117. channel_mjoin(sd); //join new map
  9118. clif_pk_mode_message(sd);
  9119. }
  9120. if( sd->guild && ( battle_config.guild_notice_changemap == 2 || guild_notice ) ){
  9121. // Displays at end
  9122. clif_guild_notice( *sd );
  9123. }
  9124. mail_clear(sd);
  9125. /* Guild Aura Init */
  9126. if( sd->guild && sd->state.gmaster_flag ) {
  9127. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  9128. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  9129. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  9130. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  9131. }
  9132. if( sd->state.vending ) { /* show we have a vending */
  9133. clif_openvending( *sd );
  9134. clif_showvendingboard( *sd );
  9135. }
  9136. // Don't trigger NPC event or opening vending/buyingstore will be failed
  9137. if(!sd->state.autotrade && mapdata->getMapFlag(MF_LOADEVENT)) // Lance
  9138. npc_script_event( *sd, NPCE_LOADMAP );
  9139. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  9140. clif_status_load(&sd->bl, EFST_DEVIL1, 1); //blindness [Komurka]
  9141. if (sd->sc.opt2) //Client loses these on warp.
  9142. clif_changeoption(&sd->bl);
  9143. 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)) {
  9144. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM);
  9145. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM);
  9146. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  9147. }
  9148. clif_weather_check(sd);
  9149. // For automatic triggering of NPCs after map loading (so you don't need to walk 1 step first)
  9150. if (map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNPC))
  9151. npc_touch_area_allnpc(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  9152. else
  9153. sd->areanpc.clear();
  9154. /* it broke at some point (e.g. during a crash), so we make it visibly dead again. */
  9155. if( !sd->status.hp && !pc_isdead(sd) && status_isdead(sd->bl) )
  9156. pc_setdead(sd);
  9157. // If player is dead, and is spawned (such as @refresh) send death packet. [Valaris]
  9158. if(pc_isdead(sd))
  9159. clif_clearunit_area( sd->bl, CLR_DEAD );
  9160. else {
  9161. skill_usave_trigger(sd);
  9162. if (battle_config.spawn_direction)
  9163. clif_changed_dir(sd->bl, SELF);
  9164. }
  9165. // Trigger skill effects if you appear standing on them
  9166. if(!battle_config.pc_invincible_time)
  9167. skill_unit_move(&sd->bl,gettick(),1);
  9168. pc_show_questinfo_reinit(sd);
  9169. pc_show_questinfo(sd);
  9170. #if PACKETVER >= 20150513
  9171. if( sd->mail.inbox.unread ){
  9172. clif_Mail_new(sd, 0, nullptr, nullptr);
  9173. }
  9174. #endif
  9175. sd->state.connect_new = 0;
  9176. sd->state.changemap = false;
  9177. }
  9178. /// Server's tick (ZC_NOTIFY_TIME).
  9179. /// 007f <time>.L
  9180. void clif_notify_time(map_session_data* sd, t_tick time)
  9181. {
  9182. int fd = sd->fd;
  9183. WFIFOHEAD(fd,packet_len(0x7f));
  9184. WFIFOW(fd,0) = 0x7f;
  9185. WFIFOL(fd,2) = client_tick(time);
  9186. WFIFOSET(fd,packet_len(0x7f));
  9187. }
  9188. /// Request for server's tick.
  9189. /// 007e <client tick>.L (CZ_REQUEST_TIME)
  9190. /// 0360 <client tick>.L (CZ_REQUEST_TIME2)
  9191. /// There are various variants of this packet, some of them have padding between fields.
  9192. void clif_parse_TickSend(int fd, map_session_data *sd)
  9193. {
  9194. sd->client_tick = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9195. clif_notify_time(sd, gettick());
  9196. }
  9197. /// Sends hotkey bar.
  9198. /// 02b9 { <is skill>.B <id>.L <count>.W }*27 (ZC_SHORTCUT_KEY_LIST)
  9199. /// 07d9 { <is skill>.B <id>.L <count>.W }*36 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090603)
  9200. /// 07d9 { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090617)
  9201. /// 0a00 <rotate>.B { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V3, PACKETVER >= 20141022)
  9202. void clif_hotkeys_send( map_session_data *sd, int tab ){
  9203. #if defined(HOTKEY_SAVING) && ( PACKETVER_MAIN_NUM >= 20070711 || PACKETVER_RE_NUM >= 20080827 || PACKETVER_AD_NUM >= 20070711 || PACKETVER_SAK_NUM >= 20070628 )
  9204. nullpo_retv( sd );
  9205. PACKET_ZC_SHORTCUT_KEY_LIST p = {};
  9206. p.packetType = HEADER_ZC_SHORTCUT_KEY_LIST;
  9207. #if PACKETVER_MAIN_NUM >= 20141022 || PACKETVER_RE_NUM >= 20141015 || defined(PACKETVER_ZERO)
  9208. if( tab == 0 ){
  9209. p.rotate = sd->status.hotkey_rowshift;
  9210. }else{
  9211. p.rotate = sd->status.hotkey_rowshift2;
  9212. }
  9213. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9214. p.tab = tab;
  9215. #endif
  9216. #endif
  9217. for( int i = 0, offset = tab * MAX_HOTKEYS; i < MAX_HOTKEYS_PACKET; i++ ){
  9218. p.hotkey[i].isSkill = sd->status.hotkeys[i + offset].type;
  9219. p.hotkey[i].id = sd->status.hotkeys[i + offset].id;
  9220. p.hotkey[i].count = sd->status.hotkeys[i + offset].lv;
  9221. }
  9222. clif_send( &p, sizeof(PACKET_ZC_SHORTCUT_KEY_LIST), &sd->bl, SELF );
  9223. #endif
  9224. }
  9225. /// Request to update a position on the hotkey row bar
  9226. void clif_parse_HotkeyRowShift( int fd, map_session_data *sd ){
  9227. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9228. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE2* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE2*>( RFIFOP( fd, 0 ) );
  9229. if( p->tab == 0 ){
  9230. sd->status.hotkey_rowshift = p->rowshift;
  9231. }else{
  9232. sd->status.hotkey_rowshift2 = p->rowshift;
  9233. }
  9234. #elif PACKETVER_MAIN_NUM >= 20140129 || PACKETVER_RE_NUM >= 20140129 || defined(PACKETVER_ZERO)
  9235. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE1* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE1*>( RFIFOP( fd, 0 ) );
  9236. sd->status.hotkey_rowshift = p->rowshift;
  9237. #endif
  9238. }
  9239. /// Request to update a position on the hotkey bar.
  9240. /// 02ba <index>.W <is skill>.B <id>.L <count>.W (CZ_SHORTCUT_KEY_CHANGE)
  9241. void clif_parse_Hotkey(int fd, map_session_data *sd) {
  9242. #ifdef HOTKEY_SAVING
  9243. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9244. const PACKET_CZ_SHORTCUT_KEY_CHANGE2* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE2*>( RFIFOP( fd, 0 ) );
  9245. const unsigned short idx = p->index + p->tab * MAX_HOTKEYS;
  9246. if( idx >= MAX_HOTKEYS_DB ){
  9247. return;
  9248. }
  9249. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9250. sd->status.hotkeys[idx].id = p->hotkey.id;
  9251. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9252. #elif PACKETVER_MAIN_NUM >= 20070618 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_AD_NUM >= 20070618 || PACKETVER_SAK_NUM >= 20070618
  9253. const PACKET_CZ_SHORTCUT_KEY_CHANGE1* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE1*>( RFIFOP( fd, 0 ) );
  9254. const unsigned short idx = p->index;
  9255. if( idx >= MAX_HOTKEYS_DB ){
  9256. return;
  9257. }
  9258. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9259. sd->status.hotkeys[idx].id = p->hotkey.id;
  9260. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9261. #endif
  9262. #endif
  9263. }
  9264. /// Displays cast-like progress bar (ZC_PROGRESS).
  9265. /// 02f0 <color>.L <time>.L
  9266. void clif_progressbar(map_session_data * sd, unsigned long color, unsigned int second)
  9267. {
  9268. int fd = sd->fd;
  9269. WFIFOHEAD(fd,packet_len(0x2f0));
  9270. WFIFOW(fd,0) = 0x2f0;
  9271. WFIFOL(fd,2) = color;
  9272. WFIFOL(fd,6) = second;
  9273. WFIFOSET(fd,packet_len(0x2f0));
  9274. }
  9275. /// Removes an ongoing progress bar (ZC_PROGRESS_CANCEL).
  9276. /// 02f2
  9277. void clif_progressbar_abort(map_session_data * sd)
  9278. {
  9279. int fd = sd->fd;
  9280. WFIFOHEAD(fd,packet_len(0x2f2));
  9281. WFIFOW(fd,0) = 0x2f2;
  9282. WFIFOSET(fd,packet_len(0x2f2));
  9283. }
  9284. /// Notification from the client, that the progress bar has reached 100%.
  9285. /// 02f1 (CZ_PROGRESS)
  9286. void clif_parse_progressbar(int fd, map_session_data * sd){
  9287. // No progressbar active, ignore it
  9288. if( !sd->progressbar.npc_id ){
  9289. return;
  9290. }
  9291. int npc_id = sd->progressbar.npc_id;
  9292. bool closing = false;
  9293. // Check if the progress was canceled
  9294. if( gettick() < sd->progressbar.timeout && sd->st ){
  9295. closing = true;
  9296. sd->st->state = CLOSE; // will result in END in npc_scriptcont
  9297. // If a message window was open, offer a close button to the user
  9298. if( sd->st->mes_active ){
  9299. clif_scriptclose( *sd, npc_id );
  9300. }
  9301. }
  9302. sd->progressbar.npc_id = 0;
  9303. sd->progressbar.timeout = 0;
  9304. sd->state.workinprogress = WIP_DISABLE_NONE;
  9305. if( battle_config.idletime_option&IDLE_NPC_PROGRESS ){
  9306. sd->idletime = last_tick;
  9307. }
  9308. npc_scriptcont(sd, npc_id, closing);
  9309. }
  9310. /// Displays cast-like progress bar on a NPC
  9311. /// 09d1 <id>.L <color>.L <time>.L (ZC_PROGRESS_ACTOR)
  9312. void clif_progressbar_npc( struct npc_data *nd, map_session_data* sd ){
  9313. #if PACKETVER >= 20130821
  9314. unsigned char buf[14];
  9315. if( nd->progressbar.timeout > 0 ){
  9316. WBUFW(buf,0) = 0x9d1;
  9317. WBUFL(buf,2) = nd->bl.id;
  9318. WBUFL(buf,6) = nd->progressbar.color;
  9319. WBUFL(buf,10) = client_tick( ( nd->progressbar.timeout - gettick() ) / 1000 );
  9320. if( sd ){
  9321. clif_send(buf, packet_len(0x9d1), &sd->bl, SELF);
  9322. }else{
  9323. clif_send(buf, packet_len(0x9d1), &nd->bl, AREA);
  9324. }
  9325. }
  9326. #endif
  9327. }
  9328. /// Request to walk to a certain position on the current map.
  9329. /// 0085 <dest>.3B (CZ_REQUEST_MOVE)
  9330. /// 035f <dest>.3B (CZ_REQUEST_MOVE2)
  9331. /// There are various variants of this packet, some of them have padding between fields.
  9332. void clif_parse_WalkToXY(int fd, map_session_data *sd)
  9333. {
  9334. short x, y;
  9335. if (pc_isdead(sd)) {
  9336. clif_clearunit_area( sd->bl, CLR_DEAD );
  9337. return;
  9338. }
  9339. if (sd->sc.opt1 && ( sd->sc.opt1 == OPT1_STONEWAIT || sd->sc.opt1 == OPT1_BURNING ))
  9340. ; //You CAN walk on this OPT1 value.
  9341. else if (sd->progressbar.npc_id) {
  9342. clif_progressbar_abort(sd);
  9343. return; // First walk attempt cancels the progress bar
  9344. } else if (pc_cant_act(sd))
  9345. return;
  9346. if(sd->sc.getSCE(SC_RUN) || sd->sc.getSCE(SC_WUGDASH))
  9347. return;
  9348. RFIFOPOS(fd, packet_db[RFIFOW(fd,0)].pos[0], &x, &y, nullptr);
  9349. //A move command one cell west is only valid if the target cell is free
  9350. if(battle_config.official_cell_stack_limit > 0
  9351. && sd->bl.x == x+1 && sd->bl.y == y && map_count_oncell(sd->bl.m, x, y, BL_CHAR|BL_NPC, 1))
  9352. return;
  9353. // Cloaking wall check is actually updated when you click to process next movement
  9354. // not when you move each cell. This is official behaviour.
  9355. if (sd->sc.getSCE(SC_CLOAKING))
  9356. skill_check_cloaking(&sd->bl, sd->sc.getSCE(SC_CLOAKING));
  9357. status_change_end(&sd->bl, SC_ROLLINGCUTTER); // If you move, you lose your counters. [malufett]
  9358. status_change_end(&sd->bl, SC_CRESCIVEBOLT);
  9359. pc_delinvincibletimer(sd);
  9360. //Set last idle time... [Skotlex]
  9361. if (battle_config.idletime_option&IDLE_WALK)
  9362. sd->idletime = last_tick;
  9363. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_WALK)
  9364. sd->idletime_hom = last_tick;
  9365. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_WALK)
  9366. sd->idletime_mer = last_tick;
  9367. unit_walktoxy(&sd->bl, x, y, 4);
  9368. }
  9369. /// Notification about the result of a disconnect request (ZC_ACK_REQ_DISCONNECT).
  9370. /// 018b <result>.W
  9371. /// result:
  9372. /// 0 = disconnect (quit)
  9373. /// 1 = cannot disconnect (wait 10 seconds)
  9374. /// ? = ignored
  9375. void clif_disconnect_ack(map_session_data* sd, short result)
  9376. {
  9377. int fd = sd->fd;
  9378. WFIFOHEAD(fd,packet_len(0x18b));
  9379. WFIFOW(fd,0) = 0x18b;
  9380. WFIFOW(fd,2) = result;
  9381. WFIFOSET(fd,packet_len(0x18b));
  9382. }
  9383. /// Request to disconnect from server (CZ_REQ_DISCONNECT).
  9384. /// 018a <type>.W
  9385. /// type:
  9386. /// 0 = quit
  9387. void clif_parse_QuitGame(int fd, map_session_data *sd)
  9388. {
  9389. /* Rovert's prevent logout option fixed [Valaris] */
  9390. 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) &&
  9391. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9392. {
  9393. pc_damage_log_clear(sd,0);
  9394. clif_disconnect_ack(sd, 0);
  9395. flush_fifo( fd );
  9396. if( battle_config.drop_connection_on_quit ){
  9397. set_eof( fd );
  9398. }
  9399. } else {
  9400. clif_disconnect_ack(sd, 1);
  9401. }
  9402. }
  9403. /// Requesting unit's name.
  9404. /// 0094 <id>.L (CZ_REQNAME)
  9405. /// 0368 <id>.L (CZ_REQNAME2)
  9406. /// There are various variants of this packet, some of them have padding between fields.
  9407. void clif_parse_GetCharNameRequest(int fd, map_session_data *sd)
  9408. {
  9409. int id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9410. struct block_list* bl;
  9411. //status_change *sc;
  9412. if( id < 0 && -id == sd->bl.id ) // for disguises [Valaris]
  9413. id = sd->bl.id;
  9414. bl = map_id2bl(id);
  9415. if( bl == nullptr )
  9416. return; // Lagged clients could request names of already gone mobs/players. [Skotlex]
  9417. if( sd->bl.m != bl->m || !check_distance_bl(&sd->bl, bl, AREA_SIZE) )
  9418. return; // Block namerequests past view range
  9419. // 'see people in GM hide' cheat detection
  9420. /* disabled due to false positives (network lag + request name of char that's about to hide = race condition)
  9421. sc = status_get_sc(bl);
  9422. if (sc && sc->option&OPTION_INVISIBLE && !disguised(bl) &&
  9423. bl->type != BL_NPC && //Skip hidden NPCs which can be seen using Maya Purple
  9424. pc_get_group_level(sd) < battle_config.hack_info_GM_level
  9425. ) {
  9426. char gm_msg[256];
  9427. 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);
  9428. ShowWarning(gm_msg);
  9429. // information is sent to all online GMs
  9430. intif_wis_message_to_gm(wisp_server_name, battle_config.hack_info_GM_level, gm_msg);
  9431. return;
  9432. }
  9433. */
  9434. clif_name(&sd->bl,bl,SELF);
  9435. }
  9436. /// Validates and processes global messages
  9437. /// 008c <packet len>.W <text>.?B (<name> : <message>) 00 (CZ_REQUEST_CHAT)
  9438. /// There are various variants of this packet.
  9439. void clif_parse_GlobalMessage(int fd, map_session_data* sd)
  9440. {
  9441. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9442. size_t length;
  9443. // validate packet and retrieve name and message
  9444. if( !clif_process_message(sd, false, name, message, output ) )
  9445. return;
  9446. if( sd->gcbind && ((sd->gcbind->opt&CHAN_OPT_CAN_CHAT) || pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN)) ) {
  9447. channel_send(sd->gcbind,sd,message);
  9448. return;
  9449. }
  9450. // send message to others (using the send buffer for temp. storage)
  9451. clif_GlobalMessage( sd->bl, output, sd->chatID ? CHAT_WOS : AREA_CHAT_WOC );
  9452. length = strlen(output) + 1;
  9453. // send back message to the speaker
  9454. WFIFOHEAD(fd,4+length);
  9455. WFIFOW(fd,0) = 0x8e;
  9456. WFIFOW(fd,2) = (uint16)(4+length);
  9457. safestrncpy(WFIFOCP(fd,4), output, length );
  9458. WFIFOSET(fd, WFIFOW(fd,2));
  9459. #ifdef PCRE_SUPPORT
  9460. // trigger listening npcs
  9461. map_foreachinallrange(npc_chat_sub, &sd->bl, AREA_SIZE, BL_NPC, output, strlen(output), &sd->bl);
  9462. #endif
  9463. // Chat logging type 'O' / Global Chat
  9464. 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);
  9465. //achievement_update_objective(sd, AG_CHAT, 1, sd->bl.m); //! TODO: What's the official use of this achievement type?
  9466. }
  9467. /// /mm /mapmove (as @rura GM command) (CZ_MOVETO_MAP).
  9468. /// Request to warp to a map on given coordinates.
  9469. /// 0140 <map name>.16B <x>.W <y>.W
  9470. void clif_parse_MapMove(int fd, map_session_data *sd)
  9471. {
  9472. char command[MAP_NAME_LENGTH_EXT+25];
  9473. char* map_name;
  9474. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9475. map_name = RFIFOCP(fd,info->pos[0]);
  9476. map_name[MAP_NAME_LENGTH_EXT-1]='\0';
  9477. safesnprintf(command,sizeof(command),"%cmapmove %s %d %d", atcommand_symbol, map_name,
  9478. RFIFOW(fd,info->pos[1]), //x
  9479. RFIFOW(fd,info->pos[2])); //y
  9480. is_atcommand(fd, sd, command, 1);
  9481. }
  9482. /// Updates body and head direction of an object (ZC_CHANGE_DIRECTION).
  9483. /// 009c <id>.L <head dir>.W <dir>.B
  9484. /// head dir:
  9485. /// 0 = straight
  9486. /// 1 = turned CW
  9487. /// 2 = turned CCW
  9488. /// dir:
  9489. /// 0 = north
  9490. /// 1 = northwest
  9491. /// 2 = west
  9492. /// 3 = southwest
  9493. /// 4 = south
  9494. /// 5 = southeast
  9495. /// 6 = east
  9496. /// 7 = northeast
  9497. void clif_changed_dir(block_list& bl, enum send_target target){
  9498. PACKET_ZC_CHANGE_DIRECTION p{};
  9499. p.packetType = HEADER_ZC_CHANGE_DIRECTION;
  9500. p.srcId = bl.id;
  9501. if( bl.type == BL_PC ){
  9502. p.headDir = reinterpret_cast<map_session_data*>(&bl)->head_dir;
  9503. }else{
  9504. p.headDir = 0;
  9505. }
  9506. p.dir = unit_getdir(&bl);
  9507. clif_send(&p, sizeof(p), &bl, target);
  9508. if (disguised(&bl)) {
  9509. p.srcId = disguised_bl_id(bl.id);
  9510. p.headDir = 0;
  9511. clif_send(&p, sizeof(p), &bl, SELF);
  9512. }
  9513. }
  9514. /// Request to change own body and head direction.
  9515. /// 009b <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION)
  9516. /// 0361 <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION2)
  9517. /// There are various variants of this packet, some of them have padding between fields.
  9518. void clif_parse_ChangeDir(int fd, map_session_data *sd)
  9519. {
  9520. unsigned char headdir, dir;
  9521. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9522. headdir = RFIFOB(fd,info->pos[0]);
  9523. dir = RFIFOB(fd,info->pos[1]);
  9524. pc_setdir(sd, dir, headdir);
  9525. clif_changed_dir(sd->bl, AREA_WOS);
  9526. }
  9527. /// Request to show an emotion (CZ_REQ_EMOTION).
  9528. /// 00bf <type>.B
  9529. /// type:
  9530. /// @see enum emotion_type
  9531. void clif_parse_Emotion(int fd, map_session_data *sd){
  9532. if( sd == nullptr ){
  9533. return;
  9534. }
  9535. int emoticon = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9536. if (battle_config.basic_skill_check == 0 || pc_checkskill(sd, NV_BASIC) >= 2 || pc_checkskill(sd, SU_BASIC_SKILL) >= 1) {
  9537. if (emoticon == ET_CHAT_PROHIBIT) {// prevent use of the mute emote [Valaris]
  9538. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9539. return;
  9540. }
  9541. // fix flood of emotion icon (ro-proxy): flood only the hacker player
  9542. if (sd->emotionlasttime + 1 >= time(nullptr)) { // not more than 1 per second
  9543. sd->emotionlasttime = time(nullptr);
  9544. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9545. return;
  9546. }
  9547. sd->emotionlasttime = time(nullptr);
  9548. if (battle_config.idletime_option&IDLE_EMOTION)
  9549. sd->idletime = last_tick;
  9550. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_EMOTION)
  9551. sd->idletime_hom = last_tick;
  9552. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_EMOTION)
  9553. sd->idletime_mer = last_tick;
  9554. if (sd->state.block_action & PCBLOCK_EMOTION) {
  9555. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9556. return;
  9557. }
  9558. if(battle_config.client_reshuffle_dice && emoticon>=ET_DICE1 && emoticon<=ET_DICE6) {// re-roll dice
  9559. emoticon = rnd()%6+ET_DICE1;
  9560. }
  9561. clif_emotion(&sd->bl, emoticon);
  9562. } else
  9563. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9564. }
  9565. /// Amount of currently online players, reply to /w /who
  9566. /// 00c2 <count>.L (ZC_USER_COUNT)
  9567. static void clif_user_count(map_session_data& sd)
  9568. {
  9569. PACKET_ZC_USER_COUNT p{};
  9570. p.packetType = HEADER_ZC_USER_COUNT;
  9571. p.playersCount = map_getusers();
  9572. clif_send(&p,sizeof(p),&sd.bl,SELF);
  9573. }
  9574. /// /w /who (CZ_REQ_USER_COUNT).
  9575. /// Request to display amount of currently connected players.
  9576. /// 00c1
  9577. void clif_parse_HowManyConnections(int fd, map_session_data *sd) {
  9578. if( sd == nullptr ){
  9579. return;
  9580. }
  9581. clif_user_count(*sd);
  9582. }
  9583. void clif_parse_ActionRequest_sub( map_session_data& sd, uint8 action_type, int target_id, t_tick tick ){
  9584. if (pc_isdead(&sd)) {
  9585. clif_clearunit_area(sd.bl, CLR_DEAD);
  9586. return;
  9587. }
  9588. // Statuses that don't let the player sit / stand / talk with NPCs (targeted)
  9589. if (action_type != DMG_NORMAL && action_type != DMG_REPEAT) {
  9590. if (sd.sc.cant.interact)
  9591. return;
  9592. pc_stop_walking(&sd, 1);
  9593. }
  9594. pc_stop_attack(&sd);
  9595. if(target_id<0 && -target_id == sd.bl.id) // for disguises [Valaris]
  9596. target_id = sd.bl.id;
  9597. switch(action_type)
  9598. {
  9599. case DMG_NORMAL: // once attack
  9600. case DMG_REPEAT: // continuous attack
  9601. if( pc_cant_act(&sd) )
  9602. return;
  9603. if (!battle_config.sdelay_attack_enable && pc_checkskill(&sd, SA_FREECAST) <= 0) {
  9604. if (DIFF_TICK(tick, sd.ud.canact_tick) < 0) {
  9605. clif_skill_fail( sd, 1, USESKILL_FAIL_SKILLINTERVAL );
  9606. return;
  9607. }
  9608. }
  9609. pc_delinvincibletimer(&sd);
  9610. if (battle_config.idletime_option&IDLE_ATTACK)
  9611. sd.idletime = last_tick;
  9612. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_ATTACK)
  9613. sd.idletime_hom = last_tick;
  9614. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_ATTACK)
  9615. sd.idletime_mer = last_tick;
  9616. unit_attack(&sd.bl, target_id, action_type != 0);
  9617. break;
  9618. case DMG_SIT_DOWN: // sitdown
  9619. if (battle_config.basic_skill_check && pc_checkskill(&sd, NV_BASIC) < 3 && pc_checkskill(&sd, SU_BASIC_SKILL) < 1) {
  9620. clif_skill_fail( sd, 1, USESKILL_FAIL_LEVEL, 2 );
  9621. break;
  9622. }
  9623. if(pc_issit(&sd)) {
  9624. //Bugged client? Just refresh them.
  9625. clif_sitting(sd.bl);
  9626. return;
  9627. }
  9628. if (sd.ud.skilltimer != INVALID_TIMER || (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING))
  9629. break;
  9630. if (sd.sc.count && (
  9631. sd.sc.getSCE(SC_DANCING) ||
  9632. (sd.sc.getSCE(SC_GRAVITATION) && sd.sc.getSCE(SC_GRAVITATION)->val3 == BCT_SELF)
  9633. )) //No sitting during these states either.
  9634. break;
  9635. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9636. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9637. break;
  9638. }
  9639. if (battle_config.idletime_option&IDLE_SIT)
  9640. sd.idletime = last_tick;
  9641. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9642. sd.idletime_hom = last_tick;
  9643. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9644. sd.idletime_mer = last_tick;
  9645. pc_setsit(&sd);
  9646. skill_sit(&sd, true);
  9647. clif_sitting(sd.bl);
  9648. break;
  9649. case DMG_STAND_UP: // standup
  9650. if (!pc_issit(&sd)) {
  9651. //Bugged client? Just refresh them.
  9652. clif_standing(sd.bl);
  9653. return;
  9654. }
  9655. if (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING)
  9656. break;
  9657. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9658. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9659. break;
  9660. }
  9661. if (pc_setstand(&sd, false)) {
  9662. if (battle_config.idletime_option&IDLE_SIT)
  9663. sd.idletime = last_tick;
  9664. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9665. sd.idletime_hom = last_tick;
  9666. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9667. sd.idletime_mer = last_tick;
  9668. skill_sit(&sd, false);
  9669. clif_standing(sd.bl);
  9670. }
  9671. break;
  9672. }
  9673. }
  9674. /// Request for an action.
  9675. /// 0089 <target id>.L <action>.B (CZ_REQUEST_ACT)
  9676. /// 0437 <target id>.L <action>.B (CZ_REQUEST_ACT2)
  9677. /// action:
  9678. /// 0 = attack
  9679. /// 1 = pick up item
  9680. /// 2 = sit down
  9681. /// 3 = stand up
  9682. /// 7 = continous attack
  9683. /// 12 = (touch skill?)
  9684. /// There are various variants of this packet, some of them have padding between fields.
  9685. void clif_parse_ActionRequest(int fd, map_session_data *sd)
  9686. {
  9687. if( sd == nullptr ){
  9688. return;
  9689. }
  9690. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9691. clif_parse_ActionRequest_sub( *sd,
  9692. RFIFOB(fd,info->pos[1]),
  9693. RFIFOL(fd,info->pos[0]),
  9694. gettick()
  9695. );
  9696. }
  9697. /// Response to the death/system menu (CZ_RESTART).
  9698. /// 00b2 <type>.B
  9699. /// type:
  9700. /// 0 = restart (respawn)
  9701. /// 1 = char-select (disconnect)
  9702. void clif_parse_Restart(int fd, map_session_data *sd)
  9703. {
  9704. switch(RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0])) {
  9705. case 0x00:
  9706. pc_respawn(sd,CLR_OUTSIGHT);
  9707. break;
  9708. case 0x01:
  9709. /* Rovert's Prevent logout option - Fixed [Valaris] */
  9710. 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) &&
  9711. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9712. { //Send to char-server for character selection.
  9713. pc_damage_log_clear(sd,0);
  9714. chrif_charselectreq(sd, session[fd]->client_addr);
  9715. } else {
  9716. clif_disconnect_ack(sd, 1);
  9717. }
  9718. break;
  9719. }
  9720. }
  9721. /// Validates and processes whispered messages (CZ_WHISPER).
  9722. /// 0096 <packet len>.W <nick>.24B <message>.?B
  9723. void clif_parse_WisMessage(int fd, map_session_data* sd)
  9724. {
  9725. map_session_data* dstsd;
  9726. int i;
  9727. char target[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9728. // validate packet and retrieve name and message
  9729. if( !clif_process_message( sd, true, target, message, output ) )
  9730. return;
  9731. // Chat logging type 'W' / Whisper
  9732. 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);
  9733. //-------------------------------------------------------//
  9734. // Lordalfa - Paperboy - To whisper NPC commands //
  9735. //-------------------------------------------------------//
  9736. if (target[0] && (strncasecmp(target,"NPC:",4) == 0) && (strlen(target) > 4))
  9737. {
  9738. char* str = target+4; //Skip the NPC: string part.
  9739. struct npc_data* npc;
  9740. if ((npc = npc_name2id(str))) {
  9741. char split_data[NUM_WHISPER_VAR][CHAT_SIZE_MAX];
  9742. char *split;
  9743. char event[EVENT_NAME_LENGTH];
  9744. str = message;
  9745. // skip codepage indicator, if detected
  9746. if( str[0] == '|' && strlen(str) >= 4 )
  9747. str += 3;
  9748. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {// Splits the message using '#' as separators
  9749. split = strchr(str,'#');
  9750. if( split == nullptr ) { // use the remaining string
  9751. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9752. for( ++i; i < NUM_WHISPER_VAR; ++i )
  9753. split_data[i][0] = '\0';
  9754. break;
  9755. }
  9756. *split = '\0';
  9757. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9758. str = split+1;
  9759. }
  9760. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {
  9761. char variablename[CHAT_SIZE_MAX];
  9762. safesnprintf(variablename,sizeof(variablename),"@whispervar%d$", i);
  9763. set_var_str( sd, variablename, split_data[i] );
  9764. }
  9765. safesnprintf(event,sizeof(event),"%s::%s", npc->exname,script_config.onwhisper_event_name);
  9766. npc_event(sd,event,0); // Calls the NPC label
  9767. return;
  9768. }
  9769. } else if( target[0] == '#' ) {
  9770. struct Channel *channel = nullptr;
  9771. char* chname = target;
  9772. channel = channel_name2channel(chname,sd,3);
  9773. if(channel && (pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) || ((channel->opt&CHAN_OPT_CAN_CHAT) && channel_pccheckgroup(channel,sd->group_id)))){
  9774. if(channel_pc_haschan(sd,channel)>=0){ //we are in the chan
  9775. channel_send(channel,sd,message);
  9776. }
  9777. else if( channel->pass[0] == '\0') { //no pass needed
  9778. if (channel_join(channel,sd)==0)
  9779. channel_send(channel,sd,message); //join success
  9780. }
  9781. else {
  9782. clif_displaymessage(fd, msg_txt(sd,1402)); //You're not in that channel, type '@join <#channel_name>'
  9783. }
  9784. return;
  9785. }
  9786. }
  9787. // searching destination character
  9788. dstsd = map_nick2sd(target,false);
  9789. if (dstsd == nullptr || strcmp(dstsd->status.name, target) != 0) {
  9790. // player is not on this map-server
  9791. // At this point, don't send wisp/page if it's not exactly the same name, because (example)
  9792. // if there are 'Test' player on an other map-server and 'test' player on this map-server,
  9793. // and if we ask for 'Test', we must not contact 'test' player
  9794. // so, we send information to inter-server, which is the only one which decide (and copy correct name).
  9795. intif_wis_message(sd, target, message, strlen(message) + 1);
  9796. return;
  9797. }
  9798. // if player ignores everyone
  9799. if (dstsd->state.ignoreAll && pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9800. if (pc_isinvisible(dstsd) && pc_get_group_level(sd) < pc_get_group_level(dstsd))
  9801. clif_wis_end( *sd, ACKWHISPER_TARGET_OFFLINE );
  9802. else
  9803. clif_wis_end( *sd, ACKWHISPER_ALL_IGNORED );
  9804. return;
  9805. }
  9806. // if player is autotrading
  9807. if (dstsd->state.autotrade == 1){
  9808. safesnprintf(output,sizeof(output),"%s is in autotrade mode and cannot receive whispered messages.", dstsd->status.name);
  9809. clif_wis_message(sd, wisp_server_name, output, strlen(output) + 1, 0);
  9810. return;
  9811. }
  9812. if (pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9813. // if player ignores the source character
  9814. ARR_FIND(0, MAX_IGNORE_LIST, i, dstsd->ignore[i].name[0] == '\0' || strcmp(dstsd->ignore[i].name, sd->status.name) == 0);
  9815. if(i < MAX_IGNORE_LIST && dstsd->ignore[i].name[0] != '\0') { // source char present in ignore list
  9816. clif_wis_end( *sd, ACKWHISPER_IGNORED );
  9817. return;
  9818. }
  9819. }
  9820. // notify sender of success
  9821. clif_wis_end( *sd, ACKWHISPER_SUCCESS );
  9822. // Normal message
  9823. clif_wis_message(dstsd, sd->status.name, message, strlen(message)+1, 0);
  9824. }
  9825. /// /b /nb (CZ_BROADCAST).
  9826. /// Request to broadcast a message on whole server.
  9827. /// 0099 <packet len>.W <text>.?B 00
  9828. void clif_parse_Broadcast(int fd, map_session_data* sd) {
  9829. char command[CHAT_SIZE_MAX+11];
  9830. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9831. unsigned int len = RFIFOW(fd,info->pos[0])-4;
  9832. char* msg = RFIFOCP(fd,info->pos[1]);
  9833. // as the length varies depending on the command used, just block unreasonably long strings
  9834. mes_len_check(msg, len, CHAT_SIZE_MAX);
  9835. safesnprintf(command,sizeof(command),"%ckami %s", atcommand_symbol, msg);
  9836. is_atcommand(fd, sd, command, 1);
  9837. }
  9838. /// Request to pick up an item.
  9839. /// 009f <id>.L (CZ_ITEM_PICKUP)
  9840. /// 0362 <id>.L (CZ_ITEM_PICKUP2)
  9841. /// There are various variants of this packet, some of them have padding between fields.
  9842. void clif_parse_TakeItem(int fd, map_session_data *sd)
  9843. {
  9844. struct flooritem_data *fitem;
  9845. int map_object_id;
  9846. map_object_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9847. fitem = (struct flooritem_data*)map_id2bl(map_object_id);
  9848. do {
  9849. if (pc_isdead(sd)) {
  9850. clif_clearunit_area( sd->bl, CLR_DEAD );
  9851. break;
  9852. }
  9853. if (fitem == nullptr || fitem->bl.type != BL_ITEM || fitem->bl.m != sd->bl.m)
  9854. break;
  9855. if (pc_cant_act(sd))
  9856. break;
  9857. if (!pc_takeitem(sd, fitem))
  9858. break;
  9859. return;
  9860. } while (0);
  9861. // Client REQUIRES a fail packet or you can no longer pick items.
  9862. clif_additem(sd,0,0,6);
  9863. }
  9864. /// Request to drop an item.
  9865. /// 00a2 <index>.W <amount>.W (CZ_ITEM_THROW)
  9866. /// 0363 <index>.W <amount>.W (CZ_ITEM_THROW2)
  9867. /// There are various variants of this packet, some of them have padding between fields.
  9868. void clif_parse_DropItem(int fd, map_session_data *sd){
  9869. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9870. int item_index = RFIFOW(fd,info->pos[0]) -2;
  9871. int item_amount = RFIFOW(fd,info->pos[1]) ;
  9872. for(;;) {
  9873. if (pc_isdead(sd))
  9874. break;
  9875. if (pc_cant_act2(sd) || sd->npc_id)
  9876. break;
  9877. if (sd->sc.cant.drop)
  9878. break;
  9879. if (!pc_dropitem(sd, item_index, item_amount))
  9880. break;
  9881. if (battle_config.idletime_option&IDLE_DROPITEM)
  9882. sd->idletime = last_tick;
  9883. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_DROPITEM)
  9884. sd->idletime_hom = last_tick;
  9885. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_DROPITEM)
  9886. sd->idletime_mer = last_tick;
  9887. return;
  9888. }
  9889. //Because the client does not like being ignored.
  9890. clif_dropitem( *sd, item_index, 0 );
  9891. }
  9892. /// Request to use an item.
  9893. /// 00a7 <index>.W <account id>.L (CZ_USE_ITEM)
  9894. /// 0439 <index>.W <account id>.L (CZ_USE_ITEM2)
  9895. /// There are various variants of this packet, some of them have padding between fields.
  9896. void clif_parse_UseItem(int fd, map_session_data *sd)
  9897. {
  9898. int n;
  9899. if (pc_isdead(sd)) {
  9900. clif_clearunit_area( sd->bl, CLR_DEAD );
  9901. return;
  9902. }
  9903. if ( (!sd->npc_id && pc_istrading(sd)) || sd->chatID || (sd->state.block_action & PCBLOCK_USEITEM) ) {
  9904. clif_msg(sd, MSI_BUSY);
  9905. return;
  9906. }
  9907. //Whether the item is used or not is irrelevant, the char ain't idle. [Skotlex]
  9908. if (battle_config.idletime_option&IDLE_USEITEM)
  9909. sd->idletime = last_tick;
  9910. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9911. sd->idletime_hom = last_tick;
  9912. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  9913. sd->idletime_mer = last_tick;
  9914. n = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  9915. if(n <0 || n >= MAX_INVENTORY)
  9916. return;
  9917. if (!pc_useitem(sd,n))
  9918. clif_useitemack(sd,n,0,false); //Send an empty ack packet or the client gets stuck.
  9919. }
  9920. /// Request to equip an item
  9921. /// 00a9 <index>.W <position>.W (CZ_REQ_WEAR_EQUIP).
  9922. /// 0998 <index>.W <position>.L (CZ_REQ_WEAR_EQUIP_V5)
  9923. void clif_parse_EquipItem(int fd,map_session_data *sd)
  9924. {
  9925. int index;
  9926. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9927. if(pc_isdead(sd)) {
  9928. clif_clearunit_area( sd->bl, CLR_DEAD );
  9929. return;
  9930. }
  9931. index = RFIFOW(fd,info->pos[0])-2;
  9932. if (index < 0 || index >= MAX_INVENTORY)
  9933. return; //Out of bounds check.
  9934. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  9935. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  9936. return;
  9937. } else if (sd->state.storage_flag || sd->sc.opt1)
  9938. ; //You can equip/unequip stuff while storage is open/under status changes
  9939. else if (pc_cant_act2(sd))
  9940. return;
  9941. if(!sd->inventory.u.items_inventory[index].identify) {
  9942. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, index ); // fail
  9943. return;
  9944. }
  9945. if(!sd->inventory_data[index])
  9946. return;
  9947. if(sd->inventory_data[index]->type == IT_PETARMOR){
  9948. pet_equipitem(sd,index);
  9949. return;
  9950. }
  9951. if (battle_config.idletime_option&IDLE_USEITEM)
  9952. sd->idletime = last_tick;
  9953. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9954. sd->idletime_hom = last_tick;
  9955. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  9956. sd->idletime_mer = last_tick;
  9957. //Client doesn't send the position for ammo.
  9958. if(sd->inventory_data[index]->type == IT_AMMO)
  9959. pc_equipitem(sd,index,EQP_AMMO);
  9960. else {
  9961. int req_pos;
  9962. #if PACKETVER >= 20120925
  9963. req_pos = RFIFOL(fd,info->pos[1]);
  9964. #else
  9965. req_pos = (int)RFIFOW(fd,info->pos[1]);
  9966. #endif
  9967. pc_equipitem(sd,index,req_pos);
  9968. }
  9969. }
  9970. /// Request to take off an equip (CZ_REQ_TAKEOFF_EQUIP).
  9971. /// 00ab <index>.W
  9972. void clif_parse_UnequipItem(int fd,map_session_data *sd)
  9973. {
  9974. int index;
  9975. if(pc_isdead(sd)) {
  9976. clif_clearunit_area( sd->bl, CLR_DEAD );
  9977. return;
  9978. }
  9979. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  9980. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  9981. return;
  9982. } else if (sd->state.storage_flag || sd->sc.opt1)
  9983. ; //You can equip/unequip stuff while storage is open/under status changes
  9984. else if (pc_cant_act2(sd))
  9985. return;
  9986. index = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  9987. if (battle_config.idletime_option&IDLE_USEITEM)
  9988. sd->idletime = last_tick;
  9989. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9990. sd->idletime_hom = last_tick;
  9991. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  9992. sd->idletime_mer = last_tick;
  9993. pc_unequipitem(sd,index,1);
  9994. }
  9995. /// Request to start a conversation with an NPC (CZ_CONTACTNPC).
  9996. /// 0090 <id>.L <type>.B
  9997. /// type:
  9998. /// 1 = click
  9999. void clif_parse_NpcClicked(int fd,map_session_data *sd)
  10000. {
  10001. if( sd == nullptr ){
  10002. return;
  10003. }
  10004. struct block_list *bl;
  10005. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10006. if(pc_isdead(sd)) {
  10007. clif_clearunit_area( sd->bl, CLR_DEAD );
  10008. return;
  10009. }
  10010. if( pc_cant_act2(sd) || sd->npc_id || pc_hasprogress( sd, WIP_DISABLE_NPC ) ){
  10011. #ifdef RENEWAL
  10012. clif_msg( sd, MSI_BUSY);
  10013. #endif
  10014. return;
  10015. }
  10016. if( sd->state.mail_writing )
  10017. return;
  10018. bl = map_id2bl(RFIFOL(fd,info->pos[0]));
  10019. //type = RFIFOB(fd,info->pos[1]);
  10020. if (!bl) return;
  10021. switch (bl->type) {
  10022. case BL_MOB:
  10023. case BL_PC:
  10024. clif_parse_ActionRequest_sub( *sd, DMG_REPEAT, bl->id, gettick() );
  10025. break;
  10026. case BL_NPC:
  10027. #ifdef RENEWAL
  10028. 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
  10029. clif_msg(sd, MSI_BUSY);
  10030. break;
  10031. }
  10032. #endif
  10033. if( bl->m != -1 ){ // the user can't click floating npcs directly (hack attempt)
  10034. struct npc_data* nd = (struct npc_data*)bl;
  10035. // Progressbar is running
  10036. if( nd->progressbar.timeout > 0 ){
  10037. return;
  10038. }
  10039. npc_click(sd,nd);
  10040. }
  10041. break;
  10042. }
  10043. }
  10044. /// Selection between buy/sell was made (CZ_ACK_SELECT_DEALTYPE).
  10045. /// 00c5 <id>.L <type>.B
  10046. /// type:
  10047. /// 0 = buy
  10048. /// 1 = sell
  10049. void clif_parse_NpcBuySellSelected(int fd,map_session_data *sd)
  10050. {
  10051. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10052. if (sd->state.trading)
  10053. return;
  10054. npc_buysellsel(sd,RFIFOL(fd,info->pos[0]),RFIFOB(fd,info->pos[1]));
  10055. }
  10056. /// Notification about the result of a purchase attempt from an NPC shop (ZC_PC_PURCHASE_RESULT).
  10057. /// 00ca <result>.B
  10058. /// result:
  10059. /// 0 = "The deal has successfully completed."
  10060. /// 1 = "You do not have enough zeny."
  10061. /// 2 = "You are over your Weight Limit."
  10062. /// 3 = "Out of the maximum capacity, you have too many items."
  10063. /// 9 = "Amounts are exceeded the possession of the item is not available for purchase."
  10064. /// 10 = "Props open-air store sales will be traded in RODEX"
  10065. /// 11 = "The exchange failed."
  10066. /// 12 = "The exchange was well done."
  10067. /// 13 = "The item is already sold and out of stock."
  10068. /// 14 = "There is not enough goods to exchange."
  10069. void clif_npc_buy_result( map_session_data* sd, e_purchase_result result ){
  10070. PACKET_ZC_PC_PURCHASE_RESULT p = {};
  10071. p.packetType = HEADER_ZC_PC_PURCHASE_RESULT;
  10072. p.result = (uint8)result;
  10073. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  10074. }
  10075. /// Request to buy chosen items from npc shop.
  10076. /// 00c8 <packet len>.W { <amount>.W <name id>.W }* (CZ_PC_PURCHASE_ITEMLIST)
  10077. void clif_parse_NpcBuyListSend( int fd, map_session_data* sd ){
  10078. const PACKET_CZ_PC_PURCHASE_ITEMLIST *p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10079. uint16 n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->items[0] );
  10080. e_purchase_result result;
  10081. if( sd->state.trading || !sd->npc_shopid )
  10082. result = e_purchase_result::PURCHASE_FAIL_MONEY;
  10083. else{
  10084. std::vector<s_npc_buy_list> items = {};
  10085. items.reserve( n );
  10086. for( uint16 i = 0; i < n; i++ ){
  10087. s_npc_buy_list item = {};
  10088. item.nameid = p->items[i].itemId;
  10089. item.qty = p->items[i].amount;
  10090. items.push_back( item );
  10091. }
  10092. result = npc_buylist( sd, items );
  10093. }
  10094. sd->npc_shopid = 0; //Clear shop data.
  10095. clif_npc_buy_result(sd, result);
  10096. }
  10097. /// Notification about the result of a sell attempt to an NPC shop (ZC_PC_SELL_RESULT).
  10098. /// 00cb <result>.B
  10099. /// result:
  10100. /// 0 = "The deal has successfully completed."
  10101. /// 1 = "The deal has failed."
  10102. void clif_npc_sell_result(map_session_data* sd, unsigned char result)
  10103. {
  10104. int fd = sd->fd;
  10105. WFIFOHEAD(fd,packet_len(0xcb));
  10106. WFIFOW(fd,0) = 0xcb;
  10107. WFIFOB(fd,2) = result;
  10108. WFIFOSET(fd,packet_len(0xcb));
  10109. }
  10110. /// Request to sell chosen items to npc shop (CZ_PC_SELL_ITEMLIST).
  10111. /// 00c9 <packet len>.W { <index>.W <amount>.W }*
  10112. void clif_parse_NpcSellListSend(int fd,map_session_data *sd)
  10113. {
  10114. int fail=0;
  10115. const PACKET_CZ_PC_SELL_ITEMLIST* p = reinterpret_cast<PACKET_CZ_PC_SELL_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10116. int n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->sellList[0] );
  10117. if (sd->state.trading || !sd->npc_shopid)
  10118. fail = 1;
  10119. else
  10120. fail = npc_selllist(sd, n, p->sellList);
  10121. sd->npc_shopid = 0; //Clear shop data.
  10122. clif_npc_sell_result(sd, fail);
  10123. }
  10124. /// Chatroom creation request (CZ_CREATE_CHATROOM).
  10125. /// 00d5 <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B
  10126. /// type:
  10127. /// 0 = private
  10128. /// 1 = public
  10129. void clif_parse_CreateChatRoom(int fd, map_session_data* sd)
  10130. {
  10131. if( sd == nullptr ){
  10132. return;
  10133. }
  10134. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10135. int len = RFIFOW(fd,info->pos[0])-15;
  10136. int limit = RFIFOW(fd,info->pos[1]);
  10137. bool pub = (RFIFOB(fd,info->pos[2]) != 0);
  10138. const char* password = RFIFOCP(fd,info->pos[3]); //not zero-terminated
  10139. const char* title = RFIFOCP(fd,info->pos[4]); // not zero-terminated
  10140. char s_password[CHATROOM_PASS_SIZE];
  10141. char s_title[CHATROOM_TITLE_SIZE];
  10142. if (sd->sc.getSCE(SC_NOCHAT) && sd->sc.getSCE(SC_NOCHAT)->val1&MANNER_NOROOM)
  10143. return;
  10144. if(battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 4 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10145. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 3 );
  10146. return;
  10147. }
  10148. if( npc_isnear(&sd->bl) ) {
  10149. // uncomment to send msg_txt.
  10150. //char output[150];
  10151. //sprintf(output, msg_txt(662), battle_config.min_npc_vendchat_distance);
  10152. //clif_displaymessage(sd->fd, output);
  10153. clif_skill_fail( *sd, 1, USESKILL_FAIL_THERE_ARE_NPC_AROUND );
  10154. return;
  10155. }
  10156. if( len <= 0 )
  10157. return; // invalid input
  10158. safestrncpy(s_password, password, CHATROOM_PASS_SIZE);
  10159. safestrncpy(s_title, title, min(len+1,CHATROOM_TITLE_SIZE)); //NOTE: assumes that safestrncpy will not access the len+1'th byte
  10160. chat_createpcchat(sd, s_title, s_password, limit, pub);
  10161. }
  10162. /// Chatroom join request (CZ_REQ_ENTER_ROOM).
  10163. /// 00d9 <chat ID>.L <passwd>.8B
  10164. void clif_parse_ChatAddMember(int fd, map_session_data* sd){
  10165. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10166. int chatid = RFIFOL(fd,info->pos[0]);
  10167. const char* password = RFIFOCP(fd,info->pos[1]); // not zero-terminated
  10168. chat_joinchat(sd,chatid,password);
  10169. }
  10170. /// Chatroom properties adjustment request (CZ_CHANGE_CHATROOM).
  10171. /// 00de <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B
  10172. /// type:
  10173. /// 0 = private
  10174. /// 1 = public
  10175. void clif_parse_ChatRoomStatusChange(int fd, map_session_data* sd){
  10176. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10177. int len = RFIFOW(fd,info->pos[0])-15;
  10178. int limit = RFIFOW(fd,info->pos[1]);
  10179. bool pub = (RFIFOB(fd,info->pos[2]) != 0);
  10180. const char* password = RFIFOCP(fd,info->pos[3]); // not zero-terminated
  10181. const char* title = RFIFOCP(fd,info->pos[4]); // not zero-terminated
  10182. char s_password[CHATROOM_PASS_SIZE];
  10183. char s_title[CHATROOM_TITLE_SIZE];
  10184. if( len <= 0 )
  10185. return; // invalid input
  10186. safestrncpy(s_password, password, CHATROOM_PASS_SIZE);
  10187. safestrncpy(s_title, title, min(len+1,CHATROOM_TITLE_SIZE)); //NOTE: assumes that safestrncpy will not access the len+1'th byte
  10188. chat_changechatstatus(sd, s_title, s_password, limit, pub);
  10189. }
  10190. /// Request to change the chat room ownership (CZ_REQ_ROLE_CHANGE).
  10191. /// 00e0 <role>.L <nick>.24B
  10192. /// role:
  10193. /// 0 = owner
  10194. /// 1 = normal
  10195. void clif_parse_ChangeChatOwner(int fd, map_session_data* sd)
  10196. {
  10197. //int role = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10198. chat_changechatowner(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]));
  10199. }
  10200. /// Request to expel a player from chat room (CZ_REQ_EXPEL_MEMBER).
  10201. /// 00e2 <name>.24B
  10202. void clif_parse_KickFromChat(int fd,map_session_data *sd)
  10203. {
  10204. chat_kickchat(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10205. }
  10206. /// Request to leave the current chatroom (CZ_EXIT_ROOM).
  10207. /// 00e3
  10208. void clif_parse_ChatLeave(int fd, map_session_data* sd)
  10209. {
  10210. chat_leavechat(sd,0);
  10211. }
  10212. // Handles notifying asker and rejecter of what has just ocurred.
  10213. // Type is used to determine the correct msg_txt to use
  10214. void clif_noask_sub( map_session_data& sd, map_session_data& tsd, int type ){
  10215. char output[CHAT_SIZE_MAX];
  10216. // Your request has been rejected by autoreject option.
  10217. clif_messagecolor( &sd.bl, color_table[COLOR_LIGHT_GREEN], msg_txt( &sd, 392 ), false, SELF );
  10218. // Notice that a request was rejected.
  10219. safesnprintf( output, CHAT_SIZE_MAX, msg_txt( &tsd, type ), sd.status.name );
  10220. clif_messagecolor( &tsd.bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  10221. }
  10222. /// Request to begin a trade (CZ_REQ_EXCHANGE_ITEM).
  10223. /// 00e4 <account id>.L
  10224. void clif_parse_TradeRequest(int fd,map_session_data *sd)
  10225. {
  10226. map_session_data *t_sd;
  10227. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10228. if(!sd->chatID && pc_cant_act(sd))
  10229. return; //You can trade while in a chatroom.
  10230. if(t_sd){
  10231. // @noask [LuzZza]
  10232. if(t_sd->state.noask) {
  10233. clif_noask_sub( *sd, *t_sd, 393 ); // Autorejected trade request from %s.
  10234. return;
  10235. }
  10236. if (t_sd->state.mail_writing) {
  10237. // Fake trading
  10238. clif_traderesponse(*sd,TRADE_ACK_BUSY);
  10239. return;
  10240. }
  10241. }
  10242. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 1 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10243. clif_skill_fail( *sd, 1 );
  10244. return;
  10245. }
  10246. trade_traderequest(sd,t_sd);
  10247. }
  10248. /// Answer to a trade request (CZ_ACK_EXCHANGE_ITEM).
  10249. /// 00e6 <result>.B
  10250. /// result:
  10251. /// 3 = accepted
  10252. /// 4 = rejected
  10253. void clif_parse_TradeAck(int fd,map_session_data *sd)
  10254. {
  10255. trade_tradeack(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10256. }
  10257. /// Request to add an item to current trade (CZ_ADD_EXCHANGE_ITEM).
  10258. /// 00e8 <index>.W <amount>.L
  10259. void clif_parse_TradeAddItem(int fd,map_session_data *sd)
  10260. {
  10261. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10262. short index = RFIFOW(fd,info->pos[0]);
  10263. int amount = RFIFOL(fd,info->pos[1]);
  10264. if( index == 0 )
  10265. trade_tradeaddzeny(sd, amount);
  10266. else
  10267. trade_tradeadditem(sd, server_index(index), (short)amount);
  10268. }
  10269. /// Request to lock items in current trade (CZ_CONCLUDE_EXCHANGE_ITEM).
  10270. /// 00eb
  10271. void clif_parse_TradeOk(int fd,map_session_data *sd)
  10272. {
  10273. trade_tradeok(sd);
  10274. }
  10275. /// Request to cancel current trade (CZ_CANCEL_EXCHANGE_ITEM).
  10276. /// 00ed
  10277. void clif_parse_TradeCancel(int fd,map_session_data *sd)
  10278. {
  10279. trade_tradecancel(sd);
  10280. }
  10281. /// Request to commit current trade (CZ_EXEC_EXCHANGE_ITEM).
  10282. /// 00ef
  10283. void clif_parse_TradeCommit(int fd,map_session_data *sd)
  10284. {
  10285. trade_tradecommit(sd);
  10286. }
  10287. /// Request to stop chasing/attacking an unit (CZ_CANCEL_LOCKON).
  10288. /// 0118
  10289. void clif_parse_StopAttack(int fd,map_session_data *sd)
  10290. {
  10291. pc_stop_attack(sd);
  10292. if (sd) sd->ud.state.attack_continue = 0;
  10293. }
  10294. /// Request to move an item from inventory to cart.
  10295. /// 0126 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_CART)
  10296. void clif_parse_PutItemToCart( int fd, map_session_data *sd ){
  10297. if (pc_istrading(sd) || !pc_iscarton(sd) || pc_cant_act2(sd))
  10298. return;
  10299. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10300. return;
  10301. const PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART* p = reinterpret_cast<PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART*>( RFIFOP( fd, 0 ) );
  10302. pc_putitemtocart( sd, server_index( p->index ), p->count);
  10303. }
  10304. /// Request to move an item from cart to inventory (CZ_MOVE_ITEM_FROM_CART_TO_BODY).
  10305. /// 0127 <index>.W <amount>.L
  10306. void clif_parse_GetItemFromCart(int fd,map_session_data *sd)
  10307. {
  10308. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10309. if (!pc_iscarton(sd) || pc_cant_act2(sd))
  10310. return;
  10311. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10312. return;
  10313. pc_getitemfromcart(sd,RFIFOW(fd,info->pos[0])-2,RFIFOL(fd,info->pos[1]));
  10314. }
  10315. /// Request to remove cart/falcon/peco/dragon/mado (CZ_REQ_CARTOFF).
  10316. /// 012a
  10317. void clif_parse_RemoveOption(int fd,map_session_data *sd)
  10318. {
  10319. if( !(sd->sc.option&(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR))
  10320. #ifdef NEW_CARTS
  10321. && sd->sc.getSCE(SC_PUSH_CART) )
  10322. pc_setcart(sd,0);
  10323. #else
  10324. )
  10325. pc_setoption(sd,sd->sc.option&~OPTION_CART);
  10326. #endif
  10327. else // priority to remove this option before we can clear cart
  10328. pc_setoption(sd,sd->sc.option&~(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR));
  10329. }
  10330. /// Request to select cart's visual look for new cart design (ZC_SELECTCART).
  10331. /// 097f <Length>.W <identity>.L <type>.B
  10332. void clif_SelectCart(map_session_data *sd) {
  10333. #if PACKETVER >= 20150826
  10334. int i = 0, carts = 3;
  10335. int fd = sd->fd;
  10336. WFIFOHEAD(fd,8 + carts);
  10337. WFIFOW(fd,0) = 0x97f;
  10338. WFIFOW(fd,2) = 8 + carts;
  10339. WFIFOL(fd,4) = sd->status.account_id;
  10340. // Right now we have 10-12, tested it you can also enable selection for all cart styles here(1-12)
  10341. for( i = 0; i < carts; i++ ) {
  10342. WFIFOB(fd,8 + i) = 10 + i;
  10343. }
  10344. WFIFOSET(fd,8 + carts);
  10345. #endif
  10346. }
  10347. /// Request to select cart's visual look for new cart design (CZ_SELECTCART).
  10348. /// 0980 <identity>.L <type>.B
  10349. void clif_parse_SelectCart(int fd,map_session_data *sd) {
  10350. #if PACKETVER >= 20150826
  10351. int type;
  10352. // Check identity
  10353. if( !sd || pc_checkskill(sd,MC_CARTDECORATE) < 1 || RFIFOL(fd,2) != sd->status.account_id )
  10354. return;
  10355. type = (int)RFIFOB(fd,6);
  10356. // Check type
  10357. if( type < 10 || type > 12 )
  10358. return;
  10359. pc_setcart(sd, type);
  10360. #endif
  10361. }
  10362. /// Request to change cart's visual look (CZ_REQ_CHANGECART).
  10363. /// 01af <num>.W
  10364. void clif_parse_ChangeCart(int fd,map_session_data *sd)
  10365. {// TODO: State tracking?
  10366. int type;
  10367. if( !sd || pc_checkskill(sd, MC_CHANGECART) < 1 )
  10368. return;
  10369. #ifdef RENEWAL
  10370. if (sd->npc_id || pc_hasprogress(sd, WIP_DISABLE_SKILLITEM)) {
  10371. clif_msg(sd, MSI_BUSY);
  10372. return;
  10373. }
  10374. #endif
  10375. type = (int)RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10376. if(
  10377. #ifdef NEW_CARTS
  10378. #if PACKETVER >= 20191106
  10379. (type == 13 && sd->status.base_level > 100) ||
  10380. #endif
  10381. (type == 9 && sd->status.base_level > 130) ||
  10382. (type == 8 && sd->status.base_level > 120) ||
  10383. (type == 7 && sd->status.base_level > 110) ||
  10384. (type == 6 && sd->status.base_level > 100) ||
  10385. #endif
  10386. (type == 5 && sd->status.base_level > 90) ||
  10387. (type == 4 && sd->status.base_level > 80) ||
  10388. (type == 3 && sd->status.base_level > 65) ||
  10389. (type == 2 && sd->status.base_level > 40) ||
  10390. (type == 1))
  10391. pc_setcart(sd, type);
  10392. }
  10393. /// Request to increase status (CZ_STATUS_CHANGE).
  10394. /// 00bb <status id>.W <amount>.B
  10395. /// status id:
  10396. /// SP_STR ~ SP_LUK
  10397. /// amount:
  10398. /// Old clients always send 1 for this, even when using /str+ and the like.
  10399. /// Newer clients (2013-12-23 and newer) send the correct amount.
  10400. void clif_parse_StatusUp(int fd,map_session_data *sd)
  10401. {
  10402. int increase_amount = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  10403. if( increase_amount < 0 ) {
  10404. ShowDebug("clif_parse_StatusUp: Negative 'increase' value sent by client! (fd: %d, value: %d)\n",
  10405. fd, increase_amount);
  10406. }
  10407. pc_statusup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]),increase_amount);
  10408. }
  10409. /// Request to increase trait status.
  10410. /// 0b24 <status id>.W <amount>.W (CZ_ADVANCED_STATUS_CHANGE)
  10411. /// status id:
  10412. /// SP_POW ~ SP_CON
  10413. /// amount:
  10414. /// The amount to increase the trait status
  10415. void clif_parse_traitstatus_up( int fd, map_session_data *sd ){
  10416. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10417. const PACKET_CZ_ADVANCED_STATUS_CHANGE* p = reinterpret_cast<PACKET_CZ_ADVANCED_STATUS_CHANGE*>( RFIFOP( fd, 0 ) );
  10418. if( p->amount < 0 ){
  10419. 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 );
  10420. return;
  10421. }
  10422. pc_traitstatusup( sd, p->type, p->amount );
  10423. #endif
  10424. }
  10425. /// Request to increase level of a skill (CZ_UPGRADE_SKILLLEVEL).
  10426. /// 0112 <skill id>.W
  10427. void clif_parse_SkillUp(int fd,map_session_data *sd)
  10428. {
  10429. pc_skillup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10430. }
  10431. static void clif_parse_UseSkillToId_homun(struct homun_data *hd, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, int target_id)
  10432. {
  10433. int lv;
  10434. if( !hd )
  10435. return;
  10436. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10437. clif_emotion(&hd->bl, ET_THINK);
  10438. return;
  10439. }
  10440. if( hd->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10441. target_id = hd->bl.id;
  10442. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10443. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10444. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10445. clif_emotion(&hd->bl, ET_THINK);
  10446. if (hd->master)
  10447. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10448. return;
  10449. }
  10450. lv = hom_checkskill(hd, skill_id);
  10451. if( skill_lv > lv )
  10452. skill_lv = lv;
  10453. if( skill_lv )
  10454. unit_skilluse_id(&hd->bl, target_id, skill_id, skill_lv);
  10455. }
  10456. static void clif_parse_UseSkillToPos_homun(struct homun_data *hd, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, short x, short y, int skillmoreinfo)
  10457. {
  10458. int lv;
  10459. if( !hd )
  10460. return;
  10461. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10462. clif_emotion(&hd->bl, ET_THINK);
  10463. return;
  10464. }
  10465. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10466. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10467. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10468. clif_emotion(&hd->bl, ET_THINK);
  10469. if (hd->master)
  10470. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10471. return;
  10472. }
  10473. #ifdef RENEWAL
  10474. if (hd->sc.getSCE(SC_BASILICA_CELL))
  10475. #else
  10476. if (hd->sc.getSCE(SC_BASILICA))
  10477. #endif
  10478. return;
  10479. lv = hom_checkskill(hd, skill_id);
  10480. if( skill_lv > lv )
  10481. skill_lv = lv;
  10482. if( skill_lv )
  10483. unit_skilluse_pos(&hd->bl, x, y, skill_id, skill_lv);
  10484. }
  10485. static void clif_parse_UseSkillToId_mercenary(s_mercenary_data *md, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, int target_id)
  10486. {
  10487. int lv;
  10488. if( !md )
  10489. return;
  10490. if( skill_isNotOk_mercenary(skill_id, *md) )
  10491. return;
  10492. if( md->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10493. target_id = md->bl.id;
  10494. if( md->ud.skilltimer != INVALID_TIMER )
  10495. {
  10496. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10497. }
  10498. else if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10499. return;
  10500. lv = mercenary_checkskill(md, skill_id);
  10501. if( skill_lv > lv )
  10502. skill_lv = lv;
  10503. if( skill_lv )
  10504. unit_skilluse_id(&md->bl, target_id, skill_id, skill_lv);
  10505. }
  10506. static void clif_parse_UseSkillToPos_mercenary(s_mercenary_data *md, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, short x, short y, int skillmoreinfo)
  10507. {
  10508. int lv;
  10509. if( !md )
  10510. return;
  10511. if( skill_isNotOk_mercenary(skill_id, *md) )
  10512. return;
  10513. if( md->ud.skilltimer != INVALID_TIMER )
  10514. return;
  10515. if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10516. {
  10517. if (md->master)
  10518. clif_skill_fail( *md->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10519. return;
  10520. }
  10521. #ifdef RENEWAL
  10522. if (md->sc.getSCE(SC_BASILICA_CELL))
  10523. #else
  10524. if (md->sc.getSCE(SC_BASILICA))
  10525. #endif
  10526. return;
  10527. lv = mercenary_checkskill(md, skill_id);
  10528. if( skill_lv > lv )
  10529. skill_lv = lv;
  10530. if( skill_lv )
  10531. unit_skilluse_pos(&md->bl, x, y, skill_id, skill_lv);
  10532. }
  10533. void clif_parse_skill_toid( map_session_data* sd, uint16 skill_id, uint16 skill_lv, int target_id ){
  10534. if( sd == nullptr ){
  10535. return;
  10536. }
  10537. t_tick tick = gettick();
  10538. if( skill_lv < 1 ) skill_lv = 1; //No clue, I have seen the client do this with guild skills :/ [Skotlex]
  10539. int inf = skill_get_inf(skill_id);
  10540. if (inf&INF_GROUND_SKILL || !inf)
  10541. return; //Using a ground/passive skill on a target? WRONG.
  10542. if (sd->state.block_action & PCBLOCK_SKILL) {
  10543. clif_msg(sd, MSI_BUSY);
  10544. return;
  10545. }
  10546. if( SKILL_CHK_HOMUN(skill_id) ) {
  10547. clif_parse_UseSkillToId_homun(sd->hd, sd, tick, skill_id, skill_lv, target_id);
  10548. return;
  10549. }
  10550. if( SKILL_CHK_MERC(skill_id) ) {
  10551. clif_parse_UseSkillToId_mercenary(sd->md, sd, tick, skill_id, skill_lv, target_id);
  10552. return;
  10553. }
  10554. // Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10555. // This is done here, because homunculi and mercenaries can be triggered by AI and not by the player itself
  10556. if (battle_config.idletime_option&IDLE_USESKILLTOID)
  10557. sd->idletime = last_tick;
  10558. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USESKILLTOID)
  10559. sd->idletime_hom = last_tick;
  10560. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USESKILLTOID)
  10561. sd->idletime_mer = last_tick;
  10562. if( sd->npc_id ){
  10563. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag || !( inf & INF_SELF_SKILL ) ){
  10564. #ifdef RENEWAL
  10565. clif_msg( sd, MSI_BUSY);
  10566. #endif
  10567. return;
  10568. }
  10569. }
  10570. if ((pc_cant_act2(sd) || sd->chatID) &&
  10571. skill_id != RK_REFRESH &&
  10572. !( ( skill_id == SR_GENTLETOUCH_CURE || skill_id == SU_GROOMING ) && (sd->sc.opt1 == OPT1_STONE || sd->sc.opt1 == OPT1_FREEZE || sd->sc.opt1 == OPT1_STUN)) &&
  10573. !(sd->state.storage_flag && (inf&INF_SELF_SKILL))) //SELF skills can be used with the storage open, issue: 8027
  10574. return;
  10575. if( pc_issit(sd) )
  10576. return;
  10577. if( skill_isNotOk(skill_id, *sd) )
  10578. return;
  10579. if( sd->bl.id != target_id && inf&INF_SELF_SKILL )
  10580. target_id = sd->bl.id; // never trust the client
  10581. if( target_id < 0 && -target_id == sd->bl.id ) // for disguises [Valaris]
  10582. target_id = sd->bl.id;
  10583. if( sd->ud.skilltimer != INVALID_TIMER ) {
  10584. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST )
  10585. return;
  10586. } else if( DIFF_TICK(tick, sd->ud.canact_tick) < 0 ) {
  10587. if( sd->skillitem != skill_id ) {
  10588. clif_skill_fail( *sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10589. return;
  10590. }
  10591. }
  10592. if( sd->sc.option&OPTION_COSTUME )
  10593. return;
  10594. #ifndef RENEWAL
  10595. if( sd->sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd->sc.getSCE(SC_BASILICA)->val4 != sd->bl.id) )
  10596. return; // On basilica only caster can use Basilica again to stop it.
  10597. #endif
  10598. if( sd->menuskill_id ) {
  10599. if( sd->menuskill_id == SA_TAMINGMONSTER ) {
  10600. clif_menuskill_clear(sd); //Cancel pet capture.
  10601. }else if( sd->menuskill_id == SG_FEEL ){
  10602. clif_menuskill_clear( sd ); // Cancel selection
  10603. }else if( sd->menuskill_id != SA_AUTOSPELL )
  10604. return; //Can't use skills while a menu is open.
  10605. }
  10606. if( sd->skillitem == skill_id ) {
  10607. if( skill_lv != sd->skillitemlv )
  10608. skill_lv = sd->skillitemlv;
  10609. if( !(inf&INF_SELF_SKILL) )
  10610. pc_delinvincibletimer(sd); // Target skills thru items cancel invincibility. [Inkfish]
  10611. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10612. return;
  10613. }
  10614. sd->skillitem = sd->skillitemlv = 0;
  10615. if( SKILL_CHK_GUILD(skill_id) ) {
  10616. if (sd->guild != nullptr && (sd->state.gmaster_flag || skill_id == GD_CHARGESHOUT_BEATING))
  10617. skill_lv = guild_checkskill(sd->guild->guild, skill_id);
  10618. else
  10619. skill_lv = 0;
  10620. } else {
  10621. if( skill_id != ALL_EQSWITCH ){
  10622. skill_lv = min(pc_checkskill(sd, skill_id),skill_lv); //never trust client
  10623. }
  10624. }
  10625. pc_delinvincibletimer(sd);
  10626. if( skill_lv )
  10627. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10628. }
  10629. /// Request to use a targeted skill.
  10630. /// 0113 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL)
  10631. /// 0438 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL2)
  10632. /// There are various variants of this packet, some of them have padding between fields.
  10633. void clif_parse_UseSkillToId( int fd, map_session_data *sd ){
  10634. struct s_packet_db* info = &packet_db[RFIFOW(fd, 0)];
  10635. clif_parse_skill_toid( sd, RFIFOW(fd, info->pos[1]), RFIFOW(fd, info->pos[0]), RFIFOL(fd, info->pos[2]) );
  10636. }
  10637. /*==========================================
  10638. * Client tells server he'd like to use AoE skill id 'skill_id' of level 'skill_lv' on 'x','y' location
  10639. *------------------------------------------*/
  10640. static void clif_parse_UseSkillToPosSub( int fd, map_session_data& sd, uint16 skill_lv, uint16 skill_id, short x, short y, int skillmoreinfo ){
  10641. t_tick tick = gettick();
  10642. if( !(skill_get_inf(skill_id)&INF_GROUND_SKILL) )
  10643. return; //Using a target skill on the ground? WRONG.
  10644. if (sd.state.block_action & PCBLOCK_SKILL) {
  10645. clif_msg(&sd, MSI_BUSY);
  10646. return;
  10647. }
  10648. if( SKILL_CHK_HOMUN(skill_id) ) {
  10649. clif_parse_UseSkillToPos_homun(sd.hd, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10650. return;
  10651. }
  10652. if( SKILL_CHK_MERC(skill_id) ) {
  10653. clif_parse_UseSkillToPos_mercenary(sd.md, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10654. return;
  10655. }
  10656. if( pc_hasprogress( &sd, WIP_DISABLE_SKILLITEM ) ){
  10657. #ifdef RENEWAL
  10658. clif_msg( &sd, MSI_BUSY);
  10659. #endif
  10660. return;
  10661. }
  10662. //Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10663. if (battle_config.idletime_option&IDLE_USESKILLTOPOS)
  10664. sd.idletime = last_tick;
  10665. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_USESKILLTOPOS)
  10666. sd.idletime_hom = last_tick;
  10667. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_USESKILLTOPOS)
  10668. sd.idletime_mer = last_tick;
  10669. if( skill_isNotOk(skill_id, sd) )
  10670. return;
  10671. if( skillmoreinfo != -1 ) {
  10672. if( pc_issit(&sd) ) {
  10673. clif_skill_fail( sd, skill_id );
  10674. return;
  10675. }
  10676. //You can't use Graffiti/TalkieBox AND have a vending open, so this is safe.
  10677. safestrncpy(sd.message, RFIFOCP(fd,skillmoreinfo), MESSAGE_SIZE);
  10678. }
  10679. if( sd.ud.skilltimer != INVALID_TIMER )
  10680. return;
  10681. if( DIFF_TICK(tick, sd.ud.canact_tick) < 0 ) {
  10682. if( sd.skillitem != skill_id ) {
  10683. clif_skill_fail( sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10684. return;
  10685. }
  10686. }
  10687. if( sd.sc.option&OPTION_COSTUME )
  10688. return;
  10689. #ifndef RENEWAL
  10690. if( sd.sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd.sc.getSCE(SC_BASILICA)->val4 != sd.bl.id) )
  10691. return; // On basilica only caster can use Basilica again to stop it.
  10692. #endif
  10693. if( sd.menuskill_id ) {
  10694. if( sd.menuskill_id == SA_TAMINGMONSTER ) {
  10695. clif_menuskill_clear(&sd); //Cancel pet capture.
  10696. }else if( sd.menuskill_id == SG_FEEL ){
  10697. clif_menuskill_clear( &sd ); // Cancel selection
  10698. } else if( sd.menuskill_id != SA_AUTOSPELL )
  10699. return; //Can't use skills while a menu is open.
  10700. }
  10701. pc_delinvincibletimer(&sd);
  10702. if( sd.skillitem == skill_id ) {
  10703. if( skill_lv != sd.skillitemlv )
  10704. skill_lv = sd.skillitemlv;
  10705. unit_skilluse_pos(&sd.bl, x, y, skill_id, skill_lv);
  10706. } else {
  10707. int lv;
  10708. sd.skillitem = sd.skillitemlv = 0;
  10709. if( (lv = pc_checkskill(&sd, skill_id)) > 0 ) {
  10710. if( skill_lv > lv )
  10711. skill_lv = lv;
  10712. unit_skilluse_pos(&sd.bl, x, y, skill_id,skill_lv);
  10713. }
  10714. }
  10715. }
  10716. /// Request to use a ground skill.
  10717. /// 0116 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND)
  10718. /// 0366 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND2)
  10719. /// 0AF4 <skill lv>.W <skill id>.W <x>.W <y>.W <unknown>.B (CZ_USE_SKILL_TOGROUND3)
  10720. /// There are various variants of this packet, some of them have padding between fields.
  10721. void clif_parse_UseSkillToPos(int fd, map_session_data *sd)
  10722. {
  10723. if( sd == nullptr ){
  10724. return;
  10725. }
  10726. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10727. if (pc_cant_act(sd))
  10728. return;
  10729. if (pc_issit(sd))
  10730. return;
  10731. clif_parse_UseSkillToPosSub(fd, *sd,
  10732. RFIFOW(fd,info->pos[0]), //skill lv
  10733. RFIFOW(fd,info->pos[1]), //skill num
  10734. RFIFOW(fd,info->pos[2]), //pos x
  10735. RFIFOW(fd,info->pos[3]), //pos y
  10736. // TODO: find out what this is intended to do
  10737. //RFIFOB(fd, info->pos[4])
  10738. -1 //Skill more info.
  10739. );
  10740. }
  10741. /// Request to use a ground skill with text.
  10742. /// 0190 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX)
  10743. /// 0367 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX2)
  10744. /// There are various variants of this packet, some of them have padding between fields.
  10745. void clif_parse_UseSkillToPosMoreInfo(int fd, map_session_data *sd)
  10746. {
  10747. if( sd == nullptr ){
  10748. return;
  10749. }
  10750. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10751. if (pc_cant_act(sd))
  10752. return;
  10753. if (pc_issit(sd))
  10754. return;
  10755. clif_parse_UseSkillToPosSub(fd, *sd,
  10756. RFIFOW(fd,info->pos[0]), //Skill lv
  10757. RFIFOW(fd,info->pos[1]), //Skill num
  10758. RFIFOW(fd,info->pos[2]), //pos x
  10759. RFIFOW(fd,info->pos[3]), //pos y
  10760. info->pos[4] //skill more info
  10761. );
  10762. }
  10763. /// Answer to map selection dialog (CZ_SELECT_WARPPOINT).
  10764. /// 011b <skill id>.W <map name>.16B
  10765. void clif_parse_UseSkillMap(int fd, map_session_data* sd)
  10766. {
  10767. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10768. uint16 skill_id = RFIFOW(fd,info->pos[0]);
  10769. char map_name[MAP_NAME_LENGTH];
  10770. mapindex_getmapname(RFIFOCP(fd,info->pos[1]), map_name);
  10771. sd->state.workinprogress = WIP_DISABLE_NONE;
  10772. if(skill_id != sd->menuskill_id)
  10773. return;
  10774. //It is possible to use teleport with the storage window open bugreport:8027
  10775. if (pc_cant_act(sd) && !sd->state.storage_flag && skill_id != AL_TELEPORT) {
  10776. clif_menuskill_clear(sd);
  10777. return;
  10778. }
  10779. pc_delinvincibletimer(sd);
  10780. skill_castend_map(sd,skill_id,map_name);
  10781. }
  10782. /// Request to set a memo on current map (CZ_REMEMBER_WARPPOINT).
  10783. /// 011d
  10784. void clif_parse_RequestMemo(int fd,map_session_data *sd)
  10785. {
  10786. if (!pc_isdead(sd))
  10787. pc_memo(sd,-1);
  10788. }
  10789. /// Answer to pharmacy item selection dialog.
  10790. /// 018e <name id>.W { <material id>.W }*3 (CZ_REQMAKINGITEM)
  10791. void clif_parse_ProduceMix(int fd,map_session_data *sd){
  10792. if( sd == nullptr ){
  10793. return;
  10794. }
  10795. const PACKET_CZ_REQMAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQMAKINGITEM*>( RFIFOP( fd, 0 ) );
  10796. switch( sd->menuskill_id ) {
  10797. case -1:
  10798. case AM_PHARMACY:
  10799. case RK_RUNEMASTERY:
  10800. case GC_RESEARCHNEWPOISON:
  10801. break;
  10802. default:
  10803. return;
  10804. }
  10805. if (pc_istrading(sd)) {
  10806. //Make it fail to avoid shop exploits where you sell something different than you see.
  10807. clif_skill_fail( *sd, sd->ud.skill_id );
  10808. clif_menuskill_clear(sd);
  10809. return;
  10810. }
  10811. int produce_idx;
  10812. if( (produce_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, 1)) )
  10813. skill_produce_mix(sd,0,p->itemId,p->material[0],p->material[1],p->material[2],1,produce_idx-1);
  10814. clif_menuskill_clear(sd);
  10815. }
  10816. /// Answer to mixing item selection dialog.
  10817. /// 025b <mk type>.W <name id>.W (CZ_REQ_MAKINGITEM)
  10818. /// mk type:
  10819. /// 1 = cooking
  10820. /// 2 = arrow
  10821. /// 3 = elemental
  10822. /// 4 = GN_MIX_COOKING
  10823. /// 5 = GN_MAKEBOMB
  10824. /// 6 = GN_S_PHARMACY
  10825. /// 7 = MT_M_MACHINE - Unconfirmed
  10826. /// 8 = BO_BIONIC_PHARMACY - Unconfirmed
  10827. void clif_parse_Cooking(int fd,map_session_data *sd) {
  10828. #if PACKETVER >= 20051010
  10829. if( sd == nullptr ){
  10830. return;
  10831. }
  10832. const PACKET_CZ_REQ_MAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGITEM*>( RFIFOP( fd, 0 ) );
  10833. int amount = sd->menuskill_val2 ? sd->menuskill_val2 : 1;
  10834. short food_idx = -1;
  10835. if( p->type == 6 && sd->menuskill_id != GN_MIX_COOKING && sd->menuskill_id != GN_S_PHARMACY )
  10836. return;
  10837. if (pc_istrading(sd)) {
  10838. //Make it fail to avoid shop exploits where you sell something different than you see.
  10839. clif_skill_fail( *sd, sd->ud.skill_id );
  10840. clif_menuskill_clear(sd);
  10841. return;
  10842. }
  10843. if( (food_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, amount)) )
  10844. skill_produce_mix(sd,(p->type>1?sd->menuskill_id:0),p->itemId,0,0,0,amount,food_idx-1);
  10845. clif_menuskill_clear(sd);
  10846. #endif
  10847. }
  10848. /// Answer to repair weapon item selection dialog
  10849. /// 01fd <index> W (CZ_REQ_ITEMREPAIR)
  10850. /// 01fd <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W ???
  10851. void clif_parse_RepairItem( int fd, map_session_data *sd ){
  10852. if( sd == nullptr ){
  10853. return;
  10854. }
  10855. // Hercules has wrong date -> use correct one here
  10856. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10857. const PACKET_CZ_REQ_ITEMREPAIR2* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR2*>( RFIFOP( fd, 0 ) );
  10858. #else
  10859. const PACKET_CZ_REQ_ITEMREPAIR1* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR1*>( RFIFOP( fd, 0 ) );
  10860. #endif
  10861. if (sd->menuskill_id != BS_REPAIRWEAPON)
  10862. return;
  10863. if (pc_istrading(sd)) {
  10864. //Make it fail to avoid shop exploits where you sell something different than you see.
  10865. clif_skill_fail( *sd, sd->ud.skill_id );
  10866. clif_menuskill_clear(sd);
  10867. return;
  10868. }
  10869. skill_repairweapon( *sd, p->item.index );
  10870. clif_menuskill_clear(sd);
  10871. }
  10872. /// Answer to refine weapon item selection dialog (CZ_REQ_WEAPONREFINE).
  10873. /// 0222 <index>.L
  10874. void clif_parse_WeaponRefine( int fd, map_session_data *sd ){
  10875. if( sd == nullptr ){
  10876. return;
  10877. }
  10878. if (sd->menuskill_id != WS_WEAPONREFINE) //Packet exploit?
  10879. return;
  10880. if (pc_istrading(sd)) {
  10881. //Make it fail to avoid shop exploits where you sell something different than you see.
  10882. clif_skill_fail( *sd, sd->ud.skill_id );
  10883. clif_menuskill_clear(sd);
  10884. return;
  10885. }
  10886. skill_weaponrefine( *sd, server_index( RFIFOL( fd, 2 ) ) );
  10887. clif_menuskill_clear(sd);
  10888. }
  10889. /// Answer to script menu dialog (CZ_CHOOSE_MENU).
  10890. /// 00b8 <npc id>.L <choice>.B
  10891. /// choice:
  10892. /// 1~254 = menu item
  10893. /// 255 = cancel
  10894. /// NOTE: If there were more than 254 items in the list, choice
  10895. /// overflows to choice%256.
  10896. void clif_parse_NpcSelectMenu(int fd,map_session_data *sd){
  10897. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10898. int npc_id = RFIFOL(fd,info->pos[0]);
  10899. uint8 select = RFIFOB(fd,info->pos[1]);
  10900. #ifdef SECURE_NPCTIMEOUT
  10901. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  10902. return;
  10903. #endif
  10904. if( (select > sd->npc_menu && select != 0xff) || select == 0 ) {
  10905. TBL_NPC* nd = map_id2nd(npc_id);
  10906. 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);
  10907. clif_GM_kick(nullptr,sd);
  10908. return;
  10909. }
  10910. sd->npc_menu = select;
  10911. if( battle_config.idletime_option&IDLE_NPC_MENU ){
  10912. sd->idletime = last_tick;
  10913. }
  10914. npc_scriptcont(sd,npc_id, false);
  10915. }
  10916. /// NPC dialog 'next' click (CZ_REQ_NEXT_SCRIPT).
  10917. /// 00b9 <npc id>.L
  10918. void clif_parse_NpcNextClicked(int fd,map_session_data *sd)
  10919. {
  10920. if( battle_config.idletime_option&IDLE_NPC_NEXT ){
  10921. sd->idletime = last_tick;
  10922. }
  10923. npc_scriptcont(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), false);
  10924. }
  10925. /// NPC numeric input dialog value (CZ_INPUT_EDITDLG).
  10926. /// 0143 <npc id>.L <value>.L
  10927. void clif_parse_NpcAmountInput(int fd,map_session_data *sd){
  10928. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10929. int npcid = RFIFOL(fd,info->pos[0]);
  10930. int amount = (int)RFIFOL(fd,info->pos[1]);
  10931. sd->npc_amount = amount;
  10932. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  10933. sd->idletime = last_tick;
  10934. }
  10935. npc_scriptcont(sd, npcid, false);
  10936. }
  10937. /// NPC text input dialog value (CZ_INPUT_EDITDLGSTR).
  10938. /// 01d5 <packet len>.W <npc id>.L <string>.?B
  10939. void clif_parse_NpcStringInput(int fd, map_session_data* sd){
  10940. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10941. int message_len = RFIFOW(fd,info->pos[0])-8;
  10942. int npcid = RFIFOL(fd,info->pos[1]);
  10943. const char* message = RFIFOCP(fd,info->pos[2]);
  10944. if( message_len <= 0 )
  10945. return; // invalid input
  10946. #if PACKETVER >= 20151001
  10947. message_len++;
  10948. #endif
  10949. safestrncpy(sd->npc_str, message, min(message_len,CHATBOX_SIZE));
  10950. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  10951. sd->idletime = last_tick;
  10952. }
  10953. npc_scriptcont(sd, npcid, false);
  10954. }
  10955. /// NPC dialog 'close' click (CZ_CLOSE_DIALOG).
  10956. /// 0146 <npc id>.L
  10957. void clif_parse_NpcCloseClicked(int fd,map_session_data *sd)
  10958. {
  10959. if (!sd->npc_id) //Avoid parsing anything when the script was done with. [Skotlex]
  10960. return;
  10961. if( battle_config.idletime_option&IDLE_NPC_CLOSE ){
  10962. sd->idletime = last_tick;
  10963. }
  10964. npc_scriptcont(sd, RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), true);
  10965. }
  10966. /// Answer to identify item selection dialog (CZ_REQ_ITEMIDENTIFY).
  10967. /// 0178 <index>.W
  10968. /// index:
  10969. /// -1 = cancel
  10970. void clif_parse_ItemIdentify(int fd,map_session_data *sd) {
  10971. short idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2;
  10972. if (sd->menuskill_id != MC_IDENTIFY)
  10973. return;
  10974. // Ignore the request
  10975. // - Invalid item index
  10976. // - Invalid item ID or item doesn't exist
  10977. // - Item is already identified
  10978. if (idx < 0 || idx >= MAX_INVENTORY ||
  10979. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  10980. sd->inventory.u.items_inventory[idx].identify) {// cancel pressed
  10981. sd->state.workinprogress = WIP_DISABLE_NONE;
  10982. clif_menuskill_clear(sd);
  10983. return;
  10984. }
  10985. skill_identify(sd, idx);
  10986. clif_menuskill_clear(sd);
  10987. }
  10988. /// Answer to arrow crafting item selection dialog.
  10989. /// 01ae <name id>.W (CZ_REQ_MAKINGARROW)
  10990. void clif_parse_SelectArrow(int fd,map_session_data *sd) {
  10991. if( sd == nullptr ){
  10992. return;
  10993. }
  10994. if (pc_istrading(sd)) {
  10995. //Make it fail to avoid shop exploits where you sell something different than you see.
  10996. clif_skill_fail( *sd, sd->ud.skill_id );
  10997. clif_menuskill_clear(sd);
  10998. return;
  10999. }
  11000. const PACKET_CZ_REQ_MAKINGARROW* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGARROW*>( RFIFOP( fd, 0 ) );
  11001. switch (sd->menuskill_id) {
  11002. case AC_MAKINGARROW:
  11003. skill_arrow_create(sd,p->itemId);
  11004. break;
  11005. case SA_CREATECON:
  11006. skill_produce_mix(sd,SA_CREATECON,p->itemId,0,0,0,1,-1);
  11007. break;
  11008. case GC_POISONINGWEAPON:
  11009. skill_poisoningweapon(*sd,p->itemId);
  11010. break;
  11011. case NC_MAGICDECOY:
  11012. skill_magicdecoy(*sd,p->itemId);
  11013. break;
  11014. }
  11015. clif_menuskill_clear(sd);
  11016. }
  11017. /// Answer to SA_AUTOSPELL skill selection dialog (CZ_SELECTAUTOSPELL).
  11018. /// 01ce <skill id>.L
  11019. void clif_parse_AutoSpell(int fd,map_session_data *sd)
  11020. {
  11021. if (sd->menuskill_id != SA_AUTOSPELL)
  11022. return;
  11023. sd->state.workinprogress = WIP_DISABLE_NONE;
  11024. skill_autospell(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11025. clif_menuskill_clear(sd);
  11026. }
  11027. /// Request to display item carding/composition list (CZ_REQ_ITEMCOMPOSITION_LIST).
  11028. /// 017a <card index>.W
  11029. void clif_parse_UseCard(int fd,map_session_data *sd)
  11030. {
  11031. if (sd->state.trading != 0)
  11032. return;
  11033. clif_use_card(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  11034. }
  11035. /// Answer to carding/composing item selection dialog (CZ_REQ_ITEMCOMPOSITION).
  11036. /// 017c <card index>.W <equip index>.W
  11037. void clif_parse_InsertCard(int fd,map_session_data *sd)
  11038. {
  11039. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11040. if (sd->state.trading != 0)
  11041. return;
  11042. pc_insert_card(sd,RFIFOW(fd,info->pos[0])-2,RFIFOW(fd,info->pos[1])-2);
  11043. }
  11044. /// Request of character's name by char ID.
  11045. /// 0193 <char id>.L (CZ_REQNAME_BYGID)
  11046. /// 0369 <char id>.L (CZ_REQNAME_BYGID2)
  11047. /// There are various variants of this packet, some of them have padding between fields.
  11048. void clif_parse_SolveCharName(int fd, map_session_data *sd)
  11049. {
  11050. int charid;
  11051. charid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11052. map_reqnickdb(sd, charid);
  11053. }
  11054. /// /resetskill /resetstate (CZ_RESET).
  11055. /// Request to reset stats or skills.
  11056. /// 0197 <type>.W
  11057. /// type:
  11058. /// 0 = state
  11059. /// 1 = skill
  11060. void clif_parse_ResetChar(int fd, map_session_data *sd) {
  11061. char cmd[15];
  11062. if( RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) )
  11063. safesnprintf(cmd,sizeof(cmd),"%cresetskill",atcommand_symbol);
  11064. else
  11065. safesnprintf(cmd,sizeof(cmd),"%cresetstat",atcommand_symbol);
  11066. is_atcommand(fd, sd, cmd, 1);
  11067. }
  11068. /// /lb /nlb (CZ_LOCALBROADCAST).
  11069. /// Request to broadcast a message on current map.
  11070. /// 019c <packet len>.W <text>.?B
  11071. void clif_parse_LocalBroadcast(int fd, map_session_data* sd)
  11072. {
  11073. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11074. char command[CHAT_SIZE_MAX+16];
  11075. unsigned int len = RFIFOW(fd,info->pos[0])-4;
  11076. char* msg = RFIFOCP(fd,info->pos[1]);
  11077. // as the length varies depending on the command used, just block unreasonably long strings
  11078. mes_len_check(msg, len, CHAT_SIZE_MAX);
  11079. safesnprintf(command,sizeof(command),"%clkami %s", atcommand_symbol, msg);
  11080. is_atcommand(fd, sd, command, 1);
  11081. }
  11082. /// Request to move an item from inventory to storage.
  11083. /// 00f3 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE)
  11084. /// 0364 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE2)
  11085. /// There are various variants of this packet, some of them have padding between fields.
  11086. void clif_parse_MoveToKafra(int fd, map_session_data *sd)
  11087. {
  11088. int item_index, item_amount;
  11089. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11090. if (pc_istrading(sd))
  11091. return;
  11092. item_index = RFIFOW(fd,info->pos[0])-2;
  11093. item_amount = RFIFOL(fd,info->pos[1]);
  11094. if (item_index < 0 || item_index >= MAX_INVENTORY || item_amount < 1)
  11095. return;
  11096. if( sd->inventory.u.items_inventory[item_index].equipSwitch ){
  11097. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11098. return;
  11099. }
  11100. if (sd->state.storage_flag == 1)
  11101. storage_storageadd(sd, &sd->storage, item_index, item_amount);
  11102. else
  11103. if (sd->state.storage_flag == 2)
  11104. storage_guild_storageadd(sd, item_index, item_amount);
  11105. else if (sd->state.storage_flag == 3)
  11106. storage_storageadd(sd, &sd->premiumStorage, item_index, item_amount);
  11107. }
  11108. /// Request to move an item from storage to inventory.
  11109. /// 00f5 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY)
  11110. /// 0365 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY2)
  11111. /// There are various variants of this packet, some of them have padding between fields.
  11112. void clif_parse_MoveFromKafra(int fd,map_session_data *sd)
  11113. {
  11114. int item_index, item_amount;
  11115. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11116. item_index = RFIFOW(fd,info->pos[0])-1;
  11117. item_amount = RFIFOL(fd,info->pos[1]);
  11118. if (sd->state.storage_flag == 1)
  11119. storage_storageget(sd, &sd->storage, item_index, item_amount);
  11120. else if(sd->state.storage_flag == 2)
  11121. storage_guild_storageget(sd, item_index, item_amount);
  11122. else if(sd->state.storage_flag == 3)
  11123. storage_storageget(sd, &sd->premiumStorage, item_index, item_amount);
  11124. }
  11125. /// Request to move an item from cart to storage (CZ_MOVE_ITEM_FROM_CART_TO_STORE).
  11126. /// 0129 <index>.W <amount>.L
  11127. void clif_parse_MoveToKafraFromCart(int fd, map_session_data *sd){
  11128. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11129. int idx = RFIFOW(fd,info->pos[0]) - 2;
  11130. int amount = RFIFOL(fd,info->pos[1]);
  11131. if( sd->state.vending )
  11132. return;
  11133. if (!pc_iscarton(sd))
  11134. return;
  11135. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11136. return;
  11137. if (idx < 0 || idx >= MAX_INVENTORY || amount < 1)
  11138. return;
  11139. if( sd->inventory.u.items_inventory[idx].equipSwitch ){
  11140. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11141. return;
  11142. }
  11143. if (sd->state.storage_flag == 1)
  11144. storage_storageaddfromcart(sd, &sd->storage, idx, amount);
  11145. else if (sd->state.storage_flag == 2)
  11146. storage_guild_storageaddfromcart(sd, idx, amount);
  11147. else if (sd->state.storage_flag == 3)
  11148. storage_storageaddfromcart(sd, &sd->premiumStorage, idx, amount);
  11149. }
  11150. /// Request to move an item from storage to cart (CZ_MOVE_ITEM_FROM_STORE_TO_CART).
  11151. /// 0128 <index>.W <amount>.L
  11152. void clif_parse_MoveFromKafraToCart(int fd, map_session_data *sd){
  11153. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11154. int idx = RFIFOW(fd,info->pos[0]) - 1;
  11155. int amount = RFIFOL(fd,info->pos[1]);
  11156. if( sd->state.vending )
  11157. return;
  11158. if (!pc_iscarton(sd))
  11159. return;
  11160. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11161. return;
  11162. if (sd->state.storage_flag == 1)
  11163. storage_storagegettocart(sd, &sd->storage, idx, amount);
  11164. else
  11165. if (sd->state.storage_flag == 2)
  11166. storage_guild_storagegettocart(sd, idx, amount);
  11167. else if (sd->state.storage_flag == 3)
  11168. storage_storagegettocart(sd, &sd->premiumStorage, idx, amount);
  11169. }
  11170. /// Request to close storage (CZ_CLOSE_STORE).
  11171. /// 00f7
  11172. void clif_parse_CloseKafra(int fd, map_session_data *sd)
  11173. {
  11174. if( sd->state.storage_flag == 1 )
  11175. storage_storageclose(sd);
  11176. else
  11177. if( sd->state.storage_flag == 2 )
  11178. storage_guild_storageclose(sd);
  11179. else if( sd->state.storage_flag == 3 )
  11180. storage_premiumStorage_close(sd);
  11181. }
  11182. /// Displays kafra storage password dialog (ZC_REQ_STORE_PASSWORD).
  11183. /// 023a <info>.W
  11184. /// info:
  11185. /// 0 = password has not been set yet
  11186. /// 1 = storage is password-protected
  11187. /// 8 = too many wrong passwords
  11188. /// ? = ignored
  11189. /// NOTE: This packet is only available on certain non-kRO clients.
  11190. void clif_storagepassword(map_session_data* sd, short info)
  11191. {
  11192. int fd = sd->fd;
  11193. WFIFOHEAD(fd,packet_len(0x23a));
  11194. WFIFOW(fd,0) = 0x23a;
  11195. WFIFOW(fd,2) = info;
  11196. WFIFOSET(fd,packet_len(0x23a));
  11197. }
  11198. /// Answer to the kafra storage password dialog (CZ_ACK_STORE_PASSWORD).
  11199. /// 023b <type>.W <password>.16B <new password>.16B
  11200. /// type:
  11201. /// 2 = change password
  11202. /// 3 = check password
  11203. /// NOTE: This packet is only available on certain non-kRO clients.
  11204. void clif_parse_StoragePassword(int fd, map_session_data *sd){ //@TODO
  11205. // struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11206. // int type = RFIFOW(fd,info->pos[0]);
  11207. // char* password = RFIFOP(fd,info->pos[1]);
  11208. // char* new_password = RFIFOP(fd,info->pos[2]);
  11209. // storage_changepass(sd,password,new_password);
  11210. }
  11211. /// Result of kafra storage password validation (ZC_RESULT_STORE_PASSWORD).
  11212. /// 023c <result>.W <error count>.W
  11213. /// result:
  11214. /// 4 = password change success
  11215. /// 5 = password change failure
  11216. /// 6 = password check success
  11217. /// 7 = password check failure
  11218. /// 8 = too many wrong passwords
  11219. /// ? = ignored
  11220. /// NOTE: This packet is only available on certain non-kRO clients.
  11221. void clif_storagepassword_result(map_session_data* sd, short result, short error_count)
  11222. {
  11223. int fd = sd->fd;
  11224. WFIFOHEAD(fd,packet_len(0x23c));
  11225. WFIFOW(fd,0) = 0x23c;
  11226. WFIFOW(fd,2) = result;
  11227. WFIFOW(fd,4) = error_count;
  11228. WFIFOSET(fd,packet_len(0x23c));
  11229. }
  11230. /// Party creation request
  11231. /// 00f9 <party name>.24B (CZ_MAKE_GROUP)
  11232. void clif_parse_CreateParty(int fd, map_session_data *sd){
  11233. if( sd == nullptr ){
  11234. return;
  11235. }
  11236. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11237. name[NAME_LENGTH-1] = '\0';
  11238. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11239. clif_displaymessage(fd, msg_txt(sd,227));
  11240. return;
  11241. }
  11242. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11243. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11244. return;
  11245. }
  11246. party_create( *sd, name, 0, 0 );
  11247. }
  11248. /// 01e8 <party name>.24B <item pickup rule>.B <item share rule>.B (CZ_MAKE_GROUP2)
  11249. void clif_parse_CreateParty2(int fd, map_session_data *sd){
  11250. if( sd == nullptr ){
  11251. return;
  11252. }
  11253. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11254. char* name = RFIFOCP(fd,info->pos[0]);
  11255. int item1 = RFIFOB(fd,info->pos[1]);
  11256. int item2 = RFIFOB(fd,info->pos[2]);
  11257. name[NAME_LENGTH-1] = '\0';
  11258. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11259. clif_displaymessage(fd, msg_txt(sd,227));
  11260. return;
  11261. }
  11262. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11263. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11264. return;
  11265. }
  11266. party_create( *sd, name, item1, item2 );
  11267. }
  11268. /// Party invitation request by account id
  11269. /// 00fc <account id>.L (CZ_REQ_JOIN_GROUP)
  11270. void clif_parse_PartyInvite( int fd, map_session_data *sd ){
  11271. if( sd == nullptr ){
  11272. return;
  11273. }
  11274. const PACKET_CZ_REQ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11275. party_invite( *sd, map_id2sd( p->AID ) );
  11276. }
  11277. /// Party invitation request by name
  11278. /// 02c4 <char name>.24B (CZ_PARTY_JOIN_REQ)
  11279. void clif_parse_PartyInvite2( int fd, map_session_data *sd ){
  11280. #if PACKETVER >= 20070227
  11281. if( sd == nullptr ){
  11282. return;
  11283. }
  11284. const PACKET_CZ_PARTY_JOIN_REQ* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ*>( RFIFOP( fd, 0 ) );
  11285. char name[NAME_LENGTH];
  11286. safestrncpy( name, p->name, sizeof( name ) );
  11287. party_invite( *sd, map_nick2sd( name, false ) );
  11288. #endif
  11289. }
  11290. /// Party invitation reply
  11291. /// 00ff <party id>.L <flag>.L (CZ_JOIN_GROUP)
  11292. /// flag:
  11293. /// 0 = reject
  11294. /// 1 = accept
  11295. void clif_parse_ReplyPartyInvite( int fd, map_session_data *sd ){
  11296. if( sd == nullptr ){
  11297. return;
  11298. }
  11299. const PACKET_CZ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11300. party_reply_invite( *sd, p->party_id, p->flag );
  11301. }
  11302. /// Party invitation reply
  11303. /// 02c7 <party id>.L <flag>.B (CZ_PARTY_JOIN_REQ_ACK)
  11304. /// flag:
  11305. /// 0 = reject
  11306. /// 1 = accept
  11307. void clif_parse_ReplyPartyInvite2( int fd, map_session_data *sd ){
  11308. #if PACKETVER >= 20070227
  11309. if( sd == nullptr ){
  11310. return;
  11311. }
  11312. const PACKET_CZ_PARTY_JOIN_REQ_ACK* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ_ACK*>( RFIFOP( fd, 0 ) );
  11313. party_reply_invite( *sd, p->party_id, p->flag );
  11314. #endif
  11315. }
  11316. /// Request to leave party.
  11317. /// 0100 (CZ_REQ_LEAVE_GROUP)
  11318. void clif_parse_LeaveParty( int fd, map_session_data *sd ){
  11319. if( sd == nullptr ){
  11320. return;
  11321. }
  11322. //const PACKET_CZ_REQ_LEAVE_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GROUP*>( RFIFOP( fd, 0 ) );
  11323. party_leave( *sd, true );
  11324. }
  11325. /// Request to expel a party member.
  11326. /// 0103 <account id>.L <char name>.24B (CZ_REQ_EXPEL_GROUP_MEMBER)
  11327. void clif_parse_RemovePartyMember( int fd, map_session_data* sd ){
  11328. if( sd == nullptr ){
  11329. return;
  11330. }
  11331. const PACKET_CZ_REQ_EXPEL_GROUP_MEMBER* p = reinterpret_cast<PACKET_CZ_REQ_EXPEL_GROUP_MEMBER*>( RFIFOP( fd, 0 ) );
  11332. party_removemember( *sd, p->AID, p->name );
  11333. }
  11334. /// Request to change party options.
  11335. /// 0102 <exp share rule>.L (CZ_CHANGE_GROUPEXPOPTION)
  11336. /// 07d7 <exp share rule>.L <item pickup rule>.B <item share rule>.B (CZ_GROUPINFO_CHANGE_V2)
  11337. void clif_parse_PartyChangeOption(int fd, map_session_data *sd)
  11338. {
  11339. struct party_data *p;
  11340. int i,expflag;
  11341. int cmd = RFIFOW(fd,0);
  11342. struct s_packet_db* info = &packet_db[cmd];
  11343. if( !sd->status.party_id )
  11344. return;
  11345. p = party_search(sd->status.party_id);
  11346. if( p == nullptr )
  11347. return;
  11348. ARR_FIND( 0, MAX_PARTY, i, p->data[i].sd == sd );
  11349. if( i == MAX_PARTY )
  11350. return; //Shouldn't happen
  11351. if( !p->party.member[i].leader )
  11352. return;
  11353. expflag = RFIFOL(fd,info->pos[0]);
  11354. if(cmd == 0x0102){ //Client can't change the item-field
  11355. party_changeoption(sd, expflag, p->party.item);
  11356. }
  11357. else {
  11358. int itemflag = (RFIFOB(fd,info->pos[1])?1:0)|(RFIFOB(fd,info->pos[2])?2:0);
  11359. party_changeoption(sd, expflag, itemflag);
  11360. }
  11361. }
  11362. /// Validates and processes party messages (CZ_REQUEST_CHAT_PARTY).
  11363. /// 0108 <packet len>.W <text>.?B (<name> : <message>) 00
  11364. void clif_parse_PartyMessage(int fd, map_session_data* sd){
  11365. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11366. // validate packet and retrieve name and message
  11367. if( !clif_process_message( sd, false, name, message, output ) )
  11368. return;
  11369. party_send_message(sd, output, strlen(output) + 1 );
  11370. }
  11371. /// Changes Party Leader (CZ_CHANGE_GROUP_MASTER).
  11372. /// 07da <account id>.L
  11373. void clif_parse_PartyChangeLeader(int fd, map_session_data* sd){
  11374. party_changeleader(sd, map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0])),nullptr);
  11375. }
  11376. /// Party Booking in KRO [Spiria]
  11377. ///
  11378. /// Request to register a party booking advertisment (CZ_PARTY_BOOKING_REQ_REGISTER).
  11379. /// 0802 <level>.W <map id>.W { <job>.W }*6
  11380. void clif_parse_PartyBookingRegisterReq(int fd, map_session_data* sd){
  11381. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11382. short level = RFIFOW(fd,info->pos[0]);
  11383. short mapid = RFIFOW(fd,info->pos[1]);
  11384. int idxpbj = info->pos[2];
  11385. short job[MAX_PARTY_BOOKING_JOBS];
  11386. int i;
  11387. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11388. job[i] = RFIFOB(fd,idxpbj+i*2);
  11389. party_booking_register(sd, level, mapid, job);
  11390. }
  11391. /// Result of request to register a party booking advertisment (ZC_PARTY_BOOKING_ACK_REGISTER).
  11392. /// 0803 <result>.W
  11393. /// result:
  11394. /// 0 = success
  11395. /// 1 = failure
  11396. /// 2 = already registered
  11397. void clif_PartyBookingRegisterAck(map_session_data *sd, int flag)
  11398. {
  11399. int fd = sd->fd;
  11400. WFIFOHEAD(fd,packet_len(0x803));
  11401. WFIFOW(fd,0) = 0x803;
  11402. WFIFOW(fd,2) = flag;
  11403. WFIFOSET(fd,packet_len(0x803));
  11404. }
  11405. /// Request to search for party booking advertisments (CZ_PARTY_BOOKING_REQ_SEARCH).
  11406. /// 0804 <level>.W <map id>.W <job>.W <last index>.L <result count>.W
  11407. void clif_parse_PartyBookingSearchReq(int fd, map_session_data* sd){
  11408. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11409. short level = RFIFOW(fd,info->pos[0]);
  11410. short mapid = RFIFOW(fd,info->pos[1]);
  11411. short job = RFIFOW(fd,info->pos[2]);
  11412. unsigned long lastindex = RFIFOL(fd,info->pos[3]);
  11413. short resultcount = RFIFOW(fd,info->pos[4]);
  11414. party_booking_search(sd, level, mapid, job, lastindex, resultcount);
  11415. }
  11416. /// Party booking search results (ZC_PARTY_BOOKING_ACK_SEARCH).
  11417. /// 0805 <packet len>.W <more results>.B { <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6 }*
  11418. /// more results:
  11419. /// 0 = no
  11420. /// 1 = yes
  11421. void clif_PartyBookingSearchAck(int fd, struct party_booking_ad_info** results, int count, bool more_result)
  11422. {
  11423. int i, j;
  11424. int size = sizeof(struct party_booking_ad_info); // structure size (48)
  11425. struct party_booking_ad_info *pb_ad;
  11426. WFIFOHEAD(fd,size*count + 5);
  11427. WFIFOW(fd,0) = 0x805;
  11428. WFIFOW(fd,2) = size*count + 5;
  11429. WFIFOB(fd,4) = more_result;
  11430. for(i=0; i<count; i++)
  11431. {
  11432. pb_ad = results[i];
  11433. WFIFOL(fd,i*size+5) = pb_ad->index;
  11434. safestrncpy(WFIFOCP(fd,i*size+9),pb_ad->charname,NAME_LENGTH);
  11435. WFIFOL(fd,i*size+33) = pb_ad->starttime; // FIXME: This is expire time
  11436. WFIFOW(fd,i*size+37) = pb_ad->p_detail.level;
  11437. WFIFOW(fd,i*size+39) = pb_ad->p_detail.mapid;
  11438. for(j=0; j<MAX_PARTY_BOOKING_JOBS; j++)
  11439. WFIFOW(fd,i*size+41+j*2) = pb_ad->p_detail.job[j];
  11440. }
  11441. WFIFOSET(fd,WFIFOW(fd,2));
  11442. }
  11443. /// Request to delete own party booking advertisment (CZ_PARTY_BOOKING_REQ_DELETE).
  11444. /// 0806
  11445. void clif_parse_PartyBookingDeleteReq(int fd, map_session_data* sd)
  11446. {
  11447. if(party_booking_delete(sd))
  11448. clif_PartyBookingDeleteAck(sd, 0);
  11449. }
  11450. /// Result of request to delete own party booking advertisment (ZC_PARTY_BOOKING_ACK_DELETE).
  11451. /// 0807 <result>.W
  11452. /// result:
  11453. /// 0 = success
  11454. /// 1 = success (auto-removed expired ad)
  11455. /// 2 = failure
  11456. /// 3 = nothing registered
  11457. void clif_PartyBookingDeleteAck(map_session_data* sd, int flag)
  11458. {
  11459. int fd = sd->fd;
  11460. WFIFOHEAD(fd,packet_len(0x807));
  11461. WFIFOW(fd,0) = 0x807;
  11462. WFIFOW(fd,2) = flag;
  11463. WFIFOSET(fd,packet_len(0x807));
  11464. }
  11465. /// Request to update party booking advertisment (CZ_PARTY_BOOKING_REQ_UPDATE).
  11466. /// 0808 { <job>.W }*6
  11467. void clif_parse_PartyBookingUpdateReq(int fd, map_session_data* sd)
  11468. {
  11469. short job[MAX_PARTY_BOOKING_JOBS];
  11470. int i;
  11471. int idxpbu = packet_db[RFIFOW(fd,0)].pos[0];
  11472. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11473. job[i] = RFIFOW(fd,idxpbu+i*2);
  11474. party_booking_update(sd, job);
  11475. }
  11476. /// Notification about new party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_INSERT).
  11477. /// 0809 <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6
  11478. void clif_PartyBookingInsertNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11479. {
  11480. int i;
  11481. uint8 buf[38+MAX_PARTY_BOOKING_JOBS*2];
  11482. if(pb_ad == nullptr) return;
  11483. WBUFW(buf,0) = 0x809;
  11484. WBUFL(buf,2) = pb_ad->index;
  11485. safestrncpy(WBUFCP(buf,6),pb_ad->charname,NAME_LENGTH);
  11486. WBUFL(buf,30) = pb_ad->starttime; // FIXME: This is expire time
  11487. WBUFW(buf,34) = pb_ad->p_detail.level;
  11488. WBUFW(buf,36) = pb_ad->p_detail.mapid;
  11489. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11490. WBUFW(buf,38+i*2) = pb_ad->p_detail.job[i];
  11491. clif_send(buf, packet_len(0x809), &sd->bl, ALL_CLIENT);
  11492. }
  11493. /// Notification about updated party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_UPDATE).
  11494. /// 080a <index>.L { <job>.W }*6
  11495. void clif_PartyBookingUpdateNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11496. {
  11497. int i;
  11498. uint8 buf[6+MAX_PARTY_BOOKING_JOBS*2];
  11499. if(pb_ad == nullptr) return;
  11500. WBUFW(buf,0) = 0x80a;
  11501. WBUFL(buf,2) = pb_ad->index;
  11502. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11503. WBUFW(buf,6+i*2) = pb_ad->p_detail.job[i];
  11504. clif_send(buf,packet_len(0x80a),&sd->bl,ALL_CLIENT); // Now UPDATE all client.
  11505. }
  11506. /// Notification about deleted party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_DELETE).
  11507. /// 080b <index>.L
  11508. void clif_PartyBookingDeleteNotify(map_session_data* sd, int index)
  11509. {
  11510. uint8 buf[6];
  11511. WBUFW(buf,0) = 0x80b;
  11512. WBUFL(buf,2) = index;
  11513. clif_send(buf, packet_len(0x80b), &sd->bl, ALL_CLIENT); // Now UPDATE all client.
  11514. }
  11515. /// Request to close own vending (CZ_REQ_CLOSESTORE).
  11516. /// 012e
  11517. void clif_parse_CloseVending(int fd, map_session_data* sd)
  11518. {
  11519. vending_closevending(sd);
  11520. }
  11521. /// Request to open a vending shop (CZ_REQ_BUY_FROMMC).
  11522. /// 0130 <account id>.L
  11523. void clif_parse_VendingListReq(int fd, map_session_data* sd)
  11524. {
  11525. if( sd->npc_id ) {// using an NPC
  11526. return;
  11527. }
  11528. vending_vendinglistreq(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11529. }
  11530. /// Shop item(s) purchase request.
  11531. /// 0134 <packet len>.W <account id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC)
  11532. void clif_parse_PurchaseReq(int fd, map_session_data* sd){
  11533. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC*>( RFIFOP( fd, 0 ) );
  11534. vending_purchasereq( sd, p->AID, sd->vended_id, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11535. // whether it fails or not, the buy window is closed
  11536. sd->vended_id = 0;
  11537. }
  11538. /// Shop item(s) purchase request.
  11539. /// 0801 <packet len>.W <account id>.L <unique id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC2)
  11540. void clif_parse_PurchaseReq2(int fd, map_session_data* sd){
  11541. #if PACKETVER >= 20100105
  11542. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2*>( RFIFOP( fd, 0 ) );
  11543. vending_purchasereq( sd, p->AID, p->UniqueID, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11544. // whether it fails or not, the buy window is closed
  11545. sd->vended_id = 0;
  11546. #endif
  11547. }
  11548. /// Confirm or cancel the shop preparation window.
  11549. /// 012f <packet len>.W <shop name>.80B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE)
  11550. /// 01b2 <packet len>.W <shop name>.80B <result>.B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE2)
  11551. /// result:
  11552. /// 0 = canceled
  11553. /// 1 = open
  11554. void clif_parse_OpenVending(int fd, map_session_data* sd){
  11555. if( sd == nullptr ){
  11556. return;
  11557. }
  11558. int cmd = RFIFOW(fd,0);
  11559. struct s_packet_db* info = &packet_db[cmd];
  11560. short len = (short)RFIFOW(fd,info->pos[0]);
  11561. const char* message = RFIFOCP(fd,info->pos[1]);
  11562. const uint8* data = (uint8*)RFIFOP(fd,info->pos[3]);
  11563. if(cmd == 0x12f){ // (CZ_REQ_OPENSTORE)
  11564. len -= 84;
  11565. }
  11566. else { //(CZ_REQ_OPENSTORE2)
  11567. bool flag;
  11568. len -= 85;
  11569. flag = RFIFOB(fd,info->pos[2]) != 0;
  11570. if (!flag) {
  11571. sd->state.prevend = 0;
  11572. sd->state.workinprogress = WIP_DISABLE_NONE;
  11573. }
  11574. }
  11575. if( sd->sc.getSCE(SC_NOCHAT) && sd->sc.getSCE(SC_NOCHAT)->val1&MANNER_NOROOM )
  11576. return;
  11577. if( map_getmapflag(sd->bl.m, MF_NOVENDING) ) {
  11578. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  11579. return;
  11580. }
  11581. if( map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNOVENDING) ) {
  11582. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  11583. return;
  11584. }
  11585. if( message[0] == '\0' ) // invalid input
  11586. return;
  11587. vending_openvending(*sd, message, data, len/8, nullptr);
  11588. }
  11589. /// Guild creation request (CZ_REQ_MAKE_GUILD).
  11590. /// 0165 <char id>.L <guild name>.24B
  11591. void clif_parse_CreateGuild(int fd,map_session_data *sd){
  11592. //int charid = RFIFOL(fd,packet_db[cmd].pos[0]);
  11593. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11594. name[NAME_LENGTH-1] = '\0';
  11595. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11596. clif_displaymessage(fd, msg_txt(sd,228));
  11597. return;
  11598. }
  11599. if(sd->clan){
  11600. // Should display a clientside message "You are currently joined in Clan !!" so we ignore it
  11601. return;
  11602. }
  11603. guild_create( *sd, name );
  11604. }
  11605. /// Request for guild window interface permissions
  11606. /// 014d (CZ_REQ_GUILD_MENUINTERFACE)
  11607. static void clif_parse_GuildCheckMaster(int fd, map_session_data *sd){
  11608. if(sd == nullptr)
  11609. return;
  11610. clif_guild_masterormember(*sd);
  11611. }
  11612. /// Request for guild window information (CZ_REQ_GUILD_MENU).
  11613. /// 014f <type>.L
  11614. /// type:
  11615. /// 0 = basic info
  11616. /// 1 = member manager
  11617. /// 2 = positions
  11618. /// 3 = skills
  11619. /// 4 = expulsion list
  11620. /// 5 = unknown (GM_ALLGUILDLIST)
  11621. /// 6 = notice
  11622. void clif_parse_GuildRequestInfo(int fd, map_session_data *sd)
  11623. {
  11624. int type = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11625. if( !sd->status.guild_id && !sd->bg_id )
  11626. return;
  11627. switch( type )
  11628. {
  11629. case 0: // Basic Information Guild, hostile alliance information
  11630. clif_guild_basicinfo( *sd );
  11631. clif_guild_allianceinfo(*sd);
  11632. clif_guild_castle_list(*sd);
  11633. break;
  11634. case 1: // Members list, list job title
  11635. clif_guild_positionnamelist(sd);
  11636. clif_guild_memberlist( *sd );
  11637. break;
  11638. case 2: // List job title, title information list
  11639. clif_guild_positionnamelist(sd);
  11640. clif_guild_positioninfolist(*sd);
  11641. break;
  11642. case 3: // Skill list
  11643. clif_guild_skillinfo( *sd );
  11644. break;
  11645. case 4: // Expulsion list
  11646. clif_guild_expulsionlist(*sd);
  11647. break;
  11648. default:
  11649. ShowError("clif: guild request info: unknown type %d\n", type);
  11650. break;
  11651. }
  11652. }
  11653. /// Request to update guild positions (CZ_REG_CHANGE_GUILD_POSITIONINFO).
  11654. /// 0161 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <name>.24B }*
  11655. void clif_parse_GuildChangePositionInfo(int fd, map_session_data *sd)
  11656. {
  11657. int i;
  11658. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11659. int len = RFIFOW(fd,info->pos[0]);
  11660. int idxgpos = info->pos[1];
  11661. if(!sd->state.gmaster_flag)
  11662. return;
  11663. for(i = idxgpos; i < len; i += 40 ){
  11664. guild_change_position(sd->status.guild_id, RFIFOL(fd,i), RFIFOL(fd,i+4), RFIFOL(fd,i+12), RFIFOCP(fd,i+16));
  11665. }
  11666. }
  11667. /// Request to update the position of guild members.
  11668. /// 0155 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (CZ_REQ_CHANGE_MEMBERPOS)
  11669. void clif_parse_GuildChangeMemberPosition( int fd, map_session_data *sd ){
  11670. if(!sd->state.gmaster_flag)
  11671. return;
  11672. const PACKET_CZ_REQ_CHANGE_MEMBERPOS* p = reinterpret_cast<PACKET_CZ_REQ_CHANGE_MEMBERPOS*>( RFIFOP( fd, 0 ) );
  11673. int16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  11674. for( int16 i = 0; i < entries; i++ ){
  11675. const PACKET_CZ_REQ_CHANGE_MEMBERPOS_sub& entry = p->list[i];
  11676. // Guild leadership change
  11677. if( entry.position == 0 ){
  11678. if( !battle_config.guild_leaderchange_woe && is_agit_start() ){
  11679. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_IN_SIEGE_TIME );
  11680. return;
  11681. }
  11682. if( battle_config.guild_leaderchange_delay && DIFF_TICK( time( nullptr ),sd->guild->guild.last_leader_change ) < battle_config.guild_leaderchange_delay ){
  11683. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_NOT_TIME );
  11684. return;
  11685. }
  11686. guild_gm_change( sd->status.guild_id, entry.CID );
  11687. // No further entries will be processed - the requesting player lost his guild master status
  11688. return;
  11689. }else if( entry.position > 0 ){
  11690. guild_change_memberposition( sd->status.guild_id, entry.AID, entry.CID, entry.position );
  11691. }
  11692. }
  11693. }
  11694. /// Request for guild emblem data (CZ_REQ_GUILD_EMBLEM_IMG).
  11695. /// 0151 <guild id>.L
  11696. void clif_parse_GuildRequestEmblem(int fd,map_session_data *sd)
  11697. {
  11698. int guild_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11699. auto g = guild_search(guild_id);
  11700. if (g)
  11701. clif_guild_emblem(*sd, g->guild);
  11702. }
  11703. /// Validates data of a guild emblem (compressed bitmap)
  11704. enum e_result_validate_emblem { // Used as Result for clif_validate_emblem
  11705. EMBVALIDATE_SUCCESS = 0,
  11706. EMBVALIDATE_ERR_RAW_FILEFORMAT, // Invalid File Format (Error in zlib/decompression or malformed BMP header)
  11707. EMBVALIDATE_ERR_TRANSPARENCY // uploaded emblem does not met the requirements of inter_config.emblem_transparency_limit
  11708. };
  11709. static enum e_result_validate_emblem clif_validate_emblem(const uint8* emblem, unsigned long emblem_len)
  11710. {
  11711. uint8 buf[1800]; // no well-formed emblem bitmap is larger than 1782 (24 bit) / 1654 (8 bit) bytes
  11712. unsigned long buf_len = sizeof(buf);
  11713. int offset = 0;
  11714. if(!(( decode_zip(buf, &buf_len, emblem, emblem_len) == 0 && buf_len >= 18 ) // sizeof(BITMAPFILEHEADER) + sizeof(biSize) of the following info header struct
  11715. && RBUFW(buf,0) == 0x4d42 // BITMAPFILEHEADER.bfType (signature)
  11716. && RBUFL(buf,2) == buf_len // BITMAPFILEHEADER.bfSize (file size)
  11717. && (offset = RBUFL(buf,10)) < buf_len // BITMAPFILEHEADER.bfOffBits (offset to bitmap bits)
  11718. ))
  11719. return EMBVALIDATE_ERR_RAW_FILEFORMAT;
  11720. if(inter_config.emblem_transparency_limit != 100) {
  11721. int i, transcount = 1, tmp[3];
  11722. for(i = offset; i < buf_len-1; i++) {
  11723. int j = i%3;
  11724. tmp[j] = RBUFL(buf,i);
  11725. if(j == 2 && (tmp[0] == 0xFFFF00FF) && (tmp[1] == 0xFFFF00) && (tmp[2] == 0xFF00FFFF)) //if pixel is transparent
  11726. transcount++;
  11727. }
  11728. if(((transcount*300)/(buf_len-offset)) > inter_config.emblem_transparency_limit) //convert in % to chk
  11729. return EMBVALIDATE_ERR_TRANSPARENCY;
  11730. }
  11731. return EMBVALIDATE_SUCCESS;
  11732. }
  11733. /// Request to update the guild emblem (CZ_REGISTER_GUILD_EMBLEM_IMG).
  11734. /// 0153 <packet len>.W <emblem data>.?B
  11735. void clif_parse_GuildChangeEmblem(int fd,map_session_data *sd){
  11736. if( sd == nullptr ){
  11737. return;
  11738. }
  11739. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11740. unsigned long emblem_len = RFIFOW(fd,info->pos[0])-4;
  11741. const uint8* emblem = RFIFOP(fd,info->pos[1]);
  11742. int emb_val=0;
  11743. if( !emblem_len || !sd->state.gmaster_flag )
  11744. return;
  11745. if(!(inter_config.emblem_woe_change) && is_agit_start() ){
  11746. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,385),false,SELF); //"You not allowed to change emblem during woe"
  11747. return;
  11748. }
  11749. emb_val = clif_validate_emblem(emblem, emblem_len);
  11750. if(emb_val == EMBVALIDATE_ERR_RAW_FILEFORMAT){
  11751. 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);
  11752. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,386),false,SELF); //"The chosen emblem was detected invalid\n"
  11753. return;
  11754. } else if(emb_val == EMBVALIDATE_ERR_TRANSPARENCY){
  11755. char output[128];
  11756. safesnprintf(output,sizeof(output),msg_txt(sd,387),inter_config.emblem_transparency_limit);
  11757. 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"
  11758. return;
  11759. }
  11760. guild_change_emblem( *sd, emblem_len, (const char*)emblem );
  11761. }
  11762. /// Request to update the guild emblem id
  11763. /// 0b46 <guild id>.L <version>.L
  11764. void clif_parse_GuildChangeEmblem2(int fd, map_session_data* sd) {
  11765. if( sd == nullptr ){
  11766. return;
  11767. }
  11768. #if PACKETVER >= 20190724
  11769. const PACKET_CZ_REQ_ADD_NEW_EMBLEM* p = reinterpret_cast<PACKET_CZ_REQ_ADD_NEW_EMBLEM*>( RFIFOP( fd, 0 ) );
  11770. auto &g = sd->guild;
  11771. if (!g || g->guild.guild_id != p->guild_id)
  11772. return;
  11773. if (!sd->state.gmaster_flag)
  11774. return;
  11775. if (!inter_config.emblem_woe_change && is_agit_start()) {
  11776. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 385), false, SELF); //"You not allowed to change emblem during woe"
  11777. return;
  11778. }
  11779. guild_change_emblem_version( *sd, p->version );
  11780. #endif
  11781. }
  11782. /// Guild notice update request (CZ_GUILD_NOTICE).
  11783. /// 016e <guild id>.L <msg1>.60B <msg2>.120B
  11784. void clif_parse_GuildChangeNotice(int fd, map_session_data* sd){
  11785. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11786. int guild_id = RFIFOL(fd,info->pos[0]);
  11787. char* msg1 = RFIFOCP(fd,info->pos[1]);
  11788. char* msg2 = RFIFOCP(fd,info->pos[2]);
  11789. if(!sd->state.gmaster_flag)
  11790. return;
  11791. // compensate for some client defects when using multilanguage mode
  11792. if (msg1[0] == '|' && msg1[3] == '|') msg1+= 3; // skip duplicate marker
  11793. if (msg2[0] == '|' && msg2[3] == '|') msg2+= 3; // skip duplicate marker
  11794. if (msg2[0] == '|') msg2[strnlen(msg2, MAX_GUILDMES2)-1] = '\0'; // delete extra space at the end of string
  11795. guild_change_notice(sd, guild_id, msg1, msg2);
  11796. }
  11797. /// Guild invite request.
  11798. /// 0168 <account id>.L <inviter account id>.L <inviter char id>.L (CZ_REQ_JOIN_GUILD)
  11799. void clif_parse_GuildInvite( int fd,map_session_data *sd ){
  11800. if( sd == nullptr ){
  11801. return;
  11802. }
  11803. const PACKET_CZ_REQ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11804. guild_invite( *sd, map_id2sd( p->AID ) );
  11805. }
  11806. /// Guild invite request (/guildinvite)
  11807. /// 0916 <char name>.24B (CZ_REQ_JOIN_GUILD2)
  11808. void clif_parse_GuildInvite2( int fd, map_session_data *sd ){
  11809. #if PACKETVER >= 20120410
  11810. if( sd == nullptr ){
  11811. return;
  11812. }
  11813. const PACKET_CZ_REQ_JOIN_GUILD2* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD2*>( RFIFOP( fd, 0 ) );
  11814. char nick[NAME_LENGTH] = {0};
  11815. safestrncpy( nick, p->name, NAME_LENGTH );
  11816. guild_invite( *sd, map_nick2sd( nick, false ) );
  11817. #endif
  11818. }
  11819. /// Answer to guild invitation.
  11820. /// 016b <guild id>.L <answer>.L (CZ_JOIN_GUILD)
  11821. /// answer:
  11822. /// 0 = refuse
  11823. /// 1 = accept
  11824. void clif_parse_GuildReplyInvite( int fd, map_session_data *sd ){
  11825. if( sd == nullptr ){
  11826. return;
  11827. }
  11828. const PACKET_CZ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11829. guild_reply_invite( *sd, p->guild_id, p->answer );
  11830. }
  11831. /// Request to leave guild.
  11832. /// 0159 <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_LEAVE_GUILD)
  11833. void clif_parse_GuildLeave(int fd,map_session_data *sd){
  11834. if( sd == nullptr ){
  11835. return;
  11836. }
  11837. const PACKET_CZ_REQ_LEAVE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GUILD*>( RFIFOP( fd, 0 ) );
  11838. guild_leave( *sd, p->guild_id, p->AID, p->CID, p->message );
  11839. }
  11840. /// Request to expel a member of a guild.
  11841. /// 015b <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_BAN_GUILD)
  11842. void clif_parse_GuildExpulsion(int fd,map_session_data *sd){
  11843. if( sd == nullptr ){
  11844. return;
  11845. }
  11846. const PACKET_CZ_REQ_BAN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_BAN_GUILD*>( RFIFOP( fd, 0 ) );
  11847. guild_expulsion( *sd, p->guild_id, p->AID, p->CID, p->message );
  11848. }
  11849. /// Validates and processes guild messages (CZ_GUILD_CHAT).
  11850. /// 017e <packet len>.W <text>.?B (<name> : <message>) 00
  11851. void clif_parse_GuildMessage(int fd, map_session_data* sd){
  11852. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11853. // validate packet and retrieve name and message
  11854. if( !clif_process_message( sd, false, name, message, output ) )
  11855. return;
  11856. if( sd->bg_id )
  11857. bg_send_message(sd, output, strlen(output) );
  11858. else
  11859. guild_send_message(sd, output, strlen(output) );
  11860. }
  11861. /// Guild alliance request (CZ_REQ_ALLY_GUILD).
  11862. /// 0170 <account id>.L <inviter account id>.L <inviter char id>.L
  11863. void clif_parse_GuildRequestAlliance(int fd, map_session_data *sd)
  11864. {
  11865. map_session_data *t_sd;
  11866. if(!sd->state.gmaster_flag)
  11867. return;
  11868. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11869. clif_displaymessage(fd, msg_txt(sd,228));
  11870. return;
  11871. }
  11872. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11873. //inv_aid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11874. //inv_cid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[2]);
  11875. // @noask [LuzZza]
  11876. if(t_sd && t_sd->state.noask) {
  11877. clif_noask_sub( *sd, *t_sd, 396 ); // Autorejected alliance request from %s.
  11878. return;
  11879. }
  11880. guild_reqalliance(sd,t_sd);
  11881. }
  11882. /// Answer to a guild alliance request (CZ_ALLY_GUILD).
  11883. /// 0172 <inviter account id>.L <answer>.L
  11884. /// answer:
  11885. /// 0 = refuse
  11886. /// 1 = accept
  11887. void clif_parse_GuildReplyAlliance(int fd, map_session_data *sd){
  11888. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11889. guild_reply_reqalliance(sd,
  11890. RFIFOL(fd,info->pos[0]),
  11891. RFIFOL(fd,info->pos[1]));
  11892. }
  11893. /// Request to delete a guild alliance or opposition (CZ_REQ_DELETE_RELATED_GUILD).
  11894. /// 0183 <opponent guild id>.L <relation>.L
  11895. /// relation:
  11896. /// 0 = Ally
  11897. /// 1 = Enemy
  11898. void clif_parse_GuildDelAlliance(int fd, map_session_data *sd){
  11899. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11900. if(!sd->state.gmaster_flag)
  11901. return;
  11902. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11903. clif_displaymessage(fd, msg_txt(sd,228));
  11904. return;
  11905. }
  11906. guild_delalliance(sd,
  11907. RFIFOL(fd,info->pos[0]),
  11908. RFIFOL(fd,info->pos[1]));
  11909. }
  11910. /// Request to set a guild as opposition (CZ_REQ_HOSTILE_GUILD).
  11911. /// 0180 <account id>.L
  11912. void clif_parse_GuildOpposition(int fd, map_session_data *sd)
  11913. {
  11914. map_session_data *t_sd;
  11915. if(!sd->state.gmaster_flag)
  11916. return;
  11917. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11918. clif_displaymessage(fd, msg_txt(sd,228));
  11919. return;
  11920. }
  11921. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11922. // @noask [LuzZza]
  11923. if(t_sd && t_sd->state.noask) {
  11924. clif_noask_sub( *sd, *t_sd, 397 ); // Autorejected opposition request from %s.
  11925. return;
  11926. }
  11927. guild_opposition(sd,t_sd);
  11928. }
  11929. /// Request to delete own guild.
  11930. /// 015d <key>.40B (CZ_REQ_DISORGANIZE_GUILD)
  11931. /// key:
  11932. /// now guild name; might have been (intended) email, since the
  11933. /// field name and size is same as the one in CH_DELETE_CHAR.
  11934. void clif_parse_GuildBreak( int fd, map_session_data *sd ){
  11935. if( sd == nullptr ){
  11936. return;
  11937. }
  11938. const PACKET_CZ_REQ_DISORGANIZE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_DISORGANIZE_GUILD*>( RFIFOP( fd, 0 ) );
  11939. guild_break( *sd, p->key );
  11940. }
  11941. /// Pet
  11942. ///
  11943. /// Request to invoke a pet menu action (CZ_COMMAND_PET).
  11944. /// 01a1 <type>.B
  11945. /// type:
  11946. /// 0 = pet information
  11947. /// 1 = feed
  11948. /// 2 = performance
  11949. /// 3 = return to egg
  11950. /// 4 = unequip accessory
  11951. void clif_parse_PetMenu(int fd, map_session_data *sd){
  11952. pet_menu(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11953. }
  11954. /// Attempt to tame a monster (CZ_TRYCAPTURE_MONSTER).
  11955. /// 019f <id>.L
  11956. void clif_parse_CatchPet(int fd, map_session_data *sd){
  11957. pet_catch_process2(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11958. }
  11959. /// Answer to pet incubator egg selection dialog (CZ_SELECT_PETEGG).
  11960. /// 01a7 <index>.W
  11961. void clif_parse_SelectEgg(int fd, map_session_data *sd){
  11962. if (sd->menuskill_id != SA_TAMINGMONSTER || sd->menuskill_val != -1)
  11963. return;
  11964. pet_select_egg(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  11965. clif_menuskill_clear(sd);
  11966. }
  11967. /// Request to display pet's emotion/talk (CZ_PET_ACT).
  11968. /// 01a9 <data>.L
  11969. /// data:
  11970. /// is either emotion (@see enum emotion_type) or a compound value
  11971. /// (((mob id)-100)*100+(act id)*10+(hunger)) that describes an
  11972. /// entry (given in parentheses) in data\pettalktable.xml
  11973. /// act id:
  11974. /// 0 = feeding
  11975. /// 1 = hunting
  11976. /// 2 = danger
  11977. /// 3 = dead
  11978. /// 4 = normal (stand)
  11979. /// 5 = special performance (perfor_s)
  11980. /// 6 = level up (levelup)
  11981. /// 7 = performance 1 (perfor_1)
  11982. /// 8 = performance 2 (perfor_2)
  11983. /// 9 = performance 3 (perfor_3)
  11984. /// 10 = log-in greeting (connect)
  11985. /// hungry value:
  11986. /// 0 = very hungry (hungry)
  11987. /// 1 = hungry (bit_hungry)
  11988. /// 2 = satisfied (noting)
  11989. /// 3 = stuffed (full)
  11990. /// 4 = full (so_full)
  11991. void clif_parse_SendEmotion(int fd, map_session_data *sd)
  11992. {
  11993. if(sd->pd)
  11994. clif_pet_emotion(sd->pd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11995. }
  11996. /// Request to change pet's name (CZ_RENAME_PET).
  11997. /// 01a5 <name>.24B
  11998. void clif_parse_ChangePetName(int fd, map_session_data *sd)
  11999. {
  12000. pet_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12001. }
  12002. /// /kill (CZ_DISCONNECT_CHARACTER).
  12003. /// Request to disconnect a character.
  12004. /// 00cc <account id>.L
  12005. /// NOTE: Also sent when using GM right click menu "(name) force to quit"
  12006. void clif_parse_GMKick(int fd, map_session_data *sd)
  12007. {
  12008. struct block_list *target;
  12009. int tid;
  12010. tid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12011. target = map_id2bl(tid);
  12012. if (!target) {
  12013. clif_GM_kickack(sd, 0);
  12014. return;
  12015. }
  12016. switch (target->type) {
  12017. case BL_PC:
  12018. {
  12019. char command[NAME_LENGTH+6];
  12020. safesnprintf(command,sizeof(command),"%ckick %s", atcommand_symbol, status_get_name(target));
  12021. is_atcommand(fd, sd, command, 1);
  12022. }
  12023. break;
  12024. /**
  12025. * This one does not invoke any atcommand, so we need to check for permissions.
  12026. */
  12027. case BL_MOB:
  12028. {
  12029. char command[100];
  12030. if( !pc_can_use_command(sd, "killmonster", COMMAND_ATCOMMAND)) {
  12031. clif_GM_kickack(sd, 0);
  12032. return;
  12033. }
  12034. safesnprintf(command,sizeof(command),"/kick %s (%d)", status_get_name(target), status_get_class(target));
  12035. log_atcommand(sd, command);
  12036. status_percent_damage(&sd->bl, target, 100, 0, true); // can invalidate 'target'
  12037. }
  12038. break;
  12039. case BL_NPC:
  12040. {
  12041. struct npc_data* nd = (struct npc_data *)target;
  12042. if( pc_can_use_command(sd, "unloadnpc", COMMAND_ATCOMMAND)) {
  12043. npc_unload_duplicates(nd);
  12044. npc_unload(nd,true);
  12045. npc_read_event_script();
  12046. }
  12047. }
  12048. break;
  12049. default:
  12050. clif_GM_kickack(sd, 0);
  12051. }
  12052. }
  12053. /// /killall (CZ_DISCONNECT_ALL_CHARACTER).
  12054. /// Request to disconnect all characters.
  12055. /// 00ce
  12056. void clif_parse_GMKickAll(int fd, map_session_data* sd) {
  12057. char cmd[15];
  12058. safesnprintf(cmd,sizeof(cmd),"%ckickall",atcommand_symbol);
  12059. is_atcommand(fd, sd, cmd, 1);
  12060. }
  12061. /// /remove (CZ_REMOVE_AID).
  12062. /// Request to warp to a character with given login ID.
  12063. /// 01ba <account name>.24B
  12064. /// /shift (CZ_SHIFT).
  12065. /// Request to warp to a character with given name.
  12066. /// 01bb <char name>.24B
  12067. void clif_parse_GMShift(int fd, map_session_data *sd)
  12068. {// FIXME: remove is supposed to receive account name for clients prior 20100803RE
  12069. char *player_name;
  12070. char command[NAME_LENGTH+8];
  12071. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12072. player_name[NAME_LENGTH-1] = '\0';
  12073. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, player_name);
  12074. is_atcommand(fd, sd, command, 1);
  12075. }
  12076. /// /remove (CZ_REMOVE_AID_SSO).
  12077. /// Request to warp to a character with given account ID.
  12078. /// 0843 <account id>.L
  12079. void clif_parse_GMRemove2(int fd, map_session_data* sd)
  12080. {
  12081. uint32 account_id;
  12082. map_session_data* pl_sd;
  12083. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12084. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12085. char command[NAME_LENGTH+8];
  12086. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, pl_sd->status.name);
  12087. is_atcommand(fd, sd, command, 1);
  12088. }
  12089. }
  12090. /// /recall (CZ_RECALL).
  12091. /// Request to summon a player with given login ID to own position.
  12092. /// 01bc <account name>.24B
  12093. /// /summon (CZ_RECALL_GID).
  12094. /// Request to summon a player with given name to own position.
  12095. /// 01bd <char name>.24B
  12096. void clif_parse_GMRecall(int fd, map_session_data *sd)
  12097. {// FIXME: recall is supposed to receive account name for clients prior 20100803RE
  12098. char *player_name;
  12099. char command [NAME_LENGTH+8];
  12100. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12101. player_name[NAME_LENGTH-1] = '\0';
  12102. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, player_name);
  12103. is_atcommand(fd, sd, command, 1);
  12104. }
  12105. /// /recall (CZ_RECALL_SSO).
  12106. /// Request to summon a player with given account ID to own position.
  12107. /// 0842 <account id>.L
  12108. void clif_parse_GMRecall2(int fd, map_session_data* sd)
  12109. {
  12110. uint32 account_id;
  12111. map_session_data* pl_sd;
  12112. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12113. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12114. char command[NAME_LENGTH+8];
  12115. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, pl_sd->status.name);
  12116. is_atcommand(fd, sd, command, 1);
  12117. }
  12118. }
  12119. /// /item /monster (CZ_ITEM_CREATE).
  12120. /// Request to execute GM commands.
  12121. /// Usage:
  12122. /// /item n - summon n monster or acquire n item/s
  12123. /// /item money - grants 2147483647 zenies
  12124. /// /item whereisboss - locate boss mob in current map.(not yet implemented)
  12125. /// /item regenboss_n t - regenerate n boss monster by t millisecond.(not yet implemented)
  12126. /// /item onekillmonster - toggle an ability to kill mobs in one hit.(not yet implemented)
  12127. /// /item bossinfo - display the information of a boss monster in current map.(not yet implemented)
  12128. /// /item cap_n - capture n monster as pet.(not yet implemented)
  12129. /// /item agitinvest - reset current global agit investments.(not yet implemented)
  12130. /// 013f <item/mob name>.24B
  12131. /// 09ce <item/mob name>.100B [Ind/Yommy]
  12132. void clif_parse_GM_Item_Monster(int fd, map_session_data *sd)
  12133. {
  12134. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12135. int mob_id = 0;
  12136. StringBuf command;
  12137. char *str;
  12138. //#if PACKETVER >= 20131218
  12139. // char str[100];
  12140. //#else
  12141. // char str[24];
  12142. //#endif
  12143. str = RFIFOCP(fd,info->pos[0]);
  12144. if (!str || str[0] == '\0')
  12145. return;
  12146. if (strcmpi(str,"SPPOINT") == 0 || strcmpi(str,"JOBLEVEL") == 0) //! TODO /sp sends these values
  12147. return;
  12148. trim(str);
  12149. // Zeny
  12150. if( strcmpi(str, "money") == 0 ) {
  12151. StringBuf_Init(&command);
  12152. StringBuf_Printf(&command, "%czeny %d", atcommand_symbol, INT_MAX);
  12153. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12154. StringBuf_Destroy(&command);
  12155. return;
  12156. }
  12157. std::shared_ptr<item_data> id = item_db.searchname( str );
  12158. // Item
  12159. if( id ){
  12160. StringBuf_Init(&command);
  12161. if( !itemdb_isstackable2( id.get() ) ){ //Nonstackable
  12162. StringBuf_Printf(&command, "%citem2 %u 1 0 0 0 0 0 0 0", atcommand_symbol, id->nameid);
  12163. }else{
  12164. if (id->flag.guid)
  12165. StringBuf_Printf(&command, "%citem %u 1", atcommand_symbol, id->nameid);
  12166. else
  12167. StringBuf_Printf(&command, "%citem %u 20", atcommand_symbol, id->nameid);
  12168. }
  12169. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12170. StringBuf_Destroy(&command);
  12171. return;
  12172. }
  12173. // Monster
  12174. if ((mob_id = mobdb_searchname(str)) == 0)
  12175. mob_id = mobdb_checkid(atoi(str));
  12176. std::shared_ptr<s_mob_db> mob = mob_db.find(mob_id);
  12177. if( mob != nullptr ) {
  12178. StringBuf_Init(&command);
  12179. StringBuf_Printf(&command, "%cmonster %s", atcommand_symbol, mob->sprite.c_str());
  12180. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12181. StringBuf_Destroy(&command);
  12182. return;
  12183. }
  12184. }
  12185. /// /hide (CZ_CHANGE_EFFECTSTATE).
  12186. /// 019d <effect state>.L
  12187. /// effect state:
  12188. /// TODO: Any OPTION_* ?
  12189. void clif_parse_GMHide(int fd, map_session_data *sd) {
  12190. char cmd[6];
  12191. //int eff_st = RFIFOL(packet_db[RFIFOW(fd,0)].pos[0]);
  12192. safesnprintf(cmd,sizeof(cmd),"%chide",atcommand_symbol);
  12193. is_atcommand(fd, sd, cmd, 1);
  12194. }
  12195. /// /resetcooltime
  12196. /// 0a88 (CZ_CMD_RESETCOOLTIME).
  12197. void clif_parse_gm_resetcooltime( int fd, map_session_data* sd ){
  12198. #if PACKETVER_MAIN_NUM >= 20160622 || PACKETVER_RE_NUM >= 20160622 || defined(PACKETVER_ZERO)
  12199. const PACKET_CZ_CMD_RESETCOOLTIME* p = reinterpret_cast<const PACKET_CZ_CMD_RESETCOOLTIME*>( RFIFOP( fd, 0 ) );
  12200. char cmd[CHAT_SIZE_MAX];
  12201. safesnprintf(cmd,sizeof(cmd),"%cresetcooltime",atcommand_symbol);
  12202. is_atcommand(fd, sd, cmd, 1);
  12203. #endif
  12204. }
  12205. /// Request to adjust player's manner points (CZ_REQ_GIVE_MANNER_POINT).
  12206. /// 0149 <account id>.L <type>.B <value>.W
  12207. /// type:
  12208. /// 0 = positive points
  12209. /// 1 = negative points
  12210. /// 2 = self mute (+10 minutes)
  12211. void clif_parse_GMReqNoChat(int fd,map_session_data *sd)
  12212. {
  12213. int id, type, value;
  12214. map_session_data *dstsd;
  12215. char command[NAME_LENGTH+15];
  12216. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12217. id = RFIFOL(fd,info->pos[0]);
  12218. type = RFIFOB(fd,info->pos[1]);
  12219. value = RFIFOW(fd,info->pos[2]);
  12220. if( type == 0 )
  12221. value = -value;
  12222. //If type is 2 and the ids don't match, this is a crafted hacked packet!
  12223. //Disabled because clients keep self-muting when you give players public @ commands... [Skotlex]
  12224. if (type == 2 /* && (pc_get_group_level(sd) > 0 || sd->bl.id != id)*/)
  12225. return;
  12226. dstsd = map_id2sd(id);
  12227. if( dstsd == nullptr )
  12228. return;
  12229. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, value, dstsd->status.name);
  12230. is_atcommand(fd, sd, command, 1);
  12231. }
  12232. /// /rc (CZ_REQ_GIVE_MANNER_BYNAME).
  12233. /// GM adjustment of a player's manner value by -60.
  12234. /// 0212 <char name>.24B
  12235. void clif_parse_GMRc(int fd, map_session_data* sd)
  12236. {
  12237. char command[NAME_LENGTH+15];
  12238. char *name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12239. name[NAME_LENGTH-1] = '\0';
  12240. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, 60, name);
  12241. is_atcommand(fd, sd, command, 1);
  12242. }
  12243. /// Result of request to resolve account name (ZC_ACK_ACCOUNTNAME).
  12244. /// 01e0 <account id>.L <account name>.24B
  12245. void clif_account_name(int fd, uint32 account_id, const char* accname)
  12246. {
  12247. WFIFOHEAD(fd,packet_len(0x1e0));
  12248. WFIFOW(fd,0) = 0x1e0;
  12249. WFIFOL(fd,2) = account_id;
  12250. safestrncpy(WFIFOCP(fd,6), accname, NAME_LENGTH);
  12251. WFIFOSET(fd,packet_len(0x1e0));
  12252. }
  12253. /// GM requesting account name (for right-click gm menu) (CZ_REQ_ACCOUNTNAME).
  12254. /// 01df <account id>.L
  12255. //! TODO: Figure out how does this actually work
  12256. void clif_parse_GMReqAccountName(int fd, map_session_data *sd)
  12257. {
  12258. uint32 account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12259. /*
  12260. char query[30];
  12261. safesnprintf(query,sizeof(query),"%d", account_id);
  12262. intif_request_accinfo(sd->fd, sd->bl.id, pc_get_group_level(sd), query, 1); //will call clif_account_name at return
  12263. */
  12264. clif_account_name(fd, account_id, "");
  12265. }
  12266. /// /changemaptype <x> <y> <type> (CZ_CHANGE_MAPTYPE).
  12267. /// GM single cell type change request.
  12268. /// 0198 <x>.W <y>.W <type>.W
  12269. /// type:
  12270. /// 0 = not walkable
  12271. /// 1 = walkable
  12272. void clif_parse_GMChangeMapType(int fd, map_session_data *sd)
  12273. {
  12274. int x,y,type;
  12275. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12276. if(! pc_has_permission(sd, PC_PERM_USE_CHANGEMAPTYPE) )
  12277. return;
  12278. x = RFIFOW(fd,info->pos[0]);
  12279. y = RFIFOW(fd,info->pos[1]);
  12280. type = RFIFOW(fd,info->pos[2]);
  12281. map_setgatcell(sd->bl.m,x,y,type);
  12282. clif_changemapcell(0,sd->bl.m,x,y,type,ALL_SAMEMAP);
  12283. //FIXME: once players leave the map, the client 'forgets' this information.
  12284. }
  12285. /// /in /ex (CZ_SETTING_WHISPER_PC).
  12286. /// Request to allow/deny whispers from a nick.
  12287. /// 00cf <nick>.24B <type>.B
  12288. /// type:
  12289. /// 0 = (/ex nick) deny speech from nick
  12290. /// 1 = (/in nick) allow speech from nick
  12291. void clif_parse_PMIgnore(int fd, map_session_data* sd)
  12292. {
  12293. char* nick;
  12294. uint8 type;
  12295. int i;
  12296. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12297. nick = RFIFOCP(fd,info->pos[0]);
  12298. nick[NAME_LENGTH-1] = '\0'; // to be sure that the player name has at most 23 characters
  12299. type = RFIFOB(fd,info->pos[1]);
  12300. if( type == 0 ) { // Add name to ignore list (block)
  12301. if (strcmp(wisp_server_name, nick) == 0) {
  12302. clif_wisexin(sd, type, 1); // fail
  12303. return;
  12304. }
  12305. // try to find a free spot, while checking for duplicates at the same time
  12306. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12307. if( i == MAX_IGNORE_LIST ) {// no space for new entry
  12308. clif_wisexin(sd, type, 2); // too many blocks
  12309. return;
  12310. }
  12311. if( sd->ignore[i].name[0] != '\0' ) {// name already exists
  12312. clif_wisexin(sd, type, 0); // Aegis reports success.
  12313. return;
  12314. }
  12315. //Insert in position i
  12316. safestrncpy(sd->ignore[i].name, nick, NAME_LENGTH);
  12317. } else { // Remove name from ignore list (unblock)
  12318. // find entry
  12319. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12320. if( i == MAX_IGNORE_LIST || sd->ignore[i].name[0] == '\0' ) { //Not found
  12321. clif_wisexin(sd, type, 1); // fail
  12322. return;
  12323. }
  12324. // move everything one place down to overwrite removed entry
  12325. memmove(sd->ignore[i].name, sd->ignore[i+1].name, (MAX_IGNORE_LIST-i-1)*sizeof(sd->ignore[0].name));
  12326. // wipe last entry
  12327. memset(sd->ignore[MAX_IGNORE_LIST-1].name, 0, sizeof(sd->ignore[0].name));
  12328. }
  12329. clif_wisexin(sd, type, 0); // success
  12330. }
  12331. /// /inall /exall (CZ_SETTING_WHISPER_STATE).
  12332. /// Request to allow/deny all whispers.
  12333. /// 00d0 <type>.B
  12334. /// type:
  12335. /// 0 = (/exall) deny all speech
  12336. /// 1 = (/inall) allow all speech
  12337. void clif_parse_PMIgnoreAll(int fd, map_session_data *sd)
  12338. {
  12339. int type = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]), flag;
  12340. if( type == 0 ) {// Deny all
  12341. if( sd->state.ignoreAll ) {
  12342. flag = 1; // fail
  12343. } else {
  12344. sd->state.ignoreAll = 1;
  12345. flag = 0; // success
  12346. }
  12347. } else {//Unblock everyone
  12348. if( sd->state.ignoreAll ) {
  12349. sd->state.ignoreAll = 0;
  12350. flag = 0; // success
  12351. } else {
  12352. if (sd->ignore[0].name[0] != '\0')
  12353. { //Wipe the ignore list.
  12354. memset(sd->ignore, 0, sizeof(sd->ignore));
  12355. flag = 0; // success
  12356. } else {
  12357. flag = 1; // fail
  12358. }
  12359. }
  12360. }
  12361. clif_wisall(sd, type, flag);
  12362. }
  12363. /// Whisper ignore list (ZC_WHISPER_LIST).
  12364. /// 00d4 <packet len>.W { <char name>.24B }*
  12365. void clif_PMIgnoreList(map_session_data* sd)
  12366. {
  12367. int i, fd = sd->fd;
  12368. WFIFOHEAD(fd,4+ARRAYLENGTH(sd->ignore)*NAME_LENGTH);
  12369. WFIFOW(fd,0) = 0xd4;
  12370. for( i = 0; i < ARRAYLENGTH(sd->ignore) && sd->ignore[i].name[0]; i++ ) {
  12371. safestrncpy(WFIFOCP(fd,4+i*NAME_LENGTH), sd->ignore[i].name, NAME_LENGTH);
  12372. }
  12373. WFIFOW(fd,2) = 4+i*NAME_LENGTH;
  12374. WFIFOSET(fd,WFIFOW(fd,2));
  12375. }
  12376. /// Whisper ignore list request (CZ_REQ_WHISPER_LIST).
  12377. /// 00d3
  12378. void clif_parse_PMIgnoreList(int fd,map_session_data *sd)
  12379. {
  12380. clif_PMIgnoreList(sd);
  12381. }
  12382. /// Request to invoke the /doridori recovery bonus (CZ_DORIDORI).
  12383. /// 01e7
  12384. void clif_parse_NoviceDoriDori(int fd, map_session_data *sd)
  12385. {
  12386. if (sd->state.doridori) return;
  12387. switch (sd->class_&MAPID_UPPERMASK) {
  12388. case MAPID_SOUL_LINKER:
  12389. case MAPID_STAR_GLADIATOR:
  12390. case MAPID_TAEKWON:
  12391. if (!sd->state.rest)
  12392. break;
  12393. [[fallthrough]];
  12394. case MAPID_SUPER_NOVICE:
  12395. case MAPID_SUPER_BABY:
  12396. case MAPID_SUPER_NOVICE_E:
  12397. case MAPID_SUPER_BABY_E:
  12398. sd->state.doridori=1;
  12399. break;
  12400. }
  12401. }
  12402. /// Request to invoke the effect of super novice's guardian angel prayer (CZ_CHOPOKGI).
  12403. /// 01ed
  12404. /// Note: This packet is caused by 7 lines of any text, followed by
  12405. /// the prayer and an another line of any text. The prayer is
  12406. /// defined by lines 790~793 in data\msgstringtable.txt
  12407. /// "Dear angel, can you hear my voice?"
  12408. /// "I am" (space separated player name) "Super Novice~"
  12409. /// "Help me out~ Please~ T_T"
  12410. void clif_parse_NoviceExplosionSpirits(int fd, map_session_data *sd)
  12411. {
  12412. if( (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE ) {
  12413. t_exp next = pc_nextbaseexp(sd);
  12414. if( next ) {
  12415. int percent = (int)( ( (double)sd->status.base_exp/(double)next )*1000. );
  12416. if( percent && ( percent%100 ) == 0 ) {// 10.0%, 20.0%, ..., 90.0%
  12417. sc_start(&sd->bl,&sd->bl, SC_EXPLOSIONSPIRITS, 100, 17, skill_get_time(MO_EXPLOSIONSPIRITS, 5)); //Lv17-> +50 critical (noted by Poki) [Skotlex]
  12418. clif_skill_nodamage(&sd->bl, &sd->bl, MO_EXPLOSIONSPIRITS, 5, 1); // prayer always shows successful Lv5 cast and disregards noskill restrictions
  12419. }
  12420. }
  12421. }
  12422. }
  12423. /// Friends List
  12424. ///
  12425. /// Toggles a single friend online/offline [Skotlex] (ZC_FRIENDS_STATE).
  12426. /// 0206 <account id>.L <char id>.L <state>.B
  12427. /// 0206 <account id>.L <char id>.L <state>.B <name>.24B >= 20180221
  12428. /// state:
  12429. /// 0 = online
  12430. /// 1 = offline
  12431. void clif_friendslist_toggle(map_session_data *sd,uint32 account_id, uint32 char_id, int online)
  12432. {
  12433. int i, fd = sd->fd;
  12434. //Seek friend.
  12435. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id &&
  12436. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12437. if(i == MAX_FRIENDS || sd->status.friends[i].char_id == 0)
  12438. return; //Not found
  12439. WFIFOHEAD(fd,packet_len(0x206));
  12440. WFIFOW(fd, 0) = 0x206;
  12441. WFIFOL(fd, 2) = sd->status.friends[i].account_id;
  12442. WFIFOL(fd, 6) = sd->status.friends[i].char_id;
  12443. WFIFOB(fd,10) = !online; //Yeah, a 1 here means "logged off", go figure...
  12444. #if PACKETVER >= 20180221
  12445. safestrncpy(WFIFOCP(fd, 11), sd->status.friends[i].name, NAME_LENGTH);
  12446. #endif
  12447. WFIFOSET(fd, packet_len(0x206));
  12448. }
  12449. //Subfunction called from clif_foreachclient to toggle friends on/off [Skotlex]
  12450. int clif_friendslist_toggle_sub(map_session_data *sd,va_list ap)
  12451. {
  12452. uint32 account_id, char_id, online;
  12453. account_id = va_arg(ap, int);
  12454. char_id = va_arg(ap, int);
  12455. online = va_arg(ap, int);
  12456. clif_friendslist_toggle(sd, account_id, char_id, online);
  12457. return 0;
  12458. }
  12459. /// Sends the whole friends list (ZC_FRIENDS_LIST).
  12460. /// 0201 <packet len>.W { <account id>.L <char id>.L <name>.24B }*
  12461. /// 0201 <packet len>.W { <account id>.L <char id>.L }* >= 20180221
  12462. void clif_friendslist_send( map_session_data& sd ){
  12463. PACKET_ZC_FRIENDS_LIST* p = reinterpret_cast<PACKET_ZC_FRIENDS_LIST*>( packet_buffer );
  12464. p->packetType = HEADER_ZC_FRIENDS_LIST;
  12465. p->PacketLength = sizeof( *p );
  12466. for( int i = 0, c = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12467. PACKET_ZC_FRIENDS_LIST_sub& f = p->friends[c];
  12468. f.AID = sd.status.friends[i].account_id;
  12469. f.CID = sd.status.friends[i].char_id;
  12470. #if !( PACKETVER_MAIN_NUM >= 20180307 || PACKETVER_RE_NUM >= 20180221 || PACKETVER_ZERO_NUM >= 20180328 ) || PACKETVER >= 20200902
  12471. safestrncpy( f.name, sd.status.friends[i].name, sizeof( f.name ) );
  12472. #endif
  12473. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( f ) );
  12474. c++;
  12475. }
  12476. clif_send( p, p->PacketLength, &sd.bl, SELF );
  12477. // Sending the online players
  12478. for( int i = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12479. if( map_charid2sd( sd.status.friends[i].char_id ) ){
  12480. clif_friendslist_toggle( &sd, sd.status.friends[i].account_id, sd.status.friends[i].char_id, 1 );
  12481. }
  12482. }
  12483. }
  12484. /// Notification about the result of a friend add request (ZC_ADD_FRIENDS_LIST).
  12485. /// 0209 <result>.W <account id>.L <char id>.L <name>.24B
  12486. /// result:
  12487. /// 0 = MsgStringTable[821]="You have become friends with (%s)."
  12488. /// 1 = MsgStringTable[822]="(%s) does not want to be friends with you."
  12489. /// 2 = MsgStringTable[819]="Your Friend List is full."
  12490. /// 3 = MsgStringTable[820]="(%s)'s Friend List is full."
  12491. void clif_friendslist_reqack(map_session_data *sd, map_session_data *f_sd, int type)
  12492. {
  12493. int fd;
  12494. nullpo_retv(sd);
  12495. fd = sd->fd;
  12496. WFIFOHEAD(fd,packet_len(0x209));
  12497. WFIFOW(fd,0) = 0x209;
  12498. WFIFOW(fd,2) = type;
  12499. if (f_sd) {
  12500. WFIFOL(fd,4) = f_sd->status.account_id;
  12501. WFIFOL(fd,8) = f_sd->status.char_id;
  12502. safestrncpy(WFIFOCP(fd, 12), f_sd->status.name,NAME_LENGTH);
  12503. }
  12504. WFIFOSET(fd, packet_len(0x209));
  12505. }
  12506. /// Asks a player for permission to be added as friend (ZC_REQ_ADD_FRIENDS).
  12507. /// 0207 <req account id>.L <req char id>.L <req char name>.24B
  12508. void clif_friendlist_req(map_session_data* sd, uint32 account_id, uint32 char_id, const char* name)
  12509. {
  12510. int fd = sd->fd;
  12511. WFIFOHEAD(fd,packet_len(0x207));
  12512. WFIFOW(fd,0) = 0x207;
  12513. WFIFOL(fd,2) = account_id;
  12514. WFIFOL(fd,6) = char_id;
  12515. safestrncpy(WFIFOCP(fd,10), name, NAME_LENGTH);
  12516. WFIFOSET(fd,packet_len(0x207));
  12517. }
  12518. /// Request to add a player as friend (CZ_ADD_FRIENDS).
  12519. /// 0202 <name>.24B
  12520. void clif_parse_FriendsListAdd(int fd, map_session_data *sd)
  12521. {
  12522. map_session_data *f_sd;
  12523. int i;
  12524. f_sd = map_nick2sd(RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]),false);
  12525. // Friend doesn't exist (no player with this name)
  12526. if (f_sd == nullptr) {
  12527. clif_displaymessage(fd, msg_txt(sd,3));
  12528. return;
  12529. }
  12530. if( sd->bl.id == f_sd->bl.id ) {// adding oneself as friend
  12531. return;
  12532. }
  12533. // ensure that the request player's friend list is not full
  12534. ARR_FIND(0, MAX_FRIENDS, i, sd->status.friends[i].char_id == 0);
  12535. if( i == MAX_FRIENDS ){
  12536. clif_friendslist_reqack(sd, f_sd, 2);
  12537. return;
  12538. }
  12539. // @noask [LuzZza]
  12540. if(f_sd->state.noask) {
  12541. clif_noask_sub( *sd, *f_sd, 398 ); // Autorejected friend request from %s.
  12542. return;
  12543. }
  12544. // Friend already exists
  12545. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id != 0; i++) {
  12546. if (sd->status.friends[i].char_id == f_sd->status.char_id) {
  12547. clif_displaymessage(fd, msg_txt(sd,671)); //"Friend already exists."
  12548. return;
  12549. }
  12550. }
  12551. f_sd->friend_req = sd->status.char_id;
  12552. sd->friend_req = f_sd->status.char_id;
  12553. clif_friendlist_req(f_sd, sd->status.account_id, sd->status.char_id, sd->status.name);
  12554. }
  12555. /// Answer to a friend add request (CZ_ACK_REQ_ADD_FRIENDS).
  12556. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.B
  12557. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.L (PACKETVER >= 6)
  12558. /// reply:
  12559. /// 0 = rejected
  12560. /// 1 = accepted
  12561. void clif_parse_FriendsListReply(int fd, map_session_data *sd)
  12562. {
  12563. map_session_data *f_sd;
  12564. uint32 account_id;
  12565. char reply;
  12566. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12567. account_id = RFIFOL(fd,info->pos[0]);
  12568. //char_id = RFIFOL(fd,info->pos[1]);
  12569. #if PACKETVER < 6
  12570. reply = RFIFOB(fd,info->pos[2]);
  12571. #else
  12572. reply = RFIFOL(fd,info->pos[2]);
  12573. #endif
  12574. if( sd->bl.id == account_id ) {// adding oneself as friend
  12575. return;
  12576. }
  12577. f_sd = map_id2sd(account_id); //The account id is the same as the bl.id of players.
  12578. if (f_sd == nullptr)
  12579. return;
  12580. if (reply == 0 || !( sd->friend_req == f_sd->status.char_id && f_sd->friend_req == sd->status.char_id ) )
  12581. clif_friendslist_reqack(f_sd, sd, 1);
  12582. else {
  12583. int i;
  12584. // Find an empty slot
  12585. for (i = 0; i < MAX_FRIENDS; i++)
  12586. if (f_sd->status.friends[i].char_id == 0)
  12587. break;
  12588. if (i == MAX_FRIENDS) {
  12589. clif_friendslist_reqack(f_sd, sd, 2);
  12590. return;
  12591. }
  12592. f_sd->status.friends[i].account_id = sd->status.account_id;
  12593. f_sd->status.friends[i].char_id = sd->status.char_id;
  12594. safestrncpy(f_sd->status.friends[i].name, sd->status.name, NAME_LENGTH);
  12595. clif_friendslist_reqack(f_sd, sd, 0);
  12596. achievement_update_objective(f_sd, AG_ADD_FRIEND, 1, i + 1);
  12597. if (battle_config.friend_auto_add) {
  12598. // Also add f_sd to sd's friendlist.
  12599. for (i = 0; i < MAX_FRIENDS; i++) {
  12600. if (sd->status.friends[i].char_id == f_sd->status.char_id)
  12601. return; //No need to add anything.
  12602. if (sd->status.friends[i].char_id == 0)
  12603. break;
  12604. }
  12605. if (i == MAX_FRIENDS) {
  12606. clif_friendslist_reqack(sd, f_sd, 2);
  12607. return;
  12608. }
  12609. sd->status.friends[i].account_id = f_sd->status.account_id;
  12610. sd->status.friends[i].char_id = f_sd->status.char_id;
  12611. safestrncpy(sd->status.friends[i].name, f_sd->status.name, NAME_LENGTH);
  12612. clif_friendslist_reqack(sd, f_sd, 0);
  12613. achievement_update_objective(sd, AG_ADD_FRIEND, 1, i + 1);
  12614. }
  12615. }
  12616. }
  12617. /// Request to delete a friend (CZ_DELETE_FRIENDS).
  12618. /// 0203 <account id>.L <char id>.L
  12619. void clif_parse_FriendsListRemove(int fd, map_session_data *sd)
  12620. {
  12621. map_session_data *f_sd = nullptr;
  12622. uint32 account_id, char_id;
  12623. int i, j;
  12624. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12625. account_id = RFIFOL(fd,info->pos[0]);
  12626. char_id = RFIFOL(fd,info->pos[1]);
  12627. // Search friend
  12628. for (i = 0; i < MAX_FRIENDS &&
  12629. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12630. if (i == MAX_FRIENDS) {
  12631. clif_displaymessage(fd, msg_txt(sd,672)); //"Name not found in list."
  12632. return;
  12633. }
  12634. //remove from friend's list first
  12635. if( (f_sd = map_id2sd(account_id)) && f_sd->status.char_id == char_id) {
  12636. for (i = 0; i < MAX_FRIENDS &&
  12637. (f_sd->status.friends[i].char_id != sd->status.char_id || f_sd->status.friends[i].account_id != sd->status.account_id); i++);
  12638. if (i != MAX_FRIENDS) {
  12639. // move all chars up
  12640. for(j = i + 1; j < MAX_FRIENDS; j++)
  12641. memcpy(&f_sd->status.friends[j-1], &f_sd->status.friends[j], sizeof(f_sd->status.friends[0]));
  12642. memset(&f_sd->status.friends[MAX_FRIENDS-1], 0, sizeof(f_sd->status.friends[MAX_FRIENDS-1]));
  12643. //should the guy be notified of some message? we should add it here if so
  12644. WFIFOHEAD(f_sd->fd,packet_len(0x20a));
  12645. WFIFOW(f_sd->fd,0) = 0x20a;
  12646. WFIFOL(f_sd->fd,2) = sd->status.account_id;
  12647. WFIFOL(f_sd->fd,6) = sd->status.char_id;
  12648. WFIFOSET(f_sd->fd, packet_len(0x20a));
  12649. }
  12650. } else { //friend not online -- ask char server to delete from his friendlist
  12651. if(chrif_removefriend(char_id,sd->status.char_id)) { // char-server offline, abort
  12652. clif_displaymessage(fd, msg_txt(sd,673)); //"This action can't be performed at the moment. Please try again later."
  12653. return;
  12654. }
  12655. }
  12656. // We can now delete from original requester
  12657. for (i = 0; i < MAX_FRIENDS &&
  12658. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12659. // move all chars up
  12660. for(j = i + 1; j < MAX_FRIENDS; j++)
  12661. memcpy(&sd->status.friends[j-1], &sd->status.friends[j], sizeof(sd->status.friends[0]));
  12662. memset(&sd->status.friends[MAX_FRIENDS-1], 0, sizeof(sd->status.friends[MAX_FRIENDS-1]));
  12663. clif_displaymessage(fd, msg_txt(sd,674)); //"Friend removed"
  12664. WFIFOHEAD(fd,packet_len(0x20a));
  12665. WFIFOW(fd,0) = 0x20a;
  12666. WFIFOL(fd,2) = account_id;
  12667. WFIFOL(fd,6) = char_id;
  12668. WFIFOSET(fd, packet_len(0x20a));
  12669. }
  12670. /// /pvpinfo list (ZC_ACK_PVPPOINT).
  12671. /// 0210 <char id>.L <account id>.L <win point>.L <lose point>.L <point>.L
  12672. void clif_PVPInfo(map_session_data* sd)
  12673. {
  12674. int fd = sd->fd;
  12675. WFIFOHEAD(fd,packet_len(0x210));
  12676. WFIFOW(fd,0) = 0x210;
  12677. WFIFOL(fd,2) = sd->status.char_id;
  12678. WFIFOL(fd,6) = sd->status.account_id;
  12679. WFIFOL(fd,10) = sd->pvp_won; // times won
  12680. WFIFOL(fd,14) = sd->pvp_lost; // times lost
  12681. WFIFOL(fd,18) = sd->pvp_point;
  12682. WFIFOSET(fd, packet_len(0x210));
  12683. }
  12684. /// /pvpinfo (CZ_REQ_PVPPOINT).
  12685. /// 020f <char id>.L <account id>.L
  12686. void clif_parse_PVPInfo(int fd,map_session_data *sd)
  12687. {
  12688. // TODO: Is there a way to use this on an another player (char/acc id)?
  12689. //int cid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12690. //int aid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  12691. clif_PVPInfo(sd);
  12692. }
  12693. /// SG Feel save OK [Komurka] (CZ_AGREE_STARPLACE).
  12694. /// 0254 <which>.B
  12695. /// which: (unused atm for security purpose)
  12696. /// 0 = sun
  12697. /// 1 = moon
  12698. /// 2 = star
  12699. void clif_parse_FeelSaveOk(int fd,map_session_data *sd)
  12700. {
  12701. int i;
  12702. //int wich = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12703. if (sd->menuskill_id != SG_FEEL)
  12704. return;
  12705. i = sd->menuskill_val-1;
  12706. if (i<0 || i >= MAX_PC_FEELHATE) return; //Bug?
  12707. sd->feel_map[i].index = map_id2index(sd->bl.m);
  12708. sd->feel_map[i].m = sd->bl.m;
  12709. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), sd->feel_map[i].index);
  12710. //Are these really needed? Shouldn't they show up automatically from the feel save packet?
  12711. // clif_misceffect2(&sd->bl, 0x1b0);
  12712. // clif_misceffect2(&sd->bl, 0x21f);
  12713. clif_feel_info(sd, i, 0);
  12714. clif_menuskill_clear(sd);
  12715. }
  12716. /// Star Gladiator's Feeling map confirmation prompt (ZC_STARPLACE).
  12717. /// 0253 <which>.B
  12718. /// which:
  12719. /// 0 = sun
  12720. /// 1 = moon
  12721. /// 2 = star
  12722. void clif_feel_req(int fd, map_session_data *sd, uint16 skill_lv)
  12723. {
  12724. WFIFOHEAD(fd,packet_len(0x253));
  12725. WFIFOW(fd,0)=0x253;
  12726. WFIFOB(fd,2)=TOB(skill_lv-1);
  12727. WFIFOSET(fd, packet_len(0x253));
  12728. sd->menuskill_id = SG_FEEL;
  12729. sd->menuskill_val = skill_lv;
  12730. }
  12731. /// Request to change homunculus' name (CZ_RENAME_MER).
  12732. /// 0231 <name>.24B
  12733. void clif_parse_ChangeHomunculusName(int fd, map_session_data *sd){
  12734. hom_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12735. }
  12736. /// Request to warp/move homunculus/mercenary to it's owner (CZ_REQUEST_MOVETOOWNER).
  12737. /// 0234 <id>.L
  12738. void clif_parse_HomMoveToMaster(int fd, map_session_data *sd){
  12739. int id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]); // Mercenary or Homunculus
  12740. struct block_list *bl = nullptr;
  12741. struct unit_data *ud = nullptr;
  12742. if( sd->md && sd->md->bl.id == id )
  12743. bl = &sd->md->bl;
  12744. else if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12745. bl = &sd->hd->bl; // Moving Homunculus
  12746. else
  12747. return;
  12748. unit_calc_pos(bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  12749. ud = unit_bl2ud(bl);
  12750. unit_walktoxy(bl, ud->to_x, ud->to_y, 4);
  12751. }
  12752. /// Request to move homunculus/mercenary (CZ_REQUEST_MOVENPC).
  12753. /// 0232 <id>.L <position data>.3B
  12754. void clif_parse_HomMoveTo(int fd, map_session_data *sd){
  12755. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12756. int id = RFIFOL(fd,info->pos[0]); // Mercenary or Homunculus
  12757. struct block_list *bl = nullptr;
  12758. short x, y;
  12759. RFIFOPOS(fd, info->pos[1], &x, &y, nullptr);
  12760. if( sd->md && sd->md->bl.id == id )
  12761. bl = &sd->md->bl; // Moving Mercenary
  12762. else if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12763. bl = &sd->hd->bl; // Moving Homunculus
  12764. else
  12765. return;
  12766. unit_walktoxy(bl, x, y, 4);
  12767. }
  12768. /// Request to do an action with homunculus/mercenary (CZ_REQUEST_ACTNPC).
  12769. /// 0233 <id>.L <target id>.L <action>.B
  12770. /// action:
  12771. /// always 0
  12772. void clif_parse_HomAttack(int fd,map_session_data *sd)
  12773. {
  12774. struct block_list *bl = nullptr;
  12775. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12776. int id = RFIFOL(fd,info->pos[0]);
  12777. int target_id = RFIFOL(fd,info->pos[1]);
  12778. int action_type = RFIFOB(fd,info->pos[2]);
  12779. if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12780. bl = &sd->hd->bl;
  12781. else if( sd->md && sd->md->bl.id == id )
  12782. bl = &sd->md->bl;
  12783. else return;
  12784. unit_stop_attack(bl);
  12785. unit_attack(bl, target_id, action_type != 0);
  12786. }
  12787. /// Request to invoke a homunculus menu action (CZ_COMMAND_MER).
  12788. /// 022d <type>.W <command>.B
  12789. /// type:
  12790. /// always 0
  12791. /// command:
  12792. /// 0 = homunculus information
  12793. /// 1 = feed
  12794. /// 2 = delete
  12795. void clif_parse_HomMenu(int fd, map_session_data *sd)
  12796. { //[orn]
  12797. int cmd = RFIFOW(fd,0);
  12798. //int type = RFIFOW(fd,packet_db[cmd].pos[0]);
  12799. if(!hom_is_active(sd->hd))
  12800. return;
  12801. hom_menu(sd,RFIFOB(fd,packet_db[cmd].pos[1]));
  12802. }
  12803. /// Request to resurrect oneself using Token of Siegfried (CZ_STANDING_RESURRECTION).
  12804. /// 0292
  12805. void clif_parse_AutoRevive(int fd, map_session_data *sd)
  12806. {
  12807. pc_revive_item(sd);
  12808. }
  12809. /// Information about character's status values (ZC_ACK_STATUS_GM).
  12810. /// 0214 <str>.B <standardStr>.B <agi>.B <standardAgi>.B <vit>.B <standardVit>.B
  12811. /// <int>.B <standardInt>.B <dex>.B <standardDex>.B <luk>.B <standardLuk>.B
  12812. /// <attPower>.W <refiningPower>.W <max_mattPower>.W <min_mattPower>.W
  12813. /// <itemdefPower>.W <plusdefPower>.W <mdefPower>.W <plusmdefPower>.W
  12814. /// <hitSuccessValue>.W <avoidSuccessValue>.W <plusAvoidSuccessValue>.W
  12815. /// <criticalSuccessValue>.W <ASPD>.W <plusASPD>.W
  12816. void clif_check(int fd, map_session_data* pl_sd)
  12817. {
  12818. WFIFOHEAD(fd,packet_len(0x214));
  12819. WFIFOW(fd, 0) = 0x214;
  12820. WFIFOB(fd, 2) = min(pl_sd->status.str, UINT8_MAX);
  12821. WFIFOB(fd, 3) = pc_need_status_point(pl_sd, SP_STR, 1);
  12822. WFIFOB(fd, 4) = min(pl_sd->status.agi, UINT8_MAX);
  12823. WFIFOB(fd, 5) = pc_need_status_point(pl_sd, SP_AGI, 1);
  12824. WFIFOB(fd, 6) = min(pl_sd->status.vit, UINT8_MAX);
  12825. WFIFOB(fd, 7) = pc_need_status_point(pl_sd, SP_VIT, 1);
  12826. WFIFOB(fd, 8) = min(pl_sd->status.int_, UINT8_MAX);
  12827. WFIFOB(fd, 9) = pc_need_status_point(pl_sd, SP_INT, 1);
  12828. WFIFOB(fd,10) = min(pl_sd->status.dex, UINT8_MAX);
  12829. WFIFOB(fd,11) = pc_need_status_point(pl_sd, SP_DEX, 1);
  12830. WFIFOB(fd,12) = min(pl_sd->status.luk, UINT8_MAX);
  12831. WFIFOB(fd,13) = pc_need_status_point(pl_sd, SP_LUK, 1);
  12832. WFIFOW(fd,14) = pl_sd->battle_status.batk+pl_sd->battle_status.rhw.atk+pl_sd->battle_status.lhw.atk;
  12833. WFIFOW(fd,16) = pl_sd->battle_status.rhw.atk2+pl_sd->battle_status.lhw.atk2;
  12834. WFIFOW(fd,18) = pl_sd->battle_status.matk_max;
  12835. WFIFOW(fd,20) = pl_sd->battle_status.matk_min;
  12836. WFIFOW(fd,22) = pl_sd->battle_status.def;
  12837. WFIFOW(fd,24) = pl_sd->battle_status.def2;
  12838. WFIFOW(fd,26) = pl_sd->battle_status.mdef;
  12839. WFIFOW(fd,28) = pl_sd->battle_status.mdef2;
  12840. WFIFOW(fd,30) = pl_sd->battle_status.hit;
  12841. WFIFOW(fd,32) = pl_sd->battle_status.flee;
  12842. WFIFOW(fd,34) = pl_sd->battle_status.flee2/10;
  12843. WFIFOW(fd,36) = pl_sd->battle_status.cri/10;
  12844. WFIFOW(fd,38) = (2000-pl_sd->battle_status.amotion)/10; // aspd
  12845. WFIFOW(fd,40) = 0; // FIXME: What is 'plusASPD' supposed to be? Maybe adelay?
  12846. WFIFOSET(fd,packet_len(0x214));
  12847. }
  12848. /// /check (CZ_REQ_STATUS_GM).
  12849. /// Request character's status values.
  12850. /// 0213 <char name>.24B
  12851. void clif_parse_Check(int fd, map_session_data *sd)
  12852. {
  12853. char charname[NAME_LENGTH];
  12854. map_session_data* pl_sd;
  12855. if(!pc_has_permission(sd, PC_PERM_USE_CHECK))
  12856. return;
  12857. safestrncpy(charname, RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]), sizeof(charname));
  12858. if( ( pl_sd = map_nick2sd(charname,false) ) == nullptr || pc_get_group_level(sd) < pc_get_group_level(pl_sd) )
  12859. {
  12860. return;
  12861. }
  12862. clif_check(fd, pl_sd);
  12863. }
  12864. /// MAIL SYSTEM
  12865. /// By Zephyrus
  12866. ///
  12867. /// Notification about the result of adding an item to mail
  12868. /// 0255 <index>.W <result>.B (ZC_ACK_MAIL_ADD_ITEM)
  12869. /// result:
  12870. /// 0 = success
  12871. /// 1 = failure
  12872. /// 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)
  12873. /// { <card id>.W }*4 { <option index>.W <option value>.W <option parameter>.B }*5
  12874. /// result:
  12875. /// 0 = success
  12876. /// 1 = over weight
  12877. /// 2 = fatal error
  12878. void clif_Mail_setattachment( map_session_data* sd, int index, int amount, uint8 flag ){
  12879. int fd = sd->fd;
  12880. #if PACKETVER < 20150513
  12881. WFIFOHEAD(fd,packet_len(0x255));
  12882. WFIFOW(fd,0) = 0x255;
  12883. WFIFOW(fd,2) = index;
  12884. WFIFOB(fd,4) = flag;
  12885. WFIFOSET(fd,packet_len(0x255));
  12886. #else
  12887. PACKET_ZC_ACK_ADD_ITEM_RODEX p = {};
  12888. if( flag ){
  12889. memset( &p, 0, sizeof( p ) );
  12890. }else{
  12891. struct item* item = &sd->inventory.u.items_inventory[server_index(index)];
  12892. p.index = index;
  12893. p.count = amount;
  12894. p.itemId = client_nameid( item->nameid );
  12895. p.type = itemtype( item->nameid );
  12896. p.IsIdentified = item->identify ? 1 : 0;
  12897. p.IsDamaged = item->attribute ? 1 : 0;
  12898. p.refiningLevel = item->refine;
  12899. clif_addcards( &p.slot, item );
  12900. clif_add_random_options( p.optionData, *item );
  12901. p.weight = 0;
  12902. for( int i = 0; i < MAIL_MAX_ITEM; i++ ){
  12903. if( sd->mail.item[i].nameid == 0 ){
  12904. continue;
  12905. }
  12906. p.weight += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  12907. }
  12908. p.favorite = ( item->favorite != 0 ) ? 1 : 0;
  12909. p.location = pc_equippoint( sd, server_index( index ) );
  12910. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  12911. p.grade = item->enchantgrade;
  12912. #endif
  12913. }
  12914. p.PacketType = HEADER_ZC_ACK_ADD_ITEM_RODEX;
  12915. p.result = flag;
  12916. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  12917. #endif
  12918. }
  12919. /// Notification about the result of retrieving a mail attachment
  12920. /// 0245 <result>.B (ZC_MAIL_REQ_GET_ITEM)
  12921. /// result:
  12922. /// 0 = success
  12923. /// 1 = failure
  12924. /// 2 = too many items
  12925. /// 09f2 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ZENY_FROM_MAIL)
  12926. /// 09f4 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ITEM_FROM_MAIL)
  12927. void clif_mail_getattachment(map_session_data* sd, struct mail_message *msg, uint8 result, enum mail_attachment_type type) {
  12928. #if PACKETVER < 20150513
  12929. int fd = sd->fd;
  12930. WFIFOHEAD(fd,packet_len(0x245));
  12931. WFIFOW(fd,0) = 0x245;
  12932. WFIFOB(fd,2) = result;
  12933. WFIFOSET(fd,packet_len(0x245));
  12934. #else
  12935. switch( type ){
  12936. case MAIL_ATT_ITEM:
  12937. case MAIL_ATT_ZENY:
  12938. break;
  12939. default:
  12940. return;
  12941. }
  12942. PACKET_ZC_ACK_ITEM_FROM_MAIL p = { 0 };
  12943. p.PacketType = type == MAIL_ATT_ZENY ? rodexgetzeny : rodexgetitem;
  12944. p.MailID = msg->id;
  12945. p.opentype = msg->type;
  12946. p.result = result;
  12947. clif_send(&p, sizeof(p), &sd->bl, SELF);
  12948. clif_Mail_refreshinbox( sd, msg->type, 0 );
  12949. #endif
  12950. }
  12951. /// Notification about the result of sending a mail
  12952. /// 0249 <result>.B (ZC_MAIL_REQ_SEND)
  12953. /// result:
  12954. /// 0 = success
  12955. /// 1 = recipinent does not exist
  12956. /// 09ed <result>.B (ZC_ACK_WRITE_MAIL)
  12957. void clif_Mail_send(map_session_data* sd, enum mail_send_result result){
  12958. #if PACKETVER < 20150513
  12959. int fd = sd->fd;
  12960. WFIFOHEAD(fd,packet_len(0x249));
  12961. WFIFOW(fd,0) = 0x249;
  12962. WFIFOB(fd,2) = result != WRITE_MAIL_SUCCESS;
  12963. WFIFOSET(fd,packet_len(0x249));
  12964. #else
  12965. PACKET_ZC_WRITE_MAIL_RESULT p = { 0 };
  12966. p.PacketType = rodexwriteresult;
  12967. p.result = result;
  12968. clif_send(&p, sizeof(p), &sd->bl, SELF);
  12969. #endif
  12970. }
  12971. /// Notification about the result of deleting a mail.
  12972. /// 0257 <mail id>.L <result>.W (ZC_ACK_MAIL_DELETE)
  12973. /// result:
  12974. /// 0 = success
  12975. /// 1 = failure
  12976. // 09f6 <mail tab>.B <mail id>.Q (ZC_ACK_DELETE_MAIL)
  12977. void clif_mail_delete( map_session_data* sd, struct mail_message *msg, bool success ){
  12978. #if PACKETVER < 20150513
  12979. int fd = sd->fd;
  12980. WFIFOHEAD(fd, packet_len(0x257));
  12981. WFIFOW(fd,0) = 0x257;
  12982. WFIFOL(fd,2) = msg->id;
  12983. WFIFOW(fd,6) = !success;
  12984. WFIFOSET(fd, packet_len(0x257));
  12985. #else
  12986. // This is only a success notification
  12987. if( success ){
  12988. PACKET_ZC_ACK_DELETE_MAIL p = { 0 };
  12989. p.PacketType = rodexdelete;
  12990. p.opentype = msg->type;
  12991. p.MailID = msg->id;
  12992. clif_send(&p, sizeof(p), &sd->bl, SELF);
  12993. }
  12994. #endif
  12995. }
  12996. /// Notification about the result of returning a mail (ZC_ACK_MAIL_RETURN).
  12997. /// 0274 <mail id>.L <result>.W
  12998. /// result:
  12999. /// 0 = success
  13000. /// 1 = failure
  13001. void clif_Mail_return(int fd, int mail_id, short fail)
  13002. {
  13003. WFIFOHEAD(fd,packet_len(0x274));
  13004. WFIFOW(fd,0) = 0x274;
  13005. WFIFOL(fd,2) = mail_id;
  13006. WFIFOW(fd,6) = fail;
  13007. WFIFOSET(fd,packet_len(0x274));
  13008. }
  13009. /// Notification about new mail.
  13010. /// 024a <mail id>.L <title>.40B <sender>.24B (ZC_MAIL_RECEIVE)
  13011. /// 09e7 <result>.B (ZC_NOTIFY_UNREADMAIL)
  13012. void clif_Mail_new(map_session_data* sd, int mail_id, const char *sender, const char *title){
  13013. #if PACKETVER < 20150513
  13014. int fd = sd->fd;
  13015. WFIFOHEAD(fd,packet_len(0x24a));
  13016. WFIFOW(fd,0) = 0x24a;
  13017. WFIFOL(fd,2) = mail_id;
  13018. safestrncpy(WFIFOCP(fd,6), title, MAIL_TITLE_LENGTH);
  13019. safestrncpy(WFIFOCP(fd,46), sender, NAME_LENGTH);
  13020. WFIFOSET(fd,packet_len(0x24a));
  13021. #else
  13022. PACKET_ZC_NOTIFY_UNREADMAIL p = { 0 };
  13023. p.PacketType = rodexicon;
  13024. p.result = sd->mail.inbox.unread > 0 || sd->mail.inbox.unchecked > 0; // unread
  13025. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13026. #endif
  13027. }
  13028. /// Opens/closes the mail window (ZC_MAIL_WINDOWS).
  13029. /// 0260 <type>.L
  13030. /// type:
  13031. /// 0 = open
  13032. /// 1 = close
  13033. void clif_Mail_window(int fd, int flag)
  13034. {
  13035. WFIFOHEAD(fd,packet_len(0x260));
  13036. WFIFOW(fd,0) = 0x260;
  13037. WFIFOL(fd,2) = flag;
  13038. WFIFOSET(fd,packet_len(0x260));
  13039. }
  13040. /// Lists mails stored in inbox.
  13041. /// 0240 <packet len>.W <amount>.L { <mail id>.L <title>.40B <read>.B <sender>.24B <time>.L }*amount (ZC_MAIL_REQ_GET_LIST)
  13042. /// read:
  13043. /// 0 = unread
  13044. /// 1 = read
  13045. /// 09f0 <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST)
  13046. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13047. /// 0a7d <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST2)
  13048. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13049. /// 0ac2 <packet len>.W <unknown>.B (ZC_ACK_MAIL_LIST3)
  13050. /// { <type>.B <mail id>.Q <read>.B <type>.B <sender>.24B <expires>.L <title length>.W <title>.?B }*
  13051. void clif_Mail_refreshinbox(map_session_data *sd,enum mail_inbox_type type,int64 mailID){
  13052. #if PACKETVER < 20150513
  13053. int fd = sd->fd;
  13054. struct mail_data *md = &sd->mail.inbox;
  13055. struct mail_message *msg;
  13056. int len, i, j;
  13057. len = 8 + (73 * md->amount);
  13058. WFIFOHEAD(fd,len);
  13059. WFIFOW(fd,0) = 0x240;
  13060. WFIFOW(fd,2) = len;
  13061. WFIFOL(fd,4) = md->amount;
  13062. for( i = j = 0; i < MAIL_MAX_INBOX && j < md->amount; i++ )
  13063. {
  13064. msg = &md->msg[i];
  13065. if (msg->id < 1)
  13066. continue;
  13067. WFIFOL(fd,8+73*j) = msg->id;
  13068. memcpy(WFIFOP(fd,12+73*j), msg->title, MAIL_TITLE_LENGTH);
  13069. WFIFOB(fd,52+73*j) = (msg->status != MAIL_UNREAD);
  13070. safestrncpy(WFIFOCP(fd,53+73*j), msg->send_name, NAME_LENGTH);
  13071. WFIFOL(fd,77+73*j) = (uint32)msg->timestamp;
  13072. j++;
  13073. }
  13074. WFIFOSET(fd,len);
  13075. if( md->full ) {// TODO: is this official?
  13076. char output[100];
  13077. safesnprintf(output,sizeof(output),"Inbox is full (Max %d). Delete some mails.", MAIL_MAX_INBOX);
  13078. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  13079. }
  13080. #else
  13081. int fd = sd->fd;
  13082. struct mail_data *md = &sd->mail.inbox;
  13083. struct mail_message *msg;
  13084. int i, j, k, offset, titleLength;
  13085. uint8 mailType, amount, remaining;
  13086. uint32 now = (uint32)time(nullptr);
  13087. #if PACKETVER >= 20170419
  13088. int cmd = 0xac2;
  13089. #elif PACKETVER >= 20160601
  13090. int cmd = 0xa7d;
  13091. #else
  13092. int cmd = 0x9f0;
  13093. #endif
  13094. if( battle_config.mail_daily_count ){
  13095. mail_refresh_remaining_amount(sd);
  13096. }
  13097. #if PACKETVER >= 20170419
  13098. // Always send all
  13099. i = md->amount;
  13100. #else
  13101. // If a starting mail id was sent
  13102. if( mailID != 0 ){
  13103. ARR_FIND( 0, md->amount, i, md->msg[i].id == mailID );
  13104. // Unknown mail
  13105. if( i == md->amount ){
  13106. // Ignore the request for now
  13107. return; // TODO: Should we just show the first page instead?
  13108. }
  13109. // It was actually the oldest/first mail, there is no further page
  13110. if( i == 0 ){
  13111. return;
  13112. }
  13113. // We actually want the next/older mail
  13114. i -= 1;
  13115. }else{
  13116. i = md->amount;
  13117. }
  13118. #endif
  13119. // Count the remaining mails from the starting mail or the beginning
  13120. // Only count mails of the target type(before 2017-04-19) and those that should not have been deleted already
  13121. for( j = i, remaining = 0; j >= 0; j-- ){
  13122. msg = &md->msg[j];
  13123. if (msg->id < 1)
  13124. continue;
  13125. #if PACKETVER < 20170419
  13126. if (msg->type != type)
  13127. continue;
  13128. #endif
  13129. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13130. continue;
  13131. remaining++;
  13132. }
  13133. #if PACKETVER >= 20170419
  13134. // Always send all
  13135. amount = remaining;
  13136. #else
  13137. if( remaining > MAIL_PAGE_SIZE ){
  13138. amount = MAIL_PAGE_SIZE;
  13139. }else{
  13140. amount = remaining;
  13141. }
  13142. #endif
  13143. WFIFOHEAD(fd, 7 + ((44 + MAIL_TITLE_LENGTH) * amount));
  13144. WFIFOW(fd, 0) = cmd;
  13145. #if PACKETVER >= 20170419
  13146. WFIFOB(fd, 4) = 1; // Unknown
  13147. offset = 5;
  13148. #else
  13149. WFIFOB(fd, 4) = type;
  13150. WFIFOB(fd, 5) = amount;
  13151. WFIFOB(fd, 6) = ( remaining < MAIL_PAGE_SIZE ); // last page
  13152. offset = 7;
  13153. #endif
  13154. for( amount = 0; i >= 0; i-- ){
  13155. msg = &md->msg[i];
  13156. if (msg->id < 1)
  13157. continue;
  13158. #if PACKETVER < 20170419
  13159. if (msg->type != type)
  13160. continue;
  13161. #endif
  13162. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13163. continue;
  13164. #if PACKETVER >= 20170419
  13165. WFIFOB(fd, offset) = msg->type;
  13166. offset += 1;
  13167. #endif
  13168. WFIFOQ(fd, offset + 0) = (uint64)msg->id;
  13169. WFIFOB(fd, offset + 8) = (msg->status != MAIL_UNREAD);
  13170. mailType = MAIL_TYPE_TEXT; // Normal letter
  13171. if( msg->zeny > 0 ){
  13172. mailType |= MAIL_TYPE_ZENY; // Mail contains zeny
  13173. }
  13174. for( k = 0; k < MAIL_MAX_ITEM; k++ ){
  13175. if( msg->item[k].nameid > 0 ){
  13176. mailType |= MAIL_TYPE_ITEM; // Mail contains items
  13177. break;
  13178. }
  13179. }
  13180. #if PACKETVER >= 20170419
  13181. // If it came from an npc?
  13182. if( !msg->send_id ){
  13183. mailType |= MAIL_TYPE_NPC;
  13184. }
  13185. #endif
  13186. WFIFOB(fd, offset + 9) = mailType;
  13187. safestrncpy(WFIFOCP(fd, offset + 10), msg->send_name, NAME_LENGTH);
  13188. #if PACKETVER < 20170419
  13189. // How much time has passed since you received the mail
  13190. WFIFOL(fd, offset + 34 ) = now - (uint32)msg->timestamp;
  13191. offset += 4;
  13192. #endif
  13193. // If automatic return/deletion of mails is enabled, notify the client when it will kick in
  13194. if( msg->scheduled_deletion > 0 ){
  13195. WFIFOL(fd, offset + 34) = (uint32)msg->scheduled_deletion - now;
  13196. }else{
  13197. // Fake the scheduled deletion to one year in the future
  13198. // Sadly the client always displays the scheduled deletion after 24 hours no matter how high this value gets [Lemongrass]
  13199. WFIFOL(fd, offset + 34) = 365 * 24 * 60 * 60;
  13200. }
  13201. WFIFOW(fd, offset + 38) = titleLength = (int16)(strlen(msg->title) + 1);
  13202. safestrncpy(WFIFOCP(fd, offset + 40), msg->title, titleLength);
  13203. offset += 40 + titleLength;
  13204. }
  13205. WFIFOW(fd, 2) = (int16)offset;
  13206. WFIFOSET(fd, offset);
  13207. #endif
  13208. }
  13209. /// Mail inbox list request.
  13210. /// 023f (CZ_MAIL_GET_LIST)
  13211. /// 09e8 <mail tab>.B <mail id>.Q (CZ_OPEN_MAILBOX)
  13212. /// 09ee <mail tab>.B <mail id>.Q (CZ_REQ_NEXT_MAIL_LIST)
  13213. /// 09ef <mail tab>.B <mail id>.Q (CZ_REQ_REFRESH_MAIL_LIST)
  13214. /// 0ac0 <mail id>.Q <unknown>.16B (CZ_OPEN_MAILBOX2)
  13215. /// 0ac1 <mail id>.Q <unknown>.16B (CZ_REQ_REFRESH_MAIL_LIST2)
  13216. void clif_parse_Mail_refreshinbox(int fd, map_session_data *sd){
  13217. if( mail_invalid_operation( sd ) ){
  13218. return;
  13219. }
  13220. #if PACKETVER < 20150513
  13221. struct mail_data* md = &sd->mail.inbox;
  13222. if( md->amount < MAIL_MAX_INBOX && (md->full || sd->mail.changed) )
  13223. intif_Mail_requestinbox(sd->status.char_id, 1, MAIL_INBOX_NORMAL);
  13224. else
  13225. clif_Mail_refreshinbox(sd, MAIL_INBOX_NORMAL,0);
  13226. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13227. mail_removezeny(sd, false);
  13228. #else
  13229. int cmd = RFIFOW(fd, 0);
  13230. #if PACKETVER < 20170419
  13231. uint8 openType = RFIFOB(fd, 2);
  13232. uint64 mailId = RFIFOQ(fd, 3);
  13233. #else
  13234. uint8 openType;
  13235. uint64 mailId = RFIFOQ(fd, 2);
  13236. int i;
  13237. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mailId);
  13238. if( i == MAIL_MAX_INBOX ){
  13239. openType = MAIL_INBOX_NORMAL;
  13240. mailId = 0;
  13241. }else{
  13242. openType = sd->mail.inbox.msg[i].type;
  13243. mailId = 0;
  13244. }
  13245. #endif
  13246. switch( openType ){
  13247. case MAIL_INBOX_NORMAL:
  13248. case MAIL_INBOX_ACCOUNT:
  13249. case MAIL_INBOX_RETURNED:
  13250. break;
  13251. default:
  13252. // Unknown type: ignore request
  13253. return;
  13254. }
  13255. if( sd->mail.changed || ( cmd == 0x9ef || cmd == 0xac1 ) ){
  13256. intif_Mail_requestinbox(sd->status.char_id, 1, (enum mail_inbox_type)openType);
  13257. return;
  13258. }
  13259. // If it is not a next page request
  13260. if( cmd != 0x9ee ){
  13261. mailId = 0;
  13262. }
  13263. clif_Mail_refreshinbox(sd,(enum mail_inbox_type)openType,mailId);
  13264. #endif
  13265. }
  13266. /// Opens a mail
  13267. /// 0242 <packet len>.W <mail id>.L <title>.40B <sender>.24B <time>.L <zeny>.L (ZC_MAIL_REQ_OPEN)
  13268. /// <amount>.L <name id>.W <item type>.W <identified>.B <damaged>.B <refine>.B
  13269. /// <card1>.W <card2>.W <card3>.W <card4>.W <message>.?B
  13270. /// 09eb <packet len>.W <type>.B <mail id>.Q <message length>.W <zeny>.Q <item count>.B (ZC_ACK_READ_MAIL)
  13271. /// { }*n
  13272. // TODO: Packet description => for repeated block
  13273. void clif_Mail_read( map_session_data *sd, int mail_id ){
  13274. int i, fd = sd->fd;
  13275. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13276. if( i == MAIL_MAX_INBOX ) {
  13277. clif_Mail_return(sd->fd, mail_id, 1); // Mail doesn't exist
  13278. ShowWarning("clif_parse_Mail_read: char '%s' trying to read a message not the inbox.\n", sd->status.name);
  13279. return;
  13280. } else {
  13281. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13282. struct item *item;
  13283. int16 msg_len;
  13284. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13285. if( msg_len == 0 ) {
  13286. strcpy(msg->body, "(no message)"); // TODO: confirm for RODEX
  13287. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13288. }
  13289. #if PACKETVER < 20150513
  13290. item = &msg->item[0];
  13291. int16 len = 101 + msg_len;
  13292. WFIFOHEAD(fd,len);
  13293. WFIFOW(fd,0) = 0x242;
  13294. WFIFOW(fd,2) = len;
  13295. WFIFOL(fd,4) = msg->id;
  13296. safestrncpy(WFIFOCP(fd,8), msg->title, MAIL_TITLE_LENGTH + 1);
  13297. safestrncpy(WFIFOCP(fd,48), msg->send_name, NAME_LENGTH + 1);
  13298. WFIFOL(fd,72) = 0;
  13299. WFIFOL(fd,76) = msg->zeny;
  13300. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13301. if( item->nameid && data != nullptr ) {
  13302. WFIFOL(fd,80) = item->amount;
  13303. WFIFOW(fd,84) = client_nameid( item->nameid );
  13304. WFIFOW(fd,86) = itemtype( item->nameid );
  13305. WFIFOB(fd,88) = item->identify;
  13306. WFIFOB(fd,89) = item->attribute;
  13307. WFIFOB(fd,90) = item->refine;
  13308. WFIFOW(fd,91) = item->card[0];
  13309. WFIFOW(fd,93) = item->card[1];
  13310. WFIFOW(fd,95) = item->card[2];
  13311. WFIFOW(fd,97) = item->card[3];
  13312. } else // no item, set all to zero
  13313. memset(WFIFOP(fd,80), 0x00, 19);
  13314. WFIFOB(fd,99) = (unsigned char)msg_len;
  13315. safestrncpy(WFIFOCP(fd,100), msg->body, msg_len + 1);
  13316. WFIFOSET(fd,len);
  13317. #else
  13318. msg_len += 1; // Zero Termination
  13319. PACKET_ZC_ACK_READ_RODEX* p = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX*>( packet_buffer );
  13320. p->PacketType = HEADER_ZC_ACK_READ_RODEX;
  13321. p->PacketLength = sizeof( *p );
  13322. p->opentype = msg->type;
  13323. p->MailID = msg->id;
  13324. p->TextcontentsLength = static_cast<decltype(p->TextcontentsLength)>( msg_len );
  13325. p->zeny = msg->zeny;
  13326. safestrncpy( WBUFCP( p, p->PacketLength ), msg->body, msg_len );
  13327. p->PacketLength += p->TextcontentsLength;
  13328. p->ItemCnt = 0;
  13329. for( int j = 0; j < MAIL_MAX_ITEM; j++ ){
  13330. item = &msg->item[j];
  13331. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13332. if( item->nameid > 0 && item->amount > 0 && data != nullptr ){
  13333. PACKET_ZC_ACK_READ_RODEX_SUB* mailitem = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX_SUB*>( WBUFP( p, p->PacketLength ) );
  13334. mailitem->ITID = client_nameid( item->nameid );
  13335. mailitem->count = item->amount;
  13336. mailitem->type = itemtype( item->nameid );
  13337. mailitem->IsIdentified = item->identify ? 1 : 0;
  13338. mailitem->IsDamaged = item->attribute ? 1 : 0;
  13339. mailitem->refiningLevel = item->refine;
  13340. mailitem->location = pc_equippoint_sub( sd, data.get() );
  13341. mailitem->viewSprite = data->look;
  13342. mailitem->bindOnEquip = item->bound ? 2 : data->flag.bindOnEquip ? 1 : 0;
  13343. clif_addcards( &mailitem->slot, item );
  13344. clif_add_random_options( mailitem->option_data, *item );
  13345. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  13346. mailitem->grade = item->enchantgrade;
  13347. #endif
  13348. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( *mailitem ) );
  13349. p->ItemCnt++;
  13350. }
  13351. }
  13352. clif_send( p, p->PacketLength, &sd->bl, SELF );
  13353. #endif
  13354. if (msg->status == MAIL_UNREAD) {
  13355. msg->status = MAIL_READ;
  13356. intif_Mail_read(mail_id);
  13357. clif_parse_Mail_refreshinbox(fd, sd);
  13358. sd->mail.inbox.unread--;
  13359. clif_Mail_new(sd, 0, msg->send_name, msg->title);
  13360. }
  13361. }
  13362. }
  13363. /// Request to open a mail.
  13364. /// 0241 <mail id>.L (CZ_MAIL_OPEN)
  13365. /// 09ea <mail tab>.B <mail id>.Q (CZ_REQ_READ_MAIL)
  13366. void clif_parse_Mail_read(int fd, map_session_data *sd){
  13367. #if PACKETVER < 20150513
  13368. int mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13369. #else
  13370. //uint8 openType = RFIFOB(fd, 2);
  13371. int mail_id = (int)RFIFOQ(fd, 3);
  13372. #endif
  13373. if( mail_id <= 0 )
  13374. return;
  13375. if( mail_invalid_operation(sd) )
  13376. return;
  13377. clif_Mail_read(sd, mail_id);
  13378. }
  13379. /// Allow a player to begin writing a mail
  13380. /// 0a12 <receiver>.24B <success>.B (ZC_ACK_OPEN_WRITE_MAIL)
  13381. void clif_send_Mail_beginwrite_ack( map_session_data *sd, char* name, bool success ){
  13382. PACKET_ZC_ACK_OPEN_WRITE_MAIL p = { 0 };
  13383. p.PacketType = rodexopenwrite;
  13384. safestrncpy(p.receiveName, name, NAME_LENGTH);
  13385. p.result = success;
  13386. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13387. }
  13388. /// Request to start writing a mail
  13389. /// 0a08 <receiver>.24B (CZ_REQ_OPEN_WRITE_MAIL)
  13390. void clif_parse_Mail_beginwrite( int fd, map_session_data *sd ){
  13391. char name[NAME_LENGTH];
  13392. safestrncpy(name, RFIFOCP(fd, 2), NAME_LENGTH);
  13393. if( mail_invalid_operation( sd ) ){
  13394. return;
  13395. }
  13396. if( sd->state.storage_flag || sd->state.mail_writing || sd->state.trading ){
  13397. clif_send_Mail_beginwrite_ack(sd, name, false);
  13398. return;
  13399. }
  13400. mail_clear(sd);
  13401. sd->state.mail_writing = true;
  13402. clif_send_Mail_beginwrite_ack(sd, name,true);
  13403. }
  13404. /// Notification that the client cancelled writing a mail
  13405. /// 0a03 (CZ_REQ_CANCEL_WRITE_MAIL)
  13406. void clif_parse_Mail_cancelwrite( int fd, map_session_data *sd ){
  13407. sd->state.mail_writing = false;
  13408. }
  13409. /// Give the client information about the recipient, if available
  13410. /// 0a14 <char id>.L <class>.W <base level>.W (ZC_CHECK_RECEIVE_CHARACTER_NAME)
  13411. /// 0a51 <char id>.L <class>.W <base level>.W <name>.24B (ZC_CHECK_RECEIVE_CHARACTER_NAME2)
  13412. void clif_Mail_Receiver_Ack( map_session_data* sd, uint32 char_id, short class_, uint32 level, const char* name ){
  13413. #if PACKETVER >= 20141119
  13414. PACKET_ZC_CHECKNAME p = { 0 };
  13415. p.PacketType = HEADER_ZC_CHECKNAME;
  13416. p.CharId = char_id;
  13417. p.Class = class_;
  13418. p.BaseLevel = level;
  13419. #if PACKETVER >= 20160302
  13420. strncpy(p.Name, name, NAME_LENGTH);
  13421. #endif
  13422. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13423. #endif
  13424. }
  13425. /// Request information about the recipient
  13426. /// 0a13 <name>.24B (CZ_CHECK_RECEIVE_CHARACTER_NAME)
  13427. void clif_parse_Mail_Receiver_Check(int fd, map_session_data *sd) {
  13428. #if PACKETVER >= 20140423
  13429. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13430. const PACKET_CZ_CHECKNAME2* p = reinterpret_cast<PACKET_CZ_CHECKNAME2*>( RFIFOP( fd, 0 ) );
  13431. #else
  13432. const PACKET_CZ_CHECKNAME1* p = reinterpret_cast<PACKET_CZ_CHECKNAME1*>( RFIFOP( fd, 0 ) );
  13433. #endif
  13434. static char name[NAME_LENGTH];
  13435. if( mail_invalid_operation( sd ) ){
  13436. return;
  13437. }
  13438. safestrncpy(name, p->Name, NAME_LENGTH);
  13439. intif_mail_checkreceiver(sd, name);
  13440. #endif
  13441. }
  13442. /// Request to receive mail's attachment.
  13443. /// 0244 <mail id>.L (CZ_MAIL_GET_ITEM)
  13444. /// 09f1 <mail id>.Q <mail tab>.B (CZ_REQ_ZENY_FROM_MAIL)
  13445. /// 09f3 <mail id>.Q <mail tab>.B (CZ_REQ_ITEM_FROM_MAIL)
  13446. void clif_parse_Mail_getattach( int fd, map_session_data *sd ){
  13447. int i;
  13448. struct mail_message* msg;
  13449. #if PACKETVER < 20150513
  13450. int mail_id = RFIFOL(fd, packet_db[RFIFOW(fd, 0)].pos[0]);
  13451. int attachment = MAIL_ATT_ALL;
  13452. #else
  13453. uint16 packet_id = RFIFOW(fd, 0);
  13454. int mail_id = (int)RFIFOQ(fd, 2);
  13455. //int openType = RFIFOB(fd, 10);
  13456. int attachment = packet_id == 0x9f1 ? MAIL_ATT_ZENY : packet_id == 0x9f3 ? MAIL_ATT_ITEM : MAIL_ATT_NONE;
  13457. #endif
  13458. if( !chrif_isconnected() )
  13459. return;
  13460. if( mail_id <= 0 )
  13461. return;
  13462. if( mail_invalid_operation(sd) )
  13463. return;
  13464. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13465. if( i == MAIL_MAX_INBOX )
  13466. return;
  13467. msg = &sd->mail.inbox.msg[i];
  13468. if( attachment&MAIL_ATT_ZENY && msg->zeny < 1 ){
  13469. attachment &= ~MAIL_ATT_ZENY;
  13470. }
  13471. if( attachment&MAIL_ATT_ITEM ){
  13472. ARR_FIND(0, MAIL_MAX_ITEM, i, msg->item[i].nameid > 0 || msg->item[i].amount > 0);
  13473. // No items were found
  13474. if( i == MAIL_MAX_ITEM ){
  13475. attachment &= ~MAIL_ATT_ITEM;
  13476. }
  13477. }
  13478. // Either no attachment requested at all or there are no zeny or items in the mail
  13479. if( attachment == MAIL_ATT_NONE ){
  13480. return;
  13481. }
  13482. if( attachment&MAIL_ATT_ZENY ){
  13483. if( ( msg->zeny + sd->status.zeny + sd->mail.pending_zeny ) > MAX_ZENY ){
  13484. clif_mail_getattachment(sd, msg, 1, MAIL_ATT_ZENY); //too many zeny
  13485. return;
  13486. }else{
  13487. // Store the pending zeny (required for the "retrieve all" feature)
  13488. sd->mail.pending_zeny += msg->zeny;
  13489. // To make sure another request fails
  13490. msg->zeny = 0;
  13491. }
  13492. }
  13493. if( attachment&MAIL_ATT_ITEM ){
  13494. int slots = 0, totalweight = 0;
  13495. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  13496. struct item* item = &msg->item[i];
  13497. if( item->nameid > 0 && item->amount > 0 ){
  13498. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13499. if(data == nullptr)
  13500. continue;
  13501. switch( pc_checkadditem(sd, item->nameid, item->amount) ){
  13502. case CHKADDITEM_NEW:
  13503. slots += data->inventorySlotNeeded( item->amount );
  13504. break;
  13505. case CHKADDITEM_OVERAMOUNT:
  13506. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13507. return;
  13508. }
  13509. totalweight += data->weight * item->amount;
  13510. }
  13511. }
  13512. if( ( totalweight + sd->weight + sd->mail.pending_weight ) > sd->max_weight ){
  13513. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13514. return;
  13515. }else if( pc_inventoryblank(sd) < ( slots + sd->mail.pending_slots ) ){
  13516. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13517. return;
  13518. }
  13519. // To make sure another request fails
  13520. memset(msg->item, 0, MAIL_MAX_ITEM*sizeof(struct item));
  13521. // Store the pending weight (required for the "retrieve all" feature)
  13522. sd->mail.pending_weight += totalweight;
  13523. // Store the pending slots (required for the "retrieve all" feature)
  13524. sd->mail.pending_slots += slots;
  13525. }
  13526. intif_mail_getattach(sd,msg, (enum mail_attachment_type)attachment);
  13527. }
  13528. /// Request to delete a mail.
  13529. /// 0243 <mail id>.L (CZ_MAIL_DELETE)
  13530. /// 09f5 <mail tab>.B <mail id>.Q (CZ_REQ_DELETE_MAIL)
  13531. void clif_parse_Mail_delete(int fd, map_session_data *sd){
  13532. #if PACKETVER < 20150513
  13533. int mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13534. #else
  13535. //int openType = RFIFOB(fd, 2);
  13536. int mail_id = (int)RFIFOQ(fd, 3);
  13537. #endif
  13538. int i, j;
  13539. if( !chrif_isconnected() )
  13540. return;
  13541. if( mail_id <= 0 )
  13542. return;
  13543. if( mail_invalid_operation(sd) )
  13544. return;
  13545. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13546. if (i < MAIL_MAX_INBOX) {
  13547. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13548. // can't delete mail without removing zeny first
  13549. if( msg->zeny > 0 ){
  13550. clif_mail_delete(sd, msg, false);
  13551. return;
  13552. }
  13553. // can't delete mail without removing attachment(s) first
  13554. for( j = 0; j < MAIL_MAX_ITEM; j++ ){
  13555. if( msg->item[j].nameid > 0 && msg->item[j].amount > 0 ){
  13556. clif_mail_delete(sd, msg, false);
  13557. return;
  13558. }
  13559. }
  13560. if( intif_Mail_delete(sd->status.char_id, mail_id) && msg->status == MAIL_UNREAD ){
  13561. sd->mail.inbox.unread--;
  13562. clif_Mail_new(sd,0,nullptr,nullptr);
  13563. }
  13564. }
  13565. }
  13566. /// Request to return a mail.
  13567. /// 0273 <mail id>.L <receive name>.24B (CZ_REQ_MAIL_RETURN)
  13568. void clif_parse_Mail_return(int fd, map_session_data *sd){
  13569. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13570. const PACKET_CZ_RODEX_RETURN* p = reinterpret_cast<PACKET_CZ_RODEX_RETURN*>( RFIFOP( fd, 0 ) );
  13571. //ShowDump( p, sizeof( p ) );
  13572. int mail_id = p->msgId;
  13573. // not supported for now
  13574. return;
  13575. #else
  13576. int mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13577. #endif
  13578. //char *rec_name = RFIFOP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  13579. int i;
  13580. if( mail_id <= 0 )
  13581. return;
  13582. if( mail_invalid_operation(sd) )
  13583. return;
  13584. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13585. if( i < MAIL_MAX_INBOX && sd->mail.inbox.msg[i].send_id != 0 )
  13586. intif_Mail_return(sd->status.char_id, mail_id);
  13587. else
  13588. clif_Mail_return(sd->fd, mail_id, 1);
  13589. }
  13590. /// Request to add an item or Zeny to mail.
  13591. /// 0247 <index>.W <amount>.L (CZ_MAIL_ADD_ITEM)
  13592. /// 0a04 <index>.W <amount>.W (CZ_REQ_ADD_ITEM_TO_MAIL)
  13593. void clif_parse_Mail_setattach(int fd, map_session_data *sd){
  13594. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13595. uint16 idx = RFIFOW(fd,info->pos[0]);
  13596. #if PACKETVER < 20150513
  13597. int amount = RFIFOL(fd,info->pos[1]);
  13598. #else
  13599. int amount = RFIFOW(fd,info->pos[1]);
  13600. #endif
  13601. unsigned char flag;
  13602. if( !chrif_isconnected() )
  13603. return;
  13604. if (amount < 0 || server_index(idx) >= MAX_INVENTORY)
  13605. return;
  13606. if (sd->inventory_data[server_index(idx)] == nullptr)
  13607. return;
  13608. if( mail_invalid_operation( sd ) ){
  13609. return;
  13610. }
  13611. flag = mail_setitem(sd, idx, amount);
  13612. if( flag == MAIL_ATTACH_EQUIPSWITCH ){
  13613. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  13614. }else{
  13615. clif_Mail_setattachment(sd,idx,amount,flag);
  13616. }
  13617. }
  13618. /// Remove an item from a mail
  13619. /// 0a07 <result>.B <index>.W <amount>.W <weight>.W
  13620. void clif_mail_removeitem( map_session_data* sd, bool success, int index, int amount ){
  13621. PACKET_ZC_ACK_REMOVE_ITEM_MAIL p = { 0 };
  13622. p.PacketType = rodexremoveitem;
  13623. p.result = success;
  13624. p.index = index;
  13625. p.cnt = amount;
  13626. int total = 0;
  13627. for( int i = 0; i < MAIL_MAX_ITEM; i++ ){
  13628. if( sd->mail.item[i].nameid == 0 ){
  13629. break;
  13630. }
  13631. total += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  13632. }
  13633. p.weight = total;
  13634. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13635. }
  13636. /// Request to reset mail item and/or Zeny
  13637. /// 0246 <type>.W (CZ_MAIL_RESET_ITEM)
  13638. /// type:
  13639. /// 0 = reset all
  13640. /// 1 = remove item
  13641. /// 2 = remove zeny
  13642. /// 0a06 <index>.W <amount>.W (CZ_REQ_REMOVE_ITEM_MAIL)
  13643. void clif_parse_Mail_winopen(int fd, map_session_data *sd)
  13644. {
  13645. #if PACKETVER < 20150513
  13646. int type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13647. if (type == 0 || type == 1)
  13648. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13649. if (type == 0 || type == 2)
  13650. mail_removezeny(sd, false);
  13651. #else
  13652. uint16 index = RFIFOW(fd, 2);
  13653. uint16 count = RFIFOW(fd, 4);
  13654. mail_removeitem(sd,0,index,count);
  13655. #endif
  13656. }
  13657. /// Request to send mail
  13658. /// 0248 <packet len>.W <recipient>.24B <title>.40B <body len>.B <body>.?B (CZ_MAIL_SEND)
  13659. /// 09ec <packet len>.W <recipient>.24B <sender>.24B <zeny>.Q <title length>.W <body length>.W <title>.?B <body>.?B (CZ_REQ_WRITE_MAIL)
  13660. /// 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)
  13661. void clif_parse_Mail_send(int fd, map_session_data *sd){
  13662. #if PACKETVER < 20150513
  13663. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13664. if( !chrif_isconnected() )
  13665. return;
  13666. if( RFIFOW(fd,info->pos[0]) < 69 ) {
  13667. ShowWarning("Invalid Msg Len from account %d.\n", sd->status.account_id);
  13668. return;
  13669. }
  13670. mail_send(sd, RFIFOCP(fd,info->pos[1]), RFIFOCP(fd,info->pos[2]), RFIFOCP(fd,info->pos[4]), RFIFOB(fd,info->pos[3]));
  13671. #else
  13672. uint16 length = RFIFOW(fd, 2);
  13673. if( length < 0x3e ){
  13674. ShowWarning("Too short...\n");
  13675. clif_Mail_send(sd, WRITE_MAIL_FAILED);
  13676. return;
  13677. }
  13678. if( mail_invalid_operation( sd ) ){
  13679. return;
  13680. }
  13681. // Forged request without a begin writing packet?
  13682. if( !sd->state.mail_writing ){
  13683. return; // Ignore it
  13684. }
  13685. char receiver[NAME_LENGTH];
  13686. safestrncpy(receiver, RFIFOCP(fd, 4), NAME_LENGTH);
  13687. // char sender[NAME_LENGTH];
  13688. // safestrncpy(sender, RFIFOCP(fd, 28), NAME_LENGTH);
  13689. uint64 zeny = RFIFOQ(fd, 52);
  13690. uint16 titleLength = RFIFOW(fd, 60);
  13691. uint16 textLength = RFIFOW(fd, 62);
  13692. uint16 realTitleLength = min(titleLength, MAIL_TITLE_LENGTH);
  13693. uint16 realTextLength = min(textLength, MAIL_BODY_LENGTH);
  13694. char title[MAIL_TITLE_LENGTH];
  13695. char text[MAIL_BODY_LENGTH];
  13696. #if PACKETVER <= 20160330
  13697. safestrncpy(title, RFIFOCP(fd, 64), realTitleLength);
  13698. safestrncpy(text, RFIFOCP(fd, 64 + titleLength), realTextLength);
  13699. #else
  13700. // 64 = <char id>.L
  13701. safestrncpy(title, RFIFOCP(fd, 68), realTitleLength);
  13702. safestrncpy(text, RFIFOCP(fd, 68 + titleLength), realTextLength);
  13703. #endif
  13704. if( zeny > 0 ){
  13705. if( mail_setitem(sd,0,(uint32)zeny) != MAIL_ATTACH_SUCCESS ){
  13706. clif_Mail_send(sd,WRITE_MAIL_FAILED);
  13707. return;
  13708. }
  13709. }
  13710. mail_send(sd, receiver, title, text, realTextLength);
  13711. #endif
  13712. }
  13713. /// AUCTION SYSTEM
  13714. /// By Zephyrus
  13715. ///
  13716. /// Opens/closes the auction window (ZC_AUCTION_WINDOWS).
  13717. /// 025f <type>.L
  13718. /// type:
  13719. /// 0 = open
  13720. /// 1 = close
  13721. void clif_Auction_openwindow(map_session_data *sd)
  13722. {
  13723. int fd = sd->fd;
  13724. if( sd->state.storage_flag || sd->state.vending || sd->state.buyingstore || sd->state.trading )
  13725. return;
  13726. if( !battle_config.feature_auction )
  13727. return;
  13728. WFIFOHEAD(fd,packet_len(0x25f));
  13729. WFIFOW(fd,0) = 0x25f;
  13730. WFIFOL(fd,2) = 0;
  13731. WFIFOSET(fd,packet_len(0x25f));
  13732. }
  13733. /// Returns auction item search results (ZC_AUCTION_ITEM_REQ_SEARCH).
  13734. /// 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 }*
  13735. void clif_Auction_results(map_session_data *sd, short count, short pages, uint8 *buf)
  13736. {
  13737. int i, fd = sd->fd, len = sizeof(struct auction_data);
  13738. struct auction_data auction;
  13739. WFIFOHEAD(fd,12 + (count * 83));
  13740. WFIFOW(fd,0) = 0x252;
  13741. WFIFOW(fd,2) = 12 + (count * 83);
  13742. WFIFOL(fd,4) = pages;
  13743. WFIFOL(fd,8) = count;
  13744. for( i = 0; i < count; i++ ) {
  13745. int k = 12 + (i * 83);
  13746. memcpy(&auction, RBUFP(buf,i * len), len);
  13747. WFIFOL(fd,k) = auction.auction_id;
  13748. safestrncpy(WFIFOCP(fd,4+k), auction.seller_name, NAME_LENGTH);
  13749. WFIFOW(fd,28+k) = client_nameid( auction.item.nameid );
  13750. WFIFOL(fd,30+k) = auction.type;
  13751. WFIFOW(fd,34+k) = auction.item.amount; // Always 1
  13752. WFIFOB(fd,36+k) = auction.item.identify;
  13753. WFIFOB(fd,37+k) = auction.item.attribute;
  13754. WFIFOB(fd,38+k) = auction.item.refine;
  13755. WFIFOW(fd,39+k) = auction.item.card[0];
  13756. WFIFOW(fd,41+k) = auction.item.card[1];
  13757. WFIFOW(fd,43+k) = auction.item.card[2];
  13758. WFIFOW(fd,45+k) = auction.item.card[3];
  13759. WFIFOL(fd,47+k) = auction.price;
  13760. WFIFOL(fd,51+k) = auction.buynow;
  13761. safestrncpy(WFIFOCP(fd,55+k), auction.buyer_name, NAME_LENGTH);
  13762. WFIFOL(fd,79+k) = (uint32)auction.timestamp;
  13763. }
  13764. WFIFOSET(fd,WFIFOW(fd,2));
  13765. }
  13766. /// Result from request to add an item (ZC_ACK_AUCTION_ADD_ITEM).
  13767. /// 0256 <index>.W <result>.B
  13768. /// result:
  13769. /// 0 = success
  13770. /// 1 = failure
  13771. static void clif_Auction_setitem(int fd, int index, bool fail)
  13772. {
  13773. WFIFOHEAD(fd,packet_len(0x256));
  13774. WFIFOW(fd,0) = 0x256;
  13775. WFIFOW(fd,2) = index;
  13776. WFIFOB(fd,4) = fail;
  13777. WFIFOSET(fd,packet_len(0x256));
  13778. }
  13779. /// Request to initialize 'new auction' data (CZ_AUCTION_CREATE).
  13780. /// 024b <type>.W
  13781. /// type:
  13782. /// 0 = create (any other action in auction window)
  13783. /// 1 = cancel (cancel pressed on register tab)
  13784. /// ? = junk, uninitialized value (ex. when switching between list filters)
  13785. void clif_parse_Auction_cancelreg(int fd, map_session_data *sd){
  13786. //int type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13787. if( sd->auction.amount > 0 )
  13788. clif_additem(sd, sd->auction.index, sd->auction.amount, 0);
  13789. sd->auction.amount = 0;
  13790. }
  13791. /// Request to add an item to the action (CZ_AUCTION_ADD_ITEM).
  13792. /// 024c <index>.W <count>.L
  13793. void clif_parse_Auction_setitem(int fd, map_session_data *sd){
  13794. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13795. int idx = RFIFOW(fd,info->pos[0]) - 2;
  13796. int amount = RFIFOL(fd,info->pos[1]); // Always 1
  13797. if( !battle_config.feature_auction )
  13798. return;
  13799. if( sd->auction.amount > 0 )
  13800. sd->auction.amount = 0;
  13801. if( idx < 0 || idx >= MAX_INVENTORY ) {
  13802. ShowWarning("Character %s trying to set invalid item index in auctions.\n", sd->status.name);
  13803. return;
  13804. }
  13805. 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.
  13806. ShowWarning("Character %s trying to set invalid amount in auctions.\n", sd->status.name);
  13807. return;
  13808. }
  13809. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[idx].nameid);
  13810. 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) )
  13811. { // Consumable or pets are not allowed
  13812. clif_Auction_setitem(sd->fd, idx, true);
  13813. return;
  13814. }
  13815. if( !pc_can_give_items(sd) || sd->inventory.u.items_inventory[idx].expire_time ||
  13816. !sd->inventory.u.items_inventory[idx].identify ||
  13817. (sd->inventory.u.items_inventory[idx].bound && !pc_can_give_bounded_items(sd)) ||
  13818. !itemdb_available(sd->inventory.u.items_inventory[idx].nameid) ||
  13819. !itemdb_canauction(&sd->inventory.u.items_inventory[idx],pc_get_group_level(sd)) ) { // Quest Item or something else
  13820. clif_Auction_setitem(sd->fd, idx, true);
  13821. return;
  13822. }
  13823. sd->auction.index = idx;
  13824. sd->auction.amount = amount;
  13825. clif_Auction_setitem(fd, idx + 2, false);
  13826. }
  13827. /// Result from an auction action (ZC_AUCTION_RESULT).
  13828. /// 0250 <result>.B
  13829. /// result:
  13830. /// 0 = You have failed to bid into the auction
  13831. /// 1 = You have successfully bid in the auction
  13832. /// 2 = The auction has been canceled
  13833. /// 3 = An auction with at least one bidder cannot be canceled
  13834. /// 4 = You cannot register more than 5 items in an auction at a time
  13835. /// 5 = You do not have enough Zeny to pay the Auction Fee
  13836. /// 6 = You have won the auction
  13837. /// 7 = You have failed to win the auction
  13838. /// 8 = You do not have enough Zeny
  13839. /// 9 = You cannot place more than 5 bids at a time
  13840. void clif_Auction_message(int fd, unsigned char flag)
  13841. {
  13842. WFIFOHEAD(fd,packet_len(0x250));
  13843. WFIFOW(fd,0) = 0x250;
  13844. WFIFOB(fd,2) = flag;
  13845. WFIFOSET(fd,packet_len(0x250));
  13846. }
  13847. /// Result of the auction close request (ZC_AUCTION_ACK_MY_SELL_STOP).
  13848. /// 025e <result>.W
  13849. /// result:
  13850. /// 0 = You have ended the auction
  13851. /// 1 = You cannot end the auction
  13852. /// 2 = Auction ID is incorrect
  13853. void clif_Auction_close(int fd, unsigned char flag)
  13854. {
  13855. WFIFOHEAD(fd,packet_len(0x25e));
  13856. WFIFOW(fd,0) = 0x25d; // BUG: The client identifies this packet as 0x25d (CZ_AUCTION_REQ_MY_SELL_STOP)
  13857. WFIFOW(fd,2) = flag;
  13858. WFIFOSET(fd,packet_len(0x25e));
  13859. }
  13860. /// Request to add an auction (CZ_AUCTION_ADD).
  13861. /// 024d <now money>.L <max money>.L <delete hour>.W
  13862. void clif_parse_Auction_register(int fd, map_session_data *sd)
  13863. {
  13864. struct auction_data auction;
  13865. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13866. if( !battle_config.feature_auction )
  13867. return;
  13868. auction.price = RFIFOL(fd,info->pos[0]);
  13869. auction.buynow = RFIFOL(fd,info->pos[1]);
  13870. auction.hours = RFIFOW(fd,info->pos[2]);
  13871. // Invalid Situations...
  13872. if( sd->auction.amount < 1 ) {
  13873. ShowWarning("Character %s trying to register auction without item.\n", sd->status.name);
  13874. return;
  13875. }
  13876. if( auction.price >= auction.buynow ) {
  13877. ShowWarning("Character %s trying to alter auction prices.\n", sd->status.name);
  13878. return;
  13879. }
  13880. if( auction.hours < 1 || auction.hours > 48 ) {
  13881. ShowWarning("Character %s trying to enter an invalid time for auction.\n", sd->status.name);
  13882. return;
  13883. }
  13884. // Auction checks...
  13885. if( sd->inventory.u.items_inventory[sd->auction.index].bound && !pc_can_give_bounded_items(sd) ) {
  13886. clif_displaymessage(sd->fd, msg_txt(sd,293));
  13887. clif_Auction_message(fd, 2); // The auction has been canceled
  13888. return;
  13889. }
  13890. if( sd->status.zeny < (auction.hours * battle_config.auction_feeperhour) ) {
  13891. clif_Auction_message(fd, 5); // You do not have enough zeny to pay the Auction Fee.
  13892. return;
  13893. }
  13894. if( auction.buynow > battle_config.auction_maximumprice ) { // Zeny Limits
  13895. auction.buynow = battle_config.auction_maximumprice;
  13896. if( auction.price >= auction.buynow )
  13897. auction.price = auction.buynow - 1;
  13898. }
  13899. auction.auction_id = 0;
  13900. auction.seller_id = sd->status.char_id;
  13901. safestrncpy(auction.seller_name, sd->status.name, sizeof(auction.seller_name));
  13902. auction.buyer_id = 0;
  13903. memset(auction.buyer_name, '\0', sizeof(auction.buyer_name));
  13904. if( sd->inventory.u.items_inventory[sd->auction.index].nameid == 0 || sd->inventory.u.items_inventory[sd->auction.index].amount < sd->auction.amount )
  13905. {
  13906. clif_Auction_message(fd, 2); // The auction has been canceled
  13907. return;
  13908. }
  13909. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[sd->auction.index].nameid);
  13910. if( id == nullptr )
  13911. { // Just in case
  13912. clif_Auction_message(fd, 2); // The auction has been canceled
  13913. return;
  13914. }
  13915. safestrncpy(auction.item_name, id->ename.c_str(), sizeof(auction.item_name));
  13916. auction.type = id->type;
  13917. memcpy(&auction.item, &sd->inventory.u.items_inventory[sd->auction.index], sizeof(struct item));
  13918. auction.item.amount = 1;
  13919. auction.timestamp = 0;
  13920. if( !intif_Auction_register(&auction) )
  13921. clif_Auction_message(fd, 4); // No Char Server? lets say something to the client
  13922. else
  13923. {
  13924. int zeny = auction.hours*battle_config.auction_feeperhour;
  13925. pc_delitem(sd, sd->auction.index, sd->auction.amount, 1, 6, LOG_TYPE_AUCTION);
  13926. sd->auction.amount = 0;
  13927. pc_payzeny(sd, zeny, LOG_TYPE_AUCTION);
  13928. }
  13929. }
  13930. /// Cancels an auction (CZ_AUCTION_ADD_CANCEL).
  13931. /// 024e <auction id>.L
  13932. void clif_parse_Auction_cancel(int fd, map_session_data *sd){
  13933. unsigned int auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13934. intif_Auction_cancel(sd->status.char_id, auction_id);
  13935. }
  13936. /// Closes an auction (CZ_AUCTION_REQ_MY_SELL_STOP).
  13937. /// 025d <auction id>.L
  13938. void clif_parse_Auction_close(int fd, map_session_data *sd){
  13939. unsigned int auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13940. intif_Auction_close(sd->status.char_id, auction_id);
  13941. }
  13942. /// Places a bid on an auction (CZ_AUCTION_BUY).
  13943. /// 024f <auction id>.L <money>.L
  13944. void clif_parse_Auction_bid(int fd, map_session_data *sd){
  13945. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13946. unsigned int auction_id = RFIFOL(fd,info->pos[0]);
  13947. int bid = RFIFOL(fd,info->pos[1]);
  13948. if( !pc_can_give_items(sd) ) { //They aren't supposed to give zeny [Inkfish]
  13949. clif_displaymessage(sd->fd, msg_txt(sd,246));
  13950. return;
  13951. }
  13952. if( bid <= 0 )
  13953. clif_Auction_message(fd, 0); // You have failed to bid into the auction
  13954. else if( bid > sd->status.zeny )
  13955. clif_Auction_message(fd, 8); // You do not have enough zeny
  13956. else if ( CheckForCharServer() ) // char server is down (bugreport:1138)
  13957. clif_Auction_message(fd, 0); // You have failed to bid into the auction
  13958. else {
  13959. pc_payzeny(sd, bid, LOG_TYPE_AUCTION);
  13960. intif_Auction_bid(sd->status.char_id, sd->status.name, auction_id, bid);
  13961. }
  13962. }
  13963. /// Auction Search (CZ_AUCTION_ITEM_SEARCH).
  13964. /// 0251 <search type>.W <auction id>.L <search text>.24B <page number>.W
  13965. /// search type:
  13966. /// 0 = armor
  13967. /// 1 = weapon
  13968. /// 2 = card
  13969. /// 3 = misc
  13970. /// 4 = name search
  13971. /// 5 = auction id search
  13972. void clif_parse_Auction_search(int fd, map_session_data* sd){
  13973. char search_text[NAME_LENGTH];
  13974. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13975. short type = RFIFOW(fd,info->pos[0]);
  13976. int price = RFIFOL(fd,info->pos[1]); // FIXME: bug #5071
  13977. int page = RFIFOW(fd,info->pos[3]);
  13978. if( !battle_config.feature_auction )
  13979. return;
  13980. clif_parse_Auction_cancelreg(fd, sd);
  13981. safestrncpy(search_text, RFIFOCP(fd,info->pos[2]), sizeof(search_text));
  13982. intif_Auction_requestlist(sd->status.char_id, type, price, search_text, page);
  13983. }
  13984. /// Requests list of own currently active bids or auctions (CZ_AUCTION_REQ_MY_INFO).
  13985. /// 025c <type>.W
  13986. /// type:
  13987. /// 0 = sell (own auctions)
  13988. /// 1 = buy (own bids)
  13989. void clif_parse_Auction_buysell(int fd, map_session_data* sd)
  13990. {
  13991. short type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) + 6;
  13992. if( !battle_config.feature_auction )
  13993. return;
  13994. clif_parse_Auction_cancelreg(fd, sd);
  13995. intif_Auction_requestlist(sd->status.char_id, type, 0, "", 1);
  13996. }
  13997. /// CASH/POINT SHOP
  13998. ///
  13999. void clif_cashshop_open( map_session_data* sd, int tab ){
  14000. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14001. nullpo_retv( sd );
  14002. PACKET_ZC_SE_CASHSHOP_OPEN p = {};
  14003. p.packetType = HEADER_ZC_SE_CASHSHOP_OPEN;
  14004. p.cashPoints = sd->cashPoints;
  14005. p.kafraPoints = sd->kafraPoints;
  14006. #if PACKETVER_MAIN_NUM >= 20200129 || PACKETVER_RE_NUM >= 20200205 || PACKETVER_ZERO_NUM >= 20191224
  14007. p.tab = tab;
  14008. #endif
  14009. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14010. #endif
  14011. }
  14012. void clif_parse_cashshop_open_request( int fd, map_session_data* sd ){
  14013. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14014. nullpo_retv( sd );
  14015. int tab = 0;
  14016. #if PACKETVER >= 20191224
  14017. const PACKET_CZ_SE_CASHSHOP_OPEN2* p = reinterpret_cast<PACKET_CZ_SE_CASHSHOP_OPEN2*>( RFIFOP( fd, 0 ) );
  14018. tab = p->tab;
  14019. #endif
  14020. if (map_getmapflag(sd->bl.m, MF_NOCASHSHOP)) {
  14021. clif_displaymessage(fd, msg_txt(sd, 451)); // Cash Shop is disabled on this map.
  14022. return;
  14023. }
  14024. sd->state.cashshop_open = true;
  14025. sd->npc_shopid = -1; // Set npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14026. clif_cashshop_open( sd, tab );
  14027. #endif
  14028. }
  14029. void clif_parse_cashshop_close( int fd, map_session_data* sd ){
  14030. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14031. sd->state.cashshop_open = false;
  14032. sd->npc_shopid = 0; // Reset npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14033. // No need to do anything here
  14034. #endif
  14035. }
  14036. //0846 <tabid>.W (CZ_REQ_SE_CASH_TAB_CODE))
  14037. //08c0 <len>.W <openIdentity>.L <itemcount>.W (ZC_ACK_SE_CASH_ITEM_LIST2)
  14038. void clif_parse_CashShopReqTab(int fd, map_session_data *sd) {
  14039. #if PACKETVER_MAIN_NUM >= 20100824 || PACKETVER_RE_NUM >= 20100824 || defined(PACKETVER_ZERO)
  14040. const PACKET_CZ_REQ_SE_CASH_TAB_CODE* p_in = reinterpret_cast<PACKET_CZ_REQ_SE_CASH_TAB_CODE*>( RFIFOP( fd, 0 ) );
  14041. // TODO: most likely wrong answer packet. Most likely HEADER_ZC_ACK_SE_CASH_ITEM_LIST (0x847) would be correct [Lemongrass]
  14042. // [4144] packet exists only in 2011 and was dropped after
  14043. #if PACKETVER >= 20110222 && PACKETVER < 20120000
  14044. std::shared_ptr<s_cash_item_tab> tab = cash_shop_db.find( static_cast<e_cash_shop_tab>( p_in->tab ) );
  14045. if( tab == nullptr ){
  14046. return;
  14047. }
  14048. // Skip empty tabs, the client only expects filled ones
  14049. if( tab->items.empty() ){
  14050. return;
  14051. }
  14052. #if !(PACKETVER_SUPPORTS_SALES)
  14053. if( tab->tab == CASHSHOP_TAB_SALE ){
  14054. return;
  14055. }
  14056. #endif
  14057. PACKET_ZC_ACK_SE_CASH_ITEM_LIST2* p = reinterpret_cast<PACKET_ZC_ACK_SE_CASH_ITEM_LIST2*>( packet_buffer );
  14058. p->packetType = HEADER_ZC_ACK_SE_CASH_ITEM_LIST2;
  14059. p->packetLength = sizeof( *p );
  14060. p->tab = tab->tab;
  14061. p->count = 0;
  14062. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14063. p->items[p->count].itemId = client_nameid( item->nameid );
  14064. p->items[p->count].price = item->price;
  14065. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14066. p->count++;
  14067. }
  14068. clif_send( p, p->packetLength, &sd->bl, SELF );
  14069. #endif
  14070. #endif
  14071. }
  14072. //08ca <len>.W <itemcount> W <tabcode>.W (ZC_ACK_SCHEDULER_CASHITEM)
  14073. void clif_cashshop_list( map_session_data& sd ){
  14074. for( const auto& pair : cash_shop_db ){
  14075. std::shared_ptr<s_cash_item_tab> tab = pair.second;
  14076. // Skip empty tabs, the client only expects filled ones
  14077. if( tab->items.empty() ){
  14078. continue;
  14079. }
  14080. #if !(PACKETVER_SUPPORTS_SALES)
  14081. if( tab->tab == CASHSHOP_TAB_SALE ){
  14082. continue;
  14083. }
  14084. #endif
  14085. PACKET_ZC_ACK_SCHEDULER_CASHITEM* p = reinterpret_cast<PACKET_ZC_ACK_SCHEDULER_CASHITEM*>( packet_buffer );
  14086. p->packetType = HEADER_ZC_ACK_SCHEDULER_CASHITEM;
  14087. p->packetLength = sizeof( *p );
  14088. p->count = 0;
  14089. p->tabNum = tab->tab;
  14090. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14091. p->items[p->count].itemId = client_nameid( item->nameid );
  14092. p->items[p->count].price = item->price;
  14093. #ifdef ENABLE_CASHSHOP_PREVIEW_PATCH
  14094. struct item_data* id = itemdb_search( item->nameid );
  14095. if( id == nullptr ){
  14096. p->items[p->count].location = 0;
  14097. p->items[p->count].viewSprite = 0;
  14098. }else{
  14099. p->items[p->count].location = pc_equippoint_sub( &sd, id );
  14100. p->items[p->count].viewSprite = id->look;
  14101. }
  14102. #endif
  14103. p->count++;
  14104. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14105. if( ( static_cast<size_t>( p->packetLength ) + sizeof( p->items[0] ) ) >= INT16_MAX ){
  14106. // Send current data
  14107. clif_send( p, p->packetLength, &sd.bl, SELF );
  14108. // Start a new packet
  14109. p->count = 0;
  14110. p->packetLength = sizeof( *p );
  14111. }
  14112. }
  14113. clif_send( p, p->packetLength, &sd.bl, SELF );
  14114. }
  14115. }
  14116. void clif_parse_cashshop_list_request( int fd, map_session_data* sd ){
  14117. if( !sd->status.cashshop_sent ) {
  14118. clif_cashshop_list( *sd );
  14119. #if PACKETVER_SUPPORTS_SALES
  14120. sale_notify_login(sd);
  14121. #endif
  14122. sd->status.cashshop_sent = true;
  14123. }
  14124. }
  14125. /// List of items offered in a cash shop (ZC_PC_CASH_POINT_ITEMLIST).
  14126. /// 0287 <packet len>.W <cash point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }*
  14127. /// 0287 <packet len>.W <cash point>.L <kafra point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }* (PACKETVER >= 20070711)
  14128. void clif_cashshop_show( map_session_data& sd, npc_data& nd ){
  14129. sd.npc_shopid = nd.bl.id;
  14130. int cost[2] = { 0, 0 };
  14131. npc_shop_currency_type( &sd, &nd, cost, true );
  14132. PACKET_ZC_PC_CASH_POINT_ITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_CASH_POINT_ITEMLIST*>( packet_buffer );
  14133. p->packetType = HEADER_ZC_PC_CASH_POINT_ITEMLIST;
  14134. p->packetLength = sizeof( *p );
  14135. p->cashPoints = cost[0];
  14136. #if PACKETVER >= 20070711
  14137. p->kafraPoints = cost[1];
  14138. #endif
  14139. for( int i = 0; i < nd.u.shop.count; i++ ) {
  14140. struct item_data* id = itemdb_search( nd.u.shop.shop_item[i].nameid );
  14141. p->items[i].price = nd.u.shop.shop_item[i].value;
  14142. p->items[i].discountPrice = nd.u.shop.shop_item[i].value; // Discount Price
  14143. p->items[i].itemType = itemtype( id->nameid );
  14144. p->items[i].itemId = client_nameid( id->nameid );
  14145. #ifdef ENABLE_OLD_CASHSHOP_PREVIEW_PATCH
  14146. p->items[i].location = pc_equippoint_sub( &sd, id );
  14147. p->items[i].viewSprite = id->look;
  14148. memset( p->items[i].unused, 0, sizeof( p->items[i].unused ) );
  14149. #endif
  14150. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14151. }
  14152. clif_send( p, p->packetLength, &sd.bl, SELF );
  14153. }
  14154. /// Cashshop Buy Ack (ZC_PC_CASH_POINT_UPDATE).
  14155. /// 0289 <cash point>.L <error>.W
  14156. /// 0289 <cash point>.L <kafra point>.L <error>.W (PACKETVER >= 20070711)
  14157. /// error:
  14158. /// 0 = The deal has successfully completed. (ERROR_TYPE_NONE)
  14159. /// 1 = The Purchase has failed because the NPC does not exist. (ERROR_TYPE_NPC)
  14160. /// 2 = The Purchase has failed because the Kafra Shop System is not working correctly. (ERROR_TYPE_SYSTEM)
  14161. /// 3 = You are over your Weight Limit. (ERROR_TYPE_INVENTORY_WEIGHT)
  14162. /// 4 = You cannot purchase items while you are in a trade. (ERROR_TYPE_EXCHANGE)
  14163. /// 5 = The Purchase has failed because the Item Information was incorrect. (ERROR_TYPE_ITEM_ID)
  14164. /// 6 = You do not have enough Kafra Credit Points. (ERROR_TYPE_MONEY)
  14165. /// 7 = You can purchase up to 10 items. (ERROR_TYPE_AMOUNT)
  14166. /// 8 = Some items could not be purchased. (ERROR_TYPE_PURCHASE_FAIL)
  14167. void clif_cashshop_ack(map_session_data* sd, int error)
  14168. {
  14169. int fd, cost[2] = { 0, 0 };
  14170. struct npc_data *nd;
  14171. nullpo_retv(sd);
  14172. fd = sd->fd;
  14173. nd = map_id2nd(sd->npc_shopid);
  14174. npc_shop_currency_type(sd, nd, cost, false);
  14175. WFIFOHEAD(fd, packet_len(0x289));
  14176. WFIFOW(fd,0) = 0x289;
  14177. WFIFOL(fd,2) = cost[0];
  14178. #if PACKETVER < 20070711
  14179. WFIFOW(fd,6) = TOW(error);
  14180. #else
  14181. WFIFOL(fd,6) = cost[1];
  14182. WFIFOW(fd,10) = TOW(error);
  14183. #endif
  14184. WFIFOSET(fd, packet_len(0x289));
  14185. }
  14186. void clif_cashshop_result( map_session_data *sd, t_itemid item_id, uint16 result ){
  14187. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined( PACKETVER_ZERO )
  14188. nullpo_retv( sd );
  14189. PACKET_ZC_SE_PC_BUY_CASHITEM_RESULT packet = {};
  14190. packet.packetType = HEADER_ZC_SE_PC_BUY_CASHITEM_RESULT;
  14191. if( item_id != 0 ){
  14192. packet.itemId = client_nameid( item_id );
  14193. }else{
  14194. packet.itemId = 0;
  14195. }
  14196. packet.result = result;
  14197. packet.cashPoints = sd->cashPoints;
  14198. packet.kafraPoints = sd->kafraPoints;
  14199. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  14200. #endif
  14201. }
  14202. /// Request to buy item(s) from cash shop.
  14203. /// 0288 <name id>.W <amount>.W (CZ_PC_BUY_CASH_POINT_ITEM)
  14204. /// 0288 <name id>.W <amount>.W <kafra points>.L (PACKETVER >= 20070711)
  14205. /// 0288 <packet len>.W <kafra points>.L <count>.W { <amount>.W <name id>.W }.4B*count (PACKETVER >= 20100803)
  14206. void clif_parse_npccashshop_buy( int fd, map_session_data *sd ){
  14207. nullpo_retv( sd );
  14208. if( sd->state.trading || !sd->npc_shopid ){
  14209. clif_cashshop_ack( sd, 1 );
  14210. return;
  14211. }
  14212. const PACKET_CZ_PC_BUY_CASH_POINT_ITEM* p = reinterpret_cast<PACKET_CZ_PC_BUY_CASH_POINT_ITEM*>( RFIFOP( fd, 0 ) );
  14213. #if PACKETVER < 20070711
  14214. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, 0 ) );
  14215. #elif PACKETVER < 20101116
  14216. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, p->kafraPoints ) );
  14217. #else
  14218. int s_itl = sizeof( p->items[0] );
  14219. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14220. 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 );
  14221. return;
  14222. }
  14223. std::vector<s_npc_buy_list> item_list = {};
  14224. item_list.reserve( p->count );
  14225. for( int i = 0; i < p->count; i++ ){
  14226. s_npc_buy_list item = {};
  14227. item.nameid = p->items[i].itemId;
  14228. item.qty = p->items[i].amount;
  14229. item_list.push_back( item );
  14230. }
  14231. clif_cashshop_ack( sd, npc_cashshop_buylist( sd, p->kafraPoints, item_list ) );
  14232. #endif
  14233. }
  14234. /// Request to buy item(s) from cash shop.
  14235. /// 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)
  14236. void clif_parse_cashshop_buy( int fd, map_session_data *sd ){
  14237. const PACKET_CZ_SE_PC_BUY_CASHITEM_LIST* p = reinterpret_cast<PACKET_CZ_SE_PC_BUY_CASHITEM_LIST*>( RFIFOP( fd, 0 ) );
  14238. int s_itl = sizeof( p->items[0] );
  14239. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14240. 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 );
  14241. return;
  14242. }
  14243. cashshop_buylist( sd, p->kafraPoints, p->count, p->items );
  14244. }
  14245. /// Adoption System
  14246. ///
  14247. /// Adoption message (ZC_BABYMSG).
  14248. /// 0216 <msg>.L
  14249. /// msg:
  14250. /// ADOPT_REPLY_MORE_CHILDREN = "You cannot adopt more than 1 child."
  14251. /// ADOPT_REPLY_LEVEL_70 = "You must be at least character level 70 in order to adopt someone."
  14252. /// ADOPT_REPLY_MARRIED = "You cannot adopt a married person."
  14253. void clif_Adopt_reply(map_session_data *sd, int type)
  14254. {
  14255. int fd = sd->fd;
  14256. WFIFOHEAD(fd,6);
  14257. WFIFOW(fd,0) = 0x216;
  14258. WFIFOL(fd,2) = type;
  14259. WFIFOSET(fd,6);
  14260. }
  14261. /// Adoption confirmation (ZC_REQ_BABY).
  14262. /// 01f6 <account id>.L <char id>.L <name>.B
  14263. void clif_Adopt_request(map_session_data *sd, map_session_data *src, int p_id)
  14264. {
  14265. int fd = sd->fd;
  14266. WFIFOHEAD(fd,34);
  14267. WFIFOW(fd,0) = 0x1f6;
  14268. WFIFOL(fd,2) = src->status.account_id;
  14269. WFIFOL(fd,6) = p_id;
  14270. safestrncpy(WFIFOCP(fd,10), src->status.name, NAME_LENGTH);
  14271. WFIFOSET(fd,34);
  14272. }
  14273. /// Request to adopt a player (CZ_REQ_JOIN_BABY).
  14274. /// 01f9 <account id>.L
  14275. void clif_parse_Adopt_request(int fd, map_session_data *sd)
  14276. {
  14277. TBL_PC *tsd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  14278. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  14279. if( pc_try_adopt(sd, p_sd, tsd) == ADOPT_ALLOWED )
  14280. {
  14281. tsd->adopt_invite = sd->status.account_id;
  14282. clif_Adopt_request(tsd, sd, p_sd->status.account_id);
  14283. }
  14284. }
  14285. /// Answer to adopt confirmation (CZ_JOIN_BABY).
  14286. /// 01f7 <account id>.L <char id>.L <answer>.L
  14287. /// answer:
  14288. /// 0 = rejected
  14289. /// 1 = accepted
  14290. void clif_parse_Adopt_reply(int fd, map_session_data *sd){
  14291. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  14292. int p1_id = RFIFOL(fd,info->pos[0]);
  14293. int p2_id = RFIFOL(fd,info->pos[1]);
  14294. int result = RFIFOL(fd,info->pos[2]);
  14295. map_session_data* p1_sd = map_id2sd(p1_id);
  14296. map_session_data* p2_sd = map_id2sd(p2_id);
  14297. int pid = sd->adopt_invite;
  14298. sd->adopt_invite = 0;
  14299. if( p1_sd == nullptr || p2_sd == nullptr )
  14300. return; // Both players need to be online
  14301. if( pid != p1_sd->status.account_id )
  14302. return; // Incorrect values
  14303. if( result == 0 )
  14304. return; // Rejected
  14305. pc_adoption(p1_sd, p2_sd, sd);
  14306. }
  14307. /// Convex Mirror
  14308. /// 0293 <infoType>.B <x>.L <y>.L <minHours>.W <minMinutes>.W <maxHours>.W <maxMinutes>.W <monster name>.51B (ZC_BOSS_INFO)
  14309. /// infoType:
  14310. /// BOSS_INFO_NOT = No boss on this map.
  14311. /// BOSS_INFO_ALIVE = Boss is alive (position update).
  14312. /// BOSS_INFO_ALIVE_WITHMSG = Boss is alive (initial announce).
  14313. /// BOSS_INFO_DEAD = Boss is dead.
  14314. void clif_bossmapinfo( map_session_data& sd, mob_data* md, e_bossmap_info flag ){
  14315. PACKET_ZC_BOSS_INFO p = {};
  14316. p.packetType = HEADER_ZC_BOSS_INFO;
  14317. p.type = flag;
  14318. switch (flag) {
  14319. case BOSS_INFO_NOT:
  14320. // No data required
  14321. break;
  14322. case BOSS_INFO_ALIVE_WITHMSG:
  14323. [[fallthrough]];
  14324. case BOSS_INFO_ALIVE:
  14325. p.x = md->bl.x;
  14326. p.y = md->bl.y;
  14327. break;
  14328. case BOSS_INFO_DEAD: {
  14329. const struct TimerData * timer_data = get_timer(md->spawn_timer);
  14330. unsigned int seconds;
  14331. int hours, minutes;
  14332. seconds = (unsigned int)(DIFF_TICK(timer_data->tick, gettick()) / 1000 + 60);
  14333. hours = seconds / (60 * 60);
  14334. seconds = seconds - (60 * 60 * hours);
  14335. minutes = seconds / 60;
  14336. p.minHours = hours;
  14337. p.minMinutes = minutes;
  14338. break;
  14339. }
  14340. }
  14341. if( md != nullptr ){
  14342. safestrncpy( p.name, md->db->jname.c_str(), sizeof( p.name ) );
  14343. }
  14344. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  14345. }
  14346. /// Requesting equip of a player (CZ_EQUIPWIN_MICROSCOPE).
  14347. /// 02d6 <account id>.L
  14348. void clif_parse_ViewPlayerEquip(int fd, map_session_data* sd)
  14349. {
  14350. int aid = RFIFOL(fd, packet_db[RFIFOW(fd,0)].pos[0]);
  14351. map_session_data* tsd = map_id2sd(aid);
  14352. if (!tsd)
  14353. return;
  14354. if (sd->bl.m != tsd->bl.m)
  14355. return;
  14356. else if( tsd->status.show_equip || pc_has_permission(sd, PC_PERM_VIEW_EQUIPMENT) )
  14357. clif_viewequip_ack( *sd, *tsd );
  14358. else
  14359. clif_msg(sd, MSI_OPEN_EQUIPEDITEM_REFUSED);
  14360. }
  14361. /// Request to a configuration.
  14362. /// 02d8 <type>.L <flag>.L (CZ_CONFIG)
  14363. /// type: see enum e_config_type
  14364. /// flag:
  14365. /// false = disabled
  14366. /// true = enabled
  14367. void clif_parse_configuration( int fd, map_session_data* sd ){
  14368. int cmd = RFIFOW(fd,0);
  14369. int type = RFIFOL(fd,packet_db[cmd].pos[0]);
  14370. bool flag = RFIFOL(fd,packet_db[cmd].pos[1]) != 0;
  14371. switch( type ){
  14372. case CONFIG_OPEN_EQUIPMENT_WINDOW:
  14373. sd->status.show_equip = flag;
  14374. break;
  14375. case CONFIG_CALL:
  14376. sd->status.disable_call = flag;
  14377. break;
  14378. case CONFIG_PET_AUTOFEED:
  14379. // Player can not click this if he does not have a pet
  14380. if( sd->pd == nullptr || !battle_config.feature_pet_autofeed || !sd->pd->get_pet_db()->allow_autofeed ){
  14381. return;
  14382. }
  14383. sd->pd->pet.autofeed = flag;
  14384. break;
  14385. case CONFIG_HOMUNCULUS_AUTOFEED:
  14386. // Player can not click this if he does not have a homunculus or it is vaporized
  14387. if( sd->hd == nullptr || sd->hd->homunculus.vaporize || !battle_config.feature_homunculus_autofeed ){
  14388. return;
  14389. }
  14390. sd->hd->homunculus.autofeed = flag;
  14391. break;
  14392. default:
  14393. ShowWarning( "clif_parse_configuration: received unknown configuration type '%d'...\n", type );
  14394. return;
  14395. }
  14396. clif_configuration( sd, static_cast<e_config_type>(type), flag );
  14397. }
  14398. /// Request to change party invitation tick.
  14399. /// 02C8 <enabled>.B (CZ_PARTY_CONFIG)
  14400. /// value:
  14401. /// 0 = disabled (triggered by /accept)
  14402. /// 1 = enabled (triggered by /refuse)
  14403. void clif_parse_PartyTick( int fd, map_session_data* sd ){
  14404. const PACKET_CZ_PARTY_CONFIG* p = reinterpret_cast<PACKET_CZ_PARTY_CONFIG*>( RFIFOP( fd, 0 ) );
  14405. sd->status.disable_partyinvite = p->refuseInvite;
  14406. clif_partyinvitationstate( *sd );
  14407. }
  14408. /// Questlog System [Kevin] [Inkfish]
  14409. ///
  14410. /**
  14411. * Safe check to init packet len & quest num for player.
  14412. * @param def_len
  14413. * @param info_len Length of each quest info.
  14414. * @param avail_quests Avail quests that player has.
  14415. * @param limit_out Limit for quest num, to make sure the quest num is still in allowed value.
  14416. * @param len_out Packet length as initial set => def_len + limit_out * info_len.
  14417. **/
  14418. static void clif_quest_len(int def_len, int info_len, int avail_quests, int *limit_out, int *len_out) {
  14419. (*limit_out) = (0xFFFF - def_len) / info_len;
  14420. (*limit_out) = (avail_quests > (*limit_out)) ? (*limit_out) : avail_quests;
  14421. (*len_out) = ((*limit_out) * info_len) + def_len;
  14422. }
  14423. std::string clif_quest_string( std::shared_ptr<s_quest_objective> objective ){
  14424. std::string race_name;
  14425. switch( objective->race ){
  14426. case RC_FORMLESS: race_name = "Formless"; break;
  14427. case RC_UNDEAD: race_name = "Undead"; break;
  14428. case RC_BRUTE: race_name = "Brute"; break;
  14429. case RC_PLANT: race_name = "Plant"; break;
  14430. case RC_INSECT: race_name = "Insect"; break;
  14431. case RC_FISH: race_name = "Fish"; break;
  14432. case RC_DEMON: race_name = "Demon"; break;
  14433. case RC_DEMIHUMAN: race_name = "Demihuman"; break;
  14434. case RC_ANGEL: race_name = "Angel"; break;
  14435. case RC_DRAGON: race_name = "Dragon"; break;
  14436. default:
  14437. ShowWarning( "clif_quest_string: Unsupported race %d - using empty string...\n", objective->race );
  14438. [[fallthrough]];
  14439. case RC_ALL: race_name = ""; break;
  14440. }
  14441. std::string size_name;
  14442. switch( objective->size ){
  14443. case SZ_SMALL: size_name = "Small"; break;
  14444. case SZ_MEDIUM: size_name = "Medium"; break;
  14445. case SZ_BIG: size_name = "Large"; break;
  14446. default:
  14447. ShowWarning( "clif_quest_string: Unsupported size %d - using empty string...\n", objective->size );
  14448. [[fallthrough]];
  14449. case SZ_ALL: size_name = ""; break;
  14450. }
  14451. std::string ele_name;
  14452. switch( objective->element ){
  14453. case ELE_NEUTRAL: ele_name = "Neutral Element"; break;
  14454. case ELE_WATER: ele_name = "Water Element"; break;
  14455. case ELE_EARTH: ele_name = "Earth Element"; break;
  14456. case ELE_FIRE: ele_name = "Fire Element"; break;
  14457. case ELE_WIND: ele_name = "Wind Element"; break;
  14458. case ELE_POISON: ele_name = "Poison Element"; break;
  14459. case ELE_HOLY: ele_name = "Holy Element"; break;
  14460. case ELE_DARK: ele_name = "Shadow Element"; break;
  14461. case ELE_GHOST: ele_name = "Ghost Element"; break;
  14462. case ELE_UNDEAD: ele_name = "Undead Element"; break;
  14463. default:
  14464. ShowWarning( "clif_quest_string: Unsupported element %d - using empty string...\n", objective->element );
  14465. [[fallthrough]];
  14466. case ELE_ALL: ele_name = ""; break;
  14467. }
  14468. std::string str;
  14469. if( !objective->map_name.empty() ){
  14470. str += objective->map_name;
  14471. }
  14472. if( !race_name.empty() ){
  14473. if( !str.empty() ){
  14474. str += ", ";
  14475. }
  14476. str += race_name;
  14477. }
  14478. if( !size_name.empty() ){
  14479. if( !str.empty() ){
  14480. str += ", ";
  14481. }
  14482. str += size_name;
  14483. }
  14484. if( !ele_name.empty() ){
  14485. if( !str.empty() ){
  14486. str += ", ";
  14487. }
  14488. str += ele_name;
  14489. }
  14490. return str;
  14491. }
  14492. /// Sends list of all quest states
  14493. /// 02b1 <packet len>.W <num>.L { <quest id>.L <active>.B }*num (ZC_ALL_QUEST_LIST)
  14494. /// 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)
  14495. /// 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)
  14496. void clif_quest_send_list(map_session_data *sd)
  14497. {
  14498. nullpo_retv(sd);
  14499. int fd = sd->fd;
  14500. int i;
  14501. int offset = 8;
  14502. int limit = 0;
  14503. #if PACKETVER >= 20141022
  14504. #if PACKETVER >= 20150513
  14505. int size = 22 + NAME_LENGTH;
  14506. #else
  14507. int size = 10 + NAME_LENGTH;
  14508. #endif
  14509. clif_quest_len(offset, 15 + ( size * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &i);
  14510. WFIFOHEAD(fd,i);
  14511. #if PACKETVER >= 20150513
  14512. WFIFOW(fd, 0) = 0x9f8;
  14513. #else
  14514. WFIFOW(fd, 0) = 0x97a;
  14515. #endif
  14516. WFIFOL(fd, 4) = limit;
  14517. for (i = 0; i < limit; i++) {
  14518. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14519. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14520. offset += 4;
  14521. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14522. offset++;
  14523. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14524. offset += 4;
  14525. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time);
  14526. offset += 4;
  14527. WFIFOW(fd, offset) = static_cast<uint16>(qi->objectives.size());
  14528. offset += 2;
  14529. if (!qi->objectives.empty()) {
  14530. std::shared_ptr<s_mob_db> mob;
  14531. for (int j = 0; j < qi->objectives.size(); j++) {
  14532. mob = mob_db.find(qi->objectives[j]->mob_id);
  14533. e_race race = qi->objectives[j]->race;
  14534. e_size size = qi->objectives[j]->size;
  14535. e_element element = qi->objectives[j]->element;
  14536. #if PACKETVER >= 20150513
  14537. WFIFOL(fd, offset) = sd->quest_log[i].quest_id * 1000 + j;
  14538. offset += 4;
  14539. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0)));
  14540. offset += 4;
  14541. #endif
  14542. WFIFOL(fd, offset) = ((mob && qi->objectives[j]->mob_id > 0) ? qi->objectives[j]->mob_id : MOBID_PORING);
  14543. offset += 4;
  14544. #if PACKETVER >= 20150513
  14545. WFIFOW(fd, offset) = qi->objectives[j]->min_level;
  14546. offset += 2;
  14547. WFIFOW(fd, offset) = qi->objectives[j]->max_level;
  14548. offset += 2;
  14549. #endif
  14550. WFIFOW(fd, offset) = sd->quest_log[i].count[j];
  14551. offset += 2;
  14552. WFIFOW(fd, offset) = qi->objectives[j]->count;
  14553. offset += 2;
  14554. if (mob && qi->objectives[j]->mob_id > 0)
  14555. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14556. else
  14557. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14558. offset += NAME_LENGTH;
  14559. }
  14560. }
  14561. }
  14562. WFIFOW(fd, 2) = offset;
  14563. WFIFOSET(fd, offset);
  14564. #else
  14565. clif_quest_len(offset, 5, sd->avail_quests, &limit, &i);
  14566. WFIFOHEAD(fd,i);
  14567. WFIFOW(fd, 0) = 0x2b1;
  14568. WFIFOL(fd, 4) = limit;
  14569. for (i = 0; i < limit; i++) {
  14570. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14571. offset += 4;
  14572. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14573. offset += 1;
  14574. }
  14575. WFIFOW(fd, 2) = offset;
  14576. WFIFOSET(fd, offset);
  14577. #endif
  14578. }
  14579. /// Sends list of all quest missions (ZC_ALL_QUEST_MISSION).
  14580. /// 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
  14581. void clif_quest_send_mission(map_session_data *sd)
  14582. {
  14583. nullpo_retv(sd);
  14584. int fd = sd->fd;
  14585. int limit = 0;
  14586. int len = sd->avail_quests*104+8;
  14587. clif_quest_len(8, 14 + ((6 + NAME_LENGTH) * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &len);
  14588. WFIFOHEAD(fd, len);
  14589. WFIFOW(fd, 0) = 0x2b2;
  14590. WFIFOW(fd, 2) = len;
  14591. WFIFOL(fd, 4) = limit;
  14592. for (int i = 0; i < limit; i++) {
  14593. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14594. WFIFOL(fd, i*104+8) = sd->quest_log[i].quest_id;
  14595. WFIFOL(fd, i*104+12) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14596. WFIFOL(fd, i*104+16) = static_cast<uint32>(sd->quest_log[i].time);
  14597. WFIFOW(fd, i*104+20) = static_cast<uint16>(qi->objectives.size());
  14598. for (int j = 0 ; j < qi->objectives.size(); j++) {
  14599. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[j]->mob_id);
  14600. WFIFOL(fd, i*104+22+j*30) = (mob ? qi->objectives[j]->mob_id : MOBID_PORING);
  14601. WFIFOW(fd, i*104+26+j*30) = sd->quest_log[i].count[j];
  14602. if (mob && qi->objectives[j]->mob_id > 0)
  14603. safestrncpy(WFIFOCP(fd, i*104+28+j*30), mob->jname.c_str(), NAME_LENGTH);
  14604. else
  14605. safestrncpy(WFIFOCP(fd, i*104+28+j*30), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14606. }
  14607. }
  14608. WFIFOSET(fd, len);
  14609. }
  14610. /// Notification about a new quest
  14611. /// 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)
  14612. /// 08fe <packet len>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_HUNTING_QUEST_INFO)
  14613. /// 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)
  14614. void clif_quest_add(map_session_data *sd, struct quest *qd)
  14615. {
  14616. nullpo_retv(sd);
  14617. nullpo_retv(qd);
  14618. int fd = sd->fd;
  14619. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14620. if (!qi)
  14621. return;
  14622. #if PACKETVER >= 20150513
  14623. int cmd = 0x9f9;
  14624. #else
  14625. int cmd = 0x2b3;
  14626. #endif
  14627. WFIFOHEAD(fd, packet_len(cmd));
  14628. WFIFOW(fd, 0) = cmd;
  14629. WFIFOL(fd, 2) = qd->quest_id;
  14630. WFIFOB(fd, 6) = qd->state;
  14631. WFIFOB(fd, 7) = static_cast<uint8>(qd->time - qi->time);
  14632. WFIFOL(fd, 11) = static_cast<uint32>(qd->time);
  14633. WFIFOW(fd, 15) = static_cast<uint16>(qi->objectives.size());
  14634. for (int i = 0, offset = 17; i < qi->objectives.size(); i++) {
  14635. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[i]->mob_id);
  14636. e_race race = qi->objectives[i]->race;
  14637. e_size size = qi->objectives[i]->size;
  14638. e_element element = qi->objectives[i]->element;
  14639. #if PACKETVER >= 20150513
  14640. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14641. offset += 4;
  14642. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0))); // effect ?
  14643. offset += 4;
  14644. #endif
  14645. 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
  14646. offset += 4;
  14647. #if PACKETVER >= 20150513
  14648. WFIFOW(fd, offset) = qi->objectives[i]->min_level;
  14649. offset += 2;
  14650. WFIFOW(fd, offset) = qi->objectives[i]->max_level;
  14651. offset += 2;
  14652. #endif
  14653. WFIFOW(fd, offset) = qd->count[i];
  14654. offset += 2;
  14655. if (mob && qi->objectives[i]->mob_id > 0)
  14656. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14657. else
  14658. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[i]).c_str(), NAME_LENGTH);
  14659. offset += NAME_LENGTH;
  14660. }
  14661. WFIFOSET(fd, packet_len(cmd));
  14662. #if PACKETVER >= 20150513
  14663. int16 len;
  14664. len = static_cast<decltype(len)>( 4 + qi->objectives.size() * 12 );
  14665. WFIFOHEAD(fd, len);
  14666. WFIFOW(fd, 0) = 0x8fe;
  14667. WFIFOW(fd, 2) = len;
  14668. for (int i = 0, offset = 4; i < qi->objectives.size(); i++, offset += 12) {
  14669. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14670. WFIFOL(fd, offset + 4) = qi->objectives[i]->mob_id;
  14671. WFIFOW(fd, offset + 8) = qi->objectives[i]->count;
  14672. WFIFOW(fd, offset + 10) = qd->count[i];
  14673. }
  14674. WFIFOSET(fd, len);
  14675. #endif
  14676. }
  14677. /// Notification about a quest being removed (ZC_DEL_QUEST).
  14678. /// 02b4 <quest id>.L
  14679. void clif_quest_delete(map_session_data *sd, int quest_id)
  14680. {
  14681. int fd = sd->fd;
  14682. WFIFOHEAD(fd, packet_len(0x2b4));
  14683. WFIFOW(fd, 0) = 0x2b4;
  14684. WFIFOL(fd, 2) = quest_id;
  14685. WFIFOSET(fd, packet_len(0x2b4));
  14686. }
  14687. /// Notification of an update to the hunting mission counter
  14688. /// 02b5 <packet len>.W <mobs>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT)
  14689. /// 09fa <packet len>.W <mobs>.W { <quest id>.L <hunt identification>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT_EX)
  14690. void clif_quest_update_objective(map_session_data *sd, struct quest *qd)
  14691. {
  14692. int fd = sd->fd;
  14693. int offset = 6;
  14694. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14695. int16 len;
  14696. len = static_cast<decltype(len)>( qi->objectives.size() * 12 + 6 );
  14697. #if PACKETVER >= 20150513
  14698. int cmd = 0x9fa;
  14699. #else
  14700. int cmd = 0x2b5;
  14701. #endif
  14702. WFIFOHEAD(fd, len);
  14703. WFIFOW(fd, 0) = cmd;
  14704. WFIFOW(fd, 4) = static_cast<uint16>(qi->objectives.size());
  14705. for (int i = 0; i < qi->objectives.size(); i++) {
  14706. WFIFOL(fd, offset) = qd->quest_id;
  14707. offset += 4;
  14708. #if PACKETVER >= 20150513
  14709. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14710. offset += 4;
  14711. #else
  14712. WFIFOL(fd, offset) = qi->objectives[i]->mob_id;
  14713. offset += 4;
  14714. #endif
  14715. WFIFOW(fd, offset) = qi->objectives[i]->count;
  14716. offset += 2;
  14717. WFIFOW(fd, offset) = qd->count[i];
  14718. offset += 2;
  14719. }
  14720. WFIFOW(fd, 2) = offset;
  14721. WFIFOSET(fd, offset);
  14722. }
  14723. /// Request to change the state of a quest (CZ_ACTIVE_QUEST).
  14724. /// 02b6 <quest id>.L <active>.B
  14725. void clif_parse_questStateAck(int fd, map_session_data *sd)
  14726. {
  14727. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  14728. quest_update_status(sd, RFIFOL(fd,info->pos[0]),
  14729. RFIFOB(fd,info->pos[1])?Q_ACTIVE:Q_INACTIVE);
  14730. }
  14731. /// Notification about the change of a quest state (ZC_ACTIVE_QUEST).
  14732. /// 02b7 <quest id>.L <active>.B
  14733. void clif_quest_update_status(map_session_data *sd, int quest_id, bool active)
  14734. {
  14735. int fd = sd->fd;
  14736. WFIFOHEAD(fd, packet_len(0x2b7));
  14737. WFIFOW(fd, 0) = 0x2b7;
  14738. WFIFOL(fd, 2) = quest_id;
  14739. WFIFOB(fd, 6) = active;
  14740. WFIFOSET(fd, packet_len(0x2b7));
  14741. }
  14742. /// Notification about an NPC's quest state (ZC_QUEST_NOTIFY_EFFECT).
  14743. /// 0446 <npc id>.L <x>.W <y>.W <effect>.W <color>.W
  14744. /// effect:
  14745. /// 0 = none
  14746. /// 1 = exclamation mark icon
  14747. /// 2 = question mark icon
  14748. /// color:
  14749. /// 0 = yellow
  14750. /// 1 = orange
  14751. /// 2 = green
  14752. /// 3 = purple
  14753. void clif_quest_show_event(map_session_data *sd, struct block_list *bl, e_questinfo_types effect, e_questinfo_markcolor color)
  14754. {
  14755. #if PACKETVER >= 20090218
  14756. nullpo_retv(sd);
  14757. nullpo_retv(bl);
  14758. int fd = sd->fd;
  14759. WFIFOHEAD(fd, packet_len(0x446));
  14760. WFIFOW(fd, 0) = 0x446;
  14761. WFIFOL(fd, 2) = bl->id;
  14762. WFIFOW(fd, 6) = bl->x;
  14763. WFIFOW(fd, 8) = bl->y;
  14764. WFIFOW(fd, 10) = effect;
  14765. WFIFOW(fd, 12) = color;
  14766. WFIFOSET(fd, packet_len(0x446));
  14767. #endif
  14768. }
  14769. /// Mercenary System
  14770. ///
  14771. /// Notification about a mercenary status parameter change (ZC_MER_PAR_CHANGE).
  14772. /// 02a2 <var id>.W <value>.L
  14773. void clif_mercenary_updatestatus(map_session_data *sd, int type)
  14774. {
  14775. s_mercenary_data *md;
  14776. struct status_data *status;
  14777. int fd;
  14778. if( !clif_session_isValid(sd) || (md = sd->md) == nullptr )
  14779. return;
  14780. fd = sd->fd;
  14781. status = &md->battle_status;
  14782. WFIFOHEAD(fd,packet_len(0x2a2));
  14783. WFIFOW(fd,0) = 0x2a2;
  14784. WFIFOW(fd,2) = type;
  14785. switch( type ) {
  14786. case SP_ATK1:
  14787. {
  14788. int atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14789. WFIFOL(fd,4) = cap_value(atk, 0, INT16_MAX);
  14790. }
  14791. break;
  14792. case SP_MATK1:
  14793. WFIFOL(fd,4) = min(status->matk_max, UINT16_MAX);
  14794. break;
  14795. case SP_HIT:
  14796. WFIFOL(fd,4) = status->hit;
  14797. break;
  14798. case SP_CRITICAL:
  14799. WFIFOL(fd,4) = status->cri/10;
  14800. break;
  14801. case SP_DEF1:
  14802. WFIFOL(fd,4) = status->def;
  14803. break;
  14804. case SP_MDEF1:
  14805. WFIFOL(fd,4) = status->mdef;
  14806. break;
  14807. case SP_MERCFLEE:
  14808. WFIFOL(fd,4) = status->flee;
  14809. break;
  14810. case SP_ASPD:
  14811. WFIFOL(fd,4) = status->amotion;
  14812. break;
  14813. case SP_HP:
  14814. WFIFOL(fd,4) = status->hp;
  14815. break;
  14816. case SP_MAXHP:
  14817. WFIFOL(fd,4) = status->max_hp;
  14818. break;
  14819. case SP_SP:
  14820. WFIFOL(fd,4) = status->sp;
  14821. break;
  14822. case SP_MAXSP:
  14823. WFIFOL(fd,4) = status->max_sp;
  14824. break;
  14825. case SP_MERCKILLS:
  14826. WFIFOL(fd,4) = md->mercenary.kill_count;
  14827. break;
  14828. case SP_MERCFAITH:
  14829. WFIFOL(fd,4) = mercenary_get_faith(md);
  14830. break;
  14831. }
  14832. WFIFOSET(fd,packet_len(0x2a2));
  14833. }
  14834. /// Mercenary base status data (ZC_MER_INIT).
  14835. /// 029b <id>.L <atk>.W <matk>.W <hit>.W <crit>.W <def>.W <mdef>.W <flee>.W <aspd>.W
  14836. /// <name>.24B <level>.W <hp>.L <maxhp>.L <sp>.L <maxsp>.L <expire time>.L <faith>.W
  14837. /// <calls>.L <kills>.L <atk range>.W
  14838. void clif_mercenary_info(map_session_data *sd)
  14839. {
  14840. int fd;
  14841. s_mercenary_data *md;
  14842. struct status_data *status;
  14843. int atk;
  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(0x29b));
  14849. WFIFOW(fd,0) = 0x29b;
  14850. WFIFOL(fd,2) = md->bl.id;
  14851. // Mercenary shows ATK as a random value between ATK ~ ATK2
  14852. atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14853. WFIFOW(fd,6) = cap_value(atk, 0, INT16_MAX);
  14854. WFIFOW(fd,8) = min(status->matk_max, UINT16_MAX);
  14855. WFIFOW(fd,10) = status->hit;
  14856. WFIFOW(fd,12) = status->cri/10;
  14857. WFIFOW(fd,14) = status->def;
  14858. WFIFOW(fd,16) = status->mdef;
  14859. WFIFOW(fd,18) = status->flee;
  14860. WFIFOW(fd,20) = status->amotion;
  14861. safestrncpy(WFIFOCP(fd,22), md->db->name.c_str(), NAME_LENGTH);
  14862. WFIFOW(fd,46) = md->db->lv;
  14863. WFIFOL(fd,48) = status->hp;
  14864. WFIFOL(fd,52) = status->max_hp;
  14865. WFIFOL(fd,56) = status->sp;
  14866. WFIFOL(fd,60) = status->max_sp;
  14867. WFIFOL(fd,64) = client_tick(time(nullptr) + (mercenary_get_lifetime(md) / 1000));
  14868. WFIFOW(fd,68) = mercenary_get_faith(md);
  14869. WFIFOL(fd,70) = mercenary_get_calls(md);
  14870. WFIFOL(fd,74) = md->mercenary.kill_count;
  14871. WFIFOW(fd,78) = md->battle_status.rhw.range;
  14872. WFIFOSET(fd,packet_len(0x29b));
  14873. }
  14874. /// Mercenary skill tree (ZC_MER_SKILLINFO_LIST).
  14875. /// 029d <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  14876. void clif_mercenary_skillblock(map_session_data *sd)
  14877. {
  14878. s_mercenary_data *md;
  14879. int fd, len = 4;
  14880. if( sd == nullptr || (md = sd->md) == nullptr )
  14881. return;
  14882. fd = sd->fd;
  14883. WFIFOHEAD(fd,4+37*MAX_MERCSKILL);
  14884. WFIFOW(fd,0) = 0x29d;
  14885. for (const auto &it : md->db->skill) {
  14886. uint16 id = it.first;
  14887. if (!SKILL_CHK_MERC(id))
  14888. continue;
  14889. uint16 lv = it.second;
  14890. WFIFOW(fd,len) = id;
  14891. WFIFOL(fd,len+2) = skill_get_inf(id);
  14892. WFIFOW(fd,len+6) = lv;
  14893. WFIFOW(fd,len+8) = skill_get_sp(id, lv);
  14894. WFIFOW(fd,len+10) = skill_get_range2(&md->bl, id, lv, false);
  14895. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  14896. WFIFOB(fd,len+36) = 0; // Skillable for Mercenary?
  14897. len += 37;
  14898. }
  14899. WFIFOW(fd,2) = len;
  14900. WFIFOSET(fd,len);
  14901. }
  14902. /// Request to invoke a mercenary menu action (CZ_MER_COMMAND).
  14903. /// 029f <command>.B
  14904. /// 1 = mercenary information
  14905. /// 2 = delete
  14906. void clif_parse_mercenary_action(int fd, map_session_data* sd)
  14907. {
  14908. int option = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  14909. if( sd->md == nullptr )
  14910. return;
  14911. if( option == 2 ) mercenary_delete(sd->md, 2);
  14912. }
  14913. /// Notification about the remaining time of a rental item.
  14914. /// 0298 <name id>.W <seconds>.L (ZC_CASH_TIME_COUNTER)
  14915. void clif_rental_time( map_session_data* sd, t_itemid nameid, int seconds ){
  14916. // '<ItemName>' item will disappear in <seconds/60> minutes.
  14917. PACKET_ZC_CASH_TIME_COUNTER p = {};
  14918. p.packetType = HEADER_ZC_CASH_TIME_COUNTER;
  14919. p.itemId = client_nameid( nameid );
  14920. p.seconds = seconds;
  14921. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14922. }
  14923. /// Deletes a rental item from client's inventory.
  14924. /// 0299 <index>.W <name id>.W (ZC_CASH_ITEM_DELETE)
  14925. void clif_rental_expired( map_session_data* sd, int index, t_itemid nameid ){
  14926. // '<ItemName>' item has been deleted from the Inventory
  14927. PACKET_ZC_CASH_ITEM_DELETE p = {};
  14928. p.packetType = HEADER_ZC_CASH_ITEM_DELETE;
  14929. p.index = client_index( index );
  14930. p.itemId = client_nameid( nameid );
  14931. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14932. }
  14933. /// Book Reading (ZC_READ_BOOK).
  14934. /// 0294 <book id>.L <page>.L
  14935. void clif_readbook(int fd, int book_id, int page)
  14936. {
  14937. WFIFOHEAD(fd,packet_len(0x294));
  14938. WFIFOW(fd,0) = 0x294;
  14939. WFIFOL(fd,2) = book_id;
  14940. WFIFOL(fd,6) = page;
  14941. WFIFOSET(fd,packet_len(0x294));
  14942. }
  14943. /// Battlegrounds
  14944. ///
  14945. /// Updates HP bar of a camp member.
  14946. /// 02e0 <account id>.L <name>.24B <hp>.W <max hp>.W (ZC_BATTLEFIELD_NOTIFY_HP)
  14947. /// 0a0e <account id>.L <hp>.L <max hp>.L (ZC_BATTLEFIELD_NOTIFY_HP2)
  14948. void clif_bg_hp(map_session_data *sd) {
  14949. #if PACKETVER < 20140613
  14950. unsigned char buf[34];
  14951. const int cmd = 0x2e0;
  14952. #else
  14953. unsigned char buf[14];
  14954. const int cmd = 0xa0e;
  14955. #endif
  14956. nullpo_retv(sd);
  14957. WBUFW(buf,0) = cmd;
  14958. WBUFL(buf,2) = sd->status.account_id;
  14959. #if PACKETVER < 20140613
  14960. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  14961. if( sd->battle_status.max_hp > INT16_MAX ) { // To correctly display the %hp bar. [Skotlex]
  14962. WBUFW(buf,30) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  14963. WBUFW(buf,32) = 100;
  14964. } else {
  14965. WBUFW(buf,30) = sd->battle_status.hp;
  14966. WBUFW(buf,32) = sd->battle_status.max_hp;
  14967. }
  14968. #else
  14969. if( sd->battle_status.max_hp > INT32_MAX ) {
  14970. WBUFL(buf,6) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  14971. WBUFL(buf,10) = 100;
  14972. } else {
  14973. WBUFL(buf,6) = sd->battle_status.hp;
  14974. WBUFL(buf,10) = sd->battle_status.max_hp;
  14975. }
  14976. #endif
  14977. clif_send(buf, packet_len(cmd), &sd->bl, BG_AREA_WOS);
  14978. }
  14979. /// Updates the position of a camp member on the minimap (ZC_BATTLEFIELD_NOTIFY_POSITION).
  14980. /// 02df <account id>.L <name>.24B <class>.W <x>.W <y>.W
  14981. void clif_bg_xy(map_session_data *sd)
  14982. {
  14983. unsigned char buf[36];
  14984. nullpo_retv(sd);
  14985. WBUFW(buf,0)=0x2df;
  14986. WBUFL(buf,2)=sd->status.account_id;
  14987. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  14988. WBUFW(buf,30)=sd->status.class_;
  14989. WBUFW(buf,32)=sd->bl.x;
  14990. WBUFW(buf,34)=sd->bl.y;
  14991. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  14992. }
  14993. void clif_bg_xy_remove(map_session_data *sd)
  14994. {
  14995. unsigned char buf[36];
  14996. nullpo_retv(sd);
  14997. WBUFW(buf,0)=0x2df;
  14998. WBUFL(buf,2)=sd->status.account_id;
  14999. memset(WBUFP(buf,6), 0, NAME_LENGTH);
  15000. WBUFW(buf,30)=0;
  15001. WBUFW(buf,32)=-1;
  15002. WBUFW(buf,34)=-1;
  15003. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  15004. }
  15005. /// Notifies clients of a battleground message.
  15006. /// 02DC <packet len>.W <account id>.L <name>.24B <message>.?B (ZC_BATTLEFIELD_CHAT)
  15007. void clif_bg_message( struct s_battleground_data *bg, int src_id, const char *name, const char *mes, size_t len ){
  15008. map_session_data *sd = bg_getavailablesd( bg );
  15009. if( sd == nullptr ){
  15010. return;
  15011. }
  15012. // limit length
  15013. len = zmin( len + 1, CHAT_SIZE_MAX );
  15014. unsigned char buf[8 + NAME_LENGTH + CHAT_SIZE_MAX];
  15015. WBUFW(buf,0) = 0x2dc;
  15016. WBUFW(buf,2) = static_cast<int16>( len + NAME_LENGTH + 8 );
  15017. WBUFL(buf,4) = src_id;
  15018. safestrncpy(WBUFCP(buf,8), name, NAME_LENGTH);
  15019. safestrncpy(WBUFCP(buf,8+NAME_LENGTH), mes, len );
  15020. clif_send(buf,WBUFW(buf,2), &sd->bl, BG);
  15021. }
  15022. /// Validates and processes battlechat messages.
  15023. /// All messages that are sent after enabling battleground chat with /battlechat.
  15024. /// 02DB <packet len>.W <text>.?B (CZ_BATTLEFIELD_CHAT)
  15025. void clif_parse_BattleChat(int fd, map_session_data* sd){
  15026. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  15027. if( !clif_process_message( sd, false, name, message, output ) )
  15028. return;
  15029. bg_send_message(sd, output, strlen(output) );
  15030. }
  15031. /// Notifies client of a battleground score change (ZC_BATTLEFIELD_NOTIFY_POINT).
  15032. /// 02de <camp A points>.W <camp B points>.W
  15033. void clif_bg_updatescore(int16 m)
  15034. {
  15035. struct block_list bl;
  15036. unsigned char buf[6];
  15037. struct map_data *mapdata = map_getmapdata(m);
  15038. bl.id = 0;
  15039. bl.type = BL_NUL;
  15040. bl.m = m;
  15041. WBUFW(buf,0) = 0x2de;
  15042. WBUFW(buf,2) = mapdata->bgscore_lion;
  15043. WBUFW(buf,4) = mapdata->bgscore_eagle;
  15044. clif_send(buf,packet_len(0x2de),&bl,ALL_SAMEMAP);
  15045. }
  15046. void clif_bg_updatescore_single(map_session_data *sd)
  15047. {
  15048. int fd;
  15049. nullpo_retv(sd);
  15050. fd = sd->fd;
  15051. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  15052. WFIFOHEAD(fd,packet_len(0x2de));
  15053. WFIFOW(fd,0) = 0x2de;
  15054. WFIFOW(fd,2) = mapdata->bgscore_lion;
  15055. WFIFOW(fd,4) = mapdata->bgscore_eagle;
  15056. WFIFOSET(fd,packet_len(0x2de));
  15057. }
  15058. /// Battleground camp belong-information (ZC_BATTLEFIELD_NOTIFY_CAMPINFO).
  15059. /// 02dd <account id>.L <name>.24B <camp>.W
  15060. void clif_sendbgemblem_area(map_session_data *sd)
  15061. {
  15062. unsigned char buf[33];
  15063. nullpo_retv(sd);
  15064. WBUFW(buf, 0) = 0x2dd;
  15065. WBUFL(buf,2) = sd->bl.id;
  15066. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH); // name don't show in screen.
  15067. WBUFW(buf,30) = sd->bg_id;
  15068. clif_send(buf,packet_len(0x2dd), &sd->bl, AREA);
  15069. }
  15070. void clif_sendbgemblem_single(int fd, map_session_data *sd)
  15071. {
  15072. nullpo_retv(sd);
  15073. WFIFOHEAD(fd,32);
  15074. WFIFOW(fd,0) = 0x2dd;
  15075. WFIFOL(fd,2) = sd->bl.id;
  15076. safestrncpy(WFIFOCP(fd,6), sd->status.name, NAME_LENGTH);
  15077. WFIFOW(fd,30) = sd->bg_id;
  15078. WFIFOSET(fd,packet_len(0x2dd));
  15079. }
  15080. /// Battlegrounds queue incoming apply request from client.
  15081. /// Queue types: 1 solo queue, 2 party queue, 4 guild queue.
  15082. /// 0x8d7 <queue type>.W <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_APPLY)
  15083. void clif_parse_bg_queue_apply_request(int fd, map_session_data *sd)
  15084. {
  15085. if (!battle_config.feature_bgqueue)
  15086. return;
  15087. nullpo_retv(sd);
  15088. short type = RFIFOW(fd,2);
  15089. char name[NAME_LENGTH];
  15090. safestrncpy(name, RFIFOCP(fd, 4), NAME_LENGTH);
  15091. if (sd->bg_queue_id > 0) {
  15092. //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);
  15093. clif_bg_queue_apply_result(BG_APPLY_DUPLICATE, name, sd); // Duplicate application warning
  15094. return;
  15095. } else if (type == 1) // Solo
  15096. bg_queue_join_solo(name, sd);
  15097. else if (type == 2) // Party
  15098. bg_queue_join_party(name, sd);
  15099. else if (type == 4) // Guild
  15100. bg_queue_join_guild(name, sd);
  15101. else {
  15102. 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);
  15103. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd); // Someone sent an invalid queue type packet
  15104. return;
  15105. }
  15106. }
  15107. /// Outgoing battlegrounds queue apply result.
  15108. /// Result types: @see e_bg_queue_apply_ack
  15109. /// 0x8d8 <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_APPLY)
  15110. void clif_bg_queue_apply_result(e_bg_queue_apply_ack result, const char *name, map_session_data *sd)
  15111. {
  15112. nullpo_retv(sd);
  15113. int fd = sd->fd;
  15114. WFIFOHEAD(fd, packet_len(0x8d8));
  15115. WFIFOW(fd,0) = 0x8d8;
  15116. WFIFOB(fd,2) = result;
  15117. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15118. WFIFOSET(fd, packet_len(0x8d8));
  15119. }
  15120. /// Outgoing battlegrounds queue apply notification.
  15121. /// Sends a notification at the start of the battlegrounds queue and is also used to update the queue number.
  15122. /// 0x8d9 <battleground name>.24B <queue number>.L (ZC_NOTIFY_ENTRY_QUEUE_APPLY)
  15123. void clif_bg_queue_apply_notify(const char *name, map_session_data *sd)
  15124. {
  15125. nullpo_retv(sd);
  15126. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15127. if (queue == nullptr) {
  15128. ShowError("clif_bg_queue_apply_notify: Player is not in a battleground queue.\n");
  15129. return;
  15130. }
  15131. int fd = sd->fd;
  15132. WFIFOHEAD(fd, packet_len(0x8d9));
  15133. WFIFOW(fd,0) = 0x8d9;
  15134. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15135. WFIFOL(fd,2+NAME_LENGTH) = static_cast<int32>( queue->teama_members.size() + queue->teamb_members.size() );
  15136. WFIFOSET(fd, packet_len(0x8d9));
  15137. }
  15138. /// Battlegrounds queue outgoing cancel result.
  15139. /// 0x8db <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_CANCEL)
  15140. void clif_bg_queue_cancel_result(bool success, const char *name, map_session_data *sd)
  15141. {
  15142. nullpo_retv(sd);
  15143. int fd = sd->fd;
  15144. WFIFOHEAD(fd, packet_len(0x8d8));
  15145. WFIFOW(fd,0) = 0x8db;
  15146. WFIFOB(fd,2) = success;
  15147. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15148. WFIFOSET(fd, packet_len(0x8d8));
  15149. }
  15150. /// Battlegrounds queue incoming cancel request from client.
  15151. /// 0x8da <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_CANCEL)
  15152. void clif_parse_bg_queue_cancel_request(int fd, map_session_data *sd)
  15153. {
  15154. if (!battle_config.feature_bgqueue)
  15155. return;
  15156. nullpo_retv(sd);
  15157. bool success;
  15158. if (sd->bg_queue_id > 0) {
  15159. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15160. if (queue && queue->state == QUEUE_STATE_SETUP_DELAY)
  15161. 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.
  15162. else
  15163. success = bg_queue_leave(sd);
  15164. } else {
  15165. 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);
  15166. success = false;
  15167. }
  15168. char name[NAME_LENGTH];
  15169. safestrncpy( name, RFIFOCP( fd, 2 ), NAME_LENGTH );
  15170. clif_bg_queue_cancel_result(success, name, sd);
  15171. }
  15172. /// Battleground is ready to be joined, send a window asking for players to accept or decline.
  15173. /// 0x8df <battleground name>.24B <lobby name>.24B (ZC_NOTIFY_LOBBY_ADMISSION)
  15174. void clif_bg_queue_lobby_notify(const char *name, map_session_data *sd)
  15175. {
  15176. nullpo_retv(sd);
  15177. int fd = sd->fd;
  15178. WFIFOHEAD(fd, packet_len(0x8df));
  15179. WFIFOW(fd,0) = 0x8df;
  15180. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15181. safestrncpy(WFIFOCP(fd,2+NAME_LENGTH), name, NAME_LENGTH);
  15182. WFIFOSET(fd, packet_len(0x8df));
  15183. }
  15184. /// Incoming packet from client telling server whether player wants to enter battleground or cancel.
  15185. /// Result types: 1(Accept), 2(Decline).
  15186. /// 0x8e0 <result>.B <battleground name>.24B <lobby name>.24B (CZ_REPLY_LOBBY_ADMISSION)
  15187. void clif_parse_bg_queue_lobby_reply(int fd, map_session_data *sd)
  15188. {
  15189. nullpo_retv(sd);
  15190. if(sd->bg_queue_id > 0) {
  15191. uint8 result = RFIFOB(fd, 2);
  15192. if(result == 1) { // Accept
  15193. bg_queue_on_accept_invite(sd);
  15194. } else if(result == 2) { // Decline
  15195. bg_queue_leave(sd);
  15196. clif_bg_queue_entry_init(sd);
  15197. }
  15198. }
  15199. }
  15200. /// Plays a gong sound, signaling that someone has accepted the invite to enter a battleground.
  15201. /// 0x8e1 <result>.B <battleground name>.24B <lobby name>.24B (ZC_REPLY_ACK_LOBBY_ADMISSION)
  15202. void clif_bg_queue_ack_lobby(bool result, const char *name, const char *lobbyname, map_session_data *sd)
  15203. {
  15204. nullpo_retv(sd);
  15205. int fd = sd->fd;
  15206. WFIFOHEAD(fd, packet_len(0x8e1));
  15207. WFIFOW(fd,0) = 0x8e1;
  15208. WFIFOB(fd,2) = result;
  15209. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15210. safestrncpy(WFIFOCP(fd,3+NAME_LENGTH), lobbyname, NAME_LENGTH);
  15211. WFIFOSET(fd, packet_len(0x8e1));
  15212. }
  15213. /// Battlegrounds queue incoming queue number request from client.
  15214. /// 0x90a <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_RANKING)
  15215. void clif_parse_bg_queue_request_queue_number(int fd, map_session_data *sd)
  15216. {
  15217. nullpo_retv(sd);
  15218. char name[NAME_LENGTH];
  15219. safestrncpy( name, RFIFOCP(fd, 2), NAME_LENGTH );
  15220. clif_bg_queue_apply_notify(name, sd);
  15221. }
  15222. /// Silently removes all the battlegrounds stuff client side so that you will open the first BG window when you press battle on the interface.
  15223. /// Send this when a player joins a battleground so that it will remove all the queue stuff upon warping in.
  15224. /// 0x90e (ZC_ENTRY_QUEUE_INIT)
  15225. void clif_bg_queue_entry_init(map_session_data *sd)
  15226. {
  15227. nullpo_retv(sd);
  15228. PACKET_ZC_ENTRY_QUEUE_INIT p = {};
  15229. p.packetType = HEADER_ZC_ENTRY_QUEUE_INIT;
  15230. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15231. }
  15232. /// Custom Fonts (ZC_NOTIFY_FONT).
  15233. /// 02ef <account_id>.L <font id>.W
  15234. void clif_font(map_session_data *sd)
  15235. {
  15236. #if PACKETVER >= 20080102
  15237. unsigned char buf[8];
  15238. nullpo_retv(sd);
  15239. WBUFW(buf,0) = 0x2ef;
  15240. WBUFL(buf,2) = sd->bl.id;
  15241. WBUFW(buf,6) = sd->status.font;
  15242. clif_send(buf, packet_len(0x2ef), &sd->bl, AREA);
  15243. #endif
  15244. }
  15245. /// Required to start the instancing information window on Client
  15246. /// This window re-appears each "refresh" of client automatically until the keep_limit reaches 0.
  15247. /// S 0x2cb <Instance name>.61B <Standby Position>.W
  15248. void clif_instance_create( int instance_id, size_t num ){
  15249. #if PACKETVER >= 20071128
  15250. map_session_data *sd = nullptr;
  15251. enum send_target target = PARTY;
  15252. unsigned char buf[65];
  15253. instance_getsd(instance_id, sd, &target);
  15254. if (!sd)
  15255. return;
  15256. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15257. if (!db)
  15258. return;
  15259. WBUFW(buf,0) = 0x2cb;
  15260. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15261. WBUFW( buf, 63 ) = static_cast<int16>( num );
  15262. clif_send(buf,packet_len(0x2cb),&sd->bl,target);
  15263. #endif
  15264. }
  15265. /// To announce Instancing queue creation if no maps available
  15266. /// S 0x2cc <Standby Position>.W
  15267. void clif_instance_changewait(int instance_id, int num)
  15268. {
  15269. #if PACKETVER >= 20071128
  15270. map_session_data *sd = nullptr;
  15271. enum send_target target = PARTY;
  15272. unsigned char buf[4];
  15273. instance_getsd(instance_id, sd, &target);
  15274. if (!sd)
  15275. return;
  15276. WBUFW(buf,0) = 0x2cc;
  15277. WBUFW(buf,2) = num;
  15278. clif_send(buf,packet_len(0x2cc),&sd->bl,target);
  15279. #endif
  15280. return;
  15281. }
  15282. /// Notify the current status to members
  15283. /// S 0x2cd <Instance Name>.61B <Instance Remaining Time>.L <Instance Noplayers close time>.L
  15284. void clif_instance_status(int instance_id, unsigned int limit1, unsigned int limit2)
  15285. {
  15286. #if PACKETVER >= 20071128
  15287. map_session_data *sd = nullptr;
  15288. enum send_target target = PARTY;
  15289. unsigned char buf[71];
  15290. instance_getsd(instance_id, sd, &target);
  15291. if (!sd)
  15292. return;
  15293. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15294. if (!db)
  15295. return;
  15296. WBUFW(buf,0) = 0x2cd;
  15297. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15298. WBUFL(buf,63) = limit1;
  15299. WBUFL(buf,67) = limit2;
  15300. clif_send(buf,packet_len(0x2cd),&sd->bl,target);
  15301. #endif
  15302. return;
  15303. }
  15304. /// Notify a status change to members
  15305. /// S 0x2ce <Message ID>.L
  15306. /// 0 = Notification (EnterLimitDate update?)
  15307. /// 1 = The Memorial Dungeon expired; it has been destroyed
  15308. /// 2 = The Memorial Dungeon's entry time limit expired; it has been destroyed
  15309. /// 3 = The Memorial Dungeon has been removed.
  15310. /// 4 = Create failure (removes the instance window)
  15311. void clif_instance_changestatus(int instance_id, e_instance_notify type, unsigned int limit)
  15312. {
  15313. #if PACKETVER >= 20071128
  15314. map_session_data *sd = nullptr;
  15315. enum send_target target = PARTY;
  15316. unsigned char buf[10];
  15317. instance_getsd(instance_id, sd, &target);
  15318. if (!sd)
  15319. return;
  15320. WBUFW(buf,0) = 0x2ce;
  15321. WBUFL(buf,2) = type;
  15322. WBUFL(buf,6) = limit;
  15323. clif_send(buf,packet_len(0x2ce),&sd->bl,target);
  15324. #endif
  15325. return;
  15326. }
  15327. /// Destroy an instance from the status window
  15328. /// 02cf <command>.L (CZ_MEMORIALDUNGEON_COMMAND)
  15329. void clif_parse_MemorialDungeonCommand(int fd, map_session_data *sd)
  15330. {
  15331. if (pc_istrading(sd) || pc_isdead(sd) || map_getmapdata(sd->bl.m)->instance_id > 0)
  15332. return;
  15333. const PACKET_CZ_MEMORIALDUNGEON_COMMAND* p = reinterpret_cast<PACKET_CZ_MEMORIALDUNGEON_COMMAND*>( RFIFOP( fd, 0 ) );
  15334. switch (p->command) {
  15335. case COMMAND_MEMORIALDUNGEON_DESTROY_FORCE:
  15336. instance_destroy_command(sd);
  15337. break;
  15338. }
  15339. }
  15340. void clif_instance_info( map_session_data& sd ){
  15341. if( sd.instance_id > 0 ){
  15342. instance_reqinfo( &sd, sd.instance_id );
  15343. }
  15344. if( sd.status.party_id > 0 ){
  15345. struct party_data* p = party_search( sd.status.party_id );
  15346. if( p != nullptr && p->instance_id > 0 ){
  15347. instance_reqinfo( &sd, p->instance_id );
  15348. }
  15349. }
  15350. if( sd.guild != nullptr && sd.guild->instance_id > 0 ){
  15351. instance_reqinfo( &sd, sd.guild->instance_id );
  15352. }
  15353. if( sd.clan != nullptr && sd.clan->instance_id > 0 ){
  15354. instance_reqinfo( &sd, sd.clan->instance_id );
  15355. }
  15356. }
  15357. /// Notifies clients about item picked up by a party member.
  15358. /// 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)
  15359. void clif_party_show_picker( map_session_data* sd, struct item* item_data ){
  15360. #if PACKETVER >= 20071002
  15361. nullpo_retv( sd );
  15362. nullpo_retv( item_data );
  15363. struct item_data* id = itemdb_search( item_data->nameid );
  15364. PACKET_ZC_ITEM_PICKUP_PARTY p = {};
  15365. p.packetType = HEADER_ZC_ITEM_PICKUP_PARTY;
  15366. p.AID = sd->status.account_id;
  15367. p.itemId = client_nameid( item_data->nameid );
  15368. p.identified = item_data->identify;
  15369. p.damaged = item_data->attribute;
  15370. p.refine = item_data->refine;
  15371. clif_addcards( &p.slot, item_data );
  15372. p.location = id->equip; // equip location
  15373. p.itemType = itemtype( id->nameid ); // item type
  15374. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15375. p.grade = item_data->enchantgrade;
  15376. #endif
  15377. clif_send( &p, sizeof( p ), &sd->bl, PARTY_SAMEMAP_WOS );
  15378. #endif
  15379. }
  15380. /** Display gained exp.
  15381. * 07f6 <account id>.L <amount>.L <var id>.W <exp type>.W (ZC_NOTIFY_EXP)
  15382. * 0acc <account id>.L <amount>.Q <var id>.W <exp type>.W (ZC_NOTIFY_EXP2)
  15383. * amount: INT32_MIN ~ INT32_MAX
  15384. * var id:
  15385. * SP_BASEEXP, SP_JOBEXP
  15386. * exp type:
  15387. * 0 = normal exp gained/lost
  15388. * 1 = quest exp gained/lost
  15389. * @param sd Player
  15390. * @param exp EXP value gained/loss
  15391. * @param type SP_BASEEXP, SP_JOBEXP
  15392. * @param quest False:Normal EXP; True:Quest EXP (displayed in purple color)
  15393. * @param lost True:if losing EXP
  15394. */
  15395. void clif_displayexp(map_session_data *sd, t_exp exp, char type, bool quest, bool lost)
  15396. {
  15397. int fd;
  15398. int offset;
  15399. #if PACKETVER >= 20170830
  15400. int cmd = 0xacc;
  15401. #else
  15402. int cmd = 0x7f6;
  15403. #endif
  15404. nullpo_retv(sd);
  15405. fd = sd->fd;
  15406. WFIFOHEAD(fd, packet_len(cmd));
  15407. WFIFOW(fd,0) = cmd;
  15408. WFIFOL(fd,2) = sd->bl.id;
  15409. #if PACKETVER >= 20170830
  15410. WFIFOQ(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15411. offset = 4;
  15412. #else
  15413. WFIFOL(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15414. offset = 0;
  15415. #endif
  15416. WFIFOW(fd,10+offset) = type;
  15417. WFIFOW(fd,12+offset) = (quest && type != SP_JOBEXP) ? 1 : 0; // NOTE: Somehow JobEXP always in yellow color
  15418. WFIFOSET(fd,packet_len(cmd));
  15419. }
  15420. /// Displays digital clock digits on top of the screen (ZC_SHOWDIGIT).
  15421. /// type:
  15422. /// 0 = Displays 'value' for 5 seconds.
  15423. /// 1 = Incremental counter (1 tick/second), negated 'value' specifies start value (e.g. using -10 lets the counter start at 10).
  15424. /// 2 = Decremental counter (1 tick/second), negated 'value' specifies start value (does not stop when reaching 0, but overflows).
  15425. /// 3 = Decremental counter (1 tick/second), 'value' specifies start value (stops when reaching 0, displays at most 2 digits).
  15426. /// value:
  15427. /// 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).
  15428. void clif_showdigit(map_session_data* sd, unsigned char type, int value)
  15429. {
  15430. WFIFOHEAD(sd->fd, packet_len(0x1b1));
  15431. WFIFOW(sd->fd,0) = 0x1b1;
  15432. WFIFOB(sd->fd,2) = type;
  15433. WFIFOL(sd->fd,3) = value;
  15434. WFIFOSET(sd->fd, packet_len(0x1b1));
  15435. }
  15436. /// Notification of the state of client command /effect (CZ_LESSEFFECT).
  15437. /// 021d <state>.L
  15438. /// state:
  15439. /// 0 = Full effects
  15440. /// 1 = Reduced effects
  15441. ///
  15442. /// NOTE: The state is used on Aegis for sending skill unit packet
  15443. /// 0x11f (ZC_SKILL_ENTRY) instead of 0x1c9 (ZC_SKILL_ENTRY2)
  15444. /// whenever possible. Due to the way the decision check is
  15445. /// constructed, this state tracking was rendered useless,
  15446. /// as the only skill unit, that is sent with 0x1c9 is
  15447. /// Graffiti.
  15448. void clif_parse_LessEffect(int fd, map_session_data* sd){
  15449. int isLess = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  15450. sd->state.lesseffect = ( isLess != 0 );
  15451. }
  15452. /// 07e4 <length>.w <option>.l <val>.l {<index>.w <amount>.w).4b* (CZ_ITEMLISTWIN_RES)
  15453. void clif_parse_ItemListWindowSelected(int fd, map_session_data* sd) {
  15454. if( sd == nullptr ){
  15455. return;
  15456. }
  15457. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  15458. int n = (RFIFOW(fd,info->pos[0])-12) / 4;
  15459. int type = RFIFOL(fd,info->pos[1]);
  15460. int flag = RFIFOL(fd,info->pos[2]); // Button clicked: 0 = Cancel, 1 = OK
  15461. unsigned short* item_list = (unsigned short*)RFIFOP(fd,info->pos[3]);
  15462. if( sd->state.trading || sd->npc_shopid )
  15463. return;
  15464. if( flag == 0 || n == 0) {
  15465. clif_menuskill_clear(sd);
  15466. return; // Canceled by player.
  15467. }
  15468. if( sd->menuskill_id != SO_EL_ANALYSIS && sd->menuskill_id != GN_CHANGEMATERIAL ) {
  15469. clif_menuskill_clear(sd);
  15470. return; // Prevent hacking.
  15471. }
  15472. switch( type ) {
  15473. case 0: // Change Material
  15474. skill_changematerial(sd,n,item_list);
  15475. break;
  15476. case 1: // Level 1: Pure to Rough
  15477. case 2: // Level 2: Rough to Pure
  15478. skill_elementalanalysis(*sd,n,type,item_list);
  15479. break;
  15480. }
  15481. clif_menuskill_clear(sd);
  15482. return;
  15483. }
  15484. /*==========================================
  15485. * Elemental System
  15486. *==========================================*/
  15487. /// Notifies client of a change in an elemental's status parameter.
  15488. /// 0x81e <type>.W <value>.L (ZC_EL_PAR_CHANGE)
  15489. void clif_elemental_updatestatus(map_session_data& sd, _sp type) {
  15490. #if PACKETVER >= 20100309
  15491. if (sd.ed == nullptr)
  15492. return;
  15493. PACKET_ZC_EL_PAR_CHANGE p = {};
  15494. p.packetType = HEADER_ZC_EL_PAR_CHANGE;
  15495. p.type = static_cast<decltype(p.type)>(type);
  15496. status_data* status = &sd.ed->battle_status;
  15497. switch( type ) {
  15498. case SP_HP:
  15499. p.value = static_cast<decltype(p.value)>(status->hp);
  15500. break;
  15501. case SP_MAXHP:
  15502. p.value = static_cast<decltype(p.value)>(status->max_hp);
  15503. break;
  15504. case SP_SP:
  15505. p.value = static_cast<decltype(p.value)>(status->sp);
  15506. break;
  15507. case SP_MAXSP:
  15508. p.value = static_cast<decltype(p.value)>(status->max_sp);
  15509. break;
  15510. }
  15511. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  15512. #endif
  15513. }
  15514. void clif_elemental_info(map_session_data *sd) {
  15515. int fd;
  15516. s_elemental_data *ed;
  15517. struct status_data *status;
  15518. if( !clif_session_isValid(sd) || (ed = sd->ed) == nullptr )
  15519. return;
  15520. fd = sd->fd;
  15521. status = &ed->battle_status;
  15522. WFIFOHEAD(fd,22);
  15523. WFIFOW(fd, 0) = 0x81d;
  15524. WFIFOL(fd, 2) = ed->bl.id;
  15525. WFIFOL(fd, 6) = status->hp;
  15526. WFIFOL(fd,10) = status->max_hp;
  15527. WFIFOL(fd,14) = status->sp;
  15528. WFIFOL(fd,18) = status->max_sp;
  15529. WFIFOSET(fd,22);
  15530. }
  15531. /// Buying Store System
  15532. ///
  15533. /// Opens preparation window for buying store (ZC_OPEN_BUYING_STORE).
  15534. /// 0810 <slots>.B
  15535. void clif_buyingstore_open(map_session_data* sd)
  15536. {
  15537. int fd = sd->fd;
  15538. WFIFOHEAD(fd,packet_len(0x810));
  15539. WFIFOW(fd,0) = 0x810;
  15540. WFIFOB(fd,2) = sd->buyingstore.slots;
  15541. WFIFOSET(fd,packet_len(0x810));
  15542. }
  15543. /// Request to create a buying store.
  15544. /// 0811 <packet len>.W <limit zeny>.L <result>.B <store name>.80B { <name id>.W <amount>.W <price>.L }* (CZ_REQ_OPEN_BUYING_STORE)
  15545. /// result:
  15546. /// 0 = cancel
  15547. /// 1 = open
  15548. static void clif_parse_ReqOpenBuyingStore( int fd, map_session_data* sd ){
  15549. const PACKET_CZ_REQ_OPEN_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15550. // TODO: Make this check global for all variable length packets.
  15551. // minimum packet length
  15552. if( p->packetLength < sizeof( *p ) ){
  15553. ShowError( "clif_parse_ReqOpenBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15554. return;
  15555. }
  15556. if (map_getmapflag(sd->bl.m, MF_NOBUYINGSTORE)) {
  15557. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  15558. return;
  15559. }
  15560. if (map_getcell(sd->bl.m, sd->bl.x, sd->bl.y, CELL_CHKNOBUYINGSTORE)) {
  15561. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  15562. return;
  15563. }
  15564. char storename[MESSAGE_SIZE];
  15565. safestrncpy( storename, p->storeName, sizeof( storename ) );
  15566. // so that buyingstore_create knows, how many elements it has access to
  15567. int packet_len = p->packetLength - sizeof( *p );
  15568. if( packet_len % sizeof( p->items[0] ) ){
  15569. 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] ) );
  15570. return;
  15571. }
  15572. buyingstore_create( sd, p->zenyLimit, p->result, storename, p->items, packet_len / sizeof( p->items[0] ), nullptr );
  15573. }
  15574. /// Notification, that the requested buying store could not be created (ZC_FAILED_OPEN_BUYING_STORE_TO_BUYER).
  15575. /// 0812 <result>.W <total weight>.L
  15576. /// result:
  15577. /// 1 = "Failed to open buying store." (0x6cd, MSI_BUYINGSTORE_OPEN_FAILED)
  15578. /// 2 = "Total amount of then possessed items exceeds the weight limit by <weight/10-maxweight*90%>. Please re-enter." (0x6ce, MSI_BUYINGSTORE_OVERWEIGHT)
  15579. /// 8 = "No sale (purchase) information available." (0x705)
  15580. /// ? = nothing
  15581. void clif_buyingstore_open_failed(map_session_data* sd, unsigned short result, unsigned int weight)
  15582. {
  15583. int fd = sd->fd;
  15584. WFIFOHEAD(fd,packet_len(0x812));
  15585. WFIFOW(fd,0) = 0x812;
  15586. WFIFOW(fd,2) = result;
  15587. WFIFOL(fd,4) = weight;
  15588. WFIFOSET(fd,packet_len(0x812));
  15589. }
  15590. /// Notification, that the requested buying store was created.
  15591. /// 0813 <packet len>.W <account id>.L <limit zeny>.L { <price>.L <count>.W <type>.B <name id>.W }* (ZC_MYITEMLIST_BUYING_STORE)
  15592. void clif_buyingstore_myitemlist( map_session_data& sd ){
  15593. #if PACKETVER >= 20100309
  15594. PACKET_ZC_MYITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_MYITEMLIST_BUYING_STORE*>( packet_buffer );
  15595. p->packetType = HEADER_ZC_MYITEMLIST_BUYING_STORE;
  15596. p->packetLength = sizeof( *p );
  15597. p->AID = sd.bl.id;
  15598. p->zenyLimit = sd.buyingstore.zenylimit;
  15599. for( int i = 0; i < sd.buyingstore.slots; i++ ){
  15600. PACKET_ZC_MYITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15601. entry.price = sd.buyingstore.items[i].price;
  15602. entry.amount = sd.buyingstore.items[i].amount;
  15603. entry.itemType = itemtype( sd.buyingstore.items[i].nameid );
  15604. entry.itemId = client_nameid( sd.buyingstore.items[i].nameid );
  15605. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15606. }
  15607. clif_send( p, p->packetLength, &sd.bl, SELF );
  15608. #endif
  15609. }
  15610. /// Notifies clients in area of a buying store (ZC_BUYING_STORE_ENTRY).
  15611. /// 0814 <account id>.L <store name>.80B
  15612. void clif_buyingstore_entry( map_session_data& sd, struct block_list* tbl ){
  15613. #if PACKETVER >= 20100420
  15614. enum send_target target;
  15615. if( tbl == nullptr ){
  15616. tbl = &sd.bl;
  15617. target = AREA_WOS;
  15618. }else{
  15619. target = SELF;
  15620. }
  15621. PACKET_ZC_BUYING_STORE_ENTRY p = {};
  15622. p.packetType = HEADER_ZC_BUYING_STORE_ENTRY;
  15623. p.makerAID = sd.status.account_id;
  15624. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  15625. clif_send( &p, sizeof( p ), tbl, target );
  15626. #endif
  15627. }
  15628. /// Request to close own buying store (CZ_REQ_CLOSE_BUYING_STORE).
  15629. /// 0815
  15630. static void clif_parse_ReqCloseBuyingStore(int fd, map_session_data* sd)
  15631. {
  15632. buyingstore_close(sd);
  15633. }
  15634. /// Notifies clients in area that a buying store was closed (ZC_DISAPPEAR_BUYING_STORE_ENTRY).
  15635. /// 0816 <account id>.L
  15636. void clif_buyingstore_disappear_entry( map_session_data& sd, struct block_list* tbl ){
  15637. #if PACKETVER >= 20100309
  15638. enum send_target target;
  15639. if( tbl == nullptr ){
  15640. tbl = &sd.bl;
  15641. target = AREA_WOS;
  15642. }else{
  15643. target = SELF;
  15644. }
  15645. PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY p = {};
  15646. p.packetType = HEADER_ZC_DISAPPEAR_BUYING_STORE_ENTRY;
  15647. p.makerAID = sd.status.account_id;
  15648. clif_send( &p, sizeof( p ), tbl, target );
  15649. #endif
  15650. }
  15651. /// Request to open someone else's buying store (CZ_REQ_CLICK_TO_BUYING_STORE).
  15652. /// 0817 <account id>.L
  15653. static void clif_parse_ReqClickBuyingStore(int fd, map_session_data* sd)
  15654. {
  15655. uint32 account_id;
  15656. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  15657. buyingstore_open(sd, account_id);
  15658. }
  15659. /// Sends buying store item list.
  15660. /// 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)
  15661. void clif_buyingstore_itemlist( map_session_data& sd, map_session_data& pl_sd ){
  15662. #if PACKETVER >= 20100309
  15663. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_ACK_ITEMLIST_BUYING_STORE*>( packet_buffer );
  15664. p->packetType = HEADER_ZC_ACK_ITEMLIST_BUYING_STORE;
  15665. p->packetLength = sizeof( *p );
  15666. p->AID = pl_sd.bl.id;
  15667. p->storeId = pl_sd.buyer_id;
  15668. p->zenyLimit = pl_sd.buyingstore.zenylimit;
  15669. for( int i = 0; i < pl_sd.buyingstore.slots; i++ ){
  15670. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15671. entry.price = pl_sd.buyingstore.items[i].price;
  15672. entry.amount = pl_sd.buyingstore.items[i].amount; // TODO: Figure out, if no longer needed items (amount == 0) are listed on official.
  15673. entry.itemType = itemtype(pl_sd.buyingstore.items[i].nameid);
  15674. entry.itemId = client_nameid( pl_sd.buyingstore.items[i].nameid );
  15675. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15676. }
  15677. clif_send( p, p->packetLength, &sd.bl, SELF );
  15678. #endif
  15679. }
  15680. /// Request to sell items to a buying store.
  15681. /// 0819 <packet len>.W <account id>.L <store id>.L { <index>.W <name id>.W <amount>.W }* (CZ_REQ_TRADE_BUYING_STORE)
  15682. static void clif_parse_ReqTradeBuyingStore( int fd, map_session_data* sd ){
  15683. const PACKET_CZ_REQ_TRADE_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_TRADE_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15684. // minimum packet length
  15685. if( p->packetLength < sizeof( *p ) ){
  15686. ShowError( "clif_parse_ReqTradeBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15687. return;
  15688. }
  15689. // so that buyingstore_trade knows, how many elements it has access to
  15690. int packet_len = p->packetLength - sizeof( *p );
  15691. if( packet_len % sizeof( p->items[0] ) ){
  15692. 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] ) );
  15693. return;
  15694. }
  15695. buyingstore_trade( sd, p->AID, p->storeId, p->items, packet_len / sizeof( p->items[0] ) );
  15696. }
  15697. /// Notifies the buyer, that the buying store has been closed due to a post-trade condition (ZC_FAILED_TRADE_BUYING_STORE_TO_BUYER).
  15698. /// 081a <result>.W
  15699. /// result:
  15700. /// 3 = "All items within the buy limit were purchased." (0x6cf, MSI_BUYINGSTORE_TRADE_OVERLIMITZENY)
  15701. /// 4 = "All items were purchased." (0x6d0, MSI_BUYINGSTORE_TRADE_BUYCOMPLETE)
  15702. /// ? = nothing
  15703. void clif_buyingstore_trade_failed_buyer(map_session_data* sd, short result)
  15704. {
  15705. int fd = sd->fd;
  15706. WFIFOHEAD(fd,packet_len(0x81a));
  15707. WFIFOW(fd,0) = 0x81a;
  15708. WFIFOW(fd,2) = result;
  15709. WFIFOSET(fd,packet_len(0x81a));
  15710. }
  15711. /// Updates the zeny limit and an item in the buying store item list.
  15712. /// 081b <name id>.W <amount>.W <limit zeny>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE)
  15713. /// 09e6 <name id>.W <amount>.W <zeny>.L <limit zeny>.L <GID>.L <Date>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE2)
  15714. void clif_buyingstore_update_item( map_session_data* sd, t_itemid nameid, unsigned short amount, uint32 char_id, int zeny ){
  15715. PACKET_ZC_UPDATE_ITEM_FROM_BUYING_STORE p = {};
  15716. p.packetType = buyingStoreUpdateItemType;
  15717. p.itemId = client_nameid( nameid );
  15718. p.amount = amount;
  15719. p.zenyLimit = sd->buyingstore.zenylimit;
  15720. #if PACKETVER >= 20141016
  15721. p.zeny = zeny;
  15722. p.charId = char_id; // GID
  15723. p.updateTime = (int)time(nullptr);
  15724. #endif
  15725. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15726. }
  15727. /// Deletes item from inventory, that was sold to a buying store (ZC_ITEM_DELETE_BUYING_STORE).
  15728. /// 081c <index>.W <amount>.W <price>.L
  15729. /// message:
  15730. /// "%s (%d) were sold at %dz." (0x6d2, MSI_BUYINGSTORE_TRADE_SELLCOMPLETE)
  15731. ///
  15732. /// NOTE: This function has to be called _instead_ of clif_delitem/clif_dropitem.
  15733. void clif_buyingstore_delete_item(map_session_data* sd, short index, unsigned short amount, int price)
  15734. {
  15735. int fd = sd->fd;
  15736. WFIFOHEAD(fd,packet_len(0x81c));
  15737. WFIFOW(fd,0) = 0x81c;
  15738. WFIFOW(fd,2) = client_index( index );
  15739. WFIFOW(fd,4) = amount;
  15740. WFIFOL(fd,6) = price; // price per item, client calculates total Zeny by itself
  15741. WFIFOSET(fd,packet_len(0x81c));
  15742. }
  15743. /// Notifies the seller, that a buying store trade failed.
  15744. /// 0824 <result>.W <name id>.W (ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER)
  15745. /// result:
  15746. /// 5 = "The deal has failed." (0x39, MSI_DEAL_FAIL)
  15747. /// 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)
  15748. /// 7 = "The trade failed, because the buyer is lacking required balance." (0x6d1, MSI_BUYINGSTORE_TRADE_LACKBUYERZENY)
  15749. /// ? = nothing
  15750. void clif_buyingstore_trade_failed_seller( map_session_data* sd, short result, t_itemid nameid ){
  15751. PACKET_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER p = {};
  15752. p.packetType = HEADER_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER;
  15753. p.result = result;
  15754. p.itemId = client_nameid( nameid );
  15755. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15756. }
  15757. /// Search Store Info System
  15758. ///
  15759. /// Request to search for stores.
  15760. /// 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)
  15761. /// type:
  15762. /// 0 = Vending
  15763. /// 1 = Buying Store
  15764. ///
  15765. /// NOTE: The client determines the item ids by specifying a name and optionally,
  15766. /// amount of card slots. If the client does not know about the item it
  15767. /// cannot be searched.
  15768. static void clif_parse_SearchStoreInfo( int fd, map_session_data *sd ){
  15769. if (!battle_config.feature_search_stores)
  15770. return;
  15771. const PACKET_CZ_SEARCH_STORE_INFO* p = reinterpret_cast<PACKET_CZ_SEARCH_STORE_INFO*>( RFIFOP( fd, 0 ) );
  15772. // minimum packet length
  15773. if( p->packetLength < sizeof( *p ) ){
  15774. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15775. return;
  15776. }
  15777. // check, if there is enough data for the claimed count of items
  15778. int packet_len = p->packetLength - sizeof( *p );
  15779. if( packet_len % sizeof( p->items[0] ) ){
  15780. 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] ) );
  15781. return;
  15782. }
  15783. int count = packet_len / sizeof( p->items[0] );
  15784. if( count < p->itemsCount + p->cardsCount ){
  15785. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected count=%u, count=%u, account_id=%d).\n", p->itemsCount + p->cardsCount, count, sd->bl.id );
  15786. return;
  15787. }
  15788. if ( p->searchType > SEARCHTYPE_BUYING_STORE ) {
  15789. ShowError( "clif_parse_SearchStoreInfo: Invalid search type %u (account_id=%d).\n", p->searchType, sd->bl.id );
  15790. return;
  15791. }
  15792. if ( p->minPrice > battle_config.vending_max_value ) {
  15793. ShowError( "clif_parse_SearchStoreInfo: Invalid min price %u (account_id=%d).\n", p->minPrice, sd->bl.id );
  15794. return;
  15795. }
  15796. if ( p->maxPrice > battle_config.vending_max_value ) {
  15797. ShowError( "clif_parse_SearchStoreInfo: Invalid max price %u (account_id=%d).\n", p->maxPrice, sd->bl.id );
  15798. return;
  15799. }
  15800. 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 );
  15801. }
  15802. /// Results for a store search request.
  15803. /// 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)
  15804. /// is first page:
  15805. /// 0 = appends to existing results
  15806. /// 1 = clears previous results before displaying this result set
  15807. /// is next page:
  15808. /// 0 = no "next" label
  15809. /// 1 = "next" label to retrieve more results
  15810. void clif_search_store_info_ack( map_session_data& sd ){
  15811. #if PACKETVER_MAIN_NUM >= 20100817 || PACKETVER_RE_NUM >= 20100706 || defined(PACKETVER_ZERO)
  15812. if (!battle_config.feature_search_stores)
  15813. return;
  15814. uint32 start = sd.searchstore.pages * SEARCHSTORE_RESULTS_PER_PAGE ;
  15815. uint32 end = umin( static_cast<uint32>( sd.searchstore.items.size() ), start + SEARCHSTORE_RESULTS_PER_PAGE );
  15816. PACKET_ZC_SEARCH_STORE_INFO_ACK* p = reinterpret_cast<PACKET_ZC_SEARCH_STORE_INFO_ACK*>( packet_buffer );
  15817. p->packetType = HEADER_ZC_SEARCH_STORE_INFO_ACK;
  15818. p->packetLength = sizeof( *p );
  15819. p->firstPage = !sd.searchstore.pages;
  15820. p->nextPage = searchstore_querynext( sd );
  15821. p->usesCount = static_cast<decltype(p->usesCount)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(p->usesCount)>::max() ) );
  15822. for( int i = 0, count = 0; i < end - start; i++ ){
  15823. std::shared_ptr<s_search_store_info_item> ssitem = sd.searchstore.items[start + i];
  15824. if( ssitem == nullptr ){
  15825. continue;
  15826. }
  15827. PACKET_ZC_SEARCH_STORE_INFO_ACK_sub& entry = p->items[count];
  15828. entry.storeId = ssitem->store_id;
  15829. entry.AID = ssitem->account_id;
  15830. safestrncpy( entry.shopName, ssitem->store_name, MESSAGE_SIZE );
  15831. entry.itemId = client_nameid( ssitem->nameid );
  15832. entry.itemType = itemtype( ssitem->nameid );
  15833. entry.price = ssitem->price;
  15834. entry.amount = ssitem->amount;
  15835. entry.refine = ssitem->refine;
  15836. // make-up an item for clif_addcards
  15837. struct item it = {};
  15838. for( int j = 0; j < MAX_SLOTS; j++ ){
  15839. it.card[j] = ssitem->card[j];
  15840. }
  15841. it.nameid = ssitem->nameid;
  15842. it.amount = ssitem->amount;
  15843. clif_addcards( &entry.slot, &it );
  15844. #if PACKETVER >= 20150226
  15845. clif_add_random_options( entry.option_data, it );
  15846. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15847. entry.grade = ssitem->enchantgrade;
  15848. #endif
  15849. #endif
  15850. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15851. count++;
  15852. }
  15853. clif_send( p, p->packetLength, &sd.bl, SELF );
  15854. #endif
  15855. }
  15856. /// Notification of failure when searching for stores.
  15857. /// 0837 <reason>.B (ZC_SEARCH_STORE_INFO_FAILED)
  15858. void clif_search_store_info_failed(map_session_data& sd, e_searchstore_failure reason){
  15859. #if PACKETVER_MAIN_NUM >= 20100601 || PACKETVER_RE_NUM >= 20100601 || defined(PACKETVER_ZERO)
  15860. if (!battle_config.feature_search_stores)
  15861. return;
  15862. PACKET_ZC_SEARCH_STORE_INFO_FAILED packet{};
  15863. packet.packetType = HEADER_ZC_SEARCH_STORE_INFO_FAILED;
  15864. packet.reason = static_cast<decltype(packet.reason)>(reason);
  15865. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15866. #endif
  15867. }
  15868. /// Request to display next page of results.
  15869. /// 0838 (CZ_SEARCH_STORE_INFO_NEXT_PAGE)
  15870. static void clif_parse_SearchStoreInfoNextPage(int fd, map_session_data* sd){
  15871. if (!battle_config.feature_search_stores)
  15872. return;
  15873. searchstore_next(*sd);
  15874. }
  15875. /// Opens the search store window.
  15876. /// 083a <effect>.W <remaining uses>.B (ZC_OPEN_SEARCH_STORE_INFO)
  15877. void clif_open_search_store_info(map_session_data& sd){
  15878. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15879. if (!battle_config.feature_search_stores)
  15880. return;
  15881. PACKET_ZC_OPEN_SEARCH_STORE_INFO packet{};
  15882. packet.packetType = HEADER_ZC_OPEN_SEARCH_STORE_INFO;
  15883. packet.effect = static_cast<decltype(packet.effect)>(sd.searchstore.effect);
  15884. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15885. packet.remainingUses = static_cast<decltype(packet.remainingUses)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(packet.remainingUses)>::max() ) );
  15886. #endif
  15887. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15888. #endif
  15889. }
  15890. /// Request to close the store search window.
  15891. /// 083b (CZ_CLOSE_SEARCH_STORE_INFO)
  15892. static void clif_parse_CloseSearchStoreInfo(int fd, map_session_data* sd){
  15893. if (!battle_config.feature_search_stores)
  15894. return;
  15895. searchstore_close(*sd);
  15896. }
  15897. /// Request to invoke catalog effect on a store from search results.
  15898. /// 083c <account id>.L <store id>.L <nameid>.W (CZ_SSILIST_ITEM_CLICK)
  15899. static void clif_parse_SearchStoreInfoListItemClick( int fd, map_session_data* sd ){
  15900. if (!battle_config.feature_search_stores)
  15901. return;
  15902. const PACKET_CZ_SSILIST_ITEM_CLICK* p = reinterpret_cast<PACKET_CZ_SSILIST_ITEM_CLICK*>( RFIFOP( fd, 0 ) );
  15903. searchstore_click( *sd, p->AID, p->storeId, p->itemId );
  15904. }
  15905. /// Notification of the store position on current map.
  15906. /// 083d <xPos>.W <yPos>.W (ZC_SSILIST_ITEM_CLICK_ACK)
  15907. void clif_search_store_info_click_ack(map_session_data& sd, int16 x, int16 y){
  15908. #if PACKETVER_MAIN_NUM >= 20100608 || PACKETVER_RE_NUM >= 20100608 || defined(PACKETVER_ZERO)
  15909. if (!battle_config.feature_search_stores)
  15910. return;
  15911. PACKET_ZC_SSILIST_ITEM_CLICK_ACK packet{};
  15912. packet.packetType = HEADER_ZC_SSILIST_ITEM_CLICK_ACK;
  15913. packet.x = x;
  15914. packet.y = y;
  15915. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15916. #endif
  15917. }
  15918. /// Parse function for packet debugging.
  15919. void clif_parse_debug(int fd,map_session_data *sd)
  15920. {
  15921. int16 packet_len;
  15922. // clif_parse ensures, that there is at least 2 bytes of data
  15923. uint16 cmd = RFIFOW(fd,0);
  15924. if( sd ) {
  15925. packet_len = packet_db[cmd].len;
  15926. if( packet_len == 0 )
  15927. {// unknown
  15928. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  15929. }
  15930. else if( packet_len == -1 )
  15931. {// variable length
  15932. packet_len = RFIFOW(fd,2); // clif_parse ensures, that this amount of data is already received
  15933. }
  15934. 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);
  15935. }
  15936. else
  15937. {
  15938. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  15939. ShowDebug("Packet debug of 0x%04X (length %d), session #%d\n", cmd, packet_len, fd);
  15940. }
  15941. ShowDump(RFIFOP(fd,0), packet_len);
  15942. }
  15943. /*==========================================
  15944. * Server tells client to display a window similar to Magnifier (item) one
  15945. * Server populates the window with avilable elemental converter options according to player's inventory
  15946. *------------------------------------------*/
  15947. void clif_elementalconverter_list( map_session_data& sd ){
  15948. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  15949. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  15950. p->packetLength = sizeof( *p );
  15951. int count = 0;
  15952. for( int i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  15953. if( skill_can_produce_mix( &sd, skill_produce_db[i].nameid, 23, 1 ) ){
  15954. p->items[count].itemId = client_nameid( skill_produce_db[i].nameid );
  15955. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  15956. count++;
  15957. }
  15958. }
  15959. clif_send( p, p->packetLength, &sd.bl, SELF );
  15960. if( count > 0 ){
  15961. sd.menuskill_id = SA_CREATECON;
  15962. sd.menuskill_val = count;
  15963. }
  15964. }
  15965. /**
  15966. * Rune Knight
  15967. **/
  15968. void clif_millenniumshield( block_list& bl, int16 shields ){
  15969. #if PACKETVER >= 20081126
  15970. PACKET_ZC_MILLENNIUMSHIELD packet{};
  15971. packet.packetType = HEADER_ZC_MILLENNIUMSHIELD;
  15972. packet.aid = bl.id;
  15973. packet.num = shields;
  15974. packet.state = 0;
  15975. clif_send( &packet, sizeof( packet ), &bl, AREA );
  15976. #endif
  15977. }
  15978. /**
  15979. * Mechanic
  15980. **/
  15981. /*==========================================
  15982. * Magic Decoy Material List
  15983. *------------------------------------------*/
  15984. void clif_magicdecoy_list( map_session_data& sd, uint16 skill_lv, short x, short y ){
  15985. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  15986. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  15987. p->packetLength = sizeof( *p );
  15988. size_t count = 0;
  15989. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  15990. if( itemdb_group.item_exists( IG_ELEMENT, sd.inventory.u.items_inventory[i].nameid ) ){
  15991. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  15992. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  15993. count++;
  15994. }
  15995. }
  15996. if( count > 0 ){
  15997. clif_send( p, p->packetLength, &sd.bl, SELF );
  15998. sd.menuskill_id = NC_MAGICDECOY;
  15999. sd.menuskill_val = skill_lv;
  16000. sd.sc.comet_x = x;
  16001. sd.sc.comet_y = y;
  16002. }else{
  16003. clif_skill_fail( sd, NC_MAGICDECOY );
  16004. }
  16005. }
  16006. /*==========================================
  16007. * Guillotine Cross Poisons List
  16008. *------------------------------------------*/
  16009. void clif_poison_list( map_session_data& sd, uint16 skill_lv ){
  16010. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16011. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16012. p->packetLength = sizeof( *p );
  16013. size_t count = 0;
  16014. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  16015. if( itemdb_group.item_exists( IG_POISON, sd.inventory.u.items_inventory[i].nameid ) ){
  16016. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  16017. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16018. count++;
  16019. }
  16020. }
  16021. if( count > 0 ){
  16022. clif_send( p, p->packetLength, &sd.bl, SELF );
  16023. sd.menuskill_id = GC_POISONINGWEAPON;
  16024. sd.menuskill_val = skill_lv;
  16025. }else{
  16026. clif_skill_fail( sd, GC_POISONINGWEAPON, USESKILL_FAIL_GUILLONTINE_POISON );
  16027. }
  16028. }
  16029. /// 0442 <Length>.W <why>.L { <Skill_Id>.W }* (ZC_SKILL_SELECT_REQUEST).
  16030. void clif_autoshadowspell_list( map_session_data& sd ){
  16031. #if PACKETVER >= 20081210
  16032. if( sd.menuskill_id == SC_AUTOSHADOWSPELL )
  16033. return;
  16034. PACKET_ZC_SKILL_SELECT_REQUEST* p = reinterpret_cast<PACKET_ZC_SKILL_SELECT_REQUEST*>( packet_buffer );
  16035. p->packetType = HEADER_ZC_SKILL_SELECT_REQUEST;
  16036. p->packetLength = sizeof( *p );
  16037. p->flag = 1; // enum PACKET_ZC_SKILL_SELECT_REQUEST::enumWHY::WHY_SC_AUTOSHADOWSPELL = 0x1
  16038. size_t count = 0;
  16039. for( size_t i = 0; i < MAX_SKILL; i++ ){
  16040. if( sd.status.skill[i].flag == SKILL_FLAG_PLAGIARIZED && sd.status.skill[i].id > 0 &&
  16041. skill_get_inf2(sd.status.skill[i].id, INF2_ISAUTOSHADOWSPELL))
  16042. {
  16043. p->skillIds[count] = sd.status.skill[i].id;
  16044. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skillIds[0] ) );
  16045. count++;
  16046. }
  16047. }
  16048. if( count > 0 ) {
  16049. clif_send( p, p->packetLength, &sd.bl, SELF );
  16050. sd.menuskill_id = SC_AUTOSHADOWSPELL;
  16051. sd.menuskill_val = static_cast<decltype(sd.menuskill_val)>( count );
  16052. } else {
  16053. status_change_end( &sd.bl, SC_STOP );
  16054. clif_skill_fail( sd, SC_AUTOSHADOWSPELL, USESKILL_FAIL_IMITATION_SKILL_NONE );
  16055. }
  16056. #endif
  16057. }
  16058. /*===========================================
  16059. * Skill list for Four Elemental Analysis
  16060. * and Change Material skills.
  16061. * 07e3 <type>.L (ZC_ITEMLISTWIN_OPEN).
  16062. *------------------------------------------*/
  16063. int clif_skill_itemlistwindow( map_session_data *sd, uint16 skill_id, uint16 skill_lv ) {
  16064. #if PACKETVER >= 20090922
  16065. int fd = sd->fd;
  16066. nullpo_ret(sd);
  16067. sd->menuskill_id = skill_id; // To prevent hacking.
  16068. sd->menuskill_val = skill_lv;
  16069. if( skill_id == GN_CHANGEMATERIAL )
  16070. skill_lv = 0; // Changematerial
  16071. WFIFOHEAD(fd, packet_len(0x7e3));
  16072. WFIFOW(fd,0) = 0x7e3;
  16073. WFIFOL(fd,2) = skill_lv;
  16074. WFIFOL(fd,4) = 0;
  16075. WFIFOSET(fd, packet_len(0x7e3));
  16076. #endif
  16077. return 1;
  16078. }
  16079. /*==========================================
  16080. * Select a skill into a given list (used by SA_AUTOSPELL/SC_AUTOSHADOWSPELL)
  16081. * 0443 <type>.L <skill_id>.W (CZ_SKILL_SELECT_RESPONSE)
  16082. *------------------------------------------*/
  16083. void clif_parse_SkillSelectMenu(int fd, map_session_data *sd) {
  16084. #if PACKETVER >= 20081210
  16085. if( sd == nullptr ){
  16086. return;
  16087. }
  16088. const PACKET_CZ_SKILL_SELECT_RESPONSE* p = reinterpret_cast<PACKET_CZ_SKILL_SELECT_RESPONSE*>( RFIFOP( fd, 0 ) );
  16089. if (sd->menuskill_id == SA_AUTOSPELL) {
  16090. sd->state.workinprogress = WIP_DISABLE_NONE;
  16091. skill_autospell(sd, p->selectedSkillId);
  16092. } else if (sd->menuskill_id == SC_AUTOSHADOWSPELL) {
  16093. if (pc_istrading(sd)) {
  16094. clif_skill_fail( *sd, sd->ud.skill_id );
  16095. clif_menuskill_clear(sd);
  16096. return;
  16097. }
  16098. skill_select_menu(*sd, p->selectedSkillId);
  16099. } else
  16100. return;
  16101. clif_menuskill_clear(sd);
  16102. #endif
  16103. }
  16104. /// Kagerou/Oboro amulet spirit.
  16105. /// 08cf <id>.L <type>.W <num>.W (ZC_SPIRITS_ATTRIBUTE)
  16106. void clif_spiritcharm( map_session_data& sd ){
  16107. #if PACKETVER >= 20111102
  16108. PACKET_ZC_SPIRITS_ATTRIBUTE packet{};
  16109. packet.packetType = HEADER_ZC_SPIRITS_ATTRIBUTE;
  16110. packet.aid = sd.bl.id;
  16111. packet.spiritsType = static_cast<decltype(packet.spiritsType)>(sd.spiritcharm_type);
  16112. packet.num = static_cast<decltype(packet.num)>(sd.spiritcharm);
  16113. clif_send( &packet, sizeof( packet ), &sd.bl, AREA );
  16114. #endif
  16115. }
  16116. /// Move Item from or to Personal Tab
  16117. /// 0907 <index>.W <type>.B (CZ_INVENTORY_TAB)
  16118. /// type:
  16119. /// 0 = move item to personal tab
  16120. /// 1 = move item to normal tab
  16121. void clif_parse_MoveItem( int fd, map_session_data* sd ){
  16122. // TODO: Check for correct packet version
  16123. #if PACKETVER >= 20120410
  16124. /* can't move while dead. */
  16125. if(pc_isdead(sd)) {
  16126. return;
  16127. }
  16128. const PACKET_CZ_INVENTORY_TAB* p = reinterpret_cast<PACKET_CZ_INVENTORY_TAB*>( RFIFOP( fd, 0 ) );
  16129. uint16 index = server_index( p->index );
  16130. if( index >= MAX_INVENTORY ){
  16131. return;
  16132. }
  16133. if ( sd->inventory.u.items_inventory[index].favorite != 0 && p->favorite == true )
  16134. sd->inventory.u.items_inventory[index].favorite = 0;
  16135. else if( p->favorite == false )
  16136. sd->inventory.u.items_inventory[index].favorite = 1;
  16137. else
  16138. return;/* nothing to do. */
  16139. clif_favorite_item( *sd, index );
  16140. #endif
  16141. }
  16142. /// Items that are in favorite tab of inventory.
  16143. /// 0908 <index>.W <favorite>.B (ZC_INVENTORY_TAB)
  16144. static void clif_favorite_item( map_session_data& sd, uint16 index ){
  16145. // TODO: Check for correct packet version
  16146. #if PACKETVER >= 20111122
  16147. PACKET_ZC_INVENTORY_TAB p = {};
  16148. p.packetType = HEADER_ZC_INVENTORY_TAB;
  16149. p.index = client_index( index );
  16150. p.favorite = ( sd.inventory.u.items_inventory[index].favorite == 1 ) ? false : true;
  16151. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16152. #endif
  16153. }
  16154. /// 08d2 <id>.L <Pos X>.W <Pos Y>.W (ZC_FASTMOVE).
  16155. void clif_snap( struct block_list *bl, short x, short y ) {
  16156. unsigned char buf[10];
  16157. WBUFW(buf,0) = 0x8d2;
  16158. WBUFL(buf,2) = bl->id;
  16159. WBUFW(buf,6) = x;
  16160. WBUFW(buf,8) = y;
  16161. if( disguised(bl) )
  16162. {
  16163. clif_send(buf, packet_len(0x8d2), bl, AREA_WOS);
  16164. WBUFL(buf,2) = disguised_bl_id(bl->id);
  16165. clif_send(buf, packet_len(0x8d2), bl, SELF);
  16166. } else
  16167. clif_send(buf,packet_len(0x8d2),bl, AREA);
  16168. }
  16169. /// 0977 <id>.L <HP>.L <maxHP>.L (ZC_HP_INFO).
  16170. void clif_monster_hp_bar( struct mob_data* md, int fd ) {
  16171. #if PACKETVER >= 20120404
  16172. WFIFOHEAD(fd,packet_len(0x977));
  16173. WFIFOW(fd,0) = 0x977;
  16174. WFIFOL(fd,2) = md->bl.id;
  16175. WFIFOL(fd,6) = md->status.hp;
  16176. WFIFOL(fd,10) = md->status.max_hp;
  16177. WFIFOSET(fd,packet_len(0x977));
  16178. #endif
  16179. }
  16180. /* [Ind] placeholder for unsupported incoming packets (avoids server disconnecting client) */
  16181. void __attribute__ ((unused)) clif_parse_dull(int fd, map_session_data *sd) {
  16182. return;
  16183. }
  16184. /// Ack world info (ZC_ACK_BEFORE_WORLD_INFO)
  16185. /// 0979 <world name>.24B <char name>.24B
  16186. void clif_ackworldinfo(map_session_data* sd) {
  16187. int fd;
  16188. nullpo_retv(sd);
  16189. fd = sd->fd;
  16190. WFIFOHEAD(fd,packet_len(0x979));
  16191. WFIFOW(fd,0)=0x979;
  16192. //AID -> world name ?
  16193. safestrncpy(WFIFOCP(fd,2), "" /* World name */, 24);
  16194. safestrncpy(WFIFOCP(fd,26), sd->status.name, NAME_LENGTH);
  16195. WFIFOSET(fd,packet_len(0x979));
  16196. }
  16197. /// req world info (CZ_REQ_BEFORE_WORLD_INFO)
  16198. /// 0978 <AID>.L
  16199. void clif_parse_reqworldinfo(int fd,map_session_data *sd) {
  16200. //uint32 aid = RFIFOL(fd,2); //should we trust client ?
  16201. if(sd)
  16202. clif_ackworldinfo(sd);
  16203. }
  16204. static void clif_loadConfirm( map_session_data *sd ){
  16205. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410
  16206. nullpo_retv( sd );
  16207. PACKET_ZC_NOTIFY_ACTORINIT p = {};
  16208. p.packetType = HEADER_ZC_NOTIFY_ACTORINIT;
  16209. clif_send( &p, sizeof(p), &sd->bl, SELF );
  16210. clif_instance_info( *sd );
  16211. #endif
  16212. }
  16213. /// unknown usage (CZ_BLOCKING_PLAY_CANCEL)
  16214. /// 0447
  16215. void clif_parse_blocking_playcancel( int fd, map_session_data *sd ){
  16216. clif_loadConfirm( sd );
  16217. int32 mf = map_getmapflag(sd->bl.m, MF_SPECIALPOPUP);
  16218. if (mf > 0) {
  16219. clif_specialpopup(*sd, mf);
  16220. }
  16221. }
  16222. /// req world info (CZ_CLIENT_VERSION)
  16223. /// 044A <version>.L
  16224. void clif_parse_client_version(int fd, map_session_data *sd) {
  16225. //if(sd)
  16226. ;
  16227. }
  16228. /// Ranking list
  16229. /// ranking pointlist { <name>.24B <point>.L }*10
  16230. void clif_sub_ranklist(unsigned char *buf,int idx,map_session_data* sd, enum e_rank rankingtype){
  16231. struct fame_list* list;
  16232. int i, size = MAX_FAME_LIST;
  16233. switch(rankingtype) {
  16234. case RANK_BLACKSMITH: list = smith_fame_list; break;
  16235. case RANK_ALCHEMIST: list = chemist_fame_list; break;
  16236. case RANK_TAEKWON: list = taekwon_fame_list; break;
  16237. // PK currently unsupported
  16238. case RANK_KILLER: list = nullptr; size = 0; break;
  16239. default:
  16240. ShowError( "clif_sub_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16241. return;
  16242. }
  16243. //Packet size limits this list to 10 elements. [Skotlex]
  16244. for (i = 0; i < min(10,size); i++) {
  16245. if (list[i].id > 0) {
  16246. const char* name;
  16247. if (strcmp(list[i].name, "-") == 0 &&
  16248. (name = map_charid2nick(list[i].id)) != nullptr)
  16249. {
  16250. safestrncpy(WBUFCP(buf,idx + NAME_LENGTH * i), name, NAME_LENGTH);
  16251. } else {
  16252. safestrncpy(WBUFCP(buf,idx + NAME_LENGTH * i), list[i].name, NAME_LENGTH);
  16253. }
  16254. } else {
  16255. safestrncpy(WBUFCP(buf, idx + NAME_LENGTH * i), "None", NAME_LENGTH);
  16256. }
  16257. WBUFL(buf, idx+NAME_LENGTH*10 + i * 4) = list[i].fame; //points
  16258. }
  16259. for(;i < 10; i++) { //In case the MAX is less than 10.
  16260. safestrncpy(WBUFCP(buf, idx + NAME_LENGTH * i), "Unavailable", NAME_LENGTH);
  16261. WBUFL(buf, idx+NAME_LENGTH*10 + i * 4) = 0;
  16262. }
  16263. }
  16264. /// Request for the blacksmith ranklist.
  16265. /// /blacksmith command sends this packet to the server.
  16266. /// 0217 (CZ_BLACKSMITH_RANK)
  16267. void clif_parse_Blacksmith( int fd, map_session_data *sd ){
  16268. clif_ranklist(sd,RANK_BLACKSMITH);
  16269. }
  16270. /// Request for the alchemist ranklist.
  16271. /// /alchemist command sends this packet to the server.
  16272. /// 0218 (CZ_ALCHEMIST_RANK)
  16273. void clif_parse_Alchemist( int fd, map_session_data *sd ){
  16274. clif_ranklist(sd,RANK_ALCHEMIST);
  16275. }
  16276. /// Request for the taekwon ranklist.
  16277. /// /taekwon command sends this packet to the server.
  16278. /// 0225 (CZ_TAEKWON_RANK)
  16279. void clif_parse_Taekwon( int fd, map_session_data *sd ){
  16280. clif_ranklist(sd,RANK_TAEKWON);
  16281. }
  16282. /// Request for the killer ranklist.
  16283. /// /pk command sends this packet to the server.
  16284. /// 0237 (CZ_KILLER_RANK)
  16285. void clif_parse_RankingPk( int fd, map_session_data *sd ){
  16286. clif_ranklist(sd,RANK_KILLER);
  16287. }
  16288. /// 0219 { <name>.24B }*10 { <point>.L }*10 (ZC_BLACKSMITH_RANK)
  16289. /// 021a { <name>.24B }*10 { <point>.L }*10 (ZC_ALCHEMIST_RANK)
  16290. /// 0226 { <name>.24B }*10 { <point>.L }*10 (ZC_TAEKWON_RANK)
  16291. /// 0238 { <name>.24B }*10 { <point>.L }*10 (ZC_KILLER_RANK)
  16292. /// 097d <RankingType>.W {<CharName>.24B <point>L}*10 <mypoint>L (ZC_ACK_RANKING)
  16293. void clif_ranklist(map_session_data *sd, int16 rankingType) {
  16294. enum e_rank rank;
  16295. #if PACKETVER < 20130710
  16296. unsigned char buf[MAX_FAME_LIST * sizeof(struct fame_list)+2];
  16297. short cmd;
  16298. switch( rankingType ){
  16299. case RANK_BLACKSMITH: cmd = 0x219; break;
  16300. case RANK_ALCHEMIST: cmd = 0x21a; break;
  16301. case RANK_TAEKWON: cmd = 0x226; break;
  16302. case RANK_KILLER: cmd = 0x238; break;
  16303. default:
  16304. ShowError( "clif_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingType );
  16305. return;
  16306. }
  16307. // Can be safely casted here, since we validated it before
  16308. rank = (enum e_rank)rankingType;
  16309. WBUFW(buf,0) = cmd;
  16310. clif_sub_ranklist(buf,2,sd,rank);
  16311. clif_send(buf, packet_len(cmd), &sd->bl, SELF);
  16312. #else
  16313. unsigned char buf[MAX_FAME_LIST * sizeof(struct fame_list)+8];
  16314. int mypoint = 0;
  16315. switch( rankingType ){
  16316. case RANK_BLACKSMITH:
  16317. case RANK_ALCHEMIST:
  16318. case RANK_TAEKWON:
  16319. case RANK_KILLER:
  16320. break;
  16321. default:
  16322. ShowError( "clif_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingType );
  16323. return;
  16324. }
  16325. // Can be safely casted here, since we validated it before
  16326. rank = (enum e_rank)rankingType;
  16327. WBUFW(buf,0) = 0x97d;
  16328. WBUFW(buf,2) = rank;
  16329. clif_sub_ranklist(buf,4,sd,rank);
  16330. switch(sd->class_&MAPID_UPPERMASK){ //mypoint (checking if valid type)
  16331. case MAPID_BLACKSMITH:
  16332. case MAPID_ALCHEMIST:
  16333. case MAPID_TAEKWON:
  16334. mypoint = sd->status.fame;
  16335. }
  16336. WBUFL(buf,284) = mypoint; //mypoint
  16337. clif_send(buf, 288, &sd->bl, SELF);
  16338. #endif
  16339. }
  16340. /*
  16341. * 097c <type> (CZ_REQ_RANKING)
  16342. * type
  16343. * 0: /blacksmith
  16344. * 1: /alchemist
  16345. * 2: /taekwon
  16346. * 3: /pk
  16347. * */
  16348. void clif_parse_ranklist(int fd,map_session_data *sd) {
  16349. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  16350. int16 rankingtype = RFIFOW(fd,info->pos[0]); //type
  16351. clif_ranklist(sd,rankingtype);
  16352. }
  16353. /// Updates the fame rank points for the given ranking.
  16354. /// 021b <points>.L <total points>.L (ZC_BLACKSMITH_POINT)
  16355. /// 021c <points>.L <total points>.L (ZC_ALCHEMIST_POINT)
  16356. /// 0224 <points>.L <total points>.L (ZC_TAEKWON_POINT)
  16357. /// 097e <RankingType>.W <point>.L <TotalPoint>.L (ZC_UPDATE_RANKING_POINT)
  16358. void clif_update_rankingpoint(map_session_data &sd, int rankingtype, int point) {
  16359. int fd = sd.fd;
  16360. #if PACKETVER < 20130710
  16361. short cmd;
  16362. switch(rankingtype){
  16363. case RANK_BLACKSMITH: cmd = 0x21b; break;
  16364. case RANK_ALCHEMIST: cmd = 0x21c; break;
  16365. case RANK_TAEKWON: cmd = 0x224; break;
  16366. default:
  16367. ShowError( "clif_update_rankingpoint: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16368. return;
  16369. }
  16370. WFIFOHEAD(fd,packet_len(cmd));
  16371. WFIFOW(fd,0) = cmd;
  16372. WFIFOL(fd,2) = point;
  16373. WFIFOL(fd,6) = sd.status.fame;
  16374. WFIFOSET(fd, packet_len(cmd));
  16375. #else
  16376. WFIFOHEAD(fd,packet_len(0x97e));
  16377. WFIFOW(fd,0) = 0x97e;
  16378. WFIFOW(fd,2) = rankingtype;
  16379. WFIFOL(fd,4) = point;
  16380. WFIFOL(fd,8) = sd.status.fame;
  16381. WFIFOSET(fd,packet_len(0x97e));
  16382. #endif
  16383. }
  16384. /**
  16385. * Transmit personal information to player. (rates)
  16386. * 0x08cb <packet len>.W <exp>.W <death>.W <drop>.W <DETAIL_EXP_INFO>7B (ZC_PERSONAL_INFOMATION)
  16387. * <InfoType>.B <Exp>.W <Death>.W <Drop>.W (DETAIL_EXP_INFO 0x08cb)
  16388. * 0x097b <packet len>.W <exp>.L <death>.L <drop>.L <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION2)
  16389. * 0x0981 <packet len>.W <exp>.W <death>.W <drop>.W <activity rate>.W <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION_CHN)
  16390. * <InfoType>.B <Exp>.L <Death>.L <Drop>.L (DETAIL_EXP_INFO 0x97b|0981)
  16391. * FIXME!
  16392. * - Find/decide for details of EXP, Drop, and Death penalty rates
  16393. * - For now, we're assuming values for DETAIL_EXP_INFO are:
  16394. * 0 - map adjustment (bexp mapflag), 1 - Premium/VIP adjustment, 2 - Server rate adjustment, 3 - None
  16395. */
  16396. #ifdef VIP_ENABLE
  16397. void clif_display_pinfo( map_session_data& sd ){
  16398. #if PACKETVER_MAIN_NUM >= 20110627 || PACKETVER_RE_NUM >= 20110628 || defined(PACKETVER_ZERO)
  16399. // 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]
  16400. int details_bexp[PINFO_MAX], details_drop[PINFO_MAX], details_penalty[PINFO_MAX];
  16401. /**
  16402. * EXP
  16403. */
  16404. //0:PCRoom
  16405. details_bexp[PINFO_BASIC] = map_getmapflag( sd.bl.m, MF_BEXP );
  16406. if (details_bexp[PINFO_BASIC] == 100 || !details_bexp[PINFO_BASIC])
  16407. details_bexp[PINFO_BASIC] = 0;
  16408. else {
  16409. if (details_bexp[PINFO_BASIC] < 100) {
  16410. details_bexp[PINFO_BASIC] = 100 - details_bexp[PINFO_BASIC];
  16411. details_bexp[PINFO_BASIC] = 0 - details_bexp[PINFO_BASIC];
  16412. } else
  16413. details_bexp[PINFO_BASIC] = details_bexp[PINFO_BASIC] - 100;
  16414. }
  16415. //1:Premium
  16416. if( pc_isvip( &sd ) ){
  16417. details_bexp[PINFO_PREMIUM] = battle_config.vip_base_exp_increase * battle_config.base_exp_rate / 100;
  16418. if (details_bexp[PINFO_PREMIUM] < 0)
  16419. details_bexp[PINFO_PREMIUM] = 0 - details_bexp[PINFO_PREMIUM];
  16420. } else
  16421. details_bexp[PINFO_PREMIUM] = 0;
  16422. //2:Server
  16423. details_bexp[PINFO_SERVER] = battle_config.base_exp_rate;
  16424. if (details_bexp[PINFO_SERVER] == 100)
  16425. details_bexp[PINFO_SERVER] = 0;
  16426. else {
  16427. if (details_bexp[PINFO_SERVER] < 100) {
  16428. details_bexp[PINFO_SERVER] = 100 - details_bexp[PINFO_SERVER];
  16429. details_bexp[PINFO_SERVER] = 0 - details_bexp[PINFO_SERVER];
  16430. } else
  16431. details_bexp[PINFO_SERVER] = details_bexp[PINFO_SERVER] - 100;
  16432. }
  16433. //3:TPLUS
  16434. details_bexp[PINFO_CAFE] = 0;
  16435. /**
  16436. * Drop rate
  16437. */
  16438. //0:PCRoom
  16439. details_drop[PINFO_BASIC] = 0;
  16440. //1:Premium
  16441. if( pc_isvip( &sd ) ){
  16442. details_drop[PINFO_PREMIUM] = (battle_config.vip_drop_increase * battle_config.item_rate_common) / 100;
  16443. if (details_drop[PINFO_PREMIUM] < 0)
  16444. details_drop[PINFO_PREMIUM] = 0 - details_drop[PINFO_PREMIUM];
  16445. } else
  16446. details_drop[PINFO_PREMIUM] = 0;
  16447. //2:Server
  16448. details_drop[PINFO_SERVER] = battle_config.item_rate_common;
  16449. if (details_drop[PINFO_SERVER] == 100)
  16450. details_drop[PINFO_SERVER] = 0;
  16451. else {
  16452. if (details_drop[PINFO_SERVER] < 100) {
  16453. details_drop[PINFO_SERVER] = 100 - details_drop[PINFO_SERVER];
  16454. details_drop[PINFO_SERVER] = 0 - details_drop[PINFO_SERVER];
  16455. } else
  16456. details_drop[PINFO_SERVER] = details_drop[PINFO_SERVER] - 100;
  16457. }
  16458. //3:TPLUS
  16459. details_drop[PINFO_CAFE] = 0;
  16460. /**
  16461. * Penalty rate
  16462. */
  16463. //! FIXME: Current penalty system makes this announcement unable to give info on + or - rate
  16464. //0:PCRoom
  16465. details_penalty[PINFO_BASIC] = 0;
  16466. //1:Premium
  16467. if( pc_isvip( &sd ) ){
  16468. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base;
  16469. if (details_penalty[PINFO_PREMIUM] == 100)
  16470. details_penalty[PINFO_PREMIUM] = 0;
  16471. else {
  16472. if (details_penalty[PINFO_PREMIUM] < 100) {
  16473. details_penalty[PINFO_PREMIUM] = 100 - details_penalty[PINFO_PREMIUM];
  16474. details_penalty[PINFO_PREMIUM] = 0 - details_penalty[PINFO_PREMIUM];
  16475. } else
  16476. details_penalty[PINFO_PREMIUM] = details_penalty[PINFO_PREMIUM] - 100;
  16477. }
  16478. if (battle_config.death_penalty_base > battle_config.vip_exp_penalty_base)
  16479. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base - battle_config.death_penalty_base;
  16480. } else
  16481. details_penalty[PINFO_PREMIUM] = 0;
  16482. //2:Server
  16483. details_penalty[PINFO_SERVER] = battle_config.death_penalty_base;
  16484. if (details_penalty[PINFO_SERVER] == 100)
  16485. details_penalty[PINFO_SERVER] = 0;
  16486. else {
  16487. if (details_penalty[PINFO_SERVER] < 100) {
  16488. details_penalty[PINFO_SERVER] = 100 - details_penalty[PINFO_SERVER];
  16489. details_penalty[PINFO_SERVER] = 0 - details_penalty[PINFO_SERVER];
  16490. } else
  16491. details_penalty[PINFO_SERVER] = details_penalty[PINFO_SERVER] - 100;
  16492. }
  16493. //3:TPLUS
  16494. details_penalty[PINFO_CAFE] = 0;
  16495. PACKET_ZC_PERSONAL_INFOMATION* p = reinterpret_cast<PACKET_ZC_PERSONAL_INFOMATION*>( packet_buffer );
  16496. p->packetType = HEADER_ZC_PERSONAL_INFOMATION;
  16497. p->length = sizeof( *p );
  16498. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16499. p->total_exp = 100 * 1000;
  16500. p->total_death = 100 * 1000;
  16501. p->total_drop = 100 * 1000;
  16502. #else
  16503. p->total_exp = 100;
  16504. p->total_death = 100;
  16505. p->total_drop = 100;
  16506. #endif
  16507. for( int i = PINFO_BASIC; i < PINFO_MAX; i++ ){
  16508. p->details[i].type = i; // infotype 0 PCRoom, 1 Premium, 2 Server, 3 TPlus
  16509. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16510. p->details[i].exp = details_bexp[i] * 1000;
  16511. p->details[i].death = details_penalty[i] * 1000;
  16512. p->details[i].drop = details_drop[i] * 1000;
  16513. #else
  16514. p->details[i].exp = details_bexp[i];
  16515. p->details[i].death = details_penalty[i];
  16516. p->details[i].drop = details_drop[i];
  16517. #endif
  16518. p->total_exp += p->details[i].exp;
  16519. p->total_death += p->details[i].death;
  16520. p->total_drop += p->details[i].drop;
  16521. p->length += static_cast<decltype(p->length)>( sizeof( p->details[0] ) );
  16522. }
  16523. clif_send( p, p->length, &sd.bl, SELF );
  16524. #endif
  16525. }
  16526. #endif
  16527. void clif_parse_GMFullStrip(int fd, map_session_data *sd) {
  16528. char cmd[30];
  16529. int t_aid = RFIFOL(fd,2);
  16530. safesnprintf(cmd,sizeof(cmd),"%cfullstrip %d",atcommand_symbol,t_aid);
  16531. is_atcommand(fd, sd, cmd, 1);
  16532. }
  16533. /**
  16534. * Marks Crimson Marker target on mini-map to the caster
  16535. * 09C1 <id>.L <x>.W <y>.W (ZC_C_MARKERINFO)
  16536. * @param fd
  16537. * @param bl Crimson Marker target
  16538. **/
  16539. void clif_crimson_marker( map_session_data& sd, struct block_list& bl, bool remove ){
  16540. #if PACKETVER_MAIN_NUM >= 20130731 || PACKETVER_RE_NUM >= 20130707 || defined(PACKETVER_ZERO)
  16541. PACKET_ZC_C_MARKERINFO p = {};
  16542. p.PacketType = HEADER_ZC_C_MARKERINFO;
  16543. p.AID = bl.id;
  16544. if( remove ){
  16545. p.xPos = -1;
  16546. p.yPos = -1;
  16547. }else{
  16548. p.xPos = bl.x;
  16549. p.yPos = bl.y;
  16550. }
  16551. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16552. #endif
  16553. }
  16554. /**
  16555. * 02d3 <index>.W (ZC_NOTIFY_BIND_ON_EQUIP)
  16556. **/
  16557. void clif_notify_bindOnEquip( map_session_data& sd, int16 index ){
  16558. #if PACKETVER >= 20070227
  16559. PACKET_ZC_NOTIFY_BIND_ON_EQUIP p = {};
  16560. p.packetType = HEADER_ZC_NOTIFY_BIND_ON_EQUIP;
  16561. p.index = client_index( index );
  16562. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16563. #endif
  16564. }
  16565. /**
  16566. * [Ind/Hercules]
  16567. * 08b3 <Length>.W <id>.L <message>.?B (ZC_SHOWSCRIPT)
  16568. **/
  16569. void clif_showscript(struct block_list* bl, const char* message, enum send_target flag) {
  16570. char buf[256];
  16571. size_t len;
  16572. nullpo_retv(bl);
  16573. if(!message)
  16574. return;
  16575. len = strlen(message)+1;
  16576. if( len > sizeof(buf)-8 ) {
  16577. ShowWarning("clif_showscript: Truncating too long message '%s' (len=%" PRIuPTR ").\n", message, len);
  16578. len = sizeof(buf)-8;
  16579. }
  16580. WBUFW(buf,0) = 0x8b3;
  16581. WBUFW(buf,2) = (uint16)(len+8);
  16582. WBUFL(buf,4) = bl->id;
  16583. safestrncpy(WBUFCP(buf,8), message, len);
  16584. clif_send((unsigned char *) buf, WBUFW(buf,2), bl, flag);
  16585. }
  16586. /**
  16587. * [Ind/Hercules]
  16588. * 07fc <OldMasterAID>.L <NewMasterAID>.L (ZC_CHANGE_GROUP_MASTER)
  16589. **/
  16590. void clif_party_leaderchanged(map_session_data *sd, int prev_leader_aid, int new_leader_aid) {
  16591. unsigned char buf[10];
  16592. nullpo_retv(sd);
  16593. WBUFW(buf,0) = 0x7fc;
  16594. WBUFL(buf,2) = prev_leader_aid;
  16595. WBUFL(buf,6) = new_leader_aid;
  16596. clif_send(buf, packet_len(0x7fc), &sd->bl, PARTY);
  16597. }
  16598. /**
  16599. * Sends a clan message to a player
  16600. * 098e <length>.W <name>.24B <message>.?B (ZC_NOTIFY_CLAN_CHAT)
  16601. **/
  16602. void clif_clan_message( struct clan& clan, const char *mes, size_t len ){
  16603. #if PACKETVER >= 20131223
  16604. map_session_data* sd = clan_getavailablesd( clan );
  16605. if( len == 0 ){
  16606. return;
  16607. }
  16608. PACKET_ZC_NOTIFY_CLAN_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CLAN_CHAT*>( packet_buffer );
  16609. // Maximum message length = size of our packet buffer minus fixed size of the packet and 1 byte zero termination
  16610. static size_t len_max = sizeof( packet_buffer ) - sizeof( *p ) - 1;
  16611. // Is the length bigger than the maximum message length
  16612. if( len > len_max ){
  16613. ShowWarning( "clif_clan_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", clan_id=%d).\n", mes, len, len_max, clan.id );
  16614. len = len_max;
  16615. }
  16616. p->PacketType = HEADER_ZC_NOTIFY_CLAN_CHAT;
  16617. p->PacketLength = sizeof( *p );
  16618. // 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
  16619. safestrncpy( p->MemberName, "", sizeof( p->MemberName ) );
  16620. safestrncpy( p->Message, mes, len + 1 );
  16621. p->PacketLength += static_cast<decltype(p->PacketLength)>( len + 1 );
  16622. clif_send( p, p->PacketLength, &sd->bl, CLAN );
  16623. #endif
  16624. }
  16625. /**
  16626. * Parses a clan message from a player.
  16627. * 098d <length>.W <text>.?B (<name> : <message>) (CZ_CLAN_CHAT)
  16628. **/
  16629. void clif_parse_clan_chat( int fd, map_session_data* sd ){
  16630. #if PACKETVER >= 20131223
  16631. if( sd == nullptr ){
  16632. return;
  16633. }
  16634. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  16635. // validate packet and retrieve name and message
  16636. if( !clif_process_message(sd, false, name, message, output ) )
  16637. return;
  16638. clan_send_message( *sd, RFIFOCP(fd,4), RFIFOW(fd,2) - 4 );
  16639. #endif
  16640. }
  16641. /**
  16642. * Sends the basic clan informations to the client.
  16643. * 098a <length>.W <clan id>.L <clan name>.24B <clan master>.24B <clan map>.16B <alliance count>.B
  16644. * <antagonist count>.B { <alliance>.24B } * alliance count { <antagonist>.24B } * antagonist count (ZC_CLANINFO)
  16645. **/
  16646. void clif_clan_basicinfo( map_session_data& sd ){
  16647. #if PACKETVER_MAIN_NUM >= 20130626 || PACKETVER_RE_NUM >= 20130605 || defined(PACKETVER_ZERO)
  16648. struct clan* clan = sd.clan;
  16649. if( clan == nullptr ){
  16650. return;
  16651. }
  16652. PACKET_ZC_CLANINFO* p = reinterpret_cast<PACKET_ZC_CLANINFO*>( packet_buffer );
  16653. p->PacketType = HEADER_ZC_CLANINFO;
  16654. p->PacketLength = sizeof( *p );
  16655. p->ClanID = clan->id;
  16656. safestrncpy( p->ClanName, clan->name, sizeof( p->ClanName ) );
  16657. safestrncpy( p->MasterName, clan->master, sizeof( p->MasterName ) );
  16658. mapindex_getmapname_ext( clan->map, p->Map );
  16659. p->AllyCount = clan_get_alliance_count( *clan, 0 );
  16660. p->AntagonistCount = clan_get_alliance_count( *clan, 1 );
  16661. for( int flag = 0; flag < 2; flag++ ){
  16662. for( int i = 0; i < MAX_CLANALLIANCE; i++ ){
  16663. if( clan->alliance[i].clan_id > 0 && clan->alliance[i].opposition == flag ){
  16664. char* name = reinterpret_cast<char*>( WBUFP( p, p->PacketLength ) );
  16665. safestrncpy( name, clan->alliance[i].name, NAME_LENGTH );
  16666. p->PacketLength += static_cast<decltype(p->PacketLength)>( NAME_LENGTH );
  16667. }
  16668. }
  16669. }
  16670. clif_send( p, p->PacketLength, &sd.bl, SELF );
  16671. #endif
  16672. }
  16673. /**
  16674. * Updates the online and maximum player count of a clan.
  16675. * 0988 <online count>.W <maximum member amount>.W (ZC_NOTIFY_CLAN_CONNECTINFO)
  16676. **/
  16677. void clif_clan_onlinecount( struct clan& clan ){
  16678. #if PACKETVER >= 20131223
  16679. map_session_data* sd = clan_getavailablesd( clan );
  16680. if( sd == nullptr){
  16681. return;
  16682. }
  16683. PACKET_ZC_NOTIFY_CLAN_CONNECTINFO p = {};
  16684. p.PacketType = HEADER_ZC_NOTIFY_CLAN_CONNECTINFO;
  16685. p.NumConnect = clan.connect_member;
  16686. p.NumTotal = clan.max_member;
  16687. clif_send( &p, sizeof( p ), &sd->bl, CLAN );
  16688. #endif
  16689. }
  16690. /**
  16691. * Notifies the client that the player has left his clan.
  16692. * 0989 (ZC_ACK_CLAN_LEAVE)
  16693. **/
  16694. void clif_clan_leave( map_session_data& sd ){
  16695. #if PACKETVER >= 20131223
  16696. PACKET_ZC_ACK_CLAN_LEAVE p = {};
  16697. p.PacketType = HEADER_ZC_ACK_CLAN_LEAVE;
  16698. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16699. #endif
  16700. }
  16701. /**
  16702. * Acknowledge the client about change title result (ZC_ACK_CHANGE_TITLE).
  16703. * 0A2F <result>.B <title_id>.L
  16704. */
  16705. void clif_change_title_ack(map_session_data *sd, unsigned char result, unsigned long title_id)
  16706. {
  16707. #if PACKETVER >= 20150513
  16708. int fd;
  16709. nullpo_retv(sd);
  16710. if (!clif_session_isValid(sd))
  16711. return;
  16712. fd = sd->fd;
  16713. WFIFOHEAD(fd, packet_len(0xa2f));
  16714. WFIFOW(fd, 0) = 0xa2f;
  16715. WFIFOB(fd, 2) = result;
  16716. WFIFOL(fd, 3) = title_id;
  16717. WFIFOSET(fd, packet_len(0xa2f));
  16718. #endif
  16719. }
  16720. /**
  16721. * Parsing a request from the client change title (CZ_REQ_CHANGE_TITLE).
  16722. * 0A2E <title_id>.L
  16723. */
  16724. void clif_parse_change_title(int fd, map_session_data *sd)
  16725. {
  16726. int title_id;
  16727. nullpo_retv(sd);
  16728. title_id = RFIFOL(fd, 2);
  16729. if( title_id == sd->status.title_id ){
  16730. // It is exactly the same as the old one
  16731. return;
  16732. }else if( title_id <= 0 ){
  16733. sd->status.title_id = 0;
  16734. }else{
  16735. if (std::find(sd->titles.begin(), sd->titles.end(), title_id) == sd->titles.end()) {
  16736. clif_change_title_ack(sd, 1, title_id);
  16737. return;
  16738. }
  16739. sd->status.title_id = title_id;
  16740. }
  16741. clif_name_area(&sd->bl);
  16742. clif_change_title_ack(sd, 0, title_id);
  16743. }
  16744. #ifdef DUMP_UNKNOWN_PACKET
  16745. void DumpUnknown(int fd,TBL_PC *sd,int cmd,int packet_len)
  16746. {
  16747. const char* packet_txt = "save/packet.txt";
  16748. FILE* fp;
  16749. time_t time_server;
  16750. struct tm *datetime;
  16751. char datestr[512];
  16752. time(&time_server); // get time in seconds since 1/1/1970
  16753. datetime = localtime(&time_server); // convert seconds in structure
  16754. // like sprintf, but only for date/time (Sunday, November 02 2003 15:12:52)
  16755. strftime(datestr, sizeof(datestr)-1, "%A, %B %d %Y %X.", datetime); // Server time (normal time): %A, %B %d %Y %X.
  16756. if( ( fp = fopen( packet_txt , "a" ) ) != nullptr ) {
  16757. if( sd ) {
  16758. 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);
  16759. } else {
  16760. fprintf(fp, "Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16761. }
  16762. WriteDump(fp, RFIFOP(fd,0), packet_len);
  16763. fprintf(fp, "\n");
  16764. fclose(fp);
  16765. } else {
  16766. ShowError("Failed to write '%s'.\n", packet_txt);
  16767. // Dump on console instead
  16768. if( sd ) {
  16769. 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);
  16770. } else {
  16771. ShowDebug("Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16772. }
  16773. ShowDump(RFIFOP(fd,0), packet_len);
  16774. }
  16775. }
  16776. #endif
  16777. /// Roulette System
  16778. /// Author: Yommy
  16779. void roulette_generate_bonus( map_session_data& sd ){
  16780. if( battle_config.feature_roulette_bonus_reward && sd.roulette.bonusItemID == 0 ){
  16781. int next_stage = 0;
  16782. if( sd.roulette_point.bronze > 0 ){
  16783. next_stage = 0;
  16784. }else if( sd.roulette_point.silver > 9 ){
  16785. next_stage = 2;
  16786. }else if( sd.roulette_point.gold > 9 ){
  16787. next_stage = 4;
  16788. }
  16789. // Get bonus item stage only from current stage or higher
  16790. int reward_stage = rnd_value( next_stage, MAX_ROULETTE_LEVEL - 1 );
  16791. sd.roulette.bonusItemID = rd.nameid[reward_stage][rnd()%rd.items[reward_stage]];
  16792. }else{
  16793. sd.roulette.bonusItemID = 0;
  16794. }
  16795. }
  16796. /// Opens the roulette window
  16797. /// 0A1A <result>.B <serial>.L <stage>.B <price index>.B <additional item id>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_OPEN_ROULETTE)
  16798. void clif_roulette_open( map_session_data* sd ){
  16799. nullpo_retv( sd );
  16800. roulette_generate_bonus( *sd );
  16801. struct packet_roulette_open_ack p;
  16802. p.PacketType = 0xa1a;
  16803. p.Result = 0; // result
  16804. p.Serial = 0; // serial
  16805. p.Step = (sd->roulette.claimPrize) ? sd->roulette.stage - 1 : 0;
  16806. p.Idx = (sd->roulette.claimPrize) ? sd->roulette.prizeIdx : -1;
  16807. p.AdditionItemID = sd->roulette.bonusItemID;
  16808. p.GoldPoint = sd->roulette_point.gold;
  16809. p.SilverPoint = sd->roulette_point.silver;
  16810. p.BronzePoint = sd->roulette_point.bronze;
  16811. sd->state.roulette_open = true;
  16812. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  16813. }
  16814. /// Request to open the roulette window
  16815. /// 0A19 (CZ_REQ_OPEN_ROULETTE)
  16816. void clif_parse_roulette_open( int fd, map_session_data* sd ){
  16817. nullpo_retv(sd);
  16818. if (!battle_config.feature_roulette) {
  16819. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16820. return;
  16821. }
  16822. clif_roulette_open(sd);
  16823. }
  16824. /// Sends the info about the available roulette rewards to the client
  16825. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.W <amount>.W } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO)
  16826. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.L <amount>.L } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO) >= 20180516
  16827. void clif_roulette_info( map_session_data* sd ){
  16828. nullpo_retv(sd);
  16829. struct packet_roulette_info_ack p;
  16830. p.PacketType = rouletteinfoackType;
  16831. p.PacketLength = sizeof(p);
  16832. p.RouletteSerial = 1;
  16833. for( int i = 0, count = 0; i < MAX_ROULETTE_LEVEL; i++ ){
  16834. for( int j = 0; j < MAX_ROULETTE_COLUMNS - i; j++ ){
  16835. p.ItemInfo[count].Row = i;
  16836. p.ItemInfo[count].Position = j;
  16837. p.ItemInfo[count].ItemId = client_nameid( rd.nameid[i][j] );
  16838. p.ItemInfo[count].Count = rd.qty[i][j];
  16839. #if PACKETVER >= 20180511
  16840. p.ItemInfo[count].unused = 0;
  16841. #endif
  16842. count++;
  16843. }
  16844. }
  16845. clif_send( &p, sizeof(p), &sd->bl, SELF );
  16846. }
  16847. /// Request the roulette reward data
  16848. /// 0A1B (CZ_REQ_ROULETTE_INFO)
  16849. void clif_parse_roulette_info( int fd, map_session_data* sd ){
  16850. nullpo_retv(sd);
  16851. if (!battle_config.feature_roulette) {
  16852. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16853. return;
  16854. }
  16855. clif_roulette_info(sd);
  16856. }
  16857. /// Notification of the client that the roulette window was closed
  16858. /// 0A1D (CZ_REQ_CLOSE_ROULETTE)
  16859. void clif_parse_roulette_close( int fd, map_session_data* sd ){
  16860. nullpo_retv(sd);
  16861. if (!battle_config.feature_roulette) {
  16862. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16863. return;
  16864. }
  16865. sd->state.roulette_open = false;
  16866. }
  16867. /// Response to a item reward request
  16868. /// 0A22 <type>.B <bonus item>.W (ZC_RECV_ROULETTE_ITEM)
  16869. static void clif_roulette_recvitem_ack(map_session_data *sd, enum RECV_ROULETTE_ITEM_REQ type) {
  16870. #if PACKETVER >= 20141016
  16871. nullpo_retv(sd);
  16872. struct packet_roulette_itemrecv_ack p;
  16873. p.PacketType = roulettercvitemackType;
  16874. p.Result = type;
  16875. p.AdditionItemID = sd->roulette.bonusItemID;
  16876. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  16877. #endif
  16878. }
  16879. /**
  16880. * Claim roulette reward
  16881. * @param sd Player
  16882. * @return 0:Success
  16883. **/
  16884. static uint8 clif_roulette_getitem(map_session_data *sd) {
  16885. struct item it;
  16886. uint8 res = 1;
  16887. unsigned short factor = 1;
  16888. nullpo_retr(1, sd);
  16889. if (sd->roulette.prizeIdx < 0)
  16890. return RECV_ITEM_FAILED;
  16891. memset(&it, 0, sizeof(it));
  16892. it.nameid = rd.nameid[sd->roulette.prizeStage][sd->roulette.prizeIdx];
  16893. it.identify = 1;
  16894. if( sd->roulette.bonusItemID != 0 ){
  16895. if( sd->roulette.bonusItemID == it.nameid && battle_config.feature_roulette_bonus_reward && !( rd.flag[sd->roulette.prizeStage][sd->roulette.prizeIdx]&1 ) ){
  16896. factor = 2;
  16897. // Reset the Bonus Item to trigger new calculation
  16898. sd->roulette.bonusItemID = 0;
  16899. }else{
  16900. for( int i = 0; i < MAX_ROULETTE_LEVEL && sd->roulette.bonusItemID != 0; i++ ){
  16901. for( int j = 0; j < MAX_ROULETTE_COLUMNS - i && sd->roulette.bonusItemID != 0; j++ ){
  16902. if( rd.nameid[i][j] == sd->roulette.bonusItemID ){
  16903. // 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
  16904. if( sd->roulette.prizeStage >= i ){
  16905. // Reset the Bonus Item to trigger new calculation and to cancel the loops
  16906. sd->roulette.bonusItemID = 0;
  16907. }
  16908. }
  16909. }
  16910. }
  16911. }
  16912. }
  16913. if ((res = pc_additem(sd, &it, rd.qty[sd->roulette.prizeStage][sd->roulette.prizeIdx] * factor, LOG_TYPE_ROULETTE)) == 0) {
  16914. ; // onSuccess
  16915. }
  16916. if( sd->roulette.bonusItemID == 0 ){
  16917. roulette_generate_bonus( *sd );
  16918. }
  16919. sd->roulette.claimPrize = false;
  16920. sd->roulette.prizeStage = 0;
  16921. sd->roulette.prizeIdx = -1;
  16922. sd->roulette.stage = 0;
  16923. return res;
  16924. }
  16925. /// Update Roulette window with current stats
  16926. /// 0A20 <result>.B <stage>.W <price index>.W <bonus item>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_GENERATE_ROULETTE)
  16927. void clif_roulette_generate( map_session_data *sd, unsigned char result, short stage, short prizeIdx, t_itemid bonusItemID ){
  16928. nullpo_retv( sd );
  16929. struct packet_roulette_generate_ack p;
  16930. p.PacketType = roulettgenerateackType;
  16931. p.Result = result;
  16932. p.Step = stage;
  16933. p.Idx = prizeIdx;
  16934. p.AdditionItemID = battle_config.feature_roulette_bonus_reward ? bonusItemID : 0;
  16935. p.RemainGold = sd->roulette_point.gold;
  16936. p.RemainSilver = sd->roulette_point.silver;
  16937. p.RemainBronze = sd->roulette_point.bronze;
  16938. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  16939. }
  16940. /// Request to start the roulette
  16941. /// 0A1F (CZ_REQ_GENERATE_ROULETTE)
  16942. void clif_parse_roulette_generate( int fd, map_session_data* sd ){
  16943. enum GENERATE_ROULETTE_ACK result;
  16944. nullpo_retv(sd);
  16945. if (!battle_config.feature_roulette) {
  16946. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16947. return;
  16948. }
  16949. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  16950. return;
  16951. }
  16952. // Player has not claimed his prize yet
  16953. if( sd->roulette.claimPrize ){
  16954. clif_roulette_getitem( sd );
  16955. }
  16956. if (sd->roulette.stage >= MAX_ROULETTE_LEVEL){
  16957. // Make sure everything is reset
  16958. sd->roulette.stage = 0;
  16959. sd->roulette.claimPrize = false;
  16960. sd->roulette.prizeStage = 0;
  16961. sd->roulette.prizeIdx = -1;
  16962. }
  16963. if( !sd->roulette.stage && sd->roulette_point.bronze <= 0 && sd->roulette_point.silver < 10 && sd->roulette_point.gold < 10 ){
  16964. result = GENERATE_ROULETTE_NO_ENOUGH_POINT;
  16965. }else{
  16966. if (!sd->roulette.stage) {
  16967. if (sd->roulette_point.bronze > 0) {
  16968. sd->roulette_point.bronze -= 1;
  16969. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  16970. } else if (sd->roulette_point.silver > 9) {
  16971. sd->roulette_point.silver -= 10;
  16972. sd->roulette.stage = 2;
  16973. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  16974. } else if (sd->roulette_point.gold > 9) {
  16975. sd->roulette_point.gold -= 10;
  16976. sd->roulette.stage = 4;
  16977. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  16978. }
  16979. }
  16980. sd->roulette.prizeStage = sd->roulette.stage;
  16981. sd->roulette.prizeIdx = rnd()%rd.items[sd->roulette.stage];
  16982. sd->roulette.claimPrize = true;
  16983. sd->roulette.tick = gettick() + i64max( 1, ( MAX_ROULETTE_COLUMNS - sd->roulette.prizeStage - 3 ) ) * 1000;
  16984. if( rd.flag[sd->roulette.stage][sd->roulette.prizeIdx]&1 ){
  16985. result = GENERATE_ROULETTE_LOSING;
  16986. sd->roulette.stage = 0;
  16987. }else{
  16988. result = GENERATE_ROULETTE_SUCCESS;
  16989. sd->roulette.stage++;
  16990. }
  16991. }
  16992. clif_roulette_generate(sd,result,sd->roulette.prizeStage,(sd->roulette.prizeIdx == -1 ? 0 : sd->roulette.prizeIdx), sd->roulette.bonusItemID);
  16993. }
  16994. /// Request to claim a prize
  16995. /// 0A21 (CZ_RECV_ROULETTE_ITEM)
  16996. void clif_parse_roulette_item( int fd, map_session_data* sd ){
  16997. enum RECV_ROULETTE_ITEM_REQ type = RECV_ITEM_FAILED;
  16998. nullpo_retv(sd);
  16999. if (!battle_config.feature_roulette) {
  17000. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17001. return;
  17002. }
  17003. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  17004. return;
  17005. }
  17006. if (sd->roulette.claimPrize && sd->roulette.prizeIdx != -1) {
  17007. switch (clif_roulette_getitem(sd)) {
  17008. case ADDITEM_SUCCESS:
  17009. type = RECV_ITEM_SUCCESS;
  17010. break;
  17011. case ADDITEM_INVALID:
  17012. case ADDITEM_OVERITEM:
  17013. case ADDITEM_OVERAMOUNT:
  17014. type = RECV_ITEM_OVERCOUNT;
  17015. break;
  17016. case ADDITEM_OVERWEIGHT:
  17017. type = RECV_ITEM_OVERWEIGHT;
  17018. break;
  17019. case ADDITEM_STACKLIMIT:
  17020. default:
  17021. type = RECV_ITEM_FAILED;
  17022. break;
  17023. }
  17024. }
  17025. clif_roulette_recvitem_ack(sd,type);
  17026. }
  17027. /// MERGE ITEM
  17028. /**
  17029. * Acknowledge the client about item merger result
  17030. * ZC 096F <index>.W <total>.W <result>.B (ZC_ACK_MERGE_ITEM)
  17031. * @param fd
  17032. * @param sd
  17033. **/
  17034. void clif_merge_item_ack( map_session_data &sd, enum MERGE_ITEM_ACK type, uint16 index = 0, uint16 count = 0 ){
  17035. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17036. PACKET_ZC_ACK_MERGE_ITEM p = {};
  17037. p.packetType = HEADER_ZC_ACK_MERGE_ITEM;
  17038. if( type == MERGE_ITEM_SUCCESS ){
  17039. p.index = client_index( index );
  17040. }else{
  17041. p.index = index;
  17042. }
  17043. p.amount = count;
  17044. p.reason = type;
  17045. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17046. #endif
  17047. }
  17048. /**
  17049. * Check if item has the pair to be merged
  17050. * @param sd
  17051. * @param it Item
  17052. * @return True if has pair, False if not
  17053. **/
  17054. static bool clif_merge_item_has_pair(map_session_data *sd, struct item *it) {
  17055. struct item *it_;
  17056. unsigned short i;
  17057. nullpo_retr(false, sd);
  17058. 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);
  17059. if (i < MAX_INVENTORY)
  17060. return true;
  17061. return false;
  17062. }
  17063. /**
  17064. * Check if item can be merged
  17065. * @param id Item Data
  17066. * @param it Item
  17067. * @return True if can be merged, False if not
  17068. **/
  17069. static bool clif_merge_item_check(struct item_data *id, struct item *it) {
  17070. if (!id || !it)
  17071. return false;
  17072. if (id->type == IT_CASH)
  17073. return false;
  17074. if (!itemdb_isstackable2(id))
  17075. return false;
  17076. if (itemdb_isspecial(it->card[0]))
  17077. return false;
  17078. if (!it->unique_id)
  17079. return false;
  17080. return true;
  17081. }
  17082. /**
  17083. * Open available item to be merged.
  17084. * Only show 1 item in 1 process.
  17085. * ZC 096D <size>.W { <index>.W }* (ZC_MERGE_ITEM_OPEN)
  17086. * @param sd
  17087. **/
  17088. void clif_merge_item_open( map_session_data& sd ){
  17089. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17090. PACKET_ZC_MERGE_ITEM_OPEN* p = reinterpret_cast<PACKET_ZC_MERGE_ITEM_OPEN*>( packet_buffer );
  17091. p->packetType = HEADER_ZC_MERGE_ITEM_OPEN;
  17092. p->packetLen = sizeof( *p );
  17093. int n = 0;
  17094. // Get entries
  17095. for( int i = 0; i < MAX_INVENTORY; i++ ){
  17096. struct item* it = &sd.inventory.u.items_inventory[i];
  17097. if( !clif_merge_item_check( sd.inventory_data[i], it ) ){
  17098. continue;
  17099. }
  17100. if( clif_merge_item_has_pair( &sd, it ) ){
  17101. p->items[n++].index = client_index( i );
  17102. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( p->items[0] ) );
  17103. }
  17104. }
  17105. // No item need to be merged
  17106. if( n < 2 ){
  17107. clif_msg( &sd, MSI_NOT_EXIST_MERGE_ITEM );
  17108. return;
  17109. }
  17110. clif_send( p, p->packetLen, &sd.bl, SELF );
  17111. #endif
  17112. }
  17113. /**
  17114. * Process item merge request.
  17115. * 096E <size>.W { <index>.W }* (CZ_REQ_MERGE_ITEM)
  17116. * @param fd
  17117. * @param sd
  17118. **/
  17119. void clif_parse_merge_item_req( int fd, map_session_data* sd ){
  17120. const PACKET_CZ_REQ_MERGE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_MERGE_ITEM*>( RFIFOP( fd, 0 ) );
  17121. int count = ( p->packetLength - sizeof( *p ) ) / sizeof( p->indices[0] );
  17122. // No item need to be merged
  17123. if( count < 2 ){
  17124. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17125. return;
  17126. }
  17127. uint16 idx_main = server_index( p->indices[0] );
  17128. if( idx_main >= MAX_INVENTORY ){
  17129. return;
  17130. }
  17131. if( !clif_merge_item_check( sd->inventory_data[idx_main], &sd->inventory.u.items_inventory[idx_main] ) ){
  17132. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17133. return;
  17134. }
  17135. // Ensure each index only comes once
  17136. std::unordered_set<uint16> indices;
  17137. for( int i = 1; i < count; i++ ){
  17138. uint16 idx = server_index( p->indices[i] );
  17139. if( idx >= MAX_INVENTORY ){
  17140. return;
  17141. }
  17142. if( sd->inventory_data[idx] == nullptr ){
  17143. return;
  17144. }
  17145. // Check if it is the same item
  17146. if( sd->inventory_data[idx]->nameid != sd->inventory_data[idx_main]->nameid ){
  17147. return;
  17148. }
  17149. if( !clif_merge_item_check( sd->inventory_data[idx], &sd->inventory.u.items_inventory[idx] ) ){
  17150. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17151. return;
  17152. }
  17153. indices.insert( idx );
  17154. }
  17155. if( indices.empty() ){
  17156. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17157. return;
  17158. }
  17159. uint32 total_amount = sd->inventory.u.items_inventory[idx_main].amount;
  17160. for( uint16 idx : indices ){
  17161. total_amount += sd->inventory.u.items_inventory[idx].amount;
  17162. }
  17163. uint16 stack = sd->inventory_data[idx_main]->stack.amount;
  17164. if( stack == 0 ){
  17165. stack = MAX_AMOUNT;
  17166. }
  17167. if( total_amount >= stack ){
  17168. clif_merge_item_ack( *sd, MERGE_ITEM_FAILED_MAX_COUNT );
  17169. return;
  17170. }
  17171. // Merrrrge!!!!
  17172. for( uint16 idx : indices ){
  17173. uint16 amount = sd->inventory.u.items_inventory[idx].amount;
  17174. log_pick_pc( sd, LOG_TYPE_MERGE_ITEM, -amount, &sd->inventory.u.items_inventory[idx] );
  17175. memset( &sd->inventory.u.items_inventory[idx], 0, sizeof( sd->inventory.u.items_inventory[0] ) );
  17176. sd->inventory_data[idx] = nullptr;
  17177. clif_delitem( *sd, idx, amount, 0 );
  17178. }
  17179. sd->inventory.u.items_inventory[idx_main].amount = total_amount;
  17180. clif_merge_item_ack( *sd, MERGE_ITEM_SUCCESS, idx_main, total_amount );
  17181. }
  17182. /**
  17183. * Cancel item merge
  17184. * CZ 0974 (CZ_CANCEL_MERGE_ITEM)
  17185. * @param fd
  17186. * @param sd
  17187. **/
  17188. void clif_parse_merge_item_cancel(int fd, map_session_data* sd) {
  17189. return; // Nothing todo yet
  17190. }
  17191. static std::string clif_hide_name(const char* original_name)
  17192. {
  17193. std::string censored(original_name);
  17194. size_t hide = zmin(battle_config.broadcast_hide_name, censored.length() - 1);
  17195. censored.replace(censored.length() - hide, hide, hide, '*');
  17196. return censored;
  17197. }
  17198. /**
  17199. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <containerid>.W (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17200. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <srcname>.24B (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17201. * type: ITEMOBTAIN_TYPE_BOXITEM & ITEMOBTAIN_TYPE_MONSTER_ITEM "[playername] ... [sourcename] ... [itemname]" -> MsgStringTable[1629]
  17202. * type: ITEMOBTAIN_TYPE_NPC "[playername] ... [itemname]" -> MsgStringTable[1870]
  17203. **/
  17204. void clif_broadcast_obtain_special_item( const char *char_name, t_itemid nameid, t_itemid container, enum BROADCASTING_SPECIAL_ITEM_OBTAIN type ){
  17205. char name[NAME_LENGTH];
  17206. if( battle_config.broadcast_hide_name ){
  17207. std::string dispname = clif_hide_name(char_name);
  17208. safestrncpy(name, dispname.c_str(), sizeof(name));
  17209. }else{
  17210. safestrncpy( name, char_name, sizeof( name ) );
  17211. }
  17212. switch( type ){
  17213. case ITEMOBTAIN_TYPE_BOXITEM:
  17214. #if PACKETVER >= 20091201
  17215. {
  17216. PACKET_ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN_item p = {};
  17217. p.PacketType = package_item_announceType;
  17218. p.PacketLength = sizeof( p );
  17219. p.type = type;
  17220. p.ItemID = client_nameid( nameid );
  17221. p.len = NAME_LENGTH;
  17222. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17223. p.boxItemID_len = (int8)sizeof( p.BoxItemID );
  17224. p.BoxItemID = client_nameid( container );
  17225. #if PACKETVER_MAIN_NUM >= 20220518 || PACKETVER_ZERO_NUM >= 20220518
  17226. p.refineLevel_len = (int8)sizeof( p.refineLevel );
  17227. p.refineLevel = 0; // TODO: implement
  17228. #endif
  17229. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17230. }
  17231. #endif
  17232. break;
  17233. case ITEMOBTAIN_TYPE_MONSTER_ITEM:
  17234. case ITEMOBTAIN_TYPE_NPC:
  17235. {
  17236. struct packet_item_drop_announce p;
  17237. p.PacketType = item_drop_announceType;
  17238. p.type = type;
  17239. p.ItemID = client_nameid( nameid );
  17240. p.len = NAME_LENGTH;
  17241. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17242. if( type == ITEMOBTAIN_TYPE_MONSTER_ITEM ){
  17243. std::shared_ptr<s_mob_db> db = mob_db.find( container );
  17244. p.monsterNameLen = NAME_LENGTH;
  17245. safestrncpy( p.monsterName, db->name.c_str(), NAME_LENGTH );
  17246. }else{
  17247. p.monsterNameLen = 0;
  17248. p.monsterName[0] = '\0';
  17249. }
  17250. #if PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  17251. p.PacketLength = 12 + p.len + p.monsterNameLen;
  17252. #else
  17253. p.PacketLength = 8 + p.len + p.monsterNameLen;
  17254. #endif
  17255. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17256. }
  17257. break;
  17258. }
  17259. }
  17260. /// Show body view windows (ZC_DRESSROOM_OPEN).
  17261. /// 0A02 <view>.W
  17262. /// Value <flag> has the following effects:
  17263. /// 1: Open a Dress Room window.
  17264. void clif_dressing_room(map_session_data *sd, int flag) {
  17265. #if PACKETVER >= 20150513
  17266. int fd = sd->fd;
  17267. nullpo_retv(sd);
  17268. WFIFOHEAD(fd, packet_len(0xa02));
  17269. WFIFOW(fd,0) = 0xa02;
  17270. WFIFOW(fd,2) = flag;
  17271. WFIFOSET(fd, packet_len(0xa02));
  17272. #endif
  17273. }
  17274. /// Parsing a request from the client item identify oneclick (CZ_REQ_ONECLICK_ITEMIDENTIFY).
  17275. /// 0A35 <result>.W
  17276. void clif_parse_Oneclick_Itemidentify(int fd, map_session_data *sd) {
  17277. #if PACKETVER >= 20150513
  17278. short idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2, magnifier_idx;
  17279. // Ignore the request
  17280. // - Invalid item index
  17281. // - Invalid item ID or item doesn't exist
  17282. // - Item is already identified
  17283. if (idx < 0 || idx >= MAX_INVENTORY ||
  17284. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  17285. sd->inventory.u.items_inventory[idx].identify)
  17286. return;
  17287. // Ignore the request - No magnifiers in inventory
  17288. if ((magnifier_idx = pc_search_inventory(sd, ITEMID_MAGNIFIER)) == -1 &&
  17289. (magnifier_idx = pc_search_inventory(sd, ITEMID_NOVICE_MAGNIFIER)) == -1)
  17290. return;
  17291. if (pc_delitem(sd, magnifier_idx, 1, 0, 0, LOG_TYPE_OTHER) != 0) // Deleting of magnifier failed, for whatever reason...
  17292. return;
  17293. skill_identify(sd, idx);
  17294. #endif
  17295. }
  17296. /// Starts navigation to the given target on client side
  17297. void clif_navigateTo(map_session_data *sd, const char* mapname, uint16 x, uint16 y, uint8 flag, bool hideWindow, uint16 mob_id ){
  17298. #if PACKETVER >= 20111010
  17299. int fd = sd->fd;
  17300. WFIFOHEAD(fd,27);
  17301. WFIFOW(fd,0) = 0x08e2;
  17302. // How detailed will our navigation be?
  17303. if( mob_id > 0 ){
  17304. x = 0;
  17305. y = 0;
  17306. WFIFOB(fd,2) = 3; // monster with destination field
  17307. }else if( x > 0 && y > 0 ){
  17308. WFIFOB(fd,2) = 0; // with coordinates
  17309. }else{
  17310. x = 0;
  17311. y = 0;
  17312. WFIFOB(fd,2) = 1; // without coordinates(will fail if you are already on the map)
  17313. }
  17314. // Which services can be used for transportation?
  17315. WFIFOB(fd,3) = flag;
  17316. // If this flag is set, the navigation window will not be opened up
  17317. WFIFOB(fd,4) = hideWindow;
  17318. // Target map
  17319. safestrncpy( WFIFOCP(fd,5),mapname,MAP_NAME_LENGTH_EXT);
  17320. // Target x
  17321. WFIFOW(fd,21) = x;
  17322. // Target y
  17323. WFIFOW(fd,23) = y;
  17324. // Target monster ID
  17325. WFIFOW(fd,25) = mob_id;
  17326. WFIFOSET(fd,27);
  17327. #endif
  17328. }
  17329. /// Send hat effects to the client.
  17330. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17331. void clif_hat_effects( map_session_data& sd, block_list& bl, enum send_target target ){
  17332. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17333. map_session_data *tsd;
  17334. block_list* tbl;
  17335. if( target == SELF ){
  17336. tsd = BL_CAST(BL_PC,&bl);
  17337. tbl = &sd.bl;
  17338. }else{
  17339. tsd = &sd;
  17340. tbl = &bl;
  17341. }
  17342. if( tsd == nullptr ){
  17343. return;
  17344. }
  17345. if( tsd->hatEffects.empty() || map_getmapdata(tbl->m)->getMapFlag(MF_NOCOSTUME) ){
  17346. return;
  17347. }
  17348. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17349. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17350. p->packetLength = sizeof( *p );
  17351. p->aid = tsd->bl.id;
  17352. p->status = 1;
  17353. for( size_t i = 0; i < tsd->hatEffects.size(); i++ ){
  17354. p->effects[i] = tsd->hatEffects[i];
  17355. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17356. }
  17357. clif_send( p, p->packetLength, tbl, target );
  17358. #endif
  17359. }
  17360. /// Send a single hat effect to the client.
  17361. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17362. void clif_hat_effect_single( map_session_data& sd, uint16 effectId, bool enable ){
  17363. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17364. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17365. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17366. p->packetLength = sizeof( *p );
  17367. p->aid = sd.bl.id;
  17368. p->status = enable;
  17369. p->effects[0] = effectId;
  17370. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17371. clif_send( p, p->packetLength, &sd.bl, AREA );
  17372. #endif
  17373. }
  17374. /// Notify the client that a sale has started
  17375. /// 09b2 <item id>.W <remaining time>.L (ZC_NOTIFY_BARGAIN_SALE_SELLING)
  17376. void clif_sale_start( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17377. #if PACKETVER_SUPPORTS_SALES
  17378. PACKET_ZC_NOTIFY_BARGAIN_SALE_SELLING p = {};
  17379. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_SELLING;
  17380. p.itemId = client_nameid( sale_item->nameid );
  17381. p.remainingTime = (uint32)(sale_item->end - time(nullptr)); // time in S
  17382. clif_send( &p, sizeof( p ), bl, target );
  17383. #endif
  17384. }
  17385. /// Notify the client that a sale has ended
  17386. /// 09b3 <item id>.W (ZC_NOTIFY_BARGAIN_SALE_CLOSE)
  17387. void clif_sale_end( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17388. #if PACKETVER_SUPPORTS_SALES
  17389. PACKET_ZC_NOTIFY_BARGAIN_SALE_CLOSE p = {};
  17390. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_CLOSE;
  17391. p.itemId = client_nameid( sale_item->nameid );
  17392. clif_send( &p, sizeof( p ), bl, target );
  17393. #endif
  17394. }
  17395. /// Update the remaining amount of a sale item.
  17396. /// 09c4 <item id>.W <amount>.L (ZC_ACK_COUNT_BARGAIN_SALE_ITEM)
  17397. void clif_sale_amount( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17398. #if PACKETVER_SUPPORTS_SALES
  17399. PACKET_ZC_ACK_COUNT_BARGAIN_SALE_ITEM p = {};
  17400. p.packetType = HEADER_ZC_ACK_COUNT_BARGAIN_SALE_ITEM;
  17401. p.itemId = client_nameid( sale_item->nameid );
  17402. p.amount = sale_item->amount;
  17403. clif_send( &p, sizeof( p ), bl, target );
  17404. #endif
  17405. }
  17406. /// The client requested a refresh of the current remaining count of a sale item
  17407. /// 09ac <account id>.L <item id>.W (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17408. void clif_parse_sale_refresh( int fd, map_session_data* sd ){
  17409. #if PACKETVER_SUPPORTS_SALES
  17410. const PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO* p = reinterpret_cast<PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO*>( RFIFOP( fd, 0 ) );
  17411. struct sale_item_data* sale;
  17412. if( p->AID != sd->status.account_id ){
  17413. return;
  17414. }
  17415. sale = sale_find_item( p->itemId, true );
  17416. if( sale == nullptr ){
  17417. return;
  17418. }
  17419. clif_sale_amount(sale, &sd->bl, SELF);
  17420. #endif
  17421. }
  17422. /// Opens the sale administration window on the client
  17423. /// 09b5 (ZC_OPEN_BARGAIN_SALE_TOOL)
  17424. void clif_sale_open( map_session_data* sd ){
  17425. #if PACKETVER_SUPPORTS_SALES
  17426. nullpo_retv(sd);
  17427. if( sd->state.sale_open ){
  17428. return;
  17429. }
  17430. sd->state.sale_open = true;
  17431. int fd = sd->fd;
  17432. WFIFOHEAD(fd, 2);
  17433. WFIFOW(fd, 0) = 0x9b5;
  17434. WFIFOSET(fd, 2);
  17435. #endif
  17436. }
  17437. /// Client request to open the sale administration window.
  17438. /// This is sent by /limitedsale
  17439. /// 09b4 <account id>.L (CZ_OPEN_BARGAIN_SALE_TOOL)
  17440. void clif_parse_sale_open( int fd, map_session_data* sd ){
  17441. #if PACKETVER_SUPPORTS_SALES
  17442. nullpo_retv(sd);
  17443. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17444. return;
  17445. }
  17446. char command[CHAT_SIZE_MAX];
  17447. safesnprintf( command, sizeof(command), "%climitedsale", atcommand_symbol );
  17448. is_atcommand(fd, sd, command, 1);
  17449. #endif
  17450. }
  17451. /// Closes the sale administration window on the client.
  17452. /// 09bd (ZC_CLOSE_BARGAIN_SALE_TOOL)
  17453. void clif_sale_close(map_session_data* sd) {
  17454. #if PACKETVER_SUPPORTS_SALES
  17455. nullpo_retv(sd);
  17456. if( !sd->state.sale_open ){
  17457. return;
  17458. }
  17459. sd->state.sale_open = false;
  17460. int fd = sd->fd;
  17461. WFIFOHEAD(fd, 2);
  17462. WFIFOW(fd, 0) = 0x9bd;
  17463. WFIFOSET(fd, 2);
  17464. #endif
  17465. }
  17466. /// Client request to close the sale administration window.
  17467. /// 09bc (CZ_CLOSE_BARGAIN_SALE_TOOL)
  17468. void clif_parse_sale_close(int fd, map_session_data* sd) {
  17469. #if PACKETVER_SUPPORTS_SALES
  17470. nullpo_retv(sd);
  17471. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17472. return;
  17473. }
  17474. clif_sale_close(sd);
  17475. #endif
  17476. }
  17477. /// Reply to a item search request for item sale administration.
  17478. /// 09ad <result>.W <item id>.W <price>.L (ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO)
  17479. void clif_sale_search_reply( map_session_data* sd, std::shared_ptr<s_cash_item> item ){
  17480. #if PACKETVER_SUPPORTS_SALES
  17481. PACKET_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO p = {};
  17482. p.packetType = HEADER_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO;
  17483. if( item != nullptr ){
  17484. p.result = 0;
  17485. p.itemId = client_nameid( item->nameid );
  17486. p.price = item->price;
  17487. }else{
  17488. p.result = 1;
  17489. p.itemId = 0;
  17490. p.price = 0;
  17491. }
  17492. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17493. #endif
  17494. }
  17495. /// Search request for an item sale administration.
  17496. /// 09ac <length>.W <account id>.L <item name>.?B (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17497. void clif_parse_sale_search( int fd, map_session_data* sd ){
  17498. #if PACKETVER_SUPPORTS_SALES
  17499. char item_name[ITEM_NAME_LENGTH];
  17500. nullpo_retv(sd);
  17501. if( RFIFOL(fd, 4) != sd->status.account_id ){
  17502. return;
  17503. }
  17504. if( !sd->state.sale_open ){
  17505. return;
  17506. }
  17507. safestrncpy( item_name, RFIFOCP(fd, 8), min(RFIFOW(fd, 2) - 7, ITEM_NAME_LENGTH) );
  17508. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  17509. // not found
  17510. if( id == nullptr ){
  17511. clif_sale_search_reply( sd, nullptr );
  17512. return;
  17513. }
  17514. clif_sale_search_reply( sd, cash_shop_db.findItemInTab( CASHSHOP_TAB_SALE, id->nameid ) );
  17515. #endif
  17516. }
  17517. /// Reply if an item was successfully put on sale or not.
  17518. /// 09af <result>.W (ZC_ACK_APPLY_BARGAIN_SALE_ITEM)
  17519. void clif_sale_add_reply( map_session_data* sd, enum e_sale_add_result result ){
  17520. #if PACKETVER_SUPPORTS_SALES
  17521. int fd = sd->fd;
  17522. WFIFOHEAD(fd, 4);
  17523. WFIFOW(fd, 0) = 0x9af;
  17524. WFIFOW(fd, 2) = (uint16)result;
  17525. WFIFOSET(fd, 4);
  17526. #endif
  17527. }
  17528. /// A client request to put an item on sale.
  17529. /// 09ae <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.B (CZ_REQ_APPLY_BARGAIN_SALE_ITEM)
  17530. /// 0a3d <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.W (CZ_REQ_APPLY_BARGAIN_SALE_ITEM2)
  17531. void clif_parse_sale_add( int fd, map_session_data* sd ){
  17532. #if PACKETVER_SUPPORTS_SALES
  17533. nullpo_retv( sd );
  17534. const PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17535. if( p->AID != sd->status.account_id ){
  17536. return;
  17537. }
  17538. if( !sd->state.sale_open ){
  17539. return;
  17540. }
  17541. time_t endTime = p->startTime + p->hours * 60 * 60;
  17542. clif_sale_add_reply( sd, sale_add_item( p->itemId, p->amount, p->startTime, endTime ) );
  17543. #endif
  17544. }
  17545. /// Reply to an item removal from sale.
  17546. /// 09b1 <result>.W (ZC_ACK_REMOVE_BARGAIN_SALE_ITEM)
  17547. void clif_sale_remove_reply( map_session_data* sd, bool failed ){
  17548. #if PACKETVER_SUPPORTS_SALES
  17549. int fd = sd->fd;
  17550. WFIFOHEAD(fd, 4);
  17551. WFIFOW(fd, 0) = 0x9b1;
  17552. WFIFOW(fd, 2) = failed;
  17553. WFIFOSET(fd, 4);
  17554. #endif
  17555. }
  17556. /// Request to remove an item from sale.
  17557. /// 09b0 <account id>.L <item id>.W (CZ_REQ_REMOVE_BARGAIN_SALE_ITEM)
  17558. void clif_parse_sale_remove( int fd, map_session_data* sd ){
  17559. #if PACKETVER_SUPPORTS_SALES
  17560. nullpo_retv( sd );
  17561. const PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17562. if( p->AID != sd->status.account_id ){
  17563. return;
  17564. }
  17565. if( !sd->state.sale_open ){
  17566. return;
  17567. }
  17568. clif_sale_remove_reply( sd, !sale_remove_item( p->itemId ) );
  17569. #endif
  17570. }
  17571. /// Achievement System
  17572. /// Author: Luxuri, Aleos
  17573. /**
  17574. * Sends all achievement data to the client (ZC_ALL_ACH_LIST).
  17575. * 0a23 <packetLength>.W <ACHCount>.L <ACHPoint>.L
  17576. */
  17577. void clif_achievement_list_all(map_session_data *sd)
  17578. {
  17579. nullpo_retv(sd);
  17580. if (!battle_config.feature_achievement) {
  17581. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17582. return;
  17583. }
  17584. int count = sd->achievement_data.count; // All achievements should be sent to the client
  17585. if (count == 0)
  17586. return;
  17587. int len = (50 * count) + 22;
  17588. int fd = sd->fd;
  17589. int *info = achievement_level(sd, true);
  17590. WFIFOHEAD(fd,len);
  17591. WFIFOW(fd, 0) = 0xa23;
  17592. WFIFOW(fd, 2) = len;
  17593. WFIFOL(fd, 4) = count; // Amount of achievements the player has in their list (started/completed)
  17594. WFIFOL(fd, 8) = sd->achievement_data.total_score; // Top number
  17595. WFIFOW(fd, 12) = sd->achievement_data.level; // Achievement Level (gold circle)
  17596. WFIFOL(fd, 14) = info[0]; // Achievement EXP (left number in bar)
  17597. WFIFOL(fd, 18) = info[1]; // Achievement EXP TNL (right number in bar)
  17598. for (int i = 0; i < count; i++) {
  17599. WFIFOL(fd, i * 50 + 22) = (uint32)sd->achievement_data.achievements[i].achievement_id;
  17600. WFIFOB(fd, i * 50 + 26) = (uint32)sd->achievement_data.achievements[i].completed > 0;
  17601. for (int j = 0; j < MAX_ACHIEVEMENT_OBJECTIVES; j++)
  17602. WFIFOL(fd, (i * 50) + 27 + (j * 4)) = (uint32)sd->achievement_data.achievements[i].count[j];
  17603. WFIFOL(fd, i * 50 + 67) = (uint32)sd->achievement_data.achievements[i].completed;
  17604. WFIFOB(fd, i * 50 + 71) = sd->achievement_data.achievements[i].rewarded > 0;
  17605. }
  17606. WFIFOSET(fd, len);
  17607. }
  17608. /**
  17609. * Sends a single achievement's data to the client (ZC_ACH_UPDATE).
  17610. * 0a24 <ACHPoint>.L
  17611. */
  17612. void clif_achievement_update(map_session_data *sd, struct achievement *ach, int count)
  17613. {
  17614. nullpo_retv(sd);
  17615. if (!battle_config.feature_achievement) {
  17616. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17617. return;
  17618. }
  17619. int fd = sd->fd;
  17620. int *info = achievement_level(sd, true);
  17621. WFIFOHEAD(fd, packet_len(0xa24));
  17622. WFIFOW(fd, 0) = 0xa24;
  17623. WFIFOL(fd, 2) = sd->achievement_data.total_score; // Total Achievement Points (top of screen)
  17624. WFIFOW(fd, 6) = sd->achievement_data.level; // Achievement Level (gold circle)
  17625. WFIFOL(fd, 8) = info[0]; // Achievement EXP (left number in bar)
  17626. WFIFOL(fd, 12) = info[1]; // Achievement EXP TNL (right number in bar)
  17627. if (ach) {
  17628. WFIFOL(fd, 16) = ach->achievement_id; // Achievement ID
  17629. WFIFOB(fd, 20) = ach->completed > 0; // Is it complete?
  17630. for (int i = 0; i < MAX_ACHIEVEMENT_OBJECTIVES; i++)
  17631. WFIFOL(fd, 21 + (i * 4)) = (uint32)ach->count[i]; // 1~10 pre-reqs
  17632. WFIFOL(fd, 61) = (uint32)ach->completed; // Epoch time
  17633. WFIFOB(fd, 65) = ach->rewarded > 0; // Got reward?
  17634. } else
  17635. memset(WFIFOP(fd, 16), 0, 40);
  17636. WFIFOSET(fd, packet_len(0xa24));
  17637. }
  17638. /**
  17639. * Checks if an achievement reward can be rewarded (CZ_REQ_ACH_REWARD).
  17640. * 0a25 <achievementID>.L
  17641. */
  17642. void clif_parse_AchievementCheckReward(int fd, map_session_data *sd)
  17643. {
  17644. nullpo_retv(sd);
  17645. if( sd->achievement_data.save )
  17646. intif_achievement_save(sd);
  17647. achievement_check_reward(sd, RFIFOL(fd,2));
  17648. }
  17649. /**
  17650. * Returns the result of achievement_check_reward (ZC_REQ_ACH_REWARD_ACK).
  17651. * 0a26 <result>.W <achievementID>.L
  17652. */
  17653. void clif_achievement_reward_ack(int fd, unsigned char result, int achievement_id)
  17654. {
  17655. WFIFOHEAD(fd, packet_len(0xa26));
  17656. WFIFOW(fd, 0) = 0xa26;
  17657. WFIFOB(fd, 2) = result;
  17658. WFIFOL(fd, 3) = achievement_id;
  17659. WFIFOSET(fd, packet_len(0xa26));
  17660. }
  17661. /// Process the pet evolution request
  17662. /// 09fb <packetType>.W <packetLength>.W <evolutionPetEggITID>.W (CZ_PET_EVOLUTION)
  17663. void clif_parse_pet_evolution( int fd, map_session_data *sd ){
  17664. #if PACKETVER >= 20141008
  17665. const PACKET_CZ_PET_EVOLUTION* p = reinterpret_cast<PACKET_CZ_PET_EVOLUTION*>( RFIFOP( fd, 0 ) );
  17666. auto pet = pet_db_search( p->EvolvedPetEggID, PET_EGG );
  17667. if (!pet) {
  17668. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_NOT_PETEGG);
  17669. return;
  17670. }
  17671. pet_evolution(sd, pet->class_);
  17672. #endif
  17673. }
  17674. /// Sends the result of the evolution to the client.
  17675. /// 09fc <result>.L (ZC_PET_EVOLUTION_RESULT)
  17676. void clif_pet_evolution_result( map_session_data* sd, e_pet_evolution_result result ){
  17677. #if PACKETVER >= 20141008
  17678. int fd = sd->fd;
  17679. WFIFOHEAD(fd, packet_len(0x9fc));
  17680. WFIFOW(fd, 0) = 0x9fc;
  17681. WFIFOL(fd, 2) = static_cast<uint32>(result);
  17682. WFIFOSET(fd, packet_len(0x9fc));
  17683. #endif
  17684. }
  17685. /*
  17686. * This packet is sent by /changedress or /nocosplay
  17687. *
  17688. * 0ae8
  17689. */
  17690. void clif_parse_changedress( int fd, map_session_data* sd ){
  17691. #if PACKETVER >= 20180103
  17692. char command[CHAT_SIZE_MAX];
  17693. safesnprintf( command, sizeof(command), "%cchangedress", atcommand_symbol );
  17694. is_atcommand( fd, sd, command, 1 );
  17695. #endif
  17696. }
  17697. /// Opens an UI window of the given type and initializes it with the given data
  17698. /// 0AE2 <type>.B <data>.L
  17699. void clif_ui_open( map_session_data& sd, enum out_ui_type ui_type, int32 data ){
  17700. #if PACKETVER >= 20151202
  17701. // If the UI requires state tracking
  17702. switch( ui_type ){
  17703. case OUT_UI_STYLIST:
  17704. sd.state.stylist_open = true;
  17705. break;
  17706. case OUT_UI_ENCHANTGRADE:
  17707. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  17708. sd.state.enchantgrade_open = true;
  17709. break;
  17710. #else
  17711. return;
  17712. #endif
  17713. }
  17714. PACKET_ZC_UI_OPEN p = {};
  17715. p.PacketType = HEADER_ZC_UI_OPEN;
  17716. p.UIType = ui_type;
  17717. #if PACKETVER >= 20171122
  17718. p.data = data;
  17719. #endif
  17720. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17721. #endif
  17722. }
  17723. /// Request to open an UI window of the given type
  17724. /// 0A68 <type>.B
  17725. void clif_parse_open_ui( int fd, map_session_data* sd ){
  17726. switch( RFIFOB(fd,2) ){
  17727. case IN_UI_ATTENDANCE:
  17728. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  17729. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 791 ), false, SELF ); // You are not allowed to use the attendance system.
  17730. }else if( pc_attendance_enabled() ){
  17731. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  17732. }else{
  17733. clif_msg_color( sd, MSI_CHECK_ATTENDANCE_NOT_EVENT, color_table[COLOR_RED] );
  17734. }
  17735. break;
  17736. #if PACKETVER >= 20160316
  17737. case IN_UI_MACRO_REGISTER:
  17738. clif_ui_open(*sd, OUT_UI_CAPTCHA, 0);
  17739. break;
  17740. case IN_UI_MACRO_DETECTOR:
  17741. clif_ui_open(*sd, OUT_UI_MACRO, 0);
  17742. break;
  17743. #endif
  17744. }
  17745. }
  17746. /// Response for attedance request
  17747. /// 0AF0 <unknown>.L <data>.L
  17748. void clif_attendence_response( map_session_data *sd, int32 data ){
  17749. nullpo_retv(sd);
  17750. int fd = sd->fd;
  17751. WFIFOHEAD(fd,packet_len(0xAF0));
  17752. WFIFOW(fd,0) = 0xAF0;
  17753. WFIFOL(fd,2) = 0;
  17754. WFIFOL(fd,6) = data;
  17755. WFIFOSET(fd,packet_len(0xAF0));
  17756. }
  17757. /// Request from the client to retrieve today's attendance reward
  17758. /// 0AEF
  17759. void clif_parse_attendance_request( int fd, map_session_data* sd ){
  17760. pc_attendance_claim_reward(sd);
  17761. }
  17762. /// Send out the percentage of weight that causes it to be displayed in red.
  17763. /// 0ADE <percentage>.L
  17764. void clif_weight_limit( map_session_data* sd ){
  17765. #if PACKETVER >= 20171025
  17766. nullpo_retv(sd);
  17767. int fd = sd->fd;
  17768. WFIFOHEAD(fd, packet_len(0xADE));
  17769. WFIFOW(fd, 0) = 0xADE;
  17770. #ifdef RENEWAL
  17771. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate_renewal;
  17772. #else
  17773. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate;
  17774. #endif
  17775. WFIFOSET(fd, packet_len(0xADE));
  17776. #endif
  17777. }
  17778. enum e_private_airship_response : uint32{
  17779. PRIVATEAIRSHIP_OK,
  17780. PRIVATEAIRSHIP_RETRY,
  17781. PRIVATEAIRSHIP_ITEM_NOT_ENOUGH,
  17782. PRIVATEAIRSHIP_DESTINATION_MAP_INVALID,
  17783. PRIVATEAIRSHIP_SOURCE_MAP_INVALID,
  17784. PRIVATEAIRSHIP_ITEM_UNAVAILABLE
  17785. };
  17786. /// Send out the response to a private airship request
  17787. /// 0A4A <response>.L
  17788. void clif_private_airship_response( map_session_data* sd, enum e_private_airship_response response ){
  17789. #if PACKETVER >= 20180321
  17790. nullpo_retv( sd );
  17791. int fd = sd->fd;
  17792. WFIFOHEAD( fd, packet_len( 0xA4A ) );
  17793. WFIFOW( fd, 0 ) = 0xA4A;
  17794. WFIFOL( fd, 2 ) = response;
  17795. WFIFOSET( fd, packet_len( 0xA4A ) );
  17796. #endif
  17797. }
  17798. /// Parses a request for a private airship
  17799. /// 0A49 <mapname>.16B <itemid>.W
  17800. void clif_parse_private_airship_request( int fd, map_session_data* sd ){
  17801. #if PACKETVER >= 20180321
  17802. // Check if the feature is enabled
  17803. if( !battle_config.feature_privateairship ){
  17804. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 792 ), false, SELF ); // The private airship system is disabled.
  17805. return;
  17806. }
  17807. // Check if the player is allowed to warp from the source map
  17808. if( !map_getmapflag( sd->bl.m, MF_PRIVATEAIRSHIP_SOURCE ) ){
  17809. clif_private_airship_response( sd, PRIVATEAIRSHIP_SOURCE_MAP_INVALID );
  17810. return;
  17811. }
  17812. const PACKET_CZ_PRIVATE_AIRSHIP_REQUEST* p = reinterpret_cast<PACKET_CZ_PRIVATE_AIRSHIP_REQUEST*>( RFIFOP( fd, 0 ) );
  17813. char mapname[MAP_NAME_LENGTH_EXT];
  17814. safestrncpy( mapname, p->mapName, MAP_NAME_LENGTH_EXT );
  17815. int16 mapindex = mapindex_name2id( mapname );
  17816. // Check if we know the mapname
  17817. if( mapindex < 0 ){
  17818. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  17819. return;
  17820. }
  17821. int16 mapid = map_mapindex2mapid( mapindex );
  17822. // Check if the map is available on this server
  17823. if( mapid < 0 ){
  17824. // TODO: add multi map-server support, cant validate the mapflags of the other server
  17825. return;
  17826. }
  17827. // Check if the player tried to warp to the map he is on
  17828. if( sd->bl.m == mapid ){
  17829. // This is blocked by the client, but just to be sure
  17830. return;
  17831. }
  17832. // This can only be a hack, so we prevent it
  17833. if( map_getmapdata( mapid )->instance_id ){
  17834. // Ignore requests to warp directly into a running instance
  17835. return;
  17836. }
  17837. // Check if the player is allowed to warp to the target map
  17838. if( !map_getmapflag( mapid, MF_PRIVATEAIRSHIP_DESTINATION ) ){
  17839. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  17840. return;
  17841. }
  17842. t_itemid item_id = p->ItemID;
  17843. // Check if the item sent by the client is known to us
  17844. if( !itemdb_group.item_exists(IG_PRIVATE_AIRSHIP, item_id) ){
  17845. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_UNAVAILABLE );
  17846. return;
  17847. }
  17848. int idx = pc_search_inventory( sd, item_id );
  17849. // Check if the player has the item at all
  17850. if( idx < 0 ){
  17851. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_NOT_ENOUGH );
  17852. return;
  17853. }
  17854. // Delete the chosen item
  17855. if( pc_delitem( sd, idx, 1, 0, 0, LOG_TYPE_PRIVATE_AIRSHIP ) ){
  17856. clif_private_airship_response( sd, PRIVATEAIRSHIP_RETRY );
  17857. return;
  17858. }
  17859. // Warp the player to a random spot on the destination map
  17860. pc_setpos( sd, mapindex, 0, 0, CLR_TELEPORT );
  17861. #endif
  17862. }
  17863. /// Sends out the usage history of the guild storage
  17864. /// 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
  17865. /// { <card item id>.W }*4 <name>.24B <time>.24B <attribute>.B }*count (ZC_ACK_GUILDSTORAGE_LOG)
  17866. void clif_guild_storage_log( map_session_data& sd, std::vector<struct guild_log_entry>& log, enum e_guild_storage_log result ){
  17867. #if PACKETVER >= 20140205
  17868. PACKET_ZC_ACK_GUILDSTORAGE_LOG* p = reinterpret_cast<PACKET_ZC_ACK_GUILDSTORAGE_LOG*>( packet_buffer );
  17869. p->packetType = HEADER_ZC_ACK_GUILDSTORAGE_LOG;
  17870. p->PacketLength = sizeof( *p );
  17871. p->result = result;
  17872. if( result == GUILDSTORAGE_LOG_FINAL_SUCCESS ){
  17873. p->amount = (uint16)log.size();
  17874. for( int i = 0; i < log.size(); i++ ){
  17875. PACKET_ZC_ACK_GUILDSTORAGE_LOG_sub& item = p->items[i];
  17876. struct guild_log_entry& entry = log[i];
  17877. item.id = entry.id;
  17878. item.itemId = client_nameid( entry.item.nameid );
  17879. item.amount = (uint16)( entry.amount > 0 ? entry.amount : ( entry.amount * -1 ) );
  17880. item.action = entry.amount > 0; // action = true(put), false(get)
  17881. item.refine = entry.item.refine;
  17882. item.uniqueId = entry.item.unique_id;
  17883. item.IsIdentified = entry.item.identify;
  17884. item.itemType = itemtype( entry.item.nameid );
  17885. clif_addcards( &item.slot, &entry.item );
  17886. safestrncpy( item.name, entry.name, NAME_LENGTH );
  17887. safestrncpy( item.time, entry.time, NAME_LENGTH );
  17888. item.attribute = entry.item.attribute;
  17889. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof ( item ) );
  17890. }
  17891. }else{
  17892. p->amount = 0;
  17893. }
  17894. clif_send( p, p->PacketLength, &sd.bl, SELF );
  17895. #endif
  17896. }
  17897. /// Activates the client camera info or updates the client camera with the given values.
  17898. /// 0A78 <type>.B <range>.F <rotation>.F <latitude>.F
  17899. void clif_camerainfo( map_session_data* sd, bool show, float range, float rotation, float latitude ){
  17900. #if PACKETVER >= 20160525
  17901. int fd = sd->fd;
  17902. WFIFOHEAD(fd, packet_len(0xa78));
  17903. WFIFOW(fd, 0) = 0xa78;
  17904. WFIFOB(fd, 2) = show;
  17905. WFIFOF(fd, 3) = range;
  17906. WFIFOF(fd, 7) = rotation;
  17907. WFIFOF(fd, 11) = latitude;
  17908. WFIFOSET(fd, packet_len(0xa78));
  17909. #endif
  17910. }
  17911. /// Activates or deactives the client camera info or updates the camera settings.
  17912. /// This packet is triggered by /viewpointvalue or /setcamera
  17913. /// 0A77 <type>.B <range>.F <rotation>.F <latitude>.F
  17914. void clif_parse_camerainfo( int fd, map_session_data* sd ){
  17915. char command[CHAT_SIZE_MAX];
  17916. // /viewpointvalue
  17917. if( RFIFOB( fd, 2 ) == 1 ){
  17918. safesnprintf( command, sizeof( command ), "%ccamerainfo", atcommand_symbol );
  17919. // /setcamera
  17920. }else{
  17921. safesnprintf( command, sizeof( command ), "%ccamerainfo %03.03f %03.03f %03.03f", atcommand_symbol, RFIFOF( fd, 3 ), RFIFOF( fd, 7 ), RFIFOF( fd, 11 ) );
  17922. }
  17923. is_atcommand( fd, sd, command, 1 );
  17924. }
  17925. /// Send the full list of items in the equip switch window
  17926. /// 0a9b <length>.W { <index>.W <position>.L }*
  17927. void clif_equipswitch_list( map_session_data* sd ){
  17928. #if PACKETVER >= 20170208
  17929. int fd = sd->fd;
  17930. int offset, i, position;
  17931. WFIFOW(fd, 0) = 0xa9b;
  17932. for( i = 0, offset = 4, position = 0; i < EQI_MAX; i++ ){
  17933. short index = sd->equip_switch_index[i];
  17934. if( index >= 0 && !( position & equip_bitmask[i] ) ){
  17935. WFIFOW(fd, offset) = index + 2;
  17936. WFIFOL(fd, offset + 2) = sd->inventory.u.items_inventory[index].equipSwitch;
  17937. position |= sd->inventory.u.items_inventory[index].equipSwitch;
  17938. offset += 6;
  17939. }
  17940. }
  17941. WFIFOW(fd, 2) = offset;
  17942. WFIFOSET(fd, offset);
  17943. #endif
  17944. }
  17945. /// Acknowledgement for removing an equip to the equip switch window
  17946. /// 0a9a <index>.W <position.>.L <failure>.W
  17947. void clif_equipswitch_remove( map_session_data* sd, uint16 index, uint32 pos, bool failed ){
  17948. #if PACKETVER >= 20170208
  17949. int fd = sd->fd;
  17950. WFIFOHEAD(fd, packet_len(0xa9a));
  17951. WFIFOW(fd, 0) = 0xa9a;
  17952. WFIFOW(fd, 2) = index + 2;
  17953. WFIFOL(fd, 4) = pos;
  17954. WFIFOW(fd, 8) = failed;
  17955. WFIFOSET(fd, packet_len(0xa9a));
  17956. #endif
  17957. }
  17958. /// Request to remove an equip from the equip switch window
  17959. /// 0a99 <index>.W <position>.L <= 20170502
  17960. /// 0a99 <index>.W
  17961. void clif_parse_equipswitch_remove( int fd, map_session_data* sd ){
  17962. #if PACKETVER >= 20170208
  17963. uint16 index = RFIFOW(fd, 2) - 2;
  17964. bool removed = false;
  17965. if( !battle_config.feature_equipswitch ){
  17966. return;
  17967. }
  17968. // Check if the index is valid
  17969. if( index >= MAX_INVENTORY ){
  17970. return;
  17971. }
  17972. pc_equipswitch_remove( sd, index );
  17973. #endif
  17974. }
  17975. /// Acknowledgement for adding an equip to the equip switch window
  17976. /// 0a98 <index>.W <position.>.L <flag>.L <= 20170502
  17977. /// 0a98 <index>.W <position.>.L <flag>.W
  17978. void clif_equipswitch_add( map_session_data* sd, uint16 index, uint32 pos, uint8 flag ){
  17979. #if PACKETVER >= 20170208
  17980. int fd = sd->fd;
  17981. WFIFOHEAD(fd, packet_len(0xa98));
  17982. WFIFOW(fd, 0) = 0xa98;
  17983. WFIFOW(fd, 2) = index + 2;
  17984. WFIFOL(fd, 4) = pos;
  17985. #if PACKETVER <= 20170502
  17986. WFIFOL(fd, 8) = flag;
  17987. #else
  17988. WFIFOW(fd, 8) = flag;
  17989. #endif
  17990. WFIFOSET(fd,packet_len(0xa98));
  17991. #endif
  17992. }
  17993. /// Request to add an equip to the equip switch window
  17994. /// 0a97 <index>.W <position>.L
  17995. void clif_parse_equipswitch_add( int fd, map_session_data* sd ){
  17996. #if PACKETVER >= 20170208
  17997. uint16 index = RFIFOW(fd, 2) - 2;
  17998. uint32 position = RFIFOL(fd, 4);
  17999. if( !battle_config.feature_equipswitch ){
  18000. return;
  18001. }
  18002. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  18003. return;
  18004. }
  18005. if( sd->state.trading || sd->npc_shopid ){
  18006. clif_equipswitch_add( sd, index, position, ITEM_EQUIP_ACK_OK );
  18007. return;
  18008. }
  18009. if( sd->inventory_data[index]->type == IT_AMMO ){
  18010. position = EQP_AMMO;
  18011. }
  18012. pc_equipitem( sd, index, position, true );
  18013. #endif
  18014. }
  18015. /// Acknowledgement packet for the full equip switch
  18016. /// 0a9d <failed>.W
  18017. void clif_equipswitch_reply( map_session_data* sd, bool failed ){
  18018. #if PACKETVER >= 20170208
  18019. int fd = sd->fd;
  18020. WFIFOHEAD(fd, packet_len(0xa9d));
  18021. WFIFOW(fd, 0) = 0xa9d;
  18022. WFIFOW(fd, 2) = failed;
  18023. WFIFOSET(fd, packet_len(0xa9d));
  18024. #endif
  18025. }
  18026. /// Request to do a full equip switch
  18027. /// 0a9c
  18028. void clif_parse_equipswitch_request( int fd, map_session_data* sd ){
  18029. #if PACKETVER >= 20170208
  18030. int i;
  18031. t_tick tick = gettick();
  18032. uint16 skill_id = ALL_EQSWITCH, skill_lv = 1;
  18033. if( DIFF_TICK(tick, sd->equipswitch_tick) < 0 ) {
  18034. // Client will not let you send a request
  18035. return;
  18036. }
  18037. sd->equipswitch_tick = tick + skill_get_cooldown( skill_id, skill_lv );
  18038. if( !battle_config.feature_equipswitch ){
  18039. return;
  18040. }
  18041. ARR_FIND( 0, EQI_MAX, i, sd->equip_switch_index[i] >= 0 );
  18042. if( i == EQI_MAX ){
  18043. // client will show: "There is no item to replace." and should not even come here
  18044. clif_equipswitch_reply( sd, false );
  18045. return;
  18046. }
  18047. if( pc_issit(sd) && !pc_setstand( sd, false ) ){
  18048. return;
  18049. }
  18050. clif_parse_skill_toid( sd, skill_id, skill_lv, sd->bl.id );
  18051. #endif
  18052. }
  18053. /// Request to do a single equip switch
  18054. /// 0ace <index>.W
  18055. void clif_parse_equipswitch_request_single( int fd, map_session_data* sd ){
  18056. #if PACKETVER >= 20170502
  18057. uint16 index = RFIFOW(fd, 2) - 2;
  18058. if( !battle_config.feature_equipswitch ){
  18059. return;
  18060. }
  18061. // Check if the index is valid
  18062. if( index >= MAX_INVENTORY ){
  18063. return;
  18064. }
  18065. // Check if the item exists
  18066. if (sd->inventory_data[index] == nullptr)
  18067. return;
  18068. // Check if the item was already added to equip switch
  18069. if( sd->inventory.u.items_inventory[index].equipSwitch ){
  18070. if( sd->npc_id ){
  18071. #ifdef RENEWAL
  18072. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) ){
  18073. clif_msg( sd, MSI_BUSY);
  18074. return;
  18075. }
  18076. #endif
  18077. if( !sd->npc_item_flag ){
  18078. return;
  18079. }
  18080. }
  18081. pc_equipswitch( sd, index );
  18082. return;
  18083. }
  18084. pc_equipitem( sd, index, pc_equippoint(sd, index), true );
  18085. #endif
  18086. }
  18087. void clif_parse_StartUseSkillToId( int fd, map_session_data* sd ){
  18088. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18089. const PACKET_CZ_USE_SKILL_START* p = reinterpret_cast<PACKET_CZ_USE_SKILL_START*>( RFIFOP( fd, 0 ) );
  18090. // Only rolling cutter is supported for now
  18091. if( p->skillId != GC_ROLLINGCUTTER ){
  18092. return;
  18093. }
  18094. // Already running - cant happen on officials, since only one skill is supported
  18095. if( sd->skill_keep_using.skill_id != 0 ){
  18096. return;
  18097. }
  18098. sd->skill_keep_using.tid = INVALID_TIMER;
  18099. sd->skill_keep_using.skill_id = p->skillId;
  18100. sd->skill_keep_using.level = p->skillLv;
  18101. sd->skill_keep_using.target = p->targetId;
  18102. clif_parse_skill_toid( sd, sd->skill_keep_using.skill_id, sd->skill_keep_using.level, sd->skill_keep_using.target );
  18103. #endif
  18104. }
  18105. void clif_parse_StopUseSkillToId( int fd, map_session_data* sd ){
  18106. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18107. const PACKET_CZ_USE_SKILL_END* p = reinterpret_cast<PACKET_CZ_USE_SKILL_END*>( RFIFOP( fd, 0 ) );
  18108. // Not running
  18109. if( sd->skill_keep_using.skill_id == 0 ){
  18110. return;
  18111. }
  18112. #if 0
  18113. // Hack
  18114. if( p->skillId != sd->skill_keep_using.skill_id ){
  18115. return;
  18116. }
  18117. #endif
  18118. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  18119. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  18120. sd->skill_keep_using.tid = INVALID_TIMER;
  18121. }
  18122. sd->skill_keep_using.skill_id = 0;
  18123. sd->skill_keep_using.level = 0;
  18124. sd->skill_keep_using.target = 0;
  18125. #endif
  18126. }
  18127. void clif_ping( map_session_data* sd ){
  18128. #if PACKETVER_MAIN_NUM >= 20190213 || PACKETVER_RE_NUM >= 20190213 || PACKETVER_ZERO_NUM >= 20190130
  18129. nullpo_retv( sd );
  18130. int fd = sd->fd;
  18131. if( !session_isActive( fd ) ){
  18132. return;
  18133. }
  18134. PACKET_ZC_PING_LIVE p = {};
  18135. p.packetType = HEADER_ZC_PING_LIVE;
  18136. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18137. #endif
  18138. }
  18139. int clif_ping_timer_sub( map_session_data *sd, va_list ap ){
  18140. nullpo_ret( sd );
  18141. int fd = sd->fd;
  18142. if( !session_isActive( fd ) ){
  18143. return 0;
  18144. }
  18145. t_tick tick = va_arg( ap, t_tick );
  18146. if( session[fd]->wdata_tick + battle_config.ping_time < tick ){
  18147. clif_ping( sd );
  18148. }
  18149. return 0;
  18150. }
  18151. TIMER_FUNC( clif_ping_timer ){
  18152. map_foreachpc( clif_ping_timer_sub, gettick() );
  18153. return 0;
  18154. }
  18155. /**
  18156. * Opens the refine UI on the designated client.
  18157. * 0aa0
  18158. */
  18159. void clif_refineui_open( map_session_data* sd ){
  18160. #if PACKETVER >= 20161012
  18161. nullpo_retv( sd );
  18162. PACKET_ZC_OPEN_REFINING_UI p = {};
  18163. p.packetType = HEADER_ZC_OPEN_REFINING_UI;
  18164. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18165. sd->state.refineui_open = true;
  18166. #endif
  18167. }
  18168. /**
  18169. * Parses cancel/close of the refine UI on client side.
  18170. * 0aa4
  18171. */
  18172. void clif_parse_refineui_close( int fd, map_session_data* sd ){
  18173. #if PACKETVER >= 20161012
  18174. sd->state.refineui_open = false;
  18175. #endif
  18176. }
  18177. /**
  18178. * Adds the selected item into the refine UI and sends the possible materials
  18179. * to the client.
  18180. * 0aa2 <length>.W <index>.W <catalyst count>.B { <material>.W <chance>.B <price>.L }*
  18181. */
  18182. void clif_refineui_info( map_session_data* sd, uint16 index ){
  18183. #if PACKETVER >= 20161012
  18184. int fd = sd->fd;
  18185. // Get the item db reference
  18186. struct item_data* id = sd->inventory_data[index];
  18187. // No item data was found
  18188. if( id == nullptr ){
  18189. return;
  18190. }
  18191. // Check the inventory
  18192. struct item* item = &sd->inventory.u.items_inventory[index];
  18193. // No item was found at the given index
  18194. if( item == nullptr ){
  18195. return;
  18196. }
  18197. // Check if the item is identified
  18198. if( !item->identify ){
  18199. return;
  18200. }
  18201. // Check if the item is broken
  18202. if( item->attribute ){
  18203. return;
  18204. }
  18205. PACKET_ZC_REFINING_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_REFINING_MATERIAL_LIST*>( packet_buffer );
  18206. p->packetType = HEADER_ZC_REFINING_MATERIAL_LIST;
  18207. p->packetLength = sizeof( *p );
  18208. p->itemIndex = client_index( index );
  18209. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18210. // No possibilities were found
  18211. if( info == nullptr ){
  18212. p->blacksmithBlessing = 0;
  18213. }else{
  18214. p->blacksmithBlessing = static_cast<decltype(p->blacksmithBlessing)>( info->blessing_amount );
  18215. for( uint16 i = REFINE_COST_NORMAL, count = 0; i < REFINE_COST_MAX; i++ ){
  18216. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, i );
  18217. if( cost == nullptr ){
  18218. continue;
  18219. }
  18220. PACKET_ZC_REFINING_MATERIAL_LIST_SUB& entry = p->req[count];
  18221. entry.itemId = client_nameid( cost->nameid );
  18222. entry.chance = static_cast<decltype(entry.chance)>( cost->chance / 100 );
  18223. entry.zeny = cost->zeny;
  18224. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  18225. count++;
  18226. }
  18227. }
  18228. clif_send( p, p->packetLength, &sd->bl, SELF );
  18229. #endif
  18230. }
  18231. /**
  18232. * Client request to add an item to the refine UI.
  18233. * 0aa1 <index>.W
  18234. */
  18235. void clif_parse_refineui_add( int fd, map_session_data* sd ){
  18236. #if PACKETVER >= 20161012
  18237. const PACKET_CZ_REFINING_SELECT_ITEM* p = reinterpret_cast<PACKET_CZ_REFINING_SELECT_ITEM*>( RFIFOP( fd, 0 ) );
  18238. uint16 index = server_index( p->index );
  18239. // Check if the refine UI is open
  18240. if( !sd->state.refineui_open ){
  18241. return;
  18242. }
  18243. // Check if the index is valid
  18244. if( index >= MAX_INVENTORY ){
  18245. return;
  18246. }
  18247. // Send out the requirements for the refine process
  18248. clif_refineui_info( sd, index );
  18249. #endif
  18250. }
  18251. /**
  18252. * Client requests to try to refine an item.
  18253. * 0aa3 <index>.W <material>.W <catalyst>.B
  18254. */
  18255. void clif_parse_refineui_refine( int fd, map_session_data* sd ){
  18256. #if PACKETVER >= 20161012
  18257. const PACKET_CZ_REQ_REFINING* p = reinterpret_cast<PACKET_CZ_REQ_REFINING*>( RFIFOP( fd, 0 ) );
  18258. uint16 index = server_index( p->index );
  18259. t_itemid material = p->itemId;
  18260. int16 j;
  18261. // Check if the refine UI is open
  18262. if( !sd->state.refineui_open ){
  18263. return;
  18264. }
  18265. // Check if the index is valid
  18266. if( index >= MAX_INVENTORY ){
  18267. return;
  18268. }
  18269. // Get the item db reference
  18270. struct item_data* id = sd->inventory_data[index];
  18271. // No item data was found
  18272. if( id == nullptr ){
  18273. return;
  18274. }
  18275. // Check the inventory
  18276. struct item* item = &sd->inventory.u.items_inventory[index];
  18277. // No item was found at the given index
  18278. if( item == nullptr ){
  18279. return;
  18280. }
  18281. // Check if the item is identified
  18282. if( !item->identify ){
  18283. return;
  18284. }
  18285. // Check if the item is broken
  18286. if( item->attribute ){
  18287. return;
  18288. }
  18289. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18290. // No refine possible
  18291. if( info == nullptr ){
  18292. return;
  18293. }
  18294. // No possibilities were found
  18295. if( info->costs.empty() ){
  18296. return;
  18297. }
  18298. // Check if the player has the selected material
  18299. if( ( j = pc_search_inventory( sd, material ) ) < 0 ){
  18300. return;
  18301. }
  18302. int16 blacksmith_index = -1;
  18303. uint16 blacksmith_amount = 0;
  18304. // Check if the player has enough blacksmith blessings
  18305. if( p->blacksmithBlessing != 0 ){
  18306. blacksmith_amount = info->blessing_amount;
  18307. // Player tried to use blacksmith blessing on a refine level, where it is not available
  18308. if( blacksmith_amount == 0 ){
  18309. return;
  18310. }
  18311. // Check if the player has blacksmith blessings
  18312. if( ( blacksmith_index = pc_search_inventory( sd, ITEMID_BLACKSMITH_BLESSING ) ) < 0 ){
  18313. return;
  18314. }
  18315. // Check if the player has enough blacksmith blessings
  18316. if( sd->inventory.u.items_inventory[blacksmith_index].amount < blacksmith_amount ){
  18317. return;
  18318. }
  18319. }
  18320. std::shared_ptr<s_refine_cost> cost = nullptr;
  18321. for( const auto& pair : info->costs ){
  18322. if( pair.second->nameid == material ){
  18323. cost = pair.second;
  18324. break;
  18325. }
  18326. }
  18327. // The material was not in the list of possible materials
  18328. if( cost == nullptr ){
  18329. return;
  18330. }
  18331. // Try to pay for the refine
  18332. if( pc_payzeny( sd, cost->zeny, LOG_TYPE_CONSUME ) ){
  18333. clif_npc_buy_result( sd, e_purchase_result::PURCHASE_FAIL_MONEY ); // "You do not have enough zeny."
  18334. return;
  18335. }
  18336. // Delete the required material
  18337. if( pc_delitem( sd, j, 1, 0, 0, LOG_TYPE_CONSUME ) ){
  18338. return;
  18339. }
  18340. // Delete the required blacksmith blessings
  18341. if( blacksmith_amount > 0 && pc_delitem( sd, blacksmith_index, blacksmith_amount, 0, 0, LOG_TYPE_CONSUME ) ){
  18342. return;
  18343. }
  18344. // Try to refine the item
  18345. if( cost->chance >= ( rnd() % 10000 ) ){
  18346. log_pick_pc( sd, LOG_TYPE_OTHER, -1, item );
  18347. // Success
  18348. item->refine = cap_value( item->refine + 1, 0, MAX_REFINE );
  18349. log_pick_pc( sd, LOG_TYPE_OTHER, 1, item );
  18350. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_SUCCESS );
  18351. clif_refine( *sd, index, ITEMREFINING_SUCCESS );
  18352. if (info->broadcast_success) {
  18353. clif_broadcast_refine_result(*sd, item->nameid, item->refine, true);
  18354. }
  18355. if( id->type == IT_WEAPON ){
  18356. achievement_update_objective( sd, AG_ENCHANT_SUCCESS, 2, id->weapon_level, item->refine );
  18357. }
  18358. clif_refineui_info( sd, index );
  18359. }else{
  18360. // Failure
  18361. if (info->broadcast_failure) {
  18362. clif_broadcast_refine_result(*sd, item->nameid, item->refine, false);
  18363. }
  18364. // Blacksmith blessings were used to prevent breaking and downgrading
  18365. if( blacksmith_amount > 0 ){
  18366. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18367. clif_refineui_info( sd, index );
  18368. // Delete the item if it is breakable
  18369. }else if( cost->breaking_rate > 0 && ( rnd() % 10000 ) < cost->breaking_rate ){
  18370. clif_refine( *sd, index, ITEMREFINING_FAILURE );
  18371. pc_delitem( sd, index, 1, 0, 2, LOG_TYPE_CONSUME );
  18372. // Downgrade the item if necessary
  18373. }else if( cost->downgrade_amount > 0 ){
  18374. item->refine = cap_value( item->refine - cost->downgrade_amount, 0, MAX_REFINE );
  18375. clif_refine( *sd, index, ITEMREFINING_DOWNGRADE );
  18376. clif_refineui_info(sd, index);
  18377. // Only show failure, but dont do anything
  18378. }else{
  18379. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18380. clif_refineui_info( sd, index );
  18381. }
  18382. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_FAILURE );
  18383. achievement_update_objective( sd, AG_ENCHANT_FAIL, 1, 1 );
  18384. }
  18385. #endif
  18386. }
  18387. void clif_unequipall_reply( map_session_data* sd, bool failed ){
  18388. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18389. PACKET_ZC_ACK_TAKEOFF_EQUIP_ALL p = {};
  18390. p.PacketType = HEADER_ZC_ACK_TAKEOFF_EQUIP_ALL;
  18391. p.result = failed;
  18392. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18393. #endif // PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18394. }
  18395. void clif_parse_unequipall( int fd, map_session_data* sd ){
  18396. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20210818
  18397. if( pc_cant_act( sd ) ){
  18398. clif_unequipall_reply( sd, true );
  18399. return;
  18400. }
  18401. const PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL* p = reinterpret_cast<PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL*>( RFIFOP( fd, 0 ) );
  18402. #if PACKETVER_MAIN_NUM >= 20230906
  18403. uint32 location = p->location;
  18404. int max = EQI_MAX;
  18405. #else
  18406. uint32 location = 0xFFFFFFFF;
  18407. int max = EQI_COSTUME_HEAD_TOP;
  18408. #endif
  18409. for( int i = 0; i < max; i++ ){
  18410. if( sd->equip_index[i] >= 0 && ( location & equip_bitmask[i] ) != 0 ){
  18411. pc_unequipitem( sd, sd->equip_index[i], 1 );
  18412. }
  18413. }
  18414. clif_unequipall_reply( sd, false );
  18415. #endif
  18416. }
  18417. void clif_stylist_response( map_session_data* sd, bool failed ){
  18418. #if PACKETVER >= 20151104
  18419. PACKET_ZC_STYLE_CHANGE_RES p = {};
  18420. p.PacketType = HEADER_ZC_STYLE_CHANGE_RES;
  18421. p.flag = failed;
  18422. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18423. if( !failed ){
  18424. sd->state.stylist_open = false;
  18425. }
  18426. #endif
  18427. }
  18428. bool clif_parse_stylist_buy_sub( map_session_data* sd, _look look, int16 index ){
  18429. std::shared_ptr<s_stylist_list> list = stylist_db.find( look );
  18430. if( list == nullptr ){
  18431. return false;
  18432. }
  18433. std::shared_ptr<s_stylist_entry> entry = util::umap_find( list->entries, index );
  18434. if( entry == nullptr ){
  18435. return false;
  18436. }
  18437. std::shared_ptr<s_stylist_costs> costs;
  18438. if( ( sd->class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  18439. costs = entry->doram;
  18440. }else{
  18441. costs = entry->human;
  18442. }
  18443. if( costs == nullptr ){
  18444. return false;
  18445. }
  18446. if( sd->status.zeny < costs->price ){
  18447. return false;
  18448. }
  18449. int16 inventoryIndex = -1;
  18450. if( costs->requiredItem != 0 ){
  18451. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18452. if( inventoryIndex < 0 ){
  18453. // No other option
  18454. if( costs->requiredItemBox == 0 ){
  18455. return false;
  18456. }
  18457. // Check if the box that contains the item is in the inventory
  18458. inventoryIndex = pc_search_inventory( sd, costs->requiredItemBox );
  18459. // The box containing the item also does not exist
  18460. if( inventoryIndex < 0 ){
  18461. return false;
  18462. }
  18463. }
  18464. }else if( costs->requiredItemBox != 0 ){
  18465. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18466. if( inventoryIndex < 0 ){
  18467. return false;
  18468. }
  18469. }
  18470. if( inventoryIndex >= 0 && pc_delitem( sd, inventoryIndex, 1, 0, 0, LOG_TYPE_OTHER ) != 0 ){
  18471. return false;
  18472. }
  18473. if( costs->price > 0 && pc_payzeny( sd, costs->price, LOG_TYPE_OTHER ) != 0 ){
  18474. return false;
  18475. }
  18476. switch( look ){
  18477. case LOOK_HAIR:
  18478. case LOOK_HAIR_COLOR:
  18479. case LOOK_CLOTHES_COLOR:
  18480. case LOOK_BODY2:
  18481. pc_changelook( sd, look, entry->value );
  18482. break;
  18483. case LOOK_HEAD_BOTTOM:
  18484. case LOOK_HEAD_MID:
  18485. case LOOK_HEAD_TOP: {
  18486. struct mail_message msg = {};
  18487. msg.dest_id = sd->status.char_id;
  18488. safestrncpy( msg.send_name, "Styling Shop", NAME_LENGTH );
  18489. safestrncpy( msg.title, "<MSG>2949</MSG>", MAIL_TITLE_LENGTH );
  18490. safestrncpy( msg.body, "<MSG>2950</MSG>", MAIL_BODY_LENGTH );
  18491. msg.item[0].nameid = entry->value;
  18492. msg.item[0].identify = 1;
  18493. msg.item[0].amount = 1;
  18494. msg.status = MAIL_NEW;
  18495. msg.type = MAIL_INBOX_NORMAL;
  18496. msg.timestamp = time( nullptr );
  18497. intif_Mail_send( 0, &msg );
  18498. } break;
  18499. }
  18500. return true;
  18501. }
  18502. void clif_parse_stylist_buy( int fd, map_session_data* sd ){
  18503. #if PACKETVER >= 20151104
  18504. #if PACKETVER >= 20180516
  18505. const PACKET_CZ_REQ_STYLE_CHANGE2* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE2*>( RFIFOP( fd, 0 ) );
  18506. #else
  18507. const PACKET_CZ_REQ_STYLE_CHANGE* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE*>( RFIFOP( fd, 0 ) );
  18508. #endif
  18509. if( p->HeadPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR_COLOR, p->HeadPalette ) ){
  18510. clif_stylist_response( sd, true );
  18511. return;
  18512. }
  18513. if( p->HeadStyle != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR, p->HeadStyle ) ){
  18514. clif_stylist_response( sd, true );
  18515. return;
  18516. }
  18517. if( p->BodyPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_CLOTHES_COLOR, p->BodyPalette ) ){
  18518. clif_stylist_response( sd, true );
  18519. return;
  18520. }
  18521. if( p->TopAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_TOP, p->TopAccessory ) ){
  18522. clif_stylist_response( sd, true );
  18523. return;
  18524. }
  18525. if( p->MidAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_MID, p->MidAccessory ) ){
  18526. clif_stylist_response( sd, true );
  18527. return;
  18528. }
  18529. if( p->BottomAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_BOTTOM, p->BottomAccessory ) ){
  18530. clif_stylist_response( sd, true );
  18531. return;
  18532. }
  18533. #if PACKETVER >= 20180516
  18534. if( p->BodyStyle != 0 && ( sd->class_ & JOBL_THIRD ) != 0 && ( sd->class_ & JOBL_FOURTH ) == 0 && !clif_parse_stylist_buy_sub( sd, LOOK_BODY2, p->BodyStyle ) ){
  18535. clif_stylist_response( sd, true );
  18536. return;
  18537. }
  18538. #endif
  18539. clif_stylist_response( sd, false );
  18540. #endif
  18541. }
  18542. void clif_parse_stylist_close( int fd, map_session_data* sd ){
  18543. #if PACKETVER >= 20151104
  18544. sd->state.stylist_open = false;
  18545. #endif
  18546. }
  18547. void clif_inventory_expansion_info( map_session_data* sd ){
  18548. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18549. nullpo_retv( sd );
  18550. PACKET_ZC_EXTEND_BODYITEM_SIZE p = {};
  18551. p.packetType = HEADER_ZC_EXTEND_BODYITEM_SIZE;
  18552. p.expansionSize = sd->status.inventory_slots - INVENTORY_BASE_SIZE;
  18553. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18554. #endif
  18555. }
  18556. enum class e_inventory_expansion_response : uint8{
  18557. ASK_CONFIRMATION = 0,
  18558. FAILED,
  18559. BUSY,
  18560. MISSING_ITEM,
  18561. MAXIMUM_REACHED
  18562. };
  18563. void clif_inventory_expansion_response( map_session_data* sd, e_inventory_expansion_response response ){
  18564. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18565. nullpo_retv( sd );
  18566. PACKET_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE p = {};
  18567. p.packetType = HEADER_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE;
  18568. p.result = (uint8)response;
  18569. p.itemId = sd->state.inventory_expansion_confirmation;
  18570. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18571. #endif
  18572. }
  18573. void clif_parse_inventory_expansion_request( int fd, map_session_data* sd ){
  18574. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18575. // Check if player is dead or busy with other stuff
  18576. if( pc_isdead( sd ) || pc_cant_act( sd ) ){
  18577. clif_inventory_expansion_response( sd, e_inventory_expansion_response::BUSY );
  18578. return;
  18579. }
  18580. // Check if the player already reached the maximum
  18581. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18582. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18583. return;
  18584. }
  18585. static std::map<t_itemid, uint16> items = {
  18586. // The order of entries in this list defines which will be used first
  18587. // This order and the usable items are hardcoded into the client
  18588. // The number of increased slots is "hardcoded" in the message of the client and cannot be sent per item
  18589. { ITEMID_INVENTORY_EX_EVT, 10 },
  18590. { ITEMID_INVENTORY_EX_DIS, 10 },
  18591. { ITEMID_INVENTORY_EX, 10 },
  18592. };
  18593. int16 index = -1;
  18594. bool found_over_limit = false;
  18595. for( const auto& entry : items ){
  18596. // Check if the player has the required item
  18597. index = pc_search_inventory( sd, entry.first );
  18598. // Found an item
  18599. if( index >= 0 ){
  18600. // Check if the player would exceed the maximum
  18601. if( sd->status.inventory_slots + entry.second > MAX_INVENTORY ){
  18602. found_over_limit = true;
  18603. }else{
  18604. found_over_limit = false;
  18605. sd->state.inventory_expansion_confirmation = entry.first;
  18606. sd->state.inventory_expansion_amount = entry.second;
  18607. break;
  18608. }
  18609. }
  18610. }
  18611. // Check if an item was found
  18612. if( sd->state.inventory_expansion_confirmation == 0 ){
  18613. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MISSING_ITEM );
  18614. return;
  18615. }
  18616. // Check if an item would have been found, but the player would exceed the maximum
  18617. if( found_over_limit ){
  18618. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18619. return;
  18620. }
  18621. // The player met all requirements => ask him for confirmation
  18622. clif_inventory_expansion_response( sd, e_inventory_expansion_response::ASK_CONFIRMATION );
  18623. #endif
  18624. }
  18625. enum class e_inventory_expansion_result : uint8{
  18626. SUCCESS = 0,
  18627. FAILED,
  18628. BUSY,
  18629. MISSING_ITEM,
  18630. MAXIMUM_REACHED
  18631. };
  18632. void clif_inventory_expansion_result( map_session_data* sd, e_inventory_expansion_result result ){
  18633. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18634. nullpo_retv( sd );
  18635. PACKET_ZC_ACK_EXTEND_BODYITEM_SIZE p = {};
  18636. p.packetType = HEADER_ZC_ACK_EXTEND_BODYITEM_SIZE;
  18637. p.result = (uint8)result;
  18638. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18639. // Reset the state tracking
  18640. sd->state.inventory_expansion_confirmation = 0;
  18641. sd->state.inventory_expansion_amount = 0;
  18642. #endif
  18643. }
  18644. void clif_parse_inventory_expansion_confirm( int fd, map_session_data* sd ){
  18645. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18646. if( sd->state.inventory_expansion_confirmation == 0 ){
  18647. return;
  18648. }
  18649. // Check if player is dead
  18650. if( pc_isdead( sd ) ){
  18651. clif_inventory_expansion_result( sd, e_inventory_expansion_result::BUSY );
  18652. return;
  18653. }
  18654. // Check if the player already reached the maximum
  18655. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18656. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18657. return;
  18658. }
  18659. // Check if the player has the required item
  18660. int index = pc_search_inventory( sd, sd->state.inventory_expansion_confirmation );
  18661. // The player did not have the item anymore
  18662. if( index < 0 ){
  18663. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MISSING_ITEM );
  18664. return;
  18665. }
  18666. // Check if the player would exceed the maximum
  18667. if( sd->status.inventory_slots + sd->state.inventory_expansion_amount > MAX_INVENTORY ){
  18668. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18669. return;
  18670. }
  18671. // Delete the required item
  18672. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_OTHER ) ){
  18673. clif_inventory_expansion_result( sd, e_inventory_expansion_result::FAILED );
  18674. return;
  18675. }
  18676. // Increase the slots
  18677. sd->status.inventory_slots += sd->state.inventory_expansion_amount;
  18678. // Save player data (slots) and inventory data (removed item)
  18679. chrif_save( sd, CSAVE_NORMAL | CSAVE_INVENTORY );
  18680. // Inform the player of success
  18681. clif_inventory_expansion_result( sd, e_inventory_expansion_result::SUCCESS );
  18682. clif_inventory_expansion_info( sd );
  18683. #endif
  18684. }
  18685. void clif_parse_inventory_expansion_reject( int fd, map_session_data* sd ){
  18686. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18687. sd->state.inventory_expansion_confirmation = 0;
  18688. sd->state.inventory_expansion_amount = 0;
  18689. #endif
  18690. }
  18691. void clif_barter_open( map_session_data& sd, struct npc_data& nd ){
  18692. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18693. if( nd.subtype != NPCTYPE_BARTER || nd.u.barter.extended || sd.state.barter_open ){
  18694. return;
  18695. }
  18696. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18697. if( barter == nullptr ){
  18698. return;
  18699. }
  18700. sd.state.barter_open = true;
  18701. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  18702. p->packetType = HEADER_ZC_NPC_BARTER_MARKET_ITEMINFO;
  18703. p->packetLength = sizeof( *p );
  18704. int16 count = 0;
  18705. for( const auto& itemPair : barter->items ){
  18706. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  18707. if( id == nullptr ){
  18708. continue;
  18709. }
  18710. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO_sub& item = p->list[count];
  18711. item.nameid = client_nameid( id->nameid );
  18712. item.type = itemtype( id->nameid );
  18713. if( itemPair.second->stockLimited ){
  18714. item.amount = itemPair.second->stock;
  18715. }else{
  18716. item.amount = -1;
  18717. }
  18718. item.weight = id->weight;
  18719. item.index = itemPair.second->index;
  18720. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  18721. item.viewSprite = id->look;
  18722. item.location = pc_equippoint_sub( &sd, id.get() );
  18723. #endif
  18724. // Use a loop if someone did not start with index 0
  18725. for( const auto& requirementPair : itemPair.second->requirements ){
  18726. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  18727. item.currencyNameid = client_nameid( requirement->nameid );
  18728. item.currencyAmount = requirement->amount;
  18729. // It is a normal barter, cancel after first entry
  18730. break;
  18731. }
  18732. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( item ) );
  18733. count++;
  18734. }
  18735. clif_send( p, p->packetLength, &sd.bl, SELF );
  18736. #endif
  18737. }
  18738. void clif_parse_barter_close( int fd, map_session_data* sd ){
  18739. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18740. if( sd->state.barter_open ){
  18741. sd->npc_shopid = 0;
  18742. sd->state.barter_open = false;
  18743. }
  18744. #endif
  18745. }
  18746. void clif_parse_barter_buy( int fd, map_session_data* sd ){
  18747. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18748. // No shop open
  18749. if( sd->npc_shopid == 0 || !sd->state.barter_open ){
  18750. return;
  18751. }
  18752. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  18753. // Unknown shop
  18754. if( nd == nullptr ){
  18755. return;
  18756. }
  18757. // Not a barter
  18758. if( nd->subtype != NPCTYPE_BARTER ){
  18759. return;
  18760. }
  18761. // It is an extended barter
  18762. if( nd->u.barter.extended ){
  18763. return;
  18764. }
  18765. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  18766. if( barter == nullptr ){
  18767. return;
  18768. }
  18769. const PACKET_CZ_NPC_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  18770. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  18771. // Empty purchase list
  18772. if( entries == 0 ){
  18773. return;
  18774. }
  18775. std::vector<s_barter_purchase> purchases;
  18776. purchases.reserve( entries );
  18777. // Make sure each shop index and target item id is only used once
  18778. for( int i = 0; i < entries; i++ ){
  18779. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  18780. // Invalid shop index
  18781. if( item == nullptr ){
  18782. return;
  18783. }
  18784. for( int j = i + 1; j < entries; j++ ){
  18785. // Same shop index
  18786. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  18787. return;
  18788. }
  18789. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  18790. // Invalid shop index
  18791. if( item2 == nullptr ){
  18792. return;
  18793. }
  18794. // Same target item id
  18795. if( item->nameid == item2->nameid ){
  18796. return;
  18797. }
  18798. }
  18799. s_barter_purchase purchase = {};
  18800. purchase.item = item;
  18801. purchase.amount = p->list[i].amount;
  18802. purchases.push_back( purchase );
  18803. }
  18804. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  18805. #endif
  18806. }
  18807. void clif_barter_extended_open( map_session_data& sd, struct npc_data& nd ){
  18808. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18809. if( nd.subtype != NPCTYPE_BARTER || !nd.u.barter.extended || sd.state.barter_extended_open ){
  18810. return;
  18811. }
  18812. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18813. if( barter == nullptr ){
  18814. return;
  18815. }
  18816. sd.state.barter_extended_open = true;
  18817. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  18818. p->packetType = HEADER_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO;
  18819. p->packetLength = sizeof( *p );
  18820. p->items_count = 0;
  18821. for( const auto& itemPair : barter->items ){
  18822. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  18823. if( id == nullptr ){
  18824. continue;
  18825. }
  18826. // Needs dynamic calculation, because of variable currencies
  18827. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub* item = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub*>( WBUFP( p, p->packetLength ) );
  18828. item->nameid = client_nameid( id->nameid );
  18829. item->type = itemtype( id->nameid );
  18830. if( itemPair.second->stockLimited ){
  18831. item->amount = itemPair.second->stock;
  18832. }else{
  18833. item->amount = -1;
  18834. }
  18835. item->weight = id->weight;
  18836. item->index = itemPair.second->index;
  18837. item->zeny = itemPair.second->price;
  18838. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  18839. item->viewSprite = id->look;
  18840. item->location = pc_equippoint_sub( &sd, id.get() );
  18841. #endif
  18842. // Because of a MSVS bug, the currencies have been defined with a fixed array entry, which has to be substracted
  18843. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *item ) - sizeof( item->currencies ) );
  18844. p->items_count++;
  18845. item->currency_count = 0;
  18846. for( const auto& requirementPair : itemPair.second->requirements ){
  18847. // Needs dynamic calculation, because of variable currencies
  18848. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2* req = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2*>( WBUFP( p, p->packetLength ) );
  18849. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  18850. req->nameid = requirement->nameid;
  18851. if( requirement->refine >= 0 ){
  18852. req->refine_level = requirement->refine;
  18853. }else{
  18854. req->refine_level = 0;
  18855. }
  18856. req->amount = requirement->amount;
  18857. req->type = itemtype( requirement->nameid );
  18858. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *req ) );
  18859. item->currency_count++;
  18860. }
  18861. }
  18862. clif_send( p, p->packetLength, &sd.bl, SELF );
  18863. #endif
  18864. }
  18865. void clif_parse_barter_extended_close( int fd, map_session_data* sd ){
  18866. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18867. if( sd->state.barter_extended_open ){
  18868. sd->npc_shopid = 0;
  18869. sd->state.barter_extended_open = false;
  18870. }
  18871. #endif
  18872. }
  18873. void clif_parse_barter_extended_buy( int fd, map_session_data* sd ){
  18874. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18875. // No shop open
  18876. if( sd->npc_shopid == 0 || !sd->state.barter_extended_open ){
  18877. return;
  18878. }
  18879. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  18880. // Unknown shop
  18881. if( nd == nullptr ){
  18882. return;
  18883. }
  18884. // Not a barter
  18885. if( nd->subtype != NPCTYPE_BARTER ){
  18886. return;
  18887. }
  18888. // Not an extended barter
  18889. if( !nd->u.barter.extended ){
  18890. return;
  18891. }
  18892. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  18893. if( barter == nullptr ){
  18894. return;
  18895. }
  18896. const PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  18897. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  18898. // Empty purchase list
  18899. if( entries == 0 ){
  18900. return;
  18901. }
  18902. std::vector<s_barter_purchase> purchases;
  18903. purchases.reserve( entries );
  18904. // Make sure each shop index and target item id is only used once
  18905. for( int i = 0; i < entries; i++ ){
  18906. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  18907. // Invalid shop index
  18908. if( item == nullptr ){
  18909. return;
  18910. }
  18911. for( int j = i + 1; j < entries; j++ ){
  18912. // Same shop index
  18913. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  18914. return;
  18915. }
  18916. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  18917. // Invalid shop index
  18918. if( item2 == nullptr ){
  18919. return;
  18920. }
  18921. // Same target item id
  18922. if( item->nameid == item2->nameid ){
  18923. return;
  18924. }
  18925. }
  18926. s_barter_purchase purchase = {};
  18927. purchase.item = item;
  18928. purchase.amount = p->list[i].amount;
  18929. purchases.push_back( purchase );
  18930. }
  18931. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  18932. #endif
  18933. }
  18934. void clif_summon_init(struct mob_data& md) {
  18935. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  18936. struct block_list* master_bl = battle_get_master(&md.bl);
  18937. if( master_bl == nullptr ){
  18938. return;
  18939. }
  18940. PACKET_ZC_SUMMON_HP_INIT p = {};
  18941. p.PacketType = HEADER_ZC_SUMMON_HP_INIT;
  18942. p.summonAID = md.bl.id;
  18943. p.CurrentHP = md.status.hp;
  18944. p.MaxHP = md.status.max_hp;
  18945. clif_send( &p, sizeof( p ), master_bl, SELF );
  18946. #endif
  18947. }
  18948. void clif_summon_hp_bar(struct mob_data& md) {
  18949. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  18950. struct block_list* master_bl = battle_get_master(&md.bl);
  18951. if( master_bl == nullptr ){
  18952. return;
  18953. }
  18954. PACKET_ZC_SUMMON_HP_UPDATE p = {};
  18955. p.PacketType = HEADER_ZC_SUMMON_HP_UPDATE;
  18956. p.summonAID = md.bl.id;
  18957. p.VarId = SP_HP; // HP parameter
  18958. p.Value = md.status.hp;
  18959. clif_send( &p, sizeof( p ), master_bl, SELF );
  18960. #endif
  18961. }
  18962. void clif_laphine_synthesis_open( map_session_data *sd, std::shared_ptr<s_laphine_synthesis> synthesis ){
  18963. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  18964. nullpo_retv( sd );
  18965. sd->state.laphine_synthesis = synthesis->item_id;
  18966. PACKET_ZC_RANDOM_COMBINE_ITEM_UI_OPEN p = {};
  18967. p.packetType = HEADER_ZC_RANDOM_COMBINE_ITEM_UI_OPEN;
  18968. p.itemId = client_nameid( synthesis->item_id );
  18969. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18970. #endif
  18971. }
  18972. void clif_parse_laphine_synthesis_close( int fd, map_session_data* sd ){
  18973. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  18974. sd->state.laphine_synthesis = 0;
  18975. #endif
  18976. }
  18977. enum e_laphine_synthesis_result : int16{
  18978. LAPHINE_SYNTHESIS_SUCCESS = 0,
  18979. LAPHINE_SYNTHESIS_AMOUNT = 5,
  18980. LAPHINE_SYNTHESIS_ITEM = 7
  18981. };
  18982. void clif_laphine_synthesis_result( map_session_data* sd, enum e_laphine_synthesis_result result ){
  18983. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  18984. nullpo_retv( sd );
  18985. PACKET_ZC_ACK_RANDOM_COMBINE_ITEM p = {};
  18986. p.packetType = HEADER_ZC_ACK_RANDOM_COMBINE_ITEM;
  18987. p.result = result;
  18988. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18989. #endif
  18990. }
  18991. void clif_parse_laphine_synthesis( int fd, map_session_data* sd ){
  18992. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  18993. if( sd->state.laphine_synthesis == 0 ){
  18994. return;
  18995. }
  18996. const PACKET_CZ_REQ_RANDOM_COMBINE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_COMBINE_ITEM*>( RFIFOP( fd, 0 ) );
  18997. if( sd->state.laphine_synthesis != p->itemId ){
  18998. return;
  18999. }
  19000. std::shared_ptr<s_laphine_synthesis> synthesis = laphine_synthesis_db.find( sd->state.laphine_synthesis );
  19001. if( synthesis == nullptr ){
  19002. return;
  19003. }
  19004. size_t count = ( p->packetLength - sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM ) ) / sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM_sub );
  19005. // Player sent more or less than actually required
  19006. if( count != synthesis->requiredRequirements ){
  19007. return;
  19008. }
  19009. // Check for duplicates
  19010. for( size_t i = 0; i < count; i++ ){
  19011. for( size_t j = i + 1; j < count; j++ ){
  19012. if( p->items[i].index == p->items[j].index ){
  19013. return;
  19014. }
  19015. }
  19016. }
  19017. for( size_t i = 0; i < count; i++ ){
  19018. int16 index = server_index( p->items[i].index );
  19019. if( index >= MAX_INVENTORY ){
  19020. return;
  19021. }
  19022. if( sd->inventory_data[index] == nullptr ){
  19023. return;
  19024. }
  19025. struct item* item = &sd->inventory.u.items_inventory[index];
  19026. std::shared_ptr<s_laphine_synthesis_requirement> requirement = util::umap_find( synthesis->requirements, item->nameid );
  19027. if( requirement == nullptr ){
  19028. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19029. return;
  19030. }
  19031. if( p->items[i].count != requirement->amount ){
  19032. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19033. return;
  19034. }
  19035. if( item->amount < requirement->amount ){
  19036. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19037. return;
  19038. }
  19039. if( item->refine < synthesis->minimumRefine ){
  19040. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19041. return;
  19042. }
  19043. if( item->refine > synthesis->maximumRefine ){
  19044. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19045. return;
  19046. }
  19047. if( item->equip != 0 ){
  19048. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19049. return;
  19050. }
  19051. if( item->equipSwitch != 0 ){
  19052. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19053. return;
  19054. }
  19055. }
  19056. // If triggered from item
  19057. if( sd->itemid == sd->state.laphine_synthesis ){
  19058. int16 index = pc_search_inventory( sd, sd->state.laphine_synthesis );
  19059. if( index < 0 ){
  19060. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19061. return;
  19062. }
  19063. if( ( sd->inventory_data[index]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19064. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19065. return;
  19066. }
  19067. }
  19068. }
  19069. for( size_t i = 0; i < count; i++ ){
  19070. int16 index = server_index( p->items[i].index );
  19071. if( pc_delitem( sd, index, p->items[i].count, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19072. return;
  19073. }
  19074. }
  19075. itemdb_group.pc_get_itemgroup( synthesis->rewardGroupId, true, *sd );
  19076. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_SUCCESS );
  19077. #endif
  19078. }
  19079. void clif_laphine_upgrade_open( map_session_data* sd, std::shared_ptr<s_laphine_upgrade> upgrade ){
  19080. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19081. nullpo_retv( sd );
  19082. sd->state.laphine_upgrade = upgrade->item_id;
  19083. PACKET_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN p = {};
  19084. p.packetType = HEADER_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN;
  19085. p.itemId = client_nameid( upgrade->item_id );
  19086. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19087. #endif
  19088. }
  19089. void clif_parse_laphine_upgrade_close( int fd, map_session_data* sd ){
  19090. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19091. sd->state.laphine_upgrade = 0;
  19092. #endif
  19093. }
  19094. void clif_laphine_upgrade_result( map_session_data *sd, bool failed ){
  19095. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19096. PACKET_ZC_ACK_RANDOM_UPGRADE_ITEM p = {};
  19097. p.packetType = HEADER_ZC_ACK_RANDOM_UPGRADE_ITEM;
  19098. p.result = failed;
  19099. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19100. #endif
  19101. }
  19102. static void clif_item_preview( map_session_data *sd, int16 index ){
  19103. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19104. nullpo_retv( sd );
  19105. struct item* item = &sd->inventory.u.items_inventory[index];
  19106. PACKET_ZC_CHANGE_ITEM_OPTION p = {};
  19107. p.packetType = HEADER_ZC_CHANGE_ITEM_OPTION;
  19108. p.index = client_index( index );
  19109. #if PACKETVER_MAIN_NUM >= 20181017 || PACKETVER_RE_NUM >= 20181017 || PACKETVER_ZERO_NUM >= 20181024
  19110. p.isDamaged = item->attribute != 0;
  19111. #endif
  19112. p.refiningLevel = item->refine;
  19113. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723
  19114. p.grade = item->enchantgrade;
  19115. #endif
  19116. clif_addcards( &p.slot, item );
  19117. clif_add_random_options( p.option_data, *item );
  19118. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19119. #endif
  19120. }
  19121. void clif_parse_laphine_upgrade( int fd, map_session_data* sd ){
  19122. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19123. if( sd->state.laphine_upgrade == 0 ){
  19124. return;
  19125. }
  19126. const PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM*>( RFIFOP( fd, 0 ) );
  19127. if( sd->state.laphine_upgrade != p->itemId ){
  19128. return;
  19129. }
  19130. std::shared_ptr<s_laphine_upgrade> upgrade = laphine_upgrade_db.find( sd->state.laphine_upgrade );
  19131. if( upgrade == nullptr ){
  19132. return;
  19133. }
  19134. uint16 index = server_index( p->index );
  19135. if( index >= MAX_INVENTORY ){
  19136. return;
  19137. }
  19138. if( sd->inventory_data[index] == nullptr ){
  19139. return;
  19140. }
  19141. struct item* item = &sd->inventory.u.items_inventory[index];
  19142. // Not a valid target item
  19143. if( !util::vector_exists( upgrade->target_item_ids, item->nameid ) ){
  19144. clif_laphine_upgrade_result( sd, true );
  19145. return;
  19146. }
  19147. // If target item is not identified
  19148. if( item->identify == 0 ){
  19149. clif_laphine_upgrade_result( sd, true );
  19150. return;
  19151. }
  19152. // If target item is equipped
  19153. if( item->equip != 0 ){
  19154. clif_laphine_upgrade_result( sd, true );
  19155. return;
  19156. }
  19157. // If target item is in equipswitch
  19158. if( item->equipSwitch != 0 ){
  19159. clif_laphine_upgrade_result( sd, true );
  19160. return;
  19161. }
  19162. // Check minimum refine requirement
  19163. if( item->refine < upgrade->minimumRefine ){
  19164. clif_laphine_upgrade_result( sd, true );
  19165. return;
  19166. }
  19167. // Check maximum refine requirement
  19168. if( item->refine > upgrade->maximumRefine ){
  19169. clif_laphine_upgrade_result( sd, true );
  19170. return;
  19171. }
  19172. // If no cards are allowed
  19173. if( !upgrade->cardsAllowed ){
  19174. for( int i = 0; i < MAX_SLOTS; i++ ){
  19175. if( item->card[i] != 0 ){
  19176. clif_laphine_upgrade_result( sd, true );
  19177. return;
  19178. }
  19179. }
  19180. }
  19181. // If random options are required
  19182. if( upgrade->requiredRandomOptions > 0 ){
  19183. int i;
  19184. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19185. if( item->option[i].id != 0 ){
  19186. break;
  19187. }
  19188. }
  19189. if( ( i + 1 ) < upgrade->requiredRandomOptions ){
  19190. clif_laphine_upgrade_result( sd, true );
  19191. return;
  19192. }
  19193. }
  19194. int16 index2 = pc_search_inventory( sd, sd->state.laphine_upgrade );
  19195. if( index2 < 0 ){
  19196. clif_laphine_upgrade_result( sd, true );
  19197. return;
  19198. }
  19199. if( ( sd->inventory_data[index2]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19200. if( pc_delitem( sd, index2, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19201. return;
  19202. }
  19203. }
  19204. // Log removal of item
  19205. log_pick_pc( sd, LOG_TYPE_LAPHINE, -1, item );
  19206. // Visually remove it from the client
  19207. clif_delitem( *sd, index, 1, 0 );
  19208. // Apply the random options
  19209. if( upgrade->randomOptionGroup != nullptr ){
  19210. upgrade->randomOptionGroup->apply( *item );
  19211. }
  19212. // Change the refine rate if needed
  19213. if( upgrade->resultRefine > 0 ){
  19214. // Absolute refine level change
  19215. item->refine = max( item->refine, upgrade->resultRefine );
  19216. }else if( upgrade->resultRefineMaximum > 0 ){
  19217. // If a minimum is specified it can also downgrade
  19218. if( upgrade->resultRefineMinimum ){
  19219. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, upgrade->resultRefineMaximum ) );
  19220. }else{
  19221. // Otherwise it can only be upgraded until the maximum, but not downgraded
  19222. item->refine = static_cast<uint8>( rnd_value<uint16>( item->refine, upgrade->resultRefineMaximum ) );
  19223. }
  19224. }else if( upgrade->resultRefineMinimum > 0 ){
  19225. // No maximum has been specified, so it can be anything between minimum and MAX_REFINE
  19226. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, MAX_REFINE ) );
  19227. }
  19228. // Log retrieving the item again -> with the new options
  19229. log_pick_pc( sd, LOG_TYPE_LAPHINE, 1, item );
  19230. // Make it visible for the client again
  19231. clif_additem( sd, index, 1, 0 );
  19232. // Open a preview of the item
  19233. clif_item_preview( sd, index );
  19234. // Tell the client we are done
  19235. clif_laphine_upgrade_result( sd, false );
  19236. #endif
  19237. }
  19238. void clif_enchantgrade_add( map_session_data& sd, uint16 index = UINT16_MAX, std::shared_ptr<s_enchantgradelevel> gradeLevel = nullptr ){
  19239. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19240. PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST*>( packet_buffer );
  19241. p->PacketType = HEADER_ZC_GRADE_ENCHANT_MATERIAL_LIST;
  19242. p->PacketLength = sizeof( *p );
  19243. if( index < UINT16_MAX ){
  19244. p->index = client_index( index );
  19245. p->success_chance = gradeLevel->chances[sd.inventory.u.items_inventory[index].refine] / 100;
  19246. p->blessing_info.id = client_nameid( gradeLevel->catalyst.item );
  19247. p->blessing_info.amount = gradeLevel->catalyst.amountPerStep;
  19248. p->blessing_info.max_blessing = gradeLevel->catalyst.maximumSteps;
  19249. p->blessing_info.bonus = gradeLevel->catalyst.chanceIncrease / 100;
  19250. // Not displayed by client
  19251. p->protect_itemid = 0;
  19252. p->protect_amount = 0;
  19253. int i = 0;
  19254. for( const auto& pair : gradeLevel->options ){
  19255. GRADE_ENCHANT_MATERIAL& mat = p->material_info[i];
  19256. std::shared_ptr<s_enchantgradeoption> option = pair.second;
  19257. mat.nameid = client_nameid( option->item );
  19258. mat.amount = option->amount;
  19259. mat.price = option->zeny;
  19260. mat.downgrade = option->downgrade_amount > 0;
  19261. mat.breakable = option->breaking_rate > 0;
  19262. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( mat ) );
  19263. i++;
  19264. }
  19265. }else{
  19266. p->index = -1;
  19267. p->success_chance = 0;
  19268. p->blessing_info.id = 0;
  19269. p->blessing_info.amount = 0;
  19270. p->blessing_info.max_blessing = 0;
  19271. p->blessing_info.bonus = 0;
  19272. p->protect_itemid = 0;
  19273. p->protect_amount = 0;
  19274. }
  19275. clif_send( p, p->PacketLength, &sd.bl, SELF );
  19276. #endif
  19277. }
  19278. void clif_parse_enchantgrade_add( int fd, map_session_data* sd ){
  19279. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19280. nullpo_retv( sd );
  19281. if( !sd->state.enchantgrade_open ){
  19282. return;
  19283. }
  19284. const PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT*>( RFIFOP( fd, 0 ) );
  19285. uint16 index = server_index( p->index );
  19286. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19287. return;
  19288. }
  19289. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19290. // Unsupported item type - no answer, because client should have actually prevented this request
  19291. if( enchantgrade == nullptr ){
  19292. return;
  19293. }
  19294. // Item can't be enhanced
  19295. if( !sd->inventory_data[index]->flag.gradable ){
  19296. return;
  19297. }
  19298. uint16 level = 0;
  19299. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19300. level = sd->inventory_data[index]->weapon_level;
  19301. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19302. level = sd->inventory_data[index]->armor_level;
  19303. }
  19304. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19305. // Cannot upgrade this weapon or armor level
  19306. if( enchantgradelevels == enchantgrade->levels.end() ){
  19307. clif_enchantgrade_add( *sd );
  19308. return;
  19309. }
  19310. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19311. // Cannot increase enchantgrade any further - no answer, because client should have actually prevented this request
  19312. if( enchantgradelevel == nullptr ){
  19313. return;
  19314. }
  19315. clif_enchantgrade_add( *sd, index, enchantgradelevel );
  19316. #endif
  19317. }
  19318. /// <summary>
  19319. /// Sends the result for trying to enchant an item
  19320. /// </summary>
  19321. /// <param name="sd">The player session</param>
  19322. /// <param name="index">The target item</param>
  19323. /// <param name="result">
  19324. /// 0= The grade has been successfully upgraded.
  19325. /// 1= Refinement failed.
  19326. /// 2= The refine level has decreased.
  19327. /// 3= Equipment destroyed.
  19328. /// 4= The equipment is protected.
  19329. /// </param>
  19330. void clif_enchantgrade_result( map_session_data& sd, uint16 index, e_enchantgrade_result result ){
  19331. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19332. PACKET_ZC_GRADE_ENCHANT_ACK p = {};
  19333. p.PacketType = HEADER_ZC_GRADE_ENCHANT_ACK;
  19334. p.index = client_index( index );
  19335. p.grade = sd.inventory.u.items_inventory[index].enchantgrade;
  19336. p.result = result;
  19337. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19338. #endif
  19339. }
  19340. void clif_enchantgrade_announce( map_session_data& sd, struct item& item, bool success ){
  19341. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19342. PACKET_ZC_GRADE_ENCHANT_BROADCAST_RESULT p = {};
  19343. p.packetType = HEADER_ZC_GRADE_ENCHANT_BROADCAST_RESULT;
  19344. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  19345. p.itemId = client_nameid( item.nameid );
  19346. p.grade = item.enchantgrade;
  19347. p.status = success;
  19348. clif_send( &p, sizeof( p ), nullptr, ALL_CLIENT );
  19349. #endif
  19350. }
  19351. void clif_parse_enchantgrade_start( int fd, map_session_data* sd ){
  19352. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19353. nullpo_retv( sd );
  19354. if( !sd->state.enchantgrade_open ){
  19355. return;
  19356. }
  19357. const PACKET_CZ_GRADE_ENCHANT_REQUEST* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_REQUEST*>( RFIFOP( fd, 0 ) );
  19358. uint16 index = server_index( p->index );
  19359. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19360. return;
  19361. }
  19362. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19363. // Unsupported item type - no answer
  19364. if( enchantgrade == nullptr ){
  19365. return;
  19366. }
  19367. uint16 level = 0;
  19368. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19369. level = sd->inventory_data[index]->weapon_level;
  19370. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19371. level = sd->inventory_data[index]->armor_level;
  19372. }
  19373. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19374. // Cannot upgrade this weapon or armor level - no answer
  19375. if( enchantgradelevels == enchantgrade->levels.end() ){
  19376. return;
  19377. }
  19378. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19379. // Cannot increase enchantgrade any further - no answer
  19380. if( enchantgradelevel == nullptr ){
  19381. return;
  19382. }
  19383. uint16 totalChance = enchantgradelevel->chances[sd->inventory.u.items_inventory[index].refine];
  19384. // No chance to increase the enchantgrade
  19385. if( totalChance == 0 ){
  19386. return;
  19387. }
  19388. std::shared_ptr<s_enchantgradeoption> option = util::map_find( enchantgradelevel->options, (uint16)p->material_index );
  19389. // Unknown option id - no answer
  19390. if( option == nullptr ){
  19391. return;
  19392. }
  19393. // Not enough zeny
  19394. if( sd->status.zeny < option->zeny ){
  19395. return;
  19396. }
  19397. uint16 steps = min( p->blessing_amount, enchantgradelevel->catalyst.maximumSteps );
  19398. std::unordered_map<uint16, uint16> requiredItems;
  19399. if( p->blessing_flag ){
  19400. // If the catalysator item is the same as the option item build the sum of amounts
  19401. if( enchantgradelevel->catalyst.item == option->item ){
  19402. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps + option->amount;
  19403. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19404. if( index < 0 ){
  19405. return;
  19406. }
  19407. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19408. return;
  19409. }
  19410. requiredItems[index] = amount;
  19411. }else{
  19412. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps;
  19413. // Check catalysator item
  19414. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19415. if( index < 0 ){
  19416. return;
  19417. }
  19418. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19419. return;
  19420. }
  19421. requiredItems[index] = amount;
  19422. // Check option item
  19423. index = pc_search_inventory( sd, option->item );
  19424. if( index < 0 ){
  19425. return;
  19426. }
  19427. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19428. return;
  19429. }
  19430. requiredItems[index] = option->amount;
  19431. }
  19432. totalChance += steps * enchantgradelevel->catalyst.chanceIncrease;
  19433. }else{
  19434. // Check option item
  19435. int16 index = pc_search_inventory( sd, option->item );
  19436. if( index < 0 ){
  19437. return;
  19438. }
  19439. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19440. return;
  19441. }
  19442. requiredItems[index] = option->amount;
  19443. }
  19444. // All items should be there, start deleting
  19445. for( const auto& pair : requiredItems ){
  19446. if( pc_delitem( sd, pair.first, pair.second, 0, 0, LOG_TYPE_ENCHANTGRADE ) != 0 ){
  19447. return;
  19448. }
  19449. }
  19450. if( pc_payzeny( sd, option->zeny, LOG_TYPE_ENCHANTGRADE ) > 0 ){
  19451. return;
  19452. }
  19453. if( rnd()%10000 < totalChance ){
  19454. // Log removal of item
  19455. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19456. // Increase enchantgrade
  19457. sd->inventory.u.items_inventory[index].enchantgrade = min( sd->inventory.u.items_inventory[index].enchantgrade + 1, MAX_ENCHANTGRADE );
  19458. // On successful enchantgrade increase the refine is reset
  19459. sd->inventory.u.items_inventory[index].refine = 0;
  19460. // Log retrieving the item again -> with the new refine and enchantgrade
  19461. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19462. // Show success
  19463. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_SUCCESS );
  19464. // Check if it has to be announced
  19465. if( enchantgradelevel->announceSuccess ){
  19466. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], true );
  19467. }
  19468. }else{
  19469. // Check if it has to be announced (has to be done before deleting the item from inventory)
  19470. if( enchantgradelevel->announceFail ){
  19471. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], false );
  19472. }
  19473. // Delete the item if it is breakable
  19474. if( option->breaking_rate > 0 && ( rnd() % 10000 ) < option->breaking_rate ){
  19475. // Delete the item
  19476. pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_ENCHANTGRADE );
  19477. // Show failure
  19478. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_BREAK );
  19479. // Downgrade the item if necessary
  19480. }else if( option->downgrade_amount > 0 ){
  19481. // Log removal of item
  19482. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19483. // Decrease refine level
  19484. sd->inventory.u.items_inventory[index].refine = cap_value( sd->inventory.u.items_inventory[index].refine - option->downgrade_amount, 0, MAX_REFINE );
  19485. // Log retrieving the item again -> with the new refine
  19486. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19487. // Show downgrade
  19488. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_DOWNGRADE );
  19489. // Only show failure, but dont do anything
  19490. }else{
  19491. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_FAILED );
  19492. }
  19493. }
  19494. #endif
  19495. }
  19496. void clif_parse_enchantgrade_close( int fd, map_session_data* sd ){
  19497. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19498. nullpo_retv( sd );
  19499. sd->state.enchantgrade_open = false;
  19500. #endif
  19501. }
  19502. void clif_reputation_type( map_session_data& sd, int64 type, int64 points ){
  19503. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19504. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19505. p->packetType = HEADER_ZC_REPUTE_INFO;
  19506. p->packetLength = sizeof( *p );
  19507. p->success = true;
  19508. PACKET_ZC_REPUTE_INFO_sub& entry = p->list[0];
  19509. entry.type = type;
  19510. entry.points = points;
  19511. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  19512. clif_send( p, p->packetLength, &sd.bl, SELF );
  19513. #endif
  19514. }
  19515. void clif_reputation_list( map_session_data& sd ){
  19516. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19517. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19518. p->packetType = HEADER_ZC_REPUTE_INFO;
  19519. p->packetLength = sizeof( *p );
  19520. p->success = true;
  19521. size_t index = 0;
  19522. for( const auto& entry : reputation_db ){
  19523. std::shared_ptr<s_reputation> reputation = entry.second;
  19524. PACKET_ZC_REPUTE_INFO_sub& list_entry = p->list[index];
  19525. list_entry.type = reputation->id;
  19526. list_entry.points = pc_readreg2( &sd, reputation->variable.c_str() );
  19527. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( list_entry ) );
  19528. index++;
  19529. }
  19530. clif_send( p, p->packetLength, &sd.bl, SELF );
  19531. #endif
  19532. }
  19533. void clif_item_reform_open( map_session_data& sd, t_itemid item ){
  19534. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19535. PACKET_ZC_OPEN_REFORM_UI p = {};
  19536. p.PacketType = HEADER_ZC_OPEN_REFORM_UI;
  19537. p.ITID = item;
  19538. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19539. sd.state.item_reform = item;
  19540. #endif
  19541. }
  19542. void clif_parse_item_reform_close( int fd, map_session_data* sd ){
  19543. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19544. sd->state.item_reform = 0;
  19545. #endif
  19546. }
  19547. void clif_item_reform_result( map_session_data& sd, uint16 index, uint8 result ){
  19548. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19549. PACKET_ZC_ITEM_REFORM_ACK p = {};
  19550. p.PacketType = HEADER_ZC_ITEM_REFORM_ACK;
  19551. p.index = client_index( index );
  19552. p.result = result;
  19553. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19554. if( result == 0 ){
  19555. // Client closes the window on success
  19556. sd.state.item_reform = 0;
  19557. }
  19558. #endif
  19559. }
  19560. void clif_parse_item_reform_start( int fd, map_session_data* sd ){
  19561. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19562. // Not opened
  19563. if( sd->state.item_reform == 0 ){
  19564. return;
  19565. }
  19566. const PACKET_CZ_ITEM_REFORM* p = reinterpret_cast<PACKET_CZ_ITEM_REFORM*>( RFIFOP( fd, 0 ) );
  19567. // Item mismatch
  19568. if( p->ITID != sd->state.item_reform ){
  19569. return;
  19570. }
  19571. uint16 index = server_index( p->index );
  19572. if( index >= MAX_INVENTORY ){
  19573. return;
  19574. }
  19575. if( sd->inventory_data[index] == nullptr ){
  19576. return;
  19577. }
  19578. std::shared_ptr<s_item_reform> reform = item_reform_db.find( sd->state.item_reform );
  19579. if( reform == nullptr ){
  19580. return;
  19581. }
  19582. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19583. std::shared_ptr<s_item_reform_base> base = util::umap_find( reform->base_items, selected_item.nameid );
  19584. if( base == nullptr ){
  19585. return;
  19586. }
  19587. // If target item is not identified
  19588. if( selected_item.identify == 0 ){
  19589. return;
  19590. }
  19591. // If target item is equipped
  19592. if( selected_item.equip != 0 ){
  19593. return;
  19594. }
  19595. // Check minimum refine requirement
  19596. if( selected_item.refine < base->minimumRefine ){
  19597. return;
  19598. }
  19599. // Check maximum refine requirement
  19600. if( selected_item.refine > base->maximumRefine ){
  19601. return;
  19602. }
  19603. // If no cards are allowed
  19604. if( !base->cardsAllowed ){
  19605. for( int i = 0; i < MAX_SLOTS; i++ ){
  19606. if( selected_item.card[i] != 0 ){
  19607. return;
  19608. }
  19609. }
  19610. }
  19611. // If random options are required
  19612. if( base->requiredRandomOptions > 0 ){
  19613. int i;
  19614. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19615. if( selected_item.option[i].id != 0 ){
  19616. break;
  19617. }
  19618. }
  19619. if( ( i + 1 ) < base->requiredRandomOptions ){
  19620. return;
  19621. }
  19622. }
  19623. std::unordered_map<uint16, uint16> materials;
  19624. // Check if all materials exist
  19625. for( const auto& material : base->materials ){
  19626. int16 material_index = pc_search_inventory( sd, material.first );
  19627. if( material_index < 0 ){
  19628. return;
  19629. }
  19630. if( sd->inventory.u.items_inventory[material_index].amount < material.second ){
  19631. return;
  19632. }
  19633. materials[material_index] = material.second;
  19634. }
  19635. // Remove the material
  19636. for( const auto& material : materials ){
  19637. if( pc_delitem( sd, material.first, material.second, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19638. return;
  19639. }
  19640. }
  19641. // If triggered from item
  19642. if( sd->itemid == sd->state.item_reform && pc_delitem( sd, sd->itemindex, 1, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19643. return;
  19644. }
  19645. // Log removal of item
  19646. log_pick_pc( sd, LOG_TYPE_REFORM, -1, &selected_item );
  19647. // Visually remove it from the client
  19648. clif_delitem( *sd, index, 1, 0 );
  19649. // Apply the random options
  19650. if( base->randomOptionGroup != nullptr ){
  19651. base->randomOptionGroup->apply( selected_item );
  19652. }
  19653. // Change the refine rate if needed
  19654. if( base->refineChange != 0 ){
  19655. selected_item.refine = cap_value( selected_item.refine + base->refineChange, 0, MAX_REFINE );
  19656. }
  19657. // Remove all cards and socket enchants
  19658. if( base->clearSlots ){
  19659. for( int i = 0; i < MAX_SLOTS; i++ ){
  19660. selected_item.card[i] = 0;
  19661. }
  19662. }
  19663. // Remove the current enchantgrade
  19664. if( base->removeEnchantgrade ){
  19665. selected_item.enchantgrade = 0;
  19666. }
  19667. // Finally change the item id
  19668. selected_item.nameid = base->resultItemId;
  19669. // Link inventory data cache to the new item
  19670. sd->inventory_data[index] = itemdb_search( base->resultItemId );
  19671. // Log retrieving the item again -> with the new options
  19672. log_pick_pc( sd, LOG_TYPE_REFORM, 1, &selected_item );
  19673. // Make it visible for the client again
  19674. clif_additem( sd, index, 1, 0 );
  19675. clif_item_reform_result( *sd, index, 0 );
  19676. #endif
  19677. }
  19678. void clif_enchantwindow_open( map_session_data& sd, uint64 clientLuaIndex ){
  19679. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19680. // Hardcoded clientside check
  19681. if( sd.weight > ( ( sd.max_weight * 70 ) / 100 ) ){
  19682. clif_msg_color( &sd, MSI_ENCHANT_FAILED_OVER_WEIGHT, color_table[COLOR_RED] );
  19683. sd.state.item_enchant_index = 0;
  19684. return;
  19685. }
  19686. PACKET_ZC_UI_OPEN_V3 p = {};
  19687. p.packetType = HEADER_ZC_UI_OPEN_V3;
  19688. p.type = OUT_UI_ENCHANT;
  19689. p.data = clientLuaIndex;
  19690. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19691. sd.state.item_enchant_index = clientLuaIndex;
  19692. #endif
  19693. }
  19694. void clif_enchantwindow_result( map_session_data& sd, bool success, t_itemid enchant = 0 ){
  19695. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19696. PACKET_ZC_RESPONSE_ENCHANT p = {};
  19697. p.PacketType = HEADER_ZC_RESPONSE_ENCHANT;
  19698. if( success ){
  19699. p.msgId = MSI_ENCHANT_SUCCESS;
  19700. }else{
  19701. p.msgId = MSI_ENCHANT_FAILED;
  19702. }
  19703. p.ITID = enchant;
  19704. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19705. sd.state.item_enchant_index = 0;
  19706. #endif
  19707. }
  19708. bool clif_parse_enchant_basecheck( struct item& selected_item, std::shared_ptr<s_item_enchant> enchant ){
  19709. if( selected_item.equip != 0 ){
  19710. return false;
  19711. }
  19712. if( selected_item.equipSwitch != 0 ){
  19713. return false;
  19714. }
  19715. if( selected_item.attribute != 0 ){
  19716. return false;
  19717. }
  19718. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  19719. return false;
  19720. }
  19721. if( selected_item.refine < enchant->minimumRefine ){
  19722. return false;
  19723. }
  19724. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  19725. return false;
  19726. }
  19727. if( !enchant->allowRandomOptions ){
  19728. for( const s_item_randomoption& option : selected_item.option ){
  19729. if( option.id != 0 ){
  19730. return false;
  19731. }
  19732. }
  19733. }
  19734. return true;
  19735. }
  19736. void clif_parse_enchantwindow_general( int fd, map_session_data* sd ){
  19737. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19738. const PACKET_CZ_REQUEST_RANDOM_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RANDOM_ENCHANT*>( RFIFOP( fd, 0 ) );
  19739. if( sd->state.item_enchant_index != p->enchant_group ){
  19740. return;
  19741. }
  19742. uint16 index = server_index( p->index );
  19743. if( index >= MAX_INVENTORY ){
  19744. return;
  19745. }
  19746. if( sd->inventory_data[index] == nullptr ){
  19747. return;
  19748. }
  19749. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19750. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19751. if( enchant == nullptr ){
  19752. return;
  19753. }
  19754. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19755. return;
  19756. }
  19757. uint16 slot = MAX_SLOTS;
  19758. for( uint16 nextSlot : enchant->order ){
  19759. if( selected_item.card[nextSlot] == 0 ){
  19760. slot = nextSlot;
  19761. break;
  19762. }
  19763. }
  19764. if( slot == MAX_SLOTS ){
  19765. return;
  19766. }
  19767. if( slot < sd->inventory_data[index]->slots ){
  19768. return;
  19769. }
  19770. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19771. if( enchant_slot == nullptr ){
  19772. return;
  19773. }
  19774. std::shared_ptr<s_item_enchant_normal> enchants_for_enchantgrade = util::umap_find( enchant_slot->normal.enchants, (uint16)selected_item.enchantgrade );
  19775. if( enchants_for_enchantgrade == nullptr ){
  19776. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 829 ), false, SELF); // Enchanting is not possible for your item's enchant grade.
  19777. clif_enchantwindow_result( *sd, false );
  19778. return;
  19779. }
  19780. if( sd->status.zeny < enchant_slot->normal.zeny ){
  19781. return;
  19782. }
  19783. std::unordered_map<uint16, uint16> materials;
  19784. for( const auto& entry : enchant_slot->normal.materials ){
  19785. int16 idx = pc_search_inventory( sd, entry.first );
  19786. if( idx < 0 ){
  19787. return;
  19788. }
  19789. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19790. return;
  19791. }
  19792. materials[idx] = entry.second;
  19793. }
  19794. if( pc_payzeny( sd, enchant_slot->normal.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19795. return;
  19796. }
  19797. for( const auto& entry : materials ){
  19798. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19799. return;
  19800. }
  19801. }
  19802. uint32 chance = enchant_slot->normal.chance;
  19803. for( int i = 0; i <= MAX_ENCHANTGRADE; i++ ){
  19804. chance += enchant_slot->normal.enchantgradeChanceIncrease[i];
  19805. }
  19806. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  19807. clif_enchantwindow_result( *sd, false );
  19808. return;
  19809. }
  19810. // Log removal of item
  19811. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19812. size_t maximum = 3 * enchant_slot->normal.enchants.size();
  19813. bool enchanted = false;
  19814. for( int i = 0; i < maximum; i++ ){
  19815. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  19816. if( rnd_value( 0, 10000 ) < normal_enchant->chance ){
  19817. selected_item.card[slot] = normal_enchant->item_id;
  19818. enchanted = true;
  19819. break;
  19820. }
  19821. }
  19822. if( !enchanted ){
  19823. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  19824. selected_item.card[slot] = normal_enchant->item_id;
  19825. }
  19826. // Log retrieving the item again -> with the new enchant
  19827. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  19828. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  19829. #endif
  19830. }
  19831. void clif_parse_enchantwindow_perfect( int fd, map_session_data* sd ){
  19832. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19833. const PACKET_CZ_REQUEST_PERFECT_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_PERFECT_ENCHANT*>( RFIFOP( fd, 0 ) );
  19834. if( sd->state.item_enchant_index != p->enchant_group ){
  19835. return;
  19836. }
  19837. uint16 index = server_index( p->index );
  19838. if( index >= MAX_INVENTORY ){
  19839. return;
  19840. }
  19841. if( sd->inventory_data[index] == nullptr ){
  19842. return;
  19843. }
  19844. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19845. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19846. if( enchant == nullptr ){
  19847. return;
  19848. }
  19849. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19850. return;
  19851. }
  19852. uint16 slot = MAX_SLOTS;
  19853. for( uint16 nextSlot : enchant->order ){
  19854. if( selected_item.card[nextSlot] == 0 ){
  19855. slot = nextSlot;
  19856. break;
  19857. }
  19858. }
  19859. if( slot == MAX_SLOTS ){
  19860. return;
  19861. }
  19862. if( slot < sd->inventory_data[index]->slots ){
  19863. return;
  19864. }
  19865. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19866. if( enchant_slot == nullptr ){
  19867. return;
  19868. }
  19869. std::shared_ptr<s_item_enchant_perfect> perfect_enchant = util::umap_find( enchant_slot->perfect.enchants, p->ITID );
  19870. if( perfect_enchant == nullptr ){
  19871. return;
  19872. }
  19873. if( sd->status.zeny < perfect_enchant->zeny ){
  19874. return;
  19875. }
  19876. std::unordered_map<uint16, uint16> materials;
  19877. for( const auto& entry : perfect_enchant->materials ){
  19878. int16 idx = pc_search_inventory( sd, entry.first );
  19879. if( idx < 0 ){
  19880. return;
  19881. }
  19882. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19883. return;
  19884. }
  19885. materials[idx] = entry.second;
  19886. }
  19887. if( pc_payzeny( sd, perfect_enchant->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19888. return;
  19889. }
  19890. for( const auto& entry : materials ){
  19891. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19892. return;
  19893. }
  19894. }
  19895. // Log removal of item
  19896. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19897. selected_item.card[slot] = perfect_enchant->item_id;
  19898. // Log retrieving the item again -> with the new enchant
  19899. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  19900. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  19901. #endif
  19902. }
  19903. void clif_parse_enchantwindow_upgrade( int fd, map_session_data* sd ){
  19904. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19905. const PACKET_CZ_REQUEST_UPGRADE_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_UPGRADE_ENCHANT*>( RFIFOP( fd, 0 ) );
  19906. if( sd->state.item_enchant_index != p->enchant_group ){
  19907. return;
  19908. }
  19909. uint16 index = server_index( p->index );
  19910. if( index >= MAX_INVENTORY ){
  19911. return;
  19912. }
  19913. if( sd->inventory_data[index] == nullptr ){
  19914. return;
  19915. }
  19916. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19917. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19918. if( enchant == nullptr ){
  19919. return;
  19920. }
  19921. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19922. return;
  19923. }
  19924. uint16 slot = p->slot;
  19925. if( slot >= MAX_SLOTS ){
  19926. return;
  19927. }
  19928. if( slot < sd->inventory_data[index]->slots ){
  19929. return;
  19930. }
  19931. if( selected_item.card[slot] == 0 ){
  19932. return;
  19933. }
  19934. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19935. if( enchant_slot == nullptr ){
  19936. return;
  19937. }
  19938. std::shared_ptr<s_item_enchant_upgrade> upgrade = util::umap_find( enchant_slot->upgrade.enchants, selected_item.card[slot] );
  19939. if( upgrade == nullptr ){
  19940. return;
  19941. }
  19942. if( sd->status.zeny < upgrade->zeny ){
  19943. return;
  19944. }
  19945. std::unordered_map<uint16, uint16> materials;
  19946. for( const auto& entry : upgrade->materials ){
  19947. int16 idx = pc_search_inventory( sd, entry.first );
  19948. if( idx < 0 ){
  19949. return;
  19950. }
  19951. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19952. return;
  19953. }
  19954. materials[idx] = entry.second;
  19955. }
  19956. if( pc_payzeny( sd, upgrade->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19957. return;
  19958. }
  19959. for( const auto& entry : materials ){
  19960. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19961. return;
  19962. }
  19963. }
  19964. // Log removal of item
  19965. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19966. selected_item.card[slot] = upgrade->upgrade_item_id;
  19967. // Log retrieving the item again -> with the new enchant
  19968. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  19969. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  19970. #endif
  19971. }
  19972. void clif_parse_enchantwindow_reset( int fd, map_session_data* sd ){
  19973. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19974. const PACKET_CZ_REQUEST_RESET_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RESET_ENCHANT*>( RFIFOP( fd, 0 ) );
  19975. if( sd->state.item_enchant_index != p->enchant_group ){
  19976. return;
  19977. }
  19978. uint16 index = server_index( p->index );
  19979. if( index >= MAX_INVENTORY ){
  19980. return;
  19981. }
  19982. if( sd->inventory_data[index] == nullptr ){
  19983. return;
  19984. }
  19985. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19986. if( selected_item.equip != 0 ){
  19987. return;
  19988. }
  19989. if( selected_item.equipSwitch != 0 ){
  19990. return;
  19991. }
  19992. if( selected_item.attribute != 0 ){
  19993. return;
  19994. }
  19995. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19996. if( enchant == nullptr ){
  19997. return;
  19998. }
  19999. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  20000. return;
  20001. }
  20002. if( selected_item.refine < enchant->minimumRefine ){
  20003. return;
  20004. }
  20005. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  20006. return;
  20007. }
  20008. bool is_enchanted = false;
  20009. for( int i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20010. if( selected_item.card[i] != 0 ){
  20011. is_enchanted = true;
  20012. break;
  20013. }
  20014. }
  20015. if( !is_enchanted ){
  20016. return;
  20017. }
  20018. if( sd->status.zeny < enchant->reset.zeny ){
  20019. return;
  20020. }
  20021. std::unordered_map<uint16, uint16> materials;
  20022. for( const auto& entry : enchant->reset.materials ){
  20023. int16 idx = pc_search_inventory( sd, entry.first );
  20024. if( idx < 0 ){
  20025. return;
  20026. }
  20027. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20028. return;
  20029. }
  20030. materials[idx] = entry.second;
  20031. }
  20032. if( pc_payzeny( sd, enchant->reset.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20033. return;
  20034. }
  20035. for( const auto& entry : materials ){
  20036. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20037. return;
  20038. }
  20039. }
  20040. uint32 chance = enchant->reset.chance;
  20041. if( chance == 0 ){
  20042. return;
  20043. }
  20044. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  20045. clif_enchantwindow_result( *sd, false );
  20046. return;
  20047. }
  20048. // Log removal of item
  20049. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20050. for( int i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20051. selected_item.card[i] = 0;
  20052. }
  20053. // Log retrieving the item again -> with the new enchant
  20054. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20055. clif_enchantwindow_result( *sd, true );
  20056. #endif
  20057. }
  20058. void clif_parse_enchantwindow_close( int fd, map_session_data* sd ){
  20059. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20060. sd->state.item_enchant_index = 0;
  20061. #endif
  20062. }
  20063. void clif_parse_itempackage_select( int fd, map_session_data* sd ){
  20064. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220316
  20065. const PACKET_CZ_USE_PACKAGEITEM* p = reinterpret_cast<PACKET_CZ_USE_PACKAGEITEM*>( RFIFOP( fd, 0 ) );
  20066. if( p->AID != sd->status.account_id ){
  20067. return;
  20068. }
  20069. uint16 index = server_index( p->index );
  20070. if( index >= MAX_INVENTORY ){
  20071. return;
  20072. }
  20073. if( sd->inventory_data[index] == nullptr ){
  20074. return;
  20075. }
  20076. if( sd->inventory_data[index]->nameid != p->itemID ){
  20077. return;
  20078. }
  20079. if( sd->inventory_data[index]->elv > sd->status.base_level ){
  20080. return;
  20081. }
  20082. std::shared_ptr<s_item_package> package = item_package_db.find( p->itemID );
  20083. if( package == nullptr ){
  20084. return;
  20085. }
  20086. std::shared_ptr<s_item_package_group> group = util::umap_find( package->groups, p->BoxIndex );
  20087. if( group == nullptr ){
  20088. return;
  20089. }
  20090. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_PACKAGE ) != 0 ){
  20091. return;
  20092. }
  20093. for( const auto& entry : group->items ){
  20094. struct item item = {};
  20095. item.nameid = entry.second->item_id;
  20096. item.identify = 1;
  20097. item.refine = (char)entry.second->refine;
  20098. if( entry.second->rentalhours ){
  20099. item.expire_time = (uint32)( time( nullptr ) + entry.second->rentalhours * 3600 );
  20100. }
  20101. // Check if it is a pet egg
  20102. std::shared_ptr<s_pet_db> pet = pet_db_search( item.nameid, PET_EGG );
  20103. if( pet != nullptr ){
  20104. for( int i = 0; i < entry.second->amount; i++ ){
  20105. pet_create_egg( sd, item.nameid );
  20106. }
  20107. }else if( entry.second->amount > 1 && ( !itemdb_isstackable( item.nameid ) || item.expire_time > 0 ) ){
  20108. for( int i = 0; i < entry.second->amount; i++ ){
  20109. // New random options on each iteration
  20110. if( entry.second->randomOptionGroup != nullptr ){
  20111. entry.second->randomOptionGroup->apply( item );
  20112. }
  20113. pc_additem( sd, &item, 1, LOG_TYPE_PACKAGE );
  20114. }
  20115. }else{
  20116. if( entry.second->randomOptionGroup != nullptr ){
  20117. entry.second->randomOptionGroup->apply( item );
  20118. }
  20119. pc_additem( sd, &item, entry.second->amount, LOG_TYPE_PACKAGE );
  20120. }
  20121. }
  20122. #endif
  20123. }
  20124. void clif_broadcast_refine_result(map_session_data& sd, t_itemid itemId, int8 level, bool success)
  20125. {
  20126. #if PACKETVER_MAIN_NUM >= 20170906 || PACKETVER_RE_NUM >= 20170830 || defined(PACKETVER_ZERO)
  20127. PACKET_ZC_BROADCAST_ITEMREFINING_RESULT p{};
  20128. p.packetType = HEADER_ZC_BROADCAST_ITEMREFINING_RESULT;
  20129. p.itemId = itemId;
  20130. p.refine_level = level;
  20131. p.status = (int8)success;
  20132. if (battle_config.broadcast_hide_name) {
  20133. std::string dispname = clif_hide_name(sd.status.name);
  20134. safestrncpy(p.name, dispname.c_str(), sizeof(p.name));
  20135. }
  20136. else {
  20137. safestrncpy(p.name, sd.status.name, sizeof(p.name));
  20138. }
  20139. clif_send(&p, sizeof(p), &sd.bl, ALL_CLIENT);
  20140. #endif
  20141. }
  20142. void clif_parse_captcha_register(int fd, map_session_data *sd) {
  20143. #if PACKETVER >= 20160316
  20144. nullpo_retv(sd);
  20145. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20146. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20147. return;
  20148. }
  20149. const PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20150. pc_macro_captcha_register(*sd, p->imageSize, p->answer);
  20151. #endif
  20152. }
  20153. void clif_captcha_upload_request(map_session_data &sd) {
  20154. #if PACKETVER >= 20160330
  20155. PACKET_ZC_ACK_UPLOAD_MACRO_DETECTOR p = {};
  20156. p.PacketType = HEADER_ZC_ACK_UPLOAD_MACRO_DETECTOR;
  20157. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20158. if (sd.captcha_upload.cd != nullptr) {
  20159. p.captchaFlag = 0;
  20160. } else {
  20161. p.captchaFlag = 1;
  20162. }
  20163. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20164. #endif
  20165. }
  20166. void clif_parse_captcha_upload(int fd, map_session_data *sd) {
  20167. #if PACKETVER >= 20160316
  20168. nullpo_retv(sd);
  20169. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20170. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20171. return;
  20172. }
  20173. const PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20174. int16 upload_size = p->PacketLength - sizeof( * p );
  20175. if (upload_size < 1 || upload_size > MAX_CAPTCHA_CHUNK_SIZE)
  20176. return;
  20177. if (sd->captcha_upload.upload_size + upload_size > sd->captcha_upload.cd->image_size)
  20178. return;
  20179. pc_macro_captcha_register_upload(*sd, upload_size, p->imageData);
  20180. #endif
  20181. }
  20182. void clif_captcha_upload_end(map_session_data &sd) {
  20183. #if PACKETVER >= 20160330
  20184. PACKET_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA p = {};
  20185. p.PacketType = HEADER_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA;
  20186. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20187. #endif
  20188. }
  20189. void clif_parse_captcha_preview_request(int fd, map_session_data *sd) {
  20190. #if PACKETVER >= 20160323
  20191. nullpo_retv(sd);
  20192. if (!(pc_has_permission(sd, PC_PERM_MACRO_REGISTER) && pc_has_permission(sd, PC_PERM_MACRO_DETECT))) {
  20193. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20194. return;
  20195. }
  20196. const PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20197. clif_captcha_preview_response(*sd, captcha_db.find(p->captchaID));
  20198. #endif
  20199. }
  20200. void clif_captcha_preview_response(map_session_data &sd, std::shared_ptr<s_captcha_data> cd) {
  20201. #if PACKETVER >= 20160330
  20202. PACKET_ZC_ACK_PREVIEW_MACRO_DETECTOR p = {};
  20203. p.PacketType = HEADER_ZC_ACK_PREVIEW_MACRO_DETECTOR;
  20204. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20205. if (cd == nullptr) {
  20206. p.captchaFlag = 1;
  20207. p.imageSize = 0;
  20208. } else {
  20209. p.captchaFlag = 0;
  20210. p.imageSize = cd->image_size;
  20211. }
  20212. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20213. if (cd != nullptr) {
  20214. for (uint16 offset = 0; offset < cd->image_size;) {
  20215. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20216. PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20217. p2->PacketType = HEADER_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA;
  20218. p2->PacketLength = sizeof( *p2 );
  20219. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20220. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20221. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20222. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20223. offset += chunk_size;
  20224. }
  20225. }
  20226. #endif
  20227. }
  20228. void clif_macro_detector_request(map_session_data &sd) {
  20229. #if PACKETVER >= 20160330
  20230. std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  20231. if (cd == nullptr) {
  20232. return;
  20233. }
  20234. // Send preview initialization request to the client.
  20235. PACKET_ZC_APPLY_MACRO_DETECTOR p = {};
  20236. p.PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR;
  20237. p.imageSize = cd->image_size;
  20238. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20239. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20240. for (uint16 offset = 0; offset < cd->image_size;) {
  20241. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20242. PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20243. p2->PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR_CAPTCHA;
  20244. p2->PacketLength = sizeof( *p2 );
  20245. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20246. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20247. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20248. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20249. offset += chunk_size;
  20250. }
  20251. #endif
  20252. }
  20253. void clif_macro_detector_request_show(map_session_data &sd) {
  20254. #if PACKETVER >= 20160330
  20255. PACKET_ZC_REQ_ANSWER_MACRO_DETECTOR p = {};
  20256. p.PacketType = HEADER_ZC_REQ_ANSWER_MACRO_DETECTOR;
  20257. p.retryCount = sd.macro_detect.retry;
  20258. p.timeout = battle_config.macro_detection_timeout;
  20259. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20260. #endif
  20261. }
  20262. void clif_parse_macro_detector_download_ack(int fd, map_session_data *sd) {
  20263. #if PACKETVER >= 20160316
  20264. nullpo_retv(sd);
  20265. if (sd->macro_detect.retry != 0) {
  20266. //const PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20267. clif_macro_detector_request_show(*sd);
  20268. }
  20269. #endif
  20270. }
  20271. void clif_parse_macro_detector_answer(int fd, map_session_data *sd) {
  20272. #if PACKETVER >= 20160316
  20273. nullpo_retv(sd);
  20274. const PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20275. pc_macro_detector_process_answer(*sd, p->answer);
  20276. #endif
  20277. }
  20278. void clif_macro_detector_status(map_session_data &sd, e_macro_detect_status stype) {
  20279. #if PACKETVER >= 20160330
  20280. PACKET_ZC_CLOSE_MACRO_DETECTOR p = {};
  20281. p.PacketType = HEADER_ZC_CLOSE_MACRO_DETECTOR;
  20282. p.status = stype;
  20283. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20284. #endif
  20285. }
  20286. void clif_parse_macro_reporter_select(int fd, map_session_data *sd) {
  20287. #if PACKETVER >= 20160330
  20288. nullpo_retv(sd);
  20289. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20290. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20291. return;
  20292. }
  20293. const PACKET_CZ_REQ_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_CZ_REQ_PLAYER_AID_IN_RANGE*>( RFIFOP( fd, 0 ) );
  20294. pc_macro_reporter_area_select(*sd, p->xPos, p->yPos, p->RadiusRange);
  20295. #endif
  20296. }
  20297. void clif_macro_reporter_select(map_session_data &sd, const std::vector<uint32> &aid_list) {
  20298. #if PACKETVER >= 20160330
  20299. PACKET_ZC_ACK_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_ZC_ACK_PLAYER_AID_IN_RANGE*>( packet_buffer );
  20300. p->PacketType = HEADER_ZC_ACK_PLAYER_AID_IN_RANGE;
  20301. p->PacketLength = sizeof( *p );
  20302. for (size_t i = 0; i < aid_list.size(); i++){
  20303. p->AID[i] = aid_list[static_cast<int32>(i)];
  20304. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->AID[0] ) );
  20305. }
  20306. clif_send(p, p->PacketLength, &sd.bl, SELF);
  20307. #endif
  20308. }
  20309. void clif_parse_macro_reporter_ack(int fd, map_session_data *sd) {
  20310. #if PACKETVER >= 20160316
  20311. nullpo_retv(sd);
  20312. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20313. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20314. return;
  20315. }
  20316. const PACKET_CZ_REQ_APPLY_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20317. map_session_data *tsd = map_id2sd(p->AID);
  20318. if (tsd == nullptr) {
  20319. clif_displaymessage(fd, msg_txt(sd, 3)); // Character not found.
  20320. return;
  20321. }
  20322. if (tsd->macro_detect.retry != 0) {
  20323. clif_macro_reporter_status(*sd, MCR_INPROGRESS);
  20324. return;
  20325. }
  20326. if (captcha_db.empty()) {
  20327. clif_macro_reporter_status(*sd, MCR_NO_DATA);
  20328. return;
  20329. }
  20330. pc_macro_reporter_process(*tsd, sd->status.account_id);
  20331. clif_macro_reporter_status(*sd, MCR_MONITORING);
  20332. #endif
  20333. }
  20334. void clif_macro_reporter_status(map_session_data &sd, e_macro_report_status stype) {
  20335. #if PACKETVER >= 20160330
  20336. PACKET_ZC_ACK_APPLY_MACRO_DETECTOR p = {};
  20337. p.PacketType = HEADER_ZC_ACK_APPLY_MACRO_DETECTOR;
  20338. p.status = stype;
  20339. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20340. #endif
  20341. }
  20342. void clif_dynamicnpc_result( map_session_data& sd, e_dynamicnpc_result result ){
  20343. #if PACKETVER_MAIN_NUM >= 20140430 || PACKETVER_RE_NUM >= 20140430 || defined(PACKETVER_ZERO)
  20344. PACKET_ZC_DYNAMICNPC_CREATE_RESULT p = {};
  20345. p.PacketType = HEADER_ZC_DYNAMICNPC_CREATE_RESULT;
  20346. p.result = result;
  20347. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20348. #endif
  20349. }
  20350. void clif_partybooking_ask( map_session_data* sd, map_session_data* joining_sd ){
  20351. #if PACKETVER >= 20191204
  20352. PACKET_ZC_PARTY_REQ_MASTER_TO_JOIN p = {};
  20353. p.packetType = HEADER_ZC_PARTY_REQ_MASTER_TO_JOIN;
  20354. p.CID = joining_sd->status.char_id;
  20355. p.AID = joining_sd->status.account_id;
  20356. safestrncpy( p.name, joining_sd->status.name, NAME_LENGTH );
  20357. p.x = joining_sd->status.base_level;
  20358. p.y = joining_sd->status.class_;
  20359. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20360. #endif
  20361. }
  20362. void clif_parse_partybooking_join( int fd, map_session_data* sd ){
  20363. #if PACKETVER >= 20191204
  20364. const PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20365. // Character is already in a party
  20366. if( sd->status.party_id != 0 ){
  20367. return;
  20368. }
  20369. map_session_data* tsd = map_charid2sd( p->CID );
  20370. // Target player is offline
  20371. if( tsd == nullptr ){
  20372. return;
  20373. }
  20374. if( tsd->status.account_id != p->AID ){
  20375. return;
  20376. }
  20377. struct s_party_booking_requirement requirement;
  20378. if( !party_booking_load( tsd->status.account_id, tsd->status.char_id, &requirement ) ){
  20379. return;
  20380. }
  20381. if( sd->status.base_level < requirement.minimum_level ){
  20382. return;
  20383. }
  20384. if( sd->status.base_level > requirement.maximum_level ){
  20385. return;
  20386. }
  20387. // Already requested to join this party
  20388. if( util::vector_exists( tsd->party_booking_requests, sd->status.char_id ) ){
  20389. return;
  20390. }
  20391. // Store information that the player tried to join the party
  20392. tsd->party_booking_requests.push_back( sd->status.char_id );
  20393. clif_partybooking_ask( tsd, sd );
  20394. #endif
  20395. }
  20396. void clif_partybooking_reply( map_session_data* sd, map_session_data* party_leader_sd, bool accepted ){
  20397. #if PACKETVER >= 20191204
  20398. PACKET_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER p = {};
  20399. if( party_leader_sd->status.party_id == 0 ){
  20400. return;
  20401. }
  20402. struct party_data* party = party_search( party_leader_sd->status.party_id );
  20403. if( party == nullptr ){
  20404. return;
  20405. }
  20406. p.packetType = HEADER_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER;
  20407. safestrncpy( p.player_name, party_leader_sd->status.name, NAME_LENGTH );
  20408. safestrncpy( p.party_name, party->party.name, NAME_LENGTH );
  20409. p.AID = party_leader_sd->status.account_id;
  20410. p.refused = !accepted;
  20411. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20412. #endif
  20413. }
  20414. void clif_parse_partybooking_reply( int fd, map_session_data* sd ){
  20415. #if PACKETVER >= 20191204
  20416. const PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20417. map_session_data* tsd = map_charid2sd( p->CID );
  20418. // Target player is offline
  20419. if( tsd == nullptr ){
  20420. return;
  20421. }
  20422. if( tsd->status.account_id != p->AID ){
  20423. return;
  20424. }
  20425. // Check if the player even requested to join the party
  20426. if( !util::vector_exists( sd->party_booking_requests, p->CID ) ){
  20427. return;
  20428. }
  20429. util::vector_erase_if_exists( sd->party_booking_requests, p->CID );
  20430. // Only party leaders can reply
  20431. if( !party_isleader( sd ) ){
  20432. return;
  20433. }
  20434. struct party_data* party = party_search( sd->status.party_id );
  20435. if( party == nullptr ){
  20436. return;
  20437. }
  20438. if( party->instance_id > 0 && battle_config.instance_block_invite ){
  20439. return;
  20440. }
  20441. if( p->accept ){
  20442. party_join( *tsd, sd->status.party_id );
  20443. }
  20444. clif_partybooking_reply( tsd, sd, p->accept );
  20445. #endif
  20446. }
  20447. void clif_parse_reset_skill( int fd, map_session_data* sd ){
  20448. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220203
  20449. const PACKET_CZ_RESET_SKILL* p = reinterpret_cast<PACKET_CZ_RESET_SKILL*>( RFIFOP( fd, 0 ) );
  20450. #endif
  20451. }
  20452. void clif_set_dialog_align(map_session_data& sd, int npcid, e_say_dialog_align align)
  20453. {
  20454. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20455. PACKET_ZC_DIALOG_TEXT_ALIGN p = {};
  20456. p.PacketType = HEADER_ZC_DIALOG_TEXT_ALIGN;
  20457. p.align = align;
  20458. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20459. #endif // PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20460. }
  20461. void clif_set_npc_window_size(map_session_data& sd, int width, int height)
  20462. {
  20463. #if PACKETVER_MAIN_NUM >= 20220504
  20464. PACKET_ZC_DIALOG_WINDOW_SIZE p = {};
  20465. p.PacketType = HEADER_ZC_DIALOG_WINDOW_SIZE;
  20466. p.width = width;
  20467. p.height = height;
  20468. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20469. #endif // PACKETVER_MAIN_NUM >= 20220504
  20470. }
  20471. void clif_set_npc_window_pos(map_session_data& sd, int x, int y)
  20472. {
  20473. #if PACKETVER_MAIN_NUM >= 20220504
  20474. PACKET_ZC_DIALOG_WINDOW_POS p = {};
  20475. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS;
  20476. p.x = x;
  20477. p.y = y;
  20478. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20479. #endif // PACKETVER_MAIN_NUM >= 20220504
  20480. }
  20481. void clif_set_npc_window_pos_percent(map_session_data& sd, int x, int y)
  20482. {
  20483. #if PACKETVER_MAIN_NUM >= 20220504
  20484. PACKET_ZC_DIALOG_WINDOW_POS2 p = {};
  20485. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS2;
  20486. p.x = x;
  20487. p.y = y;
  20488. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20489. #endif // PACKETVER_MAIN_NUM >= 20220504
  20490. }
  20491. /// Displays a special popup.
  20492. /// Works only if player moved from one map to another.
  20493. /// 0bbe <popup id>.L (ZC_SPECIALPOPUP)
  20494. void clif_specialpopup(map_session_data& sd, int32 id ){
  20495. #if PACKETVER >= 20221005
  20496. PACKET_ZC_SPECIALPOPUP p = {};
  20497. p.PacketType = HEADER_ZC_SPECIALPOPUP;
  20498. p.ppId = id;
  20499. clif_send( &p, sizeof( p ), &sd.bl, SELF);
  20500. #endif
  20501. }
  20502. /*==========================================
  20503. * Main client packet processing function
  20504. *------------------------------------------*/
  20505. static int clif_parse(int fd)
  20506. {
  20507. int cmd, packet_len;
  20508. TBL_PC* sd;
  20509. int pnum;
  20510. #ifdef PACKET_OBFUSCATION
  20511. int cmd2;
  20512. #endif
  20513. //TODO apply delays or disconnect based on packet throughput [FlavioJS]
  20514. // Note: "click masters" can do 80+ clicks in 10 seconds
  20515. 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]
  20516. { // begin main client packet processing loop
  20517. sd = (TBL_PC *)session[fd]->session_data;
  20518. if (session[fd]->flag.eof) {
  20519. if (sd) {
  20520. if (sd->state.autotrade) {
  20521. //Disassociate character from the socket connection.
  20522. session[fd]->session_data = nullptr;
  20523. sd->fd = 0;
  20524. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off (using @autotrade).\n", sd->status.name);
  20525. } else
  20526. if (sd->state.active) {
  20527. // Player logout display [Valaris]
  20528. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off.\n", sd->status.name);
  20529. clif_quitsave(fd, sd);
  20530. } else {
  20531. //Unusual logout (during log on/off/map-changer procedure)
  20532. ShowInfo("Player AID:%d/CID:%d logged off.\n", sd->status.account_id, sd->status.char_id);
  20533. map_quit(sd);
  20534. }
  20535. } else {
  20536. ShowInfo("Closed connection from '" CL_WHITE "%s" CL_RESET "'.\n", ip2str(session[fd]->client_addr, nullptr));
  20537. }
  20538. do_close(fd);
  20539. return 0;
  20540. }
  20541. if (RFIFOREST(fd) < 2)
  20542. return 0;
  20543. cmd = RFIFOW(fd, 0);
  20544. #ifdef PACKET_OBFUSCATION
  20545. // Check if it is a player that tries to connect to the map server.
  20546. if( sd ){
  20547. // Decrypt the current packet ID with the last key stored in the session.
  20548. cmd = (cmd ^ ((sd->cryptKey >> 16) & 0x7FFF));
  20549. }else{
  20550. // Store the original value for checking
  20551. cmd2 = cmd;
  20552. // A player tries to connect - use the initial keys for the decryption of the packet ID.
  20553. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20554. }
  20555. #endif
  20556. // filter out invalid / unsupported packets
  20557. if (cmd > MAX_PACKET_DB || cmd < MIN_PACKET_DB || packet_db[cmd].len == 0) {
  20558. ShowWarning("clif_parse: Received unsupported packet (packet 0x%04x, %d bytes received), disconnecting session #%d.\n", cmd, RFIFOREST(fd), fd);
  20559. #ifdef DUMP_INVALID_PACKET
  20560. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20561. #endif
  20562. #ifdef PACKET_OBFUSCATION_WARN
  20563. // If it is a connection attempt, check if the packet would have been valid without decrypting it
  20564. if( !sd ){
  20565. #ifdef PACKET_OBFUSCATION
  20566. if( cmd2 >= MIN_PACKET_DB && cmd2 < MAX_PACKET_DB && packet_db[cmd2].len != 0 && packet_db[cmd2].func == clif_parse_WantToConnection ){
  20567. ShowWarning( "clif_parse: It looks like you have enabled PACKET_OBFUSCATION on server side, but disabled it on client side.\n" );
  20568. }
  20569. #else
  20570. // Try to use the initial keys for the decryption of the packet ID.
  20571. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20572. if( cmd >= MIN_PACKET_DB && cmd < MAX_PACKET_DB && packet_db[cmd].len != 0 && packet_db[cmd].func == clif_parse_WantToConnection ){
  20573. ShowWarning( "clif_parse: It looks like you have disabled PACKET_OBFUSCATION on server side, but enabled it on client side.\n" );
  20574. }
  20575. #endif
  20576. }
  20577. #endif
  20578. set_eof(fd);
  20579. return 0;
  20580. }
  20581. // determine real packet length
  20582. packet_len = packet_db[cmd].len;
  20583. if (packet_len == -1) { // variable-length packet
  20584. if (RFIFOREST(fd) < 4)
  20585. return 0;
  20586. packet_len = RFIFOW(fd,2);
  20587. if (packet_len < 4 || packet_len > 32768) {
  20588. ShowWarning("clif_parse: Received packet 0x%04x specifies invalid packet_len (%d), disconnecting session #%d.\n", cmd, packet_len, fd);
  20589. #ifdef DUMP_INVALID_PACKET
  20590. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20591. #endif
  20592. set_eof(fd);
  20593. return 0;
  20594. }
  20595. }
  20596. if ((int)RFIFOREST(fd) < packet_len){
  20597. 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 );
  20598. #ifdef DUMP_INVALID_PACKET
  20599. ShowDump( RFIFOP( fd, 0 ), RFIFOREST( fd ) );
  20600. #endif
  20601. set_eof( fd );
  20602. return 0; // not enough data received to form the packet
  20603. }
  20604. #ifdef PACKET_OBFUSCATION
  20605. RFIFOW(fd, 0) = cmd;
  20606. if (sd)
  20607. sd->cryptKey = ((sd->cryptKey * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF; // Update key for the next packet
  20608. #endif
  20609. if( packet_db[cmd].func == clif_parse_debug )
  20610. packet_db[cmd].func(fd, sd);
  20611. else if( packet_db[cmd].func != nullptr ) {
  20612. if( !sd && packet_db[cmd].func != clif_parse_WantToConnection )
  20613. ; //Only valid packet when there is no session
  20614. else
  20615. if( sd && sd->bl.prev == nullptr && packet_db[cmd].func != clif_parse_LoadEndAck )
  20616. ; //Only valid packet when player is not on a map
  20617. else
  20618. packet_db[cmd].func(fd, sd);
  20619. }
  20620. #ifdef DUMP_UNKNOWN_PACKET
  20621. else DumpUnknown(fd,sd,cmd,packet_len);
  20622. #endif
  20623. RFIFOSKIP(fd, packet_len);
  20624. }; // main loop end
  20625. return 0;
  20626. }
  20627. void packetdb_addpacket( uint16 cmd, uint16 length, void (*func)(int, map_session_data *), ... ){
  20628. va_list argp;
  20629. int i;
  20630. if(cmd <= 0 || cmd > MAX_PACKET_DB)
  20631. return;
  20632. packet_db[cmd].len = length;
  20633. packet_db[cmd].func = func;
  20634. va_start(argp, func);
  20635. for( i = 0; i < MAX_PACKET_POS; i++ ){
  20636. int offset = va_arg(argp, int);
  20637. if( offset == 0 ){
  20638. break;
  20639. }
  20640. packet_db[cmd].pos[i] = offset;
  20641. }
  20642. if( i == MAX_PACKET_POS ){
  20643. ShowError( "Too many positions found for packet 0x%04x (max=%d).\n", cmd, MAX_PACKET_POS );
  20644. }
  20645. va_end(argp);
  20646. }
  20647. /*==========================================
  20648. * Reads packets and setups its array reference
  20649. *------------------------------------------*/
  20650. void packetdb_readdb(){
  20651. memset(packet_db,0,sizeof(packet_db));
  20652. #include "clif_packetdb.hpp"
  20653. #include "clif_shuffle.hpp"
  20654. ShowStatus("Using packet version: " CL_WHITE "%d" CL_RESET ".\n", PACKETVER);
  20655. #ifdef PACKET_OBFUSCATION
  20656. 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]);
  20657. #else
  20658. ShowStatus("Packet Obfuscation: " CL_RED "Disabled" CL_RESET ".\n");
  20659. #endif
  20660. }
  20661. /*==========================================
  20662. *
  20663. *------------------------------------------*/
  20664. void do_init_clif(void) {
  20665. const int colors[COLOR_MAX] = {
  20666. 0x00FF00, // COLOR_DEFAULT
  20667. 0xFF0000, // COLOR_RED
  20668. 0xFFFFFF, // COLOR_WHITE
  20669. 0xFFFF00, // COLOR_YELLOW
  20670. 0x00FFFF, // COLOR_CYAN
  20671. 0xB5FFB5, // COLOR_LIGHT_GREEN
  20672. 0xFFFF63, // COLOR_LIGHT_YELLOW
  20673. };
  20674. /**
  20675. * Setup Color Table (saves unnecessary load of strtoul on every call)
  20676. **/
  20677. for( int i = 0; i < COLOR_MAX; i++ ){
  20678. color_table[i] = ( colors[i] & 0x0000FF ) << 16 | ( colors[i] & 0x00FF00 ) | ( colors[i] & 0xFF0000 ) >> 16; //RGB to BGR
  20679. }
  20680. packetdb_readdb();
  20681. set_defaultparse(clif_parse);
  20682. if( make_listen_bind(bind_ip,map_port) == -1 ) {
  20683. ShowFatalError("Failed to bind to port '" CL_WHITE "%d" CL_RESET "'\n",map_port);
  20684. exit(EXIT_FAILURE);
  20685. }
  20686. add_timer_func_list(clif_clearunit_delayed_sub, "clif_clearunit_delayed_sub");
  20687. add_timer_func_list(clif_delayquit, "clif_delayquit");
  20688. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320
  20689. add_timer_func_list( clif_ping_timer, "clif_ping_timer" );
  20690. if( battle_config.ping_timer_interval > 0 ){
  20691. add_timer_interval( gettick() + battle_config.ping_timer_interval * 1000, clif_ping_timer, 0, 0, battle_config.ping_timer_interval * 1000 );
  20692. }
  20693. #endif
  20694. delay_clearunit_ers = ers_new(sizeof(struct block_list),"clif.cpp::delay_clearunit_ers",ERS_OPT_CLEAR);
  20695. }
  20696. void do_final_clif(void) {
  20697. ers_destroy(delay_clearunit_ers);
  20698. }