clif.cpp 740 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387
  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 (ZC_DESTROY_ROOM).
  3817. /// 00d8 <chat id>.L
  3818. void clif_clearchat(struct chat_data *cd,int fd)
  3819. {
  3820. unsigned char buf[32];
  3821. nullpo_retv(cd);
  3822. WBUFW(buf,0) = 0xd8;
  3823. WBUFL(buf,2) = cd->bl.id;
  3824. if( session_isActive(fd) ) {
  3825. WFIFOHEAD(fd,packet_len(0xd8));
  3826. memcpy(WFIFOP(fd,0),buf,packet_len(0xd8));
  3827. WFIFOSET(fd,packet_len(0xd8));
  3828. } else {
  3829. clif_send(buf,packet_len(0xd8),cd->owner,AREA_WOSC);
  3830. }
  3831. }
  3832. /// Displays messages regarding join chat failures.
  3833. /// 00da <result>.B (ZC_REFUSE_ENTER_ROOM)
  3834. /// result:
  3835. /// 0 = room full
  3836. /// 1 = wrong password
  3837. /// 2 = kicked
  3838. /// 3 = success (no message)
  3839. /// 4 = no enough zeny
  3840. /// 5 = too low level
  3841. /// 6 = too high level
  3842. /// 7 = unsuitable job class
  3843. void clif_joinchatfail( map_session_data& sd, e_refuse_enter_room result ){
  3844. PACKET_ZC_REFUSE_ENTER_ROOM packet{};
  3845. packet.packetType = HEADER_ZC_REFUSE_ENTER_ROOM;
  3846. packet.result = static_cast<decltype(packet.result)>(result);
  3847. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3848. }
  3849. /// Notifies the client about entering a chatroom (ZC_ENTER_ROOM).
  3850. /// 00db <packet len>.W <chat id>.L { <role>.L <name>.24B }*
  3851. /// role:
  3852. /// 0 = owner (menu)
  3853. /// 1 = normal
  3854. void clif_joinchatok(map_session_data& sd, chat_data& cd){
  3855. PACKET_ZC_ENTER_ROOM* p = reinterpret_cast<PACKET_ZC_ENTER_ROOM*>( packet_buffer );
  3856. p->packetType = HEADER_ZC_ENTER_ROOM;
  3857. p->packetSize = sizeof(*p);
  3858. p->chatId = cd.bl.id;
  3859. if(cd.owner->type == BL_NPC){
  3860. PACKET_ZC_ENTER_ROOM_sub& owner = p->members[0];
  3861. owner.flag = 0;
  3862. safestrncpy(owner.name, reinterpret_cast<npc_data*>(cd.owner)->name, sizeof(owner.name));
  3863. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( owner ) );
  3864. for (size_t i = 0; i < cd.users; i++) {
  3865. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i + 1];
  3866. member.flag = 1;
  3867. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3868. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3869. }
  3870. }else{
  3871. for (size_t i = 0; i < cd.users; i++) {
  3872. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i];
  3873. member.flag = i > 0;
  3874. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3875. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3876. }
  3877. }
  3878. clif_send(p,p->packetSize,&sd.bl,SELF);
  3879. }
  3880. /// Notifies clients in a chat about a new member (ZC_MEMBER_NEWENTRY).
  3881. /// 00dc <users>.W <name>.24B
  3882. void clif_addchat(struct chat_data* cd,map_session_data *sd)
  3883. {
  3884. unsigned char buf[29];
  3885. nullpo_retv(sd);
  3886. nullpo_retv(cd);
  3887. WBUFW(buf, 0) = 0xdc;
  3888. WBUFW(buf, 2) = cd->users;
  3889. safestrncpy(WBUFCP(buf, 4),sd->status.name,NAME_LENGTH);
  3890. clif_send(buf,packet_len(0xdc),&sd->bl,CHAT_WOS);
  3891. }
  3892. /// Announce the new owner (ZC_ROLE_CHANGE).
  3893. /// 00e1 <role>.L <nick>.24B
  3894. /// role:
  3895. /// 0 = owner (menu)
  3896. /// 1 = normal
  3897. void clif_changechatowner(struct chat_data* cd, map_session_data* sd)
  3898. {
  3899. unsigned char buf[64];
  3900. nullpo_retv(sd);
  3901. nullpo_retv(cd);
  3902. WBUFW(buf, 0) = 0xe1;
  3903. WBUFL(buf, 2) = 1;
  3904. safestrncpy(WBUFCP(buf,6),cd->usersd[0]->status.name,NAME_LENGTH);
  3905. WBUFW(buf,30) = 0xe1;
  3906. WBUFL(buf,32) = 0;
  3907. safestrncpy(WBUFCP(buf,36),sd->status.name,NAME_LENGTH);
  3908. clif_send(buf,packet_len(0xe1)*2,&sd->bl,CHAT);
  3909. }
  3910. /// Notify about user leaving the chatroom (ZC_MEMBER_EXIT).
  3911. /// 00dd <users>.W <nick>.24B <flag>.B
  3912. /// flag:
  3913. /// 0 = left
  3914. /// 1 = kicked
  3915. void clif_leavechat(struct chat_data* cd, map_session_data* sd, bool flag)
  3916. {
  3917. unsigned char buf[32];
  3918. nullpo_retv(sd);
  3919. nullpo_retv(cd);
  3920. WBUFW(buf, 0) = 0xdd;
  3921. WBUFW(buf, 2) = cd->users-1;
  3922. safestrncpy(WBUFCP(buf,4),sd->status.name,NAME_LENGTH);
  3923. WBUFB(buf,28) = flag;
  3924. clif_send(buf,packet_len(0xdd),&sd->bl,CHAT);
  3925. }
  3926. /// Opens a trade request window from char 'name'.
  3927. /// 00e5 <nick>.24B (ZC_REQ_EXCHANGE_ITEM)
  3928. /// 01f4 <nick>.24B <targetid>.L <baselvl>.W (ZC_REQ_EXCHANGE_ITEM2)
  3929. void clif_traderequest(map_session_data& sd, const char* name){
  3930. PACKET_ZC_REQ_EXCHANGE_ITEM p{};
  3931. p.packetType = HEADER_ZC_REQ_EXCHANGE_ITEM;
  3932. safestrncpy(p.requesterName, name, sizeof(p.requesterName));
  3933. #if PACKETVER > 6
  3934. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  3935. p.targetLv = sd.trade_partner.lv;
  3936. #endif
  3937. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3938. }
  3939. /// Reply to a trade-request.
  3940. /// 00e7 <result>.B (ZC_ACK_EXCHANGE_ITEM)
  3941. /// 01f5 <result>.B <targetid>.L <baselvl>.W (ZC_ACK_EXCHANGE_ITEM2)
  3942. /// result:
  3943. /// 0 = Char is too far
  3944. /// 1 = Character does not exist
  3945. /// 2 = Trade failed
  3946. /// 3 = Accept
  3947. /// 4 = Cancel
  3948. /// 5 = Busy
  3949. void clif_traderesponse( map_session_data& sd, e_ack_trade_response result ){
  3950. PACKET_ZC_ACK_EXCHANGE_ITEM p{};
  3951. p.packetType = HEADER_ZC_ACK_EXCHANGE_ITEM;
  3952. p.result = static_cast<decltype(p.result)>( result );
  3953. #if PACKETVER > 6
  3954. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  3955. p.targetLv = sd.trade_partner.lv;
  3956. #endif
  3957. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3958. }
  3959. /// Notifies the client about an item from other player in current trade.
  3960. /// 00e9 <amount>.L <nameid>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_EXCHANGE_ITEM)
  3961. /// 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)
  3962. /// 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)
  3963. void clif_tradeadditem( map_session_data* sd, map_session_data* tsd, int index, int amount ){
  3964. nullpo_retv( sd );
  3965. nullpo_retv( tsd );
  3966. int fd = tsd->fd;
  3967. if( !session_isActive( fd ) ){
  3968. return;
  3969. }
  3970. PACKET_ZC_ADD_EXCHANGE_ITEM p = {};
  3971. if( index ){
  3972. index = server_index( index );
  3973. if( index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0 || sd->inventory_data[index] == nullptr ){
  3974. return;
  3975. }
  3976. p.itemId = client_nameid( sd->inventory.u.items_inventory[index].nameid );
  3977. #if PACKETVER >= 20100223
  3978. p.itemType = sd->inventory_data[index]->type;
  3979. #endif
  3980. p.identified = sd->inventory.u.items_inventory[index].identify;
  3981. p.damaged = sd->inventory.u.items_inventory[index].attribute;
  3982. p.refine = sd->inventory.u.items_inventory[index].refine;
  3983. clif_addcards( &p.slot, &sd->inventory.u.items_inventory[index] );
  3984. #if PACKETVER >= 20150226
  3985. clif_add_random_options( p.option_data, sd->inventory.u.items_inventory[index] );
  3986. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  3987. p.location = pc_equippoint_sub( sd, sd->inventory_data[index] );
  3988. p.look = sd->inventory_data[index]->look;
  3989. p.grade = sd->inventory.u.items_inventory[index].enchantgrade;
  3990. #endif
  3991. #endif
  3992. }else{
  3993. p = {};
  3994. }
  3995. p.packetType = HEADER_ZC_ADD_EXCHANGE_ITEM;
  3996. p.amount = amount;
  3997. clif_send( &p, sizeof( p ), &tsd->bl, SELF );
  3998. }
  3999. /// Notifies the client about the result of request to add an item to the current trade (ZC_ACK_ADD_EXCHANGE_ITEM).
  4000. /// 00ea <index>.W <result>.B
  4001. /// result:
  4002. /// 0 = success
  4003. /// 1 = overweight
  4004. /// 2 = trade canceled
  4005. void clif_tradeitemok(map_session_data& sd, int index, e_exitem_add_result result)
  4006. {
  4007. PACKET_ZC_ACK_ADD_EXCHANGE_ITEM p = {};
  4008. p.packetType = HEADER_ZC_ACK_ADD_EXCHANGE_ITEM;
  4009. p.index = client_index(index);
  4010. p.result = static_cast<uint8>(result);
  4011. clif_send(&p, sizeof(p), &sd.bl, SELF);
  4012. }
  4013. /// Notifies the client about finishing one side of the current trade.
  4014. /// 00ec <who>.B (ZC_CONCLUDE_EXCHANGE_ITEM)
  4015. /// who:
  4016. /// 0 = self
  4017. /// 1 = other player
  4018. void clif_tradedeal_lock( map_session_data& sd, bool who ){
  4019. PACKET_ZC_CONCLUDE_EXCHANGE_ITEM packet{};
  4020. packet.packetType = HEADER_ZC_CONCLUDE_EXCHANGE_ITEM;
  4021. packet.who = who;
  4022. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4023. }
  4024. /// Notifies the client about the trade being canceled.
  4025. /// 00ee (ZC_CANCEL_EXCHANGE_ITEM)
  4026. void clif_tradecancelled( map_session_data& sd ){
  4027. PACKET_ZC_CANCEL_EXCHANGE_ITEM packet{};
  4028. packet.packetType = HEADER_ZC_CANCEL_EXCHANGE_ITEM;
  4029. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4030. }
  4031. /// Result of a trade.
  4032. /// 00f0 <result>.B (ZC_EXEC_EXCHANGE_ITEM)
  4033. /// result:
  4034. /// 0 = success
  4035. /// 1 = failure
  4036. void clif_tradecompleted( map_session_data& sd ){
  4037. PACKET_ZC_EXEC_EXCHANGE_ITEM packet{};
  4038. packet.packetType = HEADER_ZC_EXEC_EXCHANGE_ITEM;
  4039. packet.result = 0;
  4040. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4041. }
  4042. /// Resets the trade window on the send side.
  4043. /// 00f1 (ZC_EXCHANGEITEM_UNDO)
  4044. /// NOTE: Unknown purpose. Items are not removed until the window is
  4045. /// refreshed (ex. by putting another item in there).
  4046. void clif_tradeundo( map_session_data& sd ){
  4047. PACKET_ZC_EXCHANGEITEM_UNDO packet{};
  4048. packet.packetType = HEADER_ZC_EXCHANGEITEM_UNDO;
  4049. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4050. }
  4051. /// Updates storage total amount.
  4052. /// 00f2 <current count>.W <max count>.W (ZC_NOTIFY_STOREITEM_COUNTINFO)
  4053. void clif_updatestorageamount( map_session_data& sd, uint16 amount, uint16 max_amount ){
  4054. PACKET_ZC_NOTIFY_STOREITEM_COUNTINFO packet{};
  4055. packet.packetType = HEADER_ZC_NOTIFY_STOREITEM_COUNTINFO;
  4056. packet.amount = amount;
  4057. packet.max_amount = max_amount;
  4058. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4059. }
  4060. /// Notifies the client of an item being added to the storage.
  4061. /// 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)
  4062. /// 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)
  4063. /// 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)
  4064. void clif_storageitemadded( map_session_data* sd, struct item* i, int index, int amount ){
  4065. nullpo_retv( sd );
  4066. nullpo_retv( i );
  4067. int fd = sd->fd;
  4068. if( !session_isActive( fd ) ){
  4069. return;
  4070. }
  4071. PACKET_ZC_ADD_ITEM_TO_STORE p = {};
  4072. p.packetType = HEADER_ZC_ADD_ITEM_TO_STORE; // Storage item added
  4073. p.index = client_storage_index( index ); // index
  4074. p.amount = amount; // amount
  4075. p.itemId = client_nameid( i->nameid ); // id
  4076. #if PACKETVER >= 5
  4077. p.itemType = itemtype( i->nameid ); //type
  4078. #endif
  4079. p.identified = i->identify; //identify flag
  4080. p.damaged = i->attribute; // attribute
  4081. p.refine = i->refine; //refine
  4082. clif_addcards( &p.slot, i );
  4083. #if PACKETVER_MAIN_NUM >= 20140813 || PACKETVER_RE_NUM >= 20140402 || defined(PACKETVER_ZERO)
  4084. clif_add_random_options( p.option_data, *i );
  4085. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20221024
  4086. p.grade = i->enchantgrade;
  4087. #endif
  4088. #endif
  4089. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  4090. }
  4091. /// Notifies the client of an item being deleted from the storage.
  4092. /// 00f6 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_STORE)
  4093. void clif_storageitemremoved( map_session_data& sd, uint16 index, uint32 amount ){
  4094. PACKET_ZC_DELETE_ITEM_FROM_STORE packet{};
  4095. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_STORE;
  4096. packet.index = client_storage_index(index);
  4097. packet.amount = amount;
  4098. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4099. }
  4100. /// Closes storage.
  4101. /// 00f8 (ZC_CLOSE_STORE)
  4102. void clif_storageclose( map_session_data& sd ){
  4103. PACKET_ZC_CLOSE_STORE packet{};
  4104. packet.packetType = HEADER_ZC_CLOSE_STORE;
  4105. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4106. }
  4107. /// Notifies clients in an area of a player's souls.
  4108. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  4109. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  4110. /// 0b73 <id>.L <amount>.W (ZC_SOULENERGY)
  4111. void clif_soulball( map_session_data *sd, struct block_list* target, enum send_target send_target ){
  4112. #if PACKETVER_MAIN_NUM >= 20200414 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20200506
  4113. PACKET_ZC_SOULENERGY p = {};
  4114. p.PacketType = HEADER_ZC_SOULENERGY;
  4115. #else
  4116. PACKET_ZC_SPIRITS p = {};
  4117. p.PacketType = HEADER_ZC_SPIRITS;
  4118. #endif
  4119. p.AID = sd->bl.id;
  4120. p.num = sd->soulball;
  4121. clif_send( &p, sizeof( p ), target == nullptr ? &sd->bl : target, send_target );
  4122. }
  4123. /*==========================================
  4124. * Server tells 'sd' player client the abouts of 'dstsd' player
  4125. *------------------------------------------*/
  4126. static void clif_getareachar_pc(map_session_data* sd,map_session_data* dstsd)
  4127. {
  4128. struct block_list *d_bl;
  4129. int i;
  4130. if( dstsd->chatID ) {
  4131. struct chat_data *cd = nullptr;
  4132. if( (cd = (struct chat_data*)map_id2bl(dstsd->chatID)) && cd->usersd[0]==dstsd)
  4133. clif_dispchat(cd,sd->fd);
  4134. } else if( dstsd->state.vending )
  4135. clif_showvendingboard( *dstsd, SELF, &sd->bl );
  4136. else if( dstsd->state.buyingstore )
  4137. clif_buyingstore_entry( *dstsd, &sd->bl );
  4138. if(dstsd->spiritball > 0)
  4139. clif_spiritball( &dstsd->bl, &sd->bl, SELF );
  4140. clif_millenniumshield_single( *dstsd, *sd );
  4141. clif_spiritcharm_single( *dstsd, *sd );
  4142. if (dstsd->soulball > 0)
  4143. clif_soulball( dstsd, &sd->bl, SELF );
  4144. if (dstsd->servantball > 0)
  4145. clif_servantball( *dstsd, &sd->bl, SELF );
  4146. if (dstsd->abyssball > 0)
  4147. clif_abyssball( *dstsd, &sd->bl, SELF );
  4148. if( (sd->status.party_id && dstsd->status.party_id == sd->status.party_id) || //Party-mate, or hpdisp setting.
  4149. (sd->bg_id && sd->bg_id == dstsd->bg_id) || //BattleGround
  4150. pc_has_permission(sd, PC_PERM_VIEW_HPMETER)
  4151. )
  4152. clif_hpmeter_single( *sd, dstsd->bl.id, dstsd->battle_status.hp, dstsd->battle_status.max_hp );
  4153. // display link (sd - dstsd) to sd
  4154. ARR_FIND( 0, MAX_DEVOTION, i, sd->devotion[i] == dstsd->bl.id );
  4155. if( i < MAX_DEVOTION )
  4156. clif_devotion(&sd->bl, sd);
  4157. // display links (dstsd - devotees) to sd
  4158. ARR_FIND( 0, MAX_DEVOTION, i, dstsd->devotion[i] > 0 );
  4159. if( i < MAX_DEVOTION )
  4160. clif_devotion(&dstsd->bl, sd);
  4161. // display link (dstsd - crusader) to sd
  4162. if( dstsd->sc.getSCE(SC_DEVOTION) && (d_bl = map_id2bl(dstsd->sc.getSCE(SC_DEVOTION)->val1)) != nullptr )
  4163. clif_devotion(d_bl, sd);
  4164. }
  4165. void clif_getareachar_unit( map_session_data* sd,struct block_list *bl ){
  4166. struct unit_data *ud;
  4167. struct view_data *vd;
  4168. bool option = false;
  4169. unsigned int option_val = 0;
  4170. vd = status_get_viewdata(bl);
  4171. if (!vd || vd->class_ == JT_INVISIBLE)
  4172. return;
  4173. // Hide NPC from Maya Purple card
  4174. if (clif_npc_mayapurple(bl))
  4175. return;
  4176. if( bl->type == BL_NPC && npc_is_hidden_dynamicnpc( *( (struct npc_data*)bl ), *sd ) ){
  4177. // Do not send anything
  4178. return;
  4179. }
  4180. ud = unit_bl2ud(bl);
  4181. if( ud && ud->walktimer != INVALID_TIMER ){
  4182. clif_set_unit_walking( *bl, sd, *ud, SELF );
  4183. }else{
  4184. clif_set_unit_idle( bl, false, SELF, &sd->bl );
  4185. }
  4186. if (vd->cloth_color)
  4187. clif_refreshlook(&sd->bl,bl->id,LOOK_CLOTHES_COLOR,vd->cloth_color,SELF);
  4188. if (vd->body_style)
  4189. clif_refreshlook(&sd->bl,bl->id,LOOK_BODY2,vd->body_style,SELF);
  4190. switch (bl->type)
  4191. {
  4192. case BL_PC:
  4193. {
  4194. TBL_PC* tsd = (TBL_PC*)bl;
  4195. clif_getareachar_pc(sd, tsd);
  4196. if(tsd->state.size==SZ_BIG) // tiny/big players [Valaris]
  4197. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4198. else if(tsd->state.size==SZ_MEDIUM)
  4199. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4200. if( tsd->bg_id && map_getmapflag(tsd->bl.m, MF_BATTLEGROUND) )
  4201. clif_sendbgemblem_single(sd->fd,tsd);
  4202. if ( tsd->status.robe )
  4203. clif_refreshlook(&sd->bl,bl->id,LOOK_ROBE,tsd->status.robe,SELF);
  4204. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4205. clif_hat_effects( *sd, tsd->bl, SELF );
  4206. }
  4207. break;
  4208. case BL_MER: // Devotion Effects
  4209. if( ((TBL_MER*)bl)->devotion_flag )
  4210. clif_devotion(bl, sd);
  4211. break;
  4212. case BL_NPC:
  4213. {
  4214. TBL_NPC* nd = (TBL_NPC*)bl;
  4215. if( nd->chat_id )
  4216. clif_dispchat((struct chat_data*)map_id2bl(nd->chat_id),sd->fd);
  4217. if( nd->size == SZ_BIG )
  4218. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4219. else if( nd->size == SZ_MEDIUM )
  4220. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4221. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4222. clif_progressbar_npc(nd, sd);
  4223. }
  4224. break;
  4225. case BL_MOB:
  4226. {
  4227. TBL_MOB* md = (TBL_MOB*)bl;
  4228. if(md->special_state.size==SZ_BIG) // tiny/big mobs [Valaris]
  4229. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4230. else if(md->special_state.size==SZ_MEDIUM)
  4231. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4232. #if PACKETVER >= 20120404
  4233. if (battle_config.monster_hp_bars_info && !map_getmapflag(bl->m, MF_HIDEMOBHPBAR)) {
  4234. int i;
  4235. for(i = 0; i < DAMAGELOG_SIZE; i++)// must show hp bar to all char who already hit the mob.
  4236. if( md->dmglog[i].id == sd->status.char_id )
  4237. clif_monster_hp_bar(md, sd->fd);
  4238. }
  4239. #endif
  4240. }
  4241. break;
  4242. case BL_PET:
  4243. if (vd->head_bottom)
  4244. clif_pet_equip(sd, (TBL_PET*)bl); // needed to display pet equip properly
  4245. break;
  4246. }
  4247. }
  4248. //Modifies the type of damage according to status changes [Skotlex]
  4249. //Aegis data specifies that: 4 endure against single hit sources, 9 against multi-hit.
  4250. static enum e_damage_type clif_calc_delay(enum e_damage_type type, int div, int64 damage, int delay)
  4251. {
  4252. return ( delay == 0 && damage > 0 ) ? ( div > 1 ? DMG_MULTI_HIT_ENDURE : DMG_ENDURE ) : type;
  4253. }
  4254. /*==========================================
  4255. * Estimates walk delay based on the damage criteria. [Skotlex]
  4256. *------------------------------------------*/
  4257. static int clif_calc_walkdelay(struct block_list *bl,int delay, char type, int64 damage, int div_)
  4258. {
  4259. if (type == DMG_ENDURE || type == DMG_MULTI_HIT_ENDURE || damage <= 0)
  4260. return 0;
  4261. if (bl->type == BL_PC) {
  4262. if (battle_config.pc_walk_delay_rate != 100)
  4263. delay = delay*battle_config.pc_walk_delay_rate/100;
  4264. } else
  4265. if (battle_config.walk_delay_rate != 100)
  4266. delay = delay*battle_config.walk_delay_rate/100;
  4267. if (div_ > 1) //Multi-hit skills mean higher delays.
  4268. delay += battle_config.multihit_delay*(div_-1);
  4269. return (delay > 0) ? delay:1; //Return 1 to specify there should be no noticeable delay, but you should stop walking.
  4270. }
  4271. /*========================================== [Playtester]
  4272. * Returns hallucination damage the client displays
  4273. *------------------------------------------*/
  4274. static int clif_hallucination_damage()
  4275. {
  4276. int digit = rnd() % 5 + 1;
  4277. switch (digit)
  4278. {
  4279. case 1:
  4280. return (rnd() % 10);
  4281. case 2:
  4282. return (rnd() % 100);
  4283. case 3:
  4284. return (rnd() % 1000);
  4285. case 4:
  4286. return (rnd() % 10000);
  4287. }
  4288. return (rnd() % 32767);
  4289. }
  4290. #define DEFAULT_ANIMATION_SPEED 432
  4291. /// Sends a 'damage' packet (src performs action on dst)
  4292. /// 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)
  4293. /// 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)
  4294. /// 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)
  4295. /// type:
  4296. /// 0 = damage [ damage: total damage, div: amount of hits, damage2: assassin dual-wield damage ]
  4297. /// 1 = pick up item
  4298. /// 2 = sit down
  4299. /// 3 = stand up
  4300. /// 4 = damage (endure)
  4301. /// 5 = (splash?)
  4302. /// 6 = (skill?)
  4303. /// 7 = (repeat damage?)
  4304. /// 8 = multi-hit damage
  4305. /// 9 = multi-hit damage (endure)
  4306. /// 10 = critical hit
  4307. /// 11 = lucky dodge
  4308. /// 12 = (touch skill?)
  4309. /// 13 = multi-hit critical
  4310. 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){
  4311. int damage = (int)cap_value(sdamage,INT_MIN,INT_MAX);
  4312. int damage2 = (int)cap_value(sdamage2,INT_MIN,INT_MAX);
  4313. if (type != DMG_MULTI_HIT_CRITICAL)
  4314. type = clif_calc_delay(type,div,damage+damage2,ddelay);
  4315. status_change *sc = status_get_sc(&dst);
  4316. if(sc && sc->count) {
  4317. if(sc->getSCE(SC_HALLUCINATION)) {
  4318. damage = clif_hallucination_damage();
  4319. if(damage2)
  4320. damage2 = clif_hallucination_damage();
  4321. }
  4322. }
  4323. // Calculate what sdelay to send to the client so it applies damage at the same time as the server
  4324. if (battle_config.synchronize_damage && src.type == BL_MOB) {
  4325. // When a clif_damage packet is sent to the client it will also send "sdelay" (amotion) as value.
  4326. // The client however does not interpret this value as AttackMotion but incorrectly as an inverted
  4327. // animation speed modifier, with 432 standing for 1x animation speed.
  4328. // The client will ignore all values above 432, but lower values will speed up the animation.
  4329. // 216 for example means play the animation at double the speed. 108 is quadruple speed.
  4330. // Each monster has an attack animation and may define the frame in the attack animation on which
  4331. // it displays the damage and makes the target flinch / stop. If the damage frame is undefined,
  4332. // it instead displays the damage / flinch / stop at the beginning of the second to last frame.
  4333. // We define the time after which the damage frame shows at 1x speed as clientamotion.
  4334. uint16 clientamotion = std::max((uint16)1, status_get_clientamotion(&src));
  4335. // Knowing when the damage frame happens in the animation allows us to synchronize the timing
  4336. // between client and server using the formula below.
  4337. sdelay = sdelay * DEFAULT_ANIMATION_SPEED / clientamotion;
  4338. // Hint: If amotion is larger than clientamotion this results in a value above 432 which makes the
  4339. // client display the attack at 1x speed. In this case we need to shorten the delay damage timer
  4340. // on the server to clientamotion ms instead (see battle_delay_damage).
  4341. sdelay = std::min(sdelay, DEFAULT_ANIMATION_SPEED);
  4342. }
  4343. PACKET_ZC_NOTIFY_ACT p{};
  4344. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4345. p.srcID = src.id;
  4346. p.targetID = dst.id;
  4347. p.serverTick = client_tick(tick);
  4348. p.srcSpeed = sdelay;
  4349. p.dmgSpeed = ddelay;
  4350. if (battle_config.hide_woe_damage && map_flag_gvg(src.m)) {
  4351. p.damage = damage ? div : 0;
  4352. p.damage2 = damage2 ? div : 0;
  4353. } else {
  4354. p.damage = static_cast<decltype(p.damage)>( std::min( damage, static_cast<decltype(damage)>( std::numeric_limits<decltype(p.damage)>::max() ) ) );
  4355. p.damage2 = static_cast<decltype(p.damage2)>( std::min( damage2, static_cast<decltype(damage2)>( std::numeric_limits<decltype(p.damage2)>::max() ) ) );
  4356. }
  4357. #if PACKETVER >= 20131223
  4358. p.isSPDamage = (spdamage) ? 1 : 0; // IsSPDamage - Displays blue digits.
  4359. #endif
  4360. p.div = div;
  4361. p.type = type;
  4362. if(disguised(&dst)) {
  4363. clif_send( &p, sizeof(p), &dst, AREA_WOS);
  4364. p.targetID = disguised_bl_id( dst.id );
  4365. clif_send( &p, sizeof(p), &dst, SELF);
  4366. } else
  4367. clif_send(&p, sizeof(p), &dst, AREA);
  4368. if(disguised(&src)) {
  4369. p.srcID = disguised_bl_id( src.id );
  4370. if(damage > 0)
  4371. p.damage = -1;
  4372. if(damage2 > 0)
  4373. p.damage2 = -1;
  4374. clif_send( &p, sizeof(p), &src, SELF);
  4375. }
  4376. if(&src == &dst)
  4377. unit_setdir(&src, unit_getdir(&src));
  4378. // In case this assignment is bypassed by DMG_MULTI_HIT_CRITICAL
  4379. type = clif_calc_delay(type, div, damage + damage2, ddelay);
  4380. //Return adjusted can't walk delay for further processing.
  4381. return clif_calc_walkdelay(&dst, ddelay, type, damage+damage2, div);
  4382. }
  4383. /*==========================================
  4384. * src picks up dst
  4385. *------------------------------------------*/
  4386. void clif_takeitem(block_list& src, block_list& dst){
  4387. PACKET_ZC_NOTIFY_ACT p{};
  4388. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4389. p.srcID = src.id;
  4390. p.targetID = dst.id;
  4391. p.type = DMG_PICKUP_ITEM;
  4392. clif_send(&p, sizeof(p), &src, AREA);
  4393. }
  4394. /*==========================================
  4395. * inform clients in area that `bl` is sitting
  4396. *------------------------------------------*/
  4397. void clif_sitting(block_list& bl){
  4398. PACKET_ZC_NOTIFY_ACT p{};
  4399. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4400. p.srcID = bl.id;
  4401. p.type = DMG_SIT_DOWN;
  4402. clif_send(&p, sizeof(p), &bl, AREA);
  4403. if(disguised(&bl)) {
  4404. p.srcID = disguised_bl_id( bl.id );
  4405. clif_send(&p, sizeof(p), &bl, SELF);
  4406. }
  4407. }
  4408. /*==========================================
  4409. * inform clients in area that `bl` is standing
  4410. *------------------------------------------*/
  4411. void clif_standing(block_list& bl){
  4412. PACKET_ZC_NOTIFY_ACT p{};
  4413. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4414. p.srcID = bl.id;
  4415. p.type = DMG_STAND_UP;
  4416. clif_send(&p, sizeof(p), &bl, AREA);
  4417. if(disguised(&bl)) {
  4418. p.srcID = disguised_bl_id( bl.id );
  4419. clif_send(&p, sizeof(p), &bl, SELF);
  4420. }
  4421. }
  4422. /// Inform client(s) about a map-cell change (ZC_UPDATE_MAPINFO).
  4423. /// 0192 <x>.W <y>.W <type>.W <map name>.16B
  4424. void clif_changemapcell(int fd, int16 m, int x, int y, int type, enum send_target target)
  4425. {
  4426. unsigned char buf[32];
  4427. WBUFW(buf,0) = 0x192;
  4428. WBUFW(buf,2) = x;
  4429. WBUFW(buf,4) = y;
  4430. WBUFW(buf,6) = type;
  4431. mapindex_getmapname_ext(map_mapid2mapname(m),WBUFCP(buf,8));
  4432. if( session_isActive(fd) )
  4433. {
  4434. WFIFOHEAD(fd,packet_len(0x192));
  4435. memcpy(WFIFOP(fd,0), buf, packet_len(0x192));
  4436. WFIFOSET(fd,packet_len(0x192));
  4437. }
  4438. else
  4439. {
  4440. struct block_list dummy_bl;
  4441. dummy_bl.type = BL_NUL;
  4442. dummy_bl.x = x;
  4443. dummy_bl.y = y;
  4444. dummy_bl.m = m;
  4445. clif_send(buf,packet_len(0x192),&dummy_bl,target);
  4446. }
  4447. }
  4448. /// Notifies the client about an item on floor (ZC_ITEM_ENTRY).
  4449. /// 009d <id>.L <name id>.W <identified>.B <x>.W <y>.W <amount>.W <subX>.B <subY>.B
  4450. void clif_getareachar_item( map_session_data* sd,struct flooritem_data* fitem ){
  4451. nullpo_retv( sd );
  4452. nullpo_retv( fitem );
  4453. PACKET_ZC_ITEM_ENTRY p = {};
  4454. p.packetType = HEADER_ZC_ITEM_ENTRY;
  4455. p.AID = fitem->bl.id;
  4456. p.itemId = client_nameid( fitem->item.nameid );
  4457. p.identify = fitem->item.identify;
  4458. p.x = fitem->bl.x;
  4459. p.y = fitem->bl.y;
  4460. p.amount = fitem->item.amount;
  4461. p.subX = fitem->subx;
  4462. p.subY = fitem->suby;
  4463. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  4464. }
  4465. /// Notifes client about Graffiti
  4466. /// 01c9 <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B <has msg>.B <msg>.80B (ZC_SKILL_ENTRY2)
  4467. static void clif_graffiti(struct block_list *bl, struct skill_unit *unit, enum send_target target) {
  4468. unsigned char buf[128];
  4469. nullpo_retv(bl);
  4470. nullpo_retv(unit);
  4471. WBUFW(buf, 0) = 0x1c9;
  4472. WBUFL(buf, 2) = unit->bl.id;
  4473. WBUFL(buf, 6) = unit->group->src_id;
  4474. WBUFW(buf,10) = unit->bl.x;
  4475. WBUFW(buf,12) = unit->bl.y;
  4476. WBUFB(buf,14) = unit->group->unit_id;
  4477. WBUFB(buf,15) = 1;
  4478. WBUFB(buf,16) = 1;
  4479. safestrncpy(WBUFCP(buf,17),unit->group->valstr,MESSAGE_SIZE);
  4480. clif_send(buf,packet_len(0x1c9),bl,target);
  4481. }
  4482. /// Notifies the client of a skill unit.
  4483. /// 011f <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B (ZC_SKILL_ENTRY)
  4484. /// 08c7 <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.B <range>.W <visible>.B (ZC_SKILL_ENTRY3)
  4485. /// 099f <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.L <range>.W <visible>.B (ZC_SKILL_ENTRY4)
  4486. /// 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)
  4487. void clif_getareachar_skillunit(struct block_list *bl, struct skill_unit *unit, enum send_target target, bool visible) {
  4488. int header = 0, unit_id = 0, pos = 0, fd = 0, len = -1;
  4489. unsigned char buf[128];
  4490. nullpo_retv(bl);
  4491. nullpo_retv(unit);
  4492. if (bl->type == BL_PC)
  4493. fd = ((TBL_PC*)bl)->fd;
  4494. if (unit->group->state.guildaura)
  4495. return;
  4496. if (unit->group->state.song_dance&0x1 && unit->val2&(1 << UF_ENSEMBLE))
  4497. unit_id = unit->val2&(1 << UF_SONG) ? UNT_DISSONANCE : UNT_UGLYDANCE;
  4498. else if (skill_get_unit_flag(unit->group->skill_id, UF_RANGEDSINGLEUNIT) && !(unit->val2 & (1 << UF_RANGEDSINGLEUNIT)))
  4499. unit_id = UNT_DUMMYSKILL; // Use invisible unit id for other case of rangedsingle unit
  4500. else
  4501. unit_id = unit->group->unit_id;
  4502. if (!visible)
  4503. unit_id = UNT_DUMMYSKILL; // Hack to makes hidden trap really hidden!
  4504. #if PACKETVER >= 3
  4505. if (unit_id == UNT_GRAFFITI) { // Graffiti [Valaris]
  4506. clif_graffiti(bl, unit, target);
  4507. return;
  4508. }
  4509. #endif
  4510. #if PACKETVER <= 20120702
  4511. header = 0x011f;
  4512. //#if PACKETVER < 20110718
  4513. // header = 0x011f;
  4514. //#elif PACKETVER < 20121212
  4515. // header = 0x08c7;
  4516. #elif PACKETVER < 20130731
  4517. header = 0x099f;
  4518. #else
  4519. header = 0x09ca;
  4520. #endif
  4521. len = packet_len(header);
  4522. WBUFW(buf,pos) = header;
  4523. if (header != 0x011f) {
  4524. WBUFW(buf, pos+2) = len;
  4525. pos += 2;
  4526. }
  4527. WBUFL(buf,pos+2) = unit->bl.id;
  4528. WBUFL(buf,pos+6) = unit->group->src_id;
  4529. WBUFW(buf,pos+10) = unit->bl.x;
  4530. WBUFW(buf,pos+12) = unit->bl.y;
  4531. switch (header) {
  4532. case 0x011f:
  4533. WBUFB(buf,pos+14) = unit_id;
  4534. WBUFB(buf,pos+15) = visible;
  4535. break;
  4536. case 0x08c7:
  4537. WBUFB(buf,pos+14) = unit_id;
  4538. WBUFW(buf,pos+15) = unit->range;
  4539. WBUFB(buf,pos+17) = visible;
  4540. break;
  4541. case 0x099f:
  4542. WBUFL(buf,pos+14) = unit_id;
  4543. WBUFW(buf,pos+18) = unit->range;
  4544. WBUFB(buf,pos+20) = visible;
  4545. break;
  4546. case 0x09ca:
  4547. WBUFL(buf,pos+14) = unit_id;
  4548. WBUFB(buf,pos+18) = (unsigned char)unit->range;
  4549. WBUFB(buf,pos+19) = visible;
  4550. WBUFB(buf,pos+20) = (unsigned char)unit->group->skill_lv;
  4551. break;
  4552. }
  4553. clif_send(buf, len, bl, target);
  4554. if (unit->group->skill_id == WZ_ICEWALL)
  4555. clif_changemapcell(fd, unit->bl.m, unit->bl.x, unit->bl.y, 5, SELF);
  4556. }
  4557. /// 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)
  4558. void clif_skill_unit_test(struct block_list *bl, short x, short y, int unit_id, short range, short skill_lv) {
  4559. unsigned char buf[128];
  4560. nullpo_retv(bl);
  4561. WBUFW(buf, 0) = 0x09ca;
  4562. WBUFW(buf, 2) = packet_len(0x09ca);
  4563. WBUFL(buf, 4) = 1000;
  4564. WBUFL(buf, 8) = 2000;
  4565. WBUFW(buf, 12) = x;
  4566. WBUFW(buf, 14) = y;
  4567. WBUFL(buf, 16) = unit_id;
  4568. WBUFB(buf, 20) = (unsigned char)range;
  4569. WBUFB(buf, 21) = 1;
  4570. WBUFB(buf, 22) = (unsigned char)skill_lv;
  4571. clif_send(buf, packet_len(0x09ca), bl, AREA);
  4572. }
  4573. /// Server tells client to remove unit of id 'unit->bl.id'
  4574. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4575. static void clif_clearchar_skillunit( skill_unit& unit, map_session_data& sd ){
  4576. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4577. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4578. packet.GID = unit.bl.id;
  4579. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4580. if( unit.group && unit.group->skill_id == WZ_ICEWALL ){
  4581. clif_changemapcell( sd.fd, unit.bl.m, unit.bl.x, unit.bl.y, unit.val2, SELF );
  4582. }
  4583. }
  4584. /// Removes a skill unit.
  4585. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4586. void clif_skill_delunit( skill_unit& unit ){
  4587. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4588. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4589. packet.GID = unit.bl.id;
  4590. clif_send( &packet, sizeof( packet ), &unit.bl, AREA );
  4591. }
  4592. /// Sent when an object gets ankle-snared.
  4593. /// 01ac <id>.L (ZC_SKILL_UPDATE)
  4594. /// Only affects units with class [139,153] client-side.
  4595. void clif_skillunit_update( block_list& bl ){
  4596. PACKET_ZC_SKILL_UPDATE packet{};
  4597. packet.packetType = HEADER_ZC_SKILL_UPDATE;
  4598. packet.GID = bl.id;
  4599. clif_send( &packet, sizeof( packet ), &bl, AREA );
  4600. }
  4601. /*==========================================
  4602. *
  4603. *------------------------------------------*/
  4604. static int clif_getareachar(struct block_list* bl,va_list ap)
  4605. {
  4606. map_session_data *sd;
  4607. nullpo_ret(bl);
  4608. sd=va_arg(ap,map_session_data*);
  4609. if (!clif_session_isValid(sd))
  4610. return 0;
  4611. switch(bl->type){
  4612. case BL_ITEM:
  4613. clif_getareachar_item(sd,(struct flooritem_data*) bl);
  4614. break;
  4615. case BL_SKILL:
  4616. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &sd->bl);
  4617. break;
  4618. default:
  4619. if(&sd->bl == bl)
  4620. break;
  4621. clif_getareachar_unit(sd,bl);
  4622. break;
  4623. }
  4624. return 0;
  4625. }
  4626. /*==========================================
  4627. * tbl has gone out of view-size of bl
  4628. *------------------------------------------*/
  4629. int clif_outsight(struct block_list *bl,va_list ap)
  4630. {
  4631. struct block_list *tbl;
  4632. struct view_data *vd;
  4633. TBL_PC *sd, *tsd;
  4634. tbl=va_arg(ap,struct block_list*);
  4635. if(bl == tbl) return 0;
  4636. sd = BL_CAST(BL_PC, bl);
  4637. tsd = BL_CAST(BL_PC, tbl);
  4638. if (clif_session_isValid(tsd)) { //tsd has lost sight of the bl object.
  4639. nullpo_ret(bl);
  4640. switch(bl->type){
  4641. case BL_PC:
  4642. if(sd->vd.class_ != JT_INVISIBLE)
  4643. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4644. if(sd->chatID){
  4645. struct chat_data *cd;
  4646. cd=(struct chat_data*)map_id2bl(sd->chatID);
  4647. if(cd->usersd[0]==sd)
  4648. clif_dispchat(cd,tsd->fd);
  4649. }
  4650. if(sd->state.vending)
  4651. clif_closevendingboard(bl,tsd->fd);
  4652. if(sd->state.buyingstore)
  4653. clif_buyingstore_disappear_entry( *sd, &tsd->bl );
  4654. break;
  4655. case BL_ITEM:
  4656. clif_clearflooritem( *reinterpret_cast<flooritem_data*>( bl ), tsd );
  4657. break;
  4658. case BL_SKILL:
  4659. clif_clearchar_skillunit( *((skill_unit *)bl), *tsd );
  4660. break;
  4661. case BL_NPC:
  4662. if(!(((TBL_NPC*)bl)->is_invisible))
  4663. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4664. break;
  4665. default:
  4666. if((vd=status_get_viewdata(bl)) && vd->class_ != JT_INVISIBLE)
  4667. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4668. break;
  4669. }
  4670. }
  4671. if (clif_session_isValid(sd)) { //sd is watching tbl go out of view.
  4672. nullpo_ret(tbl);
  4673. if(tbl->type == BL_SKILL) //Trap knocked out of sight
  4674. clif_clearchar_skillunit( *((skill_unit *)tbl), *sd );
  4675. else if(((vd=status_get_viewdata(tbl)) && vd->class_ != JT_INVISIBLE) &&
  4676. !(tbl->type == BL_NPC && (((TBL_NPC*)tbl)->is_invisible)))
  4677. clif_clearunit_single( tbl->id, CLR_OUTSIGHT, *sd );
  4678. }
  4679. return 0;
  4680. }
  4681. /*==========================================
  4682. * tbl has come into view of bl
  4683. *------------------------------------------*/
  4684. int clif_insight(struct block_list *bl,va_list ap)
  4685. {
  4686. struct block_list *tbl;
  4687. TBL_PC *sd, *tsd;
  4688. tbl=va_arg(ap,struct block_list*);
  4689. if (bl == tbl) return 0;
  4690. sd = BL_CAST(BL_PC, bl);
  4691. tsd = BL_CAST(BL_PC, tbl);
  4692. if (clif_session_isValid(tsd)) { //Tell tsd that bl entered into his view
  4693. switch(bl->type){
  4694. case BL_ITEM:
  4695. clif_getareachar_item(tsd,(struct flooritem_data*)bl);
  4696. break;
  4697. case BL_SKILL:
  4698. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &tsd->bl);
  4699. break;
  4700. default:
  4701. clif_getareachar_unit(tsd,bl);
  4702. break;
  4703. }
  4704. }
  4705. if (clif_session_isValid(sd)) { //Tell sd that tbl walked into his view
  4706. clif_getareachar_unit(sd,tbl);
  4707. }
  4708. return 0;
  4709. }
  4710. /// Updates whole skill tree (ZC_SKILLINFO_LIST).
  4711. /// 010f <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  4712. void clif_skillinfoblock(map_session_data *sd)
  4713. {
  4714. int fd;
  4715. int i,len,id;
  4716. nullpo_retv(sd);
  4717. fd = sd->fd;
  4718. if (!session_isActive(fd))
  4719. return;
  4720. WFIFOHEAD(fd, MAX_SKILL * 37 + 4);
  4721. WFIFOW(fd,0) = 0x10f;
  4722. bool haveCallPartnerSkill = false;
  4723. for ( i = 0, len = 4; i < MAX_SKILL; i++)
  4724. {
  4725. if( (id = sd->status.skill[i].id) != 0 )
  4726. {
  4727. // workaround for bugreport:5348
  4728. if (len + 37 > 8192)
  4729. break;
  4730. // skip WE_CALLPARTNER and send it in special way
  4731. if (id == WE_CALLPARTNER) {
  4732. haveCallPartnerSkill = true;
  4733. continue;
  4734. }
  4735. WFIFOW(fd,len) = id;
  4736. WFIFOL(fd,len+2) = skill_get_inf(id);
  4737. WFIFOW(fd,len+6) = sd->status.skill[i].lv;
  4738. WFIFOW(fd,len+8) = skill_get_sp(id,sd->status.skill[i].lv);
  4739. WFIFOW(fd,len+10)= skill_get_range2(&sd->bl,id,sd->status.skill[i].lv,false);
  4740. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  4741. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  4742. WFIFOB(fd,len+36) = (sd->status.skill[i].lv < skill_tree_get_max(id, sd->status.class_))? 1:0;
  4743. else
  4744. WFIFOB(fd,len+36) = 0;
  4745. len += 37;
  4746. }
  4747. }
  4748. WFIFOW(fd,2)=len;
  4749. WFIFOSET(fd,len);
  4750. // adoption fix
  4751. if (haveCallPartnerSkill) {
  4752. clif_addskill(sd, WE_CALLPARTNER);
  4753. clif_skillinfo(sd, WE_CALLPARTNER, 0);
  4754. }
  4755. // workaround for bugreport:5348; send the remaining skills one by one to bypass packet size limit
  4756. for ( ; i < MAX_SKILL; i++)
  4757. {
  4758. if( (id = sd->status.skill[i].id) != 0 && ( id != WE_CALLPARTNER || !haveCallPartnerSkill ) )
  4759. {
  4760. clif_addskill(sd, id);
  4761. clif_skillinfo(sd, id, 0);
  4762. }
  4763. }
  4764. }
  4765. /**
  4766. * Server tells client 'sd' to add skill of id 'id' to it's skill tree (e.g. with Ice Falcion item)
  4767. **/
  4768. /// Adds new skill to the skill tree (ZC_ADD_SKILL).
  4769. /// 0111 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B
  4770. void clif_addskill(map_session_data *sd, int skill_id)
  4771. {
  4772. nullpo_retv(sd);
  4773. int fd = sd->fd;
  4774. uint16 idx = skill_get_index(skill_id);
  4775. if (!session_isActive(fd) || !idx)
  4776. return;
  4777. if( sd->status.skill[idx].id <= 0 )
  4778. return;
  4779. WFIFOHEAD(fd, packet_len(0x111));
  4780. WFIFOW(fd,0) = 0x111;
  4781. WFIFOW(fd,2) = skill_id;
  4782. WFIFOL(fd,4) = skill_get_inf(skill_id);
  4783. WFIFOW(fd,8) = sd->status.skill[idx].lv;
  4784. WFIFOW(fd,10) = skill_get_sp(skill_id,sd->status.skill[idx].lv);
  4785. WFIFOW(fd,12)= skill_get_range2(&sd->bl,skill_id,sd->status.skill[idx].lv,false);
  4786. safestrncpy(WFIFOCP(fd,14), skill_get_name(skill_id), NAME_LENGTH);
  4787. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  4788. WFIFOB(fd,38) = (sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_))? 1:0;
  4789. else
  4790. WFIFOB(fd,38) = 0;
  4791. WFIFOSET(fd,packet_len(0x111));
  4792. }
  4793. /// Deletes a skill from the skill tree (ZC_SKILLINFO_DELETE).
  4794. /// 0441 <skill id>.W
  4795. void clif_deleteskill(map_session_data *sd, int skill_id, bool skip_infoblock)
  4796. {
  4797. #if PACKETVER >= 20081217
  4798. nullpo_retv(sd);
  4799. int fd = sd->fd;
  4800. uint16 idx = skill_get_index(skill_id);
  4801. if (!session_isActive(fd) || !idx)
  4802. return;
  4803. WFIFOHEAD(fd,packet_len(0x441));
  4804. WFIFOW(fd,0) = 0x441;
  4805. WFIFOW(fd,2) = skill_id;
  4806. WFIFOSET(fd,packet_len(0x441));
  4807. #endif
  4808. #if PACKETVER_MAIN_NUM >= 20190807 || PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190918
  4809. if (!skip_infoblock)
  4810. #endif
  4811. clif_skillinfoblock(sd);
  4812. }
  4813. /// Updates a skill in the skill tree (ZC_SKILLINFO_UPDATE).
  4814. /// 010e <skill id>.W <level>.W <sp cost>.W <attack range>.W <upgradable>.B
  4815. void clif_skillup(map_session_data *sd, uint16 skill_id, int lv, int range, int upgradable) {
  4816. nullpo_retv(sd);
  4817. int fd = sd->fd;
  4818. uint16 idx = skill_get_index(skill_id);
  4819. if (!session_isActive(fd) || !idx)
  4820. return;
  4821. WFIFOHEAD(fd, packet_len(0x10e));
  4822. WFIFOW(fd, 0) = 0x10e;
  4823. WFIFOW(fd, 2) = skill_id;
  4824. WFIFOW(fd, 4) = lv;
  4825. WFIFOW(fd, 6) = skill_get_sp(skill_id, lv);
  4826. WFIFOW(fd, 8) = range;
  4827. WFIFOB(fd, 10) = upgradable;
  4828. WFIFOSET(fd, packet_len(0x10e));
  4829. }
  4830. /// Updates a skill in the skill tree (ZC_SKILLINFO_UPDATE2).
  4831. /// 07e1 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <upgradable>.B
  4832. void clif_skillinfo(map_session_data *sd,int skill_id, int inf)
  4833. {
  4834. nullpo_retv(sd);
  4835. const int fd = sd->fd;
  4836. uint16 idx = skill_get_index(skill_id);
  4837. if (!session_isActive(fd) || !idx)
  4838. return;
  4839. WFIFOHEAD(fd,packet_len(0x7e1));
  4840. WFIFOW(fd,0) = 0x7e1;
  4841. WFIFOW(fd,2) = skill_id;
  4842. WFIFOL(fd,4) = inf?inf:skill_get_inf(skill_id);
  4843. WFIFOW(fd,8) = sd->status.skill[idx].lv;
  4844. WFIFOW(fd,10) = skill_get_sp(skill_id,sd->status.skill[idx].lv);
  4845. WFIFOW(fd,12) = skill_get_range2(&sd->bl,skill_id,sd->status.skill[idx].lv,false);
  4846. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  4847. WFIFOB(fd,14) = (sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_))? 1:0;
  4848. else
  4849. WFIFOB(fd,14) = 0;
  4850. WFIFOSET(fd,packet_len(0x7e1));
  4851. }
  4852. void clif_skill_scale( struct block_list *bl, int src_id, int x, int y, uint16 skill_id, uint16 skill_lv, int casttime ){
  4853. #if PACKETVER >= 20151223
  4854. if( !battle_config.show_skill_scale ){
  4855. return;
  4856. }
  4857. PACKET_ZC_SKILL_SCALE p = {};
  4858. p.PacketType = skillscale;
  4859. p.AID = src_id;
  4860. p.skill_id = skill_id;
  4861. p.skill_lv = skill_lv;
  4862. p.x = x;
  4863. p.y = y;
  4864. p.casttime = casttime;
  4865. if( disguised( bl ) ){
  4866. clif_send( &p, sizeof( p ), bl, AREA_WOS );
  4867. p.AID = disguised_bl_id( bl->id );
  4868. clif_send( &p, sizeof( p ), bl, SELF );
  4869. }else{
  4870. clif_send( &p, sizeof( p ), bl, AREA );
  4871. }
  4872. #endif
  4873. }
  4874. /// Notifies clients in area, that an object is about to use a skill.
  4875. /// 013e <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L (ZC_USESKILL_ACK)
  4876. /// 07fb <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L <is disposable>.B (ZC_USESKILL_ACK2)
  4877. /// property:
  4878. /// 0 = Yellow cast aura
  4879. /// 1 = Water elemental cast aura
  4880. /// 2 = Earth elemental cast aura
  4881. /// 3 = Fire elemental cast aura
  4882. /// 4 = Wind elemental cast aura
  4883. /// 5 = Poison elemental cast aura
  4884. /// 6 = Holy elemental cast aura
  4885. /// ? = like 0
  4886. /// is disposable:
  4887. /// 0 = yellow chat text "[src name] will use skill [skill name]."
  4888. /// 1 = no text
  4889. 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)
  4890. {
  4891. #if PACKETVER < 20091124
  4892. const int cmd = 0x13e;
  4893. #else
  4894. const int cmd = 0x7fb;
  4895. #endif
  4896. unsigned char buf[32];
  4897. WBUFW(buf,0) = cmd;
  4898. WBUFL(buf,2) = src_id;
  4899. WBUFL(buf,6) = dst_id;
  4900. WBUFW(buf,10) = dst_x;
  4901. WBUFW(buf,12) = dst_y;
  4902. WBUFW(buf,14) = skill_id;
  4903. WBUFL(buf,16) = property<0?0:property; //Avoid sending negatives as element [Skotlex]
  4904. WBUFL(buf,20) = casttime;
  4905. #if PACKETVER >= 20091124
  4906. WBUFB(buf,24) = 0; // isDisposable
  4907. #endif
  4908. if (disguised(bl)) {
  4909. clif_send(buf,packet_len(cmd), bl, AREA_WOS);
  4910. WBUFL(buf,2) = disguised_bl_id( src_id );
  4911. clif_send(buf,packet_len(cmd), bl, SELF);
  4912. } else
  4913. clif_send(buf,packet_len(cmd), bl, AREA);
  4914. if( skill_get_inf2( skill_id, INF2_SHOWSCALE ) ){
  4915. clif_skill_scale( bl, src_id, bl->x, bl->y, skill_id, skill_lv, casttime );
  4916. }
  4917. }
  4918. /// Notifies clients in area, that an object canceled casting.
  4919. /// 01b9 <id>.L (ZC_DISPEL)
  4920. void clif_skillcastcancel( block_list& bl ){
  4921. PACKET_ZC_DISPEL packet{};
  4922. packet.packetType = HEADER_ZC_DISPEL;
  4923. packet.gid = bl.id;
  4924. clif_send( &packet, sizeof(PACKET_ZC_DISPEL), &bl, AREA );
  4925. }
  4926. /// Notifies the client about the result of a skill use request (ZC_ACK_TOUSESKILL).
  4927. /// 0110 <skill id>.W <num>.L <result>.B <cause>.B
  4928. /// num (only used when skill id = NV_BASIC and cause = 0):
  4929. /// 0 = "skill failed" MsgStringTable[159]
  4930. /// 1 = "no emotions" MsgStringTable[160]
  4931. /// 2 = "no sit" MsgStringTable[161]
  4932. /// 3 = "no chat" MsgStringTable[162]
  4933. /// 4 = "no party" MsgStringTable[163]
  4934. /// 5 = "no shout" MsgStringTable[164]
  4935. /// 6 = "no PKing" MsgStringTable[165]
  4936. /// 7 = "no alligning" MsgStringTable[383]
  4937. /// ? = ignored
  4938. /// cause:
  4939. /// 0 = "not enough skill level" MsgStringTable[214] (AL_WARP)
  4940. /// "steal failed" MsgStringTable[205] (TF_STEAL)
  4941. /// "envenom failed" MsgStringTable[207] (TF_POISON)
  4942. /// "skill failed" MsgStringTable[204] (otherwise)
  4943. /// ... = @see enum useskill_fail_cause
  4944. /// ? = ignored
  4945. ///
  4946. /// if(result!=0) doesn't display any of the previous messages
  4947. /// Note: when this packet is received an unknown flag is always set to 0,
  4948. /// suggesting this is an ACK packet for the UseSkill packets and should be sent on success too [FlavioJS]
  4949. void clif_skill_fail( map_session_data& sd, uint16 skill_id, enum useskill_fail_cause cause, int btype, t_itemid itemId ){
  4950. if(battle_config.display_skill_fail&1)
  4951. return; //Disable all skill failed messages
  4952. if(cause==USESKILL_FAIL_SKILLINTERVAL && !sd.state.showdelay)
  4953. return; //Disable delay failed messages
  4954. if(skill_id == RG_SNATCHER && battle_config.display_skill_fail&4)
  4955. return;
  4956. if(skill_id == TF_POISON && battle_config.display_skill_fail&8)
  4957. return;
  4958. PACKET_ZC_ACK_TOUSESKILL p = {};
  4959. p.packetType = HEADER_ZC_ACK_TOUSESKILL;
  4960. p.skillId = skill_id;
  4961. p.btype = btype;
  4962. p.itemId = itemId;
  4963. p.flag = 0; // 0 - failed
  4964. p.cause = cause;
  4965. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  4966. }
  4967. /// Skill cooldown display icon.
  4968. /// 043d <skill ID>.W <tick>.L (ZC_SKILL_POSTDELAY)
  4969. void clif_skill_cooldown( map_session_data &sd, uint16 skill_id, t_tick tick ){
  4970. #if PACKETVER_MAIN_NUM >= 20081112 || PACKETVER_RE_NUM >= 20081111 || defined(PACKETVER_ZERO)
  4971. PACKET_ZC_SKILL_POSTDELAY packet{};
  4972. packet.PacketType = HEADER_ZC_SKILL_POSTDELAY;
  4973. packet.SKID = skill_id;
  4974. packet.DelayTM = client_tick(tick);
  4975. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4976. #endif
  4977. }
  4978. /// Skill attack effect and damage.
  4979. /// 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)
  4980. /// 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)
  4981. 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)
  4982. {
  4983. unsigned char buf[64];
  4984. status_change *sc;
  4985. int damage = (int)cap_value(sdamage,INT_MIN,INT_MAX);
  4986. nullpo_ret(src);
  4987. nullpo_ret(dst);
  4988. type = clif_calc_delay(type,div,damage,ddelay);
  4989. if( ( sc = status_get_sc(dst) ) && sc->count ) {
  4990. if(sc->getSCE(SC_HALLUCINATION) && damage)
  4991. damage = clif_hallucination_damage();
  4992. }
  4993. #if PACKETVER < 3
  4994. WBUFW(buf,0)=0x114;
  4995. WBUFW(buf,2)=skill_id;
  4996. WBUFL(buf,4)=src->id;
  4997. WBUFL(buf,8)=dst->id;
  4998. WBUFL(buf,12)=client_tick(tick);
  4999. WBUFL(buf,16)=sdelay;
  5000. WBUFL(buf,20)=ddelay;
  5001. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5002. WBUFW(buf,24)=damage?div:0;
  5003. } else {
  5004. WBUFW(buf,24)=damage;
  5005. }
  5006. WBUFW(buf,26)=skill_lv;
  5007. WBUFW(buf,28)=div;
  5008. WBUFB(buf,30)=type;
  5009. if (disguised(dst)) {
  5010. clif_send(buf,packet_len(0x114),dst,AREA_WOS);
  5011. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5012. clif_send(buf,packet_len(0x114),dst,SELF);
  5013. } else
  5014. clif_send(buf,packet_len(0x114),dst,AREA);
  5015. if(disguised(src)) {
  5016. WBUFL(buf,4)=disguised_bl_id(src->id);
  5017. if (disguised(dst))
  5018. WBUFL(buf,8)=dst->id;
  5019. if(damage > 0)
  5020. WBUFW(buf,24)=-1;
  5021. clif_send(buf,packet_len(0x114),src,SELF);
  5022. }
  5023. #else
  5024. WBUFW(buf,0)=0x1de;
  5025. WBUFW(buf,2)=skill_id;
  5026. WBUFL(buf,4)=src->id;
  5027. WBUFL(buf,8)=dst->id;
  5028. WBUFL(buf,12)=client_tick(tick);
  5029. WBUFL(buf,16)=sdelay;
  5030. WBUFL(buf,20)=ddelay;
  5031. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5032. WBUFL(buf,24)=damage?div:0;
  5033. } else {
  5034. WBUFL(buf,24)=damage;
  5035. }
  5036. WBUFW(buf,28)=skill_lv;
  5037. WBUFW(buf,30)=div;
  5038. // For some reason, late 2013 and newer clients have
  5039. // a issue that causes players and monsters to endure
  5040. // type 6 (ACTION_SKILL) skills. So we have to do a small
  5041. // hack to set all type 6 to be sent as type 8 ACTION_ATTACK_MULTIPLE
  5042. #if PACKETVER < 20131223
  5043. WBUFB(buf,32)=type;
  5044. #else
  5045. WBUFB(buf,32)=( type == DMG_SINGLE ) ? DMG_MULTI_HIT : type;
  5046. #endif
  5047. if (disguised(dst)) {
  5048. clif_send(buf,packet_len(0x1de),dst,AREA_WOS);
  5049. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5050. clif_send(buf,packet_len(0x1de),dst,SELF);
  5051. } else
  5052. clif_send(buf,packet_len(0x1de),dst,AREA);
  5053. if(disguised(src)) {
  5054. WBUFL(buf,4)=disguised_bl_id(src->id);
  5055. if (disguised(dst))
  5056. WBUFL(buf,8)=dst->id;
  5057. if(damage > 0)
  5058. WBUFL(buf,24)=-1;
  5059. clif_send(buf,packet_len(0x1de),src,SELF);
  5060. }
  5061. #endif
  5062. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5063. return clif_calc_walkdelay(dst,ddelay,type,damage,div);
  5064. }
  5065. /// Ground skill attack effect and damage (ZC_NOTIFY_SKILL_POSITION).
  5066. /// 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
  5067. /*
  5068. 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)
  5069. {
  5070. unsigned char buf[64];
  5071. status_change *sc;
  5072. nullpo_ret(src);
  5073. nullpo_ret(dst);
  5074. type = (type>DMG_NORMAL)?type:skill_get_hit(skill_id);
  5075. type = clif_calc_delay(type,div,damage,ddelay);
  5076. sc = status_get_sc(dst);
  5077. if(sc && sc->count) {
  5078. if(sc->getSCE(SC_HALLUCINATION) && damage)
  5079. damage = clif_hallucination_damage();
  5080. }
  5081. WBUFW(buf,0)=0x115;
  5082. WBUFW(buf,2)=skill_id;
  5083. WBUFL(buf,4)=src->id;
  5084. WBUFL(buf,8)=dst->id;
  5085. WBUFL(buf,12)=client_tick(tick);
  5086. WBUFL(buf,16)=sdelay;
  5087. WBUFL(buf,20)=ddelay;
  5088. WBUFW(buf,24)=dst->x;
  5089. WBUFW(buf,26)=dst->y;
  5090. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5091. WBUFW(buf,28)=damage?div:0;
  5092. } else {
  5093. WBUFW(buf,28)=damage;
  5094. }
  5095. WBUFW(buf,30)=skill_lv;
  5096. WBUFW(buf,32)=div;
  5097. WBUFB(buf,34)=type;
  5098. clif_send(buf,packet_len(0x115),src,AREA);
  5099. if(disguised(src)) {
  5100. WBUFL(buf,4)=disguised_bl_id(src->id);
  5101. if(damage > 0)
  5102. WBUFW(buf,28)=-1;
  5103. clif_send(buf,packet_len(0x115),src,SELF);
  5104. }
  5105. if (disguised(dst)) {
  5106. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5107. if (disguised(src))
  5108. WBUFL(buf,4)=src->id;
  5109. else if(damage > 0)
  5110. WBUFW(buf,28)=-1;
  5111. clif_send(buf,packet_len(0x115),dst,SELF);
  5112. }
  5113. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5114. return clif_calc_walkdelay(dst,ddelay,type,damage,div);
  5115. }
  5116. */
  5117. /// Non-damaging skill effect
  5118. /// 011a <skill id>.W <heal>.W <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL).
  5119. /// 09cb <skill id>.W <heal>.L <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL2).
  5120. bool clif_skill_nodamage(struct block_list *src,struct block_list *dst, uint16 skill_id, int heal, t_tick tick)
  5121. {
  5122. unsigned char buf[17];
  5123. #if PACKETVER < 20130731
  5124. const int cmd = 0x11a;
  5125. #else
  5126. const int cmd = 0x9cb;
  5127. #endif
  5128. int offset = 0;
  5129. bool success = ( tick != 0 );
  5130. nullpo_ret(dst);
  5131. WBUFW(buf,0) = cmd;
  5132. WBUFW(buf,2) = skill_id;
  5133. #if PACKETVER < 20130731
  5134. WBUFW(buf,4) = min(heal, INT16_MAX);
  5135. #else
  5136. WBUFL(buf,4) = min(heal, INT32_MAX);
  5137. offset += 2;
  5138. #endif
  5139. WBUFL(buf,6+offset) = dst->id;
  5140. WBUFL(buf,10+offset) = src ? src->id : 0;
  5141. WBUFB(buf,14+offset) = success;
  5142. if (disguised(dst)) {
  5143. clif_send(buf, packet_len(cmd), dst, AREA_WOS);
  5144. WBUFL(buf,6+offset) = disguised_bl_id(dst->id);
  5145. clif_send(buf, packet_len(cmd), dst, SELF);
  5146. } else
  5147. clif_send(buf, packet_len(cmd), dst, AREA);
  5148. if(src && disguised(src)) {
  5149. WBUFL(buf,10+offset) = disguised_bl_id(src->id);
  5150. if (disguised(dst))
  5151. WBUFL(buf,6+offset) = dst->id;
  5152. clif_send(buf, packet_len(cmd), src, SELF);
  5153. }
  5154. return success;
  5155. }
  5156. /// Non-damaging ground skill effect (ZC_NOTIFY_GROUNDSKILL).
  5157. /// 0117 <skill id>.W <src id>.L <level>.W <x>.W <y>.W <tick>.L
  5158. void clif_skill_poseffect(struct block_list *src,uint16 skill_id,int val,int x,int y,t_tick tick)
  5159. {
  5160. unsigned char buf[32];
  5161. nullpo_retv(src);
  5162. WBUFW(buf,0)=0x117;
  5163. WBUFW(buf,2)=skill_id;
  5164. WBUFL(buf,4)=src->id;
  5165. WBUFW(buf,8)=val;
  5166. WBUFW(buf,10)=x;
  5167. WBUFW(buf,12)=y;
  5168. WBUFL(buf,14)=client_tick(tick);
  5169. if(disguised(src)) {
  5170. clif_send(buf,packet_len(0x117),src,AREA_WOS);
  5171. WBUFL(buf,4)=disguised_bl_id(src->id);
  5172. clif_send(buf,packet_len(0x117),src,SELF);
  5173. } else
  5174. clif_send(buf,packet_len(0x117),src,AREA);
  5175. }
  5176. /// Presents a list of available warp destinations.
  5177. /// 011c <skill id>.W { <map name>.16B }*4 (ZC_WARPLIST)
  5178. /// 0abe <lenght>.W <skill id>.W { <map name>.16B }*? (ZC_WARPLIST2)
  5179. void clif_skill_warppoint( map_session_data& sd, uint16 skill_id, uint16 skill_lv, std::vector<std::string>& maps ){
  5180. if(maps.empty())
  5181. return;
  5182. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  5183. PACKET_ZC_WARPLIST* p = reinterpret_cast<PACKET_ZC_WARPLIST*>( packet_buffer );
  5184. p->packetType = HEADER_ZC_WARPLIST;
  5185. p->packetLength = sizeof( *p );
  5186. p->skillId = skill_id;
  5187. size_t memoCount = 0;
  5188. for( std::string& map : maps ){
  5189. if( map.empty() ){
  5190. continue;
  5191. }
  5192. PACKET_ZC_WARPLIST_sub& warp = p->maps[memoCount];
  5193. mapindex_getmapname_ext( map.c_str(), warp.map );
  5194. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( warp ) );
  5195. memoCount++;
  5196. }
  5197. clif_send( p, p->packetLength, &sd.bl, SELF );
  5198. #else
  5199. PACKET_ZC_WARPLIST p = {};
  5200. p.packetType = HEADER_ZC_WARPLIST;
  5201. p.skillId = skill_id;
  5202. size_t memoCount = 0, max = 4;
  5203. for( std::string& map : maps ){
  5204. if( map.empty() ){
  5205. continue;
  5206. }
  5207. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5208. mapindex_getmapname_ext( map.c_str(), warp.map );
  5209. if( memoCount++ == max ){
  5210. break;
  5211. }
  5212. }
  5213. for( ; memoCount < max; memoCount++ ){
  5214. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5215. strcpy( warp.map, "" );
  5216. }
  5217. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  5218. #endif
  5219. sd.menuskill_id = skill_id;
  5220. if (skill_id == AL_WARP) {
  5221. sd.menuskill_val = (sd.ud.skillx<<16)|sd.ud.skilly; //Store warp position here.
  5222. sd.state.workinprogress = WIP_DISABLE_ALL;
  5223. } else
  5224. sd.menuskill_val = skill_lv;
  5225. }
  5226. /// Memo message.
  5227. /// 011e <type>.B (ZC_ACK_REMEMBER_WARPPOINT)
  5228. /// type:
  5229. /// 0 = "Saved location as a Memo Point for Warp skill." in color 0xFFFF00 (cyan)
  5230. /// 1 = "Skill Level is not high enough." in color 0x0000FF (red)
  5231. /// 2 = "You haven't learned Warp." in color 0x0000FF (red)
  5232. ///
  5233. /// @param sd Who receives the message
  5234. /// @param type What message
  5235. void clif_skill_memomessage( map_session_data& sd, e_ack_remember_warppoint_result result ){
  5236. PACKET_ZC_ACK_REMEMBER_WARPPOINT packet{};
  5237. packet.packetType = HEADER_ZC_ACK_REMEMBER_WARPPOINT;
  5238. packet.type = static_cast<decltype(packet.type)>(result);
  5239. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5240. }
  5241. /// Teleport message.
  5242. /// 0189 <type>.W (ZC_NOTIFY_MAPINFO)
  5243. /// type:
  5244. /// 0 = "Unable to Teleport in this area" in color 0xFFFF00 (cyan)
  5245. /// 1 = "Saved point cannot be memorized." in color 0x0000FF (red)
  5246. /// 2 = "This skill cannot be used in this area"
  5247. /// 3 = "This item cannot be used in this area"
  5248. ///
  5249. /// @param sd Who receives the message
  5250. /// @param type What message
  5251. void clif_skill_teleportmessage( map_session_data& sd, e_notify_mapinfo_result result ){
  5252. PACKET_ZC_NOTIFY_MAPINFO packet{};
  5253. packet.packetType = HEADER_ZC_NOTIFY_MAPINFO;
  5254. packet.type = static_cast<decltype(packet.type)>(result);
  5255. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5256. }
  5257. /// Displays Sense (WZ_ESTIMATION) information window (ZC_MONSTER_INFO).
  5258. /// 018c <class>.W <level>.W <size>.W <hp>.L <def>.W <race>.W <mdef>.W <element>.W
  5259. /// <water%>.B <earth%>.B <fire%>.B <wind%>.B <poison%>.B <holy%>.B <shadow%>.B <ghost%>.B <undead%>.B
  5260. void clif_skill_estimation(map_session_data *sd,struct block_list *dst)
  5261. {
  5262. unsigned char buf[64];
  5263. int i, fix;
  5264. nullpo_retv(sd);
  5265. nullpo_retv(dst);
  5266. if( dst->type != BL_MOB )
  5267. return;
  5268. status_data* status = status_get_status_data(*dst);
  5269. WBUFW(buf, 0)=0x18c;
  5270. WBUFW(buf, 2)=status_get_class(dst);
  5271. WBUFW(buf, 4)=status_get_lv(dst);
  5272. WBUFW(buf, 6)=status->size;
  5273. WBUFL(buf, 8)=status->hp;
  5274. WBUFW(buf,12)= (battle_config.estimation_type&1?status->def:0)
  5275. +(battle_config.estimation_type&2?status->def2:0);
  5276. WBUFW(buf,14)=status->race;
  5277. WBUFW(buf,16)= (battle_config.estimation_type&1?status->mdef:0)
  5278. +(battle_config.estimation_type&2?status->mdef2:0);
  5279. WBUFW(buf,18)= status->def_ele;
  5280. for(i=0;i<9;i++)
  5281. // The following caps negative attributes to 0 since the client displays them as 255-fix. [Skotlex]
  5282. WBUFB(buf,20+i)= (unsigned char)((fix=elemental_attribute_db.getAttribute(status->ele_lv, i+1, status->def_ele))<0?0:fix);
  5283. clif_send(buf,packet_len(0x18c),&sd->bl,sd->status.party_id>0?PARTY_SAMEMAP:SELF);
  5284. }
  5285. /// Presents a textual list of producable items.
  5286. /// 018d <packet len>.W { <name id>.W { <material id>.W }*3 }* (ZC_MAKABLEITEMLIST)
  5287. /// material id:
  5288. /// unused by the client
  5289. void clif_skill_produce_mix_list( map_session_data& sd, int skill_id, int trigger ){
  5290. // Avoid resending the menu
  5291. if( sd.menuskill_id == skill_id ){
  5292. return;
  5293. }
  5294. if (skill_id == GC_CREATENEWPOISON)
  5295. skill_id = GC_RESEARCHNEWPOISON;
  5296. PACKET_ZC_MAKABLEITEMLIST* p = reinterpret_cast<PACKET_ZC_MAKABLEITEMLIST*>( packet_buffer );
  5297. p->packetType = HEADER_ZC_MAKABLEITEMLIST;
  5298. p->packetLength = sizeof( *p );
  5299. int count = 0;
  5300. for( int i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5301. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, 1 ) ){
  5302. continue;
  5303. }
  5304. if( skill_id > 0 && skill_produce_db[i].req_skill != skill_id ){
  5305. continue;
  5306. }
  5307. PACKET_ZC_MAKABLEITEMLIST_sub& entry = p->items[count];
  5308. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5309. entry.material[0] = 0;
  5310. entry.material[1] = 0;
  5311. entry.material[2] = 0;
  5312. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5313. count++;
  5314. }
  5315. clif_send( p, p->packetLength, &sd.bl, SELF );
  5316. if( count > 0 ){
  5317. sd.menuskill_id = skill_id;
  5318. sd.menuskill_val = trigger;
  5319. }
  5320. }
  5321. /// Present a list of producable items.
  5322. /// 025a <packet len>.W <mk type>.W { <name id>.W }* (ZC_MAKINGITEM_LIST)
  5323. /// mk type:
  5324. /// 1 = cooking
  5325. /// 2 = arrow
  5326. /// 3 = elemental
  5327. /// 4 = GN_MIX_COOKING
  5328. /// 5 = GN_MAKEBOMB
  5329. /// 6 = GN_S_PHARMACY
  5330. /// 7 = MT_M_MACHINE
  5331. /// 8 = BO_BIONIC_PHARMACY
  5332. void clif_cooking_list( map_session_data& sd, int trigger, uint16 skill_id, int qty, int list_type ){
  5333. #if PACKETVER >= 20051010
  5334. // Avoid resending the menu
  5335. if( sd.menuskill_id == skill_id ){
  5336. return;
  5337. }
  5338. PACKET_ZC_MAKINGITEM_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGITEM_LIST*>( packet_buffer );
  5339. p->packetType = HEADER_ZC_MAKINGITEM_LIST;
  5340. p->packetLength = sizeof( *p );
  5341. p->makeItem = list_type;
  5342. int count = 0;
  5343. for( int i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5344. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, qty ) ){
  5345. continue;
  5346. }
  5347. PACKET_ZC_MAKINGITEM_LIST_sub& entry = p->items[count];
  5348. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5349. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5350. count++;
  5351. }
  5352. if( count > 0 || skill_id == AM_PHARMACY ){
  5353. clif_send( p, p->packetLength, &sd.bl, SELF );
  5354. sd.menuskill_id = skill_id;
  5355. sd.menuskill_val = trigger;
  5356. sd.menuskill_val2 = qty; // amount.
  5357. }else{
  5358. clif_menuskill_clear( &sd );
  5359. #if PACKETVER >= 20090922
  5360. clif_msg_skill( &sd, skill_id, MSI_SKILL_INVENTORY_KINDCNT_OVER );
  5361. #else
  5362. clif_send( p, p->packetLength, &sd.bl, SELF );
  5363. #endif
  5364. }
  5365. #endif
  5366. }
  5367. /// Notifies clients of a status change.
  5368. /// 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)]
  5369. /// 043f <index>.W <id>.L <state>.B <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE2) [used exclusively for starting statuses on pcs]
  5370. /// 0983 <index>.W <id>.L <state>.B <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE3) (PACKETVER >= 20120618)
  5371. /// @param bl Sends packet to clients around this object
  5372. /// @param id ID of object that has this effect
  5373. /// @param type Status icon see enum efst_type
  5374. /// @param flag 1:Active, 0:Deactive
  5375. /// @param tick Duration in ms
  5376. /// @param val1
  5377. /// @param val2
  5378. /// @param val3
  5379. 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)
  5380. {
  5381. unsigned char buf[32];
  5382. if (type == EFST_BLANK) //It shows nothing on the client...
  5383. return;
  5384. if (type == EFST_POSTDELAY && tick == 0)
  5385. return;
  5386. nullpo_retv(bl);
  5387. // Statuses with an infinite duration, but still needs a duration sent to display properly.
  5388. if (type == EFST_LUNARSTANCE || type == EFST_UNIVERSESTANCE || type == EFST_SUNSTANCE || type == EFST_STARSTANCE)
  5389. tick = 200;
  5390. #if PACKETVER >= 20120618
  5391. if (flag && battle_config.display_status_timers)
  5392. WBUFW(buf,0) = 0x983;
  5393. else
  5394. #elif PACKETVER >= 20090121
  5395. if (flag && battle_config.display_status_timers)
  5396. WBUFW(buf,0) = 0x43f;
  5397. else
  5398. #endif
  5399. WBUFW(buf,0) = 0x196;
  5400. WBUFW(buf,2) = type;
  5401. WBUFL(buf,4) = id;
  5402. WBUFB(buf,8) = flag;
  5403. #if PACKETVER >= 20120618
  5404. if (flag && battle_config.display_status_timers) {
  5405. if (tick <= 0)
  5406. tick = 9999; // this is indeed what official servers do
  5407. WBUFL(buf,9) = client_tick(tick);/* at this stage remain and total are the same value I believe */
  5408. WBUFL(buf,13) = client_tick(tick);
  5409. WBUFL(buf,17) = val1;
  5410. WBUFL(buf,21) = val2;
  5411. WBUFL(buf,25) = val3;
  5412. }
  5413. #elif PACKETVER >= 20090121
  5414. if (flag && battle_config.display_status_timers) {
  5415. if (tick <= 0)
  5416. tick = 9999; // this is indeed what official servers do
  5417. WBUFL(buf,9) = client_tick(tick);
  5418. WBUFL(buf,13) = val1;
  5419. WBUFL(buf,17) = val2;
  5420. WBUFL(buf,21) = val3;
  5421. }
  5422. #endif
  5423. clif_send(buf, packet_len(WBUFW(buf,0)), bl, target_type);
  5424. }
  5425. /* Sends status effect to clients around the bl
  5426. * @param bl Object that has the effect
  5427. * @param type Status icon see enum efst_type
  5428. * @param flag 1:Active, 0:Deactive
  5429. * @param tick Duration in ms
  5430. * @param val1
  5431. * @param val2
  5432. * @param val3
  5433. */
  5434. void clif_status_change(struct block_list *bl, int type, int flag, t_tick tick, int val1, int val2, int val3) {
  5435. map_session_data *sd = nullptr;
  5436. if (type == EFST_BLANK) //It shows nothing on the client...
  5437. return;
  5438. if (type == EFST_POSTDELAY && tick == 0)
  5439. return;
  5440. if (type == EFST_ILLUSION && !battle_config.display_hallucination) // Disable Hallucination.
  5441. return;
  5442. #if !( PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 )
  5443. // Older clients display normal riding icon.
  5444. if (type == EFST_MADOGEAR_TYPE)
  5445. type = EFST_RIDING;
  5446. #endif
  5447. nullpo_retv(bl);
  5448. sd = BL_CAST(BL_PC, bl);
  5449. // Check if current bl type is in the returned bitmask and only send status changes that actually matter to the client
  5450. if (!(status_efst_get_bl_type(static_cast<efst_type>(type)) & bl->type))
  5451. return;
  5452. clif_status_change_sub(bl, bl->id, type, flag, tick, val1, val2, val3, ((sd ? (pc_isinvisible(sd) ? SELF : AREA) : AREA_WOS)));
  5453. }
  5454. /**
  5455. * Send any active EFST to those around.
  5456. * @param tbl: Unit to send the packet to
  5457. * @param bl: Objects walking into view
  5458. * @param target: Client send type
  5459. */
  5460. void clif_efst_status_change_sub(struct block_list *tbl, struct block_list *bl, enum send_target target) {
  5461. unsigned char i;
  5462. struct sc_display_entry **sc_display;
  5463. unsigned char sc_display_count;
  5464. bool spheres_sent;
  5465. nullpo_retv(bl);
  5466. switch( bl->type ){
  5467. case BL_PC: {
  5468. map_session_data* sd = (map_session_data*)bl;
  5469. sc_display = sd->sc_display;
  5470. sc_display_count = sd->sc_display_count;
  5471. spheres_sent = !sd->state.connect_new;
  5472. }
  5473. break;
  5474. case BL_NPC: {
  5475. struct npc_data* nd = (struct npc_data*)bl;
  5476. sc_display = nd->sc_display;
  5477. sc_display_count = nd->sc_display_count;
  5478. spheres_sent = true;
  5479. }
  5480. break;
  5481. default:
  5482. return;
  5483. }
  5484. for (i = 0; i < sc_display_count; i++) {
  5485. enum sc_type type = sc_display[i]->type;
  5486. status_change *sc = status_get_sc(bl);
  5487. const struct TimerData *td = (sc && sc->getSCE(type) ? get_timer(sc->getSCE(type)->timer) : nullptr);
  5488. t_tick tick = 0;
  5489. if (td)
  5490. tick = DIFF_TICK(td->tick, gettick());
  5491. // Status changes that need special handling
  5492. switch( type ){
  5493. case SC_SPHERE_1:
  5494. case SC_SPHERE_2:
  5495. case SC_SPHERE_3:
  5496. case SC_SPHERE_4:
  5497. case SC_SPHERE_5:
  5498. if( spheres_sent ){
  5499. target = AREA_WOS;
  5500. }
  5501. break;
  5502. case SC_HELLS_PLANT:
  5503. if( sc && sc->getSCE(type) ){
  5504. tick = sc->getSCE(type)->val4;
  5505. }
  5506. break;
  5507. }
  5508. #if PACKETVER > 20120418
  5509. 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);
  5510. #else
  5511. 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);
  5512. #endif
  5513. }
  5514. }
  5515. /// Notifies the client when a player enters the screen with an active EFST.
  5516. /// 08ff <id>.L <index>.W <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER) (PACKETVER >= 20111108)
  5517. /// 0984 <id>.L <index>.W <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER2) (PACKETVER >= 20120618)
  5518. 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) {
  5519. #if PACKETVER >= 20111108
  5520. unsigned char buf[32];
  5521. #if PACKETVER >= 20120618
  5522. const int cmd = 0x984;
  5523. #elif PACKETVER >= 20111108
  5524. const int cmd = 0x8ff;
  5525. #endif
  5526. int offset = 0;
  5527. if (type == EFST_BLANK)
  5528. return;
  5529. nullpo_retv(bl);
  5530. if (tick <= 0)
  5531. tick = 9999;
  5532. WBUFW(buf,offset + 0) = cmd;
  5533. WBUFL(buf,offset + 2) = tid;
  5534. WBUFW(buf,offset + 6) = type;
  5535. #if PACKETVER >= 20111108
  5536. WBUFL(buf,offset + 8) = client_tick(tick); // Set remaining status duration [exneval]
  5537. #if PACKETVER >= 20120618
  5538. WBUFL(buf,offset + 12) = client_tick(tick);
  5539. offset += 4;
  5540. #endif
  5541. WBUFL(buf,offset + 12) = val1;
  5542. WBUFL(buf,offset + 16) = val2;
  5543. WBUFL(buf,offset + 20) = val3;
  5544. #endif
  5545. clif_send(buf,packet_len(cmd),bl,target);
  5546. #endif
  5547. }
  5548. /// Send message (modified by [Yor]) (ZC_NOTIFY_PLAYERCHAT).
  5549. /// 008e <packet len>.W <message>.?B
  5550. void clif_displaymessage(const int fd, const char* mes)
  5551. {
  5552. nullpo_retv(mes);
  5553. if( session_isActive( fd ) ){
  5554. char *message, *line;
  5555. message = aStrdup(mes);
  5556. line = strtok(message, "\n");
  5557. while(line != nullptr) {
  5558. #if PACKETVER == 20141022
  5559. /** for some reason game client crashes depending on message pattern (only for this packet) **/
  5560. /** so we redirect to ZC_NPC_CHAT **/
  5561. //clif_messagecolor(&sd->bl, color_table[COLOR_DEFAULT], mes, false, SELF);
  5562. unsigned long color = (color_table[COLOR_DEFAULT] & 0x0000FF) << 16 | (color_table[COLOR_DEFAULT] & 0x00FF00) | (color_table[COLOR_DEFAULT] & 0xFF0000) >> 16; // RGB to BGR
  5563. unsigned short len = strnlen(line, CHAT_SIZE_MAX);
  5564. if (len > 0) {
  5565. WFIFOHEAD(fd, 13 + len);
  5566. WFIFOW(fd, 0) = 0x2C1;
  5567. WFIFOW(fd, 2) = 13 + len;
  5568. WFIFOL(fd, 4) = 0;
  5569. WFIFOL(fd, 8) = color;
  5570. safestrncpy(WFIFOCP(fd, 12), line, len + 1);
  5571. WFIFOSET(fd, WFIFOW(fd, 2));
  5572. }
  5573. #else
  5574. // Limit message to 255+1 characters (otherwise it causes a buffer overflow in the client)
  5575. int16 len;
  5576. len = static_cast<decltype(len)>( strnlen(line, CHAT_SIZE_MAX) );
  5577. if (len > 0) { // don't send a void message (it's not displaying on the client chat). @help can send void line.
  5578. WFIFOHEAD(fd, 5 + len);
  5579. WFIFOW(fd,0) = 0x8e;
  5580. WFIFOW(fd,2) = 5 + len; // 4 + len + nullptr teminate
  5581. safestrncpy(WFIFOCP(fd,4), line, len + 1);
  5582. WFIFOSET(fd, 5 + len);
  5583. }
  5584. #endif
  5585. line = strtok(nullptr, "\n");
  5586. }
  5587. aFree(message);
  5588. }
  5589. }
  5590. /// Send broadcast message in yellow or blue without font formatting.
  5591. /// 009a <packet len>.W <message>.?B (ZC_BROADCAST)
  5592. void clif_broadcast(struct block_list* bl, const char* mes, size_t len, int type, enum send_target target)
  5593. {
  5594. nullpo_retv(mes);
  5595. if (len < 2)
  5596. return;
  5597. PACKET_ZC_BROADCAST* p = reinterpret_cast<PACKET_ZC_BROADCAST*>( packet_buffer );
  5598. p->packetType = HEADER_ZC_BROADCAST;
  5599. p->PacketLength = sizeof( *p );
  5600. if( ( type&BC_BLUE ) != 0 ){
  5601. // If there's "blue" at the beginning of the message, game client will display it in blue instead of yellow.
  5602. const char* color = "blue";
  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 if( ( type&BC_WOE ) != 0 ){
  5609. // If there's "ssss", game client will recognize message as 'WoE broadcast'.
  5610. const char* color = "ssss";
  5611. size_t length = strlen( color );
  5612. strcpy( p->message, color );
  5613. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  5614. strncpy( &p->message[length], mes, len );
  5615. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5616. }else{
  5617. strncpy( p->message, mes, len );
  5618. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5619. }
  5620. clif_send( p, p->PacketLength, bl, target );
  5621. }
  5622. /*==========================================
  5623. * Displays a message on a 'bl' to all it's nearby clients
  5624. * 008d <PacketLength>.W <GID>.L <message>.?B (ZC_NOTIFY_CHAT)
  5625. *------------------------------------------*/
  5626. void clif_GlobalMessage( block_list& bl, const char* message, enum send_target target ){
  5627. nullpo_retv(message);
  5628. int16 len = (int16)( strlen( message ) + 1 );
  5629. if( len > CHAT_SIZE_MAX ) {
  5630. ShowWarning("clif_GlobalMessage: Truncating too long message '%s' (len=%" PRId16 ").\n", message, len);
  5631. len = CHAT_SIZE_MAX;
  5632. }
  5633. PACKET_ZC_NOTIFY_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT*>( packet_buffer );
  5634. p->PacketType = HEADER_ZC_NOTIFY_CHAT;
  5635. p->PacketLength = sizeof( *p );
  5636. p->GID = bl.id;
  5637. safestrncpy( p->Message, message, len );
  5638. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5639. clif_send( p, p->PacketLength, &bl, target );
  5640. }
  5641. /// Send broadcast message with font formatting.
  5642. /// 01c3 <packet len>.W <fontColor>.L <fontType>.W <fontSize>.W <fontAlign>.W <fontY>.W <message>.?B (ZC_BROADCAST2)
  5643. 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)
  5644. {
  5645. nullpo_retv(mes);
  5646. if (len < 2)
  5647. return;
  5648. PACKET_ZC_BROADCAST2* p = reinterpret_cast<PACKET_ZC_BROADCAST2*>( packet_buffer );
  5649. p->packetType = HEADER_ZC_BROADCAST2;
  5650. p->PacketLength = sizeof( *p );
  5651. p->fontColor = fontColor;
  5652. p->fontType = fontType;
  5653. p->fontSize = fontSize;
  5654. p->fontAlign = fontAlign;
  5655. p->fontY = fontY;
  5656. strncpy( p->message, mes, len );
  5657. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5658. clif_send( p, p->PacketLength, bl, target );
  5659. }
  5660. /*
  5661. * Display *msg to all *users in channel
  5662. */
  5663. void clif_channel_msg(struct Channel *channel, const char *msg, unsigned long color) {
  5664. DBIterator *iter;
  5665. map_session_data *user;
  5666. unsigned short msg_len = 0, len = 0;
  5667. unsigned char buf[CHAT_SIZE_MAX];
  5668. if (!channel || !msg)
  5669. return;
  5670. msg_len = (unsigned short)(strlen(msg) + 1);
  5671. if( msg_len > sizeof(buf)-12 ) {
  5672. ShowWarning("clif_channel_msg: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  5673. msg_len = sizeof(buf)-12;
  5674. }
  5675. WBUFW(buf,0) = 0x2C1;
  5676. WBUFW(buf,2) = (len = msg_len + 12);
  5677. WBUFL(buf,4) = 0;
  5678. WBUFL(buf,8) = color;
  5679. safestrncpy(WBUFCP(buf,12), msg, msg_len);
  5680. iter = db_iterator(channel->users);
  5681. for( user = (map_session_data *)dbi_first(iter); dbi_exists(iter); user = (map_session_data *)dbi_next(iter) ) {
  5682. clif_send(buf, len, &user->bl, SELF);
  5683. }
  5684. dbi_destroy(iter);
  5685. }
  5686. /// Displays heal effect.
  5687. /// 013d <var id>.W <amount>.W (ZC_RECOVERY)
  5688. /// 0a27 <var id>.W <amount>.L (ZC_RECOVERY2)
  5689. /// var id:
  5690. /// 5 = HP (SP_HP)
  5691. /// 7 = SP (SP_SP)
  5692. /// ? = ignored
  5693. void clif_heal( map_session_data& sd, int32 type, uint32 val ) {
  5694. PACKET_ZC_RECOVERY packet{};
  5695. packet.packetType = HEADER_ZC_RECOVERY;
  5696. packet.type = static_cast<decltype(packet.type)>(type);
  5697. packet.amount = std::min( static_cast<decltype(packet.amount)>( val ), std::numeric_limits<decltype(packet.amount)>::max() );
  5698. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5699. }
  5700. /// Displays resurrection effect.
  5701. /// 0148 <id>.L <type>.W (ZC_RESURRECTION)
  5702. /// type:
  5703. /// ignored
  5704. void clif_resurrection( block_list& bl ){
  5705. PACKET_ZC_RESURRECTION packet{};
  5706. packet.packetType = HEADER_ZC_RESURRECTION;
  5707. packet.gid = bl.id;
  5708. packet.type = 0;
  5709. clif_send( &packet, sizeof( packet ), &bl, AREA );
  5710. if (disguised(&bl)) {
  5711. clif_spawn(&bl);
  5712. }
  5713. }
  5714. /// Sets the map property
  5715. /// 0199 <type>.W (ZC_NOTIFY_MAPPROPERTY)
  5716. /// 099b <type>.W <flags>.L (ZC_MAPPROPERTY_R2)
  5717. void clif_map_property(struct block_list *bl, enum map_property property, enum send_target t)
  5718. {
  5719. #if PACKETVER >= 20121010
  5720. short cmd = 0x99b;
  5721. unsigned char buf[8];
  5722. #else
  5723. short cmd = 0x199;
  5724. unsigned char buf[4];
  5725. #endif
  5726. WBUFW(buf,0)=cmd;
  5727. WBUFW(buf,2)=property;
  5728. #if PACKETVER >= 20121010
  5729. struct map_data *mapdata = map_getmapdata(bl->m);
  5730. map_session_data *sd = BL_CAST(BL_PC, bl);
  5731. WBUFL(buf,4) = ((mapdata->getMapFlag(MF_PVP) || (sd && sd->duel_group > 0))<<0)| // PARTY - Show attack cursor on non-party members (PvP)
  5732. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<1)|// GUILD - Show attack cursor on non-guild members (GvG)
  5733. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<2)|// SIEGE - Show emblem over characters heads when in GvG (WoE castle)
  5734. ((mapdata->getMapFlag(MF_FORCEMINEFFECT) || mapdata_flag_gvg2(mapdata))<<3)| // USE_SIMPLE_EFFECT - Forces simpler skill effects, like /mineffect command
  5735. ((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
  5736. ((mapdata->getMapFlag(MF_PVP))<<5)| // COUNT_PK - Show the PvP counter
  5737. ((mapdata->getMapFlag(MF_PARTYLOCK))<<6)| // NO_PARTY_FORMATION - Prevents party creation/modification (Might be used for instance dungeons)
  5738. ((mapdata->getMapFlag(MF_BATTLEGROUND))<<7)| // BATTLEFIELD - Unknown (Does something for battlegrounds areas)
  5739. ((mapdata->getMapFlag(MF_NOCOSTUME))<<8)| // DISABLE_COSTUMEITEM - Disable costume sprites
  5740. ((!mapdata->getMapFlag(MF_NOUSECART))<<9)| // USECART - Allow opening cart inventory (Well force it to always allow it)
  5741. ((!mapdata->getMapFlag(MF_NOSUNMOONSTARMIRACLE))<<10); // SUNMOONSTAR_MIRACLE - Allows Star Gladiator's Miracle to activate
  5742. //(1<<11); // Unused bits. 1 - 10 is 0x1 length and 11 is 0x15 length. May be used for future settings.
  5743. #endif
  5744. clif_send(buf,packet_len(cmd),bl,t);
  5745. }
  5746. /// Set the map type.
  5747. /// 01d6 <type>.W (ZC_NOTIFY_MAPPROPERTY2)
  5748. void clif_map_type( map_session_data& sd, e_map_type type ){
  5749. PACKET_ZC_NOTIFY_MAPPROPERTY2 packet{};
  5750. packet.packetType = HEADER_ZC_NOTIFY_MAPPROPERTY2;
  5751. packet.type = static_cast<decltype(packet.type)>(type);
  5752. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5753. }
  5754. /// Updates PvP ranking (ZC_NOTIFY_RANKING).
  5755. /// 019a <id>.L <ranking>.L <total>.L
  5756. void clif_pvpset(map_session_data *sd,int pvprank,int pvpnum,int type)
  5757. {
  5758. if(type == 2) {
  5759. int fd = sd->fd;
  5760. WFIFOHEAD(fd,packet_len(0x19a));
  5761. WFIFOW(fd,0) = 0x19a;
  5762. WFIFOL(fd,2) = sd->bl.id;
  5763. WFIFOL(fd,6) = pvprank;
  5764. WFIFOL(fd,10) = pvpnum;
  5765. WFIFOSET(fd,packet_len(0x19a));
  5766. } else {
  5767. unsigned char buf[32];
  5768. WBUFW(buf,0) = 0x19a;
  5769. WBUFL(buf,2) = sd->bl.id;
  5770. if(sd->sc.option&(OPTION_HIDE|OPTION_CLOAK))
  5771. WBUFL(buf,6) = UINT32_MAX; //On client displays as --
  5772. else
  5773. WBUFL(buf,6) = pvprank;
  5774. WBUFL(buf,10) = pvpnum;
  5775. if(pc_isinvisible(sd) || sd->disguise) //Causes crashes when a 'mob' with pvp info dies.
  5776. clif_send(buf,packet_len(0x19a),&sd->bl,SELF);
  5777. else if(!type)
  5778. clif_send(buf,packet_len(0x19a),&sd->bl,AREA);
  5779. else
  5780. clif_send(buf,packet_len(0x19a),&sd->bl,ALL_SAMEMAP);
  5781. }
  5782. }
  5783. /*==========================================
  5784. *
  5785. *------------------------------------------*/
  5786. void clif_map_property_mapall(int map_idx, enum map_property property)
  5787. {
  5788. struct block_list bl;
  5789. bl.id = 0;
  5790. bl.type = BL_NUL;
  5791. bl.m = map_idx;
  5792. clif_map_property( &bl, property, ALL_SAMEMAP );
  5793. }
  5794. /// Notifies the client about the result of a refine attempt.
  5795. /// 0188 <result>.W <index>.W <refine>.W (ZC_ACK_ITEMREFINING)
  5796. /// result:
  5797. /// 0 = success
  5798. /// 1 = failure
  5799. /// 2 = downgrade
  5800. /// 3 = failure (?)
  5801. void clif_refine( map_session_data& sd, uint16 index, e_ack_itemrefining result ){
  5802. PACKET_ZC_ACK_ITEMREFINING packet{};
  5803. packet.packetType = HEADER_ZC_ACK_ITEMREFINING;
  5804. packet.result = static_cast<decltype(packet.result)>(result);
  5805. packet.index = client_index( index );
  5806. packet.value = static_cast<decltype(packet.value)>( sd.inventory.u.items_inventory[index].refine );
  5807. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5808. }
  5809. /// Notifies the client about the result of a weapon refine attempt (ZC_ACK_WEAPONREFINE).
  5810. /// 0223 <result>.L <nameid>.W
  5811. /// result:
  5812. /// 0 = "weapon upgraded: %s" MsgStringTable[911] in rgb(0,255,255)
  5813. /// 1 = "weapon upgraded: %s" MsgStringTable[912] in rgb(0,205,205)
  5814. /// 2 = "cannot upgrade %s until you level up the upgrade weapon skill" MsgStringTable[913] in rgb(255,200,200)
  5815. /// 3 = "you lack the item %s to upgrade the weapon" MsgStringTable[914] in rgb(255,200,200)
  5816. void clif_upgrademessage( map_session_data* sd, int result, t_itemid item_id ){
  5817. PACKET_ZC_ACK_WEAPONREFINE p = {};
  5818. p.packetType = HEADER_ZC_ACK_WEAPONREFINE;
  5819. p.result = result;
  5820. p.itemId = client_nameid( item_id );
  5821. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  5822. }
  5823. /// Whisper is transmitted to the destination player (ZC_WHISPER).
  5824. /// 0097 <packet len>.W <nick>.24B <message>.?B
  5825. /// 0097 <packet len>.W <nick>.24B <isAdmin>.L <message>.?B (PACKETVER >= 20091104)
  5826. void clif_wis_message(map_session_data* sd, const char* nick, const char* mes, size_t mes_len, int gmlvl){
  5827. PACKET_ZC_WHISPER* p = reinterpret_cast<PACKET_ZC_WHISPER*>( packet_buffer );
  5828. p->PacketType = HEADER_ZC_WHISPER;
  5829. p->PacketLength = sizeof( *p );
  5830. safestrncpy( p->sender, nick, NAME_LENGTH );
  5831. safestrncpy( p->message, mes, mes_len );
  5832. p->PacketLength += static_cast<decltype( p->PacketLength )>( mes_len );
  5833. #if PACKETVER >= 20091104
  5834. map_session_data* ssd = map_nick2sd(nick, false);
  5835. // If it is not a message from the server or a player from another map-server
  5836. if( ssd != nullptr ){
  5837. gmlvl = pc_get_group_level(ssd);
  5838. }
  5839. p->isAdmin = (gmlvl == 99) ? 1 : 0;
  5840. #if PACKETVER_MAIN_NUM >= 20131204 || PACKETVER_RE_NUM >= 20131120 || defined(PACKETVER_ZERO)
  5841. if( ssd != nullptr ){
  5842. p->senderGID = ssd->bl.id;
  5843. }else{
  5844. p->senderGID = 0;
  5845. }
  5846. #endif
  5847. #endif
  5848. clif_send( p, p->PacketLength, &sd->bl, SELF );
  5849. }
  5850. /// Inform the player about the result of his whisper action
  5851. /// 0098 <result>.B (ZC_ACK_WHISPER).
  5852. /// 09df <result>.B <CID>.L (ZC_ACK_WHISPER02).
  5853. /// result:
  5854. /// 0 = success to send whisper
  5855. /// 1 = target character is not loged in
  5856. /// 2 = ignored by target
  5857. /// 3 = everyone ignored by target
  5858. void clif_wis_end( map_session_data& sd, e_ack_whisper result ){
  5859. PACKET_ZC_ACK_WHISPER packet{};
  5860. packet.packetType = HEADER_ZC_ACK_WHISPER;
  5861. packet.result = static_cast<decltype(packet.result)>(result);
  5862. #if PACKETVER >= 20131223
  5863. packet.CID = sd.status.char_id;
  5864. #endif
  5865. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5866. }
  5867. /// Returns character name requested by char_id (ZC_ACK_REQNAME_BYGID).
  5868. /// 0194 <char id>.L <name>.24B
  5869. /// 0af7 <flag>.W <char id>.L <name>.24B
  5870. void clif_solved_charname(int fd, int charid, const char* name)
  5871. {
  5872. #if PACKETVER >= 20180221
  5873. WFIFOHEAD(fd,packet_len(0xaf7));
  5874. WFIFOW(fd,0) = 0xaf7;
  5875. WFIFOW(fd,2) = name[0] ? 3 : 2;
  5876. WFIFOL(fd,4) = charid;
  5877. safestrncpy(WFIFOCP(fd, 8), name, NAME_LENGTH);
  5878. WFIFOSET(fd,packet_len(0x0af7));
  5879. #else
  5880. WFIFOHEAD(fd,packet_len(0x194));
  5881. WFIFOW(fd,0)=0x194;
  5882. WFIFOL(fd,2)=charid;
  5883. safestrncpy(WFIFOCP(fd,6), name, NAME_LENGTH);
  5884. WFIFOSET(fd,packet_len(0x194));
  5885. #endif
  5886. }
  5887. /// Presents a list of items that can be carded/composed (ZC_ITEMCOMPOSITION_LIST).
  5888. /// 017b <packet len>.W { <name id>.W }*
  5889. void clif_use_card(map_session_data *sd,int idx)
  5890. {
  5891. int i,c,ep;
  5892. int fd=sd->fd;
  5893. nullpo_retv(sd);
  5894. if (idx < 0 || idx >= MAX_INVENTORY) //Crash-fix from bad packets.
  5895. return;
  5896. if (!sd->inventory_data[idx] || sd->inventory_data[idx]->type != IT_CARD)
  5897. return; //Avoid parsing invalid item indexes (no card/no item)
  5898. ep=sd->inventory_data[idx]->equip;
  5899. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5900. WFIFOW(fd,0)=0x17b;
  5901. for(i=c=0;i<MAX_INVENTORY;i++){
  5902. int j;
  5903. if(sd->inventory_data[i] == nullptr)
  5904. continue;
  5905. if(sd->inventory_data[i]->type!=IT_WEAPON && sd->inventory_data[i]->type!=IT_ARMOR)
  5906. continue;
  5907. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0])) //Can't slot it
  5908. continue;
  5909. if(sd->inventory.u.items_inventory[i].identify==0 ) //Not identified
  5910. continue;
  5911. if((sd->inventory_data[i]->equip&ep)==0) //Not equippable on this part.
  5912. continue;
  5913. if(sd->inventory_data[i]->type==IT_WEAPON && ep==EQP_SHIELD) //Shield card won't go on left weapon.
  5914. continue;
  5915. 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.
  5916. continue;
  5917. ARR_FIND( 0, sd->inventory_data[i]->slots, j, sd->inventory.u.items_inventory[i].card[j] == 0 );
  5918. if( j == sd->inventory_data[i]->slots ) // No room
  5919. continue;
  5920. if( sd->inventory.u.items_inventory[i].equip > 0 ) // Do not check items that are already equipped
  5921. continue;
  5922. WFIFOW(fd,4+c*2)=i+2;
  5923. c++;
  5924. }
  5925. if( !c ) return; // no item is available for card insertion
  5926. WFIFOW(fd,2)=4+c*2;
  5927. WFIFOSET(fd,WFIFOW(fd,2));
  5928. }
  5929. /// Notifies the client about the result of item carding/composition.
  5930. /// 017d <equip index>.W <card index>.W <result>.B (ZC_ACK_ITEMCOMPOSITION)
  5931. /// result:
  5932. /// 0 = success
  5933. /// 1 = failure
  5934. void clif_insert_card( map_session_data& sd, int32 idx_equip, int32 idx_card, bool failure ){
  5935. PACKET_ZC_ACK_ITEMCOMPOSITION packet{};
  5936. packet.packetType = HEADER_ZC_ACK_ITEMCOMPOSITION;
  5937. packet.equipIndex = client_index( idx_equip );
  5938. packet.cardIndex = client_index( idx_card );
  5939. packet.result = failure;
  5940. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5941. }
  5942. /// Presents a list of items that can be identified (ZC_ITEMIDENTIFY_LIST).
  5943. /// 0177 <packet len>.W { <index>.W }*
  5944. void clif_item_identify_list(map_session_data *sd)
  5945. {
  5946. int i,c;
  5947. int fd;
  5948. nullpo_retv(sd);
  5949. fd=sd->fd;
  5950. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5951. WFIFOW(fd,0)=0x177;
  5952. for(i=c=0;i<MAX_INVENTORY;i++){
  5953. if(sd->inventory.u.items_inventory[i].nameid > 0 && !sd->inventory.u.items_inventory[i].identify){
  5954. WFIFOW(fd,c*2+4)=client_index(i);
  5955. c++;
  5956. }
  5957. }
  5958. if(c > 0) {
  5959. WFIFOW(fd,2)=c*2+4;
  5960. WFIFOSET(fd,WFIFOW(fd,2));
  5961. sd->menuskill_id = MC_IDENTIFY;
  5962. sd->menuskill_val = c;
  5963. sd->state.workinprogress = WIP_DISABLE_ALL;
  5964. }
  5965. }
  5966. /// Notifies the client about the result of a item identify request.
  5967. /// 0179 <index>.W <result>.B (ZC_ACK_ITEMIDENTIFY)
  5968. /// result:
  5969. /// 0 = success
  5970. /// 1 = failure
  5971. void clif_item_identified( map_session_data& sd, int32 idx, bool failure ){
  5972. PACKET_ZC_ACK_ITEMIDENTIFY packet{};
  5973. packet.packetType = HEADER_ZC_ACK_ITEMIDENTIFY;
  5974. packet.index = client_index( idx );
  5975. packet.result = failure;
  5976. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5977. }
  5978. /// Presents a list of items that can be repaired.
  5979. /// 01fc <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }* (ZC_REPAIRITEMLIST)
  5980. void clif_item_repair_list( map_session_data& sd, map_session_data& dstsd, uint16 lv ){
  5981. PACKET_ZC_REPAIRITEMLIST* p = reinterpret_cast<PACKET_ZC_REPAIRITEMLIST*>( packet_buffer );
  5982. p->packetType = HEADER_ZC_REPAIRITEMLIST;
  5983. p->packetLength = sizeof( *p );
  5984. size_t c = 0;
  5985. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  5986. 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)){
  5987. p->items[c].index = static_cast<decltype( p->items[0].index )>( i );
  5988. p->items[c].itemId = client_nameid( dstsd.inventory.u.items_inventory[i].nameid );
  5989. p->items[c].refine = dstsd.inventory.u.items_inventory[i].refine;
  5990. clif_addcards( &p->items[c].slot, &dstsd.inventory.u.items_inventory[i] );
  5991. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  5992. c++;
  5993. }
  5994. }
  5995. if( c > 0 ){
  5996. clif_send( p, p->packetLength, &sd.bl, SELF );
  5997. sd.menuskill_id = BS_REPAIRWEAPON;
  5998. sd.menuskill_val = dstsd.bl.id;
  5999. sd.menuskill_val2 = lv;
  6000. }else{
  6001. clif_skill_fail( sd, sd.ud.skill_id );
  6002. }
  6003. }
  6004. /// Notifies the client about the result of a item repair request.
  6005. /// 01fe <index>.W <result>.B (ZC_ACK_ITEMREPAIR)
  6006. /// index:
  6007. /// ignored (inventory index)
  6008. /// result:
  6009. /// 0 = Item repair success.
  6010. /// 1 = Item repair failure.
  6011. void clif_item_repaireffect( map_session_data& sd, int32 idx, bool failure ){
  6012. PACKET_ZC_ACK_ITEMREPAIR packet{};
  6013. packet.packetType = HEADER_ZC_ACK_ITEMREPAIR;
  6014. packet.index = client_index( idx );
  6015. packet.result = failure;
  6016. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6017. }
  6018. /// Displays a message, that an equipment got damaged.
  6019. /// 02bb <equip location>.W <account id>.L (ZC_EQUIPITEM_DAMAGED)
  6020. void clif_item_damaged( map_session_data& sd, uint16 position ){
  6021. PACKET_ZC_EQUIPITEM_DAMAGED packet{};
  6022. packet.packetType = HEADER_ZC_EQUIPITEM_DAMAGED;
  6023. packet.equipLocation = position;
  6024. packet.GID = sd.bl.id;
  6025. // TODO: the packet seems to be sent to other people as well, probably party and/or guild.
  6026. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6027. }
  6028. /// Presents a list of weapon items that can be refined [Taken from jAthena] (ZC_NOTIFY_WEAPONITEMLIST).
  6029. /// 0221 <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }*
  6030. void clif_item_refine_list( map_session_data& sd ){
  6031. PACKET_ZC_NOTIFY_WEAPONITEMLIST* p = reinterpret_cast<PACKET_ZC_NOTIFY_WEAPONITEMLIST*>( packet_buffer );
  6032. p->packetType = HEADER_ZC_NOTIFY_WEAPONITEMLIST;
  6033. p->packetLength = sizeof( *p );
  6034. uint16 skill_lv = pc_checkskill( &sd, WS_WEAPONREFINE );
  6035. int refine_item[MAX_WEAPON_LEVEL];
  6036. refine_item[0] = pc_search_inventory( &sd, ITEMID_PHRACON );
  6037. refine_item[1] = pc_search_inventory( &sd, ITEMID_EMVERETARCON );
  6038. refine_item[2] = refine_item[3] = pc_search_inventory( &sd, ITEMID_ORIDECON );
  6039. #ifdef RENEWAL
  6040. refine_item[4] = -1;
  6041. #endif
  6042. int count = 0;
  6043. for( int i = 0; i < MAX_INVENTORY; i++ ){
  6044. if( sd.inventory.u.items_inventory[i].nameid > 0 && sd.inventory.u.items_inventory[i].refine < skill_lv &&
  6045. sd.inventory_data[i] != nullptr && sd.inventory_data[i]->type == IT_WEAPON &&
  6046. sd.inventory.u.items_inventory[i].identify && sd.inventory_data[i]->weapon_level >= 1 &&
  6047. refine_item[sd.inventory_data[i]->weapon_level - 1] != -1 && !( sd.inventory.u.items_inventory[i].equip & EQP_ARMS ) ){
  6048. p->items[count].index = client_index( i );
  6049. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  6050. p->items[count].refine = sd.inventory.u.items_inventory[i].refine;
  6051. clif_addcards( &p->items[count].slot, &sd.inventory.u.items_inventory[i] );
  6052. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  6053. count++;
  6054. }
  6055. }
  6056. clif_send( p, p->packetLength, &sd.bl, SELF );
  6057. if( count > 0 ){
  6058. sd.menuskill_id = WS_WEAPONREFINE;
  6059. sd.menuskill_val = skill_lv;
  6060. }
  6061. }
  6062. /// Notification of an auto-casted skill (ZC_AUTORUN_SKILL).
  6063. /// 0147 <skill id>.W <type>.L <level>.W <sp cost>.W <atk range>.W <skill name>.24B <upgradable>.B
  6064. void clif_item_skill( map_session_data *sd, uint16 skill_id, uint16 skill_lv ){
  6065. nullpo_retv( sd );
  6066. PACKET_ZC_AUTORUN_SKILL p = {};
  6067. p.packetType = HEADER_ZC_AUTORUN_SKILL;
  6068. p.skill_id = skill_id;
  6069. p.skill_type = skill_get_inf( skill_id );
  6070. p.skill_lv = skill_lv;
  6071. p.skill_sp = skill_get_sp( skill_id, skill_lv );
  6072. p.skill_range = skill_get_range2( &sd->bl, skill_id, skill_lv, false );
  6073. safestrncpy( p.skill_name, skill_get_name( skill_id ), NAME_LENGTH );
  6074. p.up_flag = false;
  6075. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6076. }
  6077. /// Adds an item to character's cart.
  6078. /// 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)
  6079. /// 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)
  6080. /// 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)
  6081. void clif_cart_additem( map_session_data *sd, int n, int amount ){
  6082. nullpo_retv( sd );
  6083. int fd = sd->fd;
  6084. if( !session_isActive( fd ) ){
  6085. return;
  6086. }
  6087. if( n < 0 || n >= MAX_CART || sd->cart.u.items_cart[n].nameid == 0 ){
  6088. return;
  6089. }
  6090. PACKET_ZC_ADD_ITEM_TO_CART p = {};
  6091. p.packetType = HEADER_ZC_ADD_ITEM_TO_CART;
  6092. p.index = client_index( n );
  6093. p.amount = amount;
  6094. p.itemId = client_nameid( sd->cart.u.items_cart[n].nameid );
  6095. #if PACKETVER >= 5
  6096. p.itemType = itemdb_type( sd->cart.u.items_cart[n].nameid );
  6097. #endif
  6098. p.identified = sd->cart.u.items_cart[n].identify;
  6099. p.damaged = sd->cart.u.items_cart[n].attribute;
  6100. p.refine = sd->cart.u.items_cart[n].refine;
  6101. clif_addcards( &p.slot, &sd->cart.u.items_cart[n] );
  6102. #if PACKETVER >= 20150226
  6103. clif_add_random_options( p.option_data, sd->cart.u.items_cart[n] );
  6104. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6105. p.grade = sd->cart.u.items_cart[n].enchantgrade;
  6106. #endif
  6107. #endif
  6108. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6109. }
  6110. /// Acknowledge an item have been added to cart
  6111. /// 012c <result>.B (ZC_ACK_ADDITEM_TO_CART)
  6112. /// result:
  6113. /// 0 = ADDITEM_TO_CART_FAIL_WEIGHT
  6114. /// 1 = ADDITEM_TO_CART_FAIL_COUNT
  6115. void clif_cart_additem_ack( map_session_data& sd, e_ack_additem_to_cart flag ){
  6116. PACKET_ZC_ACK_ADDITEM_TO_CART packet{};
  6117. packet.packetType = HEADER_ZC_ACK_ADDITEM_TO_CART;
  6118. packet.result = static_cast<decltype(packet.result)>(flag);
  6119. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6120. }
  6121. // 09B7 <unknow data> (ZC_ACK_OPEN_BANKING)
  6122. void clif_bank_open( map_session_data& sd ){
  6123. #if PACKETVER >= 20130717
  6124. PACKET_ZC_ACK_OPEN_BANKING p = {};
  6125. p.packetType = HEADER_ZC_ACK_OPEN_BANKING;
  6126. p.unknown = 0;
  6127. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6128. #endif
  6129. }
  6130. /*
  6131. * Request to Open the banking system
  6132. * 09B6 <aid>L (CZ_REQ_OPEN_BANKING)
  6133. */
  6134. void clif_parse_BankOpen(int fd, map_session_data* sd) {
  6135. #if PACKETVER >= 20130717
  6136. const PACKET_CZ_REQ_OPEN_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BANKING*>( RFIFOP( fd, 0 ) );
  6137. //TODO check if preventing trade or stuff like that
  6138. //also mark something in case char ain't available for saving, should we check now ?
  6139. nullpo_retv(sd);
  6140. if( !battle_config.feature_banking ) {
  6141. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6142. return;
  6143. }
  6144. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6145. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6146. return;
  6147. }
  6148. else {
  6149. if(sd->status.account_id == p->AID){
  6150. sd->state.banking = 1;
  6151. //request save ?
  6152. //chrif_bankdata_request(sd->status.account_id, sd->status.char_id);
  6153. //on succes open bank ?
  6154. clif_bank_open( *sd );
  6155. }
  6156. }
  6157. #endif
  6158. }
  6159. // 09B9 <unknow data> (ZC_ACK_CLOSE_BANKING)
  6160. void clif_bank_close( map_session_data& sd ){
  6161. #if PACKETVER >= 20130717
  6162. PACKET_ZC_ACK_CLOSE_BANKING p = {};
  6163. p.packetType = HEADER_ZC_ACK_CLOSE_BANKING;
  6164. p.unknown = 0;
  6165. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6166. #endif
  6167. }
  6168. /*
  6169. * Request to close the banking system
  6170. * 09B8 <aid>L (CZ_REQ_CLOSE_BANKING)
  6171. */
  6172. void clif_parse_BankClose(int fd, map_session_data* sd) {
  6173. #if PACKETVER >= 20130717
  6174. const PACKET_CZ_REQ_CLOSE_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_CLOSE_BANKING*>( RFIFOP( fd, 0 ) );
  6175. nullpo_retv(sd);
  6176. if( !battle_config.feature_banking ) {
  6177. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6178. //still allow to go trough to not stuck player if we have disable it while they was in
  6179. }
  6180. if(sd->status.account_id == p->AID){
  6181. sd->state.banking = 0;
  6182. clif_bank_close( *sd );
  6183. }
  6184. #endif
  6185. }
  6186. /*
  6187. * Display how much we got in bank (I suppose)
  6188. * 09A6 <Bank_Vault>Q <Reason>W (ZC_BANKING_CHECK)
  6189. */
  6190. void clif_Bank_Check( map_session_data& sd ){
  6191. #if PACKETVER >= 20130717
  6192. PACKET_ZC_BANKING_CHECK p = {};
  6193. p.packetType = HEADER_ZC_BANKING_CHECK;
  6194. p.money = sd.bank_vault;
  6195. p.reason = 0;
  6196. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6197. #endif
  6198. }
  6199. /*
  6200. * Requesting the data in bank
  6201. * 09AB <aid>L (CZ_REQ_BANKING_CHECK)
  6202. */
  6203. void clif_parse_BankCheck(int fd, map_session_data* sd) {
  6204. #if PACKETVER >= 20130717
  6205. const PACKET_CZ_REQ_BANKING_CHECK* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_CHECK*>( RFIFOP( fd, 0 ) );
  6206. nullpo_retv(sd);
  6207. if( !battle_config.feature_banking ) {
  6208. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6209. return;
  6210. }
  6211. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6212. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6213. return;
  6214. }
  6215. else {
  6216. if(sd->status.account_id == p->AID) //since we have it let check it for extra security
  6217. clif_Bank_Check( *sd );
  6218. }
  6219. #endif
  6220. }
  6221. /*
  6222. * Acknowledge of deposit some money in bank
  6223. 09A8 <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_DEPOSIT)
  6224. */
  6225. void clif_bank_deposit( map_session_data& sd, enum e_BANKING_DEPOSIT_ACK reason ){
  6226. #if PACKETVER >= 20130717
  6227. PACKET_ZC_ACK_BANKING_DEPOSIT p = {};
  6228. p.packetType = HEADER_ZC_ACK_BANKING_DEPOSIT;
  6229. p.money = sd.bank_vault;
  6230. p.zeny = sd.status.zeny;
  6231. p.reason = reason;
  6232. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6233. #endif
  6234. }
  6235. /*
  6236. * Request saving some money in bank
  6237. * @author : original [Yommy/Hercules]
  6238. * 09A7 <AID>L <Money>L (CZ_REQ_BANKING_DEPOSIT)
  6239. */
  6240. void clif_parse_BankDeposit(int fd, map_session_data* sd) {
  6241. #if PACKETVER >= 20130717
  6242. const PACKET_CZ_REQ_BANKING_DEPOSIT* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_DEPOSIT*>( RFIFOP( fd, 0 ) );
  6243. nullpo_retv(sd);
  6244. if( !battle_config.feature_banking ) {
  6245. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6246. return;
  6247. }
  6248. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6249. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6250. return;
  6251. }
  6252. else {
  6253. if(sd->status.account_id == p->AID){
  6254. enum e_BANKING_DEPOSIT_ACK reason = pc_bank_deposit(sd,max(0,p->zeny));
  6255. clif_bank_deposit( *sd, reason );
  6256. }
  6257. }
  6258. #endif
  6259. }
  6260. /*
  6261. * Acknowledge of withdrawing some money from bank
  6262. * 09AA <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_WITHDRAW)
  6263. */
  6264. void clif_bank_withdraw( map_session_data& sd, enum e_BANKING_WITHDRAW_ACK reason ){
  6265. #if PACKETVER >= 20130717
  6266. PACKET_ZC_ACK_BANKING_WITHDRAW p = {};
  6267. p.packetType = HEADER_ZC_ACK_BANKING_WITHDRAW;
  6268. p.reason = reason;
  6269. p.money = sd.bank_vault;
  6270. p.zeny = sd.status.zeny;
  6271. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6272. #endif
  6273. }
  6274. /*
  6275. * Request Withdrawing some money from bank
  6276. * 09A9 <AID>L <Money>L (CZ_REQ_BANKING_WITHDRAW)
  6277. */
  6278. void clif_parse_BankWithdraw(int fd, map_session_data* sd) {
  6279. #if PACKETVER >= 20130717
  6280. const PACKET_CZ_REQ_BANKING_WITHDRAW* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_WITHDRAW*>( RFIFOP( fd, 0 ) );
  6281. nullpo_retv(sd);
  6282. if( !battle_config.feature_banking ) {
  6283. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6284. return;
  6285. }
  6286. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6287. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6288. return;
  6289. }
  6290. else {
  6291. if(sd->status.account_id == p->AID){
  6292. enum e_BANKING_WITHDRAW_ACK reason = pc_bank_withdraw(sd,max(0,p->zeny));
  6293. clif_bank_withdraw( *sd, reason );
  6294. }
  6295. }
  6296. #endif
  6297. }
  6298. /// Deletes an item from character's cart.
  6299. /// 0125 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_CART)
  6300. void clif_cart_delitem( map_session_data& sd, int32 index, int32 amount ){
  6301. PACKET_ZC_DELETE_ITEM_FROM_CART packet{};
  6302. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_CART;
  6303. packet.index = client_index( index );
  6304. packet.amount = amount;
  6305. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6306. }
  6307. /// Opens the shop creation menu.
  6308. /// 012d <num>.W (ZC_OPENSTORE)
  6309. /// num:
  6310. /// number of allowed item slots
  6311. void clif_openvendingreq( map_session_data& sd, uint16 num ){
  6312. PACKET_ZC_OPENSTORE packet{};
  6313. packet.packetType = HEADER_ZC_OPENSTORE;
  6314. packet.num = num;
  6315. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6316. }
  6317. /// Displays a vending board to target/area (ZC_STORE_ENTRY).
  6318. /// 0131 <owner id>.L <message>.80B
  6319. void clif_showvendingboard( map_session_data& sd, enum send_target target, struct block_list* tbl ){
  6320. if( tbl == nullptr ){
  6321. tbl = &sd.bl;
  6322. target = AREA_WOS;
  6323. }
  6324. PACKET_ZC_STORE_ENTRY p = {};
  6325. p.packetType = HEADER_ZC_STORE_ENTRY;
  6326. p.makerAID = sd.status.account_id;
  6327. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  6328. clif_send( &p, sizeof( p ), tbl, target );
  6329. }
  6330. /// Removes a vending board from screen (ZC_DISAPPEAR_ENTRY).
  6331. /// 0132 <owner id>.L
  6332. void clif_closevendingboard(struct block_list* bl, int fd)
  6333. {
  6334. unsigned char buf[16];
  6335. nullpo_retv(bl);
  6336. WBUFW(buf,0) = 0x132;
  6337. WBUFL(buf,2) = bl->id;
  6338. if( session_isActive(fd) ) {
  6339. WFIFOHEAD(fd,packet_len(0x132));
  6340. memcpy(WFIFOP(fd,0),buf,packet_len(0x132));
  6341. WFIFOSET(fd,packet_len(0x132));
  6342. } else {
  6343. clif_send(buf,packet_len(0x132),bl,AREA_WOS);
  6344. }
  6345. }
  6346. /// Sends a list of items in a shop.
  6347. /// 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)
  6348. /// 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)
  6349. void clif_vendinglist( map_session_data& sd, map_session_data& vsd ){
  6350. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC*>( packet_buffer );
  6351. p->packetType = HEADER_ZC_PC_PURCHASE_ITEMLIST_FROMMC;
  6352. p->packetLength = sizeof( *p );
  6353. p->AID = vsd.status.account_id;
  6354. #if PACKETVER >= 20100105
  6355. p->venderId = vsd.vender_id;
  6356. #endif
  6357. for( int i = 0, count = 0; i < vsd.vend_num; i++ ){
  6358. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC_sub& entry = p->items[count];
  6359. int index = vsd.vending[i].index;
  6360. struct item_data* data = itemdb_search( vsd.cart.u.items_cart[index].nameid );
  6361. if( data == nullptr ){
  6362. continue;
  6363. }
  6364. entry.price = vsd.vending[i].value;
  6365. entry.amount = vsd.vending[i].amount;
  6366. entry.index = client_index( index );
  6367. entry.itemType = itemtype( vsd.cart.u.items_cart[index].nameid );
  6368. entry.itemId = client_nameid( vsd.cart.u.items_cart[index].nameid );
  6369. entry.identified = vsd.cart.u.items_cart[index].identify;
  6370. entry.damaged = vsd.cart.u.items_cart[index].attribute;
  6371. entry.refine = vsd.cart.u.items_cart[index].refine;
  6372. clif_addcards( &entry.slot, &vsd.cart.u.items_cart[index] );
  6373. #if PACKETVER >= 20150226
  6374. clif_add_random_options( entry.option_data, vsd.cart.u.items_cart[index] );
  6375. #if PACKETVER >= 20160921
  6376. entry.location = pc_equippoint_sub( &sd, data );
  6377. entry.viewSprite = data->look;
  6378. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6379. entry.grade = vsd.cart.u.items_cart[index].enchantgrade;
  6380. #endif
  6381. #endif
  6382. #endif
  6383. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6384. count++;
  6385. }
  6386. clif_send( p, p->packetLength, &sd.bl, SELF );
  6387. }
  6388. /// Shop purchase failure.
  6389. /// 0135 <index>.W <amount>.W <result>.B (ZC_PC_PURCHASE_RESULT_FROMMC)
  6390. /// result:
  6391. /// 0 = success
  6392. /// 1 = not enough zeny
  6393. /// 2 = overweight
  6394. /// 4 = out of stock
  6395. /// 5 = "cannot use an npc shop while in a trade"
  6396. /// 6 = Because the store information was incorrect the item was not purchased.
  6397. /// 7 = No sales information.
  6398. void clif_buyvending( map_session_data& sd, uint16 index, uint16 amount, e_pc_purchase_result_frommc result ){
  6399. PACKET_ZC_PC_PURCHASE_RESULT_FROMMC packet{};
  6400. packet.packetType = HEADER_ZC_PC_PURCHASE_RESULT_FROMMC;
  6401. packet.index = client_index( index );
  6402. packet.amount = amount;
  6403. packet.result = static_cast<decltype(packet.result)>(result);
  6404. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6405. }
  6406. /// Show's vending player its list of items for sale.
  6407. /// 0a28 <result>.B (ZC_ACK_OPENSTORE2)
  6408. void clif_openvending_ack( map_session_data& sd, e_ack_openstore2 result ){
  6409. #if PACKETVER >= 20141022
  6410. PACKET_ZC_ACK_OPENSTORE2 packet{};
  6411. packet.packetType = HEADER_ZC_ACK_OPENSTORE2;
  6412. packet.result = static_cast<decltype(packet.result)>(result);
  6413. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6414. #endif
  6415. }
  6416. /// Shop creation success.
  6417. /// 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)
  6418. void clif_openvending( map_session_data& sd ){
  6419. PACKET_ZC_PC_PURCHASE_MYITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_MYITEMLIST*>( packet_buffer );
  6420. p->packetType = HEADER_ZC_PC_PURCHASE_MYITEMLIST;
  6421. p->packetLength = sizeof( *p );
  6422. p->AID = sd.bl.id;
  6423. for( int i = 0; i < sd.vend_num; i++ ) {
  6424. PACKET_ZC_PC_PURCHASE_MYITEMLIST_sub& entry = p->items[i];
  6425. int16 index = sd.vending[i].index;
  6426. entry.price = sd.vending[i].value;
  6427. entry.index = client_index( index );
  6428. entry.amount = sd.vending[i].amount;
  6429. entry.itemType = itemtype( sd.cart.u.items_cart[index].nameid );
  6430. entry.itemId = client_nameid( sd.cart.u.items_cart[index].nameid );
  6431. entry.identified = sd.cart.u.items_cart[index].identify;
  6432. entry.damaged = sd.cart.u.items_cart[index].attribute;
  6433. entry.refine = sd.cart.u.items_cart[index].refine;
  6434. clif_addcards( &entry.slot, &sd.cart.u.items_cart[index] );
  6435. #if PACKETVER >= 20150226
  6436. clif_add_random_options( entry.option_data, sd.cart.u.items_cart[index] );
  6437. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6438. entry.grade = sd.cart.u.items_cart[index].enchantgrade;
  6439. #endif
  6440. #endif
  6441. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6442. }
  6443. clif_send( p, p->packetLength, &sd.bl, SELF );
  6444. clif_openvending_ack( sd, OPENSTORE2_SUCCESS );
  6445. }
  6446. /// Inform merchant that someone has bought an item.
  6447. /// 0137 <index>.W <amount>.W (ZC_DELETEITEM_FROM_MCSTORE)
  6448. /// 09e5 <index>.W <amount>.W <GID>.L <Date>.L <zeny>.L (ZC_DELETEITEM_FROM_MCSTORE2)
  6449. void clif_vendingreport( map_session_data& sd, uint16 index, uint16 amount, uint32 char_id, int32 zeny ){
  6450. PACKET_ZC_DELETEITEM_FROM_MCSTORE packet{};
  6451. packet.packetType = HEADER_ZC_DELETEITEM_FROM_MCSTORE;
  6452. packet.index = client_index( index );
  6453. packet.amount = amount;
  6454. // TODO : not sure for client date [Napster]
  6455. #if PACKETVER >= 20141016
  6456. packet.buyerCID = char_id;
  6457. packet.date = client_tick( time(nullptr) );
  6458. packet.zeny = zeny;
  6459. #endif
  6460. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6461. }
  6462. /// Result of organizing a party (ZC_ACK_MAKE_GROUP).
  6463. /// 00fa <result>.B
  6464. /// result:
  6465. /// 0 = opens party window and shows MsgStringTable[77]="party successfully organized"
  6466. /// 1 = MsgStringTable[78]="party name already exists"
  6467. /// 2 = MsgStringTable[79]="already in a party"
  6468. /// 3 = cannot organize parties on this map
  6469. /// ? = nothing
  6470. void clif_party_created( map_session_data& sd, int result ){
  6471. PACKET_ZC_ACK_MAKE_GROUP p = {};
  6472. p.PacketType = HEADER_ZC_ACK_MAKE_GROUP;
  6473. p.result = result;
  6474. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6475. }
  6476. /// Adds new member to a party.
  6477. /// 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)
  6478. /// 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)
  6479. /// 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)
  6480. /// role:
  6481. /// 0 = leader
  6482. /// 1 = normal
  6483. /// state:
  6484. /// 0 = connected
  6485. /// 1 = disconnected
  6486. void clif_party_member_info( struct party_data& party, map_session_data& sd ){
  6487. int id = party_getmemberid( &party, &sd );
  6488. if( id < 0 ){
  6489. return;
  6490. }
  6491. PACKET_ZC_ADD_MEMBER_TO_GROUP p = {};
  6492. p.packetType = partymemberinfo;
  6493. p.AID = sd.status.account_id;
  6494. #if PACKETVER >= 20171207
  6495. p.GID = sd.status.char_id;
  6496. #endif
  6497. p.leader = ( party.party.member[id].leader != 0 ) ? 0 : 1;
  6498. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6499. p.class_ = sd.status.class_;
  6500. p.baseLevel = sd.status.base_level;
  6501. #endif
  6502. p.x = sd.bl.x;
  6503. p.y = sd.bl.y;
  6504. p.offline = ( party.party.member[id].online != 0 ) ? 0 : 1;
  6505. safestrncpy( p.partyName, party.party.name, sizeof( p.partyName ) );
  6506. safestrncpy( p.playerName, sd.status.name, sizeof( p.playerName ) );
  6507. mapindex_getmapname_ext( map_mapid2mapname( sd.bl.m ), p.mapName );
  6508. p.sharePickup = ( party.party.item&1 ) ? 1 : 0;
  6509. p.shareLoot = ( party.party.item&2 )? 1 : 0;
  6510. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6511. }
  6512. /// Sends party information (ZC_GROUP_LIST).
  6513. /// 00fb <packet len>.W <party name>.24B { <account id>.L <nick>.24B <map name>.16B <role>.B <state>.B }*
  6514. /// role:
  6515. /// 0 = leader
  6516. /// 1 = normal
  6517. /// state:
  6518. /// 0 = connected
  6519. /// 1 = disconnected
  6520. /// 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
  6521. void clif_party_info( struct party_data& party, map_session_data* sd ){
  6522. send_target target;
  6523. if( sd == nullptr ){
  6524. sd = party_getavailablesd( &party );
  6525. if( sd == nullptr ){
  6526. // Nobody is online to notify
  6527. return;
  6528. }
  6529. // Send to whole party
  6530. target = PARTY;
  6531. }else{
  6532. // Send only to self
  6533. target = SELF;
  6534. }
  6535. PACKET_ZC_GROUP_LIST* p = reinterpret_cast<PACKET_ZC_GROUP_LIST*>( packet_buffer );
  6536. p->packetType = partyinfo;
  6537. p->packetLen = sizeof( *p );
  6538. safestrncpy( p->partyName, party.party.name, sizeof( p->partyName ) );
  6539. for( int i = 0, c = 0; i < MAX_PARTY; i++ ){
  6540. struct party_member& m = party.party.member[i];
  6541. if( m.account_id == 0 ){
  6542. continue;
  6543. }
  6544. PACKET_ZC_GROUP_LIST_SUB& member = p->members[c];
  6545. member.AID = m.account_id;
  6546. #if PACKETVER >= 20171207
  6547. member.GID = m.char_id;
  6548. #endif
  6549. safestrncpy( member.playerName, m.name, sizeof( member.playerName ) );
  6550. mapindex_getmapname_ext( m.map, member.mapName );
  6551. member.leader = ( m.leader ) ? 0 : 1;
  6552. member.offline = ( m.online ) ? 0 : 1;
  6553. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6554. member.class_ = m.class_;
  6555. member.baseLevel = m.lv;
  6556. #endif
  6557. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( member ) );
  6558. c++;
  6559. }
  6560. clif_send( p, p->packetLen, &sd->bl, target );
  6561. }
  6562. /// The player's 'party invite' state, sent during login (ZC_PARTY_CONFIG).
  6563. /// 02c9 <flag>.B
  6564. /// flag:
  6565. /// 0 = allow party invites
  6566. /// 1 = auto-deny party invites
  6567. void clif_partyinvitationstate( map_session_data& sd ){
  6568. #if PACKETVER_MAIN_NUM >= 20070911 || defined(PACKETVER_RE) || PACKETVER_AD_NUM >= 20070911 || PACKETVER_SAK_NUM >= 20070904 || defined(PACKETVER_ZERO)
  6569. PACKET_ZC_PARTY_CONFIG p = {};
  6570. p.packetType = HEADER_ZC_PARTY_CONFIG;
  6571. p.denyPartyInvites = sd.status.disable_partyinvite;
  6572. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6573. #endif
  6574. }
  6575. /// Party invitation request.
  6576. /// 00fe <party id>.L <party name>.24B (ZC_REQ_JOIN_GROUP)
  6577. /// 02c6 <party id>.L <party name>.24B (ZC_PARTY_JOIN_REQ)
  6578. void clif_party_invite( map_session_data& sd, map_session_data& tsd ){
  6579. struct party_data* party = party_search( sd.status.party_id );
  6580. if( party == nullptr ){
  6581. return;
  6582. }
  6583. PACKET_ZC_PARTY_JOIN_REQ p = {};
  6584. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ;
  6585. p.GRID = party->party.party_id;
  6586. safestrncpy( p.groupName, party->party.name, sizeof( p.groupName ) );
  6587. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6588. }
  6589. /// Party invite result.
  6590. /// 00fd <nick>.24S <result>.B (ZC_ACK_REQ_JOIN_GROUP)
  6591. /// 02c5 <nick>.24S <result>.L (ZC_PARTY_JOIN_REQ_ACK)
  6592. /// result=0 : char is already in a party -> MsgStringTable[80]
  6593. /// result=1 : party invite was rejected -> MsgStringTable[81]
  6594. /// result=2 : party invite was accepted -> MsgStringTable[82]
  6595. /// result=3 : party is full -> MsgStringTable[83]
  6596. /// result=4 : char of the same account already joined the party -> MsgStringTable[608]
  6597. /// result=5 : char blocked party invite -> MsgStringTable[1324] (since 20070904)
  6598. /// result=7 : char is not online or doesn't exist -> MsgStringTable[71] (since 20070904)
  6599. /// result=8 : (%s) are currently in restricted map to join a party. -> MsgStringTable[1388] (since 20080527)
  6600. /// result=9 : Cannot join a party in this map. -> MsgStringTable[1871] (since 20110215)
  6601. /// result=10 : You cannot invite or withdraw while in memorial dungeon -> message: MSG_ID_BD3 (since 20161130)
  6602. /// result=11 : The character is a level that can not join the party -> message: MSG_ID_C9A (since 20170412)
  6603. void clif_party_invite_reply( map_session_data& sd, const char* nick, enum e_party_invite_reply reply ){
  6604. #if PACKETVER < 20070904
  6605. if( reply == PARTY_REPLY_OFFLINE ){
  6606. clif_displaymessage( sd.fd, msg_txt( &sd, 3 ) ); // Character not found.
  6607. return;
  6608. }
  6609. #endif
  6610. PACKET_ZC_PARTY_JOIN_REQ_ACK p = {};
  6611. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ_ACK;
  6612. safestrncpy( p.characterName, nick, sizeof( p.characterName ) );
  6613. p.result = reply;
  6614. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6615. }
  6616. /// Updates party settings.
  6617. /// 0101 <exp option>.L (ZC_GROUPINFO_CHANGE)
  6618. /// 07d8 <exp option>.L <item pick rule>.B <item share rule>.B (ZC_REQ_GROUPINFO_CHANGE_V2)
  6619. /// exp option:
  6620. /// 0 = exp sharing disabled
  6621. /// 1 = exp sharing enabled
  6622. /// 2 = cannot change exp sharing
  6623. ///
  6624. /// flag:
  6625. /// 0 = send to party
  6626. /// 1 = send to sd
  6627. void clif_party_option(struct party_data *p,map_session_data *sd,int flag)
  6628. {
  6629. unsigned char buf[16];
  6630. #if PACKETVER < 20090603
  6631. const int cmd = 0x101;
  6632. #else
  6633. const int cmd = 0x7d8;
  6634. #endif
  6635. nullpo_retv(p);
  6636. if(!sd && flag==0){
  6637. int i;
  6638. ARR_FIND(0,MAX_PARTY,i,p->data[i].sd);
  6639. if (i < MAX_PARTY)
  6640. sd = p->data[i].sd;
  6641. }
  6642. if(!sd) return;
  6643. WBUFW(buf,0)=cmd;
  6644. WBUFL(buf,2)=((flag&0x01)?2:p->party.exp);
  6645. #if PACKETVER >= 20090603
  6646. WBUFB(buf,6)=(p->party.item&1)?1:0;
  6647. WBUFB(buf,7)=(p->party.item&2)?1:0;
  6648. #endif
  6649. if(flag==0)
  6650. clif_send(buf,packet_len(cmd),&sd->bl,PARTY);
  6651. else
  6652. clif_send(buf,packet_len(cmd),&sd->bl,SELF);
  6653. }
  6654. /**
  6655. * 0105 <account id>.L <char name>.24B <result>.B (ZC_DELETE_MEMBER_FROM_GROUP).
  6656. * @param sd Send the notification for this player
  6657. * @param account_id Account ID of kicked member
  6658. * @param name Name of kicked member
  6659. * @param result Party leave result @see PARTY_MEMBER_WITHDRAW
  6660. * @param target Send target
  6661. **/
  6662. void clif_party_withdraw( map_session_data& sd, uint32 account_id, const char* name, enum e_party_member_withdraw result, enum send_target target ){
  6663. PACKET_ZC_DELETE_MEMBER_FROM_GROUP p = {};
  6664. p.PacketType = HEADER_ZC_DELETE_MEMBER_FROM_GROUP;
  6665. p.AID = account_id;
  6666. safestrncpy( p.characterName, name, sizeof( p.characterName ) );
  6667. p.result = result;
  6668. clif_send( &p, sizeof( p ), &sd.bl, target );
  6669. }
  6670. /// Party chat message (ZC_NOTIFY_CHAT_PARTY).
  6671. /// 0109 <packet len>.W <account id>.L <message>.?B
  6672. void clif_party_message( struct party_data& party, uint32 account_id, const char* mes, size_t len ){
  6673. map_session_data* sd = party_getavailablesd( &party );
  6674. if( sd == nullptr ){
  6675. return;
  6676. }
  6677. if (len > CHAT_SIZE_MAX) {
  6678. ShowWarning( "clif_party_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", party_id=%d).\n", mes, len, CHAT_SIZE_MAX, party.party.party_id );
  6679. len = CHAT_SIZE_MAX;
  6680. }
  6681. PACKET_ZC_NOTIFY_CHAT_PARTY* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT_PARTY*>( packet_buffer );
  6682. p->PacketType = HEADER_ZC_NOTIFY_CHAT_PARTY;
  6683. p->PacketLength = sizeof( *p );
  6684. p->AID = account_id;
  6685. safestrncpy( p->chatMsg, mes, len );
  6686. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  6687. clif_send( p, p->PacketLength, &sd->bl, PARTY );
  6688. }
  6689. /// Updates the position of a party member on the minimap (ZC_NOTIFY_POSITION_TO_GROUPM).
  6690. /// 0107 <account id>.L <x>.W <y>.W
  6691. void clif_party_xy( map_session_data& sd ){
  6692. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6693. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6694. p.AID = sd.status.account_id;
  6695. p.xPos = sd.bl.x;
  6696. p.yPos = sd.bl.y;
  6697. clif_send( &p, sizeof( p ), &sd.bl, PARTY_SAMEMAP_WOS );
  6698. }
  6699. /*==========================================
  6700. * Sends x/y dot to a single fd. [Skotlex]
  6701. *------------------------------------------*/
  6702. void clif_party_xy_single( map_session_data& sd, map_session_data& tsd ){
  6703. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6704. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6705. p.AID = sd.status.account_id;
  6706. p.xPos = sd.bl.x;
  6707. p.yPos = sd.bl.y;
  6708. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6709. }
  6710. /// Updates HP bar of a party member.
  6711. /// 0106 <account id>.L <hp>.W <max hp>.W (ZC_NOTIFY_HP_TO_GROUPM)
  6712. /// 080e <account id>.L <hp>.L <max hp>.L (ZC_NOTIFY_HP_TO_GROUPM_R2)
  6713. void clif_party_hp( map_session_data& sd ){
  6714. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6715. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6716. p.AID = sd.status.account_id;
  6717. #if PACKETVER < 20100119
  6718. // To correctly display the %hp bar. [Skotlex]
  6719. if( sd.battle_status.max_hp > INT16_MAX ){
  6720. p.hp = sd.battle_status.hp / ( sd.battle_status.max_hp / 100 );
  6721. p.maxhp = 100;
  6722. }else{
  6723. p.hp = sd.battle_status.hp;
  6724. p.maxhp = sd.battle_status.max_hp;
  6725. }
  6726. #else
  6727. p.hp = sd.battle_status.hp;
  6728. p.maxhp = sd.battle_status.max_hp;
  6729. #endif
  6730. clif_send( &p, sizeof( p ), &sd.bl, PARTY_AREA_WOS );
  6731. }
  6732. /// Notifies the party members of a character's death or revival.
  6733. /// 0AB2 <GID>.L <dead>.B
  6734. void clif_party_dead( map_session_data& sd ){
  6735. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6736. PACKET_ZC_GROUP_ISALIVE p = {};
  6737. p.packetType = HEADER_ZC_GROUP_ISALIVE;
  6738. p.AID = sd.status.account_id;
  6739. p.isDead = pc_isdead( &sd );
  6740. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6741. #endif
  6742. }
  6743. /// Updates the job and level of a party member
  6744. /// 0abd <account id>.L <job>.W <level>.W
  6745. void clif_party_job_and_level( map_session_data& sd ){
  6746. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  6747. PACKET_ZC_NOTIFY_MEMBERINFO_TO_GROUPM p = {};
  6748. p.PacketType = HEADER_ZC_NOTIFY_MEMBERINFO_TO_GROUPM;
  6749. p.AID = sd.status.account_id;
  6750. p.job = sd.status.class_;
  6751. p.level = sd.status.base_level;
  6752. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6753. #endif
  6754. }
  6755. /*==========================================
  6756. * Sends HP bar to a single fd. [Skotlex]
  6757. *------------------------------------------*/
  6758. void clif_hpmeter_single( map_session_data& sd, uint32 id, uint32 hp, uint32 maxhp ){
  6759. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6760. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6761. p.AID = id;
  6762. #if PACKETVER < 20100119
  6763. // To correctly display the %hp bar. [Skotlex]
  6764. if( maxhp > INT16_MAX ){
  6765. p.hp = hp / ( maxhp / 100 );
  6766. p.maxhp = 100;
  6767. }else{
  6768. p.hp = hp;
  6769. p.maxhp = maxhp;
  6770. }
  6771. #else
  6772. p.hp = hp;
  6773. p.maxhp = maxhp;
  6774. #endif
  6775. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6776. }
  6777. /// Notifies the client, that it's attack target is too far.
  6778. /// 0139 <target id>.L <target x>.W <target y>.W <x>.W <y>.W <atk range>.W (ZC_ATTACK_FAILURE_FOR_DISTANCE)
  6779. void clif_movetoattack( map_session_data& sd, block_list& bl ){
  6780. PACKET_ZC_ATTACK_FAILURE_FOR_DISTANCE packet{};
  6781. packet.PacketType = HEADER_ZC_ATTACK_FAILURE_FOR_DISTANCE;
  6782. packet.targetAID = bl.id;
  6783. packet.targetXPos = bl.x;
  6784. packet.targetYPos = bl.y;
  6785. packet.xPos = sd.bl.x;
  6786. packet.yPos = sd.bl.y;
  6787. packet.currentAttRange = sd.battle_status.rhw.range;
  6788. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6789. }
  6790. /// Notifies the client about the result of an item produce request (ZC_ACK_REQMAKINGITEM).
  6791. /// 018f <result>.W <name id>.W
  6792. /// result:
  6793. /// 0 = success
  6794. /// 1 = failure
  6795. /// 2 = success (alchemist)
  6796. /// 3 = failure (alchemist)
  6797. void clif_produceeffect(map_session_data* sd,int flag, t_itemid nameid){
  6798. nullpo_retv( sd );
  6799. clif_solved_charname( sd->fd, sd->status.char_id, sd->status.name );
  6800. PACKET_ZC_ACK_REQMAKINGITEM p = {};
  6801. p.packetType = HEADER_ZC_ACK_REQMAKINGITEM;
  6802. p.result = flag;
  6803. p.itemId = client_nameid( nameid );
  6804. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6805. }
  6806. /// Initiates the pet taming process.
  6807. /// 019e (ZC_START_CAPTURE)
  6808. void clif_catch_process( map_session_data& sd ){
  6809. PACKET_ZC_START_CAPTURE packet{};
  6810. packet.PacketType = HEADER_ZC_START_CAPTURE;
  6811. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6812. }
  6813. /// Displays the result of a pet taming attempt.
  6814. /// 01a0 <result>.B (ZC_TRYCAPTURE_MONSTER)
  6815. /// 0 = failure
  6816. /// 1 = success
  6817. void clif_pet_roulette( map_session_data& sd, bool success ){
  6818. PACKET_ZC_TRYCAPTURE_MONSTER packet{};
  6819. packet.PacketType = HEADER_ZC_TRYCAPTURE_MONSTER;
  6820. packet.result = success;
  6821. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6822. }
  6823. /// Presents a list of pet eggs that can be hatched (ZC_PETEGG_LIST).
  6824. /// 01a6 <packet len>.W { <index>.W }*
  6825. void clif_sendegg(map_session_data *sd)
  6826. {
  6827. int i,n=0,fd;
  6828. nullpo_retv(sd);
  6829. fd=sd->fd;
  6830. if (battle_config.pet_no_gvg && map_flag_gvg2(sd->bl.m)) { //Disable pet hatching in GvG grounds during Guild Wars [Skotlex]
  6831. clif_displaymessage(fd, msg_txt(sd,666));
  6832. return;
  6833. }
  6834. WFIFOHEAD(fd, MAX_INVENTORY * 2 + 4);
  6835. WFIFOW(fd,0)=0x1a6;
  6836. for(i=0,n=0;i<MAX_INVENTORY;i++){
  6837. if(sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr ||
  6838. sd->inventory_data[i]->type != IT_PETEGG ||
  6839. sd->inventory.u.items_inventory[i].amount <= 0)
  6840. continue;
  6841. WFIFOW(fd,n*2+4)=client_index(i);
  6842. n++;
  6843. }
  6844. WFIFOW(fd,2)=4+n*2;
  6845. WFIFOSET(fd,WFIFOW(fd,2));
  6846. sd->menuskill_id = SA_TAMINGMONSTER;
  6847. sd->menuskill_val = -1;
  6848. }
  6849. /// Sends a specific pet data update (ZC_CHANGESTATE_PET).
  6850. /// 01a4 <type>.B <id>.L <data>.L
  6851. /// type:
  6852. /// 0 = pre-init (data = 0)
  6853. /// 1 = intimacy (data = 0~4)
  6854. /// 2 = hunger (data = 0~4)
  6855. /// 3 = accessory
  6856. /// 4 = performance (data = 1~3: normal, 4: special)
  6857. /// 5 = hairstyle
  6858. /// 6 = close egg selection ui and update egg in inventory (PACKETVER >= 20180704)
  6859. ///
  6860. /// If sd is null, the update is sent to nearby objects, otherwise it is sent only to that player.
  6861. void clif_send_petdata(map_session_data* sd, struct pet_data* pd, int type, int param)
  6862. {
  6863. uint8 buf[16];
  6864. nullpo_retv(pd);
  6865. WBUFW(buf,0) = 0x1a4;
  6866. WBUFB(buf,2) = type;
  6867. WBUFL(buf,3) = pd->bl.id;
  6868. WBUFL(buf,7) = param;
  6869. if (sd)
  6870. clif_send(buf, packet_len(0x1a4), &sd->bl, SELF);
  6871. else
  6872. clif_send(buf, packet_len(0x1a4), &pd->bl, AREA);
  6873. }
  6874. /// Pet's base data (ZC_PROPERTY_PET).
  6875. /// 01a2 <name>.24B <renamed>.B <level>.W <hunger>.W <intimacy>.W <accessory id>.W <class>.W
  6876. void clif_send_petstatus(map_session_data *sd)
  6877. {
  6878. int fd;
  6879. struct s_pet *pet;
  6880. nullpo_retv(sd);
  6881. nullpo_retv(sd->pd);
  6882. fd=sd->fd;
  6883. pet = &sd->pd->pet;
  6884. WFIFOHEAD(fd,packet_len(0x1a2));
  6885. WFIFOW(fd,0)=0x1a2;
  6886. safestrncpy(WFIFOCP(fd,2),pet->name,NAME_LENGTH);
  6887. WFIFOB(fd,26)=battle_config.pet_rename?0:pet->rename_flag;
  6888. WFIFOW(fd,27)=pet->level;
  6889. WFIFOW(fd,29)=pet->hungry;
  6890. WFIFOW(fd,31)=pet->intimate;
  6891. WFIFOW(fd,33)=pet->equip;
  6892. #if PACKETVER >= 20081126
  6893. WFIFOW(fd,35)=pet->class_;
  6894. #endif
  6895. WFIFOSET(fd,packet_len(0x1a2));
  6896. }
  6897. /// Notification about a pet's emotion/talk (ZC_PET_ACT).
  6898. /// 01aa <id>.L <data>.L
  6899. /// data:
  6900. /// @see CZ_PET_ACT.
  6901. void clif_pet_emotion(struct pet_data *pd,int param)
  6902. {
  6903. unsigned char buf[16];
  6904. nullpo_retv(pd);
  6905. memset(buf,0,packet_len(0x1aa));
  6906. WBUFW(buf,0)=0x1aa;
  6907. WBUFL(buf,2)=pd->bl.id;
  6908. WBUFL(buf,6)=param;
  6909. clif_send(buf,packet_len(0x1aa),&pd->bl,AREA);
  6910. }
  6911. /// Result of request to feed a pet.
  6912. /// 01a3 <result>.B <name id>.W (ZC_FEED_PET)
  6913. /// result:
  6914. /// 0 = failure
  6915. /// 1 = success
  6916. void clif_pet_food( map_session_data& sd, int32 foodid, bool success ){
  6917. PACKET_ZC_FEED_PET packet{};
  6918. packet.packetType = HEADER_ZC_FEED_PET;
  6919. packet.result = success;
  6920. packet.itemId = client_nameid( foodid );
  6921. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6922. }
  6923. /// Send pet auto feed info.
  6924. void clif_pet_autofeed_status(map_session_data* sd, bool force) {
  6925. #if PACKETVER >= 20141008
  6926. nullpo_retv(sd);
  6927. if (force || battle_config.pet_autofeed_always) {
  6928. // Always send ON or OFF
  6929. if (sd->pd && battle_config.feature_pet_autofeed) {
  6930. clif_configuration(sd, CONFIG_PET_AUTOFEED, sd->pd->pet.autofeed);
  6931. }
  6932. else {
  6933. clif_configuration(sd, CONFIG_PET_AUTOFEED, false);
  6934. }
  6935. }
  6936. else {
  6937. // Only send when enabled
  6938. if (sd->pd && battle_config.feature_pet_autofeed && sd->pd->pet.autofeed) {
  6939. clif_configuration(sd, CONFIG_PET_AUTOFEED, true);
  6940. }
  6941. }
  6942. #endif
  6943. }
  6944. /// Presents a list of skills that can be auto-spelled.
  6945. /// 01cd { <skill id>.L }*7 (ZC_AUTOSPELLLIST)
  6946. void clif_autospell( map_session_data& sd, uint16 skill_lv ){
  6947. struct s_autospell_requirement{
  6948. uint16 skill_id;
  6949. uint16 required_autospell_skill_lv;
  6950. };
  6951. #ifdef RENEWAL
  6952. const std::vector<s_autospell_requirement> autospell_skills = {
  6953. { MG_FIREBOLT, 0 },
  6954. { MG_COLDBOLT, 0 },
  6955. { MG_LIGHTNINGBOLT, 0 },
  6956. { MG_SOULSTRIKE, 3 },
  6957. { MG_FIREBALL, 3 },
  6958. { WZ_EARTHSPIKE, 6 },
  6959. { MG_FROSTDIVER, 6 },
  6960. { MG_THUNDERSTORM, 9 },
  6961. { WZ_HEAVENDRIVE, 9 }
  6962. };
  6963. #else
  6964. const std::vector<s_autospell_requirement> autospell_skills = {
  6965. { MG_NAPALMBEAT, 0 },
  6966. { MG_COLDBOLT, 1 },
  6967. { MG_FIREBOLT, 1 },
  6968. { MG_LIGHTNINGBOLT, 1 },
  6969. { MG_SOULSTRIKE, 4 },
  6970. { MG_FIREBALL, 7 },
  6971. { MG_FROSTDIVER, 9 },
  6972. };
  6973. #endif
  6974. #if PACKETVER_MAIN_NUM >= 20181128 || PACKETVER_RE_NUM >= 20181031
  6975. PACKET_ZC_AUTOSPELLLIST* p = reinterpret_cast<PACKET_ZC_AUTOSPELLLIST*>( packet_buffer );
  6976. p->packetType = HEADER_ZC_AUTOSPELLLIST;
  6977. p->packetLength = sizeof( *p );
  6978. size_t count = 0;
  6979. for( const s_autospell_requirement& requirement : autospell_skills ){
  6980. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  6981. p->skills[count++] = requirement.skill_id;
  6982. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skills[0] ) );
  6983. }
  6984. }
  6985. clif_send( p, p->packetLength, &sd.bl, SELF );
  6986. #elif PACKETVER_MAIN_NUM >= 20090406 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_SAK_NUM >= 20080618
  6987. PACKET_ZC_AUTOSPELLLIST p = {};
  6988. p.packetType = HEADER_ZC_AUTOSPELLLIST;
  6989. size_t count = 0;
  6990. for( const s_autospell_requirement& requirement : autospell_skills ){
  6991. if( count == ARRAYLENGTH( p.skills ) ){
  6992. break;
  6993. }
  6994. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  6995. p.skills[count++] = requirement.skill_id;
  6996. }else{
  6997. p.skills[count++] = 0;
  6998. }
  6999. }
  7000. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7001. #endif
  7002. sd.menuskill_id = SA_AUTOSPELL;
  7003. sd.menuskill_val = skill_lv;
  7004. }
  7005. /// Devotion's visual effect (ZC_DEVOTIONLIST).
  7006. /// 01cf <devoter id>.L { <devotee id>.L }*5 <max distance>.W
  7007. void clif_devotion(struct block_list *src, map_session_data *tsd)
  7008. {
  7009. unsigned char buf[56];
  7010. nullpo_retv(src);
  7011. memset(buf,0,packet_len(0x1cf));
  7012. WBUFW(buf,0) = 0x1cf;
  7013. WBUFL(buf,2) = src->id;
  7014. if( src->type == BL_MER )
  7015. {
  7016. s_mercenary_data *md = BL_CAST(BL_MER,src);
  7017. if( md && md->master && md->devotion_flag )
  7018. WBUFL(buf,6) = md->master->bl.id;
  7019. WBUFW(buf,26) = skill_get_range2(src, ML_DEVOTION, mercenary_checkskill(md, ML_DEVOTION), false);
  7020. }
  7021. else
  7022. {
  7023. int i;
  7024. map_session_data *sd = BL_CAST(BL_PC,src);
  7025. if( sd == nullptr )
  7026. return;
  7027. for( i = 0; i < 5 /*MAX_DEVOTION*/; i++ ) // Client only able show to 5 links
  7028. WBUFL(buf,6+4*i) = sd->devotion[i];
  7029. WBUFW(buf,26) = skill_get_range2(src, CR_DEVOTION, pc_checkskill(sd, CR_DEVOTION), false);
  7030. }
  7031. if( tsd )
  7032. clif_send(buf, packet_len(0x1cf), &tsd->bl, SELF);
  7033. else
  7034. clif_send(buf, packet_len(0x1cf), src, AREA);
  7035. }
  7036. /// Notifies the client of an object's spirits.
  7037. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  7038. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  7039. void clif_spiritball( struct block_list *bl, struct block_list* target, enum send_target send_target ){
  7040. nullpo_retv( bl );
  7041. PACKET_ZC_SPIRITS p = {};
  7042. p.PacketType = HEADER_ZC_SPIRITS;
  7043. p.AID = bl->id;
  7044. switch( bl->type ){
  7045. case BL_PC:
  7046. p.num = ( (map_session_data*)bl )->spiritball;
  7047. break;
  7048. case BL_HOM:
  7049. p.num = ( (struct homun_data*)bl )->homunculus.spiritball;
  7050. break;
  7051. }
  7052. clif_send( &p, sizeof( p ), target == nullptr ? bl : target, send_target );
  7053. }
  7054. /// Notifies clients in area of a character's combo delay (ZC_COMBODELAY).
  7055. /// 01d2 <account id>.L <delay>.L
  7056. void clif_combo_delay(struct block_list *bl,t_tick wait)
  7057. {
  7058. unsigned char buf[32];
  7059. nullpo_retv(bl);
  7060. WBUFW(buf,0)=0x1d2;
  7061. WBUFL(buf,2)=bl->id;
  7062. WBUFL(buf,6)=client_tick(wait);
  7063. clif_send(buf,packet_len(0x1d2),bl,AREA);
  7064. }
  7065. /// Notifies clients in area that a character has blade-stopped another (ZC_BLADESTOP).
  7066. /// 01d1 <src id>.L <dst id>.L <flag>.L
  7067. /// flag:
  7068. /// 0 = inactive
  7069. /// 1 = active
  7070. void clif_bladestop(struct block_list *src, int dst_id, int active)
  7071. {
  7072. unsigned char buf[32];
  7073. nullpo_retv(src);
  7074. WBUFW(buf,0)=0x1d1;
  7075. WBUFL(buf,2)=src->id;
  7076. WBUFL(buf,6)=dst_id;
  7077. WBUFL(buf,10)=active;
  7078. clif_send(buf,packet_len(0x1d1),src,AREA);
  7079. }
  7080. /// MVP effect (ZC_MVP).
  7081. /// 010c <account id>.L
  7082. void clif_mvp_effect(map_session_data *sd)
  7083. {
  7084. unsigned char buf[16];
  7085. nullpo_retv(sd);
  7086. WBUFW(buf,0)=0x10c;
  7087. WBUFL(buf,2)=sd->bl.id;
  7088. clif_send(buf,packet_len(0x10c),&sd->bl,AREA);
  7089. }
  7090. /// MVP item reward message (ZC_MVP_GETTING_ITEM).
  7091. /// 010a <name id>.W
  7092. void clif_mvp_item( map_session_data *sd, t_itemid nameid ){
  7093. PACKET_ZC_MVP_GETTING_ITEM p = {};
  7094. p.packetType = HEADER_ZC_MVP_GETTING_ITEM;
  7095. p.itemId = client_nameid( nameid );
  7096. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  7097. }
  7098. /// MVP EXP reward message (ZC_MVP_GETTING_SPECIAL_EXP).
  7099. /// 010b <exp>.L
  7100. void clif_mvp_exp(map_session_data *sd, t_exp exp) {
  7101. #if PACKETVER >= 20131223 // Kro remove this packet [Napster]
  7102. if (battle_config.mvp_exp_reward_message) {
  7103. char e_msg[CHAT_SIZE_MAX];
  7104. sprintf(e_msg, msg_txt(sd, 717), exp);
  7105. clif_messagecolor(&sd->bl, color_table[COLOR_CYAN], e_msg, false, SELF); // Congratulations! You are the MVP! Your reward EXP Points are %u !!
  7106. }
  7107. #else
  7108. int fd;
  7109. nullpo_retv(sd);
  7110. fd = sd->fd;
  7111. WFIFOHEAD(fd, packet_len(0x10b));
  7112. WFIFOW(fd,0) = 0x10b;
  7113. WFIFOL(fd,2) = (uint32)u64min( exp, MAX_EXP );
  7114. WFIFOSET(fd, packet_len(0x10b));
  7115. #endif
  7116. }
  7117. /// Dropped MVP item reward message (ZC_THROW_MVPITEM).
  7118. /// 010d
  7119. ///
  7120. /// "You are the MVP, but cannot obtain the reward because
  7121. /// you are overweight."
  7122. void clif_mvp_noitem(map_session_data* sd)
  7123. {
  7124. int fd = sd->fd;
  7125. WFIFOHEAD(fd,packet_len(0x10d));
  7126. WFIFOW(fd,0) = 0x10d;
  7127. WFIFOSET(fd,packet_len(0x10d));
  7128. }
  7129. /// Guild creation result.
  7130. /// 0167 <result>.B (ZC_RESULT_MAKE_GUILD)
  7131. /// result:
  7132. /// 0 = "Guild has been created."
  7133. /// 1 = "You are already in a Guild."
  7134. /// 2 = "That Guild Name already exists."
  7135. /// 3 = "You need the neccessary item to create a Guild."
  7136. void clif_guild_created( map_session_data& sd, int flag ){
  7137. PACKET_ZC_RESULT_MAKE_GUILD p = {};
  7138. p.packetType = HEADER_ZC_RESULT_MAKE_GUILD;
  7139. p.result = static_cast<decltype(p.result)>( flag );
  7140. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7141. }
  7142. /// Notifies the client that it is belonging to a guild (ZC_UPDATE_GDID).
  7143. /// 016c <guild id>.L <emblem id>.L <mode>.L <ismaster>.B <inter sid>.L <guild name>.24B
  7144. /// mode:
  7145. /// &0x01 = allow invite
  7146. /// &0x10 = allow expel
  7147. void clif_guild_belonginfo( map_session_data& sd ){
  7148. if( sd.guild == nullptr ){
  7149. return;
  7150. }
  7151. const auto &guild = sd.guild->guild;
  7152. int position = guild_getposition(sd);
  7153. if( position < 0 ){
  7154. return;
  7155. }
  7156. PACKET_ZC_UPDATE_GDID p = {};
  7157. p.PacketType = HEADER_ZC_UPDATE_GDID;
  7158. p.guildId = guild.guild_id;
  7159. p.emblemVersion = guild.emblem_id;
  7160. p.mode = guild.position[position].mode;
  7161. p.isMaster = ( sd.state.gmaster_flag == 1 );
  7162. p.interSid = 0; // InterSID (unknown purpose)
  7163. safestrncpy( p.guildName, guild.name, sizeof( p.guildName ) );
  7164. #if PACKETVER_MAIN_NUM >= 20220216
  7165. p.masterGID = guild.member[0].char_id;
  7166. #endif
  7167. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7168. }
  7169. /// Guild member login notice.
  7170. /// 016d <account id>.L <char id>.L <status>.L (ZC_UPDATE_CHARSTAT)
  7171. /// 01f2 <account id>.L <char id>.L <status>.L <gender>.W <hair style>.W <hair color>.W (ZC_UPDATE_CHARSTAT2)
  7172. /// status:
  7173. /// 0 = offline
  7174. /// 1 = online
  7175. void clif_guild_memberlogin_notice(const struct mmo_guild &g,int idx,int flag)
  7176. {
  7177. unsigned char buf[64];
  7178. map_session_data* sd;
  7179. WBUFW(buf, 0)=0x1f2;
  7180. WBUFL(buf, 2)=g.member[idx].account_id;
  7181. WBUFL(buf, 6)=g.member[idx].char_id;
  7182. WBUFL(buf,10)=flag;
  7183. if( ( sd = g.member[idx].sd ) != nullptr )
  7184. {
  7185. WBUFW(buf,14) = sd->status.sex;
  7186. WBUFW(buf,16) = sd->status.hair;
  7187. WBUFW(buf,18) = sd->status.hair_color;
  7188. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD_WOS);
  7189. }
  7190. else if( ( sd = guild_getavailablesd(g) ) != nullptr )
  7191. {
  7192. WBUFW(buf,14) = 0;
  7193. WBUFW(buf,16) = 0;
  7194. WBUFW(buf,18) = 0;
  7195. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD);
  7196. }
  7197. }
  7198. // Function `clif_guild_memberlogin_notice` sends info about
  7199. // logins and logouts of a guild member to the rest members.
  7200. // But at the 1st time (after a player login or map changing)
  7201. // the client won't show the message.
  7202. // So I suggest use this function for sending "first-time-info"
  7203. // to some player on entering the game or changing location.
  7204. // At next time the client would always show the message.
  7205. // The function sends all the statuses in the single packet
  7206. // to economize traffic. [LuzZza]
  7207. void clif_guild_send_onlineinfo(map_session_data *sd)
  7208. {
  7209. unsigned char buf[14*128];
  7210. int i, count=0, p_len;
  7211. nullpo_retv(sd);
  7212. p_len = packet_len(0x16d);
  7213. auto &g = sd->guild;
  7214. if (!g)
  7215. return;
  7216. for(i=0; i<g->guild.max_member; i++) {
  7217. if(g->guild.member[i].account_id > 0 &&
  7218. g->guild.member[i].account_id != sd->status.account_id) {
  7219. WBUFW(buf,count*p_len) = 0x16d;
  7220. WBUFL(buf,count*p_len+2) = g->guild.member[i].account_id;
  7221. WBUFL(buf,count*p_len+6) = g->guild.member[i].char_id;
  7222. WBUFL(buf,count*p_len+10) = g->guild.member[i].online;
  7223. count++;
  7224. }
  7225. }
  7226. clif_send(buf, p_len*count, &sd->bl, SELF);
  7227. }
  7228. /// Bitmask of enabled guild window tabs (ZC_ACK_GUILD_MENUINTERFACE).
  7229. /// 014e <menu flag>.L
  7230. /// menu flag:
  7231. /// 0x00 = Basic Info (always on)
  7232. /// &0x01 = Member manager
  7233. /// &0x02 = Positions
  7234. /// &0x04 = Skills
  7235. /// &0x10 = Expulsion list
  7236. /// &0x40 = Unknown (GMENUFLAG_ALLGUILDLIST)
  7237. /// &0x80 = Notice
  7238. void clif_guild_masterormember(map_session_data *sd)
  7239. {
  7240. int fd;
  7241. nullpo_retv(sd);
  7242. fd=sd->fd;
  7243. WFIFOHEAD(fd,packet_len(0x14e));
  7244. WFIFOW(fd,0) = 0x14e;
  7245. WFIFOL(fd,2) = (sd->state.gmaster_flag) ? 0xd7 : 0x57;
  7246. WFIFOSET(fd,packet_len(0x14e));
  7247. }
  7248. /// Guild basic information (Territories [Valaris])
  7249. /// 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)
  7250. /// 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)
  7251. /// 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)
  7252. void clif_guild_basicinfo( map_session_data& sd ){
  7253. if( sd.guild == nullptr ){
  7254. return;
  7255. }
  7256. const auto &guild = sd.guild->guild;
  7257. PACKET_ZC_GUILD_INFO p = {};
  7258. p.PacketType = HEADER_ZC_GUILD_INFO;
  7259. p.GDID = guild.guild_id;
  7260. p.level = guild.guild_lv;
  7261. p.userNum = guild.connect_member;
  7262. p.maxUserNum = guild.max_member;
  7263. p.userAverageLevel = guild.average_lv;
  7264. p.exp = (uint32)cap_value( guild.exp, 0, MAX_GUILD_EXP );
  7265. p.maxExp = (uint32)cap_value( guild.next_exp, 0, MAX_GUILD_EXP );
  7266. p.point = 0; // Tax Points
  7267. p.honor = 0; // Honor: (left) Vulgar [-100,100] Famed (right)
  7268. p.virtue = 0; // Virtue: (down) Wicked [-100,100] Righteous (up)
  7269. p.emblemVersion = guild.emblem_id;
  7270. safestrncpy( p.guildname, guild.name, sizeof( p.guildname ) );
  7271. safestrncpy( p.manageLand, msg_txt( &sd, 300 + guild_checkcastles( guild ) ), sizeof( p.manageLand ) );
  7272. p.zeny = 0;
  7273. #if PACKETVER >= 20200902
  7274. p.masterGID = guild.member[0].char_id; // leader
  7275. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7276. #elif PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7277. p.masterGID = guild.member[0].char_id; // leader
  7278. #else
  7279. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7280. #endif
  7281. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7282. }
  7283. /// Guild alliance and opposition list (ZC_MYGUILD_BASIC_INFO).
  7284. /// 014c <packet len>.W { <relation>.L <guild id>.L <guild name>.24B }*
  7285. void clif_guild_allianceinfo(map_session_data *sd)
  7286. {
  7287. int fd,i,c;
  7288. nullpo_retv(sd);
  7289. auto &g = sd->guild;
  7290. if (!g)
  7291. return;
  7292. fd = sd->fd;
  7293. WFIFOHEAD(fd, MAX_GUILDALLIANCE * 32 + 4);
  7294. WFIFOW(fd, 0)=0x14c;
  7295. for(i=c=0;i<MAX_GUILDALLIANCE;i++){
  7296. struct guild_alliance *a=&g->guild.alliance[i];
  7297. if(a->guild_id>0){
  7298. WFIFOL(fd,c*32+4)=a->opposition;
  7299. WFIFOL(fd,c*32+8)=a->guild_id;
  7300. safestrncpy(WFIFOCP(fd,c*32+12),a->name,NAME_LENGTH);
  7301. c++;
  7302. }
  7303. }
  7304. WFIFOW(fd, 2)=c*32+4;
  7305. WFIFOSET(fd,WFIFOW(fd,2));
  7306. }
  7307. /// Guild member manager information
  7308. /// 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)
  7309. /// state:
  7310. /// 0 = offline
  7311. /// 1 = online
  7312. /// memo:
  7313. /// probably member's self-introduction (unused, no client UI/packets for editing it)
  7314. /// 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)
  7315. void clif_guild_memberlist( map_session_data& sd ){
  7316. if( sd.guild == nullptr ){
  7317. return;
  7318. }
  7319. const auto &guild = sd.guild->guild;
  7320. PACKET_ZC_MEMBERMGR_INFO* p = reinterpret_cast<PACKET_ZC_MEMBERMGR_INFO*>( packet_buffer );
  7321. p->PacketType = HEADER_ZC_MEMBERMGR_INFO;
  7322. p->packetLength = sizeof( *p );
  7323. for( int i = 0, c = 0; i < guild.max_member; i++ ){
  7324. const auto &member = guild.member[i];
  7325. if( member.account_id == 0 ){
  7326. continue;
  7327. }
  7328. GUILD_MEMBER_INFO& member_info = p->guildMemberInfo[c];
  7329. member_info.AID = member.account_id;
  7330. member_info.GID = member.char_id;
  7331. member_info.head = member.hair;
  7332. member_info.headPalette = member.hair_color;
  7333. member_info.sex = member.gender;
  7334. member_info.job = member.class_;
  7335. member_info.level = member.lv;
  7336. member_info.contributionExp = (uint32)cap_value( member.exp, 0, MAX_GUILD_EXP );
  7337. member_info.currentState = member.online;
  7338. member_info.positionID = member.position;
  7339. #if PACKETVER >= 20200902
  7340. member_info.lastLoginTime = member.last_login;
  7341. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7342. #elif PACKETVER_MAIN_NUM >= 20161214 || PACKETVER_RE_NUM >= 20161130 || defined(PACKETVER_ZERO)
  7343. member_info.lastLoginTime = member.last_login;
  7344. #else
  7345. 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.
  7346. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7347. #endif
  7348. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( member_info ) );
  7349. c++;
  7350. }
  7351. clif_send( p, p->packetLength, &sd.bl, SELF );
  7352. }
  7353. /// Guild position name information (ZC_POSITION_ID_NAME_INFO).
  7354. /// 0166 <packet len>.W { <position id>.L <position name>.24B }*
  7355. void clif_guild_positionnamelist(map_session_data *sd)
  7356. {
  7357. int i,fd;
  7358. nullpo_retv(sd);
  7359. auto &g = sd->guild;
  7360. if (!g)
  7361. return;
  7362. fd = sd->fd;
  7363. WFIFOHEAD(fd, MAX_GUILDPOSITION * 28 + 4);
  7364. WFIFOW(fd, 0)=0x166;
  7365. for(i=0;i<MAX_GUILDPOSITION;i++){
  7366. WFIFOL(fd,i*28+4)=i;
  7367. safestrncpy(WFIFOCP(fd,i*28+8),g->guild.position[i].name,NAME_LENGTH);
  7368. }
  7369. WFIFOW(fd,2)=i*28+4;
  7370. WFIFOSET(fd,WFIFOW(fd,2));
  7371. }
  7372. /// Guild position information
  7373. /// 0160 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L }* (ZC_POSITION_INFO)
  7374. /// mode:
  7375. /// See enum e_guild_permission
  7376. /// ranking:
  7377. /// TODO
  7378. static void clif_guild_positioninfolist(map_session_data& sd){
  7379. auto &g = sd.guild;
  7380. if (g == nullptr){
  7381. return;
  7382. }
  7383. PACKET_ZC_POSITION_INFO* p = reinterpret_cast<PACKET_ZC_POSITION_INFO*>( packet_buffer );
  7384. p->PacketType = HEADER_ZC_POSITION_INFO;
  7385. p->PacketLength = sizeof(*p);
  7386. for(size_t i=0;i<MAX_GUILDPOSITION;i++){
  7387. guild_position& gp = g->guild.position[i];
  7388. p->posInfo[i].positionID = i;
  7389. p->posInfo[i].right = gp.mode;
  7390. p->posInfo[i].ranking = i;
  7391. p->posInfo[i].payRate = gp.exp_mode;
  7392. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->posInfo[0] ) );
  7393. }
  7394. clif_send(p,p->PacketLength,&sd.bl,SELF);
  7395. }
  7396. /// Notifies clients in a guild about updated position information.
  7397. /// 0174 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <position name>.24B }* (ZC_ACK_CHANGE_GUILD_POSITIONINFO)
  7398. /// mode:
  7399. /// &0x01 = allow invite
  7400. /// &0x10 = allow expel
  7401. /// ranking:
  7402. /// TODO
  7403. void clif_guild_positionchanged(const struct mmo_guild &g,int idx)
  7404. {
  7405. // FIXME: This packet is intended to update the clients after a
  7406. // commit of position info changes, not sending one packet per
  7407. // position.
  7408. map_session_data *sd;
  7409. unsigned char buf[128];
  7410. WBUFW(buf, 0)=0x174;
  7411. WBUFW(buf, 2)=44; // packet len
  7412. // GUILD_REG_POSITION_INFO{
  7413. WBUFL(buf, 4)=idx;
  7414. WBUFL(buf, 8)=g.position[idx].mode;
  7415. WBUFL(buf,12)=idx;
  7416. WBUFL(buf,16)=g.position[idx].exp_mode;
  7417. safestrncpy(WBUFCP(buf,20),g.position[idx].name,NAME_LENGTH);
  7418. // }*
  7419. if( (sd=guild_getavailablesd(g))!=nullptr )
  7420. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7421. }
  7422. /// Notifies clients in a guild about updated member position assignments.
  7423. /// 0156 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (ZC_ACK_REQ_CHANGE_MEMBERS)
  7424. void clif_guild_memberpositionchanged(const struct mmo_guild &g, int idx)
  7425. {
  7426. // FIXME: This packet is intended to update the clients after a
  7427. // commit of member position assignment changes, not sending one
  7428. // packet per position.
  7429. map_session_data *sd;
  7430. unsigned char buf[64];
  7431. WBUFW(buf, 0)=0x156;
  7432. WBUFW(buf, 2)=16; // packet len
  7433. // MEMBER_POSITION_INFO{
  7434. WBUFL(buf, 4)=g.member[idx].account_id;
  7435. WBUFL(buf, 8)=g.member[idx].char_id;
  7436. WBUFL(buf,12)=g.member[idx].position;
  7437. // }*
  7438. if( (sd=guild_getavailablesd(g))!=nullptr )
  7439. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7440. }
  7441. /// Sends emblems bitmap data to the client that requested it.
  7442. /// 0152 <packet len>.W <guild id>.L <emblem id>.L <emblem data>.?B (ZC_GUILD_EMBLEM_IMG)
  7443. void clif_guild_emblem(const map_session_data &sd, const struct mmo_guild &g)
  7444. {
  7445. int fd = sd.fd;
  7446. if( g.emblem_len <= 0 )
  7447. return;
  7448. WFIFOHEAD(fd,g.emblem_len+12);
  7449. WFIFOW(fd,0)=0x152;
  7450. WFIFOW(fd,2)=g.emblem_len+12;
  7451. WFIFOL(fd,4)=g.guild_id;
  7452. WFIFOL(fd,8)=g.emblem_id;
  7453. memcpy(WFIFOP(fd,12),g.emblem_data,g.emblem_len);
  7454. WFIFOSET(fd,WFIFOW(fd,2));
  7455. }
  7456. /// Sends update of the guild id/emblem id to everyone in the area.
  7457. /// 01b4 <id>.L <guild id>.L <emblem id>.W (ZC_CHANGE_GUILD)
  7458. /// 0b1f <guild id>.L <version>.L <AID>.L (ZC_NEW_EMBLEM_DOWNLOAD)
  7459. /// 0b47 <guild id>.L <version>.L <AID>.L (ZC_ACK_ADD_NEW_EMBLEM)
  7460. void clif_guild_emblem_area(struct block_list* bl)
  7461. {
  7462. // TODO this packet doesn't force the update of ui components that have the emblem visible
  7463. // (emblem in the flag npcs and emblem over the head in agit maps) [FlavioJS]
  7464. PACKET_ZC_CHANGE_GUILD p{};
  7465. p.packetType = HEADER_ZC_CHANGE_GUILD;
  7466. p.guild_id = status_get_guild_id(bl);
  7467. p.emblem_id = status_get_emblem_id(bl);
  7468. p.AID = bl->id;
  7469. clif_send(&p, sizeof(p), bl, AREA_WOS);
  7470. }
  7471. /// Sends guild skills.
  7472. /// 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)
  7473. void clif_guild_skillinfo( map_session_data& sd ){
  7474. auto& g = sd.guild;
  7475. if( g == nullptr ){
  7476. return;
  7477. }
  7478. PACKET_ZC_GUILD_SKILLINFO* p = reinterpret_cast<PACKET_ZC_GUILD_SKILLINFO*>( packet_buffer );
  7479. p->PacketType = HEADER_ZC_GUILD_SKILLINFO;
  7480. p->PacketLength = sizeof( *p );
  7481. p->skillPoint = g->guild.skill_point;
  7482. for( size_t i = 0, c = 0; i < MAX_GUILDSKILL; i++ ){
  7483. if( g->guild.skill[i].id <= 0 ){
  7484. continue;
  7485. }
  7486. if( !guild_check_skill_require( g->guild, g->guild.skill[i].id ) ){
  7487. continue;
  7488. }
  7489. GUILD_SKILLDATA& gs = p->skillInfo[c];
  7490. int skill_id = g->guild.skill[i].id;
  7491. gs.id = skill_id;
  7492. gs.inf = skill_get_inf(skill_id);
  7493. gs.level = g->guild.skill[i].lv;
  7494. if( g->guild.skill[i].lv > 0 ){
  7495. gs.sp = skill_get_sp( skill_id, g->guild.skill[i].lv );
  7496. gs.range2 = skill_get_range( skill_id, g->guild.skill[i].lv );
  7497. }else{
  7498. gs.sp = 0;
  7499. gs.range2 = 0;
  7500. }
  7501. safestrncpy( gs.name, skill_get_name( skill_id ), sizeof( gs.name ) );
  7502. gs.upFlag = ( g->guild.skill[i].lv < guild_skill_get_max( skill_id ) && &sd == g->guild.member[0].sd ) ? 1 : 0;
  7503. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( gs ) );
  7504. c++;
  7505. }
  7506. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7507. }
  7508. /// Sends guild notice to client.
  7509. /// 016f <subject>.60B <notice>.120B (ZC_GUILD_NOTICE)
  7510. void clif_guild_notice( map_session_data& sd ){
  7511. auto& g = sd.guild;
  7512. if( g == nullptr ){
  7513. return;
  7514. }
  7515. if( g->guild.mes1[0] == '\0' && g->guild.mes2[0] == '\0' ){
  7516. return;
  7517. }
  7518. PACKET_ZC_GUILD_NOTICE p = {};
  7519. p.packetType = HEADER_ZC_GUILD_NOTICE;
  7520. safestrncpy( p.subject, g->guild.mes1, sizeof( p.subject ) );
  7521. safestrncpy( p.notice, g->guild.mes2, sizeof( p.notice ) );
  7522. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7523. }
  7524. /// Guild invite.
  7525. /// 016a <guild id>.L <guild name>.24B (ZC_REQ_JOIN_GUILD)
  7526. void clif_guild_invite( map_session_data& sd, const struct mmo_guild& g ){
  7527. PACKET_ZC_REQ_JOIN_GUILD p = {};
  7528. p.packetType = HEADER_ZC_REQ_JOIN_GUILD;
  7529. p.guild_id = g.guild_id;
  7530. safestrncpy( p.guild_name, g.name, sizeof( p.guild_name ) );
  7531. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7532. }
  7533. /// Reply to invite request.
  7534. /// 0169 <answer>.B (ZC_ACK_REQ_JOIN_GUILD)
  7535. /// answer:
  7536. /// 0 = Already in guild.
  7537. /// 1 = Offer rejected.
  7538. /// 2 = Offer accepted.
  7539. /// 3 = Guild full.
  7540. void clif_guild_inviteack( map_session_data& sd, int flag ){
  7541. PACKET_ZC_ACK_REQ_JOIN_GUILD p = {};
  7542. p.packetType = HEADER_ZC_ACK_REQ_JOIN_GUILD;
  7543. p.result = static_cast<decltype(p.result)>( flag );
  7544. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7545. }
  7546. /// Notifies clients of a guild of a leaving member.
  7547. /// 015a <char name>.24B <reason>.40B (ZC_ACK_LEAVE_GUILD)
  7548. /// 0a83 <CID>.L <reason>.40B (ZC_ACK_LEAVE_GUILD_DELNAME)
  7549. void clif_guild_leave( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7550. PACKET_ZC_ACK_LEAVE_GUILD p = {};
  7551. p.packetType = guildLeave;
  7552. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7553. p.GID = char_id;
  7554. #else
  7555. safestrncpy( p.name, name, sizeof( p.name ) );
  7556. #endif
  7557. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7558. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7559. }
  7560. /// Notifies clients of a guild of an expelled member.
  7561. /// 015c <char name>.24B <reason>.40B <account name>.24B (ZC_ACK_BAN_GUILD)
  7562. /// 0839 <char name>.24B <reason>.40B (ZC_ACK_BAN_GUILD_SSO)
  7563. /// 0a82 <CID>.L <reason>.40B (ZC_ACK_BAN_GUILD_DELNAME)
  7564. void clif_guild_expulsion( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7565. PACKET_ZC_ACK_BAN_GUILD p = {};
  7566. p.packetType = guildExpulsion;
  7567. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7568. p.GID = char_id;
  7569. #else
  7570. safestrncpy( p.name, name, sizeof( p.name ) );
  7571. #endif
  7572. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7573. #if PACKETVER < 20100803
  7574. // account name is not sent for security reasons
  7575. safestrncpy( p.account_name, "", sizeof( p.account_name ) );
  7576. #endif
  7577. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7578. }
  7579. /// Guild expulsion list
  7580. /// 0163 <packet len>.W { <char name>.24B <account name>.24B <reason>.40B }* (ZC_BAN_LIST)
  7581. /// 0163 <packet len>.W { <char name>.24B <reason>.40B }* (PACKETVER >= 20100803) (ZC_BAN_LIST)
  7582. /// 0a87 <packet len>.W { <charid>.L <reason>.40B }* (ZC_BAN_LIST2)
  7583. /// 0b7c <packet len>.W { <charid>.L <reason>.40B <char name>.24B }* (ZC_BAN_LIST3)
  7584. static void clif_guild_expulsionlist(map_session_data& sd){
  7585. auto &g = sd.guild;
  7586. if (!g)
  7587. return;
  7588. PACKET_ZC_BAN_LIST* p = reinterpret_cast<PACKET_ZC_BAN_LIST*>( packet_buffer );
  7589. p->packetType = HEADER_ZC_BAN_LIST;
  7590. p->packetLen = sizeof(*p);
  7591. for( size_t i = 0, c = 0; i < MAX_GUILDEXPULSION; i++ ){
  7592. struct guild_expulsion& e = g->guild.expulsion[i];
  7593. if( e.account_id == 0 ){
  7594. continue;
  7595. }
  7596. PACKET_ZC_BAN_LIST_sub& banned = p->chars[c];
  7597. #if PACKETVER >= 20161019
  7598. banned.char_id = e.char_id;
  7599. #elif PACKETVER < 20100803
  7600. // account name (not used for security reasons)
  7601. safestrncpy(banned.account_name, "", sizeof(banned.account_name));
  7602. #endif
  7603. #if PACKETVER >= 20200902 || PACKETVER < 20161019
  7604. safestrncpy(banned.char_name, e.name, sizeof(banned.char_name));
  7605. #endif
  7606. safestrncpy(banned.message, e.mes, sizeof(banned.message));
  7607. p->packetLen += static_cast<decltype(p->packetLen)>(sizeof(banned));
  7608. c++;
  7609. }
  7610. clif_send(p,p->packetLen,&sd.bl,SELF);
  7611. }
  7612. /// Guild chat message
  7613. /// 017f <packet len>.W <message>.?B (ZC_GUILD_CHAT)
  7614. void clif_guild_message( const struct mmo_guild& g, const char* mes, size_t len ){
  7615. PACKET_ZC_GUILD_CHAT *p = reinterpret_cast<PACKET_ZC_GUILD_CHAT*>( packet_buffer );
  7616. // -1 for null terminator
  7617. static const size_t max_len = CHAT_SIZE_MAX - sizeof( *p ) - 1;
  7618. map_session_data* sd = guild_getavailablesd(g);
  7619. // Ignore this message, if no guildmember is available
  7620. if (sd == nullptr)
  7621. return;
  7622. if( len == 0 ){
  7623. return;
  7624. } else if( len > max_len ){
  7625. ShowWarning("clif_guild_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", guild_id=%u).\n", mes, len, max_len, g.guild_id);
  7626. len = max_len;
  7627. }
  7628. p->packetType = HEADER_ZC_GUILD_CHAT;
  7629. p->packetLength = sizeof(*p);
  7630. safestrncpy(p->message, mes, len+1);
  7631. p->packetLength += static_cast<decltype(p->packetLength)>( len + 1 );
  7632. clif_send(p, p->packetLength, &sd->bl, GUILD_NOBG);
  7633. }
  7634. /// Request for guild alliance
  7635. /// 0171 <inviter account id>.L <guild name>.24B (ZC_REQ_ALLY_GUILD).
  7636. void clif_guild_reqalliance(map_session_data& sd,uint32 account_id,const char *name)
  7637. {
  7638. PACKET_ZC_REQ_ALLY_GUILD p{};
  7639. p.packetType = HEADER_ZC_REQ_ALLY_GUILD;
  7640. p.inviterId = account_id;
  7641. safestrncpy(p.inviterGuildName,name,sizeof(p.inviterGuildName));
  7642. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7643. }
  7644. /// Notifies the client about the result of a alliance request.
  7645. /// 0173 <answer>.B (ZC_ACK_REQ_ALLY_GUILD)
  7646. /// answer:
  7647. /// 0 = Already allied.
  7648. /// 1 = You rejected the offer.
  7649. /// 2 = You accepted the offer.
  7650. /// 3 = They have too any alliances.
  7651. /// 4 = You have too many alliances.
  7652. /// 5 = Alliances are disabled.
  7653. void clif_guild_allianceack(map_session_data& sd,uint8 flag){
  7654. PACKET_ZC_ACK_REQ_ALLY_GUILD p{};
  7655. p.packetType = HEADER_ZC_ACK_REQ_ALLY_GUILD;
  7656. p.flag = flag;
  7657. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7658. }
  7659. /// Notifies the client that a alliance or opposition has been removed.
  7660. /// 0184 <other guild id>.L <relation>.L (ZC_DELETE_RELATED_GUILD)
  7661. /// relation:
  7662. /// 0 = Ally
  7663. /// 1 = Enemy
  7664. void clif_guild_delalliance(map_session_data& sd,uint32 guild_id,uint32 flag)
  7665. {
  7666. PACKET_ZC_DELETE_RELATED_GUILD p{};
  7667. p.packetType = HEADER_ZC_DELETE_RELATED_GUILD;
  7668. p.allyID = guild_id;
  7669. p.flag = flag;
  7670. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7671. }
  7672. /// Notifies the client about the result of a opposition request.
  7673. /// 0181 <result>.B (ZC_ACK_REQ_HOSTILE_GUILD)
  7674. /// result:
  7675. /// 0 = Antagonist has been set.
  7676. /// 1 = Guild has too many Antagonists.
  7677. /// 2 = Already set as an Antagonist.
  7678. /// 3 = Antagonists are disabled.
  7679. void clif_guild_oppositionack(map_session_data& sd,uint8 flag){
  7680. PACKET_ZC_ACK_REQ_HOSTILE_GUILD p{};
  7681. p.packetType = HEADER_ZC_ACK_REQ_HOSTILE_GUILD;
  7682. p.flag = flag;
  7683. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7684. }
  7685. /// Adds alliance or opposition (ZC_ADD_RELATED_GUILD).
  7686. /// 0185 <relation>.L <guild id>.L <guild name>.24B
  7687. /*
  7688. void clif_guild_allianceadded(struct guild *g,int idx)
  7689. {
  7690. unsigned char buf[64];
  7691. WBUFW(buf,0)=0x185;
  7692. WBUFL(buf,2)=g->alliance[idx].opposition;
  7693. WBUFL(buf,6)=g->alliance[idx].guild_id;
  7694. safestrncpy(WBUFP(buf,10),g->alliance[idx].name,NAME_LENGTH);
  7695. clif_send(buf,packet_len(0x185),guild_getavailablesd(g),GUILD);
  7696. }
  7697. */
  7698. /// Notifies the client about the result of a guild break.
  7699. /// 015e <reason>.L (ZC_ACK_DISORGANIZE_GUILD_RESULT)
  7700. /// 0 = success
  7701. /// 1 = invalid key (guild name, @see clif_parse_GuildBreak)
  7702. /// 2 = there are still members in the guild
  7703. void clif_guild_broken( map_session_data& sd, int flag ){
  7704. PACKET_ZC_ACK_DISORGANIZE_GUILD_RESULT p = {};
  7705. p.packetType = HEADER_ZC_ACK_DISORGANIZE_GUILD_RESULT;
  7706. p.result = flag;
  7707. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7708. }
  7709. /// Displays emotion on an object (ZC_EMOTION).
  7710. /// 00c0 <id>.L <type>.B
  7711. /// type:
  7712. /// enum emotion_type
  7713. void clif_emotion(struct block_list *bl,int type)
  7714. {
  7715. unsigned char buf[8];
  7716. nullpo_retv(bl);
  7717. WBUFW(buf,0)=0xc0;
  7718. WBUFL(buf,2)=bl->id;
  7719. WBUFB(buf,6)=type;
  7720. clif_send(buf,packet_len(0xc0),bl,AREA);
  7721. }
  7722. /// Displays the contents of a talkiebox trap.
  7723. /// 0191 <id>.L <contents>.80B (ZC_TALKBOX_CHATCONTENTS)
  7724. void clif_talkiebox( struct block_list* bl, const char* talkie ){
  7725. nullpo_retv( bl );
  7726. nullpo_retv( talkie );
  7727. PACKET_ZC_TALKBOX_CHATCONTENTS p = {};
  7728. p.PacketType = HEADER_ZC_TALKBOX_CHATCONTENTS;
  7729. p.aid = bl->id;
  7730. safestrncpy( p.message, talkie, TALKBOX_MESSAGE_SIZE );
  7731. clif_send( &p, sizeof( PACKET_ZC_TALKBOX_CHATCONTENTS ), bl, AREA );
  7732. }
  7733. /// Displays wedding effect centered on an object (ZC_CONGRATULATION).
  7734. /// 01ea <id>.L
  7735. void clif_wedding_effect(struct block_list *bl)
  7736. {
  7737. unsigned char buf[6];
  7738. nullpo_retv(bl);
  7739. WBUFW(buf,0) = 0x1ea;
  7740. WBUFL(buf,2) = bl->id;
  7741. clif_send(buf, packet_len(0x1ea), bl, AREA);
  7742. }
  7743. /// Notifies the client of the name of the partner character (ZC_COUPLENAME).
  7744. /// 01e6 <partner name>.24B
  7745. void clif_callpartner(map_session_data& sd)
  7746. {
  7747. PACKET_ZC_COUPLENAME p = { 0 };
  7748. p.packetType = HEADER_ZC_COUPLENAME;
  7749. if( sd.status.partner_id ) {
  7750. map_session_data *p_sd = pc_get_partner(&sd);
  7751. if (p_sd != nullptr && !p_sd->state.autotrade)
  7752. safestrncpy(p.name, p_sd->status.name, NAME_LENGTH);
  7753. else
  7754. p.name[0] = 0;
  7755. } else {// Send zero-length name if no partner, to initialize the client buffer.
  7756. p.name[0] = 0;
  7757. }
  7758. clif_send(&p, sizeof(p), &sd.bl, AREA);
  7759. }
  7760. /// Initiates the partner "taming" process [DracoRPG] (ZC_START_COUPLE).
  7761. /// 01e4
  7762. /// This packet while still implemented by the client is no longer being officially used.
  7763. /*
  7764. void clif_marriage_process(map_session_data *sd)
  7765. {
  7766. int fd;
  7767. nullpo_retv(sd);
  7768. fd=sd->fd;
  7769. WFIFOHEAD(fd,packet_len(0x1e4));
  7770. WFIFOW(fd,0)=0x1e4;
  7771. WFIFOSET(fd,packet_len(0x1e4));
  7772. }
  7773. */
  7774. /// Notice of divorce (ZC_DIVORCE).
  7775. /// 0205 <partner name>.24B
  7776. void clif_divorced(map_session_data* sd, const char* name)
  7777. {
  7778. int fd;
  7779. nullpo_retv(sd);
  7780. fd=sd->fd;
  7781. WFIFOHEAD(fd,packet_len(0x205));
  7782. WFIFOW(fd,0)=0x205;
  7783. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  7784. WFIFOSET(fd, packet_len(0x205));
  7785. }
  7786. /// Marriage proposal (ZC_REQ_COUPLE).
  7787. /// 01e2 <account id>.L <char id>.L <char name>.24B
  7788. /// This packet while still implemented by the client is no longer being officially used.
  7789. /*
  7790. void clif_marriage_proposal(int fd, map_session_data *sd, map_session_data* ssd)
  7791. {
  7792. nullpo_retv(sd);
  7793. WFIFOHEAD(fd,packet_len(0x1e2));
  7794. WFIFOW(fd,0) = 0x1e2;
  7795. WFIFOL(fd,2) = ssd->status.account_id;
  7796. WFIFOL(fd,6) = ssd->status.char_id;
  7797. safestrncpy(WFIFOCP(fd,10), ssd->status.name, NAME_LENGTH);
  7798. WFIFOSET(fd, packet_len(0x1e2));
  7799. }
  7800. */
  7801. /*==========================================
  7802. * Displays a message using the guild-chat colors to the specified targets. [Skotlex]
  7803. *------------------------------------------*/
  7804. void clif_disp_message(struct block_list* src, const char* mes, size_t len, enum send_target target)
  7805. {
  7806. unsigned char buf[256];
  7807. if( len == 0 ) {
  7808. return;
  7809. } else if( len > sizeof(buf)-5 ) {
  7810. ShowWarning("clif_disp_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", aid=%d).\n", mes, len, sizeof(buf)-5, src->id);
  7811. len = sizeof(buf)-5;
  7812. }
  7813. WBUFW(buf, 0) = 0x17f;
  7814. WBUFW(buf, 2) = static_cast<int16>( len + 5 );
  7815. safestrncpy(WBUFCP(buf,4), mes, len+1);
  7816. clif_send(buf, WBUFW(buf,2), src, target);
  7817. }
  7818. /// Notifies the client about the result of a request to disconnect another player (ZC_ACK_DISCONNECT_CHARACTER).
  7819. /// 00cd <result>.L (unknown packet version or invalid information at packet_len_table)
  7820. /// 00cd <result>.B
  7821. /// result:
  7822. /// 0 = failure
  7823. /// 1 = success
  7824. void clif_GM_kickack(map_session_data *sd, int id)
  7825. {
  7826. int fd;
  7827. nullpo_retv(sd);
  7828. fd = sd->fd;
  7829. WFIFOHEAD(fd,packet_len(0xcd));
  7830. WFIFOW(fd,0) = 0xcd;
  7831. WFIFOB(fd,2) = id; // FIXME: this is not account id
  7832. WFIFOSET(fd, packet_len(0xcd));
  7833. }
  7834. void clif_GM_kick(map_session_data *sd, map_session_data *tsd)
  7835. {
  7836. nullpo_retv(tsd);
  7837. if (sd == nullptr)
  7838. tsd->state.keepshop = true;
  7839. if (session_isActive(tsd->fd))
  7840. clif_authfail_fd(tsd->fd, 15);
  7841. else
  7842. map_quit(tsd);
  7843. if (sd)
  7844. clif_GM_kickack(sd, tsd->status.account_id);
  7845. }
  7846. /// Displays various manner-related status messages (ZC_ACK_GIVE_MANNER_POINT).
  7847. /// 014a <result>.L
  7848. /// result:
  7849. /// 0 = "A manner point has been successfully aligned."
  7850. /// 1 = MP_FAILURE_EXHAUST
  7851. /// 2 = MP_FAILURE_ALREADY_GIVING
  7852. /// 3 = "Chat Block has been applied by GM due to your ill-mannerous action."
  7853. /// 4 = "Automated Chat Block has been applied due to Anti-Spam System."
  7854. /// 5 = "You got a good point from %s."
  7855. void clif_manner_message(map_session_data* sd, uint32 type)
  7856. {
  7857. int fd;
  7858. nullpo_retv(sd);
  7859. fd = sd->fd;
  7860. WFIFOHEAD(fd,packet_len(0x14a));
  7861. WFIFOW(fd,0) = 0x14a;
  7862. WFIFOL(fd,2) = type;
  7863. WFIFOSET(fd, packet_len(0x14a));
  7864. }
  7865. /// Followup to 0x14a type 3/5, informs who did the manner adjustment action (ZC_NOTIFY_MANNER_POINT_GIVEN).
  7866. /// 014b <type>.B <GM name>.24B
  7867. /// type:
  7868. /// 0 = positive (unmute)
  7869. /// 1 = negative (mute)
  7870. void clif_GM_silence(map_session_data* sd, map_session_data* tsd, uint8 type)
  7871. {
  7872. int fd;
  7873. nullpo_retv(sd);
  7874. nullpo_retv(tsd);
  7875. fd = tsd->fd;
  7876. WFIFOHEAD(fd,packet_len(0x14b));
  7877. WFIFOW(fd,0) = 0x14b;
  7878. WFIFOB(fd,2) = type;
  7879. safestrncpy(WFIFOCP(fd,3), sd->status.name, NAME_LENGTH);
  7880. WFIFOSET(fd, packet_len(0x14b));
  7881. }
  7882. /// Notifies the client about the result of a request to allow/deny whispers from a player (ZC_SETTING_WHISPER_PC).
  7883. /// 00d1 <type>.B <result>.B
  7884. /// type:
  7885. /// 0 = /ex (deny)
  7886. /// 1 = /in (allow)
  7887. /// result:
  7888. /// 0 = success
  7889. /// 1 = failure
  7890. /// 2 = too many blocks
  7891. void clif_wisexin(map_session_data *sd,int type,int flag)
  7892. {
  7893. int fd;
  7894. nullpo_retv(sd);
  7895. fd=sd->fd;
  7896. WFIFOHEAD(fd,packet_len(0xd1));
  7897. WFIFOW(fd,0)=0xd1;
  7898. WFIFOB(fd,2)=type;
  7899. WFIFOB(fd,3)=flag;
  7900. WFIFOSET(fd,packet_len(0xd1));
  7901. }
  7902. /// Notifies the client about the result of a request to allow/deny whispers from anyone (ZC_SETTING_WHISPER_STATE).
  7903. /// 00d2 <type>.B <result>.B
  7904. /// type:
  7905. /// 0 = /exall (deny)
  7906. /// 1 = /inall (allow)
  7907. /// result:
  7908. /// 0 = success
  7909. /// 1 = failure
  7910. void clif_wisall(map_session_data *sd,int type,int flag)
  7911. {
  7912. int fd;
  7913. nullpo_retv(sd);
  7914. fd=sd->fd;
  7915. WFIFOHEAD(fd,packet_len(0xd2));
  7916. WFIFOW(fd,0)=0xd2;
  7917. WFIFOB(fd,2)=type;
  7918. WFIFOB(fd,3)=flag;
  7919. WFIFOSET(fd,packet_len(0xd2));
  7920. }
  7921. /// Play a BGM! [Rikter/Yommy] (ZC_PLAY_NPC_BGM).
  7922. /// 07fe <bgm>.24B
  7923. void clif_playBGM( map_session_data& sd, const char* name ){
  7924. #if PACKETVER_MAIN_NUM >= 20220504
  7925. PACKET_ZC_PLAY_NPC_BGM* p = reinterpret_cast<PACKET_ZC_PLAY_NPC_BGM*>( packet_buffer );
  7926. size_t length = strlen( name ) + 1;
  7927. p->PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7928. p->PacketLength = sizeof( *p );
  7929. p->playType = 0; // TODO: implement, send loop for the time being
  7930. safestrncpy( p->bgm, name, length );
  7931. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  7932. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7933. #elif PACKETVER >= 20091201
  7934. PACKET_ZC_PLAY_NPC_BGM p = {};
  7935. p.PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7936. safestrncpy( p.bgm, name, sizeof( p.bgm ) );
  7937. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7938. #endif
  7939. }
  7940. /// Plays/stops a wave sound (ZC_SOUND).
  7941. /// 01d3 <file name>.24B <act>.B <term>.L <npc id>.L
  7942. /// file name:
  7943. /// relative to data\wav
  7944. /// act:
  7945. /// 0 = play (once)
  7946. /// 1 = play (repeat, does not work)
  7947. /// 2 = stops all sound instances of file name (does not work)
  7948. /// term:
  7949. /// unknown purpose, only relevant to act = 1
  7950. /// npc id:
  7951. /// The accustic direction of the sound is determined by the
  7952. /// relative position of the NPC to the player (3D sound).
  7953. void clif_soundeffect( struct block_list& bl, const char* name, int type, enum send_target target ){
  7954. PACKET_ZC_SOUND p = {};
  7955. p.PacketType = HEADER_ZC_SOUND;
  7956. safestrncpy( p.name, name, sizeof( p.name ) );
  7957. p.act = type;
  7958. p.term = 0; // TODO: implement
  7959. p.AID = bl.id;
  7960. clif_send( &p, sizeof( p ), &bl, target );
  7961. }
  7962. /// Displays special effects (npcs, weather, etc) [Valaris] (ZC_NOTIFY_EFFECT2).
  7963. /// 01f3 <id>.L <effect id>.L
  7964. /// effect id:
  7965. /// @see doc/effect_list.txt
  7966. void clif_specialeffect(struct block_list* bl, int type, enum send_target target)
  7967. {
  7968. unsigned char buf[24];
  7969. nullpo_retv(bl);
  7970. memset(buf, 0, packet_len(0x1f3));
  7971. WBUFW(buf,0) = 0x1f3;
  7972. WBUFL(buf,2) = bl->id;
  7973. WBUFL(buf,6) = type;
  7974. clif_send(buf, packet_len(0x1f3), bl, target);
  7975. if (disguised(bl)) {
  7976. WBUFL(buf,2) = disguised_bl_id(bl->id);
  7977. clif_send(buf, packet_len(0x1f3), bl, SELF);
  7978. }
  7979. }
  7980. void clif_specialeffect_single(struct block_list* bl, int type, int fd)
  7981. {
  7982. WFIFOHEAD(fd,10);
  7983. WFIFOW(fd,0) = 0x1f3;
  7984. WFIFOL(fd,2) = bl->id;
  7985. WFIFOL(fd,6) = type;
  7986. WFIFOSET(fd,10);
  7987. }
  7988. /// Notifies clients of an special/visual effect that accepts an value (ZC_NOTIFY_EFFECT3).
  7989. /// 0284 <id>.L <effect id>.L <num data>.L
  7990. /// effect id:
  7991. /// @see doc/effect_list.txt
  7992. /// num data:
  7993. /// effect-dependent value
  7994. void clif_specialeffect_value(struct block_list* bl, int effect_id, int num, send_target target)
  7995. {
  7996. uint8 buf[14];
  7997. WBUFW(buf,0) = 0x284;
  7998. WBUFL(buf,2) = bl->id;
  7999. WBUFL(buf,6) = effect_id;
  8000. WBUFL(buf,10) = num;
  8001. clif_send(buf, packet_len(0x284), bl, target);
  8002. if( disguised(bl) )
  8003. {
  8004. WBUFL(buf,2) = disguised_bl_id(bl->id);
  8005. clif_send(buf, packet_len(0x284), bl, SELF);
  8006. }
  8007. }
  8008. void clif_specialeffect_remove(struct block_list* bl_src, int effect, enum send_target e_target, struct block_list* bl_target)
  8009. {
  8010. #if PACKETVER >= 20181002
  8011. nullpo_retv( bl_src );
  8012. nullpo_retv( bl_target );
  8013. PACKET_ZC_REMOVE_EFFECT p = {};
  8014. p.packetType = HEADER_ZC_REMOVE_EFFECT;
  8015. p.aid = bl_src->id;
  8016. p.effectId = effect;
  8017. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_target, e_target );
  8018. if( disguised(bl_src) )
  8019. {
  8020. p.aid = disguised_bl_id( bl_src->id );
  8021. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_src, SELF );
  8022. }
  8023. #endif
  8024. }
  8025. /// Monster/NPC color chat [SnakeDrak] (ZC_NPC_CHAT).
  8026. /// 02c1 <packet len>.W <id>.L <color>.L <message>.?B
  8027. void clif_messagecolor_target(struct block_list *bl, unsigned long color, const char *msg, bool rgb2bgr, enum send_target type, map_session_data *sd) {
  8028. unsigned short msg_len = (unsigned short)(strlen(msg) + 1);
  8029. uint8 buf[CHAT_SIZE_MAX];
  8030. nullpo_retv(bl);
  8031. if (msg_len > sizeof(buf) - 12) {
  8032. ShowWarning("clif_messagecolor: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  8033. msg_len = sizeof(buf) - 12;
  8034. }
  8035. if (rgb2bgr)
  8036. color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16; // RGB to BGR
  8037. WBUFW(buf,0) = 0x2C1;
  8038. WBUFW(buf,2) = msg_len + 12;
  8039. WBUFL(buf,4) = bl->id;
  8040. WBUFL(buf,8) = color;
  8041. memcpy(WBUFCP(buf,12), msg, msg_len);
  8042. clif_send(buf, WBUFW(buf,2), (sd == nullptr ? bl : &(sd->bl)), type);
  8043. }
  8044. /**
  8045. * Notifies the client that the storage window is still open
  8046. *
  8047. * Should only be used in cases where the client closed the
  8048. * storage window without server's consent
  8049. */
  8050. void clif_refresh_storagewindow(map_session_data *sd) {
  8051. // Notify the client that the storage is open
  8052. if( sd->state.storage_flag == 1 ) {
  8053. storage_sortitem(sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage));
  8054. clif_storagelist(sd, sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage), storage_getName(0));
  8055. clif_updatestorageamount(*sd, sd->storage.amount, sd->storage.max_amount);
  8056. }
  8057. // Notify the client that the gstorage is open otherwise it will
  8058. // remain locked forever and nobody will be able to access it
  8059. if( sd->state.storage_flag == 2 ) {
  8060. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  8061. if( !gstor ) // Shouldn't happen. The information should already be at the map-server
  8062. intif_request_guild_storage(sd->status.account_id, sd->status.guild_id);
  8063. else {
  8064. storage_sortitem(gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild));
  8065. clif_storagelist(sd, gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild), "Guild Storage");
  8066. clif_updatestorageamount(*sd, gstor->amount, gstor->max_amount);
  8067. }
  8068. }
  8069. // Notify the client that the premium storage is open
  8070. if (sd->state.storage_flag == 3) {
  8071. storage_sortitem(sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage));
  8072. clif_storagelist(sd, sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage), storage_getName(sd->premiumStorage.stor_id));
  8073. clif_updatestorageamount(*sd, sd->premiumStorage.amount, sd->premiumStorage.max_amount);
  8074. }
  8075. }
  8076. // refresh the client's screen, getting rid of any effects
  8077. void clif_refresh(map_session_data *sd)
  8078. {
  8079. nullpo_retv(sd);
  8080. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8081. clif_inventorylist(sd);
  8082. clif_equipswitch_list(sd);
  8083. if(pc_iscarton(sd)) {
  8084. clif_cartlist(sd);
  8085. clif_updatestatus(*sd,SP_CARTINFO);
  8086. }
  8087. clif_updatestatus(*sd,SP_WEIGHT);
  8088. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8089. clif_updatestatus(*sd,SP_STR);
  8090. clif_updatestatus(*sd,SP_AGI);
  8091. clif_updatestatus(*sd,SP_VIT);
  8092. clif_updatestatus(*sd,SP_INT);
  8093. clif_updatestatus(*sd,SP_DEX);
  8094. clif_updatestatus(*sd,SP_LUK);
  8095. #ifdef RENEWAL
  8096. clif_updatestatus(*sd,SP_POW);
  8097. clif_updatestatus(*sd,SP_STA);
  8098. clif_updatestatus(*sd,SP_WIS);
  8099. clif_updatestatus(*sd,SP_SPL);
  8100. clif_updatestatus(*sd,SP_CON);
  8101. clif_updatestatus(*sd,SP_CRT);
  8102. #endif
  8103. if (sd->spiritball)
  8104. clif_spiritball( &sd->bl, &sd->bl, SELF );
  8105. clif_millenniumshield_single( *sd, *sd );
  8106. clif_spiritcharm_single( *sd, *sd );
  8107. if (sd->soulball)
  8108. clif_soulball( sd, &sd->bl, SELF );
  8109. if (sd->servantball)
  8110. clif_servantball( *sd, &sd->bl, SELF );
  8111. if (sd->abyssball)
  8112. clif_abyssball( *sd, &sd->bl, SELF );
  8113. if (sd->vd.cloth_color)
  8114. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8115. if (sd->vd.body_style)
  8116. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8117. if(hom_is_active(sd->hd))
  8118. clif_send_homdata( *sd->hd, SP_ACK );
  8119. if( sd->md ) {
  8120. clif_mercenary_info(sd);
  8121. clif_mercenary_skillblock(sd);
  8122. }
  8123. if( sd->ed )
  8124. clif_elemental_info(sd);
  8125. map_foreachinallrange(clif_getareachar,&sd->bl,AREA_SIZE,BL_ALL,sd);
  8126. clif_weather_check(sd);
  8127. if( sd->chatID )
  8128. chat_leavechat(sd,0);
  8129. if( sd->state.vending )
  8130. clif_openvending( *sd );
  8131. if( pc_issit(sd) )
  8132. clif_sitting(sd->bl); // FIXME: just send to self, not area
  8133. if( pc_isdead(sd) ) // When you refresh, resend the death packet.
  8134. clif_clearunit_single( sd->bl.id, CLR_DEAD, *sd );
  8135. else
  8136. clif_changed_dir(sd->bl, SELF);
  8137. clif_efst_status_change_sub(&sd->bl,&sd->bl,SELF);
  8138. //Issue #2143
  8139. //Cancel Trading State
  8140. if (sd->state.trading)
  8141. trade_tradecancel(sd);
  8142. //Cancel Buying/Selling State
  8143. if (sd->state.buyingstore)
  8144. buyingstore_close(sd);
  8145. mail_clear(sd);
  8146. clif_loadConfirm( sd );
  8147. if( disguised(&sd->bl) ) {/* refresh-da */
  8148. short disguise = sd->disguise;
  8149. pc_disguise(sd, 0);
  8150. pc_disguise(sd, disguise);
  8151. }
  8152. clif_refresh_storagewindow(sd);
  8153. }
  8154. /// Updates the object's (bl) name on client.
  8155. /// Used to update when a char leaves a party/guild. [Skotlex]
  8156. /// Needed because when you send a 0x95 packet, the client will not remove the cached party/guild info that is not sent.
  8157. /// 0095 <id>.L <char name>.24B (ZC_ACK_REQNAME)
  8158. /// 0195 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B (ZC_ACK_REQNAMEALL)
  8159. /// 0a30 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B <title ID>.L (ZC_ACK_REQNAMEALL2)
  8160. void clif_name( struct block_list* src, struct block_list *bl, send_target target ){
  8161. nullpo_retv( src );
  8162. nullpo_retv( bl );
  8163. switch( bl->type ){
  8164. case BL_PC: {
  8165. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8166. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8167. packet.gid = bl->id;
  8168. map_session_data *sd = (map_session_data *)bl;
  8169. //Requesting your own "shadow" name. [Skotlex]
  8170. if( src == bl && target == SELF && sd->disguise ){
  8171. packet.gid = -bl->id;
  8172. }
  8173. if( sd->fakename[0] ) {
  8174. safestrncpy( packet.name, sd->fakename, NAME_LENGTH );
  8175. clif_send( &packet, sizeof(packet), src, target );
  8176. return;
  8177. }
  8178. safestrncpy( packet.name, sd->status.name, NAME_LENGTH );
  8179. party_data *p = nullptr;
  8180. if( sd->status.party_id ){
  8181. p = party_search( sd->status.party_id );
  8182. }
  8183. // do not display party unless the player is also in a guild
  8184. if( p && ( sd->guild || battle_config.display_party_name ) ){
  8185. safestrncpy( packet.party_name, p->party.name, NAME_LENGTH );
  8186. }
  8187. if( sd->guild ){
  8188. int position;
  8189. // Will get the position of the guild the player is in
  8190. position = guild_getposition(*sd);
  8191. safestrncpy( packet.guild_name, sd->guild->guild.name, NAME_LENGTH );
  8192. safestrncpy( packet.position_name, sd->guild->guild.position[position].name, NAME_LENGTH );
  8193. }else if( sd->clan ){
  8194. safestrncpy( packet.position_name, sd->clan->name, NAME_LENGTH );
  8195. }
  8196. #if PACKETVER_MAIN_NUM >= 20150225 || PACKETVER_RE_NUM >= 20141126 || defined( PACKETVER_ZERO )
  8197. packet.title_id = sd->status.title_id; // Title ID
  8198. #endif
  8199. clif_send(&packet, sizeof(packet), src, target);
  8200. }
  8201. break;
  8202. //[blackhole89]
  8203. case BL_HOM:
  8204. case BL_MER:
  8205. case BL_PET:
  8206. case BL_NPC:
  8207. case BL_ELEM: {
  8208. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8209. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8210. packet.gid = bl->id;
  8211. switch (bl->type) {
  8212. case BL_HOM:
  8213. memcpy(packet.name, ((TBL_HOM *)bl)->homunculus.name, NAME_LENGTH);
  8214. break;
  8215. case BL_MER:
  8216. memcpy(packet.name, ((TBL_MER *)bl)->db->name.c_str(), NAME_LENGTH);
  8217. break;
  8218. case BL_PET:
  8219. safestrncpy(packet.name, ((TBL_PET *)bl)->pet.name, NAME_LENGTH);
  8220. break;
  8221. case BL_NPC:
  8222. safestrncpy(packet.name, ((TBL_NPC *)bl)->name, NAME_LENGTH);
  8223. break;
  8224. case BL_ELEM:
  8225. safestrncpy(packet.name, ((TBL_ELEM *)bl)->db->name.c_str(), NAME_LENGTH);
  8226. break;
  8227. }
  8228. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8229. unit_data *ud = unit_bl2ud(bl);
  8230. if (ud != nullptr) {
  8231. memcpy(packet.title, ud->title, NAME_LENGTH);
  8232. packet.groupId = ud->group_id;
  8233. }
  8234. #endif
  8235. clif_send(&packet, sizeof(packet), src, target);
  8236. }
  8237. break;
  8238. case BL_MOB: {
  8239. mob_data *md = (mob_data *)bl;
  8240. if( md->guardian_data && md->guardian_data->guild_id ){
  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. safestrncpy( packet.guild_name, md->guardian_data->guild_name, NAME_LENGTH );
  8246. safestrncpy( packet.position_name, md->guardian_data->castle->castle_name, NAME_LENGTH );
  8247. clif_send(&packet, sizeof(packet), src, target);
  8248. }else if( battle_config.show_mob_info ){
  8249. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8250. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8251. packet.gid = bl->id;
  8252. safestrncpy( packet.name, md->name, NAME_LENGTH );
  8253. char mobhp[50], *str_p = mobhp;
  8254. if( battle_config.show_mob_info&4 ){
  8255. str_p += sprintf( str_p, "Lv. %d | ", md->level );
  8256. }
  8257. if( battle_config.show_mob_info&1 ){
  8258. str_p += sprintf( str_p, "HP: %u/%u | ", md->status.hp, md->status.max_hp );
  8259. }
  8260. if( battle_config.show_mob_info&2 ){
  8261. str_p += sprintf( str_p, "HP: %u%% | ", get_percentage( md->status.hp, md->status.max_hp ) );
  8262. }
  8263. // Even thought mobhp ain't a name, we send it as one so the client can parse it. [Skotlex]
  8264. if( str_p != mobhp ){
  8265. *(str_p-3) = '\0'; //Remove trailing space + pipe.
  8266. safestrncpy( packet.party_name, mobhp, NAME_LENGTH );
  8267. }
  8268. clif_send(&packet, sizeof(packet), src, target);
  8269. } else {
  8270. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8271. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8272. packet.gid = bl->id;
  8273. safestrncpy(packet.name, md->name, NAME_LENGTH);
  8274. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8275. unit_data *ud = unit_bl2ud(bl);
  8276. if (ud != nullptr) {
  8277. memcpy(packet.title, ud->title, NAME_LENGTH);
  8278. packet.groupId = ud->group_id;
  8279. }
  8280. #endif
  8281. clif_send(&packet, sizeof(packet), src, target);
  8282. }
  8283. }
  8284. break;
  8285. case BL_CHAT:
  8286. case BL_SKILL:
  8287. // Newer clients request this, but do not need an answer
  8288. return;
  8289. default:
  8290. ShowError("clif_name: bad type %d(%d)\n", bl->type, bl->id);
  8291. return;
  8292. }
  8293. }
  8294. /// Taekwon Jump (TK_HIGHJUMP) effect (ZC_HIGHJUMP).
  8295. /// 01ff <id>.L <x>.W <y>.W
  8296. ///
  8297. /// Visually moves(instant) a character to x,y. The char moves even
  8298. /// when the target cell isn't walkable. If the char is sitting it
  8299. /// stays that way.
  8300. void clif_slide(block_list& bl, int x, int y){
  8301. PACKET_ZC_HIGHJUMP p{};
  8302. p.packetType = HEADER_ZC_HIGHJUMP;
  8303. p.srcId = bl.id;
  8304. p.x = x;
  8305. p.y = y;
  8306. clif_send(&p, sizeof(p), &bl, AREA);
  8307. if( disguised(&bl) )
  8308. {
  8309. p.srcId = disguised_bl_id(bl.id);
  8310. clif_send(&p, sizeof(p), &bl, SELF);
  8311. }
  8312. }
  8313. /// Public chat message (ZC_NOTIFY_CHAT). lordalfa/Skotlex - used by @me as well
  8314. /// 008d <packet len>.W <id>.L <message>.?B
  8315. void clif_disp_overhead_(struct block_list *bl, const char* mes, enum send_target flag)
  8316. {
  8317. unsigned char buf[256]; //This should be more than sufficient, the theorical max is CHAT_SIZE + 8 (pads and extra inserted crap)
  8318. int16 len_mes;
  8319. len_mes = static_cast<decltype(len_mes)>( strlen(mes) + 1 ); //Account for \0
  8320. if (len_mes > sizeof(buf)-8) {
  8321. ShowError("clif_disp_overhead: Message too long (length %d)\n", len_mes);
  8322. len_mes = sizeof(buf)-8; //Trunk it to avoid problems.
  8323. }
  8324. // send message to others
  8325. if (flag == AREA) {
  8326. WBUFW(buf,0) = 0x8d;
  8327. WBUFW(buf,2) = len_mes + 8; // len of message + 8 (command+len+id)
  8328. WBUFL(buf,4) = bl->id;
  8329. safestrncpy(WBUFCP(buf,8), mes, len_mes);
  8330. clif_send(buf, WBUFW(buf,2), bl, AREA_CHAT_WOC);
  8331. }
  8332. // send back message to the speaker
  8333. if( bl->type == BL_PC ) {
  8334. WBUFW(buf,0) = 0x8e;
  8335. WBUFW(buf, 2) = len_mes + 4;
  8336. safestrncpy(WBUFCP(buf,4), mes, len_mes);
  8337. clif_send(buf, WBUFW(buf,2), bl, SELF);
  8338. }
  8339. }
  8340. /*==========================
  8341. * Minimap fix [Kevin]
  8342. * Remove dot from minimap
  8343. *--------------------------*/
  8344. void clif_party_xy_remove(map_session_data* sd)
  8345. {
  8346. nullpo_retv(sd);
  8347. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p{};
  8348. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  8349. p.AID = sd->status.account_id;
  8350. p.xPos = -1;
  8351. p.yPos = -1;
  8352. clif_send(&p, sizeof(p), &sd->bl, PARTY_SAMEMAP_WOS);
  8353. }
  8354. /// Displays a skill message (thanks to Rayce) (ZC_SKILLMSG).
  8355. /// 0215 <msg id>.L
  8356. /// msg id:
  8357. /// 0x15 = End all negative status (PA_GOSPEL)
  8358. /// 0x16 = Immunity to all status (PA_GOSPEL)
  8359. /// 0x17 = MaxHP +100% (PA_GOSPEL)
  8360. /// 0x18 = MaxSP +100% (PA_GOSPEL)
  8361. /// 0x19 = All stats +20 (PA_GOSPEL)
  8362. /// 0x1c = Enchant weapon with Holy element (PA_GOSPEL)
  8363. /// 0x1d = Enchant armor with Holy element (PA_GOSPEL)
  8364. /// 0x1e = DEF +25% (PA_GOSPEL)
  8365. /// 0x1f = ATK +100% (PA_GOSPEL)
  8366. /// 0x20 = HIT/Flee +50 (PA_GOSPEL)
  8367. /// 0x28 = Full strip failed because of coating (ST_FULLSTRIP)
  8368. /// ? = nothing
  8369. void clif_gospel_info(map_session_data *sd, int type)
  8370. {
  8371. int fd=sd->fd;
  8372. WFIFOHEAD(fd,packet_len(0x215));
  8373. WFIFOW(fd,0)=0x215;
  8374. WFIFOL(fd,2)=type;
  8375. WFIFOSET(fd, packet_len(0x215));
  8376. }
  8377. /// Multi-purpose mission information packet (ZC_STARSKILL).
  8378. /// 020e <mapname>.24B <monster_id>.L <star>.B <result>.B
  8379. /// result:
  8380. /// 0 = Star Gladiator %s has designed <mapname>'s as the %s.
  8381. /// star:
  8382. /// 0 = Place of the Sun
  8383. /// 1 = Place of the Moon
  8384. /// 2 = Place of the Stars
  8385. /// 1 = Star Gladiator %s's %s: <mapname>
  8386. /// star:
  8387. /// 0 = Place of the Sun
  8388. /// 1 = Place of the Moon
  8389. /// 2 = Place of the Stars
  8390. /// 10 = Star Gladiator %s has designed <mapname>'s as the %s.
  8391. /// star:
  8392. /// 0 = Target of the Sun
  8393. /// 1 = Target of the Moon
  8394. /// 2 = Target of the Stars
  8395. /// 11 = Star Gladiator %s's %s: <mapname used as monster name>
  8396. /// star:
  8397. /// 0 = Monster of the Sun
  8398. /// 1 = Monster of the Moon
  8399. /// 2 = Monster of the Stars
  8400. /// 20 = [TaeKwon Mission] Target Monster : <mapname used as monster name> (<star>%)
  8401. /// 21 = [Taming Mission] Target Monster : <mapname used as monster name>
  8402. /// 22 = [Collector Rank] Target Item : <monster_id used as item id>
  8403. /// 30 = [Sun, Moon and Stars Angel] Designed places and monsters have been reset.
  8404. /// 40 = Target HP : <monster_id used as HP>
  8405. void clif_starskill(map_session_data* sd, const char* mapname, int monster_id, unsigned char star, unsigned char result)
  8406. {
  8407. int fd = sd->fd;
  8408. WFIFOHEAD(fd,packet_len(0x20e));
  8409. WFIFOW(fd,0) = 0x20e;
  8410. safestrncpy(WFIFOCP(fd,2), mapname, NAME_LENGTH);
  8411. WFIFOL(fd,26) = monster_id;
  8412. WFIFOB(fd,30) = star;
  8413. WFIFOB(fd,31) = result;
  8414. WFIFOSET(fd,packet_len(0x20e));
  8415. }
  8416. /*==========================================
  8417. * Info about Star Glaldiator save map [Komurka]
  8418. * type: 1: Information, 0: Map registered
  8419. *------------------------------------------*/
  8420. void clif_feel_info(map_session_data* sd, unsigned char feel_level, unsigned char type)
  8421. {
  8422. char mapname[MAP_NAME_LENGTH_EXT];
  8423. mapindex_getmapname_ext(mapindex_id2name(sd->feel_map[feel_level].index), mapname);
  8424. clif_starskill(sd, mapname, 0, feel_level, type ? 1 : 0);
  8425. }
  8426. /*==========================================
  8427. * Info about Star Glaldiator hate mob [Komurka]
  8428. * type: 1: Register mob, 0: Information.
  8429. *------------------------------------------*/
  8430. void clif_hate_info(map_session_data *sd, unsigned char hate_level,int class_, unsigned char type)
  8431. {
  8432. if( pcdb_checkid(class_) ) {
  8433. clif_starskill(sd, job_name(class_), class_, hate_level, type ? 10 : 11);
  8434. } else if( mobdb_checkid(class_) ) {
  8435. clif_starskill(sd, mob_db.find(class_)->jname.c_str(), class_, hate_level, type ? 10 : 11);
  8436. } else {
  8437. 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);
  8438. }
  8439. }
  8440. /*==========================================
  8441. * Info about TaeKwon Do TK_MISSION mob [Skotlex]
  8442. *------------------------------------------*/
  8443. void clif_mission_info(map_session_data *sd, int mob_id, unsigned char progress)
  8444. {
  8445. clif_starskill(sd, mob_db.find(mob_id)->jname.c_str(), mob_id, progress, 20);
  8446. }
  8447. /*==========================================
  8448. * Feel/Hate reset (thanks to Rayce) [Skotlex]
  8449. *------------------------------------------*/
  8450. void clif_feel_hate_reset(map_session_data *sd)
  8451. {
  8452. clif_starskill(sd, "", 0, 0, 30);
  8453. }
  8454. /// Send out reply to configuration change
  8455. /// 02d9 <type>.L <value>.L (ZC_CONFIG)
  8456. /// type: see enum e_config_type
  8457. /// value:
  8458. /// false = disabled
  8459. /// true = enabled
  8460. void clif_configuration( map_session_data* sd, enum e_config_type type, bool enabled ){
  8461. int fd;
  8462. nullpo_retv(sd);
  8463. fd = sd->fd;
  8464. WFIFOHEAD(fd, packet_len(0x2d9));
  8465. WFIFOW(fd, 0) = 0x2d9;
  8466. WFIFOL(fd, 2) = type;
  8467. WFIFOL(fd, 6) = enabled;
  8468. WFIFOSET(fd, packet_len(0x2d9));
  8469. }
  8470. /// The player's 'view equip' state, sent during login (ZC_CONFIG_NOTIFY).
  8471. /// 02da <open equip window>.B
  8472. /// open equip window:
  8473. /// 0 = disabled
  8474. /// 1 = enabled
  8475. void clif_equipcheckbox(map_session_data* sd)
  8476. {
  8477. int fd;
  8478. nullpo_retv(sd);
  8479. fd = sd->fd;
  8480. WFIFOHEAD(fd, packet_len(0x2da));
  8481. WFIFOW(fd, 0) = 0x2da;
  8482. WFIFOB(fd, 2) = (sd->status.show_equip ? 1 : 0);
  8483. WFIFOSET(fd, packet_len(0x2da));
  8484. }
  8485. /// Sends info about a player's equipped items.
  8486. /// 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)
  8487. /// 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)
  8488. /// 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)
  8489. /// 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)
  8490. /// 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)
  8491. /// 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)
  8492. void clif_viewequip_ack( map_session_data& sd, map_session_data& tsd ){
  8493. #if PACKETVER_AD_NUM >= 20071211 || PACKETVER_SAK_NUM >= 20071127 || PACKETVER_MAIN_NUM >= 20071211 || defined(PACKETVER_RE)
  8494. PACKET_ZC_EQUIPWIN_MICROSCOPE* p = reinterpret_cast<PACKET_ZC_EQUIPWIN_MICROSCOPE*>( packet_buffer );
  8495. p->PacketType = HEADER_ZC_EQUIPWIN_MICROSCOPE;
  8496. p->PacketLength = sizeof( *p );
  8497. safestrncpy( p->characterName, tsd.status.name, NAME_LENGTH );
  8498. p->job = tsd.status.class_;
  8499. p->head = tsd.vd.hair_style;
  8500. p->accessory = tsd.vd.head_bottom;
  8501. p->accessory2 = tsd.vd.head_mid;
  8502. p->accessory3 = tsd.vd.head_top;
  8503. #if PACKETVER >= 20110111
  8504. p->robe = tsd.vd.robe;
  8505. #endif
  8506. p->headpalette = tsd.vd.hair_color;
  8507. p->bodypalette = tsd.vd.cloth_color;
  8508. #if PACKETVER_MAIN_NUM >= 20180801 || PACKETVER_RE_NUM >= 20180801 || PACKETVER_ZERO_NUM >= 20180808
  8509. p->body2 = tsd.vd.body_style;
  8510. #endif
  8511. p->sex = tsd.vd.sex;
  8512. for( int i = 0, equip = 0; i < EQI_MAX; i++ ){
  8513. int k = tsd.equip_index[i];
  8514. if( k >= 0 ){
  8515. if( tsd.inventory.u.items_inventory[k].nameid == 0 || tsd.inventory_data[k] == nullptr ){ // Item doesn't exist
  8516. continue;
  8517. }
  8518. if( !tsd.inventory.u.items_inventory[k].equip ){
  8519. continue;
  8520. }
  8521. if( !itemdb_isequip2( tsd.inventory_data[k] ) ){ // Is not equippable
  8522. continue;
  8523. }
  8524. clif_item_equip( client_index( k ), &p->list[equip], &tsd.inventory.u.items_inventory[k], tsd.inventory_data[k], pc_equippoint( &tsd, k ) );
  8525. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->list[0] ) );
  8526. equip++;
  8527. }
  8528. }
  8529. clif_send( p, p->PacketLength, &sd.bl, SELF );
  8530. #endif
  8531. }
  8532. /// Display msgstringtable.txt string (ZC_MSG).
  8533. /// 0291 <message>.W
  8534. void clif_msg(map_session_data* sd, unsigned short id)
  8535. {
  8536. int fd;
  8537. nullpo_retv(sd);
  8538. fd = sd->fd;
  8539. WFIFOHEAD(fd, packet_len(0x291));
  8540. WFIFOW(fd, 0) = 0x291;
  8541. WFIFOW(fd, 2) = id; // zero-based msgstringtable.txt index
  8542. WFIFOSET(fd, packet_len(0x291));
  8543. }
  8544. /// Display msgstringtable.txt string and fill in a valid for %d format (ZC_MSG_VALUE).
  8545. /// 0x7e2 <message>.W <value>.L
  8546. void clif_msg_value(map_session_data* sd, unsigned short id, int value)
  8547. {
  8548. int fd = sd->fd;
  8549. WFIFOHEAD(fd, packet_len(0x7e2));
  8550. WFIFOW(fd,0) = 0x7e2;
  8551. WFIFOW(fd,2) = id;
  8552. WFIFOL(fd,4) = value;
  8553. WFIFOSET(fd, packet_len(0x7e2));
  8554. }
  8555. /// Displays msgstringtable.txt string, prefixed with a skill name. (ZC_MSG_SKILL).
  8556. /// 07e6 <skill id>.W <msg id>.L
  8557. ///
  8558. /// NOTE: Message has following format and is printed in color 0xCDCDFF (purple):
  8559. /// "[SkillName] Message"
  8560. void clif_msg_skill(map_session_data* sd, uint16 skill_id, int msg_id)
  8561. {
  8562. int fd = sd->fd;
  8563. WFIFOHEAD(fd, packet_len(0x7e6));
  8564. WFIFOW(fd,0) = 0x7e6;
  8565. WFIFOW(fd,2) = skill_id;
  8566. WFIFOL(fd,4) = msg_id;
  8567. WFIFOSET(fd, packet_len(0x7e6));
  8568. }
  8569. /// Displays msgstringtable.txt string in a color. (ZC_MSG_COLOR).
  8570. /// 09cd <msg id>.W <color>.L
  8571. void clif_msg_color( map_session_data *sd, uint16 msg_id, uint32 color ){
  8572. nullpo_retv(sd);
  8573. int fd = sd->fd;
  8574. WFIFOHEAD(fd, packet_len(0x9cd));
  8575. WFIFOW(fd, 0) = 0x9cd;
  8576. WFIFOW(fd, 2) = msg_id;
  8577. WFIFOL(fd, 4) = color;
  8578. WFIFOSET(fd, packet_len(0x9cd));
  8579. }
  8580. /// Validates one global/guild/party/whisper message packet and tries to recognize its components.
  8581. /// Returns true if the packet was parsed successfully.
  8582. /// Formats: false - <packet id>.w <packet len>.w (<name> : <message>).?B 00
  8583. /// true - <packet id>.w <packet len>.w <name>.24B <message>.?B 00
  8584. static bool clif_process_message(map_session_data* sd, bool whisperFormat, char* out_name, char* out_message, char* out_full_message ){
  8585. const char* seperator = " : ";
  8586. int fd;
  8587. struct s_packet_db* info;
  8588. uint16 packetLength, inputLength;
  8589. const char *input, *name, *message;
  8590. size_t nameLength, messageLength;
  8591. fd = sd->fd;
  8592. info = &packet_db[RFIFOW(fd,0)];
  8593. packetLength = RFIFOW(fd,info->pos[0]);
  8594. input = RFIFOCP(fd,info->pos[1]);
  8595. // basic structure check for the 4-byte header
  8596. if( packetLength < 4 ){
  8597. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no message data)!\n", sd->status.name);
  8598. return false;
  8599. }else{
  8600. inputLength = packetLength - 4;
  8601. }
  8602. // process <name> part of the packet
  8603. if( whisperFormat ) {
  8604. // name has fixed width
  8605. if( inputLength < NAME_LENGTH + 1 ) {
  8606. ShowWarning("clif_process_message: Received malformed packet from player '%s' (packet length is incorrect)!\n", sd->status.name);
  8607. return false;
  8608. }
  8609. name = input;
  8610. // validate name
  8611. nameLength = strnlen( name, NAME_LENGTH - 1 );
  8612. // only restriction is that the name must be zero-terminated
  8613. if( name[nameLength] != '\0' ) {
  8614. ShowWarning("clif_process_message: Player '%s' sent an unterminated name!\n", sd->status.name);
  8615. return false;
  8616. }
  8617. message = input + NAME_LENGTH;
  8618. messageLength = inputLength - NAME_LENGTH;
  8619. }else{
  8620. // name and message are separated by ' : '
  8621. size_t seperatorLength = strnlen( seperator, NAME_LENGTH );
  8622. nameLength = strnlen( sd->status.name, NAME_LENGTH - 1 ); // name length (w/o zero byte)
  8623. // check if there's enough data provided
  8624. if( inputLength < nameLength + seperatorLength + 1 ){
  8625. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no username data)!\n", sd->status.name);
  8626. return false;
  8627. }
  8628. name = input;
  8629. // validate name
  8630. if( strncmp( name, sd->status.name, nameLength ) || // the text must start with the speaker's name
  8631. strncmp( name + nameLength, seperator, seperatorLength ) ) // followed by the seperator
  8632. {
  8633. //Hacked message, or infamous "client desynch" issue where they pick one char while loading another.
  8634. ShowWarning("clif_process_message: Player '%s' sent a message using an incorrect name! Forcing a relog...\n", sd->status.name);
  8635. set_eof(sd->fd); // Just kick them out to correct it.
  8636. return false;
  8637. }
  8638. message = input + nameLength + seperatorLength;
  8639. messageLength = inputLength - nameLength - seperatorLength;
  8640. }
  8641. #if PACKETVER < 20151001
  8642. // the declared length must match real length
  8643. if( messageLength != strnlen(message, messageLength)+1 ) {
  8644. ShowWarning("clif_process_message: Received malformed packet from player '%s' (length is incorrect)!\n", sd->status.name);
  8645. return false;
  8646. }
  8647. // verify <message> part of the packet
  8648. if( message[messageLength-1] != '\0' ) { // message must be zero-terminated
  8649. ShowWarning("clif_process_message: Player '%s' sent an unterminated message string!\n", sd->status.name);
  8650. return false;
  8651. }
  8652. #else
  8653. // No zero termination anymore
  8654. messageLength += 1;
  8655. #endif
  8656. // messages mustn't be too long
  8657. if( messageLength > CHAT_SIZE_MAX-1 ) {
  8658. // Normally you can only enter CHATBOX_SIZE-1 letters into the chat box, but Frost Joke / Dazzler's text can be longer.
  8659. // Also, the physical size of strings that use multibyte encoding can go multiple times over the chatbox capacity.
  8660. // Neither the official client nor server place any restriction on the length of the data in the packet,
  8661. // but we'll only allow reasonably long strings here. This also makes sure that they fit into the `chatlog` table.
  8662. ShowWarning("clif_process_message: Player '%s' sent a message too long ('%.*s')!\n", sd->status.name, CHAT_SIZE_MAX-1, message);
  8663. return false;
  8664. }
  8665. // If it is not a whisper message, set the name to the fakename of the player
  8666. if( whisperFormat == false && sd->fakename[0] != '\0' ){
  8667. strcpy( out_name, sd->fakename );
  8668. }else{
  8669. safestrncpy( out_name, name, nameLength + 1 );
  8670. }
  8671. safestrncpy( out_message, message, messageLength );
  8672. if( whisperFormat ){
  8673. sprintf( out_full_message, "%-24s%s", out_name, out_message );
  8674. out_full_message[nameLength] = '\0';
  8675. }else{
  8676. sprintf( out_full_message, "%s%s%s", out_name, seperator, out_message );
  8677. }
  8678. if( is_atcommand( fd, sd, out_message, 1 ) )
  8679. return false;
  8680. if (sd->sc.cant.chat || (sd->state.block_action & PCBLOCK_CHAT))
  8681. return false; //no "chatting" while muted.
  8682. if( battle_config.min_chat_delay ) { //[Skotlex]
  8683. if (DIFF_TICK(sd->cantalk_tick, gettick()) > 0)
  8684. return false;
  8685. sd->cantalk_tick = gettick() + battle_config.min_chat_delay;
  8686. }
  8687. if (battle_config.idletime_option&IDLE_CHAT)
  8688. sd->idletime = last_tick;
  8689. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_CHAT)
  8690. sd->idletime_hom = last_tick;
  8691. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_CHAT)
  8692. sd->idletime_mer = last_tick;
  8693. //achievement_update_objective(sd, AG_CHATTING, 1, 1); // !TODO: Confirm how this achievement is triggered
  8694. return true;
  8695. }
  8696. /**
  8697. * Displays a message if the player enters a PK Zone (during pk_mode)
  8698. * @param sd: Player data
  8699. */
  8700. inline void clif_pk_mode_message(map_session_data * sd)
  8701. {
  8702. if (battle_config.pk_mode && battle_config.pk_mode_mes &&
  8703. sd && map_getmapflag(sd->bl.m, MF_PVP)) {
  8704. if( (int)sd->status.base_level < battle_config.pk_min_level ) {
  8705. char output[CHAT_SIZE_MAX];
  8706. // 1504: You've entered a PK Zone (safe until level %d).
  8707. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(sd,1504),
  8708. battle_config.pk_min_level);
  8709. clif_showscript(&sd->bl, output, SELF);
  8710. } else {
  8711. // 1503: You've entered a PK Zone.
  8712. clif_showscript(&sd->bl, msg_txt(sd,1503), SELF);
  8713. }
  8714. }
  8715. return;
  8716. }
  8717. // ---------------------
  8718. // clif_parse_wanttoconnect
  8719. // ---------------------
  8720. // Parses a WantToConnection packet to try to identify which is the packet version used. [Skotlex]
  8721. // error codes:
  8722. // 0 - Success
  8723. // 1 - Unknown packet_ver
  8724. // 2 - Invalid account_id
  8725. // 3 - Invalid char_id
  8726. // 4 - Invalid login_id1 (reserved)
  8727. // 5 - Invalid client_tick (reserved)
  8728. // 6 - Invalid sex
  8729. // Only the first 'invalid' error that appears is used.
  8730. static int clif_parse_WantToConnection_sub(int fd)
  8731. {
  8732. int value; //Value is used to temporarily store account/char_id/sex
  8733. //By default, start searching on the default one.
  8734. uint16 cmd = RFIFOW(fd, 0);
  8735. int16 packet_len;
  8736. packet_len = static_cast<decltype(packet_len)>( RFIFOREST( fd ) );
  8737. // FIXME: If the packet is not received at once, this will FAIL.
  8738. // Figure out, when it happens, that only part of the packet is
  8739. // received, or fix the function to be able to deal with that
  8740. // case.
  8741. if( packet_len != packet_db[cmd].len )
  8742. return 1; /* wrong length */
  8743. else if( (value=(int)RFIFOL(fd, packet_db[cmd].pos[0])) < START_ACCOUNT_NUM || value > END_ACCOUNT_NUM )
  8744. return 2; /* invalid account_id */
  8745. else if( (value=(int)RFIFOL(fd, packet_db[cmd].pos[1])) <= 0 )
  8746. return 3; /* invalid char_id */
  8747. /* RFIFOL(fd, packet_db[cmd].pos[2]) - don't care about login_id1 */
  8748. /* RFIFOL(fd, packet_db[cmd].pos[3]) - don't care about client_tick */
  8749. else if( (value=(int)RFIFOB(fd, packet_db[cmd].pos[4])) != 0 && value != 1 )
  8750. return 6; /* invalid sex */
  8751. else
  8752. return 0;
  8753. }
  8754. /// Request to connect to map-server.
  8755. /// 0072 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER)
  8756. /// 0436 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER2)
  8757. /// There are various variants of this packet, some of them have padding between fields.
  8758. void clif_parse_WantToConnection(int fd, map_session_data* sd)
  8759. {
  8760. struct block_list* bl;
  8761. struct auth_node* node;
  8762. int cmd, account_id, char_id, login_id1, sex, err;
  8763. t_tick client_tick; //The client tick is a tick, therefore it needs be unsigned. [Skotlex]
  8764. if (sd) {
  8765. ShowError("clif_parse_WantToConnection : invalid request (character already logged in)\n");
  8766. return;
  8767. }
  8768. cmd = RFIFOW(fd, 0);
  8769. account_id = RFIFOL(fd, packet_db[cmd].pos[0]);
  8770. char_id = RFIFOL(fd, packet_db[cmd].pos[1]);
  8771. login_id1 = RFIFOL(fd, packet_db[cmd].pos[2]);
  8772. client_tick = RFIFOL(fd, packet_db[cmd].pos[3]);
  8773. sex = RFIFOB(fd, packet_db[cmd].pos[4]);
  8774. err = clif_parse_WantToConnection_sub(fd);
  8775. if( err ){ // connection rejected
  8776. ShowInfo("clif_parse: Disconnecting session #%d with unknown connect packet 0x%04x(length:%d)%s\n", fd, cmd, RFIFOREST(fd), (
  8777. err == 1 ? "." :
  8778. err == 2 ? ", possibly for having an invalid account_id." :
  8779. err == 3 ? ", possibly for having an invalid char_id." :
  8780. /* Uncomment when checks are added in clif_parse_WantToConnection_sub. [FlavioJS]
  8781. err == 4 ? ", possibly for having an invalid login_id1." :
  8782. err == 5 ? ", possibly for having an invalid client_tick." :
  8783. */
  8784. err == 6 ? ", possibly for having an invalid sex." :
  8785. ". ERROR invalid error code"));
  8786. WFIFOHEAD(fd,packet_len(0x6a));
  8787. WFIFOW(fd,0) = 0x6a;
  8788. WFIFOB(fd,2) = err;
  8789. WFIFOSET(fd,packet_len(0x6a));
  8790. #ifdef DUMP_INVALID_PACKET
  8791. ShowDump(RFIFOP(fd, 0), RFIFOREST(fd));
  8792. #endif
  8793. RFIFOSKIP(fd, RFIFOREST(fd));
  8794. set_eof(fd);
  8795. return;
  8796. }
  8797. if( !global_core->is_running() ){ // not allowed
  8798. clif_authfail_fd(fd,1);// server closed
  8799. return;
  8800. }
  8801. //Check for double login.
  8802. bl = map_id2bl(account_id);
  8803. if(bl && bl->type != BL_PC) {
  8804. ShowError("clif_parse_WantToConnection: a non-player object already has id %d, please increase the starting account number\n", account_id);
  8805. WFIFOHEAD(fd,packet_len(0x6a));
  8806. WFIFOW(fd,0) = 0x6a;
  8807. WFIFOB(fd,2) = 3; // Rejected by server
  8808. WFIFOSET(fd,packet_len(0x6a));
  8809. set_eof(fd);
  8810. return;
  8811. }
  8812. if (bl ||
  8813. ((node=chrif_search(account_id)) && //An already existing node is valid only if it is for this login.
  8814. !(node->account_id == account_id && node->char_id == char_id && node->state == ST_LOGIN)))
  8815. {
  8816. clif_authfail_fd(fd, 8); //Still recognizes last connection
  8817. return;
  8818. }
  8819. CREATE(sd, TBL_PC, 1);
  8820. new(sd) map_session_data();
  8821. sd->fd = fd;
  8822. #ifdef PACKET_OBFUSCATION
  8823. sd->cryptKey = (((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF) * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF;
  8824. #endif
  8825. session[fd]->session_data = sd;
  8826. pc_setnewpc(sd, account_id, char_id, login_id1, client_tick, sex, fd);
  8827. #if PACKETVER < 20070521
  8828. WFIFOHEAD(fd,4);
  8829. WFIFOL(fd,0) = sd->bl.id;
  8830. WFIFOSET(fd,4);
  8831. #else
  8832. WFIFOHEAD(fd,packet_len(0x283));
  8833. WFIFOW(fd,0) = 0x283;
  8834. WFIFOL(fd,2) = sd->bl.id;
  8835. WFIFOSET(fd,packet_len(0x283));
  8836. #endif
  8837. chrif_authreq(sd,false);
  8838. }
  8839. /// Notification from the client, that it has finished map loading and is about to display player's character (CZ_NOTIFY_ACTORINIT).
  8840. /// 007d
  8841. void clif_parse_LoadEndAck(int fd,map_session_data *sd)
  8842. {
  8843. bool guild_notice = false;
  8844. if(sd->bl.prev != nullptr)
  8845. return;
  8846. // Autotraders should ignore this entirely, clif_parse_LoadEndAck is always invoked manually for them
  8847. if (!sd->state.active || (!sd->state.autotrade && !sd->state.pc_loaded)) { //Character loading is not complete yet!
  8848. //Let pc_reg_received or pc_scdata_received reinvoke this when ready.
  8849. sd->state.connect_new = 0;
  8850. return;
  8851. }
  8852. if (sd->state.rewarp) { //Rewarp player.
  8853. sd->state.rewarp = 0;
  8854. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8855. return;
  8856. }
  8857. sd->state.warping = 0;
  8858. // look
  8859. #if PACKETVER < 4
  8860. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8861. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8862. #else
  8863. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8864. #endif
  8865. pc_set_costume_view(sd);
  8866. if(sd->vd.cloth_color)
  8867. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8868. if(sd->vd.body_style)
  8869. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8870. // item
  8871. clif_inventorylist(sd); // inventory list first, otherwise deleted items in pc_checkitem show up as 'unknown item'
  8872. pc_checkitem(sd);
  8873. clif_equipswitch_list(sd);
  8874. // cart
  8875. if(pc_iscarton(sd)) {
  8876. clif_cartlist(sd);
  8877. clif_updatestatus(*sd,SP_CARTINFO);
  8878. }
  8879. // weight
  8880. clif_updatestatus(*sd,SP_WEIGHT);
  8881. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8882. // guild
  8883. // (needs to go before clif_spawn() to show guild emblems correctly)
  8884. if(sd->status.guild_id)
  8885. guild_send_memberinfoshort(sd,1);
  8886. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8887. if(battle_config.pc_invincible_time > 0) {
  8888. if(mapdata_flag_gvg(mapdata))
  8889. pc_setinvincibletimer(sd,battle_config.pc_invincible_time<<1);
  8890. else
  8891. pc_setinvincibletimer(sd,battle_config.pc_invincible_time);
  8892. }
  8893. if( mapdata->users++ == 0 && battle_config.dynamic_mobs )
  8894. map_spawnmobs(sd->bl.m);
  8895. if( !pc_isinvisible(sd) ) { // increment the number of pvp players on the map
  8896. mapdata->users_pvp++;
  8897. }
  8898. sd->state.debug_remove_map = 0; // temporary state to track double remove_map's [FlavioJS]
  8899. // reset the callshop flag if the player changes map
  8900. sd->state.callshop = 0;
  8901. if(map_addblock(&sd->bl))
  8902. return;
  8903. clif_spawn(&sd->bl);
  8904. // Party
  8905. // (needs to go after clif_spawn() to show hp bars correctly)
  8906. if(sd->status.party_id) {
  8907. party_send_movemap(sd);
  8908. clif_party_hp( *sd ); // Show hp after displacement [LuzZza]
  8909. }
  8910. if( sd->bg_id ) clif_bg_hp(sd); // BattleGround System
  8911. if(!pc_isinvisible(sd) && mapdata->getMapFlag(MF_PVP)) {
  8912. if(!battle_config.pk_mode) { // remove pvp stuff for pk_mode [Valaris]
  8913. if (!mapdata->getMapFlag(MF_PVP_NOCALCRANK))
  8914. sd->pvp_timer = add_timer(gettick()+200, pc_calc_pvprank_timer, sd->bl.id, 0);
  8915. sd->pvp_rank = 0;
  8916. sd->pvp_lastusers = 0;
  8917. sd->pvp_point = 5;
  8918. sd->pvp_won = 0;
  8919. sd->pvp_lost = 0;
  8920. }
  8921. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8922. } else if(sd->duel_group) // set flag, if it's a duel [LuzZza]
  8923. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8924. else if (mapdata->getMapFlag(MF_GVG_DUNGEON))
  8925. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF); //TODO: Figure out the real packet to send here.
  8926. else if( mapdata_flag_gvg(mapdata) )
  8927. clif_map_property(&sd->bl, MAPPROPERTY_AGITZONE, SELF);
  8928. else
  8929. clif_map_property(&sd->bl, MAPPROPERTY_NOTHING, SELF);
  8930. // info about nearby objects
  8931. // must use foreachinarea (CIRCULAR_AREA interferes with foreachinrange)
  8932. 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);
  8933. // pet
  8934. if( sd->pd ) {
  8935. if( battle_config.pet_no_gvg && mapdata_flag_gvg(mapdata) ) { //Return the pet to egg. [Skotlex]
  8936. clif_displaymessage(sd->fd, msg_txt(sd,666));
  8937. pet_return_egg( sd, sd->pd );
  8938. } else {
  8939. if(map_addblock(&sd->pd->bl))
  8940. return;
  8941. clif_spawn(&sd->pd->bl);
  8942. clif_send_petdata(sd,sd->pd,0,0);
  8943. clif_send_petstatus(sd);
  8944. // skill_unit_move(&sd->pd->bl,gettick(),1);
  8945. }
  8946. }
  8947. //homunculus [blackhole89]
  8948. if( hom_is_active(sd->hd) ) {
  8949. if(map_addblock(&sd->hd->bl))
  8950. return;
  8951. clif_spawn(&sd->hd->bl);
  8952. clif_send_homdata( *sd->hd, SP_ACK );
  8953. clif_hominfo(sd,sd->hd,1);
  8954. clif_hominfo(sd,sd->hd,0); //for some reason, at least older clients want this sent twice
  8955. clif_homskillinfoblock( *sd->hd );
  8956. status_calc_bl(&sd->hd->bl, { SCB_SPEED });
  8957. if( !(battle_config.hom_setting&HOMSET_NO_INSTANT_LAND_SKILL) )
  8958. skill_unit_move(&sd->hd->bl,gettick(),1); // apply land skills immediately
  8959. }
  8960. if( sd->md ) {
  8961. if(map_addblock(&sd->md->bl))
  8962. return;
  8963. clif_spawn(&sd->md->bl);
  8964. clif_mercenary_info(sd);
  8965. clif_mercenary_skillblock(sd);
  8966. status_calc_bl(&sd->md->bl, { SCB_SPEED }); // Mercenary mimic their master's speed on each map change
  8967. }
  8968. if( sd->ed ) {
  8969. if (map_addblock(&sd->ed->bl))
  8970. return;
  8971. clif_spawn(&sd->ed->bl);
  8972. clif_elemental_info(sd);
  8973. clif_elemental_updatestatus(*sd, SP_HP);
  8974. clif_hpmeter_single( *sd, sd->ed->bl.id, sd->ed->battle_status.hp, sd->ed->battle_status.max_hp );
  8975. clif_elemental_updatestatus(*sd, SP_SP);
  8976. status_calc_bl(&sd->ed->bl, { SCB_SPEED }); //Elemental mimic their master's speed on each map change
  8977. }
  8978. if(sd->state.connect_new) {
  8979. int lv;
  8980. guild_notice = true;
  8981. clif_skillinfoblock(sd);
  8982. clif_hotkeys_send(sd,0);
  8983. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  8984. clif_hotkeys_send(sd,1);
  8985. #endif
  8986. clif_updatestatus(*sd,SP_BASEEXP);
  8987. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  8988. clif_updatestatus(*sd,SP_JOBEXP);
  8989. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  8990. clif_updatestatus(*sd,SP_SKILLPOINT);
  8991. clif_initialstatus( *sd );
  8992. if (sd->sc.option&OPTION_FALCON)
  8993. clif_status_load(&sd->bl, EFST_FALCON, 1);
  8994. else if (sd->sc.option&(OPTION_RIDING|OPTION_DRAGON))
  8995. clif_status_load(&sd->bl, EFST_RIDING, 1);
  8996. else if (sd->sc.option&OPTION_WUGRIDER)
  8997. clif_status_load(&sd->bl, EFST_WUGRIDER, 1);
  8998. else if (sd->sc.getSCE(SC_ALL_RIDING))
  8999. clif_status_load(&sd->bl, EFST_ALL_RIDING, 1);
  9000. if(sd->status.manner < 0)
  9001. sc_start(&sd->bl,&sd->bl,SC_NOCHAT,100,0,0);
  9002. //Auron reported that This skill only triggers when you logon on the map o.O [Skotlex]
  9003. if ((lv = pc_checkskill(sd,SG_KNOWLEDGE)) > 0) {
  9004. if(sd->bl.m == sd->feel_map[0].m
  9005. || sd->bl.m == sd->feel_map[1].m
  9006. || sd->bl.m == sd->feel_map[2].m)
  9007. sc_start(&sd->bl,&sd->bl, SC_KNOWLEDGE, 100, lv, skill_get_time(SG_KNOWLEDGE, lv));
  9008. }
  9009. if(sd->pd && sd->pd->pet.intimate > 900)
  9010. clif_pet_emotion(sd->pd,(sd->pd->pet.class_ - 100)*100 + 50 + pet_hungry_val(sd->pd));
  9011. if(hom_is_active(sd->hd))
  9012. hom_init_timers(sd->hd);
  9013. if (night_flag && mapdata->getMapFlag(MF_NIGHTENABLED)) {
  9014. sd->state.night = 1;
  9015. clif_status_load(&sd->bl, EFST_SKE, 1);
  9016. }
  9017. // Notify everyone that this char logged in [Skotlex].
  9018. map_foreachpc(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 1);
  9019. if (!sd->state.autotrade) { // Don't trigger NPC event or opening vending/buyingstore will be failed
  9020. npc_script_event( *sd, NPCE_LOGIN );
  9021. }
  9022. // Set facing direction before check below to update client
  9023. if (battle_config.spawn_direction)
  9024. unit_setdir(&sd->bl, sd->status.body_direction, false);
  9025. } else {
  9026. //For some reason the client "loses" these on warp/map-change.
  9027. clif_updatestatus(*sd,SP_STR);
  9028. clif_updatestatus(*sd,SP_AGI);
  9029. clif_updatestatus(*sd,SP_VIT);
  9030. clif_updatestatus(*sd,SP_INT);
  9031. clif_updatestatus(*sd,SP_DEX);
  9032. clif_updatestatus(*sd,SP_LUK);
  9033. #ifdef RENEWAL
  9034. clif_updatestatus(*sd,SP_POW);
  9035. clif_updatestatus(*sd,SP_STA);
  9036. clif_updatestatus(*sd,SP_WIS);
  9037. clif_updatestatus(*sd,SP_SPL);
  9038. clif_updatestatus(*sd,SP_CON);
  9039. clif_updatestatus(*sd,SP_CRT);
  9040. #endif
  9041. // abort currently running script
  9042. sd->state.using_fake_npc = 0;
  9043. sd->state.menu_or_input = 0;
  9044. sd->npc_menu = 0;
  9045. if(sd->npc_id)
  9046. npc_event_dequeue(sd);
  9047. }
  9048. if( sd->state.changemap ) {// restore information that gets lost on map-change
  9049. clif_partyinvitationstate( *sd );
  9050. #if PACKETVER >= 20070918
  9051. clif_equipcheckbox(sd);
  9052. #endif
  9053. clif_pet_autofeed_status(sd,false);
  9054. clif_configuration( sd, CONFIG_CALL, sd->status.disable_call );
  9055. #if PACKETVER >= 20170920
  9056. if( battle_config.homunculus_autofeed_always ){
  9057. // Always send ON or OFF
  9058. if( sd->hd && battle_config.feature_homunculus_autofeed ){
  9059. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, sd->hd->homunculus.autofeed );
  9060. }else{
  9061. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, false );
  9062. }
  9063. }else{
  9064. // Only send when enabled
  9065. if( sd->hd && battle_config.feature_homunculus_autofeed && sd->hd->homunculus.autofeed ){
  9066. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, true );
  9067. }
  9068. }
  9069. #endif
  9070. clif_reputation_list( *sd );
  9071. if (sd->guild && battle_config.guild_notice_changemap == 1){
  9072. // Displays after VIP
  9073. clif_guild_notice( *sd );
  9074. guild_notice = false; // Do not display it twice
  9075. }
  9076. if (battle_config.bg_flee_penalty != 100 || battle_config.gvg_flee_penalty != 100) {
  9077. struct map_data *pmap = map_getmapdata(sd->state.pmap);
  9078. if ((pmap != nullptr && (mapdata_flag_gvg(pmap) || pmap->getMapFlag(MF_BATTLEGROUND))) || (mapdata != nullptr && (mapdata_flag_gvg(mapdata) || mapdata->getMapFlag(MF_BATTLEGROUND))))
  9079. status_calc_bl(&sd->bl, { SCB_FLEE }); //Refresh flee penalty
  9080. }
  9081. if( night_flag && mapdata->getMapFlag(MF_NIGHTENABLED) )
  9082. { //Display night.
  9083. if( !sd->state.night )
  9084. {
  9085. sd->state.night = 1;
  9086. clif_status_load(&sd->bl, EFST_SKE, 1);
  9087. }
  9088. }
  9089. else if( sd->state.night )
  9090. { //Clear night display.
  9091. sd->state.night = 0;
  9092. clif_status_load(&sd->bl, EFST_SKE, 0);
  9093. }
  9094. if( mapdata->getMapFlag(MF_BATTLEGROUND) )
  9095. {
  9096. clif_map_type( *sd, MAPTYPE_BATTLEFIELD ); // Battleground Mode
  9097. if( map_getmapflag(sd->bl.m, MF_BATTLEGROUND) == 2 )
  9098. clif_bg_updatescore_single(sd);
  9099. }
  9100. if( mapdata->getMapFlag(MF_ALLOWKS) && !mapdata_flag_ks(mapdata) )
  9101. {
  9102. char output[128];
  9103. sprintf(output, "[ Kill Steal Protection Disable. KS is allowed in this map ]");
  9104. clif_broadcast(&sd->bl, output, strlen(output) + 1, BC_BLUE, SELF);
  9105. }
  9106. if( pc_has_permission(sd,PC_PERM_VIEW_HPMETER) ) {
  9107. mapdata->hpmeter_visible++;
  9108. sd->state.hpmeter_visible = 1;
  9109. }
  9110. status_change_clear_onChangeMap(&sd->bl, &sd->sc);
  9111. map_iwall_get(sd); // Updates Walls Info on this Map to Client
  9112. status_calc_pc(sd, sd->state.autotrade ? SCO_FIRST : SCO_NONE); // Some conditions are map-dependent so we must recalculate
  9113. #ifdef VIP_ENABLE
  9114. if (!sd->state.connect_new &&
  9115. !sd->vip.disableshowrate &&
  9116. sd->state.pmap != sd->bl.m &&
  9117. map_getmapflag(sd->state.pmap, MF_BEXP) != mapdata->getMapFlag(MF_BEXP)
  9118. )
  9119. {
  9120. clif_display_pinfo( *sd );
  9121. }
  9122. #endif
  9123. // Instances do not need their own channels
  9124. if( channel_config.map_tmpl.name[0] && (channel_config.map_tmpl.opt&CHAN_OPT_AUTOJOIN) && !mapdata->instance_id && !mapdata->getMapFlag(MF_NOMAPCHANNELAUTOJOIN) )
  9125. channel_mjoin(sd); //join new map
  9126. clif_pk_mode_message(sd);
  9127. }
  9128. if( sd->guild && ( battle_config.guild_notice_changemap == 2 || guild_notice ) ){
  9129. // Displays at end
  9130. clif_guild_notice( *sd );
  9131. }
  9132. mail_clear(sd);
  9133. /* Guild Aura Init */
  9134. if( sd->guild && sd->state.gmaster_flag ) {
  9135. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  9136. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  9137. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  9138. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  9139. }
  9140. if( sd->state.vending ) { /* show we have a vending */
  9141. clif_openvending( *sd );
  9142. clif_showvendingboard( *sd );
  9143. }
  9144. // Don't trigger NPC event or opening vending/buyingstore will be failed
  9145. if(!sd->state.autotrade && mapdata->getMapFlag(MF_LOADEVENT)) // Lance
  9146. npc_script_event( *sd, NPCE_LOADMAP );
  9147. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  9148. clif_status_load(&sd->bl, EFST_DEVIL1, 1); //blindness [Komurka]
  9149. if (sd->sc.opt2) //Client loses these on warp.
  9150. clif_changeoption(&sd->bl);
  9151. 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)) {
  9152. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM);
  9153. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM);
  9154. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  9155. }
  9156. clif_weather_check(sd);
  9157. // For automatic triggering of NPCs after map loading (so you don't need to walk 1 step first)
  9158. if (map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNPC))
  9159. npc_touch_area_allnpc(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  9160. else
  9161. sd->areanpc.clear();
  9162. /* it broke at some point (e.g. during a crash), so we make it visibly dead again. */
  9163. if( !sd->status.hp && !pc_isdead(sd) && status_isdead(sd->bl) )
  9164. pc_setdead(sd);
  9165. // If player is dead, and is spawned (such as @refresh) send death packet. [Valaris]
  9166. if(pc_isdead(sd))
  9167. clif_clearunit_area( sd->bl, CLR_DEAD );
  9168. else {
  9169. skill_usave_trigger(sd);
  9170. if (battle_config.spawn_direction)
  9171. clif_changed_dir(sd->bl, SELF);
  9172. }
  9173. // Trigger skill effects if you appear standing on them
  9174. if(!battle_config.pc_invincible_time)
  9175. skill_unit_move(&sd->bl,gettick(),1);
  9176. pc_show_questinfo_reinit(sd);
  9177. pc_show_questinfo(sd);
  9178. #if PACKETVER >= 20150513
  9179. if( sd->mail.inbox.unread ){
  9180. clif_Mail_new(sd, 0, nullptr, nullptr);
  9181. }
  9182. #endif
  9183. sd->state.connect_new = 0;
  9184. sd->state.changemap = false;
  9185. }
  9186. /// Server's tick (ZC_NOTIFY_TIME).
  9187. /// 007f <time>.L
  9188. void clif_notify_time(map_session_data* sd, t_tick time)
  9189. {
  9190. int fd = sd->fd;
  9191. WFIFOHEAD(fd,packet_len(0x7f));
  9192. WFIFOW(fd,0) = 0x7f;
  9193. WFIFOL(fd,2) = client_tick(time);
  9194. WFIFOSET(fd,packet_len(0x7f));
  9195. }
  9196. /// Request for server's tick.
  9197. /// 007e <client tick>.L (CZ_REQUEST_TIME)
  9198. /// 0360 <client tick>.L (CZ_REQUEST_TIME2)
  9199. /// There are various variants of this packet, some of them have padding between fields.
  9200. void clif_parse_TickSend(int fd, map_session_data *sd)
  9201. {
  9202. sd->client_tick = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9203. clif_notify_time(sd, gettick());
  9204. }
  9205. /// Sends hotkey bar.
  9206. /// 02b9 { <is skill>.B <id>.L <count>.W }*27 (ZC_SHORTCUT_KEY_LIST)
  9207. /// 07d9 { <is skill>.B <id>.L <count>.W }*36 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090603)
  9208. /// 07d9 { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090617)
  9209. /// 0a00 <rotate>.B { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V3, PACKETVER >= 20141022)
  9210. void clif_hotkeys_send( map_session_data *sd, int tab ){
  9211. #if defined(HOTKEY_SAVING) && ( PACKETVER_MAIN_NUM >= 20070711 || PACKETVER_RE_NUM >= 20080827 || PACKETVER_AD_NUM >= 20070711 || PACKETVER_SAK_NUM >= 20070628 )
  9212. nullpo_retv( sd );
  9213. PACKET_ZC_SHORTCUT_KEY_LIST p = {};
  9214. p.packetType = HEADER_ZC_SHORTCUT_KEY_LIST;
  9215. #if PACKETVER_MAIN_NUM >= 20141022 || PACKETVER_RE_NUM >= 20141015 || defined(PACKETVER_ZERO)
  9216. if( tab == 0 ){
  9217. p.rotate = sd->status.hotkey_rowshift;
  9218. }else{
  9219. p.rotate = sd->status.hotkey_rowshift2;
  9220. }
  9221. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9222. p.tab = tab;
  9223. #endif
  9224. #endif
  9225. for( int i = 0, offset = tab * MAX_HOTKEYS; i < MAX_HOTKEYS_PACKET; i++ ){
  9226. p.hotkey[i].isSkill = sd->status.hotkeys[i + offset].type;
  9227. p.hotkey[i].id = sd->status.hotkeys[i + offset].id;
  9228. p.hotkey[i].count = sd->status.hotkeys[i + offset].lv;
  9229. }
  9230. clif_send( &p, sizeof(PACKET_ZC_SHORTCUT_KEY_LIST), &sd->bl, SELF );
  9231. #endif
  9232. }
  9233. /// Request to update a position on the hotkey row bar
  9234. void clif_parse_HotkeyRowShift( int fd, map_session_data *sd ){
  9235. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9236. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE2* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE2*>( RFIFOP( fd, 0 ) );
  9237. if( p->tab == 0 ){
  9238. sd->status.hotkey_rowshift = p->rowshift;
  9239. }else{
  9240. sd->status.hotkey_rowshift2 = p->rowshift;
  9241. }
  9242. #elif PACKETVER_MAIN_NUM >= 20140129 || PACKETVER_RE_NUM >= 20140129 || defined(PACKETVER_ZERO)
  9243. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE1* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE1*>( RFIFOP( fd, 0 ) );
  9244. sd->status.hotkey_rowshift = p->rowshift;
  9245. #endif
  9246. }
  9247. /// Request to update a position on the hotkey bar.
  9248. /// 02ba <index>.W <is skill>.B <id>.L <count>.W (CZ_SHORTCUT_KEY_CHANGE)
  9249. void clif_parse_Hotkey(int fd, map_session_data *sd) {
  9250. #ifdef HOTKEY_SAVING
  9251. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9252. const PACKET_CZ_SHORTCUT_KEY_CHANGE2* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE2*>( RFIFOP( fd, 0 ) );
  9253. const unsigned short idx = p->index + p->tab * MAX_HOTKEYS;
  9254. if( idx >= MAX_HOTKEYS_DB ){
  9255. return;
  9256. }
  9257. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9258. sd->status.hotkeys[idx].id = p->hotkey.id;
  9259. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9260. #elif PACKETVER_MAIN_NUM >= 20070618 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_AD_NUM >= 20070618 || PACKETVER_SAK_NUM >= 20070618
  9261. const PACKET_CZ_SHORTCUT_KEY_CHANGE1* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE1*>( RFIFOP( fd, 0 ) );
  9262. const unsigned short idx = p->index;
  9263. if( idx >= MAX_HOTKEYS_DB ){
  9264. return;
  9265. }
  9266. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9267. sd->status.hotkeys[idx].id = p->hotkey.id;
  9268. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9269. #endif
  9270. #endif
  9271. }
  9272. /// Displays cast-like progress bar (ZC_PROGRESS).
  9273. /// 02f0 <color>.L <time>.L
  9274. void clif_progressbar(map_session_data * sd, unsigned long color, unsigned int second)
  9275. {
  9276. int fd = sd->fd;
  9277. WFIFOHEAD(fd,packet_len(0x2f0));
  9278. WFIFOW(fd,0) = 0x2f0;
  9279. WFIFOL(fd,2) = color;
  9280. WFIFOL(fd,6) = second;
  9281. WFIFOSET(fd,packet_len(0x2f0));
  9282. }
  9283. /// Removes an ongoing progress bar (ZC_PROGRESS_CANCEL).
  9284. /// 02f2
  9285. void clif_progressbar_abort(map_session_data * sd)
  9286. {
  9287. int fd = sd->fd;
  9288. WFIFOHEAD(fd,packet_len(0x2f2));
  9289. WFIFOW(fd,0) = 0x2f2;
  9290. WFIFOSET(fd,packet_len(0x2f2));
  9291. }
  9292. /// Notification from the client, that the progress bar has reached 100%.
  9293. /// 02f1 (CZ_PROGRESS)
  9294. void clif_parse_progressbar(int fd, map_session_data * sd){
  9295. // No progressbar active, ignore it
  9296. if( !sd->progressbar.npc_id ){
  9297. return;
  9298. }
  9299. int npc_id = sd->progressbar.npc_id;
  9300. bool closing = false;
  9301. // Check if the progress was canceled
  9302. if( gettick() < sd->progressbar.timeout && sd->st ){
  9303. closing = true;
  9304. sd->st->state = CLOSE; // will result in END in npc_scriptcont
  9305. // If a message window was open, offer a close button to the user
  9306. if( sd->st->mes_active ){
  9307. clif_scriptclose( *sd, npc_id );
  9308. }
  9309. }
  9310. sd->progressbar.npc_id = 0;
  9311. sd->progressbar.timeout = 0;
  9312. sd->state.workinprogress = WIP_DISABLE_NONE;
  9313. if( battle_config.idletime_option&IDLE_NPC_PROGRESS ){
  9314. sd->idletime = last_tick;
  9315. }
  9316. npc_scriptcont(sd, npc_id, closing);
  9317. }
  9318. /// Displays cast-like progress bar on a NPC
  9319. /// 09d1 <id>.L <color>.L <time>.L (ZC_PROGRESS_ACTOR)
  9320. void clif_progressbar_npc( struct npc_data *nd, map_session_data* sd ){
  9321. #if PACKETVER >= 20130821
  9322. unsigned char buf[14];
  9323. if( nd->progressbar.timeout > 0 ){
  9324. WBUFW(buf,0) = 0x9d1;
  9325. WBUFL(buf,2) = nd->bl.id;
  9326. WBUFL(buf,6) = nd->progressbar.color;
  9327. WBUFL(buf,10) = client_tick( ( nd->progressbar.timeout - gettick() ) / 1000 );
  9328. if( sd ){
  9329. clif_send(buf, packet_len(0x9d1), &sd->bl, SELF);
  9330. }else{
  9331. clif_send(buf, packet_len(0x9d1), &nd->bl, AREA);
  9332. }
  9333. }
  9334. #endif
  9335. }
  9336. /// Request to walk to a certain position on the current map.
  9337. /// 0085 <dest>.3B (CZ_REQUEST_MOVE)
  9338. /// 035f <dest>.3B (CZ_REQUEST_MOVE2)
  9339. /// There are various variants of this packet, some of them have padding between fields.
  9340. void clif_parse_WalkToXY(int fd, map_session_data *sd)
  9341. {
  9342. short x, y;
  9343. if (pc_isdead(sd)) {
  9344. clif_clearunit_area( sd->bl, CLR_DEAD );
  9345. return;
  9346. }
  9347. if (sd->sc.opt1 && ( sd->sc.opt1 == OPT1_STONEWAIT || sd->sc.opt1 == OPT1_BURNING ))
  9348. ; //You CAN walk on this OPT1 value.
  9349. else if (sd->progressbar.npc_id) {
  9350. clif_progressbar_abort(sd);
  9351. return; // First walk attempt cancels the progress bar
  9352. } else if (pc_cant_act(sd))
  9353. return;
  9354. if(sd->sc.getSCE(SC_RUN) || sd->sc.getSCE(SC_WUGDASH))
  9355. return;
  9356. RFIFOPOS(fd, packet_db[RFIFOW(fd,0)].pos[0], &x, &y, nullptr);
  9357. //A move command one cell west is only valid if the target cell is free
  9358. if(battle_config.official_cell_stack_limit > 0
  9359. && sd->bl.x == x+1 && sd->bl.y == y && map_count_oncell(sd->bl.m, x, y, BL_CHAR|BL_NPC, 1))
  9360. return;
  9361. // Cloaking wall check is actually updated when you click to process next movement
  9362. // not when you move each cell. This is official behaviour.
  9363. if (sd->sc.getSCE(SC_CLOAKING))
  9364. skill_check_cloaking(&sd->bl, sd->sc.getSCE(SC_CLOAKING));
  9365. status_change_end(&sd->bl, SC_ROLLINGCUTTER); // If you move, you lose your counters. [malufett]
  9366. status_change_end(&sd->bl, SC_CRESCIVEBOLT);
  9367. pc_delinvincibletimer(sd);
  9368. //Set last idle time... [Skotlex]
  9369. if (battle_config.idletime_option&IDLE_WALK)
  9370. sd->idletime = last_tick;
  9371. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_WALK)
  9372. sd->idletime_hom = last_tick;
  9373. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_WALK)
  9374. sd->idletime_mer = last_tick;
  9375. unit_walktoxy(&sd->bl, x, y, 4);
  9376. }
  9377. /// Notification about the result of a disconnect request (ZC_ACK_REQ_DISCONNECT).
  9378. /// 018b <result>.W
  9379. /// result:
  9380. /// 0 = disconnect (quit)
  9381. /// 1 = cannot disconnect (wait 10 seconds)
  9382. /// ? = ignored
  9383. void clif_disconnect_ack(map_session_data* sd, short result)
  9384. {
  9385. int fd = sd->fd;
  9386. WFIFOHEAD(fd,packet_len(0x18b));
  9387. WFIFOW(fd,0) = 0x18b;
  9388. WFIFOW(fd,2) = result;
  9389. WFIFOSET(fd,packet_len(0x18b));
  9390. }
  9391. /// Request to disconnect from server (CZ_REQ_DISCONNECT).
  9392. /// 018a <type>.W
  9393. /// type:
  9394. /// 0 = quit
  9395. void clif_parse_QuitGame(int fd, map_session_data *sd)
  9396. {
  9397. /* Rovert's prevent logout option fixed [Valaris] */
  9398. 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) &&
  9399. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9400. {
  9401. pc_damage_log_clear(sd,0);
  9402. clif_disconnect_ack(sd, 0);
  9403. flush_fifo( fd );
  9404. if( battle_config.drop_connection_on_quit ){
  9405. set_eof( fd );
  9406. }
  9407. } else {
  9408. clif_disconnect_ack(sd, 1);
  9409. }
  9410. }
  9411. /// Requesting unit's name.
  9412. /// 0094 <id>.L (CZ_REQNAME)
  9413. /// 0368 <id>.L (CZ_REQNAME2)
  9414. /// There are various variants of this packet, some of them have padding between fields.
  9415. void clif_parse_GetCharNameRequest(int fd, map_session_data *sd)
  9416. {
  9417. int id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9418. struct block_list* bl;
  9419. //status_change *sc;
  9420. if( id < 0 && -id == sd->bl.id ) // for disguises [Valaris]
  9421. id = sd->bl.id;
  9422. bl = map_id2bl(id);
  9423. if( bl == nullptr )
  9424. return; // Lagged clients could request names of already gone mobs/players. [Skotlex]
  9425. if( sd->bl.m != bl->m || !check_distance_bl(&sd->bl, bl, AREA_SIZE) )
  9426. return; // Block namerequests past view range
  9427. // 'see people in GM hide' cheat detection
  9428. /* disabled due to false positives (network lag + request name of char that's about to hide = race condition)
  9429. sc = status_get_sc(bl);
  9430. if (sc && sc->option&OPTION_INVISIBLE && !disguised(bl) &&
  9431. bl->type != BL_NPC && //Skip hidden NPCs which can be seen using Maya Purple
  9432. pc_get_group_level(sd) < battle_config.hack_info_GM_level
  9433. ) {
  9434. char gm_msg[256];
  9435. 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);
  9436. ShowWarning(gm_msg);
  9437. // information is sent to all online GMs
  9438. intif_wis_message_to_gm(wisp_server_name, battle_config.hack_info_GM_level, gm_msg);
  9439. return;
  9440. }
  9441. */
  9442. clif_name(&sd->bl,bl,SELF);
  9443. }
  9444. /// Validates and processes global messages
  9445. /// 008c <packet len>.W <text>.?B (<name> : <message>) 00 (CZ_REQUEST_CHAT)
  9446. /// There are various variants of this packet.
  9447. void clif_parse_GlobalMessage(int fd, map_session_data* sd)
  9448. {
  9449. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9450. size_t length;
  9451. // validate packet and retrieve name and message
  9452. if( !clif_process_message(sd, false, name, message, output ) )
  9453. return;
  9454. if( sd->gcbind && ((sd->gcbind->opt&CHAN_OPT_CAN_CHAT) || pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN)) ) {
  9455. channel_send(sd->gcbind,sd,message);
  9456. return;
  9457. }
  9458. // send message to others (using the send buffer for temp. storage)
  9459. clif_GlobalMessage( sd->bl, output, sd->chatID ? CHAT_WOS : AREA_CHAT_WOC );
  9460. length = strlen(output) + 1;
  9461. // send back message to the speaker
  9462. WFIFOHEAD(fd,4+length);
  9463. WFIFOW(fd,0) = 0x8e;
  9464. WFIFOW(fd,2) = (uint16)(4+length);
  9465. safestrncpy(WFIFOCP(fd,4), output, length );
  9466. WFIFOSET(fd, WFIFOW(fd,2));
  9467. #ifdef PCRE_SUPPORT
  9468. // trigger listening npcs
  9469. map_foreachinallrange(npc_chat_sub, &sd->bl, AREA_SIZE, BL_NPC, output, strlen(output), &sd->bl);
  9470. #endif
  9471. // Chat logging type 'O' / Global Chat
  9472. 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);
  9473. //achievement_update_objective(sd, AG_CHAT, 1, sd->bl.m); //! TODO: What's the official use of this achievement type?
  9474. }
  9475. /// /mm /mapmove (as @rura GM command) (CZ_MOVETO_MAP).
  9476. /// Request to warp to a map on given coordinates.
  9477. /// 0140 <map name>.16B <x>.W <y>.W
  9478. void clif_parse_MapMove(int fd, map_session_data *sd)
  9479. {
  9480. char command[MAP_NAME_LENGTH_EXT+25];
  9481. char* map_name;
  9482. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9483. map_name = RFIFOCP(fd,info->pos[0]);
  9484. map_name[MAP_NAME_LENGTH_EXT-1]='\0';
  9485. safesnprintf(command,sizeof(command),"%cmapmove %s %d %d", atcommand_symbol, map_name,
  9486. RFIFOW(fd,info->pos[1]), //x
  9487. RFIFOW(fd,info->pos[2])); //y
  9488. is_atcommand(fd, sd, command, 1);
  9489. }
  9490. /// Updates body and head direction of an object (ZC_CHANGE_DIRECTION).
  9491. /// 009c <id>.L <head dir>.W <dir>.B
  9492. /// head dir:
  9493. /// 0 = straight
  9494. /// 1 = turned CW
  9495. /// 2 = turned CCW
  9496. /// dir:
  9497. /// 0 = north
  9498. /// 1 = northwest
  9499. /// 2 = west
  9500. /// 3 = southwest
  9501. /// 4 = south
  9502. /// 5 = southeast
  9503. /// 6 = east
  9504. /// 7 = northeast
  9505. void clif_changed_dir(block_list& bl, enum send_target target){
  9506. PACKET_ZC_CHANGE_DIRECTION p{};
  9507. p.packetType = HEADER_ZC_CHANGE_DIRECTION;
  9508. p.srcId = bl.id;
  9509. if( bl.type == BL_PC ){
  9510. p.headDir = reinterpret_cast<map_session_data*>(&bl)->head_dir;
  9511. }else{
  9512. p.headDir = 0;
  9513. }
  9514. p.dir = unit_getdir(&bl);
  9515. clif_send(&p, sizeof(p), &bl, target);
  9516. if (disguised(&bl)) {
  9517. p.srcId = disguised_bl_id(bl.id);
  9518. p.headDir = 0;
  9519. clif_send(&p, sizeof(p), &bl, SELF);
  9520. }
  9521. }
  9522. /// Request to change own body and head direction.
  9523. /// 009b <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION)
  9524. /// 0361 <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION2)
  9525. /// There are various variants of this packet, some of them have padding between fields.
  9526. void clif_parse_ChangeDir(int fd, map_session_data *sd)
  9527. {
  9528. unsigned char headdir, dir;
  9529. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9530. headdir = RFIFOB(fd,info->pos[0]);
  9531. dir = RFIFOB(fd,info->pos[1]);
  9532. pc_setdir(sd, dir, headdir);
  9533. clif_changed_dir(sd->bl, AREA_WOS);
  9534. }
  9535. /// Request to show an emotion (CZ_REQ_EMOTION).
  9536. /// 00bf <type>.B
  9537. /// type:
  9538. /// @see enum emotion_type
  9539. void clif_parse_Emotion(int fd, map_session_data *sd){
  9540. if( sd == nullptr ){
  9541. return;
  9542. }
  9543. int emoticon = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9544. if (battle_config.basic_skill_check == 0 || pc_checkskill(sd, NV_BASIC) >= 2 || pc_checkskill(sd, SU_BASIC_SKILL) >= 1) {
  9545. if (emoticon == ET_CHAT_PROHIBIT) {// prevent use of the mute emote [Valaris]
  9546. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9547. return;
  9548. }
  9549. // fix flood of emotion icon (ro-proxy): flood only the hacker player
  9550. if (sd->emotionlasttime + 1 >= time(nullptr)) { // not more than 1 per second
  9551. sd->emotionlasttime = time(nullptr);
  9552. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9553. return;
  9554. }
  9555. sd->emotionlasttime = time(nullptr);
  9556. if (battle_config.idletime_option&IDLE_EMOTION)
  9557. sd->idletime = last_tick;
  9558. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_EMOTION)
  9559. sd->idletime_hom = last_tick;
  9560. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_EMOTION)
  9561. sd->idletime_mer = last_tick;
  9562. if (sd->state.block_action & PCBLOCK_EMOTION) {
  9563. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9564. return;
  9565. }
  9566. if(battle_config.client_reshuffle_dice && emoticon>=ET_DICE1 && emoticon<=ET_DICE6) {// re-roll dice
  9567. emoticon = rnd()%6+ET_DICE1;
  9568. }
  9569. clif_emotion(&sd->bl, emoticon);
  9570. } else
  9571. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9572. }
  9573. /// Amount of currently online players, reply to /w /who
  9574. /// 00c2 <count>.L (ZC_USER_COUNT)
  9575. static void clif_user_count(map_session_data& sd)
  9576. {
  9577. PACKET_ZC_USER_COUNT p{};
  9578. p.packetType = HEADER_ZC_USER_COUNT;
  9579. p.playersCount = map_getusers();
  9580. clif_send(&p,sizeof(p),&sd.bl,SELF);
  9581. }
  9582. /// /w /who (CZ_REQ_USER_COUNT).
  9583. /// Request to display amount of currently connected players.
  9584. /// 00c1
  9585. void clif_parse_HowManyConnections(int fd, map_session_data *sd) {
  9586. if( sd == nullptr ){
  9587. return;
  9588. }
  9589. clif_user_count(*sd);
  9590. }
  9591. void clif_parse_ActionRequest_sub( map_session_data& sd, uint8 action_type, int target_id, t_tick tick ){
  9592. if (pc_isdead(&sd)) {
  9593. clif_clearunit_area(sd.bl, CLR_DEAD);
  9594. return;
  9595. }
  9596. // Statuses that don't let the player sit / stand / talk with NPCs (targeted)
  9597. if (action_type != DMG_NORMAL && action_type != DMG_REPEAT) {
  9598. if (sd.sc.cant.interact)
  9599. return;
  9600. pc_stop_walking(&sd, 1);
  9601. }
  9602. pc_stop_attack(&sd);
  9603. if(target_id<0 && -target_id == sd.bl.id) // for disguises [Valaris]
  9604. target_id = sd.bl.id;
  9605. switch(action_type)
  9606. {
  9607. case DMG_NORMAL: // once attack
  9608. case DMG_REPEAT: // continuous attack
  9609. if( pc_cant_act(&sd) )
  9610. return;
  9611. if (!battle_config.sdelay_attack_enable && pc_checkskill(&sd, SA_FREECAST) <= 0) {
  9612. if (DIFF_TICK(tick, sd.ud.canact_tick) < 0) {
  9613. clif_skill_fail( sd, 1, USESKILL_FAIL_SKILLINTERVAL );
  9614. return;
  9615. }
  9616. }
  9617. pc_delinvincibletimer(&sd);
  9618. if (battle_config.idletime_option&IDLE_ATTACK)
  9619. sd.idletime = last_tick;
  9620. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_ATTACK)
  9621. sd.idletime_hom = last_tick;
  9622. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_ATTACK)
  9623. sd.idletime_mer = last_tick;
  9624. unit_attack(&sd.bl, target_id, action_type != 0);
  9625. break;
  9626. case DMG_SIT_DOWN: // sitdown
  9627. if (battle_config.basic_skill_check && pc_checkskill(&sd, NV_BASIC) < 3 && pc_checkskill(&sd, SU_BASIC_SKILL) < 1) {
  9628. clif_skill_fail( sd, 1, USESKILL_FAIL_LEVEL, 2 );
  9629. break;
  9630. }
  9631. if(pc_issit(&sd)) {
  9632. //Bugged client? Just refresh them.
  9633. clif_sitting(sd.bl);
  9634. return;
  9635. }
  9636. if (sd.ud.skilltimer != INVALID_TIMER || (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING))
  9637. break;
  9638. if (sd.sc.count && (
  9639. sd.sc.getSCE(SC_DANCING) ||
  9640. (sd.sc.getSCE(SC_GRAVITATION) && sd.sc.getSCE(SC_GRAVITATION)->val3 == BCT_SELF)
  9641. )) //No sitting during these states either.
  9642. break;
  9643. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9644. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9645. break;
  9646. }
  9647. if (battle_config.idletime_option&IDLE_SIT)
  9648. sd.idletime = last_tick;
  9649. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9650. sd.idletime_hom = last_tick;
  9651. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9652. sd.idletime_mer = last_tick;
  9653. pc_setsit(&sd);
  9654. skill_sit(&sd, true);
  9655. clif_sitting(sd.bl);
  9656. break;
  9657. case DMG_STAND_UP: // standup
  9658. if (!pc_issit(&sd)) {
  9659. //Bugged client? Just refresh them.
  9660. clif_standing(sd.bl);
  9661. return;
  9662. }
  9663. if (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING)
  9664. break;
  9665. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9666. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9667. break;
  9668. }
  9669. if (pc_setstand(&sd, false)) {
  9670. if (battle_config.idletime_option&IDLE_SIT)
  9671. sd.idletime = last_tick;
  9672. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9673. sd.idletime_hom = last_tick;
  9674. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9675. sd.idletime_mer = last_tick;
  9676. skill_sit(&sd, false);
  9677. clif_standing(sd.bl);
  9678. }
  9679. break;
  9680. }
  9681. }
  9682. /// Request for an action.
  9683. /// 0089 <target id>.L <action>.B (CZ_REQUEST_ACT)
  9684. /// 0437 <target id>.L <action>.B (CZ_REQUEST_ACT2)
  9685. /// action:
  9686. /// 0 = attack
  9687. /// 1 = pick up item
  9688. /// 2 = sit down
  9689. /// 3 = stand up
  9690. /// 7 = continous attack
  9691. /// 12 = (touch skill?)
  9692. /// There are various variants of this packet, some of them have padding between fields.
  9693. void clif_parse_ActionRequest(int fd, map_session_data *sd)
  9694. {
  9695. if( sd == nullptr ){
  9696. return;
  9697. }
  9698. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9699. clif_parse_ActionRequest_sub( *sd,
  9700. RFIFOB(fd,info->pos[1]),
  9701. RFIFOL(fd,info->pos[0]),
  9702. gettick()
  9703. );
  9704. }
  9705. /// Response to the death/system menu (CZ_RESTART).
  9706. /// 00b2 <type>.B
  9707. /// type:
  9708. /// 0 = restart (respawn)
  9709. /// 1 = char-select (disconnect)
  9710. void clif_parse_Restart(int fd, map_session_data *sd)
  9711. {
  9712. switch(RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0])) {
  9713. case 0x00:
  9714. pc_respawn(sd,CLR_OUTSIGHT);
  9715. break;
  9716. case 0x01:
  9717. /* Rovert's Prevent logout option - Fixed [Valaris] */
  9718. 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) &&
  9719. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9720. { //Send to char-server for character selection.
  9721. pc_damage_log_clear(sd,0);
  9722. chrif_charselectreq(sd, session[fd]->client_addr);
  9723. } else {
  9724. clif_disconnect_ack(sd, 1);
  9725. }
  9726. break;
  9727. }
  9728. }
  9729. /// Validates and processes whispered messages (CZ_WHISPER).
  9730. /// 0096 <packet len>.W <nick>.24B <message>.?B
  9731. void clif_parse_WisMessage(int fd, map_session_data* sd)
  9732. {
  9733. map_session_data* dstsd;
  9734. int i;
  9735. char target[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9736. // validate packet and retrieve name and message
  9737. if( !clif_process_message( sd, true, target, message, output ) )
  9738. return;
  9739. // Chat logging type 'W' / Whisper
  9740. 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);
  9741. //-------------------------------------------------------//
  9742. // Lordalfa - Paperboy - To whisper NPC commands //
  9743. //-------------------------------------------------------//
  9744. if (target[0] && (strncasecmp(target,"NPC:",4) == 0) && (strlen(target) > 4))
  9745. {
  9746. char* str = target+4; //Skip the NPC: string part.
  9747. struct npc_data* npc;
  9748. if ((npc = npc_name2id(str))) {
  9749. char split_data[NUM_WHISPER_VAR][CHAT_SIZE_MAX];
  9750. char *split;
  9751. char event[EVENT_NAME_LENGTH];
  9752. str = message;
  9753. // skip codepage indicator, if detected
  9754. if( str[0] == '|' && strlen(str) >= 4 )
  9755. str += 3;
  9756. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {// Splits the message using '#' as separators
  9757. split = strchr(str,'#');
  9758. if( split == nullptr ) { // use the remaining string
  9759. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9760. for( ++i; i < NUM_WHISPER_VAR; ++i )
  9761. split_data[i][0] = '\0';
  9762. break;
  9763. }
  9764. *split = '\0';
  9765. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9766. str = split+1;
  9767. }
  9768. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {
  9769. char variablename[CHAT_SIZE_MAX];
  9770. safesnprintf(variablename,sizeof(variablename),"@whispervar%d$", i);
  9771. set_var_str( sd, variablename, split_data[i] );
  9772. }
  9773. safesnprintf(event,sizeof(event),"%s::%s", npc->exname,script_config.onwhisper_event_name);
  9774. npc_event(sd,event,0); // Calls the NPC label
  9775. return;
  9776. }
  9777. } else if( target[0] == '#' ) {
  9778. struct Channel *channel = nullptr;
  9779. char* chname = target;
  9780. channel = channel_name2channel(chname,sd,3);
  9781. if(channel && (pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) || ((channel->opt&CHAN_OPT_CAN_CHAT) && channel_pccheckgroup(channel,sd->group_id)))){
  9782. if(channel_pc_haschan(sd,channel)>=0){ //we are in the chan
  9783. channel_send(channel,sd,message);
  9784. }
  9785. else if( channel->pass[0] == '\0') { //no pass needed
  9786. if (channel_join(channel,sd)==0)
  9787. channel_send(channel,sd,message); //join success
  9788. }
  9789. else {
  9790. clif_displaymessage(fd, msg_txt(sd,1402)); //You're not in that channel, type '@join <#channel_name>'
  9791. }
  9792. return;
  9793. }
  9794. }
  9795. // searching destination character
  9796. dstsd = map_nick2sd(target,false);
  9797. if (dstsd == nullptr || strcmp(dstsd->status.name, target) != 0) {
  9798. // player is not on this map-server
  9799. // At this point, don't send wisp/page if it's not exactly the same name, because (example)
  9800. // if there are 'Test' player on an other map-server and 'test' player on this map-server,
  9801. // and if we ask for 'Test', we must not contact 'test' player
  9802. // so, we send information to inter-server, which is the only one which decide (and copy correct name).
  9803. intif_wis_message(sd, target, message, strlen(message) + 1);
  9804. return;
  9805. }
  9806. // if player ignores everyone
  9807. if (dstsd->state.ignoreAll && pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9808. if (pc_isinvisible(dstsd) && pc_get_group_level(sd) < pc_get_group_level(dstsd))
  9809. clif_wis_end( *sd, ACKWHISPER_TARGET_OFFLINE );
  9810. else
  9811. clif_wis_end( *sd, ACKWHISPER_ALL_IGNORED );
  9812. return;
  9813. }
  9814. // if player is autotrading
  9815. if (dstsd->state.autotrade == 1){
  9816. safesnprintf(output,sizeof(output),"%s is in autotrade mode and cannot receive whispered messages.", dstsd->status.name);
  9817. clif_wis_message(sd, wisp_server_name, output, strlen(output) + 1, 0);
  9818. return;
  9819. }
  9820. if (pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9821. // if player ignores the source character
  9822. ARR_FIND(0, MAX_IGNORE_LIST, i, dstsd->ignore[i].name[0] == '\0' || strcmp(dstsd->ignore[i].name, sd->status.name) == 0);
  9823. if(i < MAX_IGNORE_LIST && dstsd->ignore[i].name[0] != '\0') { // source char present in ignore list
  9824. clif_wis_end( *sd, ACKWHISPER_IGNORED );
  9825. return;
  9826. }
  9827. }
  9828. // notify sender of success
  9829. clif_wis_end( *sd, ACKWHISPER_SUCCESS );
  9830. // Normal message
  9831. clif_wis_message(dstsd, sd->status.name, message, strlen(message)+1, 0);
  9832. }
  9833. /// /b /nb (CZ_BROADCAST).
  9834. /// Request to broadcast a message on whole server.
  9835. /// 0099 <packet len>.W <text>.?B 00
  9836. void clif_parse_Broadcast(int fd, map_session_data* sd) {
  9837. char command[CHAT_SIZE_MAX+11];
  9838. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9839. unsigned int len = RFIFOW(fd,info->pos[0])-4;
  9840. char* msg = RFIFOCP(fd,info->pos[1]);
  9841. // as the length varies depending on the command used, just block unreasonably long strings
  9842. mes_len_check(msg, len, CHAT_SIZE_MAX);
  9843. safesnprintf(command,sizeof(command),"%ckami %s", atcommand_symbol, msg);
  9844. is_atcommand(fd, sd, command, 1);
  9845. }
  9846. /// Request to pick up an item.
  9847. /// 009f <id>.L (CZ_ITEM_PICKUP)
  9848. /// 0362 <id>.L (CZ_ITEM_PICKUP2)
  9849. /// There are various variants of this packet, some of them have padding between fields.
  9850. void clif_parse_TakeItem(int fd, map_session_data *sd)
  9851. {
  9852. struct flooritem_data *fitem;
  9853. int map_object_id;
  9854. map_object_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9855. fitem = (struct flooritem_data*)map_id2bl(map_object_id);
  9856. do {
  9857. if (pc_isdead(sd)) {
  9858. clif_clearunit_area( sd->bl, CLR_DEAD );
  9859. break;
  9860. }
  9861. if (fitem == nullptr || fitem->bl.type != BL_ITEM || fitem->bl.m != sd->bl.m)
  9862. break;
  9863. if (pc_cant_act(sd))
  9864. break;
  9865. if (!pc_takeitem(sd, fitem))
  9866. break;
  9867. return;
  9868. } while (0);
  9869. // Client REQUIRES a fail packet or you can no longer pick items.
  9870. clif_additem(sd,0,0,6);
  9871. }
  9872. /// Request to drop an item.
  9873. /// 00a2 <index>.W <amount>.W (CZ_ITEM_THROW)
  9874. /// 0363 <index>.W <amount>.W (CZ_ITEM_THROW2)
  9875. /// There are various variants of this packet, some of them have padding between fields.
  9876. void clif_parse_DropItem(int fd, map_session_data *sd){
  9877. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9878. int item_index = RFIFOW(fd,info->pos[0]) -2;
  9879. int item_amount = RFIFOW(fd,info->pos[1]) ;
  9880. for(;;) {
  9881. if (pc_isdead(sd))
  9882. break;
  9883. if (pc_cant_act2(sd) || sd->npc_id)
  9884. break;
  9885. if (sd->sc.cant.drop)
  9886. break;
  9887. if (!pc_dropitem(sd, item_index, item_amount))
  9888. break;
  9889. if (battle_config.idletime_option&IDLE_DROPITEM)
  9890. sd->idletime = last_tick;
  9891. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_DROPITEM)
  9892. sd->idletime_hom = last_tick;
  9893. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_DROPITEM)
  9894. sd->idletime_mer = last_tick;
  9895. return;
  9896. }
  9897. //Because the client does not like being ignored.
  9898. clif_dropitem( *sd, item_index, 0 );
  9899. }
  9900. /// Request to use an item.
  9901. /// 00a7 <index>.W <account id>.L (CZ_USE_ITEM)
  9902. /// 0439 <index>.W <account id>.L (CZ_USE_ITEM2)
  9903. /// There are various variants of this packet, some of them have padding between fields.
  9904. void clif_parse_UseItem(int fd, map_session_data *sd)
  9905. {
  9906. int n;
  9907. if (pc_isdead(sd)) {
  9908. clif_clearunit_area( sd->bl, CLR_DEAD );
  9909. return;
  9910. }
  9911. if ( (!sd->npc_id && pc_istrading(sd)) || sd->chatID || (sd->state.block_action & PCBLOCK_USEITEM) ) {
  9912. clif_msg(sd, MSI_BUSY);
  9913. return;
  9914. }
  9915. //Whether the item is used or not is irrelevant, the char ain't idle. [Skotlex]
  9916. if (battle_config.idletime_option&IDLE_USEITEM)
  9917. sd->idletime = last_tick;
  9918. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9919. sd->idletime_hom = last_tick;
  9920. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  9921. sd->idletime_mer = last_tick;
  9922. n = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  9923. if(n <0 || n >= MAX_INVENTORY)
  9924. return;
  9925. if (!pc_useitem(sd,n))
  9926. clif_useitemack(sd,n,0,false); //Send an empty ack packet or the client gets stuck.
  9927. }
  9928. /// Request to equip an item
  9929. /// 00a9 <index>.W <position>.W (CZ_REQ_WEAR_EQUIP).
  9930. /// 0998 <index>.W <position>.L (CZ_REQ_WEAR_EQUIP_V5)
  9931. void clif_parse_EquipItem(int fd,map_session_data *sd)
  9932. {
  9933. int index;
  9934. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9935. if(pc_isdead(sd)) {
  9936. clif_clearunit_area( sd->bl, CLR_DEAD );
  9937. return;
  9938. }
  9939. index = RFIFOW(fd,info->pos[0])-2;
  9940. if (index < 0 || index >= MAX_INVENTORY)
  9941. return; //Out of bounds check.
  9942. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  9943. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  9944. return;
  9945. } else if (sd->state.storage_flag || sd->sc.opt1)
  9946. ; //You can equip/unequip stuff while storage is open/under status changes
  9947. else if (pc_cant_act2(sd))
  9948. return;
  9949. if(!sd->inventory.u.items_inventory[index].identify) {
  9950. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, index ); // fail
  9951. return;
  9952. }
  9953. if(!sd->inventory_data[index])
  9954. return;
  9955. if(sd->inventory_data[index]->type == IT_PETARMOR){
  9956. pet_equipitem(sd,index);
  9957. return;
  9958. }
  9959. if (battle_config.idletime_option&IDLE_USEITEM)
  9960. sd->idletime = last_tick;
  9961. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9962. sd->idletime_hom = last_tick;
  9963. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  9964. sd->idletime_mer = last_tick;
  9965. //Client doesn't send the position for ammo.
  9966. if(sd->inventory_data[index]->type == IT_AMMO)
  9967. pc_equipitem(sd,index,EQP_AMMO);
  9968. else {
  9969. int req_pos;
  9970. #if PACKETVER >= 20120925
  9971. req_pos = RFIFOL(fd,info->pos[1]);
  9972. #else
  9973. req_pos = (int)RFIFOW(fd,info->pos[1]);
  9974. #endif
  9975. pc_equipitem(sd,index,req_pos);
  9976. }
  9977. }
  9978. /// Request to take off an equip (CZ_REQ_TAKEOFF_EQUIP).
  9979. /// 00ab <index>.W
  9980. void clif_parse_UnequipItem(int fd,map_session_data *sd)
  9981. {
  9982. int index;
  9983. if(pc_isdead(sd)) {
  9984. clif_clearunit_area( sd->bl, CLR_DEAD );
  9985. return;
  9986. }
  9987. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  9988. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  9989. return;
  9990. } else if (sd->state.storage_flag || sd->sc.opt1)
  9991. ; //You can equip/unequip stuff while storage is open/under status changes
  9992. else if (pc_cant_act2(sd))
  9993. return;
  9994. index = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  9995. if (battle_config.idletime_option&IDLE_USEITEM)
  9996. sd->idletime = last_tick;
  9997. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9998. sd->idletime_hom = last_tick;
  9999. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  10000. sd->idletime_mer = last_tick;
  10001. pc_unequipitem(sd,index,1);
  10002. }
  10003. /// Request to start a conversation with an NPC (CZ_CONTACTNPC).
  10004. /// 0090 <id>.L <type>.B
  10005. /// type:
  10006. /// 1 = click
  10007. void clif_parse_NpcClicked(int fd,map_session_data *sd)
  10008. {
  10009. if( sd == nullptr ){
  10010. return;
  10011. }
  10012. struct block_list *bl;
  10013. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10014. if(pc_isdead(sd)) {
  10015. clif_clearunit_area( sd->bl, CLR_DEAD );
  10016. return;
  10017. }
  10018. if( pc_cant_act2(sd) || sd->npc_id || pc_hasprogress( sd, WIP_DISABLE_NPC ) ){
  10019. #ifdef RENEWAL
  10020. clif_msg( sd, MSI_BUSY);
  10021. #endif
  10022. return;
  10023. }
  10024. if( sd->state.mail_writing )
  10025. return;
  10026. bl = map_id2bl(RFIFOL(fd,info->pos[0]));
  10027. //type = RFIFOB(fd,info->pos[1]);
  10028. if (!bl) return;
  10029. switch (bl->type) {
  10030. case BL_MOB:
  10031. case BL_PC:
  10032. clif_parse_ActionRequest_sub( *sd, DMG_REPEAT, bl->id, gettick() );
  10033. break;
  10034. case BL_NPC:
  10035. #ifdef RENEWAL
  10036. 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
  10037. clif_msg(sd, MSI_BUSY);
  10038. break;
  10039. }
  10040. #endif
  10041. if( bl->m != -1 ){ // the user can't click floating npcs directly (hack attempt)
  10042. struct npc_data* nd = (struct npc_data*)bl;
  10043. // Progressbar is running
  10044. if( nd->progressbar.timeout > 0 ){
  10045. return;
  10046. }
  10047. npc_click(sd,nd);
  10048. }
  10049. break;
  10050. }
  10051. }
  10052. /// Selection between buy/sell was made (CZ_ACK_SELECT_DEALTYPE).
  10053. /// 00c5 <id>.L <type>.B
  10054. /// type:
  10055. /// 0 = buy
  10056. /// 1 = sell
  10057. void clif_parse_NpcBuySellSelected(int fd,map_session_data *sd)
  10058. {
  10059. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10060. if (sd->state.trading)
  10061. return;
  10062. npc_buysellsel(sd,RFIFOL(fd,info->pos[0]),RFIFOB(fd,info->pos[1]));
  10063. }
  10064. /// Notification about the result of a purchase attempt from an NPC shop (ZC_PC_PURCHASE_RESULT).
  10065. /// 00ca <result>.B
  10066. /// result:
  10067. /// 0 = "The deal has successfully completed."
  10068. /// 1 = "You do not have enough zeny."
  10069. /// 2 = "You are over your Weight Limit."
  10070. /// 3 = "Out of the maximum capacity, you have too many items."
  10071. /// 9 = "Amounts are exceeded the possession of the item is not available for purchase."
  10072. /// 10 = "Props open-air store sales will be traded in RODEX"
  10073. /// 11 = "The exchange failed."
  10074. /// 12 = "The exchange was well done."
  10075. /// 13 = "The item is already sold and out of stock."
  10076. /// 14 = "There is not enough goods to exchange."
  10077. void clif_npc_buy_result( map_session_data* sd, e_purchase_result result ){
  10078. PACKET_ZC_PC_PURCHASE_RESULT p = {};
  10079. p.packetType = HEADER_ZC_PC_PURCHASE_RESULT;
  10080. p.result = (uint8)result;
  10081. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  10082. }
  10083. /// Request to buy chosen items from npc shop.
  10084. /// 00c8 <packet len>.W { <amount>.W <name id>.W }* (CZ_PC_PURCHASE_ITEMLIST)
  10085. void clif_parse_NpcBuyListSend( int fd, map_session_data* sd ){
  10086. const PACKET_CZ_PC_PURCHASE_ITEMLIST *p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10087. uint16 n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->items[0] );
  10088. e_purchase_result result;
  10089. if( sd->state.trading || !sd->npc_shopid )
  10090. result = e_purchase_result::PURCHASE_FAIL_MONEY;
  10091. else{
  10092. std::vector<s_npc_buy_list> items = {};
  10093. items.reserve( n );
  10094. for( uint16 i = 0; i < n; i++ ){
  10095. s_npc_buy_list item = {};
  10096. item.nameid = p->items[i].itemId;
  10097. item.qty = p->items[i].amount;
  10098. items.push_back( item );
  10099. }
  10100. result = npc_buylist( sd, items );
  10101. }
  10102. sd->npc_shopid = 0; //Clear shop data.
  10103. clif_npc_buy_result(sd, result);
  10104. }
  10105. /// Notification about the result of a sell attempt to an NPC shop (ZC_PC_SELL_RESULT).
  10106. /// 00cb <result>.B
  10107. /// result:
  10108. /// 0 = "The deal has successfully completed."
  10109. /// 1 = "The deal has failed."
  10110. void clif_npc_sell_result(map_session_data* sd, unsigned char result)
  10111. {
  10112. int fd = sd->fd;
  10113. WFIFOHEAD(fd,packet_len(0xcb));
  10114. WFIFOW(fd,0) = 0xcb;
  10115. WFIFOB(fd,2) = result;
  10116. WFIFOSET(fd,packet_len(0xcb));
  10117. }
  10118. /// Request to sell chosen items to npc shop (CZ_PC_SELL_ITEMLIST).
  10119. /// 00c9 <packet len>.W { <index>.W <amount>.W }*
  10120. void clif_parse_NpcSellListSend(int fd,map_session_data *sd)
  10121. {
  10122. int fail=0;
  10123. const PACKET_CZ_PC_SELL_ITEMLIST* p = reinterpret_cast<PACKET_CZ_PC_SELL_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10124. int n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->sellList[0] );
  10125. if (sd->state.trading || !sd->npc_shopid)
  10126. fail = 1;
  10127. else
  10128. fail = npc_selllist(sd, n, p->sellList);
  10129. sd->npc_shopid = 0; //Clear shop data.
  10130. clif_npc_sell_result(sd, fail);
  10131. }
  10132. /// Chatroom creation request (CZ_CREATE_CHATROOM).
  10133. /// 00d5 <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B
  10134. /// type:
  10135. /// 0 = private
  10136. /// 1 = public
  10137. void clif_parse_CreateChatRoom(int fd, map_session_data* sd)
  10138. {
  10139. if( sd == nullptr ){
  10140. return;
  10141. }
  10142. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10143. int len = RFIFOW(fd,info->pos[0])-15;
  10144. int limit = RFIFOW(fd,info->pos[1]);
  10145. bool pub = (RFIFOB(fd,info->pos[2]) != 0);
  10146. const char* password = RFIFOCP(fd,info->pos[3]); //not zero-terminated
  10147. const char* title = RFIFOCP(fd,info->pos[4]); // not zero-terminated
  10148. char s_password[CHATROOM_PASS_SIZE];
  10149. char s_title[CHATROOM_TITLE_SIZE];
  10150. if (sd->sc.getSCE(SC_NOCHAT) && sd->sc.getSCE(SC_NOCHAT)->val1&MANNER_NOROOM)
  10151. return;
  10152. if(battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 4 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10153. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 3 );
  10154. return;
  10155. }
  10156. if( npc_isnear(&sd->bl) ) {
  10157. // uncomment to send msg_txt.
  10158. //char output[150];
  10159. //sprintf(output, msg_txt(662), battle_config.min_npc_vendchat_distance);
  10160. //clif_displaymessage(sd->fd, output);
  10161. clif_skill_fail( *sd, 1, USESKILL_FAIL_THERE_ARE_NPC_AROUND );
  10162. return;
  10163. }
  10164. if( len <= 0 )
  10165. return; // invalid input
  10166. safestrncpy(s_password, password, CHATROOM_PASS_SIZE);
  10167. safestrncpy(s_title, title, min(len+1,CHATROOM_TITLE_SIZE)); //NOTE: assumes that safestrncpy will not access the len+1'th byte
  10168. chat_createpcchat(sd, s_title, s_password, limit, pub);
  10169. }
  10170. /// Chatroom join request (CZ_REQ_ENTER_ROOM).
  10171. /// 00d9 <chat ID>.L <passwd>.8B
  10172. void clif_parse_ChatAddMember(int fd, map_session_data* sd){
  10173. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10174. int chatid = RFIFOL(fd,info->pos[0]);
  10175. const char* password = RFIFOCP(fd,info->pos[1]); // not zero-terminated
  10176. chat_joinchat(sd,chatid,password);
  10177. }
  10178. /// Chatroom properties adjustment request (CZ_CHANGE_CHATROOM).
  10179. /// 00de <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B
  10180. /// type:
  10181. /// 0 = private
  10182. /// 1 = public
  10183. void clif_parse_ChatRoomStatusChange(int fd, map_session_data* sd){
  10184. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10185. int len = RFIFOW(fd,info->pos[0])-15;
  10186. int limit = RFIFOW(fd,info->pos[1]);
  10187. bool pub = (RFIFOB(fd,info->pos[2]) != 0);
  10188. const char* password = RFIFOCP(fd,info->pos[3]); // not zero-terminated
  10189. const char* title = RFIFOCP(fd,info->pos[4]); // not zero-terminated
  10190. char s_password[CHATROOM_PASS_SIZE];
  10191. char s_title[CHATROOM_TITLE_SIZE];
  10192. if( len <= 0 )
  10193. return; // invalid input
  10194. safestrncpy(s_password, password, CHATROOM_PASS_SIZE);
  10195. safestrncpy(s_title, title, min(len+1,CHATROOM_TITLE_SIZE)); //NOTE: assumes that safestrncpy will not access the len+1'th byte
  10196. chat_changechatstatus(sd, s_title, s_password, limit, pub);
  10197. }
  10198. /// Request to change the chat room ownership (CZ_REQ_ROLE_CHANGE).
  10199. /// 00e0 <role>.L <nick>.24B
  10200. /// role:
  10201. /// 0 = owner
  10202. /// 1 = normal
  10203. void clif_parse_ChangeChatOwner(int fd, map_session_data* sd)
  10204. {
  10205. //int role = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10206. chat_changechatowner(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]));
  10207. }
  10208. /// Request to expel a player from chat room (CZ_REQ_EXPEL_MEMBER).
  10209. /// 00e2 <name>.24B
  10210. void clif_parse_KickFromChat(int fd,map_session_data *sd)
  10211. {
  10212. chat_kickchat(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10213. }
  10214. /// Request to leave the current chatroom (CZ_EXIT_ROOM).
  10215. /// 00e3
  10216. void clif_parse_ChatLeave(int fd, map_session_data* sd)
  10217. {
  10218. chat_leavechat(sd,0);
  10219. }
  10220. // Handles notifying asker and rejecter of what has just ocurred.
  10221. // Type is used to determine the correct msg_txt to use
  10222. void clif_noask_sub( map_session_data& sd, map_session_data& tsd, int type ){
  10223. char output[CHAT_SIZE_MAX];
  10224. // Your request has been rejected by autoreject option.
  10225. clif_messagecolor( &sd.bl, color_table[COLOR_LIGHT_GREEN], msg_txt( &sd, 392 ), false, SELF );
  10226. // Notice that a request was rejected.
  10227. safesnprintf( output, CHAT_SIZE_MAX, msg_txt( &tsd, type ), sd.status.name );
  10228. clif_messagecolor( &tsd.bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  10229. }
  10230. /// Request to begin a trade (CZ_REQ_EXCHANGE_ITEM).
  10231. /// 00e4 <account id>.L
  10232. void clif_parse_TradeRequest(int fd,map_session_data *sd)
  10233. {
  10234. map_session_data *t_sd;
  10235. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10236. if(!sd->chatID && pc_cant_act(sd))
  10237. return; //You can trade while in a chatroom.
  10238. if(t_sd){
  10239. // @noask [LuzZza]
  10240. if(t_sd->state.noask) {
  10241. clif_noask_sub( *sd, *t_sd, 393 ); // Autorejected trade request from %s.
  10242. return;
  10243. }
  10244. if (t_sd->state.mail_writing) {
  10245. // Fake trading
  10246. clif_traderesponse(*sd,TRADE_ACK_BUSY);
  10247. return;
  10248. }
  10249. }
  10250. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 1 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10251. clif_skill_fail( *sd, 1 );
  10252. return;
  10253. }
  10254. trade_traderequest(sd,t_sd);
  10255. }
  10256. /// Answer to a trade request (CZ_ACK_EXCHANGE_ITEM).
  10257. /// 00e6 <result>.B
  10258. /// result:
  10259. /// 3 = accepted
  10260. /// 4 = rejected
  10261. void clif_parse_TradeAck(int fd,map_session_data *sd)
  10262. {
  10263. trade_tradeack(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10264. }
  10265. /// Request to add an item to current trade (CZ_ADD_EXCHANGE_ITEM).
  10266. /// 00e8 <index>.W <amount>.L
  10267. void clif_parse_TradeAddItem(int fd,map_session_data *sd)
  10268. {
  10269. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10270. short index = RFIFOW(fd,info->pos[0]);
  10271. int amount = RFIFOL(fd,info->pos[1]);
  10272. if( index == 0 )
  10273. trade_tradeaddzeny(sd, amount);
  10274. else
  10275. trade_tradeadditem(sd, server_index(index), (short)amount);
  10276. }
  10277. /// Request to lock items in current trade (CZ_CONCLUDE_EXCHANGE_ITEM).
  10278. /// 00eb
  10279. void clif_parse_TradeOk(int fd,map_session_data *sd)
  10280. {
  10281. trade_tradeok(sd);
  10282. }
  10283. /// Request to cancel current trade (CZ_CANCEL_EXCHANGE_ITEM).
  10284. /// 00ed
  10285. void clif_parse_TradeCancel(int fd,map_session_data *sd)
  10286. {
  10287. trade_tradecancel(sd);
  10288. }
  10289. /// Request to commit current trade (CZ_EXEC_EXCHANGE_ITEM).
  10290. /// 00ef
  10291. void clif_parse_TradeCommit(int fd,map_session_data *sd)
  10292. {
  10293. trade_tradecommit(sd);
  10294. }
  10295. /// Request to stop chasing/attacking an unit (CZ_CANCEL_LOCKON).
  10296. /// 0118
  10297. void clif_parse_StopAttack(int fd,map_session_data *sd)
  10298. {
  10299. pc_stop_attack(sd);
  10300. if (sd) sd->ud.state.attack_continue = 0;
  10301. }
  10302. /// Request to move an item from inventory to cart.
  10303. /// 0126 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_CART)
  10304. void clif_parse_PutItemToCart( int fd, map_session_data *sd ){
  10305. if (pc_istrading(sd) || !pc_iscarton(sd) || pc_cant_act2(sd))
  10306. return;
  10307. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10308. return;
  10309. const PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART* p = reinterpret_cast<PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART*>( RFIFOP( fd, 0 ) );
  10310. pc_putitemtocart( sd, server_index( p->index ), p->count);
  10311. }
  10312. /// Request to move an item from cart to inventory (CZ_MOVE_ITEM_FROM_CART_TO_BODY).
  10313. /// 0127 <index>.W <amount>.L
  10314. void clif_parse_GetItemFromCart(int fd,map_session_data *sd)
  10315. {
  10316. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10317. if (!pc_iscarton(sd) || pc_cant_act2(sd))
  10318. return;
  10319. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10320. return;
  10321. pc_getitemfromcart(sd,RFIFOW(fd,info->pos[0])-2,RFIFOL(fd,info->pos[1]));
  10322. }
  10323. /// Request to remove cart/falcon/peco/dragon/mado (CZ_REQ_CARTOFF).
  10324. /// 012a
  10325. void clif_parse_RemoveOption(int fd,map_session_data *sd)
  10326. {
  10327. if( !(sd->sc.option&(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR))
  10328. #ifdef NEW_CARTS
  10329. && sd->sc.getSCE(SC_PUSH_CART) )
  10330. pc_setcart(sd,0);
  10331. #else
  10332. )
  10333. pc_setoption(sd,sd->sc.option&~OPTION_CART);
  10334. #endif
  10335. else // priority to remove this option before we can clear cart
  10336. pc_setoption(sd,sd->sc.option&~(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR));
  10337. }
  10338. /// Request to select cart's visual look for new cart design (ZC_SELECTCART).
  10339. /// 097f <Length>.W <identity>.L <type>.B
  10340. void clif_SelectCart(map_session_data *sd) {
  10341. #if PACKETVER >= 20150826
  10342. int i = 0, carts = 3;
  10343. int fd = sd->fd;
  10344. WFIFOHEAD(fd,8 + carts);
  10345. WFIFOW(fd,0) = 0x97f;
  10346. WFIFOW(fd,2) = 8 + carts;
  10347. WFIFOL(fd,4) = sd->status.account_id;
  10348. // Right now we have 10-12, tested it you can also enable selection for all cart styles here(1-12)
  10349. for( i = 0; i < carts; i++ ) {
  10350. WFIFOB(fd,8 + i) = 10 + i;
  10351. }
  10352. WFIFOSET(fd,8 + carts);
  10353. #endif
  10354. }
  10355. /// Request to select cart's visual look for new cart design (CZ_SELECTCART).
  10356. /// 0980 <identity>.L <type>.B
  10357. void clif_parse_SelectCart(int fd,map_session_data *sd) {
  10358. #if PACKETVER >= 20150826
  10359. int type;
  10360. // Check identity
  10361. if( !sd || pc_checkskill(sd,MC_CARTDECORATE) < 1 || RFIFOL(fd,2) != sd->status.account_id )
  10362. return;
  10363. type = (int)RFIFOB(fd,6);
  10364. // Check type
  10365. if( type < 10 || type > 12 )
  10366. return;
  10367. pc_setcart(sd, type);
  10368. #endif
  10369. }
  10370. /// Request to change cart's visual look (CZ_REQ_CHANGECART).
  10371. /// 01af <num>.W
  10372. void clif_parse_ChangeCart(int fd,map_session_data *sd)
  10373. {// TODO: State tracking?
  10374. int type;
  10375. if( !sd || pc_checkskill(sd, MC_CHANGECART) < 1 )
  10376. return;
  10377. #ifdef RENEWAL
  10378. if (sd->npc_id || pc_hasprogress(sd, WIP_DISABLE_SKILLITEM)) {
  10379. clif_msg(sd, MSI_BUSY);
  10380. return;
  10381. }
  10382. #endif
  10383. type = (int)RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10384. if(
  10385. #ifdef NEW_CARTS
  10386. #if PACKETVER >= 20191106
  10387. (type == 13 && sd->status.base_level > 100) ||
  10388. #endif
  10389. (type == 9 && sd->status.base_level > 130) ||
  10390. (type == 8 && sd->status.base_level > 120) ||
  10391. (type == 7 && sd->status.base_level > 110) ||
  10392. (type == 6 && sd->status.base_level > 100) ||
  10393. #endif
  10394. (type == 5 && sd->status.base_level > 90) ||
  10395. (type == 4 && sd->status.base_level > 80) ||
  10396. (type == 3 && sd->status.base_level > 65) ||
  10397. (type == 2 && sd->status.base_level > 40) ||
  10398. (type == 1))
  10399. pc_setcart(sd, type);
  10400. }
  10401. /// Request to increase status (CZ_STATUS_CHANGE).
  10402. /// 00bb <status id>.W <amount>.B
  10403. /// status id:
  10404. /// SP_STR ~ SP_LUK
  10405. /// amount:
  10406. /// Old clients always send 1 for this, even when using /str+ and the like.
  10407. /// Newer clients (2013-12-23 and newer) send the correct amount.
  10408. void clif_parse_StatusUp(int fd,map_session_data *sd)
  10409. {
  10410. int increase_amount = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  10411. if( increase_amount < 0 ) {
  10412. ShowDebug("clif_parse_StatusUp: Negative 'increase' value sent by client! (fd: %d, value: %d)\n",
  10413. fd, increase_amount);
  10414. }
  10415. pc_statusup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]),increase_amount);
  10416. }
  10417. /// Request to increase trait status.
  10418. /// 0b24 <status id>.W <amount>.W (CZ_ADVANCED_STATUS_CHANGE)
  10419. /// status id:
  10420. /// SP_POW ~ SP_CON
  10421. /// amount:
  10422. /// The amount to increase the trait status
  10423. void clif_parse_traitstatus_up( int fd, map_session_data *sd ){
  10424. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10425. const PACKET_CZ_ADVANCED_STATUS_CHANGE* p = reinterpret_cast<PACKET_CZ_ADVANCED_STATUS_CHANGE*>( RFIFOP( fd, 0 ) );
  10426. if( p->amount < 0 ){
  10427. 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 );
  10428. return;
  10429. }
  10430. pc_traitstatusup( sd, p->type, p->amount );
  10431. #endif
  10432. }
  10433. /// Request to increase level of a skill (CZ_UPGRADE_SKILLLEVEL).
  10434. /// 0112 <skill id>.W
  10435. void clif_parse_SkillUp(int fd,map_session_data *sd)
  10436. {
  10437. pc_skillup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10438. }
  10439. 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)
  10440. {
  10441. int lv;
  10442. if( !hd )
  10443. return;
  10444. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10445. clif_emotion(&hd->bl, ET_THINK);
  10446. return;
  10447. }
  10448. if( hd->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10449. target_id = hd->bl.id;
  10450. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10451. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10452. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10453. clif_emotion(&hd->bl, ET_THINK);
  10454. if (hd->master)
  10455. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10456. return;
  10457. }
  10458. lv = hom_checkskill(hd, skill_id);
  10459. if( skill_lv > lv )
  10460. skill_lv = lv;
  10461. if( skill_lv )
  10462. unit_skilluse_id(&hd->bl, target_id, skill_id, skill_lv);
  10463. }
  10464. 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)
  10465. {
  10466. int lv;
  10467. if( !hd )
  10468. return;
  10469. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10470. clif_emotion(&hd->bl, ET_THINK);
  10471. return;
  10472. }
  10473. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10474. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10475. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10476. clif_emotion(&hd->bl, ET_THINK);
  10477. if (hd->master)
  10478. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10479. return;
  10480. }
  10481. #ifdef RENEWAL
  10482. if (hd->sc.getSCE(SC_BASILICA_CELL))
  10483. #else
  10484. if (hd->sc.getSCE(SC_BASILICA))
  10485. #endif
  10486. return;
  10487. lv = hom_checkskill(hd, skill_id);
  10488. if( skill_lv > lv )
  10489. skill_lv = lv;
  10490. if( skill_lv )
  10491. unit_skilluse_pos(&hd->bl, x, y, skill_id, skill_lv);
  10492. }
  10493. 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)
  10494. {
  10495. int lv;
  10496. if( !md )
  10497. return;
  10498. if( skill_isNotOk_mercenary(skill_id, *md) )
  10499. return;
  10500. if( md->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10501. target_id = md->bl.id;
  10502. if( md->ud.skilltimer != INVALID_TIMER )
  10503. {
  10504. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10505. }
  10506. else if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10507. return;
  10508. lv = mercenary_checkskill(md, skill_id);
  10509. if( skill_lv > lv )
  10510. skill_lv = lv;
  10511. if( skill_lv )
  10512. unit_skilluse_id(&md->bl, target_id, skill_id, skill_lv);
  10513. }
  10514. 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)
  10515. {
  10516. int lv;
  10517. if( !md )
  10518. return;
  10519. if( skill_isNotOk_mercenary(skill_id, *md) )
  10520. return;
  10521. if( md->ud.skilltimer != INVALID_TIMER )
  10522. return;
  10523. if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10524. {
  10525. if (md->master)
  10526. clif_skill_fail( *md->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10527. return;
  10528. }
  10529. #ifdef RENEWAL
  10530. if (md->sc.getSCE(SC_BASILICA_CELL))
  10531. #else
  10532. if (md->sc.getSCE(SC_BASILICA))
  10533. #endif
  10534. return;
  10535. lv = mercenary_checkskill(md, skill_id);
  10536. if( skill_lv > lv )
  10537. skill_lv = lv;
  10538. if( skill_lv )
  10539. unit_skilluse_pos(&md->bl, x, y, skill_id, skill_lv);
  10540. }
  10541. void clif_parse_skill_toid( map_session_data* sd, uint16 skill_id, uint16 skill_lv, int target_id ){
  10542. if( sd == nullptr ){
  10543. return;
  10544. }
  10545. t_tick tick = gettick();
  10546. if( skill_lv < 1 ) skill_lv = 1; //No clue, I have seen the client do this with guild skills :/ [Skotlex]
  10547. int inf = skill_get_inf(skill_id);
  10548. if (inf&INF_GROUND_SKILL || !inf)
  10549. return; //Using a ground/passive skill on a target? WRONG.
  10550. if (sd->state.block_action & PCBLOCK_SKILL) {
  10551. clif_msg(sd, MSI_BUSY);
  10552. return;
  10553. }
  10554. if( SKILL_CHK_HOMUN(skill_id) ) {
  10555. clif_parse_UseSkillToId_homun(sd->hd, sd, tick, skill_id, skill_lv, target_id);
  10556. return;
  10557. }
  10558. if( SKILL_CHK_MERC(skill_id) ) {
  10559. clif_parse_UseSkillToId_mercenary(sd->md, sd, tick, skill_id, skill_lv, target_id);
  10560. return;
  10561. }
  10562. // Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10563. // This is done here, because homunculi and mercenaries can be triggered by AI and not by the player itself
  10564. if (battle_config.idletime_option&IDLE_USESKILLTOID)
  10565. sd->idletime = last_tick;
  10566. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USESKILLTOID)
  10567. sd->idletime_hom = last_tick;
  10568. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USESKILLTOID)
  10569. sd->idletime_mer = last_tick;
  10570. if( sd->npc_id ){
  10571. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag || !( inf & INF_SELF_SKILL ) ){
  10572. #ifdef RENEWAL
  10573. clif_msg( sd, MSI_BUSY);
  10574. #endif
  10575. return;
  10576. }
  10577. }
  10578. if ((pc_cant_act2(sd) || sd->chatID) &&
  10579. skill_id != RK_REFRESH &&
  10580. !( ( skill_id == SR_GENTLETOUCH_CURE || skill_id == SU_GROOMING ) && (sd->sc.opt1 == OPT1_STONE || sd->sc.opt1 == OPT1_FREEZE || sd->sc.opt1 == OPT1_STUN)) &&
  10581. !(sd->state.storage_flag && (inf&INF_SELF_SKILL))) //SELF skills can be used with the storage open, issue: 8027
  10582. return;
  10583. if( pc_issit(sd) )
  10584. return;
  10585. if( skill_isNotOk(skill_id, *sd) )
  10586. return;
  10587. if( sd->bl.id != target_id && inf&INF_SELF_SKILL )
  10588. target_id = sd->bl.id; // never trust the client
  10589. if( target_id < 0 && -target_id == sd->bl.id ) // for disguises [Valaris]
  10590. target_id = sd->bl.id;
  10591. if( sd->ud.skilltimer != INVALID_TIMER ) {
  10592. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST )
  10593. return;
  10594. } else if( DIFF_TICK(tick, sd->ud.canact_tick) < 0 ) {
  10595. if( sd->skillitem != skill_id ) {
  10596. clif_skill_fail( *sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10597. return;
  10598. }
  10599. }
  10600. if( sd->sc.option&OPTION_COSTUME )
  10601. return;
  10602. #ifndef RENEWAL
  10603. if( sd->sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd->sc.getSCE(SC_BASILICA)->val4 != sd->bl.id) )
  10604. return; // On basilica only caster can use Basilica again to stop it.
  10605. #endif
  10606. if( sd->menuskill_id ) {
  10607. if( sd->menuskill_id == SA_TAMINGMONSTER ) {
  10608. clif_menuskill_clear(sd); //Cancel pet capture.
  10609. }else if( sd->menuskill_id == SG_FEEL ){
  10610. clif_menuskill_clear( sd ); // Cancel selection
  10611. }else if( sd->menuskill_id != SA_AUTOSPELL )
  10612. return; //Can't use skills while a menu is open.
  10613. }
  10614. if( sd->skillitem == skill_id ) {
  10615. if( skill_lv != sd->skillitemlv )
  10616. skill_lv = sd->skillitemlv;
  10617. if( !(inf&INF_SELF_SKILL) )
  10618. pc_delinvincibletimer(sd); // Target skills thru items cancel invincibility. [Inkfish]
  10619. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10620. return;
  10621. }
  10622. sd->skillitem = sd->skillitemlv = 0;
  10623. if( SKILL_CHK_GUILD(skill_id) ) {
  10624. if (sd->guild != nullptr && (sd->state.gmaster_flag || skill_id == GD_CHARGESHOUT_BEATING))
  10625. skill_lv = guild_checkskill(sd->guild->guild, skill_id);
  10626. else
  10627. skill_lv = 0;
  10628. } else {
  10629. if( skill_id != ALL_EQSWITCH ){
  10630. skill_lv = min(pc_checkskill(sd, skill_id),skill_lv); //never trust client
  10631. }
  10632. }
  10633. pc_delinvincibletimer(sd);
  10634. if( skill_lv )
  10635. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10636. }
  10637. /// Request to use a targeted skill.
  10638. /// 0113 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL)
  10639. /// 0438 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL2)
  10640. /// There are various variants of this packet, some of them have padding between fields.
  10641. void clif_parse_UseSkillToId( int fd, map_session_data *sd ){
  10642. struct s_packet_db* info = &packet_db[RFIFOW(fd, 0)];
  10643. clif_parse_skill_toid( sd, RFIFOW(fd, info->pos[1]), RFIFOW(fd, info->pos[0]), RFIFOL(fd, info->pos[2]) );
  10644. }
  10645. /*==========================================
  10646. * Client tells server he'd like to use AoE skill id 'skill_id' of level 'skill_lv' on 'x','y' location
  10647. *------------------------------------------*/
  10648. static void clif_parse_UseSkillToPosSub( int fd, map_session_data& sd, uint16 skill_lv, uint16 skill_id, short x, short y, int skillmoreinfo ){
  10649. t_tick tick = gettick();
  10650. if( !(skill_get_inf(skill_id)&INF_GROUND_SKILL) )
  10651. return; //Using a target skill on the ground? WRONG.
  10652. if (sd.state.block_action & PCBLOCK_SKILL) {
  10653. clif_msg(&sd, MSI_BUSY);
  10654. return;
  10655. }
  10656. if( SKILL_CHK_HOMUN(skill_id) ) {
  10657. clif_parse_UseSkillToPos_homun(sd.hd, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10658. return;
  10659. }
  10660. if( SKILL_CHK_MERC(skill_id) ) {
  10661. clif_parse_UseSkillToPos_mercenary(sd.md, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10662. return;
  10663. }
  10664. if( pc_hasprogress( &sd, WIP_DISABLE_SKILLITEM ) ){
  10665. #ifdef RENEWAL
  10666. clif_msg( &sd, MSI_BUSY);
  10667. #endif
  10668. return;
  10669. }
  10670. //Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10671. if (battle_config.idletime_option&IDLE_USESKILLTOPOS)
  10672. sd.idletime = last_tick;
  10673. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_USESKILLTOPOS)
  10674. sd.idletime_hom = last_tick;
  10675. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_USESKILLTOPOS)
  10676. sd.idletime_mer = last_tick;
  10677. if( skill_isNotOk(skill_id, sd) )
  10678. return;
  10679. if( skillmoreinfo != -1 ) {
  10680. if( pc_issit(&sd) ) {
  10681. clif_skill_fail( sd, skill_id );
  10682. return;
  10683. }
  10684. //You can't use Graffiti/TalkieBox AND have a vending open, so this is safe.
  10685. safestrncpy(sd.message, RFIFOCP(fd,skillmoreinfo), MESSAGE_SIZE);
  10686. }
  10687. if( sd.ud.skilltimer != INVALID_TIMER )
  10688. return;
  10689. if( DIFF_TICK(tick, sd.ud.canact_tick) < 0 ) {
  10690. if( sd.skillitem != skill_id ) {
  10691. clif_skill_fail( sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10692. return;
  10693. }
  10694. }
  10695. if( sd.sc.option&OPTION_COSTUME )
  10696. return;
  10697. #ifndef RENEWAL
  10698. if( sd.sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd.sc.getSCE(SC_BASILICA)->val4 != sd.bl.id) )
  10699. return; // On basilica only caster can use Basilica again to stop it.
  10700. #endif
  10701. if( sd.menuskill_id ) {
  10702. if( sd.menuskill_id == SA_TAMINGMONSTER ) {
  10703. clif_menuskill_clear(&sd); //Cancel pet capture.
  10704. }else if( sd.menuskill_id == SG_FEEL ){
  10705. clif_menuskill_clear( &sd ); // Cancel selection
  10706. } else if( sd.menuskill_id != SA_AUTOSPELL )
  10707. return; //Can't use skills while a menu is open.
  10708. }
  10709. pc_delinvincibletimer(&sd);
  10710. if( sd.skillitem == skill_id ) {
  10711. if( skill_lv != sd.skillitemlv )
  10712. skill_lv = sd.skillitemlv;
  10713. unit_skilluse_pos(&sd.bl, x, y, skill_id, skill_lv);
  10714. } else {
  10715. int lv;
  10716. sd.skillitem = sd.skillitemlv = 0;
  10717. if( (lv = pc_checkskill(&sd, skill_id)) > 0 ) {
  10718. if( skill_lv > lv )
  10719. skill_lv = lv;
  10720. unit_skilluse_pos(&sd.bl, x, y, skill_id,skill_lv);
  10721. }
  10722. }
  10723. }
  10724. /// Request to use a ground skill.
  10725. /// 0116 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND)
  10726. /// 0366 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND2)
  10727. /// 0AF4 <skill lv>.W <skill id>.W <x>.W <y>.W <unknown>.B (CZ_USE_SKILL_TOGROUND3)
  10728. /// There are various variants of this packet, some of them have padding between fields.
  10729. void clif_parse_UseSkillToPos(int fd, map_session_data *sd)
  10730. {
  10731. if( sd == nullptr ){
  10732. return;
  10733. }
  10734. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10735. if (pc_cant_act(sd))
  10736. return;
  10737. if (pc_issit(sd))
  10738. return;
  10739. clif_parse_UseSkillToPosSub(fd, *sd,
  10740. RFIFOW(fd,info->pos[0]), //skill lv
  10741. RFIFOW(fd,info->pos[1]), //skill num
  10742. RFIFOW(fd,info->pos[2]), //pos x
  10743. RFIFOW(fd,info->pos[3]), //pos y
  10744. // TODO: find out what this is intended to do
  10745. //RFIFOB(fd, info->pos[4])
  10746. -1 //Skill more info.
  10747. );
  10748. }
  10749. /// Request to use a ground skill with text.
  10750. /// 0190 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX)
  10751. /// 0367 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX2)
  10752. /// There are various variants of this packet, some of them have padding between fields.
  10753. void clif_parse_UseSkillToPosMoreInfo(int fd, map_session_data *sd)
  10754. {
  10755. if( sd == nullptr ){
  10756. return;
  10757. }
  10758. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10759. if (pc_cant_act(sd))
  10760. return;
  10761. if (pc_issit(sd))
  10762. return;
  10763. clif_parse_UseSkillToPosSub(fd, *sd,
  10764. RFIFOW(fd,info->pos[0]), //Skill lv
  10765. RFIFOW(fd,info->pos[1]), //Skill num
  10766. RFIFOW(fd,info->pos[2]), //pos x
  10767. RFIFOW(fd,info->pos[3]), //pos y
  10768. info->pos[4] //skill more info
  10769. );
  10770. }
  10771. /// Answer to map selection dialog (CZ_SELECT_WARPPOINT).
  10772. /// 011b <skill id>.W <map name>.16B
  10773. void clif_parse_UseSkillMap(int fd, map_session_data* sd)
  10774. {
  10775. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10776. uint16 skill_id = RFIFOW(fd,info->pos[0]);
  10777. char map_name[MAP_NAME_LENGTH];
  10778. mapindex_getmapname(RFIFOCP(fd,info->pos[1]), map_name);
  10779. sd->state.workinprogress = WIP_DISABLE_NONE;
  10780. if(skill_id != sd->menuskill_id)
  10781. return;
  10782. //It is possible to use teleport with the storage window open bugreport:8027
  10783. if (pc_cant_act(sd) && !sd->state.storage_flag && skill_id != AL_TELEPORT) {
  10784. clif_menuskill_clear(sd);
  10785. return;
  10786. }
  10787. pc_delinvincibletimer(sd);
  10788. skill_castend_map(sd,skill_id,map_name);
  10789. }
  10790. /// Request to set a memo on current map (CZ_REMEMBER_WARPPOINT).
  10791. /// 011d
  10792. void clif_parse_RequestMemo(int fd,map_session_data *sd)
  10793. {
  10794. if (!pc_isdead(sd))
  10795. pc_memo(sd,-1);
  10796. }
  10797. /// Answer to pharmacy item selection dialog.
  10798. /// 018e <name id>.W { <material id>.W }*3 (CZ_REQMAKINGITEM)
  10799. void clif_parse_ProduceMix(int fd,map_session_data *sd){
  10800. if( sd == nullptr ){
  10801. return;
  10802. }
  10803. const PACKET_CZ_REQMAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQMAKINGITEM*>( RFIFOP( fd, 0 ) );
  10804. switch( sd->menuskill_id ) {
  10805. case -1:
  10806. case AM_PHARMACY:
  10807. case RK_RUNEMASTERY:
  10808. case GC_RESEARCHNEWPOISON:
  10809. break;
  10810. default:
  10811. return;
  10812. }
  10813. if (pc_istrading(sd)) {
  10814. //Make it fail to avoid shop exploits where you sell something different than you see.
  10815. clif_skill_fail( *sd, sd->ud.skill_id );
  10816. clif_menuskill_clear(sd);
  10817. return;
  10818. }
  10819. int produce_idx;
  10820. if( (produce_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, 1)) )
  10821. skill_produce_mix(sd,0,p->itemId,p->material[0],p->material[1],p->material[2],1,produce_idx-1);
  10822. clif_menuskill_clear(sd);
  10823. }
  10824. /// Answer to mixing item selection dialog.
  10825. /// 025b <mk type>.W <name id>.W (CZ_REQ_MAKINGITEM)
  10826. /// mk type:
  10827. /// 1 = cooking
  10828. /// 2 = arrow
  10829. /// 3 = elemental
  10830. /// 4 = GN_MIX_COOKING
  10831. /// 5 = GN_MAKEBOMB
  10832. /// 6 = GN_S_PHARMACY
  10833. /// 7 = MT_M_MACHINE - Unconfirmed
  10834. /// 8 = BO_BIONIC_PHARMACY - Unconfirmed
  10835. void clif_parse_Cooking(int fd,map_session_data *sd) {
  10836. #if PACKETVER >= 20051010
  10837. if( sd == nullptr ){
  10838. return;
  10839. }
  10840. const PACKET_CZ_REQ_MAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGITEM*>( RFIFOP( fd, 0 ) );
  10841. int amount = sd->menuskill_val2 ? sd->menuskill_val2 : 1;
  10842. short food_idx = -1;
  10843. if( p->type == 6 && sd->menuskill_id != GN_MIX_COOKING && sd->menuskill_id != GN_S_PHARMACY )
  10844. return;
  10845. if (pc_istrading(sd)) {
  10846. //Make it fail to avoid shop exploits where you sell something different than you see.
  10847. clif_skill_fail( *sd, sd->ud.skill_id );
  10848. clif_menuskill_clear(sd);
  10849. return;
  10850. }
  10851. if( (food_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, amount)) )
  10852. skill_produce_mix(sd,(p->type>1?sd->menuskill_id:0),p->itemId,0,0,0,amount,food_idx-1);
  10853. clif_menuskill_clear(sd);
  10854. #endif
  10855. }
  10856. /// Answer to repair weapon item selection dialog
  10857. /// 01fd <index> W (CZ_REQ_ITEMREPAIR)
  10858. /// 01fd <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W ???
  10859. void clif_parse_RepairItem( int fd, map_session_data *sd ){
  10860. if( sd == nullptr ){
  10861. return;
  10862. }
  10863. // Hercules has wrong date -> use correct one here
  10864. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10865. const PACKET_CZ_REQ_ITEMREPAIR2* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR2*>( RFIFOP( fd, 0 ) );
  10866. #else
  10867. const PACKET_CZ_REQ_ITEMREPAIR1* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR1*>( RFIFOP( fd, 0 ) );
  10868. #endif
  10869. if (sd->menuskill_id != BS_REPAIRWEAPON)
  10870. return;
  10871. if (pc_istrading(sd)) {
  10872. //Make it fail to avoid shop exploits where you sell something different than you see.
  10873. clif_skill_fail( *sd, sd->ud.skill_id );
  10874. clif_menuskill_clear(sd);
  10875. return;
  10876. }
  10877. skill_repairweapon( *sd, p->item.index );
  10878. clif_menuskill_clear(sd);
  10879. }
  10880. /// Answer to refine weapon item selection dialog (CZ_REQ_WEAPONREFINE).
  10881. /// 0222 <index>.L
  10882. void clif_parse_WeaponRefine( int fd, map_session_data *sd ){
  10883. if( sd == nullptr ){
  10884. return;
  10885. }
  10886. if (sd->menuskill_id != WS_WEAPONREFINE) //Packet exploit?
  10887. return;
  10888. if (pc_istrading(sd)) {
  10889. //Make it fail to avoid shop exploits where you sell something different than you see.
  10890. clif_skill_fail( *sd, sd->ud.skill_id );
  10891. clif_menuskill_clear(sd);
  10892. return;
  10893. }
  10894. skill_weaponrefine( *sd, server_index( RFIFOL( fd, 2 ) ) );
  10895. clif_menuskill_clear(sd);
  10896. }
  10897. /// Answer to script menu dialog (CZ_CHOOSE_MENU).
  10898. /// 00b8 <npc id>.L <choice>.B
  10899. /// choice:
  10900. /// 1~254 = menu item
  10901. /// 255 = cancel
  10902. /// NOTE: If there were more than 254 items in the list, choice
  10903. /// overflows to choice%256.
  10904. void clif_parse_NpcSelectMenu(int fd,map_session_data *sd){
  10905. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10906. int npc_id = RFIFOL(fd,info->pos[0]);
  10907. uint8 select = RFIFOB(fd,info->pos[1]);
  10908. #ifdef SECURE_NPCTIMEOUT
  10909. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  10910. return;
  10911. #endif
  10912. if( (select > sd->npc_menu && select != 0xff) || select == 0 ) {
  10913. TBL_NPC* nd = map_id2nd(npc_id);
  10914. 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);
  10915. clif_GM_kick(nullptr,sd);
  10916. return;
  10917. }
  10918. sd->npc_menu = select;
  10919. if( battle_config.idletime_option&IDLE_NPC_MENU ){
  10920. sd->idletime = last_tick;
  10921. }
  10922. npc_scriptcont(sd,npc_id, false);
  10923. }
  10924. /// NPC dialog 'next' click (CZ_REQ_NEXT_SCRIPT).
  10925. /// 00b9 <npc id>.L
  10926. void clif_parse_NpcNextClicked(int fd,map_session_data *sd)
  10927. {
  10928. if( battle_config.idletime_option&IDLE_NPC_NEXT ){
  10929. sd->idletime = last_tick;
  10930. }
  10931. npc_scriptcont(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), false);
  10932. }
  10933. /// NPC numeric input dialog value (CZ_INPUT_EDITDLG).
  10934. /// 0143 <npc id>.L <value>.L
  10935. void clif_parse_NpcAmountInput(int fd,map_session_data *sd){
  10936. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10937. int npcid = RFIFOL(fd,info->pos[0]);
  10938. int amount = (int)RFIFOL(fd,info->pos[1]);
  10939. sd->npc_amount = amount;
  10940. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  10941. sd->idletime = last_tick;
  10942. }
  10943. npc_scriptcont(sd, npcid, false);
  10944. }
  10945. /// NPC text input dialog value (CZ_INPUT_EDITDLGSTR).
  10946. /// 01d5 <packet len>.W <npc id>.L <string>.?B
  10947. void clif_parse_NpcStringInput(int fd, map_session_data* sd){
  10948. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10949. int message_len = RFIFOW(fd,info->pos[0])-8;
  10950. int npcid = RFIFOL(fd,info->pos[1]);
  10951. const char* message = RFIFOCP(fd,info->pos[2]);
  10952. if( message_len <= 0 )
  10953. return; // invalid input
  10954. #if PACKETVER >= 20151001
  10955. message_len++;
  10956. #endif
  10957. safestrncpy(sd->npc_str, message, min(message_len,CHATBOX_SIZE));
  10958. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  10959. sd->idletime = last_tick;
  10960. }
  10961. npc_scriptcont(sd, npcid, false);
  10962. }
  10963. /// NPC dialog 'close' click (CZ_CLOSE_DIALOG).
  10964. /// 0146 <npc id>.L
  10965. void clif_parse_NpcCloseClicked(int fd,map_session_data *sd)
  10966. {
  10967. if (!sd->npc_id) //Avoid parsing anything when the script was done with. [Skotlex]
  10968. return;
  10969. if( battle_config.idletime_option&IDLE_NPC_CLOSE ){
  10970. sd->idletime = last_tick;
  10971. }
  10972. npc_scriptcont(sd, RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), true);
  10973. }
  10974. /// Answer to identify item selection dialog (CZ_REQ_ITEMIDENTIFY).
  10975. /// 0178 <index>.W
  10976. /// index:
  10977. /// -1 = cancel
  10978. void clif_parse_ItemIdentify(int fd,map_session_data *sd) {
  10979. short idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2;
  10980. if (sd->menuskill_id != MC_IDENTIFY)
  10981. return;
  10982. // Ignore the request
  10983. // - Invalid item index
  10984. // - Invalid item ID or item doesn't exist
  10985. // - Item is already identified
  10986. if (idx < 0 || idx >= MAX_INVENTORY ||
  10987. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  10988. sd->inventory.u.items_inventory[idx].identify) {// cancel pressed
  10989. sd->state.workinprogress = WIP_DISABLE_NONE;
  10990. clif_menuskill_clear(sd);
  10991. return;
  10992. }
  10993. skill_identify(sd, idx);
  10994. clif_menuskill_clear(sd);
  10995. }
  10996. /// Answer to arrow crafting item selection dialog.
  10997. /// 01ae <name id>.W (CZ_REQ_MAKINGARROW)
  10998. void clif_parse_SelectArrow(int fd,map_session_data *sd) {
  10999. if( sd == nullptr ){
  11000. return;
  11001. }
  11002. if (pc_istrading(sd)) {
  11003. //Make it fail to avoid shop exploits where you sell something different than you see.
  11004. clif_skill_fail( *sd, sd->ud.skill_id );
  11005. clif_menuskill_clear(sd);
  11006. return;
  11007. }
  11008. const PACKET_CZ_REQ_MAKINGARROW* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGARROW*>( RFIFOP( fd, 0 ) );
  11009. switch (sd->menuskill_id) {
  11010. case AC_MAKINGARROW:
  11011. skill_arrow_create(sd,p->itemId);
  11012. break;
  11013. case SA_CREATECON:
  11014. skill_produce_mix(sd,SA_CREATECON,p->itemId,0,0,0,1,-1);
  11015. break;
  11016. case GC_POISONINGWEAPON:
  11017. skill_poisoningweapon(*sd,p->itemId);
  11018. break;
  11019. case NC_MAGICDECOY:
  11020. skill_magicdecoy(*sd,p->itemId);
  11021. break;
  11022. }
  11023. clif_menuskill_clear(sd);
  11024. }
  11025. /// Answer to SA_AUTOSPELL skill selection dialog (CZ_SELECTAUTOSPELL).
  11026. /// 01ce <skill id>.L
  11027. void clif_parse_AutoSpell(int fd,map_session_data *sd)
  11028. {
  11029. if (sd->menuskill_id != SA_AUTOSPELL)
  11030. return;
  11031. sd->state.workinprogress = WIP_DISABLE_NONE;
  11032. skill_autospell(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11033. clif_menuskill_clear(sd);
  11034. }
  11035. /// Request to display item carding/composition list (CZ_REQ_ITEMCOMPOSITION_LIST).
  11036. /// 017a <card index>.W
  11037. void clif_parse_UseCard(int fd,map_session_data *sd)
  11038. {
  11039. if (sd->state.trading != 0)
  11040. return;
  11041. clif_use_card(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  11042. }
  11043. /// Answer to carding/composing item selection dialog (CZ_REQ_ITEMCOMPOSITION).
  11044. /// 017c <card index>.W <equip index>.W
  11045. void clif_parse_InsertCard(int fd,map_session_data *sd)
  11046. {
  11047. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11048. if (sd->state.trading != 0)
  11049. return;
  11050. pc_insert_card(sd,RFIFOW(fd,info->pos[0])-2,RFIFOW(fd,info->pos[1])-2);
  11051. }
  11052. /// Request of character's name by char ID.
  11053. /// 0193 <char id>.L (CZ_REQNAME_BYGID)
  11054. /// 0369 <char id>.L (CZ_REQNAME_BYGID2)
  11055. /// There are various variants of this packet, some of them have padding between fields.
  11056. void clif_parse_SolveCharName(int fd, map_session_data *sd)
  11057. {
  11058. int charid;
  11059. charid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11060. map_reqnickdb(sd, charid);
  11061. }
  11062. /// /resetskill /resetstate (CZ_RESET).
  11063. /// Request to reset stats or skills.
  11064. /// 0197 <type>.W
  11065. /// type:
  11066. /// 0 = state
  11067. /// 1 = skill
  11068. void clif_parse_ResetChar(int fd, map_session_data *sd) {
  11069. char cmd[15];
  11070. if( RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) )
  11071. safesnprintf(cmd,sizeof(cmd),"%cresetskill",atcommand_symbol);
  11072. else
  11073. safesnprintf(cmd,sizeof(cmd),"%cresetstat",atcommand_symbol);
  11074. is_atcommand(fd, sd, cmd, 1);
  11075. }
  11076. /// /lb /nlb (CZ_LOCALBROADCAST).
  11077. /// Request to broadcast a message on current map.
  11078. /// 019c <packet len>.W <text>.?B
  11079. void clif_parse_LocalBroadcast(int fd, map_session_data* sd)
  11080. {
  11081. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11082. char command[CHAT_SIZE_MAX+16];
  11083. unsigned int len = RFIFOW(fd,info->pos[0])-4;
  11084. char* msg = RFIFOCP(fd,info->pos[1]);
  11085. // as the length varies depending on the command used, just block unreasonably long strings
  11086. mes_len_check(msg, len, CHAT_SIZE_MAX);
  11087. safesnprintf(command,sizeof(command),"%clkami %s", atcommand_symbol, msg);
  11088. is_atcommand(fd, sd, command, 1);
  11089. }
  11090. /// Request to move an item from inventory to storage.
  11091. /// 00f3 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE)
  11092. /// 0364 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE2)
  11093. /// There are various variants of this packet, some of them have padding between fields.
  11094. void clif_parse_MoveToKafra(int fd, map_session_data *sd)
  11095. {
  11096. int item_index, item_amount;
  11097. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11098. if (pc_istrading(sd))
  11099. return;
  11100. item_index = RFIFOW(fd,info->pos[0])-2;
  11101. item_amount = RFIFOL(fd,info->pos[1]);
  11102. if (item_index < 0 || item_index >= MAX_INVENTORY || item_amount < 1)
  11103. return;
  11104. if( sd->inventory.u.items_inventory[item_index].equipSwitch ){
  11105. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11106. return;
  11107. }
  11108. if (sd->state.storage_flag == 1)
  11109. storage_storageadd(sd, &sd->storage, item_index, item_amount);
  11110. else
  11111. if (sd->state.storage_flag == 2)
  11112. storage_guild_storageadd(sd, item_index, item_amount);
  11113. else if (sd->state.storage_flag == 3)
  11114. storage_storageadd(sd, &sd->premiumStorage, item_index, item_amount);
  11115. }
  11116. /// Request to move an item from storage to inventory.
  11117. /// 00f5 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY)
  11118. /// 0365 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY2)
  11119. /// There are various variants of this packet, some of them have padding between fields.
  11120. void clif_parse_MoveFromKafra(int fd,map_session_data *sd)
  11121. {
  11122. int item_index, item_amount;
  11123. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11124. item_index = RFIFOW(fd,info->pos[0])-1;
  11125. item_amount = RFIFOL(fd,info->pos[1]);
  11126. if (sd->state.storage_flag == 1)
  11127. storage_storageget(sd, &sd->storage, item_index, item_amount);
  11128. else if(sd->state.storage_flag == 2)
  11129. storage_guild_storageget(sd, item_index, item_amount);
  11130. else if(sd->state.storage_flag == 3)
  11131. storage_storageget(sd, &sd->premiumStorage, item_index, item_amount);
  11132. }
  11133. /// Request to move an item from cart to storage (CZ_MOVE_ITEM_FROM_CART_TO_STORE).
  11134. /// 0129 <index>.W <amount>.L
  11135. void clif_parse_MoveToKafraFromCart(int fd, map_session_data *sd){
  11136. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11137. int idx = RFIFOW(fd,info->pos[0]) - 2;
  11138. int amount = RFIFOL(fd,info->pos[1]);
  11139. if( sd->state.vending )
  11140. return;
  11141. if (!pc_iscarton(sd))
  11142. return;
  11143. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11144. return;
  11145. if (idx < 0 || idx >= MAX_INVENTORY || amount < 1)
  11146. return;
  11147. if( sd->inventory.u.items_inventory[idx].equipSwitch ){
  11148. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11149. return;
  11150. }
  11151. if (sd->state.storage_flag == 1)
  11152. storage_storageaddfromcart(sd, &sd->storage, idx, amount);
  11153. else if (sd->state.storage_flag == 2)
  11154. storage_guild_storageaddfromcart(sd, idx, amount);
  11155. else if (sd->state.storage_flag == 3)
  11156. storage_storageaddfromcart(sd, &sd->premiumStorage, idx, amount);
  11157. }
  11158. /// Request to move an item from storage to cart (CZ_MOVE_ITEM_FROM_STORE_TO_CART).
  11159. /// 0128 <index>.W <amount>.L
  11160. void clif_parse_MoveFromKafraToCart(int fd, map_session_data *sd){
  11161. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11162. int idx = RFIFOW(fd,info->pos[0]) - 1;
  11163. int amount = RFIFOL(fd,info->pos[1]);
  11164. if( sd->state.vending )
  11165. return;
  11166. if (!pc_iscarton(sd))
  11167. return;
  11168. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11169. return;
  11170. if (sd->state.storage_flag == 1)
  11171. storage_storagegettocart(sd, &sd->storage, idx, amount);
  11172. else
  11173. if (sd->state.storage_flag == 2)
  11174. storage_guild_storagegettocart(sd, idx, amount);
  11175. else if (sd->state.storage_flag == 3)
  11176. storage_storagegettocart(sd, &sd->premiumStorage, idx, amount);
  11177. }
  11178. /// Request to close storage (CZ_CLOSE_STORE).
  11179. /// 00f7
  11180. void clif_parse_CloseKafra(int fd, map_session_data *sd)
  11181. {
  11182. if( sd->state.storage_flag == 1 )
  11183. storage_storageclose(sd);
  11184. else
  11185. if( sd->state.storage_flag == 2 )
  11186. storage_guild_storageclose(sd);
  11187. else if( sd->state.storage_flag == 3 )
  11188. storage_premiumStorage_close(sd);
  11189. }
  11190. /// Displays kafra storage password dialog (ZC_REQ_STORE_PASSWORD).
  11191. /// 023a <info>.W
  11192. /// info:
  11193. /// 0 = password has not been set yet
  11194. /// 1 = storage is password-protected
  11195. /// 8 = too many wrong passwords
  11196. /// ? = ignored
  11197. /// NOTE: This packet is only available on certain non-kRO clients.
  11198. void clif_storagepassword(map_session_data* sd, short info)
  11199. {
  11200. int fd = sd->fd;
  11201. WFIFOHEAD(fd,packet_len(0x23a));
  11202. WFIFOW(fd,0) = 0x23a;
  11203. WFIFOW(fd,2) = info;
  11204. WFIFOSET(fd,packet_len(0x23a));
  11205. }
  11206. /// Answer to the kafra storage password dialog (CZ_ACK_STORE_PASSWORD).
  11207. /// 023b <type>.W <password>.16B <new password>.16B
  11208. /// type:
  11209. /// 2 = change password
  11210. /// 3 = check password
  11211. /// NOTE: This packet is only available on certain non-kRO clients.
  11212. void clif_parse_StoragePassword(int fd, map_session_data *sd){ //@TODO
  11213. // struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11214. // int type = RFIFOW(fd,info->pos[0]);
  11215. // char* password = RFIFOP(fd,info->pos[1]);
  11216. // char* new_password = RFIFOP(fd,info->pos[2]);
  11217. // storage_changepass(sd,password,new_password);
  11218. }
  11219. /// Result of kafra storage password validation (ZC_RESULT_STORE_PASSWORD).
  11220. /// 023c <result>.W <error count>.W
  11221. /// result:
  11222. /// 4 = password change success
  11223. /// 5 = password change failure
  11224. /// 6 = password check success
  11225. /// 7 = password check failure
  11226. /// 8 = too many wrong passwords
  11227. /// ? = ignored
  11228. /// NOTE: This packet is only available on certain non-kRO clients.
  11229. void clif_storagepassword_result(map_session_data* sd, short result, short error_count)
  11230. {
  11231. int fd = sd->fd;
  11232. WFIFOHEAD(fd,packet_len(0x23c));
  11233. WFIFOW(fd,0) = 0x23c;
  11234. WFIFOW(fd,2) = result;
  11235. WFIFOW(fd,4) = error_count;
  11236. WFIFOSET(fd,packet_len(0x23c));
  11237. }
  11238. /// Party creation request
  11239. /// 00f9 <party name>.24B (CZ_MAKE_GROUP)
  11240. void clif_parse_CreateParty(int fd, map_session_data *sd){
  11241. if( sd == nullptr ){
  11242. return;
  11243. }
  11244. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11245. name[NAME_LENGTH-1] = '\0';
  11246. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11247. clif_displaymessage(fd, msg_txt(sd,227));
  11248. return;
  11249. }
  11250. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11251. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11252. return;
  11253. }
  11254. party_create( *sd, name, 0, 0 );
  11255. }
  11256. /// 01e8 <party name>.24B <item pickup rule>.B <item share rule>.B (CZ_MAKE_GROUP2)
  11257. void clif_parse_CreateParty2(int fd, map_session_data *sd){
  11258. if( sd == nullptr ){
  11259. return;
  11260. }
  11261. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11262. char* name = RFIFOCP(fd,info->pos[0]);
  11263. int item1 = RFIFOB(fd,info->pos[1]);
  11264. int item2 = RFIFOB(fd,info->pos[2]);
  11265. name[NAME_LENGTH-1] = '\0';
  11266. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11267. clif_displaymessage(fd, msg_txt(sd,227));
  11268. return;
  11269. }
  11270. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11271. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11272. return;
  11273. }
  11274. party_create( *sd, name, item1, item2 );
  11275. }
  11276. /// Party invitation request by account id
  11277. /// 00fc <account id>.L (CZ_REQ_JOIN_GROUP)
  11278. void clif_parse_PartyInvite( int fd, map_session_data *sd ){
  11279. if( sd == nullptr ){
  11280. return;
  11281. }
  11282. const PACKET_CZ_REQ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11283. party_invite( *sd, map_id2sd( p->AID ) );
  11284. }
  11285. /// Party invitation request by name
  11286. /// 02c4 <char name>.24B (CZ_PARTY_JOIN_REQ)
  11287. void clif_parse_PartyInvite2( int fd, map_session_data *sd ){
  11288. #if PACKETVER >= 20070227
  11289. if( sd == nullptr ){
  11290. return;
  11291. }
  11292. const PACKET_CZ_PARTY_JOIN_REQ* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ*>( RFIFOP( fd, 0 ) );
  11293. char name[NAME_LENGTH];
  11294. safestrncpy( name, p->name, sizeof( name ) );
  11295. party_invite( *sd, map_nick2sd( name, false ) );
  11296. #endif
  11297. }
  11298. /// Party invitation reply
  11299. /// 00ff <party id>.L <flag>.L (CZ_JOIN_GROUP)
  11300. /// flag:
  11301. /// 0 = reject
  11302. /// 1 = accept
  11303. void clif_parse_ReplyPartyInvite( int fd, map_session_data *sd ){
  11304. if( sd == nullptr ){
  11305. return;
  11306. }
  11307. const PACKET_CZ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11308. party_reply_invite( *sd, p->party_id, p->flag );
  11309. }
  11310. /// Party invitation reply
  11311. /// 02c7 <party id>.L <flag>.B (CZ_PARTY_JOIN_REQ_ACK)
  11312. /// flag:
  11313. /// 0 = reject
  11314. /// 1 = accept
  11315. void clif_parse_ReplyPartyInvite2( int fd, map_session_data *sd ){
  11316. #if PACKETVER >= 20070227
  11317. if( sd == nullptr ){
  11318. return;
  11319. }
  11320. const PACKET_CZ_PARTY_JOIN_REQ_ACK* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ_ACK*>( RFIFOP( fd, 0 ) );
  11321. party_reply_invite( *sd, p->party_id, p->flag );
  11322. #endif
  11323. }
  11324. /// Request to leave party.
  11325. /// 0100 (CZ_REQ_LEAVE_GROUP)
  11326. void clif_parse_LeaveParty( int fd, map_session_data *sd ){
  11327. if( sd == nullptr ){
  11328. return;
  11329. }
  11330. //const PACKET_CZ_REQ_LEAVE_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GROUP*>( RFIFOP( fd, 0 ) );
  11331. party_leave( *sd, true );
  11332. }
  11333. /// Request to expel a party member.
  11334. /// 0103 <account id>.L <char name>.24B (CZ_REQ_EXPEL_GROUP_MEMBER)
  11335. void clif_parse_RemovePartyMember( int fd, map_session_data* sd ){
  11336. if( sd == nullptr ){
  11337. return;
  11338. }
  11339. const PACKET_CZ_REQ_EXPEL_GROUP_MEMBER* p = reinterpret_cast<PACKET_CZ_REQ_EXPEL_GROUP_MEMBER*>( RFIFOP( fd, 0 ) );
  11340. party_removemember( *sd, p->AID, p->name );
  11341. }
  11342. /// Request to change party options.
  11343. /// 0102 <exp share rule>.L (CZ_CHANGE_GROUPEXPOPTION)
  11344. /// 07d7 <exp share rule>.L <item pickup rule>.B <item share rule>.B (CZ_GROUPINFO_CHANGE_V2)
  11345. void clif_parse_PartyChangeOption(int fd, map_session_data *sd)
  11346. {
  11347. struct party_data *p;
  11348. int i,expflag;
  11349. int cmd = RFIFOW(fd,0);
  11350. struct s_packet_db* info = &packet_db[cmd];
  11351. if( !sd->status.party_id )
  11352. return;
  11353. p = party_search(sd->status.party_id);
  11354. if( p == nullptr )
  11355. return;
  11356. ARR_FIND( 0, MAX_PARTY, i, p->data[i].sd == sd );
  11357. if( i == MAX_PARTY )
  11358. return; //Shouldn't happen
  11359. if( !p->party.member[i].leader )
  11360. return;
  11361. expflag = RFIFOL(fd,info->pos[0]);
  11362. if(cmd == 0x0102){ //Client can't change the item-field
  11363. party_changeoption(sd, expflag, p->party.item);
  11364. }
  11365. else {
  11366. int itemflag = (RFIFOB(fd,info->pos[1])?1:0)|(RFIFOB(fd,info->pos[2])?2:0);
  11367. party_changeoption(sd, expflag, itemflag);
  11368. }
  11369. }
  11370. /// Validates and processes party messages (CZ_REQUEST_CHAT_PARTY).
  11371. /// 0108 <packet len>.W <text>.?B (<name> : <message>) 00
  11372. void clif_parse_PartyMessage(int fd, map_session_data* sd){
  11373. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11374. // validate packet and retrieve name and message
  11375. if( !clif_process_message( sd, false, name, message, output ) )
  11376. return;
  11377. party_send_message(sd, output, strlen(output) + 1 );
  11378. }
  11379. /// Changes Party Leader (CZ_CHANGE_GROUP_MASTER).
  11380. /// 07da <account id>.L
  11381. void clif_parse_PartyChangeLeader(int fd, map_session_data* sd){
  11382. party_changeleader(sd, map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0])),nullptr);
  11383. }
  11384. /// Party Booking in KRO [Spiria]
  11385. ///
  11386. /// Request to register a party booking advertisment (CZ_PARTY_BOOKING_REQ_REGISTER).
  11387. /// 0802 <level>.W <map id>.W { <job>.W }*6
  11388. void clif_parse_PartyBookingRegisterReq(int fd, map_session_data* sd){
  11389. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11390. short level = RFIFOW(fd,info->pos[0]);
  11391. short mapid = RFIFOW(fd,info->pos[1]);
  11392. int idxpbj = info->pos[2];
  11393. short job[MAX_PARTY_BOOKING_JOBS];
  11394. int i;
  11395. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11396. job[i] = RFIFOB(fd,idxpbj+i*2);
  11397. party_booking_register(sd, level, mapid, job);
  11398. }
  11399. /// Result of request to register a party booking advertisment (ZC_PARTY_BOOKING_ACK_REGISTER).
  11400. /// 0803 <result>.W
  11401. /// result:
  11402. /// 0 = success
  11403. /// 1 = failure
  11404. /// 2 = already registered
  11405. void clif_PartyBookingRegisterAck(map_session_data *sd, int flag)
  11406. {
  11407. int fd = sd->fd;
  11408. WFIFOHEAD(fd,packet_len(0x803));
  11409. WFIFOW(fd,0) = 0x803;
  11410. WFIFOW(fd,2) = flag;
  11411. WFIFOSET(fd,packet_len(0x803));
  11412. }
  11413. /// Request to search for party booking advertisments (CZ_PARTY_BOOKING_REQ_SEARCH).
  11414. /// 0804 <level>.W <map id>.W <job>.W <last index>.L <result count>.W
  11415. void clif_parse_PartyBookingSearchReq(int fd, map_session_data* sd){
  11416. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11417. short level = RFIFOW(fd,info->pos[0]);
  11418. short mapid = RFIFOW(fd,info->pos[1]);
  11419. short job = RFIFOW(fd,info->pos[2]);
  11420. unsigned long lastindex = RFIFOL(fd,info->pos[3]);
  11421. short resultcount = RFIFOW(fd,info->pos[4]);
  11422. party_booking_search(sd, level, mapid, job, lastindex, resultcount);
  11423. }
  11424. /// Party booking search results (ZC_PARTY_BOOKING_ACK_SEARCH).
  11425. /// 0805 <packet len>.W <more results>.B { <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6 }*
  11426. /// more results:
  11427. /// 0 = no
  11428. /// 1 = yes
  11429. void clif_PartyBookingSearchAck(int fd, struct party_booking_ad_info** results, int count, bool more_result)
  11430. {
  11431. int i, j;
  11432. int size = sizeof(struct party_booking_ad_info); // structure size (48)
  11433. struct party_booking_ad_info *pb_ad;
  11434. WFIFOHEAD(fd,size*count + 5);
  11435. WFIFOW(fd,0) = 0x805;
  11436. WFIFOW(fd,2) = size*count + 5;
  11437. WFIFOB(fd,4) = more_result;
  11438. for(i=0; i<count; i++)
  11439. {
  11440. pb_ad = results[i];
  11441. WFIFOL(fd,i*size+5) = pb_ad->index;
  11442. safestrncpy(WFIFOCP(fd,i*size+9),pb_ad->charname,NAME_LENGTH);
  11443. WFIFOL(fd,i*size+33) = pb_ad->starttime; // FIXME: This is expire time
  11444. WFIFOW(fd,i*size+37) = pb_ad->p_detail.level;
  11445. WFIFOW(fd,i*size+39) = pb_ad->p_detail.mapid;
  11446. for(j=0; j<MAX_PARTY_BOOKING_JOBS; j++)
  11447. WFIFOW(fd,i*size+41+j*2) = pb_ad->p_detail.job[j];
  11448. }
  11449. WFIFOSET(fd,WFIFOW(fd,2));
  11450. }
  11451. /// Request to delete own party booking advertisment (CZ_PARTY_BOOKING_REQ_DELETE).
  11452. /// 0806
  11453. void clif_parse_PartyBookingDeleteReq(int fd, map_session_data* sd)
  11454. {
  11455. if(party_booking_delete(sd))
  11456. clif_PartyBookingDeleteAck(sd, 0);
  11457. }
  11458. /// Result of request to delete own party booking advertisment (ZC_PARTY_BOOKING_ACK_DELETE).
  11459. /// 0807 <result>.W
  11460. /// result:
  11461. /// 0 = success
  11462. /// 1 = success (auto-removed expired ad)
  11463. /// 2 = failure
  11464. /// 3 = nothing registered
  11465. void clif_PartyBookingDeleteAck(map_session_data* sd, int flag)
  11466. {
  11467. int fd = sd->fd;
  11468. WFIFOHEAD(fd,packet_len(0x807));
  11469. WFIFOW(fd,0) = 0x807;
  11470. WFIFOW(fd,2) = flag;
  11471. WFIFOSET(fd,packet_len(0x807));
  11472. }
  11473. /// Request to update party booking advertisment (CZ_PARTY_BOOKING_REQ_UPDATE).
  11474. /// 0808 { <job>.W }*6
  11475. void clif_parse_PartyBookingUpdateReq(int fd, map_session_data* sd)
  11476. {
  11477. short job[MAX_PARTY_BOOKING_JOBS];
  11478. int i;
  11479. int idxpbu = packet_db[RFIFOW(fd,0)].pos[0];
  11480. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11481. job[i] = RFIFOW(fd,idxpbu+i*2);
  11482. party_booking_update(sd, job);
  11483. }
  11484. /// Notification about new party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_INSERT).
  11485. /// 0809 <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6
  11486. void clif_PartyBookingInsertNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11487. {
  11488. int i;
  11489. uint8 buf[38+MAX_PARTY_BOOKING_JOBS*2];
  11490. if(pb_ad == nullptr) return;
  11491. WBUFW(buf,0) = 0x809;
  11492. WBUFL(buf,2) = pb_ad->index;
  11493. safestrncpy(WBUFCP(buf,6),pb_ad->charname,NAME_LENGTH);
  11494. WBUFL(buf,30) = pb_ad->starttime; // FIXME: This is expire time
  11495. WBUFW(buf,34) = pb_ad->p_detail.level;
  11496. WBUFW(buf,36) = pb_ad->p_detail.mapid;
  11497. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11498. WBUFW(buf,38+i*2) = pb_ad->p_detail.job[i];
  11499. clif_send(buf, packet_len(0x809), &sd->bl, ALL_CLIENT);
  11500. }
  11501. /// Notification about updated party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_UPDATE).
  11502. /// 080a <index>.L { <job>.W }*6
  11503. void clif_PartyBookingUpdateNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11504. {
  11505. int i;
  11506. uint8 buf[6+MAX_PARTY_BOOKING_JOBS*2];
  11507. if(pb_ad == nullptr) return;
  11508. WBUFW(buf,0) = 0x80a;
  11509. WBUFL(buf,2) = pb_ad->index;
  11510. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11511. WBUFW(buf,6+i*2) = pb_ad->p_detail.job[i];
  11512. clif_send(buf,packet_len(0x80a),&sd->bl,ALL_CLIENT); // Now UPDATE all client.
  11513. }
  11514. /// Notification about deleted party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_DELETE).
  11515. /// 080b <index>.L
  11516. void clif_PartyBookingDeleteNotify(map_session_data* sd, int index)
  11517. {
  11518. uint8 buf[6];
  11519. WBUFW(buf,0) = 0x80b;
  11520. WBUFL(buf,2) = index;
  11521. clif_send(buf, packet_len(0x80b), &sd->bl, ALL_CLIENT); // Now UPDATE all client.
  11522. }
  11523. /// Request to close own vending (CZ_REQ_CLOSESTORE).
  11524. /// 012e
  11525. void clif_parse_CloseVending(int fd, map_session_data* sd)
  11526. {
  11527. vending_closevending(sd);
  11528. }
  11529. /// Request to open a vending shop (CZ_REQ_BUY_FROMMC).
  11530. /// 0130 <account id>.L
  11531. void clif_parse_VendingListReq(int fd, map_session_data* sd)
  11532. {
  11533. if( sd->npc_id ) {// using an NPC
  11534. return;
  11535. }
  11536. vending_vendinglistreq(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11537. }
  11538. /// Shop item(s) purchase request.
  11539. /// 0134 <packet len>.W <account id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC)
  11540. void clif_parse_PurchaseReq(int fd, map_session_data* sd){
  11541. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC*>( RFIFOP( fd, 0 ) );
  11542. vending_purchasereq( sd, p->AID, sd->vended_id, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11543. // whether it fails or not, the buy window is closed
  11544. sd->vended_id = 0;
  11545. }
  11546. /// Shop item(s) purchase request.
  11547. /// 0801 <packet len>.W <account id>.L <unique id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC2)
  11548. void clif_parse_PurchaseReq2(int fd, map_session_data* sd){
  11549. #if PACKETVER >= 20100105
  11550. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2*>( RFIFOP( fd, 0 ) );
  11551. vending_purchasereq( sd, p->AID, p->UniqueID, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11552. // whether it fails or not, the buy window is closed
  11553. sd->vended_id = 0;
  11554. #endif
  11555. }
  11556. /// Confirm or cancel the shop preparation window.
  11557. /// 012f <packet len>.W <shop name>.80B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE)
  11558. /// 01b2 <packet len>.W <shop name>.80B <result>.B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE2)
  11559. /// result:
  11560. /// 0 = canceled
  11561. /// 1 = open
  11562. void clif_parse_OpenVending(int fd, map_session_data* sd){
  11563. if( sd == nullptr ){
  11564. return;
  11565. }
  11566. int cmd = RFIFOW(fd,0);
  11567. struct s_packet_db* info = &packet_db[cmd];
  11568. short len = (short)RFIFOW(fd,info->pos[0]);
  11569. const char* message = RFIFOCP(fd,info->pos[1]);
  11570. const uint8* data = (uint8*)RFIFOP(fd,info->pos[3]);
  11571. if(cmd == 0x12f){ // (CZ_REQ_OPENSTORE)
  11572. len -= 84;
  11573. }
  11574. else { //(CZ_REQ_OPENSTORE2)
  11575. bool flag;
  11576. len -= 85;
  11577. flag = RFIFOB(fd,info->pos[2]) != 0;
  11578. if (!flag) {
  11579. sd->state.prevend = 0;
  11580. sd->state.workinprogress = WIP_DISABLE_NONE;
  11581. }
  11582. }
  11583. if( sd->sc.getSCE(SC_NOCHAT) && sd->sc.getSCE(SC_NOCHAT)->val1&MANNER_NOROOM )
  11584. return;
  11585. if( map_getmapflag(sd->bl.m, MF_NOVENDING) ) {
  11586. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  11587. return;
  11588. }
  11589. if( map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNOVENDING) ) {
  11590. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  11591. return;
  11592. }
  11593. if( message[0] == '\0' ) // invalid input
  11594. return;
  11595. vending_openvending(*sd, message, data, len/8, nullptr);
  11596. }
  11597. /// Guild creation request (CZ_REQ_MAKE_GUILD).
  11598. /// 0165 <char id>.L <guild name>.24B
  11599. void clif_parse_CreateGuild(int fd,map_session_data *sd){
  11600. //int charid = RFIFOL(fd,packet_db[cmd].pos[0]);
  11601. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11602. name[NAME_LENGTH-1] = '\0';
  11603. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11604. clif_displaymessage(fd, msg_txt(sd,228));
  11605. return;
  11606. }
  11607. if(sd->clan){
  11608. // Should display a clientside message "You are currently joined in Clan !!" so we ignore it
  11609. return;
  11610. }
  11611. guild_create( *sd, name );
  11612. }
  11613. /// Request for guild window interface permissions (CZ_REQ_GUILD_MENUINTERFACE).
  11614. /// 014d
  11615. void clif_parse_GuildCheckMaster(int fd, map_session_data *sd)
  11616. {
  11617. clif_guild_masterormember(sd);
  11618. }
  11619. /// Request for guild window information (CZ_REQ_GUILD_MENU).
  11620. /// 014f <type>.L
  11621. /// type:
  11622. /// 0 = basic info
  11623. /// 1 = member manager
  11624. /// 2 = positions
  11625. /// 3 = skills
  11626. /// 4 = expulsion list
  11627. /// 5 = unknown (GM_ALLGUILDLIST)
  11628. /// 6 = notice
  11629. void clif_parse_GuildRequestInfo(int fd, map_session_data *sd)
  11630. {
  11631. int type = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11632. if( !sd->status.guild_id && !sd->bg_id )
  11633. return;
  11634. switch( type )
  11635. {
  11636. case 0: // Basic Information Guild, hostile alliance information
  11637. clif_guild_basicinfo( *sd );
  11638. clif_guild_allianceinfo(sd);
  11639. clif_guild_castle_list(*sd);
  11640. break;
  11641. case 1: // Members list, list job title
  11642. clif_guild_positionnamelist(sd);
  11643. clif_guild_memberlist( *sd );
  11644. break;
  11645. case 2: // List job title, title information list
  11646. clif_guild_positionnamelist(sd);
  11647. clif_guild_positioninfolist(*sd);
  11648. break;
  11649. case 3: // Skill list
  11650. clif_guild_skillinfo( *sd );
  11651. break;
  11652. case 4: // Expulsion list
  11653. clif_guild_expulsionlist(*sd);
  11654. break;
  11655. default:
  11656. ShowError("clif: guild request info: unknown type %d\n", type);
  11657. break;
  11658. }
  11659. }
  11660. /// Request to update guild positions (CZ_REG_CHANGE_GUILD_POSITIONINFO).
  11661. /// 0161 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <name>.24B }*
  11662. void clif_parse_GuildChangePositionInfo(int fd, map_session_data *sd)
  11663. {
  11664. int i;
  11665. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11666. int len = RFIFOW(fd,info->pos[0]);
  11667. int idxgpos = info->pos[1];
  11668. if(!sd->state.gmaster_flag)
  11669. return;
  11670. for(i = idxgpos; i < len; i += 40 ){
  11671. guild_change_position(sd->status.guild_id, RFIFOL(fd,i), RFIFOL(fd,i+4), RFIFOL(fd,i+12), RFIFOCP(fd,i+16));
  11672. }
  11673. }
  11674. /// Request to update the position of guild members.
  11675. /// 0155 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (CZ_REQ_CHANGE_MEMBERPOS)
  11676. void clif_parse_GuildChangeMemberPosition( int fd, map_session_data *sd ){
  11677. if(!sd->state.gmaster_flag)
  11678. return;
  11679. const PACKET_CZ_REQ_CHANGE_MEMBERPOS* p = reinterpret_cast<PACKET_CZ_REQ_CHANGE_MEMBERPOS*>( RFIFOP( fd, 0 ) );
  11680. int16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  11681. for( int16 i = 0; i < entries; i++ ){
  11682. const PACKET_CZ_REQ_CHANGE_MEMBERPOS_sub& entry = p->list[i];
  11683. // Guild leadership change
  11684. if( entry.position == 0 ){
  11685. if( !battle_config.guild_leaderchange_woe && is_agit_start() ){
  11686. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_IN_SIEGE_TIME );
  11687. return;
  11688. }
  11689. if( battle_config.guild_leaderchange_delay && DIFF_TICK( time( nullptr ),sd->guild->guild.last_leader_change ) < battle_config.guild_leaderchange_delay ){
  11690. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_NOT_TIME );
  11691. return;
  11692. }
  11693. guild_gm_change( sd->status.guild_id, entry.CID );
  11694. // No further entries will be processed - the requesting player lost his guild master status
  11695. return;
  11696. }else if( entry.position > 0 ){
  11697. guild_change_memberposition( sd->status.guild_id, entry.AID, entry.CID, entry.position );
  11698. }
  11699. }
  11700. }
  11701. /// Request for guild emblem data (CZ_REQ_GUILD_EMBLEM_IMG).
  11702. /// 0151 <guild id>.L
  11703. void clif_parse_GuildRequestEmblem(int fd,map_session_data *sd)
  11704. {
  11705. int guild_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11706. auto g = guild_search(guild_id);
  11707. if (g)
  11708. clif_guild_emblem(*sd, g->guild);
  11709. }
  11710. /// Validates data of a guild emblem (compressed bitmap)
  11711. enum e_result_validate_emblem { // Used as Result for clif_validate_emblem
  11712. EMBVALIDATE_SUCCESS = 0,
  11713. EMBVALIDATE_ERR_RAW_FILEFORMAT, // Invalid File Format (Error in zlib/decompression or malformed BMP header)
  11714. EMBVALIDATE_ERR_TRANSPARENCY // uploaded emblem does not met the requirements of inter_config.emblem_transparency_limit
  11715. };
  11716. static enum e_result_validate_emblem clif_validate_emblem(const uint8* emblem, unsigned long emblem_len)
  11717. {
  11718. uint8 buf[1800]; // no well-formed emblem bitmap is larger than 1782 (24 bit) / 1654 (8 bit) bytes
  11719. unsigned long buf_len = sizeof(buf);
  11720. int offset = 0;
  11721. if(!(( decode_zip(buf, &buf_len, emblem, emblem_len) == 0 && buf_len >= 18 ) // sizeof(BITMAPFILEHEADER) + sizeof(biSize) of the following info header struct
  11722. && RBUFW(buf,0) == 0x4d42 // BITMAPFILEHEADER.bfType (signature)
  11723. && RBUFL(buf,2) == buf_len // BITMAPFILEHEADER.bfSize (file size)
  11724. && (offset = RBUFL(buf,10)) < buf_len // BITMAPFILEHEADER.bfOffBits (offset to bitmap bits)
  11725. ))
  11726. return EMBVALIDATE_ERR_RAW_FILEFORMAT;
  11727. if(inter_config.emblem_transparency_limit != 100) {
  11728. int i, transcount = 1, tmp[3];
  11729. for(i = offset; i < buf_len-1; i++) {
  11730. int j = i%3;
  11731. tmp[j] = RBUFL(buf,i);
  11732. if(j == 2 && (tmp[0] == 0xFFFF00FF) && (tmp[1] == 0xFFFF00) && (tmp[2] == 0xFF00FFFF)) //if pixel is transparent
  11733. transcount++;
  11734. }
  11735. if(((transcount*300)/(buf_len-offset)) > inter_config.emblem_transparency_limit) //convert in % to chk
  11736. return EMBVALIDATE_ERR_TRANSPARENCY;
  11737. }
  11738. return EMBVALIDATE_SUCCESS;
  11739. }
  11740. /// Request to update the guild emblem (CZ_REGISTER_GUILD_EMBLEM_IMG).
  11741. /// 0153 <packet len>.W <emblem data>.?B
  11742. void clif_parse_GuildChangeEmblem(int fd,map_session_data *sd){
  11743. if( sd == nullptr ){
  11744. return;
  11745. }
  11746. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11747. unsigned long emblem_len = RFIFOW(fd,info->pos[0])-4;
  11748. const uint8* emblem = RFIFOP(fd,info->pos[1]);
  11749. int emb_val=0;
  11750. if( !emblem_len || !sd->state.gmaster_flag )
  11751. return;
  11752. if(!(inter_config.emblem_woe_change) && is_agit_start() ){
  11753. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,385),false,SELF); //"You not allowed to change emblem during woe"
  11754. return;
  11755. }
  11756. emb_val = clif_validate_emblem(emblem, emblem_len);
  11757. if(emb_val == EMBVALIDATE_ERR_RAW_FILEFORMAT){
  11758. 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);
  11759. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,386),false,SELF); //"The chosen emblem was detected invalid\n"
  11760. return;
  11761. } else if(emb_val == EMBVALIDATE_ERR_TRANSPARENCY){
  11762. char output[128];
  11763. safesnprintf(output,sizeof(output),msg_txt(sd,387),inter_config.emblem_transparency_limit);
  11764. 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"
  11765. return;
  11766. }
  11767. guild_change_emblem( *sd, emblem_len, (const char*)emblem );
  11768. }
  11769. /// Request to update the guild emblem id
  11770. /// 0b46 <guild id>.L <version>.L
  11771. void clif_parse_GuildChangeEmblem2(int fd, map_session_data* sd) {
  11772. if( sd == nullptr ){
  11773. return;
  11774. }
  11775. #if PACKETVER >= 20190724
  11776. const PACKET_CZ_REQ_ADD_NEW_EMBLEM* p = reinterpret_cast<PACKET_CZ_REQ_ADD_NEW_EMBLEM*>( RFIFOP( fd, 0 ) );
  11777. auto &g = sd->guild;
  11778. if (!g || g->guild.guild_id != p->guild_id)
  11779. return;
  11780. if (!sd->state.gmaster_flag)
  11781. return;
  11782. if (!inter_config.emblem_woe_change && is_agit_start()) {
  11783. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 385), false, SELF); //"You not allowed to change emblem during woe"
  11784. return;
  11785. }
  11786. guild_change_emblem_version( *sd, p->version );
  11787. #endif
  11788. }
  11789. /// Guild notice update request (CZ_GUILD_NOTICE).
  11790. /// 016e <guild id>.L <msg1>.60B <msg2>.120B
  11791. void clif_parse_GuildChangeNotice(int fd, map_session_data* sd){
  11792. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11793. int guild_id = RFIFOL(fd,info->pos[0]);
  11794. char* msg1 = RFIFOCP(fd,info->pos[1]);
  11795. char* msg2 = RFIFOCP(fd,info->pos[2]);
  11796. if(!sd->state.gmaster_flag)
  11797. return;
  11798. // compensate for some client defects when using multilanguage mode
  11799. if (msg1[0] == '|' && msg1[3] == '|') msg1+= 3; // skip duplicate marker
  11800. if (msg2[0] == '|' && msg2[3] == '|') msg2+= 3; // skip duplicate marker
  11801. if (msg2[0] == '|') msg2[strnlen(msg2, MAX_GUILDMES2)-1] = '\0'; // delete extra space at the end of string
  11802. guild_change_notice(sd, guild_id, msg1, msg2);
  11803. }
  11804. /// Guild invite request.
  11805. /// 0168 <account id>.L <inviter account id>.L <inviter char id>.L (CZ_REQ_JOIN_GUILD)
  11806. void clif_parse_GuildInvite( int fd,map_session_data *sd ){
  11807. if( sd == nullptr ){
  11808. return;
  11809. }
  11810. const PACKET_CZ_REQ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11811. guild_invite( *sd, map_id2sd( p->AID ) );
  11812. }
  11813. /// Guild invite request (/guildinvite)
  11814. /// 0916 <char name>.24B (CZ_REQ_JOIN_GUILD2)
  11815. void clif_parse_GuildInvite2( int fd, map_session_data *sd ){
  11816. #if PACKETVER >= 20120410
  11817. if( sd == nullptr ){
  11818. return;
  11819. }
  11820. const PACKET_CZ_REQ_JOIN_GUILD2* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD2*>( RFIFOP( fd, 0 ) );
  11821. char nick[NAME_LENGTH] = {0};
  11822. safestrncpy( nick, p->name, NAME_LENGTH );
  11823. guild_invite( *sd, map_nick2sd( nick, false ) );
  11824. #endif
  11825. }
  11826. /// Answer to guild invitation.
  11827. /// 016b <guild id>.L <answer>.L (CZ_JOIN_GUILD)
  11828. /// answer:
  11829. /// 0 = refuse
  11830. /// 1 = accept
  11831. void clif_parse_GuildReplyInvite( int fd, map_session_data *sd ){
  11832. if( sd == nullptr ){
  11833. return;
  11834. }
  11835. const PACKET_CZ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11836. guild_reply_invite( *sd, p->guild_id, p->answer );
  11837. }
  11838. /// Request to leave guild.
  11839. /// 0159 <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_LEAVE_GUILD)
  11840. void clif_parse_GuildLeave(int fd,map_session_data *sd){
  11841. if( sd == nullptr ){
  11842. return;
  11843. }
  11844. const PACKET_CZ_REQ_LEAVE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GUILD*>( RFIFOP( fd, 0 ) );
  11845. guild_leave( *sd, p->guild_id, p->AID, p->CID, p->message );
  11846. }
  11847. /// Request to expel a member of a guild.
  11848. /// 015b <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_BAN_GUILD)
  11849. void clif_parse_GuildExpulsion(int fd,map_session_data *sd){
  11850. if( sd == nullptr ){
  11851. return;
  11852. }
  11853. const PACKET_CZ_REQ_BAN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_BAN_GUILD*>( RFIFOP( fd, 0 ) );
  11854. guild_expulsion( *sd, p->guild_id, p->AID, p->CID, p->message );
  11855. }
  11856. /// Validates and processes guild messages (CZ_GUILD_CHAT).
  11857. /// 017e <packet len>.W <text>.?B (<name> : <message>) 00
  11858. void clif_parse_GuildMessage(int fd, map_session_data* sd){
  11859. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11860. // validate packet and retrieve name and message
  11861. if( !clif_process_message( sd, false, name, message, output ) )
  11862. return;
  11863. if( sd->bg_id )
  11864. bg_send_message(sd, output, strlen(output) );
  11865. else
  11866. guild_send_message(sd, output, strlen(output) );
  11867. }
  11868. /// Guild alliance request (CZ_REQ_ALLY_GUILD).
  11869. /// 0170 <account id>.L <inviter account id>.L <inviter char id>.L
  11870. void clif_parse_GuildRequestAlliance(int fd, map_session_data *sd)
  11871. {
  11872. map_session_data *t_sd;
  11873. if(!sd->state.gmaster_flag)
  11874. return;
  11875. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11876. clif_displaymessage(fd, msg_txt(sd,228));
  11877. return;
  11878. }
  11879. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11880. //inv_aid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11881. //inv_cid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[2]);
  11882. // @noask [LuzZza]
  11883. if(t_sd && t_sd->state.noask) {
  11884. clif_noask_sub( *sd, *t_sd, 396 ); // Autorejected alliance request from %s.
  11885. return;
  11886. }
  11887. guild_reqalliance(sd,t_sd);
  11888. }
  11889. /// Answer to a guild alliance request (CZ_ALLY_GUILD).
  11890. /// 0172 <inviter account id>.L <answer>.L
  11891. /// answer:
  11892. /// 0 = refuse
  11893. /// 1 = accept
  11894. void clif_parse_GuildReplyAlliance(int fd, map_session_data *sd){
  11895. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11896. guild_reply_reqalliance(sd,
  11897. RFIFOL(fd,info->pos[0]),
  11898. RFIFOL(fd,info->pos[1]));
  11899. }
  11900. /// Request to delete a guild alliance or opposition (CZ_REQ_DELETE_RELATED_GUILD).
  11901. /// 0183 <opponent guild id>.L <relation>.L
  11902. /// relation:
  11903. /// 0 = Ally
  11904. /// 1 = Enemy
  11905. void clif_parse_GuildDelAlliance(int fd, map_session_data *sd){
  11906. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11907. if(!sd->state.gmaster_flag)
  11908. return;
  11909. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11910. clif_displaymessage(fd, msg_txt(sd,228));
  11911. return;
  11912. }
  11913. guild_delalliance(sd,
  11914. RFIFOL(fd,info->pos[0]),
  11915. RFIFOL(fd,info->pos[1]));
  11916. }
  11917. /// Request to set a guild as opposition (CZ_REQ_HOSTILE_GUILD).
  11918. /// 0180 <account id>.L
  11919. void clif_parse_GuildOpposition(int fd, map_session_data *sd)
  11920. {
  11921. map_session_data *t_sd;
  11922. if(!sd->state.gmaster_flag)
  11923. return;
  11924. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11925. clif_displaymessage(fd, msg_txt(sd,228));
  11926. return;
  11927. }
  11928. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11929. // @noask [LuzZza]
  11930. if(t_sd && t_sd->state.noask) {
  11931. clif_noask_sub( *sd, *t_sd, 397 ); // Autorejected opposition request from %s.
  11932. return;
  11933. }
  11934. guild_opposition(sd,t_sd);
  11935. }
  11936. /// Request to delete own guild.
  11937. /// 015d <key>.40B (CZ_REQ_DISORGANIZE_GUILD)
  11938. /// key:
  11939. /// now guild name; might have been (intended) email, since the
  11940. /// field name and size is same as the one in CH_DELETE_CHAR.
  11941. void clif_parse_GuildBreak( int fd, map_session_data *sd ){
  11942. if( sd == nullptr ){
  11943. return;
  11944. }
  11945. const PACKET_CZ_REQ_DISORGANIZE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_DISORGANIZE_GUILD*>( RFIFOP( fd, 0 ) );
  11946. guild_break( *sd, p->key );
  11947. }
  11948. /// Pet
  11949. ///
  11950. /// Request to invoke a pet menu action (CZ_COMMAND_PET).
  11951. /// 01a1 <type>.B
  11952. /// type:
  11953. /// 0 = pet information
  11954. /// 1 = feed
  11955. /// 2 = performance
  11956. /// 3 = return to egg
  11957. /// 4 = unequip accessory
  11958. void clif_parse_PetMenu(int fd, map_session_data *sd){
  11959. pet_menu(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11960. }
  11961. /// Attempt to tame a monster (CZ_TRYCAPTURE_MONSTER).
  11962. /// 019f <id>.L
  11963. void clif_parse_CatchPet(int fd, map_session_data *sd){
  11964. pet_catch_process2(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11965. }
  11966. /// Answer to pet incubator egg selection dialog (CZ_SELECT_PETEGG).
  11967. /// 01a7 <index>.W
  11968. void clif_parse_SelectEgg(int fd, map_session_data *sd){
  11969. if (sd->menuskill_id != SA_TAMINGMONSTER || sd->menuskill_val != -1)
  11970. return;
  11971. pet_select_egg(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  11972. clif_menuskill_clear(sd);
  11973. }
  11974. /// Request to display pet's emotion/talk (CZ_PET_ACT).
  11975. /// 01a9 <data>.L
  11976. /// data:
  11977. /// is either emotion (@see enum emotion_type) or a compound value
  11978. /// (((mob id)-100)*100+(act id)*10+(hunger)) that describes an
  11979. /// entry (given in parentheses) in data\pettalktable.xml
  11980. /// act id:
  11981. /// 0 = feeding
  11982. /// 1 = hunting
  11983. /// 2 = danger
  11984. /// 3 = dead
  11985. /// 4 = normal (stand)
  11986. /// 5 = special performance (perfor_s)
  11987. /// 6 = level up (levelup)
  11988. /// 7 = performance 1 (perfor_1)
  11989. /// 8 = performance 2 (perfor_2)
  11990. /// 9 = performance 3 (perfor_3)
  11991. /// 10 = log-in greeting (connect)
  11992. /// hungry value:
  11993. /// 0 = very hungry (hungry)
  11994. /// 1 = hungry (bit_hungry)
  11995. /// 2 = satisfied (noting)
  11996. /// 3 = stuffed (full)
  11997. /// 4 = full (so_full)
  11998. void clif_parse_SendEmotion(int fd, map_session_data *sd)
  11999. {
  12000. if(sd->pd)
  12001. clif_pet_emotion(sd->pd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12002. }
  12003. /// Request to change pet's name (CZ_RENAME_PET).
  12004. /// 01a5 <name>.24B
  12005. void clif_parse_ChangePetName(int fd, map_session_data *sd)
  12006. {
  12007. pet_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12008. }
  12009. /// /kill (CZ_DISCONNECT_CHARACTER).
  12010. /// Request to disconnect a character.
  12011. /// 00cc <account id>.L
  12012. /// NOTE: Also sent when using GM right click menu "(name) force to quit"
  12013. void clif_parse_GMKick(int fd, map_session_data *sd)
  12014. {
  12015. struct block_list *target;
  12016. int tid;
  12017. tid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12018. target = map_id2bl(tid);
  12019. if (!target) {
  12020. clif_GM_kickack(sd, 0);
  12021. return;
  12022. }
  12023. switch (target->type) {
  12024. case BL_PC:
  12025. {
  12026. char command[NAME_LENGTH+6];
  12027. safesnprintf(command,sizeof(command),"%ckick %s", atcommand_symbol, status_get_name(target));
  12028. is_atcommand(fd, sd, command, 1);
  12029. }
  12030. break;
  12031. /**
  12032. * This one does not invoke any atcommand, so we need to check for permissions.
  12033. */
  12034. case BL_MOB:
  12035. {
  12036. char command[100];
  12037. if( !pc_can_use_command(sd, "killmonster", COMMAND_ATCOMMAND)) {
  12038. clif_GM_kickack(sd, 0);
  12039. return;
  12040. }
  12041. safesnprintf(command,sizeof(command),"/kick %s (%d)", status_get_name(target), status_get_class(target));
  12042. log_atcommand(sd, command);
  12043. status_percent_damage(&sd->bl, target, 100, 0, true); // can invalidate 'target'
  12044. }
  12045. break;
  12046. case BL_NPC:
  12047. {
  12048. struct npc_data* nd = (struct npc_data *)target;
  12049. if( pc_can_use_command(sd, "unloadnpc", COMMAND_ATCOMMAND)) {
  12050. npc_unload_duplicates(nd);
  12051. npc_unload(nd,true);
  12052. npc_read_event_script();
  12053. }
  12054. }
  12055. break;
  12056. default:
  12057. clif_GM_kickack(sd, 0);
  12058. }
  12059. }
  12060. /// /killall (CZ_DISCONNECT_ALL_CHARACTER).
  12061. /// Request to disconnect all characters.
  12062. /// 00ce
  12063. void clif_parse_GMKickAll(int fd, map_session_data* sd) {
  12064. char cmd[15];
  12065. safesnprintf(cmd,sizeof(cmd),"%ckickall",atcommand_symbol);
  12066. is_atcommand(fd, sd, cmd, 1);
  12067. }
  12068. /// /remove (CZ_REMOVE_AID).
  12069. /// Request to warp to a character with given login ID.
  12070. /// 01ba <account name>.24B
  12071. /// /shift (CZ_SHIFT).
  12072. /// Request to warp to a character with given name.
  12073. /// 01bb <char name>.24B
  12074. void clif_parse_GMShift(int fd, map_session_data *sd)
  12075. {// FIXME: remove is supposed to receive account name for clients prior 20100803RE
  12076. char *player_name;
  12077. char command[NAME_LENGTH+8];
  12078. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12079. player_name[NAME_LENGTH-1] = '\0';
  12080. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, player_name);
  12081. is_atcommand(fd, sd, command, 1);
  12082. }
  12083. /// /remove (CZ_REMOVE_AID_SSO).
  12084. /// Request to warp to a character with given account ID.
  12085. /// 0843 <account id>.L
  12086. void clif_parse_GMRemove2(int fd, map_session_data* sd)
  12087. {
  12088. uint32 account_id;
  12089. map_session_data* pl_sd;
  12090. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12091. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12092. char command[NAME_LENGTH+8];
  12093. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, pl_sd->status.name);
  12094. is_atcommand(fd, sd, command, 1);
  12095. }
  12096. }
  12097. /// /recall (CZ_RECALL).
  12098. /// Request to summon a player with given login ID to own position.
  12099. /// 01bc <account name>.24B
  12100. /// /summon (CZ_RECALL_GID).
  12101. /// Request to summon a player with given name to own position.
  12102. /// 01bd <char name>.24B
  12103. void clif_parse_GMRecall(int fd, map_session_data *sd)
  12104. {// FIXME: recall is supposed to receive account name for clients prior 20100803RE
  12105. char *player_name;
  12106. char command [NAME_LENGTH+8];
  12107. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12108. player_name[NAME_LENGTH-1] = '\0';
  12109. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, player_name);
  12110. is_atcommand(fd, sd, command, 1);
  12111. }
  12112. /// /recall (CZ_RECALL_SSO).
  12113. /// Request to summon a player with given account ID to own position.
  12114. /// 0842 <account id>.L
  12115. void clif_parse_GMRecall2(int fd, map_session_data* sd)
  12116. {
  12117. uint32 account_id;
  12118. map_session_data* pl_sd;
  12119. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12120. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12121. char command[NAME_LENGTH+8];
  12122. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, pl_sd->status.name);
  12123. is_atcommand(fd, sd, command, 1);
  12124. }
  12125. }
  12126. /// /item /monster (CZ_ITEM_CREATE).
  12127. /// Request to execute GM commands.
  12128. /// Usage:
  12129. /// /item n - summon n monster or acquire n item/s
  12130. /// /item money - grants 2147483647 zenies
  12131. /// /item whereisboss - locate boss mob in current map.(not yet implemented)
  12132. /// /item regenboss_n t - regenerate n boss monster by t millisecond.(not yet implemented)
  12133. /// /item onekillmonster - toggle an ability to kill mobs in one hit.(not yet implemented)
  12134. /// /item bossinfo - display the information of a boss monster in current map.(not yet implemented)
  12135. /// /item cap_n - capture n monster as pet.(not yet implemented)
  12136. /// /item agitinvest - reset current global agit investments.(not yet implemented)
  12137. /// 013f <item/mob name>.24B
  12138. /// 09ce <item/mob name>.100B [Ind/Yommy]
  12139. void clif_parse_GM_Item_Monster(int fd, map_session_data *sd)
  12140. {
  12141. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12142. int mob_id = 0;
  12143. StringBuf command;
  12144. char *str;
  12145. //#if PACKETVER >= 20131218
  12146. // char str[100];
  12147. //#else
  12148. // char str[24];
  12149. //#endif
  12150. str = RFIFOCP(fd,info->pos[0]);
  12151. if (!str || str[0] == '\0')
  12152. return;
  12153. if (strcmpi(str,"SPPOINT") == 0 || strcmpi(str,"JOBLEVEL") == 0) //! TODO /sp sends these values
  12154. return;
  12155. trim(str);
  12156. // Zeny
  12157. if( strcmpi(str, "money") == 0 ) {
  12158. StringBuf_Init(&command);
  12159. StringBuf_Printf(&command, "%czeny %d", atcommand_symbol, INT_MAX);
  12160. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12161. StringBuf_Destroy(&command);
  12162. return;
  12163. }
  12164. std::shared_ptr<item_data> id = item_db.searchname( str );
  12165. // Item
  12166. if( id ){
  12167. StringBuf_Init(&command);
  12168. if( !itemdb_isstackable2( id.get() ) ){ //Nonstackable
  12169. StringBuf_Printf(&command, "%citem2 %u 1 0 0 0 0 0 0 0", atcommand_symbol, id->nameid);
  12170. }else{
  12171. if (id->flag.guid)
  12172. StringBuf_Printf(&command, "%citem %u 1", atcommand_symbol, id->nameid);
  12173. else
  12174. StringBuf_Printf(&command, "%citem %u 20", atcommand_symbol, id->nameid);
  12175. }
  12176. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12177. StringBuf_Destroy(&command);
  12178. return;
  12179. }
  12180. // Monster
  12181. if ((mob_id = mobdb_searchname(str)) == 0)
  12182. mob_id = mobdb_checkid(atoi(str));
  12183. std::shared_ptr<s_mob_db> mob = mob_db.find(mob_id);
  12184. if( mob != nullptr ) {
  12185. StringBuf_Init(&command);
  12186. StringBuf_Printf(&command, "%cmonster %s", atcommand_symbol, mob->sprite.c_str());
  12187. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12188. StringBuf_Destroy(&command);
  12189. return;
  12190. }
  12191. }
  12192. /// /hide (CZ_CHANGE_EFFECTSTATE).
  12193. /// 019d <effect state>.L
  12194. /// effect state:
  12195. /// TODO: Any OPTION_* ?
  12196. void clif_parse_GMHide(int fd, map_session_data *sd) {
  12197. char cmd[6];
  12198. //int eff_st = RFIFOL(packet_db[RFIFOW(fd,0)].pos[0]);
  12199. safesnprintf(cmd,sizeof(cmd),"%chide",atcommand_symbol);
  12200. is_atcommand(fd, sd, cmd, 1);
  12201. }
  12202. /// /resetcooltime
  12203. /// 0a88 (CZ_CMD_RESETCOOLTIME).
  12204. void clif_parse_gm_resetcooltime( int fd, map_session_data* sd ){
  12205. #if PACKETVER_MAIN_NUM >= 20160622 || PACKETVER_RE_NUM >= 20160622 || defined(PACKETVER_ZERO)
  12206. const PACKET_CZ_CMD_RESETCOOLTIME* p = reinterpret_cast<const PACKET_CZ_CMD_RESETCOOLTIME*>( RFIFOP( fd, 0 ) );
  12207. char cmd[CHAT_SIZE_MAX];
  12208. safesnprintf(cmd,sizeof(cmd),"%cresetcooltime",atcommand_symbol);
  12209. is_atcommand(fd, sd, cmd, 1);
  12210. #endif
  12211. }
  12212. /// Request to adjust player's manner points (CZ_REQ_GIVE_MANNER_POINT).
  12213. /// 0149 <account id>.L <type>.B <value>.W
  12214. /// type:
  12215. /// 0 = positive points
  12216. /// 1 = negative points
  12217. /// 2 = self mute (+10 minutes)
  12218. void clif_parse_GMReqNoChat(int fd,map_session_data *sd)
  12219. {
  12220. int id, type, value;
  12221. map_session_data *dstsd;
  12222. char command[NAME_LENGTH+15];
  12223. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12224. id = RFIFOL(fd,info->pos[0]);
  12225. type = RFIFOB(fd,info->pos[1]);
  12226. value = RFIFOW(fd,info->pos[2]);
  12227. if( type == 0 )
  12228. value = -value;
  12229. //If type is 2 and the ids don't match, this is a crafted hacked packet!
  12230. //Disabled because clients keep self-muting when you give players public @ commands... [Skotlex]
  12231. if (type == 2 /* && (pc_get_group_level(sd) > 0 || sd->bl.id != id)*/)
  12232. return;
  12233. dstsd = map_id2sd(id);
  12234. if( dstsd == nullptr )
  12235. return;
  12236. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, value, dstsd->status.name);
  12237. is_atcommand(fd, sd, command, 1);
  12238. }
  12239. /// /rc (CZ_REQ_GIVE_MANNER_BYNAME).
  12240. /// GM adjustment of a player's manner value by -60.
  12241. /// 0212 <char name>.24B
  12242. void clif_parse_GMRc(int fd, map_session_data* sd)
  12243. {
  12244. char command[NAME_LENGTH+15];
  12245. char *name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12246. name[NAME_LENGTH-1] = '\0';
  12247. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, 60, name);
  12248. is_atcommand(fd, sd, command, 1);
  12249. }
  12250. /// Result of request to resolve account name (ZC_ACK_ACCOUNTNAME).
  12251. /// 01e0 <account id>.L <account name>.24B
  12252. void clif_account_name(int fd, uint32 account_id, const char* accname)
  12253. {
  12254. WFIFOHEAD(fd,packet_len(0x1e0));
  12255. WFIFOW(fd,0) = 0x1e0;
  12256. WFIFOL(fd,2) = account_id;
  12257. safestrncpy(WFIFOCP(fd,6), accname, NAME_LENGTH);
  12258. WFIFOSET(fd,packet_len(0x1e0));
  12259. }
  12260. /// GM requesting account name (for right-click gm menu) (CZ_REQ_ACCOUNTNAME).
  12261. /// 01df <account id>.L
  12262. //! TODO: Figure out how does this actually work
  12263. void clif_parse_GMReqAccountName(int fd, map_session_data *sd)
  12264. {
  12265. uint32 account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12266. /*
  12267. char query[30];
  12268. safesnprintf(query,sizeof(query),"%d", account_id);
  12269. intif_request_accinfo(sd->fd, sd->bl.id, pc_get_group_level(sd), query, 1); //will call clif_account_name at return
  12270. */
  12271. clif_account_name(fd, account_id, "");
  12272. }
  12273. /// /changemaptype <x> <y> <type> (CZ_CHANGE_MAPTYPE).
  12274. /// GM single cell type change request.
  12275. /// 0198 <x>.W <y>.W <type>.W
  12276. /// type:
  12277. /// 0 = not walkable
  12278. /// 1 = walkable
  12279. void clif_parse_GMChangeMapType(int fd, map_session_data *sd)
  12280. {
  12281. int x,y,type;
  12282. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12283. if(! pc_has_permission(sd, PC_PERM_USE_CHANGEMAPTYPE) )
  12284. return;
  12285. x = RFIFOW(fd,info->pos[0]);
  12286. y = RFIFOW(fd,info->pos[1]);
  12287. type = RFIFOW(fd,info->pos[2]);
  12288. map_setgatcell(sd->bl.m,x,y,type);
  12289. clif_changemapcell(0,sd->bl.m,x,y,type,ALL_SAMEMAP);
  12290. //FIXME: once players leave the map, the client 'forgets' this information.
  12291. }
  12292. /// /in /ex (CZ_SETTING_WHISPER_PC).
  12293. /// Request to allow/deny whispers from a nick.
  12294. /// 00cf <nick>.24B <type>.B
  12295. /// type:
  12296. /// 0 = (/ex nick) deny speech from nick
  12297. /// 1 = (/in nick) allow speech from nick
  12298. void clif_parse_PMIgnore(int fd, map_session_data* sd)
  12299. {
  12300. char* nick;
  12301. uint8 type;
  12302. int i;
  12303. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12304. nick = RFIFOCP(fd,info->pos[0]);
  12305. nick[NAME_LENGTH-1] = '\0'; // to be sure that the player name has at most 23 characters
  12306. type = RFIFOB(fd,info->pos[1]);
  12307. if( type == 0 ) { // Add name to ignore list (block)
  12308. if (strcmp(wisp_server_name, nick) == 0) {
  12309. clif_wisexin(sd, type, 1); // fail
  12310. return;
  12311. }
  12312. // try to find a free spot, while checking for duplicates at the same time
  12313. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12314. if( i == MAX_IGNORE_LIST ) {// no space for new entry
  12315. clif_wisexin(sd, type, 2); // too many blocks
  12316. return;
  12317. }
  12318. if( sd->ignore[i].name[0] != '\0' ) {// name already exists
  12319. clif_wisexin(sd, type, 0); // Aegis reports success.
  12320. return;
  12321. }
  12322. //Insert in position i
  12323. safestrncpy(sd->ignore[i].name, nick, NAME_LENGTH);
  12324. } else { // Remove name from ignore list (unblock)
  12325. // find entry
  12326. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12327. if( i == MAX_IGNORE_LIST || sd->ignore[i].name[0] == '\0' ) { //Not found
  12328. clif_wisexin(sd, type, 1); // fail
  12329. return;
  12330. }
  12331. // move everything one place down to overwrite removed entry
  12332. memmove(sd->ignore[i].name, sd->ignore[i+1].name, (MAX_IGNORE_LIST-i-1)*sizeof(sd->ignore[0].name));
  12333. // wipe last entry
  12334. memset(sd->ignore[MAX_IGNORE_LIST-1].name, 0, sizeof(sd->ignore[0].name));
  12335. }
  12336. clif_wisexin(sd, type, 0); // success
  12337. }
  12338. /// /inall /exall (CZ_SETTING_WHISPER_STATE).
  12339. /// Request to allow/deny all whispers.
  12340. /// 00d0 <type>.B
  12341. /// type:
  12342. /// 0 = (/exall) deny all speech
  12343. /// 1 = (/inall) allow all speech
  12344. void clif_parse_PMIgnoreAll(int fd, map_session_data *sd)
  12345. {
  12346. int type = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]), flag;
  12347. if( type == 0 ) {// Deny all
  12348. if( sd->state.ignoreAll ) {
  12349. flag = 1; // fail
  12350. } else {
  12351. sd->state.ignoreAll = 1;
  12352. flag = 0; // success
  12353. }
  12354. } else {//Unblock everyone
  12355. if( sd->state.ignoreAll ) {
  12356. sd->state.ignoreAll = 0;
  12357. flag = 0; // success
  12358. } else {
  12359. if (sd->ignore[0].name[0] != '\0')
  12360. { //Wipe the ignore list.
  12361. memset(sd->ignore, 0, sizeof(sd->ignore));
  12362. flag = 0; // success
  12363. } else {
  12364. flag = 1; // fail
  12365. }
  12366. }
  12367. }
  12368. clif_wisall(sd, type, flag);
  12369. }
  12370. /// Whisper ignore list (ZC_WHISPER_LIST).
  12371. /// 00d4 <packet len>.W { <char name>.24B }*
  12372. void clif_PMIgnoreList(map_session_data* sd)
  12373. {
  12374. int i, fd = sd->fd;
  12375. WFIFOHEAD(fd,4+ARRAYLENGTH(sd->ignore)*NAME_LENGTH);
  12376. WFIFOW(fd,0) = 0xd4;
  12377. for( i = 0; i < ARRAYLENGTH(sd->ignore) && sd->ignore[i].name[0]; i++ ) {
  12378. safestrncpy(WFIFOCP(fd,4+i*NAME_LENGTH), sd->ignore[i].name, NAME_LENGTH);
  12379. }
  12380. WFIFOW(fd,2) = 4+i*NAME_LENGTH;
  12381. WFIFOSET(fd,WFIFOW(fd,2));
  12382. }
  12383. /// Whisper ignore list request (CZ_REQ_WHISPER_LIST).
  12384. /// 00d3
  12385. void clif_parse_PMIgnoreList(int fd,map_session_data *sd)
  12386. {
  12387. clif_PMIgnoreList(sd);
  12388. }
  12389. /// Request to invoke the /doridori recovery bonus (CZ_DORIDORI).
  12390. /// 01e7
  12391. void clif_parse_NoviceDoriDori(int fd, map_session_data *sd)
  12392. {
  12393. if (sd->state.doridori) return;
  12394. switch (sd->class_&MAPID_UPPERMASK) {
  12395. case MAPID_SOUL_LINKER:
  12396. case MAPID_STAR_GLADIATOR:
  12397. case MAPID_TAEKWON:
  12398. if (!sd->state.rest)
  12399. break;
  12400. [[fallthrough]];
  12401. case MAPID_SUPER_NOVICE:
  12402. case MAPID_SUPER_BABY:
  12403. case MAPID_SUPER_NOVICE_E:
  12404. case MAPID_SUPER_BABY_E:
  12405. sd->state.doridori=1;
  12406. break;
  12407. }
  12408. }
  12409. /// Request to invoke the effect of super novice's guardian angel prayer (CZ_CHOPOKGI).
  12410. /// 01ed
  12411. /// Note: This packet is caused by 7 lines of any text, followed by
  12412. /// the prayer and an another line of any text. The prayer is
  12413. /// defined by lines 790~793 in data\msgstringtable.txt
  12414. /// "Dear angel, can you hear my voice?"
  12415. /// "I am" (space separated player name) "Super Novice~"
  12416. /// "Help me out~ Please~ T_T"
  12417. void clif_parse_NoviceExplosionSpirits(int fd, map_session_data *sd)
  12418. {
  12419. if( (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE ) {
  12420. t_exp next = pc_nextbaseexp(sd);
  12421. if( next ) {
  12422. int percent = (int)( ( (double)sd->status.base_exp/(double)next )*1000. );
  12423. if( percent && ( percent%100 ) == 0 ) {// 10.0%, 20.0%, ..., 90.0%
  12424. sc_start(&sd->bl,&sd->bl, SC_EXPLOSIONSPIRITS, 100, 17, skill_get_time(MO_EXPLOSIONSPIRITS, 5)); //Lv17-> +50 critical (noted by Poki) [Skotlex]
  12425. clif_skill_nodamage(&sd->bl, &sd->bl, MO_EXPLOSIONSPIRITS, 5, 1); // prayer always shows successful Lv5 cast and disregards noskill restrictions
  12426. }
  12427. }
  12428. }
  12429. }
  12430. /// Friends List
  12431. ///
  12432. /// Toggles a single friend online/offline [Skotlex] (ZC_FRIENDS_STATE).
  12433. /// 0206 <account id>.L <char id>.L <state>.B
  12434. /// 0206 <account id>.L <char id>.L <state>.B <name>.24B >= 20180221
  12435. /// state:
  12436. /// 0 = online
  12437. /// 1 = offline
  12438. void clif_friendslist_toggle(map_session_data *sd,uint32 account_id, uint32 char_id, int online)
  12439. {
  12440. int i, fd = sd->fd;
  12441. //Seek friend.
  12442. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id &&
  12443. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12444. if(i == MAX_FRIENDS || sd->status.friends[i].char_id == 0)
  12445. return; //Not found
  12446. WFIFOHEAD(fd,packet_len(0x206));
  12447. WFIFOW(fd, 0) = 0x206;
  12448. WFIFOL(fd, 2) = sd->status.friends[i].account_id;
  12449. WFIFOL(fd, 6) = sd->status.friends[i].char_id;
  12450. WFIFOB(fd,10) = !online; //Yeah, a 1 here means "logged off", go figure...
  12451. #if PACKETVER >= 20180221
  12452. safestrncpy(WFIFOCP(fd, 11), sd->status.friends[i].name, NAME_LENGTH);
  12453. #endif
  12454. WFIFOSET(fd, packet_len(0x206));
  12455. }
  12456. //Subfunction called from clif_foreachclient to toggle friends on/off [Skotlex]
  12457. int clif_friendslist_toggle_sub(map_session_data *sd,va_list ap)
  12458. {
  12459. uint32 account_id, char_id, online;
  12460. account_id = va_arg(ap, int);
  12461. char_id = va_arg(ap, int);
  12462. online = va_arg(ap, int);
  12463. clif_friendslist_toggle(sd, account_id, char_id, online);
  12464. return 0;
  12465. }
  12466. /// Sends the whole friends list (ZC_FRIENDS_LIST).
  12467. /// 0201 <packet len>.W { <account id>.L <char id>.L <name>.24B }*
  12468. /// 0201 <packet len>.W { <account id>.L <char id>.L }* >= 20180221
  12469. void clif_friendslist_send( map_session_data& sd ){
  12470. PACKET_ZC_FRIENDS_LIST* p = reinterpret_cast<PACKET_ZC_FRIENDS_LIST*>( packet_buffer );
  12471. p->packetType = HEADER_ZC_FRIENDS_LIST;
  12472. p->PacketLength = sizeof( *p );
  12473. for( int i = 0, c = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12474. PACKET_ZC_FRIENDS_LIST_sub& f = p->friends[c];
  12475. f.AID = sd.status.friends[i].account_id;
  12476. f.CID = sd.status.friends[i].char_id;
  12477. #if !( PACKETVER_MAIN_NUM >= 20180307 || PACKETVER_RE_NUM >= 20180221 || PACKETVER_ZERO_NUM >= 20180328 ) || PACKETVER >= 20200902
  12478. safestrncpy( f.name, sd.status.friends[i].name, sizeof( f.name ) );
  12479. #endif
  12480. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( f ) );
  12481. c++;
  12482. }
  12483. clif_send( p, p->PacketLength, &sd.bl, SELF );
  12484. // Sending the online players
  12485. for( int i = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12486. if( map_charid2sd( sd.status.friends[i].char_id ) ){
  12487. clif_friendslist_toggle( &sd, sd.status.friends[i].account_id, sd.status.friends[i].char_id, 1 );
  12488. }
  12489. }
  12490. }
  12491. /// Notification about the result of a friend add request (ZC_ADD_FRIENDS_LIST).
  12492. /// 0209 <result>.W <account id>.L <char id>.L <name>.24B
  12493. /// result:
  12494. /// 0 = MsgStringTable[821]="You have become friends with (%s)."
  12495. /// 1 = MsgStringTable[822]="(%s) does not want to be friends with you."
  12496. /// 2 = MsgStringTable[819]="Your Friend List is full."
  12497. /// 3 = MsgStringTable[820]="(%s)'s Friend List is full."
  12498. void clif_friendslist_reqack(map_session_data *sd, map_session_data *f_sd, int type)
  12499. {
  12500. int fd;
  12501. nullpo_retv(sd);
  12502. fd = sd->fd;
  12503. WFIFOHEAD(fd,packet_len(0x209));
  12504. WFIFOW(fd,0) = 0x209;
  12505. WFIFOW(fd,2) = type;
  12506. if (f_sd) {
  12507. WFIFOL(fd,4) = f_sd->status.account_id;
  12508. WFIFOL(fd,8) = f_sd->status.char_id;
  12509. safestrncpy(WFIFOCP(fd, 12), f_sd->status.name,NAME_LENGTH);
  12510. }
  12511. WFIFOSET(fd, packet_len(0x209));
  12512. }
  12513. /// Asks a player for permission to be added as friend (ZC_REQ_ADD_FRIENDS).
  12514. /// 0207 <req account id>.L <req char id>.L <req char name>.24B
  12515. void clif_friendlist_req(map_session_data* sd, uint32 account_id, uint32 char_id, const char* name)
  12516. {
  12517. int fd = sd->fd;
  12518. WFIFOHEAD(fd,packet_len(0x207));
  12519. WFIFOW(fd,0) = 0x207;
  12520. WFIFOL(fd,2) = account_id;
  12521. WFIFOL(fd,6) = char_id;
  12522. safestrncpy(WFIFOCP(fd,10), name, NAME_LENGTH);
  12523. WFIFOSET(fd,packet_len(0x207));
  12524. }
  12525. /// Request to add a player as friend (CZ_ADD_FRIENDS).
  12526. /// 0202 <name>.24B
  12527. void clif_parse_FriendsListAdd(int fd, map_session_data *sd)
  12528. {
  12529. map_session_data *f_sd;
  12530. int i;
  12531. f_sd = map_nick2sd(RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]),false);
  12532. // Friend doesn't exist (no player with this name)
  12533. if (f_sd == nullptr) {
  12534. clif_displaymessage(fd, msg_txt(sd,3));
  12535. return;
  12536. }
  12537. if( sd->bl.id == f_sd->bl.id ) {// adding oneself as friend
  12538. return;
  12539. }
  12540. // ensure that the request player's friend list is not full
  12541. ARR_FIND(0, MAX_FRIENDS, i, sd->status.friends[i].char_id == 0);
  12542. if( i == MAX_FRIENDS ){
  12543. clif_friendslist_reqack(sd, f_sd, 2);
  12544. return;
  12545. }
  12546. // @noask [LuzZza]
  12547. if(f_sd->state.noask) {
  12548. clif_noask_sub( *sd, *f_sd, 398 ); // Autorejected friend request from %s.
  12549. return;
  12550. }
  12551. // Friend already exists
  12552. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id != 0; i++) {
  12553. if (sd->status.friends[i].char_id == f_sd->status.char_id) {
  12554. clif_displaymessage(fd, msg_txt(sd,671)); //"Friend already exists."
  12555. return;
  12556. }
  12557. }
  12558. f_sd->friend_req = sd->status.char_id;
  12559. sd->friend_req = f_sd->status.char_id;
  12560. clif_friendlist_req(f_sd, sd->status.account_id, sd->status.char_id, sd->status.name);
  12561. }
  12562. /// Answer to a friend add request (CZ_ACK_REQ_ADD_FRIENDS).
  12563. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.B
  12564. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.L (PACKETVER >= 6)
  12565. /// reply:
  12566. /// 0 = rejected
  12567. /// 1 = accepted
  12568. void clif_parse_FriendsListReply(int fd, map_session_data *sd)
  12569. {
  12570. map_session_data *f_sd;
  12571. uint32 account_id;
  12572. char reply;
  12573. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12574. account_id = RFIFOL(fd,info->pos[0]);
  12575. //char_id = RFIFOL(fd,info->pos[1]);
  12576. #if PACKETVER < 6
  12577. reply = RFIFOB(fd,info->pos[2]);
  12578. #else
  12579. reply = RFIFOL(fd,info->pos[2]);
  12580. #endif
  12581. if( sd->bl.id == account_id ) {// adding oneself as friend
  12582. return;
  12583. }
  12584. f_sd = map_id2sd(account_id); //The account id is the same as the bl.id of players.
  12585. if (f_sd == nullptr)
  12586. return;
  12587. if (reply == 0 || !( sd->friend_req == f_sd->status.char_id && f_sd->friend_req == sd->status.char_id ) )
  12588. clif_friendslist_reqack(f_sd, sd, 1);
  12589. else {
  12590. int i;
  12591. // Find an empty slot
  12592. for (i = 0; i < MAX_FRIENDS; i++)
  12593. if (f_sd->status.friends[i].char_id == 0)
  12594. break;
  12595. if (i == MAX_FRIENDS) {
  12596. clif_friendslist_reqack(f_sd, sd, 2);
  12597. return;
  12598. }
  12599. f_sd->status.friends[i].account_id = sd->status.account_id;
  12600. f_sd->status.friends[i].char_id = sd->status.char_id;
  12601. safestrncpy(f_sd->status.friends[i].name, sd->status.name, NAME_LENGTH);
  12602. clif_friendslist_reqack(f_sd, sd, 0);
  12603. achievement_update_objective(f_sd, AG_ADD_FRIEND, 1, i + 1);
  12604. if (battle_config.friend_auto_add) {
  12605. // Also add f_sd to sd's friendlist.
  12606. for (i = 0; i < MAX_FRIENDS; i++) {
  12607. if (sd->status.friends[i].char_id == f_sd->status.char_id)
  12608. return; //No need to add anything.
  12609. if (sd->status.friends[i].char_id == 0)
  12610. break;
  12611. }
  12612. if (i == MAX_FRIENDS) {
  12613. clif_friendslist_reqack(sd, f_sd, 2);
  12614. return;
  12615. }
  12616. sd->status.friends[i].account_id = f_sd->status.account_id;
  12617. sd->status.friends[i].char_id = f_sd->status.char_id;
  12618. safestrncpy(sd->status.friends[i].name, f_sd->status.name, NAME_LENGTH);
  12619. clif_friendslist_reqack(sd, f_sd, 0);
  12620. achievement_update_objective(sd, AG_ADD_FRIEND, 1, i + 1);
  12621. }
  12622. }
  12623. }
  12624. /// Request to delete a friend (CZ_DELETE_FRIENDS).
  12625. /// 0203 <account id>.L <char id>.L
  12626. void clif_parse_FriendsListRemove(int fd, map_session_data *sd)
  12627. {
  12628. map_session_data *f_sd = nullptr;
  12629. uint32 account_id, char_id;
  12630. int i, j;
  12631. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12632. account_id = RFIFOL(fd,info->pos[0]);
  12633. char_id = RFIFOL(fd,info->pos[1]);
  12634. // Search friend
  12635. for (i = 0; i < MAX_FRIENDS &&
  12636. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12637. if (i == MAX_FRIENDS) {
  12638. clif_displaymessage(fd, msg_txt(sd,672)); //"Name not found in list."
  12639. return;
  12640. }
  12641. //remove from friend's list first
  12642. if( (f_sd = map_id2sd(account_id)) && f_sd->status.char_id == char_id) {
  12643. for (i = 0; i < MAX_FRIENDS &&
  12644. (f_sd->status.friends[i].char_id != sd->status.char_id || f_sd->status.friends[i].account_id != sd->status.account_id); i++);
  12645. if (i != MAX_FRIENDS) {
  12646. // move all chars up
  12647. for(j = i + 1; j < MAX_FRIENDS; j++)
  12648. memcpy(&f_sd->status.friends[j-1], &f_sd->status.friends[j], sizeof(f_sd->status.friends[0]));
  12649. memset(&f_sd->status.friends[MAX_FRIENDS-1], 0, sizeof(f_sd->status.friends[MAX_FRIENDS-1]));
  12650. //should the guy be notified of some message? we should add it here if so
  12651. WFIFOHEAD(f_sd->fd,packet_len(0x20a));
  12652. WFIFOW(f_sd->fd,0) = 0x20a;
  12653. WFIFOL(f_sd->fd,2) = sd->status.account_id;
  12654. WFIFOL(f_sd->fd,6) = sd->status.char_id;
  12655. WFIFOSET(f_sd->fd, packet_len(0x20a));
  12656. }
  12657. } else { //friend not online -- ask char server to delete from his friendlist
  12658. if(chrif_removefriend(char_id,sd->status.char_id)) { // char-server offline, abort
  12659. clif_displaymessage(fd, msg_txt(sd,673)); //"This action can't be performed at the moment. Please try again later."
  12660. return;
  12661. }
  12662. }
  12663. // We can now delete from original requester
  12664. for (i = 0; i < MAX_FRIENDS &&
  12665. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12666. // move all chars up
  12667. for(j = i + 1; j < MAX_FRIENDS; j++)
  12668. memcpy(&sd->status.friends[j-1], &sd->status.friends[j], sizeof(sd->status.friends[0]));
  12669. memset(&sd->status.friends[MAX_FRIENDS-1], 0, sizeof(sd->status.friends[MAX_FRIENDS-1]));
  12670. clif_displaymessage(fd, msg_txt(sd,674)); //"Friend removed"
  12671. WFIFOHEAD(fd,packet_len(0x20a));
  12672. WFIFOW(fd,0) = 0x20a;
  12673. WFIFOL(fd,2) = account_id;
  12674. WFIFOL(fd,6) = char_id;
  12675. WFIFOSET(fd, packet_len(0x20a));
  12676. }
  12677. /// /pvpinfo list (ZC_ACK_PVPPOINT).
  12678. /// 0210 <char id>.L <account id>.L <win point>.L <lose point>.L <point>.L
  12679. void clif_PVPInfo(map_session_data* sd)
  12680. {
  12681. int fd = sd->fd;
  12682. WFIFOHEAD(fd,packet_len(0x210));
  12683. WFIFOW(fd,0) = 0x210;
  12684. WFIFOL(fd,2) = sd->status.char_id;
  12685. WFIFOL(fd,6) = sd->status.account_id;
  12686. WFIFOL(fd,10) = sd->pvp_won; // times won
  12687. WFIFOL(fd,14) = sd->pvp_lost; // times lost
  12688. WFIFOL(fd,18) = sd->pvp_point;
  12689. WFIFOSET(fd, packet_len(0x210));
  12690. }
  12691. /// /pvpinfo (CZ_REQ_PVPPOINT).
  12692. /// 020f <char id>.L <account id>.L
  12693. void clif_parse_PVPInfo(int fd,map_session_data *sd)
  12694. {
  12695. // TODO: Is there a way to use this on an another player (char/acc id)?
  12696. //int cid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12697. //int aid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  12698. clif_PVPInfo(sd);
  12699. }
  12700. /// SG Feel save OK [Komurka] (CZ_AGREE_STARPLACE).
  12701. /// 0254 <which>.B
  12702. /// which: (unused atm for security purpose)
  12703. /// 0 = sun
  12704. /// 1 = moon
  12705. /// 2 = star
  12706. void clif_parse_FeelSaveOk(int fd,map_session_data *sd)
  12707. {
  12708. int i;
  12709. //int wich = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12710. if (sd->menuskill_id != SG_FEEL)
  12711. return;
  12712. i = sd->menuskill_val-1;
  12713. if (i<0 || i >= MAX_PC_FEELHATE) return; //Bug?
  12714. sd->feel_map[i].index = map_id2index(sd->bl.m);
  12715. sd->feel_map[i].m = sd->bl.m;
  12716. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), sd->feel_map[i].index);
  12717. //Are these really needed? Shouldn't they show up automatically from the feel save packet?
  12718. // clif_misceffect2(&sd->bl, 0x1b0);
  12719. // clif_misceffect2(&sd->bl, 0x21f);
  12720. clif_feel_info(sd, i, 0);
  12721. clif_menuskill_clear(sd);
  12722. }
  12723. /// Star Gladiator's Feeling map confirmation prompt (ZC_STARPLACE).
  12724. /// 0253 <which>.B
  12725. /// which:
  12726. /// 0 = sun
  12727. /// 1 = moon
  12728. /// 2 = star
  12729. void clif_feel_req(int fd, map_session_data *sd, uint16 skill_lv)
  12730. {
  12731. WFIFOHEAD(fd,packet_len(0x253));
  12732. WFIFOW(fd,0)=0x253;
  12733. WFIFOB(fd,2)=TOB(skill_lv-1);
  12734. WFIFOSET(fd, packet_len(0x253));
  12735. sd->menuskill_id = SG_FEEL;
  12736. sd->menuskill_val = skill_lv;
  12737. }
  12738. /// Request to change homunculus' name (CZ_RENAME_MER).
  12739. /// 0231 <name>.24B
  12740. void clif_parse_ChangeHomunculusName(int fd, map_session_data *sd){
  12741. hom_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12742. }
  12743. /// Request to warp/move homunculus/mercenary to it's owner (CZ_REQUEST_MOVETOOWNER).
  12744. /// 0234 <id>.L
  12745. void clif_parse_HomMoveToMaster(int fd, map_session_data *sd){
  12746. int id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]); // Mercenary or Homunculus
  12747. struct block_list *bl = nullptr;
  12748. struct unit_data *ud = nullptr;
  12749. if( sd->md && sd->md->bl.id == id )
  12750. bl = &sd->md->bl;
  12751. else if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12752. bl = &sd->hd->bl; // Moving Homunculus
  12753. else
  12754. return;
  12755. unit_calc_pos(bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  12756. ud = unit_bl2ud(bl);
  12757. unit_walktoxy(bl, ud->to_x, ud->to_y, 4);
  12758. }
  12759. /// Request to move homunculus/mercenary (CZ_REQUEST_MOVENPC).
  12760. /// 0232 <id>.L <position data>.3B
  12761. void clif_parse_HomMoveTo(int fd, map_session_data *sd){
  12762. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12763. int id = RFIFOL(fd,info->pos[0]); // Mercenary or Homunculus
  12764. struct block_list *bl = nullptr;
  12765. short x, y;
  12766. RFIFOPOS(fd, info->pos[1], &x, &y, nullptr);
  12767. if( sd->md && sd->md->bl.id == id )
  12768. bl = &sd->md->bl; // Moving Mercenary
  12769. else if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12770. bl = &sd->hd->bl; // Moving Homunculus
  12771. else
  12772. return;
  12773. unit_walktoxy(bl, x, y, 4);
  12774. }
  12775. /// Request to do an action with homunculus/mercenary (CZ_REQUEST_ACTNPC).
  12776. /// 0233 <id>.L <target id>.L <action>.B
  12777. /// action:
  12778. /// always 0
  12779. void clif_parse_HomAttack(int fd,map_session_data *sd)
  12780. {
  12781. struct block_list *bl = nullptr;
  12782. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12783. int id = RFIFOL(fd,info->pos[0]);
  12784. int target_id = RFIFOL(fd,info->pos[1]);
  12785. int action_type = RFIFOB(fd,info->pos[2]);
  12786. if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12787. bl = &sd->hd->bl;
  12788. else if( sd->md && sd->md->bl.id == id )
  12789. bl = &sd->md->bl;
  12790. else return;
  12791. unit_stop_attack(bl);
  12792. unit_attack(bl, target_id, action_type != 0);
  12793. }
  12794. /// Request to invoke a homunculus menu action (CZ_COMMAND_MER).
  12795. /// 022d <type>.W <command>.B
  12796. /// type:
  12797. /// always 0
  12798. /// command:
  12799. /// 0 = homunculus information
  12800. /// 1 = feed
  12801. /// 2 = delete
  12802. void clif_parse_HomMenu(int fd, map_session_data *sd)
  12803. { //[orn]
  12804. int cmd = RFIFOW(fd,0);
  12805. //int type = RFIFOW(fd,packet_db[cmd].pos[0]);
  12806. if(!hom_is_active(sd->hd))
  12807. return;
  12808. hom_menu(sd,RFIFOB(fd,packet_db[cmd].pos[1]));
  12809. }
  12810. /// Request to resurrect oneself using Token of Siegfried (CZ_STANDING_RESURRECTION).
  12811. /// 0292
  12812. void clif_parse_AutoRevive(int fd, map_session_data *sd)
  12813. {
  12814. pc_revive_item(sd);
  12815. }
  12816. /// Information about character's status values (ZC_ACK_STATUS_GM).
  12817. /// 0214 <str>.B <standardStr>.B <agi>.B <standardAgi>.B <vit>.B <standardVit>.B
  12818. /// <int>.B <standardInt>.B <dex>.B <standardDex>.B <luk>.B <standardLuk>.B
  12819. /// <attPower>.W <refiningPower>.W <max_mattPower>.W <min_mattPower>.W
  12820. /// <itemdefPower>.W <plusdefPower>.W <mdefPower>.W <plusmdefPower>.W
  12821. /// <hitSuccessValue>.W <avoidSuccessValue>.W <plusAvoidSuccessValue>.W
  12822. /// <criticalSuccessValue>.W <ASPD>.W <plusASPD>.W
  12823. void clif_check(int fd, map_session_data* pl_sd)
  12824. {
  12825. WFIFOHEAD(fd,packet_len(0x214));
  12826. WFIFOW(fd, 0) = 0x214;
  12827. WFIFOB(fd, 2) = min(pl_sd->status.str, UINT8_MAX);
  12828. WFIFOB(fd, 3) = pc_need_status_point(pl_sd, SP_STR, 1);
  12829. WFIFOB(fd, 4) = min(pl_sd->status.agi, UINT8_MAX);
  12830. WFIFOB(fd, 5) = pc_need_status_point(pl_sd, SP_AGI, 1);
  12831. WFIFOB(fd, 6) = min(pl_sd->status.vit, UINT8_MAX);
  12832. WFIFOB(fd, 7) = pc_need_status_point(pl_sd, SP_VIT, 1);
  12833. WFIFOB(fd, 8) = min(pl_sd->status.int_, UINT8_MAX);
  12834. WFIFOB(fd, 9) = pc_need_status_point(pl_sd, SP_INT, 1);
  12835. WFIFOB(fd,10) = min(pl_sd->status.dex, UINT8_MAX);
  12836. WFIFOB(fd,11) = pc_need_status_point(pl_sd, SP_DEX, 1);
  12837. WFIFOB(fd,12) = min(pl_sd->status.luk, UINT8_MAX);
  12838. WFIFOB(fd,13) = pc_need_status_point(pl_sd, SP_LUK, 1);
  12839. WFIFOW(fd,14) = pl_sd->battle_status.batk+pl_sd->battle_status.rhw.atk+pl_sd->battle_status.lhw.atk;
  12840. WFIFOW(fd,16) = pl_sd->battle_status.rhw.atk2+pl_sd->battle_status.lhw.atk2;
  12841. WFIFOW(fd,18) = pl_sd->battle_status.matk_max;
  12842. WFIFOW(fd,20) = pl_sd->battle_status.matk_min;
  12843. WFIFOW(fd,22) = pl_sd->battle_status.def;
  12844. WFIFOW(fd,24) = pl_sd->battle_status.def2;
  12845. WFIFOW(fd,26) = pl_sd->battle_status.mdef;
  12846. WFIFOW(fd,28) = pl_sd->battle_status.mdef2;
  12847. WFIFOW(fd,30) = pl_sd->battle_status.hit;
  12848. WFIFOW(fd,32) = pl_sd->battle_status.flee;
  12849. WFIFOW(fd,34) = pl_sd->battle_status.flee2/10;
  12850. WFIFOW(fd,36) = pl_sd->battle_status.cri/10;
  12851. WFIFOW(fd,38) = (2000-pl_sd->battle_status.amotion)/10; // aspd
  12852. WFIFOW(fd,40) = 0; // FIXME: What is 'plusASPD' supposed to be? Maybe adelay?
  12853. WFIFOSET(fd,packet_len(0x214));
  12854. }
  12855. /// /check (CZ_REQ_STATUS_GM).
  12856. /// Request character's status values.
  12857. /// 0213 <char name>.24B
  12858. void clif_parse_Check(int fd, map_session_data *sd)
  12859. {
  12860. char charname[NAME_LENGTH];
  12861. map_session_data* pl_sd;
  12862. if(!pc_has_permission(sd, PC_PERM_USE_CHECK))
  12863. return;
  12864. safestrncpy(charname, RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]), sizeof(charname));
  12865. if( ( pl_sd = map_nick2sd(charname,false) ) == nullptr || pc_get_group_level(sd) < pc_get_group_level(pl_sd) )
  12866. {
  12867. return;
  12868. }
  12869. clif_check(fd, pl_sd);
  12870. }
  12871. /// MAIL SYSTEM
  12872. /// By Zephyrus
  12873. ///
  12874. /// Notification about the result of adding an item to mail
  12875. /// 0255 <index>.W <result>.B (ZC_ACK_MAIL_ADD_ITEM)
  12876. /// result:
  12877. /// 0 = success
  12878. /// 1 = failure
  12879. /// 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)
  12880. /// { <card id>.W }*4 { <option index>.W <option value>.W <option parameter>.B }*5
  12881. /// result:
  12882. /// 0 = success
  12883. /// 1 = over weight
  12884. /// 2 = fatal error
  12885. void clif_Mail_setattachment( map_session_data* sd, int index, int amount, uint8 flag ){
  12886. int fd = sd->fd;
  12887. #if PACKETVER < 20150513
  12888. WFIFOHEAD(fd,packet_len(0x255));
  12889. WFIFOW(fd,0) = 0x255;
  12890. WFIFOW(fd,2) = index;
  12891. WFIFOB(fd,4) = flag;
  12892. WFIFOSET(fd,packet_len(0x255));
  12893. #else
  12894. PACKET_ZC_ACK_ADD_ITEM_RODEX p = {};
  12895. if( flag ){
  12896. memset( &p, 0, sizeof( p ) );
  12897. }else{
  12898. struct item* item = &sd->inventory.u.items_inventory[server_index(index)];
  12899. p.index = index;
  12900. p.count = amount;
  12901. p.itemId = client_nameid( item->nameid );
  12902. p.type = itemtype( item->nameid );
  12903. p.IsIdentified = item->identify ? 1 : 0;
  12904. p.IsDamaged = item->attribute ? 1 : 0;
  12905. p.refiningLevel = item->refine;
  12906. clif_addcards( &p.slot, item );
  12907. clif_add_random_options( p.optionData, *item );
  12908. p.weight = 0;
  12909. for( int i = 0; i < MAIL_MAX_ITEM; i++ ){
  12910. if( sd->mail.item[i].nameid == 0 ){
  12911. continue;
  12912. }
  12913. p.weight += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  12914. }
  12915. p.favorite = ( item->favorite != 0 ) ? 1 : 0;
  12916. p.location = pc_equippoint( sd, server_index( index ) );
  12917. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  12918. p.grade = item->enchantgrade;
  12919. #endif
  12920. }
  12921. p.PacketType = HEADER_ZC_ACK_ADD_ITEM_RODEX;
  12922. p.result = flag;
  12923. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  12924. #endif
  12925. }
  12926. /// Notification about the result of retrieving a mail attachment
  12927. /// 0245 <result>.B (ZC_MAIL_REQ_GET_ITEM)
  12928. /// result:
  12929. /// 0 = success
  12930. /// 1 = failure
  12931. /// 2 = too many items
  12932. /// 09f2 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ZENY_FROM_MAIL)
  12933. /// 09f4 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ITEM_FROM_MAIL)
  12934. void clif_mail_getattachment(map_session_data* sd, struct mail_message *msg, uint8 result, enum mail_attachment_type type) {
  12935. #if PACKETVER < 20150513
  12936. int fd = sd->fd;
  12937. WFIFOHEAD(fd,packet_len(0x245));
  12938. WFIFOW(fd,0) = 0x245;
  12939. WFIFOB(fd,2) = result;
  12940. WFIFOSET(fd,packet_len(0x245));
  12941. #else
  12942. switch( type ){
  12943. case MAIL_ATT_ITEM:
  12944. case MAIL_ATT_ZENY:
  12945. break;
  12946. default:
  12947. return;
  12948. }
  12949. PACKET_ZC_ACK_ITEM_FROM_MAIL p = { 0 };
  12950. p.PacketType = type == MAIL_ATT_ZENY ? rodexgetzeny : rodexgetitem;
  12951. p.MailID = msg->id;
  12952. p.opentype = msg->type;
  12953. p.result = result;
  12954. clif_send(&p, sizeof(p), &sd->bl, SELF);
  12955. clif_Mail_refreshinbox( sd, msg->type, 0 );
  12956. #endif
  12957. }
  12958. /// Notification about the result of sending a mail
  12959. /// 0249 <result>.B (ZC_MAIL_REQ_SEND)
  12960. /// result:
  12961. /// 0 = success
  12962. /// 1 = recipinent does not exist
  12963. /// 09ed <result>.B (ZC_ACK_WRITE_MAIL)
  12964. void clif_Mail_send(map_session_data* sd, enum mail_send_result result){
  12965. #if PACKETVER < 20150513
  12966. int fd = sd->fd;
  12967. WFIFOHEAD(fd,packet_len(0x249));
  12968. WFIFOW(fd,0) = 0x249;
  12969. WFIFOB(fd,2) = result != WRITE_MAIL_SUCCESS;
  12970. WFIFOSET(fd,packet_len(0x249));
  12971. #else
  12972. PACKET_ZC_WRITE_MAIL_RESULT p = { 0 };
  12973. p.PacketType = rodexwriteresult;
  12974. p.result = result;
  12975. clif_send(&p, sizeof(p), &sd->bl, SELF);
  12976. #endif
  12977. }
  12978. /// Notification about the result of deleting a mail.
  12979. /// 0257 <mail id>.L <result>.W (ZC_ACK_MAIL_DELETE)
  12980. /// result:
  12981. /// 0 = success
  12982. /// 1 = failure
  12983. // 09f6 <mail tab>.B <mail id>.Q (ZC_ACK_DELETE_MAIL)
  12984. void clif_mail_delete( map_session_data* sd, struct mail_message *msg, bool success ){
  12985. #if PACKETVER < 20150513
  12986. int fd = sd->fd;
  12987. WFIFOHEAD(fd, packet_len(0x257));
  12988. WFIFOW(fd,0) = 0x257;
  12989. WFIFOL(fd,2) = msg->id;
  12990. WFIFOW(fd,6) = !success;
  12991. WFIFOSET(fd, packet_len(0x257));
  12992. #else
  12993. // This is only a success notification
  12994. if( success ){
  12995. PACKET_ZC_ACK_DELETE_MAIL p = { 0 };
  12996. p.PacketType = rodexdelete;
  12997. p.opentype = msg->type;
  12998. p.MailID = msg->id;
  12999. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13000. }
  13001. #endif
  13002. }
  13003. /// Notification about the result of returning a mail (ZC_ACK_MAIL_RETURN).
  13004. /// 0274 <mail id>.L <result>.W
  13005. /// result:
  13006. /// 0 = success
  13007. /// 1 = failure
  13008. void clif_Mail_return(int fd, int mail_id, short fail)
  13009. {
  13010. WFIFOHEAD(fd,packet_len(0x274));
  13011. WFIFOW(fd,0) = 0x274;
  13012. WFIFOL(fd,2) = mail_id;
  13013. WFIFOW(fd,6) = fail;
  13014. WFIFOSET(fd,packet_len(0x274));
  13015. }
  13016. /// Notification about new mail.
  13017. /// 024a <mail id>.L <title>.40B <sender>.24B (ZC_MAIL_RECEIVE)
  13018. /// 09e7 <result>.B (ZC_NOTIFY_UNREADMAIL)
  13019. void clif_Mail_new(map_session_data* sd, int mail_id, const char *sender, const char *title){
  13020. #if PACKETVER < 20150513
  13021. int fd = sd->fd;
  13022. WFIFOHEAD(fd,packet_len(0x24a));
  13023. WFIFOW(fd,0) = 0x24a;
  13024. WFIFOL(fd,2) = mail_id;
  13025. safestrncpy(WFIFOCP(fd,6), title, MAIL_TITLE_LENGTH);
  13026. safestrncpy(WFIFOCP(fd,46), sender, NAME_LENGTH);
  13027. WFIFOSET(fd,packet_len(0x24a));
  13028. #else
  13029. PACKET_ZC_NOTIFY_UNREADMAIL p = { 0 };
  13030. p.PacketType = rodexicon;
  13031. p.result = sd->mail.inbox.unread > 0 || sd->mail.inbox.unchecked > 0; // unread
  13032. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13033. #endif
  13034. }
  13035. /// Opens/closes the mail window (ZC_MAIL_WINDOWS).
  13036. /// 0260 <type>.L
  13037. /// type:
  13038. /// 0 = open
  13039. /// 1 = close
  13040. void clif_Mail_window(int fd, int flag)
  13041. {
  13042. WFIFOHEAD(fd,packet_len(0x260));
  13043. WFIFOW(fd,0) = 0x260;
  13044. WFIFOL(fd,2) = flag;
  13045. WFIFOSET(fd,packet_len(0x260));
  13046. }
  13047. /// Lists mails stored in inbox.
  13048. /// 0240 <packet len>.W <amount>.L { <mail id>.L <title>.40B <read>.B <sender>.24B <time>.L }*amount (ZC_MAIL_REQ_GET_LIST)
  13049. /// read:
  13050. /// 0 = unread
  13051. /// 1 = read
  13052. /// 09f0 <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST)
  13053. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13054. /// 0a7d <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST2)
  13055. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13056. /// 0ac2 <packet len>.W <unknown>.B (ZC_ACK_MAIL_LIST3)
  13057. /// { <type>.B <mail id>.Q <read>.B <type>.B <sender>.24B <expires>.L <title length>.W <title>.?B }*
  13058. void clif_Mail_refreshinbox(map_session_data *sd,enum mail_inbox_type type,int64 mailID){
  13059. #if PACKETVER < 20150513
  13060. int fd = sd->fd;
  13061. struct mail_data *md = &sd->mail.inbox;
  13062. struct mail_message *msg;
  13063. int len, i, j;
  13064. len = 8 + (73 * md->amount);
  13065. WFIFOHEAD(fd,len);
  13066. WFIFOW(fd,0) = 0x240;
  13067. WFIFOW(fd,2) = len;
  13068. WFIFOL(fd,4) = md->amount;
  13069. for( i = j = 0; i < MAIL_MAX_INBOX && j < md->amount; i++ )
  13070. {
  13071. msg = &md->msg[i];
  13072. if (msg->id < 1)
  13073. continue;
  13074. WFIFOL(fd,8+73*j) = msg->id;
  13075. memcpy(WFIFOP(fd,12+73*j), msg->title, MAIL_TITLE_LENGTH);
  13076. WFIFOB(fd,52+73*j) = (msg->status != MAIL_UNREAD);
  13077. safestrncpy(WFIFOCP(fd,53+73*j), msg->send_name, NAME_LENGTH);
  13078. WFIFOL(fd,77+73*j) = (uint32)msg->timestamp;
  13079. j++;
  13080. }
  13081. WFIFOSET(fd,len);
  13082. if( md->full ) {// TODO: is this official?
  13083. char output[100];
  13084. safesnprintf(output,sizeof(output),"Inbox is full (Max %d). Delete some mails.", MAIL_MAX_INBOX);
  13085. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  13086. }
  13087. #else
  13088. int fd = sd->fd;
  13089. struct mail_data *md = &sd->mail.inbox;
  13090. struct mail_message *msg;
  13091. int i, j, k, offset, titleLength;
  13092. uint8 mailType, amount, remaining;
  13093. uint32 now = (uint32)time(nullptr);
  13094. #if PACKETVER >= 20170419
  13095. int cmd = 0xac2;
  13096. #elif PACKETVER >= 20160601
  13097. int cmd = 0xa7d;
  13098. #else
  13099. int cmd = 0x9f0;
  13100. #endif
  13101. if( battle_config.mail_daily_count ){
  13102. mail_refresh_remaining_amount(sd);
  13103. }
  13104. #if PACKETVER >= 20170419
  13105. // Always send all
  13106. i = md->amount;
  13107. #else
  13108. // If a starting mail id was sent
  13109. if( mailID != 0 ){
  13110. ARR_FIND( 0, md->amount, i, md->msg[i].id == mailID );
  13111. // Unknown mail
  13112. if( i == md->amount ){
  13113. // Ignore the request for now
  13114. return; // TODO: Should we just show the first page instead?
  13115. }
  13116. // It was actually the oldest/first mail, there is no further page
  13117. if( i == 0 ){
  13118. return;
  13119. }
  13120. // We actually want the next/older mail
  13121. i -= 1;
  13122. }else{
  13123. i = md->amount;
  13124. }
  13125. #endif
  13126. // Count the remaining mails from the starting mail or the beginning
  13127. // Only count mails of the target type(before 2017-04-19) and those that should not have been deleted already
  13128. for( j = i, remaining = 0; j >= 0; j-- ){
  13129. msg = &md->msg[j];
  13130. if (msg->id < 1)
  13131. continue;
  13132. #if PACKETVER < 20170419
  13133. if (msg->type != type)
  13134. continue;
  13135. #endif
  13136. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13137. continue;
  13138. remaining++;
  13139. }
  13140. #if PACKETVER >= 20170419
  13141. // Always send all
  13142. amount = remaining;
  13143. #else
  13144. if( remaining > MAIL_PAGE_SIZE ){
  13145. amount = MAIL_PAGE_SIZE;
  13146. }else{
  13147. amount = remaining;
  13148. }
  13149. #endif
  13150. WFIFOHEAD(fd, 7 + ((44 + MAIL_TITLE_LENGTH) * amount));
  13151. WFIFOW(fd, 0) = cmd;
  13152. #if PACKETVER >= 20170419
  13153. WFIFOB(fd, 4) = 1; // Unknown
  13154. offset = 5;
  13155. #else
  13156. WFIFOB(fd, 4) = type;
  13157. WFIFOB(fd, 5) = amount;
  13158. WFIFOB(fd, 6) = ( remaining < MAIL_PAGE_SIZE ); // last page
  13159. offset = 7;
  13160. #endif
  13161. for( amount = 0; i >= 0; i-- ){
  13162. msg = &md->msg[i];
  13163. if (msg->id < 1)
  13164. continue;
  13165. #if PACKETVER < 20170419
  13166. if (msg->type != type)
  13167. continue;
  13168. #endif
  13169. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13170. continue;
  13171. #if PACKETVER >= 20170419
  13172. WFIFOB(fd, offset) = msg->type;
  13173. offset += 1;
  13174. #endif
  13175. WFIFOQ(fd, offset + 0) = (uint64)msg->id;
  13176. WFIFOB(fd, offset + 8) = (msg->status != MAIL_UNREAD);
  13177. mailType = MAIL_TYPE_TEXT; // Normal letter
  13178. if( msg->zeny > 0 ){
  13179. mailType |= MAIL_TYPE_ZENY; // Mail contains zeny
  13180. }
  13181. for( k = 0; k < MAIL_MAX_ITEM; k++ ){
  13182. if( msg->item[k].nameid > 0 ){
  13183. mailType |= MAIL_TYPE_ITEM; // Mail contains items
  13184. break;
  13185. }
  13186. }
  13187. #if PACKETVER >= 20170419
  13188. // If it came from an npc?
  13189. if( !msg->send_id ){
  13190. mailType |= MAIL_TYPE_NPC;
  13191. }
  13192. #endif
  13193. WFIFOB(fd, offset + 9) = mailType;
  13194. safestrncpy(WFIFOCP(fd, offset + 10), msg->send_name, NAME_LENGTH);
  13195. #if PACKETVER < 20170419
  13196. // How much time has passed since you received the mail
  13197. WFIFOL(fd, offset + 34 ) = now - (uint32)msg->timestamp;
  13198. offset += 4;
  13199. #endif
  13200. // If automatic return/deletion of mails is enabled, notify the client when it will kick in
  13201. if( msg->scheduled_deletion > 0 ){
  13202. WFIFOL(fd, offset + 34) = (uint32)msg->scheduled_deletion - now;
  13203. }else{
  13204. // Fake the scheduled deletion to one year in the future
  13205. // Sadly the client always displays the scheduled deletion after 24 hours no matter how high this value gets [Lemongrass]
  13206. WFIFOL(fd, offset + 34) = 365 * 24 * 60 * 60;
  13207. }
  13208. WFIFOW(fd, offset + 38) = titleLength = (int16)(strlen(msg->title) + 1);
  13209. safestrncpy(WFIFOCP(fd, offset + 40), msg->title, titleLength);
  13210. offset += 40 + titleLength;
  13211. }
  13212. WFIFOW(fd, 2) = (int16)offset;
  13213. WFIFOSET(fd, offset);
  13214. #endif
  13215. }
  13216. /// Mail inbox list request.
  13217. /// 023f (CZ_MAIL_GET_LIST)
  13218. /// 09e8 <mail tab>.B <mail id>.Q (CZ_OPEN_MAILBOX)
  13219. /// 09ee <mail tab>.B <mail id>.Q (CZ_REQ_NEXT_MAIL_LIST)
  13220. /// 09ef <mail tab>.B <mail id>.Q (CZ_REQ_REFRESH_MAIL_LIST)
  13221. /// 0ac0 <mail id>.Q <unknown>.16B (CZ_OPEN_MAILBOX2)
  13222. /// 0ac1 <mail id>.Q <unknown>.16B (CZ_REQ_REFRESH_MAIL_LIST2)
  13223. void clif_parse_Mail_refreshinbox(int fd, map_session_data *sd){
  13224. if( mail_invalid_operation( sd ) ){
  13225. return;
  13226. }
  13227. #if PACKETVER < 20150513
  13228. struct mail_data* md = &sd->mail.inbox;
  13229. if( md->amount < MAIL_MAX_INBOX && (md->full || sd->mail.changed) )
  13230. intif_Mail_requestinbox(sd->status.char_id, 1, MAIL_INBOX_NORMAL);
  13231. else
  13232. clif_Mail_refreshinbox(sd, MAIL_INBOX_NORMAL,0);
  13233. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13234. mail_removezeny(sd, false);
  13235. #else
  13236. int cmd = RFIFOW(fd, 0);
  13237. #if PACKETVER < 20170419
  13238. uint8 openType = RFIFOB(fd, 2);
  13239. uint64 mailId = RFIFOQ(fd, 3);
  13240. #else
  13241. uint8 openType;
  13242. uint64 mailId = RFIFOQ(fd, 2);
  13243. int i;
  13244. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mailId);
  13245. if( i == MAIL_MAX_INBOX ){
  13246. openType = MAIL_INBOX_NORMAL;
  13247. mailId = 0;
  13248. }else{
  13249. openType = sd->mail.inbox.msg[i].type;
  13250. mailId = 0;
  13251. }
  13252. #endif
  13253. switch( openType ){
  13254. case MAIL_INBOX_NORMAL:
  13255. case MAIL_INBOX_ACCOUNT:
  13256. case MAIL_INBOX_RETURNED:
  13257. break;
  13258. default:
  13259. // Unknown type: ignore request
  13260. return;
  13261. }
  13262. if( sd->mail.changed || ( cmd == 0x9ef || cmd == 0xac1 ) ){
  13263. intif_Mail_requestinbox(sd->status.char_id, 1, (enum mail_inbox_type)openType);
  13264. return;
  13265. }
  13266. // If it is not a next page request
  13267. if( cmd != 0x9ee ){
  13268. mailId = 0;
  13269. }
  13270. clif_Mail_refreshinbox(sd,(enum mail_inbox_type)openType,mailId);
  13271. #endif
  13272. }
  13273. /// Opens a mail
  13274. /// 0242 <packet len>.W <mail id>.L <title>.40B <sender>.24B <time>.L <zeny>.L (ZC_MAIL_REQ_OPEN)
  13275. /// <amount>.L <name id>.W <item type>.W <identified>.B <damaged>.B <refine>.B
  13276. /// <card1>.W <card2>.W <card3>.W <card4>.W <message>.?B
  13277. /// 09eb <packet len>.W <type>.B <mail id>.Q <message length>.W <zeny>.Q <item count>.B (ZC_ACK_READ_MAIL)
  13278. /// { }*n
  13279. // TODO: Packet description => for repeated block
  13280. void clif_Mail_read( map_session_data *sd, int mail_id ){
  13281. int i, fd = sd->fd;
  13282. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13283. if( i == MAIL_MAX_INBOX ) {
  13284. clif_Mail_return(sd->fd, mail_id, 1); // Mail doesn't exist
  13285. ShowWarning("clif_parse_Mail_read: char '%s' trying to read a message not the inbox.\n", sd->status.name);
  13286. return;
  13287. } else {
  13288. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13289. struct item *item;
  13290. int16 msg_len;
  13291. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13292. if( msg_len == 0 ) {
  13293. strcpy(msg->body, "(no message)"); // TODO: confirm for RODEX
  13294. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13295. }
  13296. #if PACKETVER < 20150513
  13297. item = &msg->item[0];
  13298. int16 len = 101 + msg_len;
  13299. WFIFOHEAD(fd,len);
  13300. WFIFOW(fd,0) = 0x242;
  13301. WFIFOW(fd,2) = len;
  13302. WFIFOL(fd,4) = msg->id;
  13303. safestrncpy(WFIFOCP(fd,8), msg->title, MAIL_TITLE_LENGTH + 1);
  13304. safestrncpy(WFIFOCP(fd,48), msg->send_name, NAME_LENGTH + 1);
  13305. WFIFOL(fd,72) = 0;
  13306. WFIFOL(fd,76) = msg->zeny;
  13307. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13308. if( item->nameid && data != nullptr ) {
  13309. WFIFOL(fd,80) = item->amount;
  13310. WFIFOW(fd,84) = client_nameid( item->nameid );
  13311. WFIFOW(fd,86) = itemtype( item->nameid );
  13312. WFIFOB(fd,88) = item->identify;
  13313. WFIFOB(fd,89) = item->attribute;
  13314. WFIFOB(fd,90) = item->refine;
  13315. WFIFOW(fd,91) = item->card[0];
  13316. WFIFOW(fd,93) = item->card[1];
  13317. WFIFOW(fd,95) = item->card[2];
  13318. WFIFOW(fd,97) = item->card[3];
  13319. } else // no item, set all to zero
  13320. memset(WFIFOP(fd,80), 0x00, 19);
  13321. WFIFOB(fd,99) = (unsigned char)msg_len;
  13322. safestrncpy(WFIFOCP(fd,100), msg->body, msg_len + 1);
  13323. WFIFOSET(fd,len);
  13324. #else
  13325. msg_len += 1; // Zero Termination
  13326. PACKET_ZC_ACK_READ_RODEX* p = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX*>( packet_buffer );
  13327. p->PacketType = HEADER_ZC_ACK_READ_RODEX;
  13328. p->PacketLength = sizeof( *p );
  13329. p->opentype = msg->type;
  13330. p->MailID = msg->id;
  13331. p->TextcontentsLength = static_cast<decltype(p->TextcontentsLength)>( msg_len );
  13332. p->zeny = msg->zeny;
  13333. safestrncpy( WBUFCP( p, p->PacketLength ), msg->body, msg_len );
  13334. p->PacketLength += p->TextcontentsLength;
  13335. p->ItemCnt = 0;
  13336. for( int j = 0; j < MAIL_MAX_ITEM; j++ ){
  13337. item = &msg->item[j];
  13338. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13339. if( item->nameid > 0 && item->amount > 0 && data != nullptr ){
  13340. PACKET_ZC_ACK_READ_RODEX_SUB* mailitem = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX_SUB*>( WBUFP( p, p->PacketLength ) );
  13341. mailitem->ITID = client_nameid( item->nameid );
  13342. mailitem->count = item->amount;
  13343. mailitem->type = itemtype( item->nameid );
  13344. mailitem->IsIdentified = item->identify ? 1 : 0;
  13345. mailitem->IsDamaged = item->attribute ? 1 : 0;
  13346. mailitem->refiningLevel = item->refine;
  13347. mailitem->location = pc_equippoint_sub( sd, data.get() );
  13348. mailitem->viewSprite = data->look;
  13349. mailitem->bindOnEquip = item->bound ? 2 : data->flag.bindOnEquip ? 1 : 0;
  13350. clif_addcards( &mailitem->slot, item );
  13351. clif_add_random_options( mailitem->option_data, *item );
  13352. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  13353. mailitem->grade = item->enchantgrade;
  13354. #endif
  13355. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( *mailitem ) );
  13356. p->ItemCnt++;
  13357. }
  13358. }
  13359. clif_send( p, p->PacketLength, &sd->bl, SELF );
  13360. #endif
  13361. if (msg->status == MAIL_UNREAD) {
  13362. msg->status = MAIL_READ;
  13363. intif_Mail_read(mail_id);
  13364. clif_parse_Mail_refreshinbox(fd, sd);
  13365. sd->mail.inbox.unread--;
  13366. clif_Mail_new(sd, 0, msg->send_name, msg->title);
  13367. }
  13368. }
  13369. }
  13370. /// Request to open a mail.
  13371. /// 0241 <mail id>.L (CZ_MAIL_OPEN)
  13372. /// 09ea <mail tab>.B <mail id>.Q (CZ_REQ_READ_MAIL)
  13373. void clif_parse_Mail_read(int fd, map_session_data *sd){
  13374. #if PACKETVER < 20150513
  13375. int mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13376. #else
  13377. //uint8 openType = RFIFOB(fd, 2);
  13378. int mail_id = (int)RFIFOQ(fd, 3);
  13379. #endif
  13380. if( mail_id <= 0 )
  13381. return;
  13382. if( mail_invalid_operation(sd) )
  13383. return;
  13384. clif_Mail_read(sd, mail_id);
  13385. }
  13386. /// Allow a player to begin writing a mail
  13387. /// 0a12 <receiver>.24B <success>.B (ZC_ACK_OPEN_WRITE_MAIL)
  13388. void clif_send_Mail_beginwrite_ack( map_session_data *sd, char* name, bool success ){
  13389. PACKET_ZC_ACK_OPEN_WRITE_MAIL p = { 0 };
  13390. p.PacketType = rodexopenwrite;
  13391. safestrncpy(p.receiveName, name, NAME_LENGTH);
  13392. p.result = success;
  13393. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13394. }
  13395. /// Request to start writing a mail
  13396. /// 0a08 <receiver>.24B (CZ_REQ_OPEN_WRITE_MAIL)
  13397. void clif_parse_Mail_beginwrite( int fd, map_session_data *sd ){
  13398. char name[NAME_LENGTH];
  13399. safestrncpy(name, RFIFOCP(fd, 2), NAME_LENGTH);
  13400. if( mail_invalid_operation( sd ) ){
  13401. return;
  13402. }
  13403. if( sd->state.storage_flag || sd->state.mail_writing || sd->state.trading ){
  13404. clif_send_Mail_beginwrite_ack(sd, name, false);
  13405. return;
  13406. }
  13407. mail_clear(sd);
  13408. sd->state.mail_writing = true;
  13409. clif_send_Mail_beginwrite_ack(sd, name,true);
  13410. }
  13411. /// Notification that the client cancelled writing a mail
  13412. /// 0a03 (CZ_REQ_CANCEL_WRITE_MAIL)
  13413. void clif_parse_Mail_cancelwrite( int fd, map_session_data *sd ){
  13414. sd->state.mail_writing = false;
  13415. }
  13416. /// Give the client information about the recipient, if available
  13417. /// 0a14 <char id>.L <class>.W <base level>.W (ZC_CHECK_RECEIVE_CHARACTER_NAME)
  13418. /// 0a51 <char id>.L <class>.W <base level>.W <name>.24B (ZC_CHECK_RECEIVE_CHARACTER_NAME2)
  13419. void clif_Mail_Receiver_Ack( map_session_data* sd, uint32 char_id, short class_, uint32 level, const char* name ){
  13420. #if PACKETVER >= 20141119
  13421. PACKET_ZC_CHECKNAME p = { 0 };
  13422. p.PacketType = HEADER_ZC_CHECKNAME;
  13423. p.CharId = char_id;
  13424. p.Class = class_;
  13425. p.BaseLevel = level;
  13426. #if PACKETVER >= 20160302
  13427. strncpy(p.Name, name, NAME_LENGTH);
  13428. #endif
  13429. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13430. #endif
  13431. }
  13432. /// Request information about the recipient
  13433. /// 0a13 <name>.24B (CZ_CHECK_RECEIVE_CHARACTER_NAME)
  13434. void clif_parse_Mail_Receiver_Check(int fd, map_session_data *sd) {
  13435. #if PACKETVER >= 20140423
  13436. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13437. const PACKET_CZ_CHECKNAME2* p = reinterpret_cast<PACKET_CZ_CHECKNAME2*>( RFIFOP( fd, 0 ) );
  13438. #else
  13439. const PACKET_CZ_CHECKNAME1* p = reinterpret_cast<PACKET_CZ_CHECKNAME1*>( RFIFOP( fd, 0 ) );
  13440. #endif
  13441. static char name[NAME_LENGTH];
  13442. if( mail_invalid_operation( sd ) ){
  13443. return;
  13444. }
  13445. safestrncpy(name, p->Name, NAME_LENGTH);
  13446. intif_mail_checkreceiver(sd, name);
  13447. #endif
  13448. }
  13449. /// Request to receive mail's attachment.
  13450. /// 0244 <mail id>.L (CZ_MAIL_GET_ITEM)
  13451. /// 09f1 <mail id>.Q <mail tab>.B (CZ_REQ_ZENY_FROM_MAIL)
  13452. /// 09f3 <mail id>.Q <mail tab>.B (CZ_REQ_ITEM_FROM_MAIL)
  13453. void clif_parse_Mail_getattach( int fd, map_session_data *sd ){
  13454. int i;
  13455. struct mail_message* msg;
  13456. #if PACKETVER < 20150513
  13457. int mail_id = RFIFOL(fd, packet_db[RFIFOW(fd, 0)].pos[0]);
  13458. int attachment = MAIL_ATT_ALL;
  13459. #else
  13460. uint16 packet_id = RFIFOW(fd, 0);
  13461. int mail_id = (int)RFIFOQ(fd, 2);
  13462. //int openType = RFIFOB(fd, 10);
  13463. int attachment = packet_id == 0x9f1 ? MAIL_ATT_ZENY : packet_id == 0x9f3 ? MAIL_ATT_ITEM : MAIL_ATT_NONE;
  13464. #endif
  13465. if( !chrif_isconnected() )
  13466. return;
  13467. if( mail_id <= 0 )
  13468. return;
  13469. if( mail_invalid_operation(sd) )
  13470. return;
  13471. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13472. if( i == MAIL_MAX_INBOX )
  13473. return;
  13474. msg = &sd->mail.inbox.msg[i];
  13475. if( attachment&MAIL_ATT_ZENY && msg->zeny < 1 ){
  13476. attachment &= ~MAIL_ATT_ZENY;
  13477. }
  13478. if( attachment&MAIL_ATT_ITEM ){
  13479. ARR_FIND(0, MAIL_MAX_ITEM, i, msg->item[i].nameid > 0 || msg->item[i].amount > 0);
  13480. // No items were found
  13481. if( i == MAIL_MAX_ITEM ){
  13482. attachment &= ~MAIL_ATT_ITEM;
  13483. }
  13484. }
  13485. // Either no attachment requested at all or there are no zeny or items in the mail
  13486. if( attachment == MAIL_ATT_NONE ){
  13487. return;
  13488. }
  13489. if( attachment&MAIL_ATT_ZENY ){
  13490. if( ( msg->zeny + sd->status.zeny + sd->mail.pending_zeny ) > MAX_ZENY ){
  13491. clif_mail_getattachment(sd, msg, 1, MAIL_ATT_ZENY); //too many zeny
  13492. return;
  13493. }else{
  13494. // Store the pending zeny (required for the "retrieve all" feature)
  13495. sd->mail.pending_zeny += msg->zeny;
  13496. // To make sure another request fails
  13497. msg->zeny = 0;
  13498. }
  13499. }
  13500. if( attachment&MAIL_ATT_ITEM ){
  13501. int slots = 0, totalweight = 0;
  13502. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  13503. struct item* item = &msg->item[i];
  13504. if( item->nameid > 0 && item->amount > 0 ){
  13505. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13506. if(data == nullptr)
  13507. continue;
  13508. switch( pc_checkadditem(sd, item->nameid, item->amount) ){
  13509. case CHKADDITEM_NEW:
  13510. slots += data->inventorySlotNeeded( item->amount );
  13511. break;
  13512. case CHKADDITEM_OVERAMOUNT:
  13513. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13514. return;
  13515. }
  13516. totalweight += data->weight * item->amount;
  13517. }
  13518. }
  13519. if( ( totalweight + sd->weight + sd->mail.pending_weight ) > sd->max_weight ){
  13520. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13521. return;
  13522. }else if( pc_inventoryblank(sd) < ( slots + sd->mail.pending_slots ) ){
  13523. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13524. return;
  13525. }
  13526. // To make sure another request fails
  13527. memset(msg->item, 0, MAIL_MAX_ITEM*sizeof(struct item));
  13528. // Store the pending weight (required for the "retrieve all" feature)
  13529. sd->mail.pending_weight += totalweight;
  13530. // Store the pending slots (required for the "retrieve all" feature)
  13531. sd->mail.pending_slots += slots;
  13532. }
  13533. intif_mail_getattach(sd,msg, (enum mail_attachment_type)attachment);
  13534. }
  13535. /// Request to delete a mail.
  13536. /// 0243 <mail id>.L (CZ_MAIL_DELETE)
  13537. /// 09f5 <mail tab>.B <mail id>.Q (CZ_REQ_DELETE_MAIL)
  13538. void clif_parse_Mail_delete(int fd, map_session_data *sd){
  13539. #if PACKETVER < 20150513
  13540. int mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13541. #else
  13542. //int openType = RFIFOB(fd, 2);
  13543. int mail_id = (int)RFIFOQ(fd, 3);
  13544. #endif
  13545. int i, j;
  13546. if( !chrif_isconnected() )
  13547. return;
  13548. if( mail_id <= 0 )
  13549. return;
  13550. if( mail_invalid_operation(sd) )
  13551. return;
  13552. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13553. if (i < MAIL_MAX_INBOX) {
  13554. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13555. // can't delete mail without removing zeny first
  13556. if( msg->zeny > 0 ){
  13557. clif_mail_delete(sd, msg, false);
  13558. return;
  13559. }
  13560. // can't delete mail without removing attachment(s) first
  13561. for( j = 0; j < MAIL_MAX_ITEM; j++ ){
  13562. if( msg->item[j].nameid > 0 && msg->item[j].amount > 0 ){
  13563. clif_mail_delete(sd, msg, false);
  13564. return;
  13565. }
  13566. }
  13567. if( intif_Mail_delete(sd->status.char_id, mail_id) && msg->status == MAIL_UNREAD ){
  13568. sd->mail.inbox.unread--;
  13569. clif_Mail_new(sd,0,nullptr,nullptr);
  13570. }
  13571. }
  13572. }
  13573. /// Request to return a mail.
  13574. /// 0273 <mail id>.L <receive name>.24B (CZ_REQ_MAIL_RETURN)
  13575. void clif_parse_Mail_return(int fd, map_session_data *sd){
  13576. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13577. const PACKET_CZ_RODEX_RETURN* p = reinterpret_cast<PACKET_CZ_RODEX_RETURN*>( RFIFOP( fd, 0 ) );
  13578. //ShowDump( p, sizeof( p ) );
  13579. int mail_id = p->msgId;
  13580. // not supported for now
  13581. return;
  13582. #else
  13583. int mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13584. #endif
  13585. //char *rec_name = RFIFOP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  13586. int i;
  13587. if( mail_id <= 0 )
  13588. return;
  13589. if( mail_invalid_operation(sd) )
  13590. return;
  13591. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13592. if( i < MAIL_MAX_INBOX && sd->mail.inbox.msg[i].send_id != 0 )
  13593. intif_Mail_return(sd->status.char_id, mail_id);
  13594. else
  13595. clif_Mail_return(sd->fd, mail_id, 1);
  13596. }
  13597. /// Request to add an item or Zeny to mail.
  13598. /// 0247 <index>.W <amount>.L (CZ_MAIL_ADD_ITEM)
  13599. /// 0a04 <index>.W <amount>.W (CZ_REQ_ADD_ITEM_TO_MAIL)
  13600. void clif_parse_Mail_setattach(int fd, map_session_data *sd){
  13601. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13602. uint16 idx = RFIFOW(fd,info->pos[0]);
  13603. #if PACKETVER < 20150513
  13604. int amount = RFIFOL(fd,info->pos[1]);
  13605. #else
  13606. int amount = RFIFOW(fd,info->pos[1]);
  13607. #endif
  13608. unsigned char flag;
  13609. if( !chrif_isconnected() )
  13610. return;
  13611. if (amount < 0 || server_index(idx) >= MAX_INVENTORY)
  13612. return;
  13613. if (sd->inventory_data[server_index(idx)] == nullptr)
  13614. return;
  13615. if( mail_invalid_operation( sd ) ){
  13616. return;
  13617. }
  13618. flag = mail_setitem(sd, idx, amount);
  13619. if( flag == MAIL_ATTACH_EQUIPSWITCH ){
  13620. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  13621. }else{
  13622. clif_Mail_setattachment(sd,idx,amount,flag);
  13623. }
  13624. }
  13625. /// Remove an item from a mail
  13626. /// 0a07 <result>.B <index>.W <amount>.W <weight>.W
  13627. void clif_mail_removeitem( map_session_data* sd, bool success, int index, int amount ){
  13628. PACKET_ZC_ACK_REMOVE_ITEM_MAIL p = { 0 };
  13629. p.PacketType = rodexremoveitem;
  13630. p.result = success;
  13631. p.index = index;
  13632. p.cnt = amount;
  13633. int total = 0;
  13634. for( int i = 0; i < MAIL_MAX_ITEM; i++ ){
  13635. if( sd->mail.item[i].nameid == 0 ){
  13636. break;
  13637. }
  13638. total += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  13639. }
  13640. p.weight = total;
  13641. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13642. }
  13643. /// Request to reset mail item and/or Zeny
  13644. /// 0246 <type>.W (CZ_MAIL_RESET_ITEM)
  13645. /// type:
  13646. /// 0 = reset all
  13647. /// 1 = remove item
  13648. /// 2 = remove zeny
  13649. /// 0a06 <index>.W <amount>.W (CZ_REQ_REMOVE_ITEM_MAIL)
  13650. void clif_parse_Mail_winopen(int fd, map_session_data *sd)
  13651. {
  13652. #if PACKETVER < 20150513
  13653. int type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13654. if (type == 0 || type == 1)
  13655. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13656. if (type == 0 || type == 2)
  13657. mail_removezeny(sd, false);
  13658. #else
  13659. uint16 index = RFIFOW(fd, 2);
  13660. uint16 count = RFIFOW(fd, 4);
  13661. mail_removeitem(sd,0,index,count);
  13662. #endif
  13663. }
  13664. /// Request to send mail
  13665. /// 0248 <packet len>.W <recipient>.24B <title>.40B <body len>.B <body>.?B (CZ_MAIL_SEND)
  13666. /// 09ec <packet len>.W <recipient>.24B <sender>.24B <zeny>.Q <title length>.W <body length>.W <title>.?B <body>.?B (CZ_REQ_WRITE_MAIL)
  13667. /// 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)
  13668. void clif_parse_Mail_send(int fd, map_session_data *sd){
  13669. #if PACKETVER < 20150513
  13670. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13671. if( !chrif_isconnected() )
  13672. return;
  13673. if( RFIFOW(fd,info->pos[0]) < 69 ) {
  13674. ShowWarning("Invalid Msg Len from account %d.\n", sd->status.account_id);
  13675. return;
  13676. }
  13677. mail_send(sd, RFIFOCP(fd,info->pos[1]), RFIFOCP(fd,info->pos[2]), RFIFOCP(fd,info->pos[4]), RFIFOB(fd,info->pos[3]));
  13678. #else
  13679. uint16 length = RFIFOW(fd, 2);
  13680. if( length < 0x3e ){
  13681. ShowWarning("Too short...\n");
  13682. clif_Mail_send(sd, WRITE_MAIL_FAILED);
  13683. return;
  13684. }
  13685. if( mail_invalid_operation( sd ) ){
  13686. return;
  13687. }
  13688. // Forged request without a begin writing packet?
  13689. if( !sd->state.mail_writing ){
  13690. return; // Ignore it
  13691. }
  13692. char receiver[NAME_LENGTH];
  13693. safestrncpy(receiver, RFIFOCP(fd, 4), NAME_LENGTH);
  13694. // char sender[NAME_LENGTH];
  13695. // safestrncpy(sender, RFIFOCP(fd, 28), NAME_LENGTH);
  13696. uint64 zeny = RFIFOQ(fd, 52);
  13697. uint16 titleLength = RFIFOW(fd, 60);
  13698. uint16 textLength = RFIFOW(fd, 62);
  13699. uint16 realTitleLength = min(titleLength, MAIL_TITLE_LENGTH);
  13700. uint16 realTextLength = min(textLength, MAIL_BODY_LENGTH);
  13701. char title[MAIL_TITLE_LENGTH];
  13702. char text[MAIL_BODY_LENGTH];
  13703. #if PACKETVER <= 20160330
  13704. safestrncpy(title, RFIFOCP(fd, 64), realTitleLength);
  13705. safestrncpy(text, RFIFOCP(fd, 64 + titleLength), realTextLength);
  13706. #else
  13707. // 64 = <char id>.L
  13708. safestrncpy(title, RFIFOCP(fd, 68), realTitleLength);
  13709. safestrncpy(text, RFIFOCP(fd, 68 + titleLength), realTextLength);
  13710. #endif
  13711. if( zeny > 0 ){
  13712. if( mail_setitem(sd,0,(uint32)zeny) != MAIL_ATTACH_SUCCESS ){
  13713. clif_Mail_send(sd,WRITE_MAIL_FAILED);
  13714. return;
  13715. }
  13716. }
  13717. mail_send(sd, receiver, title, text, realTextLength);
  13718. #endif
  13719. }
  13720. /// AUCTION SYSTEM
  13721. /// By Zephyrus
  13722. ///
  13723. /// Opens/closes the auction window (ZC_AUCTION_WINDOWS).
  13724. /// 025f <type>.L
  13725. /// type:
  13726. /// 0 = open
  13727. /// 1 = close
  13728. void clif_Auction_openwindow(map_session_data *sd)
  13729. {
  13730. int fd = sd->fd;
  13731. if( sd->state.storage_flag || sd->state.vending || sd->state.buyingstore || sd->state.trading )
  13732. return;
  13733. if( !battle_config.feature_auction )
  13734. return;
  13735. WFIFOHEAD(fd,packet_len(0x25f));
  13736. WFIFOW(fd,0) = 0x25f;
  13737. WFIFOL(fd,2) = 0;
  13738. WFIFOSET(fd,packet_len(0x25f));
  13739. }
  13740. /// Returns auction item search results (ZC_AUCTION_ITEM_REQ_SEARCH).
  13741. /// 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 }*
  13742. void clif_Auction_results(map_session_data *sd, short count, short pages, uint8 *buf)
  13743. {
  13744. int i, fd = sd->fd, len = sizeof(struct auction_data);
  13745. struct auction_data auction;
  13746. WFIFOHEAD(fd,12 + (count * 83));
  13747. WFIFOW(fd,0) = 0x252;
  13748. WFIFOW(fd,2) = 12 + (count * 83);
  13749. WFIFOL(fd,4) = pages;
  13750. WFIFOL(fd,8) = count;
  13751. for( i = 0; i < count; i++ ) {
  13752. int k = 12 + (i * 83);
  13753. memcpy(&auction, RBUFP(buf,i * len), len);
  13754. WFIFOL(fd,k) = auction.auction_id;
  13755. safestrncpy(WFIFOCP(fd,4+k), auction.seller_name, NAME_LENGTH);
  13756. WFIFOW(fd,28+k) = client_nameid( auction.item.nameid );
  13757. WFIFOL(fd,30+k) = auction.type;
  13758. WFIFOW(fd,34+k) = auction.item.amount; // Always 1
  13759. WFIFOB(fd,36+k) = auction.item.identify;
  13760. WFIFOB(fd,37+k) = auction.item.attribute;
  13761. WFIFOB(fd,38+k) = auction.item.refine;
  13762. WFIFOW(fd,39+k) = auction.item.card[0];
  13763. WFIFOW(fd,41+k) = auction.item.card[1];
  13764. WFIFOW(fd,43+k) = auction.item.card[2];
  13765. WFIFOW(fd,45+k) = auction.item.card[3];
  13766. WFIFOL(fd,47+k) = auction.price;
  13767. WFIFOL(fd,51+k) = auction.buynow;
  13768. safestrncpy(WFIFOCP(fd,55+k), auction.buyer_name, NAME_LENGTH);
  13769. WFIFOL(fd,79+k) = (uint32)auction.timestamp;
  13770. }
  13771. WFIFOSET(fd,WFIFOW(fd,2));
  13772. }
  13773. /// Result from request to add an item (ZC_ACK_AUCTION_ADD_ITEM).
  13774. /// 0256 <index>.W <result>.B
  13775. /// result:
  13776. /// 0 = success
  13777. /// 1 = failure
  13778. static void clif_Auction_setitem(int fd, int index, bool fail)
  13779. {
  13780. WFIFOHEAD(fd,packet_len(0x256));
  13781. WFIFOW(fd,0) = 0x256;
  13782. WFIFOW(fd,2) = index;
  13783. WFIFOB(fd,4) = fail;
  13784. WFIFOSET(fd,packet_len(0x256));
  13785. }
  13786. /// Request to initialize 'new auction' data (CZ_AUCTION_CREATE).
  13787. /// 024b <type>.W
  13788. /// type:
  13789. /// 0 = create (any other action in auction window)
  13790. /// 1 = cancel (cancel pressed on register tab)
  13791. /// ? = junk, uninitialized value (ex. when switching between list filters)
  13792. void clif_parse_Auction_cancelreg(int fd, map_session_data *sd){
  13793. //int type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13794. if( sd->auction.amount > 0 )
  13795. clif_additem(sd, sd->auction.index, sd->auction.amount, 0);
  13796. sd->auction.amount = 0;
  13797. }
  13798. /// Request to add an item to the action (CZ_AUCTION_ADD_ITEM).
  13799. /// 024c <index>.W <count>.L
  13800. void clif_parse_Auction_setitem(int fd, map_session_data *sd){
  13801. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13802. int idx = RFIFOW(fd,info->pos[0]) - 2;
  13803. int amount = RFIFOL(fd,info->pos[1]); // Always 1
  13804. if( !battle_config.feature_auction )
  13805. return;
  13806. if( sd->auction.amount > 0 )
  13807. sd->auction.amount = 0;
  13808. if( idx < 0 || idx >= MAX_INVENTORY ) {
  13809. ShowWarning("Character %s trying to set invalid item index in auctions.\n", sd->status.name);
  13810. return;
  13811. }
  13812. 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.
  13813. ShowWarning("Character %s trying to set invalid amount in auctions.\n", sd->status.name);
  13814. return;
  13815. }
  13816. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[idx].nameid);
  13817. 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) )
  13818. { // Consumable or pets are not allowed
  13819. clif_Auction_setitem(sd->fd, idx, true);
  13820. return;
  13821. }
  13822. if( !pc_can_give_items(sd) || sd->inventory.u.items_inventory[idx].expire_time ||
  13823. !sd->inventory.u.items_inventory[idx].identify ||
  13824. (sd->inventory.u.items_inventory[idx].bound && !pc_can_give_bounded_items(sd)) ||
  13825. !itemdb_available(sd->inventory.u.items_inventory[idx].nameid) ||
  13826. !itemdb_canauction(&sd->inventory.u.items_inventory[idx],pc_get_group_level(sd)) ) { // Quest Item or something else
  13827. clif_Auction_setitem(sd->fd, idx, true);
  13828. return;
  13829. }
  13830. sd->auction.index = idx;
  13831. sd->auction.amount = amount;
  13832. clif_Auction_setitem(fd, idx + 2, false);
  13833. }
  13834. /// Result from an auction action (ZC_AUCTION_RESULT).
  13835. /// 0250 <result>.B
  13836. /// result:
  13837. /// 0 = You have failed to bid into the auction
  13838. /// 1 = You have successfully bid in the auction
  13839. /// 2 = The auction has been canceled
  13840. /// 3 = An auction with at least one bidder cannot be canceled
  13841. /// 4 = You cannot register more than 5 items in an auction at a time
  13842. /// 5 = You do not have enough Zeny to pay the Auction Fee
  13843. /// 6 = You have won the auction
  13844. /// 7 = You have failed to win the auction
  13845. /// 8 = You do not have enough Zeny
  13846. /// 9 = You cannot place more than 5 bids at a time
  13847. void clif_Auction_message(int fd, unsigned char flag)
  13848. {
  13849. WFIFOHEAD(fd,packet_len(0x250));
  13850. WFIFOW(fd,0) = 0x250;
  13851. WFIFOB(fd,2) = flag;
  13852. WFIFOSET(fd,packet_len(0x250));
  13853. }
  13854. /// Result of the auction close request (ZC_AUCTION_ACK_MY_SELL_STOP).
  13855. /// 025e <result>.W
  13856. /// result:
  13857. /// 0 = You have ended the auction
  13858. /// 1 = You cannot end the auction
  13859. /// 2 = Auction ID is incorrect
  13860. void clif_Auction_close(int fd, unsigned char flag)
  13861. {
  13862. WFIFOHEAD(fd,packet_len(0x25e));
  13863. WFIFOW(fd,0) = 0x25d; // BUG: The client identifies this packet as 0x25d (CZ_AUCTION_REQ_MY_SELL_STOP)
  13864. WFIFOW(fd,2) = flag;
  13865. WFIFOSET(fd,packet_len(0x25e));
  13866. }
  13867. /// Request to add an auction (CZ_AUCTION_ADD).
  13868. /// 024d <now money>.L <max money>.L <delete hour>.W
  13869. void clif_parse_Auction_register(int fd, map_session_data *sd)
  13870. {
  13871. struct auction_data auction;
  13872. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13873. if( !battle_config.feature_auction )
  13874. return;
  13875. auction.price = RFIFOL(fd,info->pos[0]);
  13876. auction.buynow = RFIFOL(fd,info->pos[1]);
  13877. auction.hours = RFIFOW(fd,info->pos[2]);
  13878. // Invalid Situations...
  13879. if( sd->auction.amount < 1 ) {
  13880. ShowWarning("Character %s trying to register auction without item.\n", sd->status.name);
  13881. return;
  13882. }
  13883. if( auction.price >= auction.buynow ) {
  13884. ShowWarning("Character %s trying to alter auction prices.\n", sd->status.name);
  13885. return;
  13886. }
  13887. if( auction.hours < 1 || auction.hours > 48 ) {
  13888. ShowWarning("Character %s trying to enter an invalid time for auction.\n", sd->status.name);
  13889. return;
  13890. }
  13891. // Auction checks...
  13892. if( sd->inventory.u.items_inventory[sd->auction.index].bound && !pc_can_give_bounded_items(sd) ) {
  13893. clif_displaymessage(sd->fd, msg_txt(sd,293));
  13894. clif_Auction_message(fd, 2); // The auction has been canceled
  13895. return;
  13896. }
  13897. if( sd->status.zeny < (auction.hours * battle_config.auction_feeperhour) ) {
  13898. clif_Auction_message(fd, 5); // You do not have enough zeny to pay the Auction Fee.
  13899. return;
  13900. }
  13901. if( auction.buynow > battle_config.auction_maximumprice ) { // Zeny Limits
  13902. auction.buynow = battle_config.auction_maximumprice;
  13903. if( auction.price >= auction.buynow )
  13904. auction.price = auction.buynow - 1;
  13905. }
  13906. auction.auction_id = 0;
  13907. auction.seller_id = sd->status.char_id;
  13908. safestrncpy(auction.seller_name, sd->status.name, sizeof(auction.seller_name));
  13909. auction.buyer_id = 0;
  13910. memset(auction.buyer_name, '\0', sizeof(auction.buyer_name));
  13911. if( sd->inventory.u.items_inventory[sd->auction.index].nameid == 0 || sd->inventory.u.items_inventory[sd->auction.index].amount < sd->auction.amount )
  13912. {
  13913. clif_Auction_message(fd, 2); // The auction has been canceled
  13914. return;
  13915. }
  13916. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[sd->auction.index].nameid);
  13917. if( id == nullptr )
  13918. { // Just in case
  13919. clif_Auction_message(fd, 2); // The auction has been canceled
  13920. return;
  13921. }
  13922. safestrncpy(auction.item_name, id->ename.c_str(), sizeof(auction.item_name));
  13923. auction.type = id->type;
  13924. memcpy(&auction.item, &sd->inventory.u.items_inventory[sd->auction.index], sizeof(struct item));
  13925. auction.item.amount = 1;
  13926. auction.timestamp = 0;
  13927. if( !intif_Auction_register(&auction) )
  13928. clif_Auction_message(fd, 4); // No Char Server? lets say something to the client
  13929. else
  13930. {
  13931. int zeny = auction.hours*battle_config.auction_feeperhour;
  13932. pc_delitem(sd, sd->auction.index, sd->auction.amount, 1, 6, LOG_TYPE_AUCTION);
  13933. sd->auction.amount = 0;
  13934. pc_payzeny(sd, zeny, LOG_TYPE_AUCTION);
  13935. }
  13936. }
  13937. /// Cancels an auction (CZ_AUCTION_ADD_CANCEL).
  13938. /// 024e <auction id>.L
  13939. void clif_parse_Auction_cancel(int fd, map_session_data *sd){
  13940. unsigned int auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13941. intif_Auction_cancel(sd->status.char_id, auction_id);
  13942. }
  13943. /// Closes an auction (CZ_AUCTION_REQ_MY_SELL_STOP).
  13944. /// 025d <auction id>.L
  13945. void clif_parse_Auction_close(int fd, map_session_data *sd){
  13946. unsigned int auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13947. intif_Auction_close(sd->status.char_id, auction_id);
  13948. }
  13949. /// Places a bid on an auction (CZ_AUCTION_BUY).
  13950. /// 024f <auction id>.L <money>.L
  13951. void clif_parse_Auction_bid(int fd, map_session_data *sd){
  13952. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13953. unsigned int auction_id = RFIFOL(fd,info->pos[0]);
  13954. int bid = RFIFOL(fd,info->pos[1]);
  13955. if( !pc_can_give_items(sd) ) { //They aren't supposed to give zeny [Inkfish]
  13956. clif_displaymessage(sd->fd, msg_txt(sd,246));
  13957. return;
  13958. }
  13959. if( bid <= 0 )
  13960. clif_Auction_message(fd, 0); // You have failed to bid into the auction
  13961. else if( bid > sd->status.zeny )
  13962. clif_Auction_message(fd, 8); // You do not have enough zeny
  13963. else if ( CheckForCharServer() ) // char server is down (bugreport:1138)
  13964. clif_Auction_message(fd, 0); // You have failed to bid into the auction
  13965. else {
  13966. pc_payzeny(sd, bid, LOG_TYPE_AUCTION);
  13967. intif_Auction_bid(sd->status.char_id, sd->status.name, auction_id, bid);
  13968. }
  13969. }
  13970. /// Auction Search (CZ_AUCTION_ITEM_SEARCH).
  13971. /// 0251 <search type>.W <auction id>.L <search text>.24B <page number>.W
  13972. /// search type:
  13973. /// 0 = armor
  13974. /// 1 = weapon
  13975. /// 2 = card
  13976. /// 3 = misc
  13977. /// 4 = name search
  13978. /// 5 = auction id search
  13979. void clif_parse_Auction_search(int fd, map_session_data* sd){
  13980. char search_text[NAME_LENGTH];
  13981. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13982. short type = RFIFOW(fd,info->pos[0]);
  13983. int price = RFIFOL(fd,info->pos[1]); // FIXME: bug #5071
  13984. int page = RFIFOW(fd,info->pos[3]);
  13985. if( !battle_config.feature_auction )
  13986. return;
  13987. clif_parse_Auction_cancelreg(fd, sd);
  13988. safestrncpy(search_text, RFIFOCP(fd,info->pos[2]), sizeof(search_text));
  13989. intif_Auction_requestlist(sd->status.char_id, type, price, search_text, page);
  13990. }
  13991. /// Requests list of own currently active bids or auctions (CZ_AUCTION_REQ_MY_INFO).
  13992. /// 025c <type>.W
  13993. /// type:
  13994. /// 0 = sell (own auctions)
  13995. /// 1 = buy (own bids)
  13996. void clif_parse_Auction_buysell(int fd, map_session_data* sd)
  13997. {
  13998. short type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) + 6;
  13999. if( !battle_config.feature_auction )
  14000. return;
  14001. clif_parse_Auction_cancelreg(fd, sd);
  14002. intif_Auction_requestlist(sd->status.char_id, type, 0, "", 1);
  14003. }
  14004. /// CASH/POINT SHOP
  14005. ///
  14006. void clif_cashshop_open( map_session_data* sd, int tab ){
  14007. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14008. nullpo_retv( sd );
  14009. PACKET_ZC_SE_CASHSHOP_OPEN p = {};
  14010. p.packetType = HEADER_ZC_SE_CASHSHOP_OPEN;
  14011. p.cashPoints = sd->cashPoints;
  14012. p.kafraPoints = sd->kafraPoints;
  14013. #if PACKETVER_MAIN_NUM >= 20200129 || PACKETVER_RE_NUM >= 20200205 || PACKETVER_ZERO_NUM >= 20191224
  14014. p.tab = tab;
  14015. #endif
  14016. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14017. #endif
  14018. }
  14019. void clif_parse_cashshop_open_request( int fd, map_session_data* sd ){
  14020. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14021. nullpo_retv( sd );
  14022. int tab = 0;
  14023. #if PACKETVER >= 20191224
  14024. const PACKET_CZ_SE_CASHSHOP_OPEN2* p = reinterpret_cast<PACKET_CZ_SE_CASHSHOP_OPEN2*>( RFIFOP( fd, 0 ) );
  14025. tab = p->tab;
  14026. #endif
  14027. if (map_getmapflag(sd->bl.m, MF_NOCASHSHOP)) {
  14028. clif_displaymessage(fd, msg_txt(sd, 451)); // Cash Shop is disabled on this map.
  14029. return;
  14030. }
  14031. sd->state.cashshop_open = true;
  14032. sd->npc_shopid = -1; // Set npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14033. clif_cashshop_open( sd, tab );
  14034. #endif
  14035. }
  14036. void clif_parse_cashshop_close( int fd, map_session_data* sd ){
  14037. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14038. sd->state.cashshop_open = false;
  14039. sd->npc_shopid = 0; // Reset npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14040. // No need to do anything here
  14041. #endif
  14042. }
  14043. //0846 <tabid>.W (CZ_REQ_SE_CASH_TAB_CODE))
  14044. //08c0 <len>.W <openIdentity>.L <itemcount>.W (ZC_ACK_SE_CASH_ITEM_LIST2)
  14045. void clif_parse_CashShopReqTab(int fd, map_session_data *sd) {
  14046. #if PACKETVER_MAIN_NUM >= 20100824 || PACKETVER_RE_NUM >= 20100824 || defined(PACKETVER_ZERO)
  14047. const PACKET_CZ_REQ_SE_CASH_TAB_CODE* p_in = reinterpret_cast<PACKET_CZ_REQ_SE_CASH_TAB_CODE*>( RFIFOP( fd, 0 ) );
  14048. // TODO: most likely wrong answer packet. Most likely HEADER_ZC_ACK_SE_CASH_ITEM_LIST (0x847) would be correct [Lemongrass]
  14049. // [4144] packet exists only in 2011 and was dropped after
  14050. #if PACKETVER >= 20110222 && PACKETVER < 20120000
  14051. std::shared_ptr<s_cash_item_tab> tab = cash_shop_db.find( static_cast<e_cash_shop_tab>( p_in->tab ) );
  14052. if( tab == nullptr ){
  14053. return;
  14054. }
  14055. // Skip empty tabs, the client only expects filled ones
  14056. if( tab->items.empty() ){
  14057. return;
  14058. }
  14059. #if !(PACKETVER_SUPPORTS_SALES)
  14060. if( tab->tab == CASHSHOP_TAB_SALE ){
  14061. return;
  14062. }
  14063. #endif
  14064. PACKET_ZC_ACK_SE_CASH_ITEM_LIST2* p = reinterpret_cast<PACKET_ZC_ACK_SE_CASH_ITEM_LIST2*>( packet_buffer );
  14065. p->packetType = HEADER_ZC_ACK_SE_CASH_ITEM_LIST2;
  14066. p->packetLength = sizeof( *p );
  14067. p->tab = tab->tab;
  14068. p->count = 0;
  14069. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14070. p->items[p->count].itemId = client_nameid( item->nameid );
  14071. p->items[p->count].price = item->price;
  14072. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14073. p->count++;
  14074. }
  14075. clif_send( p, p->packetLength, &sd->bl, SELF );
  14076. #endif
  14077. #endif
  14078. }
  14079. //08ca <len>.W <itemcount> W <tabcode>.W (ZC_ACK_SCHEDULER_CASHITEM)
  14080. void clif_cashshop_list( map_session_data& sd ){
  14081. for( const auto& pair : cash_shop_db ){
  14082. std::shared_ptr<s_cash_item_tab> tab = pair.second;
  14083. // Skip empty tabs, the client only expects filled ones
  14084. if( tab->items.empty() ){
  14085. continue;
  14086. }
  14087. #if !(PACKETVER_SUPPORTS_SALES)
  14088. if( tab->tab == CASHSHOP_TAB_SALE ){
  14089. continue;
  14090. }
  14091. #endif
  14092. PACKET_ZC_ACK_SCHEDULER_CASHITEM* p = reinterpret_cast<PACKET_ZC_ACK_SCHEDULER_CASHITEM*>( packet_buffer );
  14093. p->packetType = HEADER_ZC_ACK_SCHEDULER_CASHITEM;
  14094. p->packetLength = sizeof( *p );
  14095. p->count = 0;
  14096. p->tabNum = tab->tab;
  14097. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14098. p->items[p->count].itemId = client_nameid( item->nameid );
  14099. p->items[p->count].price = item->price;
  14100. #ifdef ENABLE_CASHSHOP_PREVIEW_PATCH
  14101. struct item_data* id = itemdb_search( item->nameid );
  14102. if( id == nullptr ){
  14103. p->items[p->count].location = 0;
  14104. p->items[p->count].viewSprite = 0;
  14105. }else{
  14106. p->items[p->count].location = pc_equippoint_sub( &sd, id );
  14107. p->items[p->count].viewSprite = id->look;
  14108. }
  14109. #endif
  14110. p->count++;
  14111. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14112. if( ( static_cast<size_t>( p->packetLength ) + sizeof( p->items[0] ) ) >= INT16_MAX ){
  14113. // Send current data
  14114. clif_send( p, p->packetLength, &sd.bl, SELF );
  14115. // Start a new packet
  14116. p->count = 0;
  14117. p->packetLength = sizeof( *p );
  14118. }
  14119. }
  14120. clif_send( p, p->packetLength, &sd.bl, SELF );
  14121. }
  14122. }
  14123. void clif_parse_cashshop_list_request( int fd, map_session_data* sd ){
  14124. if( !sd->status.cashshop_sent ) {
  14125. clif_cashshop_list( *sd );
  14126. #if PACKETVER_SUPPORTS_SALES
  14127. sale_notify_login(sd);
  14128. #endif
  14129. sd->status.cashshop_sent = true;
  14130. }
  14131. }
  14132. /// List of items offered in a cash shop (ZC_PC_CASH_POINT_ITEMLIST).
  14133. /// 0287 <packet len>.W <cash point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }*
  14134. /// 0287 <packet len>.W <cash point>.L <kafra point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }* (PACKETVER >= 20070711)
  14135. void clif_cashshop_show( map_session_data& sd, npc_data& nd ){
  14136. sd.npc_shopid = nd.bl.id;
  14137. int cost[2] = { 0, 0 };
  14138. npc_shop_currency_type( &sd, &nd, cost, true );
  14139. PACKET_ZC_PC_CASH_POINT_ITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_CASH_POINT_ITEMLIST*>( packet_buffer );
  14140. p->packetType = HEADER_ZC_PC_CASH_POINT_ITEMLIST;
  14141. p->packetLength = sizeof( *p );
  14142. p->cashPoints = cost[0];
  14143. #if PACKETVER >= 20070711
  14144. p->kafraPoints = cost[1];
  14145. #endif
  14146. for( int i = 0; i < nd.u.shop.count; i++ ) {
  14147. struct item_data* id = itemdb_search( nd.u.shop.shop_item[i].nameid );
  14148. p->items[i].price = nd.u.shop.shop_item[i].value;
  14149. p->items[i].discountPrice = nd.u.shop.shop_item[i].value; // Discount Price
  14150. p->items[i].itemType = itemtype( id->nameid );
  14151. p->items[i].itemId = client_nameid( id->nameid );
  14152. #ifdef ENABLE_OLD_CASHSHOP_PREVIEW_PATCH
  14153. p->items[i].location = pc_equippoint_sub( &sd, id );
  14154. p->items[i].viewSprite = id->look;
  14155. memset( p->items[i].unused, 0, sizeof( p->items[i].unused ) );
  14156. #endif
  14157. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14158. }
  14159. clif_send( p, p->packetLength, &sd.bl, SELF );
  14160. }
  14161. /// Cashshop Buy Ack (ZC_PC_CASH_POINT_UPDATE).
  14162. /// 0289 <cash point>.L <error>.W
  14163. /// 0289 <cash point>.L <kafra point>.L <error>.W (PACKETVER >= 20070711)
  14164. /// error:
  14165. /// 0 = The deal has successfully completed. (ERROR_TYPE_NONE)
  14166. /// 1 = The Purchase has failed because the NPC does not exist. (ERROR_TYPE_NPC)
  14167. /// 2 = The Purchase has failed because the Kafra Shop System is not working correctly. (ERROR_TYPE_SYSTEM)
  14168. /// 3 = You are over your Weight Limit. (ERROR_TYPE_INVENTORY_WEIGHT)
  14169. /// 4 = You cannot purchase items while you are in a trade. (ERROR_TYPE_EXCHANGE)
  14170. /// 5 = The Purchase has failed because the Item Information was incorrect. (ERROR_TYPE_ITEM_ID)
  14171. /// 6 = You do not have enough Kafra Credit Points. (ERROR_TYPE_MONEY)
  14172. /// 7 = You can purchase up to 10 items. (ERROR_TYPE_AMOUNT)
  14173. /// 8 = Some items could not be purchased. (ERROR_TYPE_PURCHASE_FAIL)
  14174. void clif_cashshop_ack(map_session_data* sd, int error)
  14175. {
  14176. int fd, cost[2] = { 0, 0 };
  14177. struct npc_data *nd;
  14178. nullpo_retv(sd);
  14179. fd = sd->fd;
  14180. nd = map_id2nd(sd->npc_shopid);
  14181. npc_shop_currency_type(sd, nd, cost, false);
  14182. WFIFOHEAD(fd, packet_len(0x289));
  14183. WFIFOW(fd,0) = 0x289;
  14184. WFIFOL(fd,2) = cost[0];
  14185. #if PACKETVER < 20070711
  14186. WFIFOW(fd,6) = TOW(error);
  14187. #else
  14188. WFIFOL(fd,6) = cost[1];
  14189. WFIFOW(fd,10) = TOW(error);
  14190. #endif
  14191. WFIFOSET(fd, packet_len(0x289));
  14192. }
  14193. void clif_cashshop_result( map_session_data *sd, t_itemid item_id, uint16 result ){
  14194. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined( PACKETVER_ZERO )
  14195. nullpo_retv( sd );
  14196. PACKET_ZC_SE_PC_BUY_CASHITEM_RESULT packet = {};
  14197. packet.packetType = HEADER_ZC_SE_PC_BUY_CASHITEM_RESULT;
  14198. if( item_id != 0 ){
  14199. packet.itemId = client_nameid( item_id );
  14200. }else{
  14201. packet.itemId = 0;
  14202. }
  14203. packet.result = result;
  14204. packet.cashPoints = sd->cashPoints;
  14205. packet.kafraPoints = sd->kafraPoints;
  14206. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  14207. #endif
  14208. }
  14209. /// Request to buy item(s) from cash shop.
  14210. /// 0288 <name id>.W <amount>.W (CZ_PC_BUY_CASH_POINT_ITEM)
  14211. /// 0288 <name id>.W <amount>.W <kafra points>.L (PACKETVER >= 20070711)
  14212. /// 0288 <packet len>.W <kafra points>.L <count>.W { <amount>.W <name id>.W }.4B*count (PACKETVER >= 20100803)
  14213. void clif_parse_npccashshop_buy( int fd, map_session_data *sd ){
  14214. nullpo_retv( sd );
  14215. if( sd->state.trading || !sd->npc_shopid ){
  14216. clif_cashshop_ack( sd, 1 );
  14217. return;
  14218. }
  14219. const PACKET_CZ_PC_BUY_CASH_POINT_ITEM* p = reinterpret_cast<PACKET_CZ_PC_BUY_CASH_POINT_ITEM*>( RFIFOP( fd, 0 ) );
  14220. #if PACKETVER < 20070711
  14221. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, 0 ) );
  14222. #elif PACKETVER < 20101116
  14223. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, p->kafraPoints ) );
  14224. #else
  14225. int s_itl = sizeof( p->items[0] );
  14226. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14227. 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 );
  14228. return;
  14229. }
  14230. std::vector<s_npc_buy_list> item_list = {};
  14231. item_list.reserve( p->count );
  14232. for( int i = 0; i < p->count; i++ ){
  14233. s_npc_buy_list item = {};
  14234. item.nameid = p->items[i].itemId;
  14235. item.qty = p->items[i].amount;
  14236. item_list.push_back( item );
  14237. }
  14238. clif_cashshop_ack( sd, npc_cashshop_buylist( sd, p->kafraPoints, item_list ) );
  14239. #endif
  14240. }
  14241. /// Request to buy item(s) from cash shop.
  14242. /// 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)
  14243. void clif_parse_cashshop_buy( int fd, map_session_data *sd ){
  14244. const PACKET_CZ_SE_PC_BUY_CASHITEM_LIST* p = reinterpret_cast<PACKET_CZ_SE_PC_BUY_CASHITEM_LIST*>( RFIFOP( fd, 0 ) );
  14245. int s_itl = sizeof( p->items[0] );
  14246. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14247. 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 );
  14248. return;
  14249. }
  14250. cashshop_buylist( sd, p->kafraPoints, p->count, p->items );
  14251. }
  14252. /// Adoption System
  14253. ///
  14254. /// Adoption message (ZC_BABYMSG).
  14255. /// 0216 <msg>.L
  14256. /// msg:
  14257. /// ADOPT_REPLY_MORE_CHILDREN = "You cannot adopt more than 1 child."
  14258. /// ADOPT_REPLY_LEVEL_70 = "You must be at least character level 70 in order to adopt someone."
  14259. /// ADOPT_REPLY_MARRIED = "You cannot adopt a married person."
  14260. void clif_Adopt_reply(map_session_data *sd, int type)
  14261. {
  14262. int fd = sd->fd;
  14263. WFIFOHEAD(fd,6);
  14264. WFIFOW(fd,0) = 0x216;
  14265. WFIFOL(fd,2) = type;
  14266. WFIFOSET(fd,6);
  14267. }
  14268. /// Adoption confirmation (ZC_REQ_BABY).
  14269. /// 01f6 <account id>.L <char id>.L <name>.B
  14270. void clif_Adopt_request(map_session_data *sd, map_session_data *src, int p_id)
  14271. {
  14272. int fd = sd->fd;
  14273. WFIFOHEAD(fd,34);
  14274. WFIFOW(fd,0) = 0x1f6;
  14275. WFIFOL(fd,2) = src->status.account_id;
  14276. WFIFOL(fd,6) = p_id;
  14277. safestrncpy(WFIFOCP(fd,10), src->status.name, NAME_LENGTH);
  14278. WFIFOSET(fd,34);
  14279. }
  14280. /// Request to adopt a player (CZ_REQ_JOIN_BABY).
  14281. /// 01f9 <account id>.L
  14282. void clif_parse_Adopt_request(int fd, map_session_data *sd)
  14283. {
  14284. TBL_PC *tsd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  14285. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  14286. if( pc_try_adopt(sd, p_sd, tsd) == ADOPT_ALLOWED )
  14287. {
  14288. tsd->adopt_invite = sd->status.account_id;
  14289. clif_Adopt_request(tsd, sd, p_sd->status.account_id);
  14290. }
  14291. }
  14292. /// Answer to adopt confirmation (CZ_JOIN_BABY).
  14293. /// 01f7 <account id>.L <char id>.L <answer>.L
  14294. /// answer:
  14295. /// 0 = rejected
  14296. /// 1 = accepted
  14297. void clif_parse_Adopt_reply(int fd, map_session_data *sd){
  14298. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  14299. int p1_id = RFIFOL(fd,info->pos[0]);
  14300. int p2_id = RFIFOL(fd,info->pos[1]);
  14301. int result = RFIFOL(fd,info->pos[2]);
  14302. map_session_data* p1_sd = map_id2sd(p1_id);
  14303. map_session_data* p2_sd = map_id2sd(p2_id);
  14304. int pid = sd->adopt_invite;
  14305. sd->adopt_invite = 0;
  14306. if( p1_sd == nullptr || p2_sd == nullptr )
  14307. return; // Both players need to be online
  14308. if( pid != p1_sd->status.account_id )
  14309. return; // Incorrect values
  14310. if( result == 0 )
  14311. return; // Rejected
  14312. pc_adoption(p1_sd, p2_sd, sd);
  14313. }
  14314. /// Convex Mirror
  14315. /// 0293 <infoType>.B <x>.L <y>.L <minHours>.W <minMinutes>.W <maxHours>.W <maxMinutes>.W <monster name>.51B (ZC_BOSS_INFO)
  14316. /// infoType:
  14317. /// BOSS_INFO_NOT = No boss on this map.
  14318. /// BOSS_INFO_ALIVE = Boss is alive (position update).
  14319. /// BOSS_INFO_ALIVE_WITHMSG = Boss is alive (initial announce).
  14320. /// BOSS_INFO_DEAD = Boss is dead.
  14321. void clif_bossmapinfo( map_session_data& sd, mob_data* md, e_bossmap_info flag ){
  14322. PACKET_ZC_BOSS_INFO p = {};
  14323. p.packetType = HEADER_ZC_BOSS_INFO;
  14324. p.type = flag;
  14325. switch (flag) {
  14326. case BOSS_INFO_NOT:
  14327. // No data required
  14328. break;
  14329. case BOSS_INFO_ALIVE_WITHMSG:
  14330. [[fallthrough]];
  14331. case BOSS_INFO_ALIVE:
  14332. p.x = md->bl.x;
  14333. p.y = md->bl.y;
  14334. break;
  14335. case BOSS_INFO_DEAD: {
  14336. const struct TimerData * timer_data = get_timer(md->spawn_timer);
  14337. unsigned int seconds;
  14338. int hours, minutes;
  14339. seconds = (unsigned int)(DIFF_TICK(timer_data->tick, gettick()) / 1000 + 60);
  14340. hours = seconds / (60 * 60);
  14341. seconds = seconds - (60 * 60 * hours);
  14342. minutes = seconds / 60;
  14343. p.minHours = hours;
  14344. p.minMinutes = minutes;
  14345. break;
  14346. }
  14347. }
  14348. if( md != nullptr ){
  14349. safestrncpy( p.name, md->db->jname.c_str(), sizeof( p.name ) );
  14350. }
  14351. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  14352. }
  14353. /// Requesting equip of a player (CZ_EQUIPWIN_MICROSCOPE).
  14354. /// 02d6 <account id>.L
  14355. void clif_parse_ViewPlayerEquip(int fd, map_session_data* sd)
  14356. {
  14357. int aid = RFIFOL(fd, packet_db[RFIFOW(fd,0)].pos[0]);
  14358. map_session_data* tsd = map_id2sd(aid);
  14359. if (!tsd)
  14360. return;
  14361. if (sd->bl.m != tsd->bl.m)
  14362. return;
  14363. else if( tsd->status.show_equip || pc_has_permission(sd, PC_PERM_VIEW_EQUIPMENT) )
  14364. clif_viewequip_ack( *sd, *tsd );
  14365. else
  14366. clif_msg(sd, MSI_OPEN_EQUIPEDITEM_REFUSED);
  14367. }
  14368. /// Request to a configuration.
  14369. /// 02d8 <type>.L <flag>.L (CZ_CONFIG)
  14370. /// type: see enum e_config_type
  14371. /// flag:
  14372. /// false = disabled
  14373. /// true = enabled
  14374. void clif_parse_configuration( int fd, map_session_data* sd ){
  14375. int cmd = RFIFOW(fd,0);
  14376. int type = RFIFOL(fd,packet_db[cmd].pos[0]);
  14377. bool flag = RFIFOL(fd,packet_db[cmd].pos[1]) != 0;
  14378. switch( type ){
  14379. case CONFIG_OPEN_EQUIPMENT_WINDOW:
  14380. sd->status.show_equip = flag;
  14381. break;
  14382. case CONFIG_CALL:
  14383. sd->status.disable_call = flag;
  14384. break;
  14385. case CONFIG_PET_AUTOFEED:
  14386. // Player can not click this if he does not have a pet
  14387. if( sd->pd == nullptr || !battle_config.feature_pet_autofeed || !sd->pd->get_pet_db()->allow_autofeed ){
  14388. return;
  14389. }
  14390. sd->pd->pet.autofeed = flag;
  14391. break;
  14392. case CONFIG_HOMUNCULUS_AUTOFEED:
  14393. // Player can not click this if he does not have a homunculus or it is vaporized
  14394. if( sd->hd == nullptr || sd->hd->homunculus.vaporize || !battle_config.feature_homunculus_autofeed ){
  14395. return;
  14396. }
  14397. sd->hd->homunculus.autofeed = flag;
  14398. break;
  14399. default:
  14400. ShowWarning( "clif_parse_configuration: received unknown configuration type '%d'...\n", type );
  14401. return;
  14402. }
  14403. clif_configuration( sd, static_cast<e_config_type>(type), flag );
  14404. }
  14405. /// Request to change party invitation tick.
  14406. /// 02C8 <enabled>.B (CZ_PARTY_CONFIG)
  14407. /// value:
  14408. /// 0 = disabled (triggered by /accept)
  14409. /// 1 = enabled (triggered by /refuse)
  14410. void clif_parse_PartyTick( int fd, map_session_data* sd ){
  14411. const PACKET_CZ_PARTY_CONFIG* p = reinterpret_cast<PACKET_CZ_PARTY_CONFIG*>( RFIFOP( fd, 0 ) );
  14412. sd->status.disable_partyinvite = p->refuseInvite;
  14413. clif_partyinvitationstate( *sd );
  14414. }
  14415. /// Questlog System [Kevin] [Inkfish]
  14416. ///
  14417. /**
  14418. * Safe check to init packet len & quest num for player.
  14419. * @param def_len
  14420. * @param info_len Length of each quest info.
  14421. * @param avail_quests Avail quests that player has.
  14422. * @param limit_out Limit for quest num, to make sure the quest num is still in allowed value.
  14423. * @param len_out Packet length as initial set => def_len + limit_out * info_len.
  14424. **/
  14425. static void clif_quest_len(int def_len, int info_len, int avail_quests, int *limit_out, int *len_out) {
  14426. (*limit_out) = (0xFFFF - def_len) / info_len;
  14427. (*limit_out) = (avail_quests > (*limit_out)) ? (*limit_out) : avail_quests;
  14428. (*len_out) = ((*limit_out) * info_len) + def_len;
  14429. }
  14430. std::string clif_quest_string( std::shared_ptr<s_quest_objective> objective ){
  14431. std::string race_name;
  14432. switch( objective->race ){
  14433. case RC_FORMLESS: race_name = "Formless"; break;
  14434. case RC_UNDEAD: race_name = "Undead"; break;
  14435. case RC_BRUTE: race_name = "Brute"; break;
  14436. case RC_PLANT: race_name = "Plant"; break;
  14437. case RC_INSECT: race_name = "Insect"; break;
  14438. case RC_FISH: race_name = "Fish"; break;
  14439. case RC_DEMON: race_name = "Demon"; break;
  14440. case RC_DEMIHUMAN: race_name = "Demihuman"; break;
  14441. case RC_ANGEL: race_name = "Angel"; break;
  14442. case RC_DRAGON: race_name = "Dragon"; break;
  14443. default:
  14444. ShowWarning( "clif_quest_string: Unsupported race %d - using empty string...\n", objective->race );
  14445. [[fallthrough]];
  14446. case RC_ALL: race_name = ""; break;
  14447. }
  14448. std::string size_name;
  14449. switch( objective->size ){
  14450. case SZ_SMALL: size_name = "Small"; break;
  14451. case SZ_MEDIUM: size_name = "Medium"; break;
  14452. case SZ_BIG: size_name = "Large"; break;
  14453. default:
  14454. ShowWarning( "clif_quest_string: Unsupported size %d - using empty string...\n", objective->size );
  14455. [[fallthrough]];
  14456. case SZ_ALL: size_name = ""; break;
  14457. }
  14458. std::string ele_name;
  14459. switch( objective->element ){
  14460. case ELE_NEUTRAL: ele_name = "Neutral Element"; break;
  14461. case ELE_WATER: ele_name = "Water Element"; break;
  14462. case ELE_EARTH: ele_name = "Earth Element"; break;
  14463. case ELE_FIRE: ele_name = "Fire Element"; break;
  14464. case ELE_WIND: ele_name = "Wind Element"; break;
  14465. case ELE_POISON: ele_name = "Poison Element"; break;
  14466. case ELE_HOLY: ele_name = "Holy Element"; break;
  14467. case ELE_DARK: ele_name = "Shadow Element"; break;
  14468. case ELE_GHOST: ele_name = "Ghost Element"; break;
  14469. case ELE_UNDEAD: ele_name = "Undead Element"; break;
  14470. default:
  14471. ShowWarning( "clif_quest_string: Unsupported element %d - using empty string...\n", objective->element );
  14472. [[fallthrough]];
  14473. case ELE_ALL: ele_name = ""; break;
  14474. }
  14475. std::string str;
  14476. if( !objective->map_name.empty() ){
  14477. str += objective->map_name;
  14478. }
  14479. if( !race_name.empty() ){
  14480. if( !str.empty() ){
  14481. str += ", ";
  14482. }
  14483. str += race_name;
  14484. }
  14485. if( !size_name.empty() ){
  14486. if( !str.empty() ){
  14487. str += ", ";
  14488. }
  14489. str += size_name;
  14490. }
  14491. if( !ele_name.empty() ){
  14492. if( !str.empty() ){
  14493. str += ", ";
  14494. }
  14495. str += ele_name;
  14496. }
  14497. return str;
  14498. }
  14499. /// Sends list of all quest states
  14500. /// 02b1 <packet len>.W <num>.L { <quest id>.L <active>.B }*num (ZC_ALL_QUEST_LIST)
  14501. /// 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)
  14502. /// 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)
  14503. void clif_quest_send_list(map_session_data *sd)
  14504. {
  14505. nullpo_retv(sd);
  14506. int fd = sd->fd;
  14507. int i;
  14508. int offset = 8;
  14509. int limit = 0;
  14510. #if PACKETVER >= 20141022
  14511. #if PACKETVER >= 20150513
  14512. int size = 22 + NAME_LENGTH;
  14513. #else
  14514. int size = 10 + NAME_LENGTH;
  14515. #endif
  14516. clif_quest_len(offset, 15 + ( size * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &i);
  14517. WFIFOHEAD(fd,i);
  14518. #if PACKETVER >= 20150513
  14519. WFIFOW(fd, 0) = 0x9f8;
  14520. #else
  14521. WFIFOW(fd, 0) = 0x97a;
  14522. #endif
  14523. WFIFOL(fd, 4) = limit;
  14524. for (i = 0; i < limit; i++) {
  14525. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14526. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14527. offset += 4;
  14528. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14529. offset++;
  14530. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14531. offset += 4;
  14532. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time);
  14533. offset += 4;
  14534. WFIFOW(fd, offset) = static_cast<uint16>(qi->objectives.size());
  14535. offset += 2;
  14536. if (!qi->objectives.empty()) {
  14537. std::shared_ptr<s_mob_db> mob;
  14538. for (int j = 0; j < qi->objectives.size(); j++) {
  14539. mob = mob_db.find(qi->objectives[j]->mob_id);
  14540. e_race race = qi->objectives[j]->race;
  14541. e_size size = qi->objectives[j]->size;
  14542. e_element element = qi->objectives[j]->element;
  14543. #if PACKETVER >= 20150513
  14544. WFIFOL(fd, offset) = sd->quest_log[i].quest_id * 1000 + j;
  14545. offset += 4;
  14546. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0)));
  14547. offset += 4;
  14548. #endif
  14549. WFIFOL(fd, offset) = ((mob && qi->objectives[j]->mob_id > 0) ? qi->objectives[j]->mob_id : MOBID_PORING);
  14550. offset += 4;
  14551. #if PACKETVER >= 20150513
  14552. WFIFOW(fd, offset) = qi->objectives[j]->min_level;
  14553. offset += 2;
  14554. WFIFOW(fd, offset) = qi->objectives[j]->max_level;
  14555. offset += 2;
  14556. #endif
  14557. WFIFOW(fd, offset) = sd->quest_log[i].count[j];
  14558. offset += 2;
  14559. WFIFOW(fd, offset) = qi->objectives[j]->count;
  14560. offset += 2;
  14561. if (mob && qi->objectives[j]->mob_id > 0)
  14562. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14563. else
  14564. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14565. offset += NAME_LENGTH;
  14566. }
  14567. }
  14568. }
  14569. WFIFOW(fd, 2) = offset;
  14570. WFIFOSET(fd, offset);
  14571. #else
  14572. clif_quest_len(offset, 5, sd->avail_quests, &limit, &i);
  14573. WFIFOHEAD(fd,i);
  14574. WFIFOW(fd, 0) = 0x2b1;
  14575. WFIFOL(fd, 4) = limit;
  14576. for (i = 0; i < limit; i++) {
  14577. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14578. offset += 4;
  14579. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14580. offset += 1;
  14581. }
  14582. WFIFOW(fd, 2) = offset;
  14583. WFIFOSET(fd, offset);
  14584. #endif
  14585. }
  14586. /// Sends list of all quest missions (ZC_ALL_QUEST_MISSION).
  14587. /// 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
  14588. void clif_quest_send_mission(map_session_data *sd)
  14589. {
  14590. nullpo_retv(sd);
  14591. int fd = sd->fd;
  14592. int limit = 0;
  14593. int len = sd->avail_quests*104+8;
  14594. clif_quest_len(8, 14 + ((6 + NAME_LENGTH) * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &len);
  14595. WFIFOHEAD(fd, len);
  14596. WFIFOW(fd, 0) = 0x2b2;
  14597. WFIFOW(fd, 2) = len;
  14598. WFIFOL(fd, 4) = limit;
  14599. for (int i = 0; i < limit; i++) {
  14600. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14601. WFIFOL(fd, i*104+8) = sd->quest_log[i].quest_id;
  14602. WFIFOL(fd, i*104+12) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14603. WFIFOL(fd, i*104+16) = static_cast<uint32>(sd->quest_log[i].time);
  14604. WFIFOW(fd, i*104+20) = static_cast<uint16>(qi->objectives.size());
  14605. for (int j = 0 ; j < qi->objectives.size(); j++) {
  14606. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[j]->mob_id);
  14607. WFIFOL(fd, i*104+22+j*30) = (mob ? qi->objectives[j]->mob_id : MOBID_PORING);
  14608. WFIFOW(fd, i*104+26+j*30) = sd->quest_log[i].count[j];
  14609. if (mob && qi->objectives[j]->mob_id > 0)
  14610. safestrncpy(WFIFOCP(fd, i*104+28+j*30), mob->jname.c_str(), NAME_LENGTH);
  14611. else
  14612. safestrncpy(WFIFOCP(fd, i*104+28+j*30), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14613. }
  14614. }
  14615. WFIFOSET(fd, len);
  14616. }
  14617. /// Notification about a new quest
  14618. /// 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)
  14619. /// 08fe <packet len>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_HUNTING_QUEST_INFO)
  14620. /// 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)
  14621. void clif_quest_add(map_session_data *sd, struct quest *qd)
  14622. {
  14623. nullpo_retv(sd);
  14624. nullpo_retv(qd);
  14625. int fd = sd->fd;
  14626. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14627. if (!qi)
  14628. return;
  14629. #if PACKETVER >= 20150513
  14630. int cmd = 0x9f9;
  14631. #else
  14632. int cmd = 0x2b3;
  14633. #endif
  14634. WFIFOHEAD(fd, packet_len(cmd));
  14635. WFIFOW(fd, 0) = cmd;
  14636. WFIFOL(fd, 2) = qd->quest_id;
  14637. WFIFOB(fd, 6) = qd->state;
  14638. WFIFOB(fd, 7) = static_cast<uint8>(qd->time - qi->time);
  14639. WFIFOL(fd, 11) = static_cast<uint32>(qd->time);
  14640. WFIFOW(fd, 15) = static_cast<uint16>(qi->objectives.size());
  14641. for (int i = 0, offset = 17; i < qi->objectives.size(); i++) {
  14642. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[i]->mob_id);
  14643. e_race race = qi->objectives[i]->race;
  14644. e_size size = qi->objectives[i]->size;
  14645. e_element element = qi->objectives[i]->element;
  14646. #if PACKETVER >= 20150513
  14647. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14648. offset += 4;
  14649. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0))); // effect ?
  14650. offset += 4;
  14651. #endif
  14652. 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
  14653. offset += 4;
  14654. #if PACKETVER >= 20150513
  14655. WFIFOW(fd, offset) = qi->objectives[i]->min_level;
  14656. offset += 2;
  14657. WFIFOW(fd, offset) = qi->objectives[i]->max_level;
  14658. offset += 2;
  14659. #endif
  14660. WFIFOW(fd, offset) = qd->count[i];
  14661. offset += 2;
  14662. if (mob && qi->objectives[i]->mob_id > 0)
  14663. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14664. else
  14665. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[i]).c_str(), NAME_LENGTH);
  14666. offset += NAME_LENGTH;
  14667. }
  14668. WFIFOSET(fd, packet_len(cmd));
  14669. #if PACKETVER >= 20150513
  14670. int16 len;
  14671. len = static_cast<decltype(len)>( 4 + qi->objectives.size() * 12 );
  14672. WFIFOHEAD(fd, len);
  14673. WFIFOW(fd, 0) = 0x8fe;
  14674. WFIFOW(fd, 2) = len;
  14675. for (int i = 0, offset = 4; i < qi->objectives.size(); i++, offset += 12) {
  14676. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14677. WFIFOL(fd, offset + 4) = qi->objectives[i]->mob_id;
  14678. WFIFOW(fd, offset + 8) = qi->objectives[i]->count;
  14679. WFIFOW(fd, offset + 10) = qd->count[i];
  14680. }
  14681. WFIFOSET(fd, len);
  14682. #endif
  14683. }
  14684. /// Notification about a quest being removed (ZC_DEL_QUEST).
  14685. /// 02b4 <quest id>.L
  14686. void clif_quest_delete(map_session_data *sd, int quest_id)
  14687. {
  14688. int fd = sd->fd;
  14689. WFIFOHEAD(fd, packet_len(0x2b4));
  14690. WFIFOW(fd, 0) = 0x2b4;
  14691. WFIFOL(fd, 2) = quest_id;
  14692. WFIFOSET(fd, packet_len(0x2b4));
  14693. }
  14694. /// Notification of an update to the hunting mission counter
  14695. /// 02b5 <packet len>.W <mobs>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT)
  14696. /// 09fa <packet len>.W <mobs>.W { <quest id>.L <hunt identification>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT_EX)
  14697. void clif_quest_update_objective(map_session_data *sd, struct quest *qd)
  14698. {
  14699. int fd = sd->fd;
  14700. int offset = 6;
  14701. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14702. int16 len;
  14703. len = static_cast<decltype(len)>( qi->objectives.size() * 12 + 6 );
  14704. #if PACKETVER >= 20150513
  14705. int cmd = 0x9fa;
  14706. #else
  14707. int cmd = 0x2b5;
  14708. #endif
  14709. WFIFOHEAD(fd, len);
  14710. WFIFOW(fd, 0) = cmd;
  14711. WFIFOW(fd, 4) = static_cast<uint16>(qi->objectives.size());
  14712. for (int i = 0; i < qi->objectives.size(); i++) {
  14713. WFIFOL(fd, offset) = qd->quest_id;
  14714. offset += 4;
  14715. #if PACKETVER >= 20150513
  14716. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14717. offset += 4;
  14718. #else
  14719. WFIFOL(fd, offset) = qi->objectives[i]->mob_id;
  14720. offset += 4;
  14721. #endif
  14722. WFIFOW(fd, offset) = qi->objectives[i]->count;
  14723. offset += 2;
  14724. WFIFOW(fd, offset) = qd->count[i];
  14725. offset += 2;
  14726. }
  14727. WFIFOW(fd, 2) = offset;
  14728. WFIFOSET(fd, offset);
  14729. }
  14730. /// Request to change the state of a quest (CZ_ACTIVE_QUEST).
  14731. /// 02b6 <quest id>.L <active>.B
  14732. void clif_parse_questStateAck(int fd, map_session_data *sd)
  14733. {
  14734. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  14735. quest_update_status(sd, RFIFOL(fd,info->pos[0]),
  14736. RFIFOB(fd,info->pos[1])?Q_ACTIVE:Q_INACTIVE);
  14737. }
  14738. /// Notification about the change of a quest state (ZC_ACTIVE_QUEST).
  14739. /// 02b7 <quest id>.L <active>.B
  14740. void clif_quest_update_status(map_session_data *sd, int quest_id, bool active)
  14741. {
  14742. int fd = sd->fd;
  14743. WFIFOHEAD(fd, packet_len(0x2b7));
  14744. WFIFOW(fd, 0) = 0x2b7;
  14745. WFIFOL(fd, 2) = quest_id;
  14746. WFIFOB(fd, 6) = active;
  14747. WFIFOSET(fd, packet_len(0x2b7));
  14748. }
  14749. /// Notification about an NPC's quest state (ZC_QUEST_NOTIFY_EFFECT).
  14750. /// 0446 <npc id>.L <x>.W <y>.W <effect>.W <color>.W
  14751. /// effect:
  14752. /// 0 = none
  14753. /// 1 = exclamation mark icon
  14754. /// 2 = question mark icon
  14755. /// color:
  14756. /// 0 = yellow
  14757. /// 1 = orange
  14758. /// 2 = green
  14759. /// 3 = purple
  14760. void clif_quest_show_event(map_session_data *sd, struct block_list *bl, e_questinfo_types effect, e_questinfo_markcolor color)
  14761. {
  14762. #if PACKETVER >= 20090218
  14763. nullpo_retv(sd);
  14764. nullpo_retv(bl);
  14765. int fd = sd->fd;
  14766. WFIFOHEAD(fd, packet_len(0x446));
  14767. WFIFOW(fd, 0) = 0x446;
  14768. WFIFOL(fd, 2) = bl->id;
  14769. WFIFOW(fd, 6) = bl->x;
  14770. WFIFOW(fd, 8) = bl->y;
  14771. WFIFOW(fd, 10) = effect;
  14772. WFIFOW(fd, 12) = color;
  14773. WFIFOSET(fd, packet_len(0x446));
  14774. #endif
  14775. }
  14776. /// Mercenary System
  14777. ///
  14778. /// Notification about a mercenary status parameter change (ZC_MER_PAR_CHANGE).
  14779. /// 02a2 <var id>.W <value>.L
  14780. void clif_mercenary_updatestatus(map_session_data *sd, int type)
  14781. {
  14782. s_mercenary_data *md;
  14783. struct status_data *status;
  14784. int fd;
  14785. if( !clif_session_isValid(sd) || (md = sd->md) == nullptr )
  14786. return;
  14787. fd = sd->fd;
  14788. status = &md->battle_status;
  14789. WFIFOHEAD(fd,packet_len(0x2a2));
  14790. WFIFOW(fd,0) = 0x2a2;
  14791. WFIFOW(fd,2) = type;
  14792. switch( type ) {
  14793. case SP_ATK1:
  14794. {
  14795. int atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14796. WFIFOL(fd,4) = cap_value(atk, 0, INT16_MAX);
  14797. }
  14798. break;
  14799. case SP_MATK1:
  14800. WFIFOL(fd,4) = min(status->matk_max, UINT16_MAX);
  14801. break;
  14802. case SP_HIT:
  14803. WFIFOL(fd,4) = status->hit;
  14804. break;
  14805. case SP_CRITICAL:
  14806. WFIFOL(fd,4) = status->cri/10;
  14807. break;
  14808. case SP_DEF1:
  14809. WFIFOL(fd,4) = status->def;
  14810. break;
  14811. case SP_MDEF1:
  14812. WFIFOL(fd,4) = status->mdef;
  14813. break;
  14814. case SP_MERCFLEE:
  14815. WFIFOL(fd,4) = status->flee;
  14816. break;
  14817. case SP_ASPD:
  14818. WFIFOL(fd,4) = status->amotion;
  14819. break;
  14820. case SP_HP:
  14821. WFIFOL(fd,4) = status->hp;
  14822. break;
  14823. case SP_MAXHP:
  14824. WFIFOL(fd,4) = status->max_hp;
  14825. break;
  14826. case SP_SP:
  14827. WFIFOL(fd,4) = status->sp;
  14828. break;
  14829. case SP_MAXSP:
  14830. WFIFOL(fd,4) = status->max_sp;
  14831. break;
  14832. case SP_MERCKILLS:
  14833. WFIFOL(fd,4) = md->mercenary.kill_count;
  14834. break;
  14835. case SP_MERCFAITH:
  14836. WFIFOL(fd,4) = mercenary_get_faith(md);
  14837. break;
  14838. }
  14839. WFIFOSET(fd,packet_len(0x2a2));
  14840. }
  14841. /// Mercenary base status data (ZC_MER_INIT).
  14842. /// 029b <id>.L <atk>.W <matk>.W <hit>.W <crit>.W <def>.W <mdef>.W <flee>.W <aspd>.W
  14843. /// <name>.24B <level>.W <hp>.L <maxhp>.L <sp>.L <maxsp>.L <expire time>.L <faith>.W
  14844. /// <calls>.L <kills>.L <atk range>.W
  14845. void clif_mercenary_info(map_session_data *sd)
  14846. {
  14847. int fd;
  14848. s_mercenary_data *md;
  14849. struct status_data *status;
  14850. int atk;
  14851. if( !clif_session_isValid(sd) || (md = sd->md) == nullptr )
  14852. return;
  14853. fd = sd->fd;
  14854. status = &md->battle_status;
  14855. WFIFOHEAD(fd,packet_len(0x29b));
  14856. WFIFOW(fd,0) = 0x29b;
  14857. WFIFOL(fd,2) = md->bl.id;
  14858. // Mercenary shows ATK as a random value between ATK ~ ATK2
  14859. atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14860. WFIFOW(fd,6) = cap_value(atk, 0, INT16_MAX);
  14861. WFIFOW(fd,8) = min(status->matk_max, UINT16_MAX);
  14862. WFIFOW(fd,10) = status->hit;
  14863. WFIFOW(fd,12) = status->cri/10;
  14864. WFIFOW(fd,14) = status->def;
  14865. WFIFOW(fd,16) = status->mdef;
  14866. WFIFOW(fd,18) = status->flee;
  14867. WFIFOW(fd,20) = status->amotion;
  14868. safestrncpy(WFIFOCP(fd,22), md->db->name.c_str(), NAME_LENGTH);
  14869. WFIFOW(fd,46) = md->db->lv;
  14870. WFIFOL(fd,48) = status->hp;
  14871. WFIFOL(fd,52) = status->max_hp;
  14872. WFIFOL(fd,56) = status->sp;
  14873. WFIFOL(fd,60) = status->max_sp;
  14874. WFIFOL(fd,64) = client_tick(time(nullptr) + (mercenary_get_lifetime(md) / 1000));
  14875. WFIFOW(fd,68) = mercenary_get_faith(md);
  14876. WFIFOL(fd,70) = mercenary_get_calls(md);
  14877. WFIFOL(fd,74) = md->mercenary.kill_count;
  14878. WFIFOW(fd,78) = md->battle_status.rhw.range;
  14879. WFIFOSET(fd,packet_len(0x29b));
  14880. }
  14881. /// Mercenary skill tree (ZC_MER_SKILLINFO_LIST).
  14882. /// 029d <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  14883. void clif_mercenary_skillblock(map_session_data *sd)
  14884. {
  14885. s_mercenary_data *md;
  14886. int fd, len = 4;
  14887. if( sd == nullptr || (md = sd->md) == nullptr )
  14888. return;
  14889. fd = sd->fd;
  14890. WFIFOHEAD(fd,4+37*MAX_MERCSKILL);
  14891. WFIFOW(fd,0) = 0x29d;
  14892. for (const auto &it : md->db->skill) {
  14893. uint16 id = it.first;
  14894. if (!SKILL_CHK_MERC(id))
  14895. continue;
  14896. uint16 lv = it.second;
  14897. WFIFOW(fd,len) = id;
  14898. WFIFOL(fd,len+2) = skill_get_inf(id);
  14899. WFIFOW(fd,len+6) = lv;
  14900. WFIFOW(fd,len+8) = skill_get_sp(id, lv);
  14901. WFIFOW(fd,len+10) = skill_get_range2(&md->bl, id, lv, false);
  14902. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  14903. WFIFOB(fd,len+36) = 0; // Skillable for Mercenary?
  14904. len += 37;
  14905. }
  14906. WFIFOW(fd,2) = len;
  14907. WFIFOSET(fd,len);
  14908. }
  14909. /// Request to invoke a mercenary menu action (CZ_MER_COMMAND).
  14910. /// 029f <command>.B
  14911. /// 1 = mercenary information
  14912. /// 2 = delete
  14913. void clif_parse_mercenary_action(int fd, map_session_data* sd)
  14914. {
  14915. int option = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  14916. if( sd->md == nullptr )
  14917. return;
  14918. if( option == 2 ) mercenary_delete(sd->md, 2);
  14919. }
  14920. /// Notification about the remaining time of a rental item.
  14921. /// 0298 <name id>.W <seconds>.L (ZC_CASH_TIME_COUNTER)
  14922. void clif_rental_time( map_session_data* sd, t_itemid nameid, int seconds ){
  14923. // '<ItemName>' item will disappear in <seconds/60> minutes.
  14924. PACKET_ZC_CASH_TIME_COUNTER p = {};
  14925. p.packetType = HEADER_ZC_CASH_TIME_COUNTER;
  14926. p.itemId = client_nameid( nameid );
  14927. p.seconds = seconds;
  14928. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14929. }
  14930. /// Deletes a rental item from client's inventory.
  14931. /// 0299 <index>.W <name id>.W (ZC_CASH_ITEM_DELETE)
  14932. void clif_rental_expired( map_session_data* sd, int index, t_itemid nameid ){
  14933. // '<ItemName>' item has been deleted from the Inventory
  14934. PACKET_ZC_CASH_ITEM_DELETE p = {};
  14935. p.packetType = HEADER_ZC_CASH_ITEM_DELETE;
  14936. p.index = client_index( index );
  14937. p.itemId = client_nameid( nameid );
  14938. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14939. }
  14940. /// Book Reading (ZC_READ_BOOK).
  14941. /// 0294 <book id>.L <page>.L
  14942. void clif_readbook(int fd, int book_id, int page)
  14943. {
  14944. WFIFOHEAD(fd,packet_len(0x294));
  14945. WFIFOW(fd,0) = 0x294;
  14946. WFIFOL(fd,2) = book_id;
  14947. WFIFOL(fd,6) = page;
  14948. WFIFOSET(fd,packet_len(0x294));
  14949. }
  14950. /// Battlegrounds
  14951. ///
  14952. /// Updates HP bar of a camp member.
  14953. /// 02e0 <account id>.L <name>.24B <hp>.W <max hp>.W (ZC_BATTLEFIELD_NOTIFY_HP)
  14954. /// 0a0e <account id>.L <hp>.L <max hp>.L (ZC_BATTLEFIELD_NOTIFY_HP2)
  14955. void clif_bg_hp(map_session_data *sd) {
  14956. #if PACKETVER < 20140613
  14957. unsigned char buf[34];
  14958. const int cmd = 0x2e0;
  14959. #else
  14960. unsigned char buf[14];
  14961. const int cmd = 0xa0e;
  14962. #endif
  14963. nullpo_retv(sd);
  14964. WBUFW(buf,0) = cmd;
  14965. WBUFL(buf,2) = sd->status.account_id;
  14966. #if PACKETVER < 20140613
  14967. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  14968. if( sd->battle_status.max_hp > INT16_MAX ) { // To correctly display the %hp bar. [Skotlex]
  14969. WBUFW(buf,30) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  14970. WBUFW(buf,32) = 100;
  14971. } else {
  14972. WBUFW(buf,30) = sd->battle_status.hp;
  14973. WBUFW(buf,32) = sd->battle_status.max_hp;
  14974. }
  14975. #else
  14976. if( sd->battle_status.max_hp > INT32_MAX ) {
  14977. WBUFL(buf,6) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  14978. WBUFL(buf,10) = 100;
  14979. } else {
  14980. WBUFL(buf,6) = sd->battle_status.hp;
  14981. WBUFL(buf,10) = sd->battle_status.max_hp;
  14982. }
  14983. #endif
  14984. clif_send(buf, packet_len(cmd), &sd->bl, BG_AREA_WOS);
  14985. }
  14986. /// Updates the position of a camp member on the minimap (ZC_BATTLEFIELD_NOTIFY_POSITION).
  14987. /// 02df <account id>.L <name>.24B <class>.W <x>.W <y>.W
  14988. void clif_bg_xy(map_session_data *sd)
  14989. {
  14990. unsigned char buf[36];
  14991. nullpo_retv(sd);
  14992. WBUFW(buf,0)=0x2df;
  14993. WBUFL(buf,2)=sd->status.account_id;
  14994. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  14995. WBUFW(buf,30)=sd->status.class_;
  14996. WBUFW(buf,32)=sd->bl.x;
  14997. WBUFW(buf,34)=sd->bl.y;
  14998. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  14999. }
  15000. void clif_bg_xy_remove(map_session_data *sd)
  15001. {
  15002. unsigned char buf[36];
  15003. nullpo_retv(sd);
  15004. WBUFW(buf,0)=0x2df;
  15005. WBUFL(buf,2)=sd->status.account_id;
  15006. memset(WBUFP(buf,6), 0, NAME_LENGTH);
  15007. WBUFW(buf,30)=0;
  15008. WBUFW(buf,32)=-1;
  15009. WBUFW(buf,34)=-1;
  15010. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  15011. }
  15012. /// Notifies clients of a battleground message.
  15013. /// 02DC <packet len>.W <account id>.L <name>.24B <message>.?B (ZC_BATTLEFIELD_CHAT)
  15014. void clif_bg_message( struct s_battleground_data *bg, int src_id, const char *name, const char *mes, size_t len ){
  15015. map_session_data *sd = bg_getavailablesd( bg );
  15016. if( sd == nullptr ){
  15017. return;
  15018. }
  15019. // limit length
  15020. len = zmin( len + 1, CHAT_SIZE_MAX );
  15021. unsigned char buf[8 + NAME_LENGTH + CHAT_SIZE_MAX];
  15022. WBUFW(buf,0) = 0x2dc;
  15023. WBUFW(buf,2) = static_cast<int16>( len + NAME_LENGTH + 8 );
  15024. WBUFL(buf,4) = src_id;
  15025. safestrncpy(WBUFCP(buf,8), name, NAME_LENGTH);
  15026. safestrncpy(WBUFCP(buf,8+NAME_LENGTH), mes, len );
  15027. clif_send(buf,WBUFW(buf,2), &sd->bl, BG);
  15028. }
  15029. /// Validates and processes battlechat messages.
  15030. /// All messages that are sent after enabling battleground chat with /battlechat.
  15031. /// 02DB <packet len>.W <text>.?B (CZ_BATTLEFIELD_CHAT)
  15032. void clif_parse_BattleChat(int fd, map_session_data* sd){
  15033. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  15034. if( !clif_process_message( sd, false, name, message, output ) )
  15035. return;
  15036. bg_send_message(sd, output, strlen(output) );
  15037. }
  15038. /// Notifies client of a battleground score change (ZC_BATTLEFIELD_NOTIFY_POINT).
  15039. /// 02de <camp A points>.W <camp B points>.W
  15040. void clif_bg_updatescore(int16 m)
  15041. {
  15042. struct block_list bl;
  15043. unsigned char buf[6];
  15044. struct map_data *mapdata = map_getmapdata(m);
  15045. bl.id = 0;
  15046. bl.type = BL_NUL;
  15047. bl.m = m;
  15048. WBUFW(buf,0) = 0x2de;
  15049. WBUFW(buf,2) = mapdata->bgscore_lion;
  15050. WBUFW(buf,4) = mapdata->bgscore_eagle;
  15051. clif_send(buf,packet_len(0x2de),&bl,ALL_SAMEMAP);
  15052. }
  15053. void clif_bg_updatescore_single(map_session_data *sd)
  15054. {
  15055. int fd;
  15056. nullpo_retv(sd);
  15057. fd = sd->fd;
  15058. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  15059. WFIFOHEAD(fd,packet_len(0x2de));
  15060. WFIFOW(fd,0) = 0x2de;
  15061. WFIFOW(fd,2) = mapdata->bgscore_lion;
  15062. WFIFOW(fd,4) = mapdata->bgscore_eagle;
  15063. WFIFOSET(fd,packet_len(0x2de));
  15064. }
  15065. /// Battleground camp belong-information (ZC_BATTLEFIELD_NOTIFY_CAMPINFO).
  15066. /// 02dd <account id>.L <name>.24B <camp>.W
  15067. void clif_sendbgemblem_area(map_session_data *sd)
  15068. {
  15069. unsigned char buf[33];
  15070. nullpo_retv(sd);
  15071. WBUFW(buf, 0) = 0x2dd;
  15072. WBUFL(buf,2) = sd->bl.id;
  15073. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH); // name don't show in screen.
  15074. WBUFW(buf,30) = sd->bg_id;
  15075. clif_send(buf,packet_len(0x2dd), &sd->bl, AREA);
  15076. }
  15077. void clif_sendbgemblem_single(int fd, map_session_data *sd)
  15078. {
  15079. nullpo_retv(sd);
  15080. WFIFOHEAD(fd,32);
  15081. WFIFOW(fd,0) = 0x2dd;
  15082. WFIFOL(fd,2) = sd->bl.id;
  15083. safestrncpy(WFIFOCP(fd,6), sd->status.name, NAME_LENGTH);
  15084. WFIFOW(fd,30) = sd->bg_id;
  15085. WFIFOSET(fd,packet_len(0x2dd));
  15086. }
  15087. /// Battlegrounds queue incoming apply request from client.
  15088. /// Queue types: 1 solo queue, 2 party queue, 4 guild queue.
  15089. /// 0x8d7 <queue type>.W <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_APPLY)
  15090. void clif_parse_bg_queue_apply_request(int fd, map_session_data *sd)
  15091. {
  15092. if (!battle_config.feature_bgqueue)
  15093. return;
  15094. nullpo_retv(sd);
  15095. short type = RFIFOW(fd,2);
  15096. char name[NAME_LENGTH];
  15097. safestrncpy(name, RFIFOCP(fd, 4), NAME_LENGTH);
  15098. if (sd->bg_queue_id > 0) {
  15099. //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);
  15100. clif_bg_queue_apply_result(BG_APPLY_DUPLICATE, name, sd); // Duplicate application warning
  15101. return;
  15102. } else if (type == 1) // Solo
  15103. bg_queue_join_solo(name, sd);
  15104. else if (type == 2) // Party
  15105. bg_queue_join_party(name, sd);
  15106. else if (type == 4) // Guild
  15107. bg_queue_join_guild(name, sd);
  15108. else {
  15109. 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);
  15110. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd); // Someone sent an invalid queue type packet
  15111. return;
  15112. }
  15113. }
  15114. /// Outgoing battlegrounds queue apply result.
  15115. /// Result types: @see e_bg_queue_apply_ack
  15116. /// 0x8d8 <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_APPLY)
  15117. void clif_bg_queue_apply_result(e_bg_queue_apply_ack result, const char *name, map_session_data *sd)
  15118. {
  15119. nullpo_retv(sd);
  15120. int fd = sd->fd;
  15121. WFIFOHEAD(fd, packet_len(0x8d8));
  15122. WFIFOW(fd,0) = 0x8d8;
  15123. WFIFOB(fd,2) = result;
  15124. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15125. WFIFOSET(fd, packet_len(0x8d8));
  15126. }
  15127. /// Outgoing battlegrounds queue apply notification.
  15128. /// Sends a notification at the start of the battlegrounds queue and is also used to update the queue number.
  15129. /// 0x8d9 <battleground name>.24B <queue number>.L (ZC_NOTIFY_ENTRY_QUEUE_APPLY)
  15130. void clif_bg_queue_apply_notify(const char *name, map_session_data *sd)
  15131. {
  15132. nullpo_retv(sd);
  15133. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15134. if (queue == nullptr) {
  15135. ShowError("clif_bg_queue_apply_notify: Player is not in a battleground queue.\n");
  15136. return;
  15137. }
  15138. int fd = sd->fd;
  15139. WFIFOHEAD(fd, packet_len(0x8d9));
  15140. WFIFOW(fd,0) = 0x8d9;
  15141. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15142. WFIFOL(fd,2+NAME_LENGTH) = static_cast<int32>( queue->teama_members.size() + queue->teamb_members.size() );
  15143. WFIFOSET(fd, packet_len(0x8d9));
  15144. }
  15145. /// Battlegrounds queue outgoing cancel result.
  15146. /// 0x8db <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_CANCEL)
  15147. void clif_bg_queue_cancel_result(bool success, const char *name, map_session_data *sd)
  15148. {
  15149. nullpo_retv(sd);
  15150. int fd = sd->fd;
  15151. WFIFOHEAD(fd, packet_len(0x8d8));
  15152. WFIFOW(fd,0) = 0x8db;
  15153. WFIFOB(fd,2) = success;
  15154. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15155. WFIFOSET(fd, packet_len(0x8d8));
  15156. }
  15157. /// Battlegrounds queue incoming cancel request from client.
  15158. /// 0x8da <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_CANCEL)
  15159. void clif_parse_bg_queue_cancel_request(int fd, map_session_data *sd)
  15160. {
  15161. if (!battle_config.feature_bgqueue)
  15162. return;
  15163. nullpo_retv(sd);
  15164. bool success;
  15165. if (sd->bg_queue_id > 0) {
  15166. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15167. if (queue && queue->state == QUEUE_STATE_SETUP_DELAY)
  15168. 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.
  15169. else
  15170. success = bg_queue_leave(sd);
  15171. } else {
  15172. 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);
  15173. success = false;
  15174. }
  15175. char name[NAME_LENGTH];
  15176. safestrncpy( name, RFIFOCP( fd, 2 ), NAME_LENGTH );
  15177. clif_bg_queue_cancel_result(success, name, sd);
  15178. }
  15179. /// Battleground is ready to be joined, send a window asking for players to accept or decline.
  15180. /// 0x8df <battleground name>.24B <lobby name>.24B (ZC_NOTIFY_LOBBY_ADMISSION)
  15181. void clif_bg_queue_lobby_notify(const char *name, map_session_data *sd)
  15182. {
  15183. nullpo_retv(sd);
  15184. int fd = sd->fd;
  15185. WFIFOHEAD(fd, packet_len(0x8df));
  15186. WFIFOW(fd,0) = 0x8df;
  15187. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15188. safestrncpy(WFIFOCP(fd,2+NAME_LENGTH), name, NAME_LENGTH);
  15189. WFIFOSET(fd, packet_len(0x8df));
  15190. }
  15191. /// Incoming packet from client telling server whether player wants to enter battleground or cancel.
  15192. /// Result types: 1(Accept), 2(Decline).
  15193. /// 0x8e0 <result>.B <battleground name>.24B <lobby name>.24B (CZ_REPLY_LOBBY_ADMISSION)
  15194. void clif_parse_bg_queue_lobby_reply(int fd, map_session_data *sd)
  15195. {
  15196. nullpo_retv(sd);
  15197. if(sd->bg_queue_id > 0) {
  15198. uint8 result = RFIFOB(fd, 2);
  15199. if(result == 1) { // Accept
  15200. bg_queue_on_accept_invite(sd);
  15201. } else if(result == 2) { // Decline
  15202. bg_queue_leave(sd);
  15203. clif_bg_queue_entry_init(sd);
  15204. }
  15205. }
  15206. }
  15207. /// Plays a gong sound, signaling that someone has accepted the invite to enter a battleground.
  15208. /// 0x8e1 <result>.B <battleground name>.24B <lobby name>.24B (ZC_REPLY_ACK_LOBBY_ADMISSION)
  15209. void clif_bg_queue_ack_lobby(bool result, const char *name, const char *lobbyname, map_session_data *sd)
  15210. {
  15211. nullpo_retv(sd);
  15212. int fd = sd->fd;
  15213. WFIFOHEAD(fd, packet_len(0x8e1));
  15214. WFIFOW(fd,0) = 0x8e1;
  15215. WFIFOB(fd,2) = result;
  15216. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15217. safestrncpy(WFIFOCP(fd,3+NAME_LENGTH), lobbyname, NAME_LENGTH);
  15218. WFIFOSET(fd, packet_len(0x8e1));
  15219. }
  15220. /// Battlegrounds queue incoming queue number request from client.
  15221. /// 0x90a <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_RANKING)
  15222. void clif_parse_bg_queue_request_queue_number(int fd, map_session_data *sd)
  15223. {
  15224. nullpo_retv(sd);
  15225. char name[NAME_LENGTH];
  15226. safestrncpy( name, RFIFOCP(fd, 2), NAME_LENGTH );
  15227. clif_bg_queue_apply_notify(name, sd);
  15228. }
  15229. /// Silently removes all the battlegrounds stuff client side so that you will open the first BG window when you press battle on the interface.
  15230. /// Send this when a player joins a battleground so that it will remove all the queue stuff upon warping in.
  15231. /// 0x90e (ZC_ENTRY_QUEUE_INIT)
  15232. void clif_bg_queue_entry_init(map_session_data *sd)
  15233. {
  15234. nullpo_retv(sd);
  15235. PACKET_ZC_ENTRY_QUEUE_INIT p = {};
  15236. p.packetType = HEADER_ZC_ENTRY_QUEUE_INIT;
  15237. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15238. }
  15239. /// Custom Fonts (ZC_NOTIFY_FONT).
  15240. /// 02ef <account_id>.L <font id>.W
  15241. void clif_font(map_session_data *sd)
  15242. {
  15243. #if PACKETVER >= 20080102
  15244. unsigned char buf[8];
  15245. nullpo_retv(sd);
  15246. WBUFW(buf,0) = 0x2ef;
  15247. WBUFL(buf,2) = sd->bl.id;
  15248. WBUFW(buf,6) = sd->status.font;
  15249. clif_send(buf, packet_len(0x2ef), &sd->bl, AREA);
  15250. #endif
  15251. }
  15252. /// Required to start the instancing information window on Client
  15253. /// This window re-appears each "refresh" of client automatically until the keep_limit reaches 0.
  15254. /// S 0x2cb <Instance name>.61B <Standby Position>.W
  15255. void clif_instance_create( int instance_id, size_t num ){
  15256. #if PACKETVER >= 20071128
  15257. map_session_data *sd = nullptr;
  15258. enum send_target target = PARTY;
  15259. unsigned char buf[65];
  15260. instance_getsd(instance_id, sd, &target);
  15261. if (!sd)
  15262. return;
  15263. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15264. if (!db)
  15265. return;
  15266. WBUFW(buf,0) = 0x2cb;
  15267. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15268. WBUFW( buf, 63 ) = static_cast<int16>( num );
  15269. clif_send(buf,packet_len(0x2cb),&sd->bl,target);
  15270. #endif
  15271. }
  15272. /// To announce Instancing queue creation if no maps available
  15273. /// S 0x2cc <Standby Position>.W
  15274. void clif_instance_changewait(int instance_id, int num)
  15275. {
  15276. #if PACKETVER >= 20071128
  15277. map_session_data *sd = nullptr;
  15278. enum send_target target = PARTY;
  15279. unsigned char buf[4];
  15280. instance_getsd(instance_id, sd, &target);
  15281. if (!sd)
  15282. return;
  15283. WBUFW(buf,0) = 0x2cc;
  15284. WBUFW(buf,2) = num;
  15285. clif_send(buf,packet_len(0x2cc),&sd->bl,target);
  15286. #endif
  15287. return;
  15288. }
  15289. /// Notify the current status to members
  15290. /// S 0x2cd <Instance Name>.61B <Instance Remaining Time>.L <Instance Noplayers close time>.L
  15291. void clif_instance_status(int instance_id, unsigned int limit1, unsigned int limit2)
  15292. {
  15293. #if PACKETVER >= 20071128
  15294. map_session_data *sd = nullptr;
  15295. enum send_target target = PARTY;
  15296. unsigned char buf[71];
  15297. instance_getsd(instance_id, sd, &target);
  15298. if (!sd)
  15299. return;
  15300. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15301. if (!db)
  15302. return;
  15303. WBUFW(buf,0) = 0x2cd;
  15304. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15305. WBUFL(buf,63) = limit1;
  15306. WBUFL(buf,67) = limit2;
  15307. clif_send(buf,packet_len(0x2cd),&sd->bl,target);
  15308. #endif
  15309. return;
  15310. }
  15311. /// Notify a status change to members
  15312. /// S 0x2ce <Message ID>.L
  15313. /// 0 = Notification (EnterLimitDate update?)
  15314. /// 1 = The Memorial Dungeon expired; it has been destroyed
  15315. /// 2 = The Memorial Dungeon's entry time limit expired; it has been destroyed
  15316. /// 3 = The Memorial Dungeon has been removed.
  15317. /// 4 = Create failure (removes the instance window)
  15318. void clif_instance_changestatus(int instance_id, e_instance_notify type, unsigned int limit)
  15319. {
  15320. #if PACKETVER >= 20071128
  15321. map_session_data *sd = nullptr;
  15322. enum send_target target = PARTY;
  15323. unsigned char buf[10];
  15324. instance_getsd(instance_id, sd, &target);
  15325. if (!sd)
  15326. return;
  15327. WBUFW(buf,0) = 0x2ce;
  15328. WBUFL(buf,2) = type;
  15329. WBUFL(buf,6) = limit;
  15330. clif_send(buf,packet_len(0x2ce),&sd->bl,target);
  15331. #endif
  15332. return;
  15333. }
  15334. /// Destroy an instance from the status window
  15335. /// 02cf <command>.L (CZ_MEMORIALDUNGEON_COMMAND)
  15336. void clif_parse_MemorialDungeonCommand(int fd, map_session_data *sd)
  15337. {
  15338. if (pc_istrading(sd) || pc_isdead(sd) || map_getmapdata(sd->bl.m)->instance_id > 0)
  15339. return;
  15340. const PACKET_CZ_MEMORIALDUNGEON_COMMAND* p = reinterpret_cast<PACKET_CZ_MEMORIALDUNGEON_COMMAND*>( RFIFOP( fd, 0 ) );
  15341. switch (p->command) {
  15342. case COMMAND_MEMORIALDUNGEON_DESTROY_FORCE:
  15343. instance_destroy_command(sd);
  15344. break;
  15345. }
  15346. }
  15347. void clif_instance_info( map_session_data& sd ){
  15348. if( sd.instance_id > 0 ){
  15349. instance_reqinfo( &sd, sd.instance_id );
  15350. }
  15351. if( sd.status.party_id > 0 ){
  15352. struct party_data* p = party_search( sd.status.party_id );
  15353. if( p != nullptr && p->instance_id > 0 ){
  15354. instance_reqinfo( &sd, p->instance_id );
  15355. }
  15356. }
  15357. if( sd.guild != nullptr && sd.guild->instance_id > 0 ){
  15358. instance_reqinfo( &sd, sd.guild->instance_id );
  15359. }
  15360. if( sd.clan != nullptr && sd.clan->instance_id > 0 ){
  15361. instance_reqinfo( &sd, sd.clan->instance_id );
  15362. }
  15363. }
  15364. /// Notifies clients about item picked up by a party member.
  15365. /// 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)
  15366. void clif_party_show_picker( map_session_data* sd, struct item* item_data ){
  15367. #if PACKETVER >= 20071002
  15368. nullpo_retv( sd );
  15369. nullpo_retv( item_data );
  15370. struct item_data* id = itemdb_search( item_data->nameid );
  15371. PACKET_ZC_ITEM_PICKUP_PARTY p = {};
  15372. p.packetType = HEADER_ZC_ITEM_PICKUP_PARTY;
  15373. p.AID = sd->status.account_id;
  15374. p.itemId = client_nameid( item_data->nameid );
  15375. p.identified = item_data->identify;
  15376. p.damaged = item_data->attribute;
  15377. p.refine = item_data->refine;
  15378. clif_addcards( &p.slot, item_data );
  15379. p.location = id->equip; // equip location
  15380. p.itemType = itemtype( id->nameid ); // item type
  15381. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15382. p.grade = item_data->enchantgrade;
  15383. #endif
  15384. clif_send( &p, sizeof( p ), &sd->bl, PARTY_SAMEMAP_WOS );
  15385. #endif
  15386. }
  15387. /** Display gained exp.
  15388. * 07f6 <account id>.L <amount>.L <var id>.W <exp type>.W (ZC_NOTIFY_EXP)
  15389. * 0acc <account id>.L <amount>.Q <var id>.W <exp type>.W (ZC_NOTIFY_EXP2)
  15390. * amount: INT32_MIN ~ INT32_MAX
  15391. * var id:
  15392. * SP_BASEEXP, SP_JOBEXP
  15393. * exp type:
  15394. * 0 = normal exp gained/lost
  15395. * 1 = quest exp gained/lost
  15396. * @param sd Player
  15397. * @param exp EXP value gained/loss
  15398. * @param type SP_BASEEXP, SP_JOBEXP
  15399. * @param quest False:Normal EXP; True:Quest EXP (displayed in purple color)
  15400. * @param lost True:if losing EXP
  15401. */
  15402. void clif_displayexp(map_session_data *sd, t_exp exp, char type, bool quest, bool lost)
  15403. {
  15404. int fd;
  15405. int offset;
  15406. #if PACKETVER >= 20170830
  15407. int cmd = 0xacc;
  15408. #else
  15409. int cmd = 0x7f6;
  15410. #endif
  15411. nullpo_retv(sd);
  15412. fd = sd->fd;
  15413. WFIFOHEAD(fd, packet_len(cmd));
  15414. WFIFOW(fd,0) = cmd;
  15415. WFIFOL(fd,2) = sd->bl.id;
  15416. #if PACKETVER >= 20170830
  15417. WFIFOQ(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15418. offset = 4;
  15419. #else
  15420. WFIFOL(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15421. offset = 0;
  15422. #endif
  15423. WFIFOW(fd,10+offset) = type;
  15424. WFIFOW(fd,12+offset) = (quest && type != SP_JOBEXP) ? 1 : 0; // NOTE: Somehow JobEXP always in yellow color
  15425. WFIFOSET(fd,packet_len(cmd));
  15426. }
  15427. /// Displays digital clock digits on top of the screen (ZC_SHOWDIGIT).
  15428. /// type:
  15429. /// 0 = Displays 'value' for 5 seconds.
  15430. /// 1 = Incremental counter (1 tick/second), negated 'value' specifies start value (e.g. using -10 lets the counter start at 10).
  15431. /// 2 = Decremental counter (1 tick/second), negated 'value' specifies start value (does not stop when reaching 0, but overflows).
  15432. /// 3 = Decremental counter (1 tick/second), 'value' specifies start value (stops when reaching 0, displays at most 2 digits).
  15433. /// value:
  15434. /// 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).
  15435. void clif_showdigit(map_session_data* sd, unsigned char type, int value)
  15436. {
  15437. WFIFOHEAD(sd->fd, packet_len(0x1b1));
  15438. WFIFOW(sd->fd,0) = 0x1b1;
  15439. WFIFOB(sd->fd,2) = type;
  15440. WFIFOL(sd->fd,3) = value;
  15441. WFIFOSET(sd->fd, packet_len(0x1b1));
  15442. }
  15443. /// Notification of the state of client command /effect (CZ_LESSEFFECT).
  15444. /// 021d <state>.L
  15445. /// state:
  15446. /// 0 = Full effects
  15447. /// 1 = Reduced effects
  15448. ///
  15449. /// NOTE: The state is used on Aegis for sending skill unit packet
  15450. /// 0x11f (ZC_SKILL_ENTRY) instead of 0x1c9 (ZC_SKILL_ENTRY2)
  15451. /// whenever possible. Due to the way the decision check is
  15452. /// constructed, this state tracking was rendered useless,
  15453. /// as the only skill unit, that is sent with 0x1c9 is
  15454. /// Graffiti.
  15455. void clif_parse_LessEffect(int fd, map_session_data* sd){
  15456. int isLess = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  15457. sd->state.lesseffect = ( isLess != 0 );
  15458. }
  15459. /// 07e4 <length>.w <option>.l <val>.l {<index>.w <amount>.w).4b* (CZ_ITEMLISTWIN_RES)
  15460. void clif_parse_ItemListWindowSelected(int fd, map_session_data* sd) {
  15461. if( sd == nullptr ){
  15462. return;
  15463. }
  15464. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  15465. int n = (RFIFOW(fd,info->pos[0])-12) / 4;
  15466. int type = RFIFOL(fd,info->pos[1]);
  15467. int flag = RFIFOL(fd,info->pos[2]); // Button clicked: 0 = Cancel, 1 = OK
  15468. unsigned short* item_list = (unsigned short*)RFIFOP(fd,info->pos[3]);
  15469. if( sd->state.trading || sd->npc_shopid )
  15470. return;
  15471. if( flag == 0 || n == 0) {
  15472. clif_menuskill_clear(sd);
  15473. return; // Canceled by player.
  15474. }
  15475. if( sd->menuskill_id != SO_EL_ANALYSIS && sd->menuskill_id != GN_CHANGEMATERIAL ) {
  15476. clif_menuskill_clear(sd);
  15477. return; // Prevent hacking.
  15478. }
  15479. switch( type ) {
  15480. case 0: // Change Material
  15481. skill_changematerial(sd,n,item_list);
  15482. break;
  15483. case 1: // Level 1: Pure to Rough
  15484. case 2: // Level 2: Rough to Pure
  15485. skill_elementalanalysis(*sd,n,type,item_list);
  15486. break;
  15487. }
  15488. clif_menuskill_clear(sd);
  15489. return;
  15490. }
  15491. /*==========================================
  15492. * Elemental System
  15493. *==========================================*/
  15494. /// Notifies client of a change in an elemental's status parameter.
  15495. /// 0x81e <type>.W <value>.L (ZC_EL_PAR_CHANGE)
  15496. void clif_elemental_updatestatus(map_session_data& sd, _sp type) {
  15497. #if PACKETVER >= 20100309
  15498. if (sd.ed == nullptr)
  15499. return;
  15500. PACKET_ZC_EL_PAR_CHANGE p = {};
  15501. p.packetType = HEADER_ZC_EL_PAR_CHANGE;
  15502. p.type = static_cast<decltype(p.type)>(type);
  15503. status_data* status = &sd.ed->battle_status;
  15504. switch( type ) {
  15505. case SP_HP:
  15506. p.value = static_cast<decltype(p.value)>(status->hp);
  15507. break;
  15508. case SP_MAXHP:
  15509. p.value = static_cast<decltype(p.value)>(status->max_hp);
  15510. break;
  15511. case SP_SP:
  15512. p.value = static_cast<decltype(p.value)>(status->sp);
  15513. break;
  15514. case SP_MAXSP:
  15515. p.value = static_cast<decltype(p.value)>(status->max_sp);
  15516. break;
  15517. }
  15518. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  15519. #endif
  15520. }
  15521. void clif_elemental_info(map_session_data *sd) {
  15522. int fd;
  15523. s_elemental_data *ed;
  15524. struct status_data *status;
  15525. if( !clif_session_isValid(sd) || (ed = sd->ed) == nullptr )
  15526. return;
  15527. fd = sd->fd;
  15528. status = &ed->battle_status;
  15529. WFIFOHEAD(fd,22);
  15530. WFIFOW(fd, 0) = 0x81d;
  15531. WFIFOL(fd, 2) = ed->bl.id;
  15532. WFIFOL(fd, 6) = status->hp;
  15533. WFIFOL(fd,10) = status->max_hp;
  15534. WFIFOL(fd,14) = status->sp;
  15535. WFIFOL(fd,18) = status->max_sp;
  15536. WFIFOSET(fd,22);
  15537. }
  15538. /// Buying Store System
  15539. ///
  15540. /// Opens preparation window for buying store (ZC_OPEN_BUYING_STORE).
  15541. /// 0810 <slots>.B
  15542. void clif_buyingstore_open(map_session_data* sd)
  15543. {
  15544. int fd = sd->fd;
  15545. WFIFOHEAD(fd,packet_len(0x810));
  15546. WFIFOW(fd,0) = 0x810;
  15547. WFIFOB(fd,2) = sd->buyingstore.slots;
  15548. WFIFOSET(fd,packet_len(0x810));
  15549. }
  15550. /// Request to create a buying store.
  15551. /// 0811 <packet len>.W <limit zeny>.L <result>.B <store name>.80B { <name id>.W <amount>.W <price>.L }* (CZ_REQ_OPEN_BUYING_STORE)
  15552. /// result:
  15553. /// 0 = cancel
  15554. /// 1 = open
  15555. static void clif_parse_ReqOpenBuyingStore( int fd, map_session_data* sd ){
  15556. const PACKET_CZ_REQ_OPEN_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15557. // TODO: Make this check global for all variable length packets.
  15558. // minimum packet length
  15559. if( p->packetLength < sizeof( *p ) ){
  15560. ShowError( "clif_parse_ReqOpenBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15561. return;
  15562. }
  15563. if (map_getmapflag(sd->bl.m, MF_NOBUYINGSTORE)) {
  15564. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  15565. return;
  15566. }
  15567. if (map_getcell(sd->bl.m, sd->bl.x, sd->bl.y, CELL_CHKNOBUYINGSTORE)) {
  15568. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  15569. return;
  15570. }
  15571. char storename[MESSAGE_SIZE];
  15572. safestrncpy( storename, p->storeName, sizeof( storename ) );
  15573. // so that buyingstore_create knows, how many elements it has access to
  15574. int packet_len = p->packetLength - sizeof( *p );
  15575. if( packet_len % sizeof( p->items[0] ) ){
  15576. 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] ) );
  15577. return;
  15578. }
  15579. buyingstore_create( sd, p->zenyLimit, p->result, storename, p->items, packet_len / sizeof( p->items[0] ), nullptr );
  15580. }
  15581. /// Notification, that the requested buying store could not be created (ZC_FAILED_OPEN_BUYING_STORE_TO_BUYER).
  15582. /// 0812 <result>.W <total weight>.L
  15583. /// result:
  15584. /// 1 = "Failed to open buying store." (0x6cd, MSI_BUYINGSTORE_OPEN_FAILED)
  15585. /// 2 = "Total amount of then possessed items exceeds the weight limit by <weight/10-maxweight*90%>. Please re-enter." (0x6ce, MSI_BUYINGSTORE_OVERWEIGHT)
  15586. /// 8 = "No sale (purchase) information available." (0x705)
  15587. /// ? = nothing
  15588. void clif_buyingstore_open_failed(map_session_data* sd, unsigned short result, unsigned int weight)
  15589. {
  15590. int fd = sd->fd;
  15591. WFIFOHEAD(fd,packet_len(0x812));
  15592. WFIFOW(fd,0) = 0x812;
  15593. WFIFOW(fd,2) = result;
  15594. WFIFOL(fd,4) = weight;
  15595. WFIFOSET(fd,packet_len(0x812));
  15596. }
  15597. /// Notification, that the requested buying store was created.
  15598. /// 0813 <packet len>.W <account id>.L <limit zeny>.L { <price>.L <count>.W <type>.B <name id>.W }* (ZC_MYITEMLIST_BUYING_STORE)
  15599. void clif_buyingstore_myitemlist( map_session_data& sd ){
  15600. #if PACKETVER >= 20100309
  15601. PACKET_ZC_MYITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_MYITEMLIST_BUYING_STORE*>( packet_buffer );
  15602. p->packetType = HEADER_ZC_MYITEMLIST_BUYING_STORE;
  15603. p->packetLength = sizeof( *p );
  15604. p->AID = sd.bl.id;
  15605. p->zenyLimit = sd.buyingstore.zenylimit;
  15606. for( int i = 0; i < sd.buyingstore.slots; i++ ){
  15607. PACKET_ZC_MYITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15608. entry.price = sd.buyingstore.items[i].price;
  15609. entry.amount = sd.buyingstore.items[i].amount;
  15610. entry.itemType = itemtype( sd.buyingstore.items[i].nameid );
  15611. entry.itemId = client_nameid( sd.buyingstore.items[i].nameid );
  15612. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15613. }
  15614. clif_send( p, p->packetLength, &sd.bl, SELF );
  15615. #endif
  15616. }
  15617. /// Notifies clients in area of a buying store (ZC_BUYING_STORE_ENTRY).
  15618. /// 0814 <account id>.L <store name>.80B
  15619. void clif_buyingstore_entry( map_session_data& sd, struct block_list* tbl ){
  15620. #if PACKETVER >= 20100420
  15621. enum send_target target;
  15622. if( tbl == nullptr ){
  15623. tbl = &sd.bl;
  15624. target = AREA_WOS;
  15625. }else{
  15626. target = SELF;
  15627. }
  15628. PACKET_ZC_BUYING_STORE_ENTRY p = {};
  15629. p.packetType = HEADER_ZC_BUYING_STORE_ENTRY;
  15630. p.makerAID = sd.status.account_id;
  15631. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  15632. clif_send( &p, sizeof( p ), tbl, target );
  15633. #endif
  15634. }
  15635. /// Request to close own buying store (CZ_REQ_CLOSE_BUYING_STORE).
  15636. /// 0815
  15637. static void clif_parse_ReqCloseBuyingStore(int fd, map_session_data* sd)
  15638. {
  15639. buyingstore_close(sd);
  15640. }
  15641. /// Notifies clients in area that a buying store was closed (ZC_DISAPPEAR_BUYING_STORE_ENTRY).
  15642. /// 0816 <account id>.L
  15643. void clif_buyingstore_disappear_entry( map_session_data& sd, struct block_list* tbl ){
  15644. #if PACKETVER >= 20100309
  15645. enum send_target target;
  15646. if( tbl == nullptr ){
  15647. tbl = &sd.bl;
  15648. target = AREA_WOS;
  15649. }else{
  15650. target = SELF;
  15651. }
  15652. PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY p = {};
  15653. p.packetType = HEADER_ZC_DISAPPEAR_BUYING_STORE_ENTRY;
  15654. p.makerAID = sd.status.account_id;
  15655. clif_send( &p, sizeof( p ), tbl, target );
  15656. #endif
  15657. }
  15658. /// Request to open someone else's buying store (CZ_REQ_CLICK_TO_BUYING_STORE).
  15659. /// 0817 <account id>.L
  15660. static void clif_parse_ReqClickBuyingStore(int fd, map_session_data* sd)
  15661. {
  15662. uint32 account_id;
  15663. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  15664. buyingstore_open(sd, account_id);
  15665. }
  15666. /// Sends buying store item list.
  15667. /// 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)
  15668. void clif_buyingstore_itemlist( map_session_data& sd, map_session_data& pl_sd ){
  15669. #if PACKETVER >= 20100309
  15670. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_ACK_ITEMLIST_BUYING_STORE*>( packet_buffer );
  15671. p->packetType = HEADER_ZC_ACK_ITEMLIST_BUYING_STORE;
  15672. p->packetLength = sizeof( *p );
  15673. p->AID = pl_sd.bl.id;
  15674. p->storeId = pl_sd.buyer_id;
  15675. p->zenyLimit = pl_sd.buyingstore.zenylimit;
  15676. for( int i = 0; i < pl_sd.buyingstore.slots; i++ ){
  15677. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15678. entry.price = pl_sd.buyingstore.items[i].price;
  15679. entry.amount = pl_sd.buyingstore.items[i].amount; // TODO: Figure out, if no longer needed items (amount == 0) are listed on official.
  15680. entry.itemType = itemtype(pl_sd.buyingstore.items[i].nameid);
  15681. entry.itemId = client_nameid( pl_sd.buyingstore.items[i].nameid );
  15682. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15683. }
  15684. clif_send( p, p->packetLength, &sd.bl, SELF );
  15685. #endif
  15686. }
  15687. /// Request to sell items to a buying store.
  15688. /// 0819 <packet len>.W <account id>.L <store id>.L { <index>.W <name id>.W <amount>.W }* (CZ_REQ_TRADE_BUYING_STORE)
  15689. static void clif_parse_ReqTradeBuyingStore( int fd, map_session_data* sd ){
  15690. const PACKET_CZ_REQ_TRADE_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_TRADE_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15691. // minimum packet length
  15692. if( p->packetLength < sizeof( *p ) ){
  15693. ShowError( "clif_parse_ReqTradeBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15694. return;
  15695. }
  15696. // so that buyingstore_trade knows, how many elements it has access to
  15697. int packet_len = p->packetLength - sizeof( *p );
  15698. if( packet_len % sizeof( p->items[0] ) ){
  15699. 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] ) );
  15700. return;
  15701. }
  15702. buyingstore_trade( sd, p->AID, p->storeId, p->items, packet_len / sizeof( p->items[0] ) );
  15703. }
  15704. /// Notifies the buyer, that the buying store has been closed due to a post-trade condition (ZC_FAILED_TRADE_BUYING_STORE_TO_BUYER).
  15705. /// 081a <result>.W
  15706. /// result:
  15707. /// 3 = "All items within the buy limit were purchased." (0x6cf, MSI_BUYINGSTORE_TRADE_OVERLIMITZENY)
  15708. /// 4 = "All items were purchased." (0x6d0, MSI_BUYINGSTORE_TRADE_BUYCOMPLETE)
  15709. /// ? = nothing
  15710. void clif_buyingstore_trade_failed_buyer(map_session_data* sd, short result)
  15711. {
  15712. int fd = sd->fd;
  15713. WFIFOHEAD(fd,packet_len(0x81a));
  15714. WFIFOW(fd,0) = 0x81a;
  15715. WFIFOW(fd,2) = result;
  15716. WFIFOSET(fd,packet_len(0x81a));
  15717. }
  15718. /// Updates the zeny limit and an item in the buying store item list.
  15719. /// 081b <name id>.W <amount>.W <limit zeny>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE)
  15720. /// 09e6 <name id>.W <amount>.W <zeny>.L <limit zeny>.L <GID>.L <Date>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE2)
  15721. void clif_buyingstore_update_item( map_session_data* sd, t_itemid nameid, unsigned short amount, uint32 char_id, int zeny ){
  15722. PACKET_ZC_UPDATE_ITEM_FROM_BUYING_STORE p = {};
  15723. p.packetType = buyingStoreUpdateItemType;
  15724. p.itemId = client_nameid( nameid );
  15725. p.amount = amount;
  15726. p.zenyLimit = sd->buyingstore.zenylimit;
  15727. #if PACKETVER >= 20141016
  15728. p.zeny = zeny;
  15729. p.charId = char_id; // GID
  15730. p.updateTime = (int)time(nullptr);
  15731. #endif
  15732. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15733. }
  15734. /// Deletes item from inventory, that was sold to a buying store (ZC_ITEM_DELETE_BUYING_STORE).
  15735. /// 081c <index>.W <amount>.W <price>.L
  15736. /// message:
  15737. /// "%s (%d) were sold at %dz." (0x6d2, MSI_BUYINGSTORE_TRADE_SELLCOMPLETE)
  15738. ///
  15739. /// NOTE: This function has to be called _instead_ of clif_delitem/clif_dropitem.
  15740. void clif_buyingstore_delete_item(map_session_data* sd, short index, unsigned short amount, int price)
  15741. {
  15742. int fd = sd->fd;
  15743. WFIFOHEAD(fd,packet_len(0x81c));
  15744. WFIFOW(fd,0) = 0x81c;
  15745. WFIFOW(fd,2) = client_index( index );
  15746. WFIFOW(fd,4) = amount;
  15747. WFIFOL(fd,6) = price; // price per item, client calculates total Zeny by itself
  15748. WFIFOSET(fd,packet_len(0x81c));
  15749. }
  15750. /// Notifies the seller, that a buying store trade failed.
  15751. /// 0824 <result>.W <name id>.W (ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER)
  15752. /// result:
  15753. /// 5 = "The deal has failed." (0x39, MSI_DEAL_FAIL)
  15754. /// 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)
  15755. /// 7 = "The trade failed, because the buyer is lacking required balance." (0x6d1, MSI_BUYINGSTORE_TRADE_LACKBUYERZENY)
  15756. /// ? = nothing
  15757. void clif_buyingstore_trade_failed_seller( map_session_data* sd, short result, t_itemid nameid ){
  15758. PACKET_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER p = {};
  15759. p.packetType = HEADER_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER;
  15760. p.result = result;
  15761. p.itemId = client_nameid( nameid );
  15762. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15763. }
  15764. /// Search Store Info System
  15765. ///
  15766. /// Request to search for stores.
  15767. /// 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)
  15768. /// type:
  15769. /// 0 = Vending
  15770. /// 1 = Buying Store
  15771. ///
  15772. /// NOTE: The client determines the item ids by specifying a name and optionally,
  15773. /// amount of card slots. If the client does not know about the item it
  15774. /// cannot be searched.
  15775. static void clif_parse_SearchStoreInfo( int fd, map_session_data *sd ){
  15776. if (!battle_config.feature_search_stores)
  15777. return;
  15778. const PACKET_CZ_SEARCH_STORE_INFO* p = reinterpret_cast<PACKET_CZ_SEARCH_STORE_INFO*>( RFIFOP( fd, 0 ) );
  15779. // minimum packet length
  15780. if( p->packetLength < sizeof( *p ) ){
  15781. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15782. return;
  15783. }
  15784. // check, if there is enough data for the claimed count of items
  15785. int packet_len = p->packetLength - sizeof( *p );
  15786. if( packet_len % sizeof( p->items[0] ) ){
  15787. 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] ) );
  15788. return;
  15789. }
  15790. int count = packet_len / sizeof( p->items[0] );
  15791. if( count < p->itemsCount + p->cardsCount ){
  15792. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected count=%u, count=%u, account_id=%d).\n", p->itemsCount + p->cardsCount, count, sd->bl.id );
  15793. return;
  15794. }
  15795. if ( p->searchType > SEARCHTYPE_BUYING_STORE ) {
  15796. ShowError( "clif_parse_SearchStoreInfo: Invalid search type %u (account_id=%d).\n", p->searchType, sd->bl.id );
  15797. return;
  15798. }
  15799. if ( p->minPrice > battle_config.vending_max_value ) {
  15800. ShowError( "clif_parse_SearchStoreInfo: Invalid min price %u (account_id=%d).\n", p->minPrice, sd->bl.id );
  15801. return;
  15802. }
  15803. if ( p->maxPrice > battle_config.vending_max_value ) {
  15804. ShowError( "clif_parse_SearchStoreInfo: Invalid max price %u (account_id=%d).\n", p->maxPrice, sd->bl.id );
  15805. return;
  15806. }
  15807. 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 );
  15808. }
  15809. /// Results for a store search request.
  15810. /// 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)
  15811. /// is first page:
  15812. /// 0 = appends to existing results
  15813. /// 1 = clears previous results before displaying this result set
  15814. /// is next page:
  15815. /// 0 = no "next" label
  15816. /// 1 = "next" label to retrieve more results
  15817. void clif_search_store_info_ack( map_session_data& sd ){
  15818. #if PACKETVER_MAIN_NUM >= 20100817 || PACKETVER_RE_NUM >= 20100706 || defined(PACKETVER_ZERO)
  15819. if (!battle_config.feature_search_stores)
  15820. return;
  15821. uint32 start = sd.searchstore.pages * SEARCHSTORE_RESULTS_PER_PAGE ;
  15822. uint32 end = umin( static_cast<uint32>( sd.searchstore.items.size() ), start + SEARCHSTORE_RESULTS_PER_PAGE );
  15823. PACKET_ZC_SEARCH_STORE_INFO_ACK* p = reinterpret_cast<PACKET_ZC_SEARCH_STORE_INFO_ACK*>( packet_buffer );
  15824. p->packetType = HEADER_ZC_SEARCH_STORE_INFO_ACK;
  15825. p->packetLength = sizeof( *p );
  15826. p->firstPage = !sd.searchstore.pages;
  15827. p->nextPage = searchstore_querynext( sd );
  15828. p->usesCount = static_cast<decltype(p->usesCount)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(p->usesCount)>::max() ) );
  15829. for( int i = 0, count = 0; i < end - start; i++ ){
  15830. std::shared_ptr<s_search_store_info_item> ssitem = sd.searchstore.items[start + i];
  15831. if( ssitem == nullptr ){
  15832. continue;
  15833. }
  15834. PACKET_ZC_SEARCH_STORE_INFO_ACK_sub& entry = p->items[count];
  15835. entry.storeId = ssitem->store_id;
  15836. entry.AID = ssitem->account_id;
  15837. safestrncpy( entry.shopName, ssitem->store_name, MESSAGE_SIZE );
  15838. entry.itemId = client_nameid( ssitem->nameid );
  15839. entry.itemType = itemtype( ssitem->nameid );
  15840. entry.price = ssitem->price;
  15841. entry.amount = ssitem->amount;
  15842. entry.refine = ssitem->refine;
  15843. // make-up an item for clif_addcards
  15844. struct item it = {};
  15845. for( int j = 0; j < MAX_SLOTS; j++ ){
  15846. it.card[j] = ssitem->card[j];
  15847. }
  15848. it.nameid = ssitem->nameid;
  15849. it.amount = ssitem->amount;
  15850. clif_addcards( &entry.slot, &it );
  15851. #if PACKETVER >= 20150226
  15852. clif_add_random_options( entry.option_data, it );
  15853. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15854. entry.grade = ssitem->enchantgrade;
  15855. #endif
  15856. #endif
  15857. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15858. count++;
  15859. }
  15860. clif_send( p, p->packetLength, &sd.bl, SELF );
  15861. #endif
  15862. }
  15863. /// Notification of failure when searching for stores.
  15864. /// 0837 <reason>.B (ZC_SEARCH_STORE_INFO_FAILED)
  15865. void clif_search_store_info_failed(map_session_data& sd, e_searchstore_failure reason){
  15866. #if PACKETVER_MAIN_NUM >= 20100601 || PACKETVER_RE_NUM >= 20100601 || defined(PACKETVER_ZERO)
  15867. if (!battle_config.feature_search_stores)
  15868. return;
  15869. PACKET_ZC_SEARCH_STORE_INFO_FAILED packet{};
  15870. packet.packetType = HEADER_ZC_SEARCH_STORE_INFO_FAILED;
  15871. packet.reason = static_cast<decltype(packet.reason)>(reason);
  15872. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15873. #endif
  15874. }
  15875. /// Request to display next page of results.
  15876. /// 0838 (CZ_SEARCH_STORE_INFO_NEXT_PAGE)
  15877. static void clif_parse_SearchStoreInfoNextPage(int fd, map_session_data* sd){
  15878. if (!battle_config.feature_search_stores)
  15879. return;
  15880. searchstore_next(*sd);
  15881. }
  15882. /// Opens the search store window.
  15883. /// 083a <effect>.W <remaining uses>.B (ZC_OPEN_SEARCH_STORE_INFO)
  15884. void clif_open_search_store_info(map_session_data& sd){
  15885. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15886. if (!battle_config.feature_search_stores)
  15887. return;
  15888. PACKET_ZC_OPEN_SEARCH_STORE_INFO packet{};
  15889. packet.packetType = HEADER_ZC_OPEN_SEARCH_STORE_INFO;
  15890. packet.effect = static_cast<decltype(packet.effect)>(sd.searchstore.effect);
  15891. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15892. packet.remainingUses = static_cast<decltype(packet.remainingUses)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(packet.remainingUses)>::max() ) );
  15893. #endif
  15894. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15895. #endif
  15896. }
  15897. /// Request to close the store search window.
  15898. /// 083b (CZ_CLOSE_SEARCH_STORE_INFO)
  15899. static void clif_parse_CloseSearchStoreInfo(int fd, map_session_data* sd){
  15900. if (!battle_config.feature_search_stores)
  15901. return;
  15902. searchstore_close(*sd);
  15903. }
  15904. /// Request to invoke catalog effect on a store from search results.
  15905. /// 083c <account id>.L <store id>.L <nameid>.W (CZ_SSILIST_ITEM_CLICK)
  15906. static void clif_parse_SearchStoreInfoListItemClick( int fd, map_session_data* sd ){
  15907. if (!battle_config.feature_search_stores)
  15908. return;
  15909. const PACKET_CZ_SSILIST_ITEM_CLICK* p = reinterpret_cast<PACKET_CZ_SSILIST_ITEM_CLICK*>( RFIFOP( fd, 0 ) );
  15910. searchstore_click( *sd, p->AID, p->storeId, p->itemId );
  15911. }
  15912. /// Notification of the store position on current map.
  15913. /// 083d <xPos>.W <yPos>.W (ZC_SSILIST_ITEM_CLICK_ACK)
  15914. void clif_search_store_info_click_ack(map_session_data& sd, int16 x, int16 y){
  15915. #if PACKETVER_MAIN_NUM >= 20100608 || PACKETVER_RE_NUM >= 20100608 || defined(PACKETVER_ZERO)
  15916. if (!battle_config.feature_search_stores)
  15917. return;
  15918. PACKET_ZC_SSILIST_ITEM_CLICK_ACK packet{};
  15919. packet.packetType = HEADER_ZC_SSILIST_ITEM_CLICK_ACK;
  15920. packet.x = x;
  15921. packet.y = y;
  15922. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15923. #endif
  15924. }
  15925. /// Parse function for packet debugging.
  15926. void clif_parse_debug(int fd,map_session_data *sd)
  15927. {
  15928. int16 packet_len;
  15929. // clif_parse ensures, that there is at least 2 bytes of data
  15930. uint16 cmd = RFIFOW(fd,0);
  15931. if( sd ) {
  15932. packet_len = packet_db[cmd].len;
  15933. if( packet_len == 0 )
  15934. {// unknown
  15935. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  15936. }
  15937. else if( packet_len == -1 )
  15938. {// variable length
  15939. packet_len = RFIFOW(fd,2); // clif_parse ensures, that this amount of data is already received
  15940. }
  15941. 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);
  15942. }
  15943. else
  15944. {
  15945. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  15946. ShowDebug("Packet debug of 0x%04X (length %d), session #%d\n", cmd, packet_len, fd);
  15947. }
  15948. ShowDump(RFIFOP(fd,0), packet_len);
  15949. }
  15950. /*==========================================
  15951. * Server tells client to display a window similar to Magnifier (item) one
  15952. * Server populates the window with avilable elemental converter options according to player's inventory
  15953. *------------------------------------------*/
  15954. void clif_elementalconverter_list( map_session_data& sd ){
  15955. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  15956. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  15957. p->packetLength = sizeof( *p );
  15958. int count = 0;
  15959. for( int i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  15960. if( skill_can_produce_mix( &sd, skill_produce_db[i].nameid, 23, 1 ) ){
  15961. p->items[count].itemId = client_nameid( skill_produce_db[i].nameid );
  15962. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  15963. count++;
  15964. }
  15965. }
  15966. clif_send( p, p->packetLength, &sd.bl, SELF );
  15967. if( count > 0 ){
  15968. sd.menuskill_id = SA_CREATECON;
  15969. sd.menuskill_val = count;
  15970. }
  15971. }
  15972. /**
  15973. * Rune Knight
  15974. **/
  15975. void clif_millenniumshield( block_list& bl, int16 shields ){
  15976. #if PACKETVER >= 20081126
  15977. PACKET_ZC_MILLENNIUMSHIELD packet{};
  15978. packet.packetType = HEADER_ZC_MILLENNIUMSHIELD;
  15979. packet.aid = bl.id;
  15980. packet.num = shields;
  15981. packet.state = 0;
  15982. clif_send( &packet, sizeof( packet ), &bl, AREA );
  15983. #endif
  15984. }
  15985. /**
  15986. * Mechanic
  15987. **/
  15988. /*==========================================
  15989. * Magic Decoy Material List
  15990. *------------------------------------------*/
  15991. void clif_magicdecoy_list( map_session_data& sd, uint16 skill_lv, short x, short y ){
  15992. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  15993. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  15994. p->packetLength = sizeof( *p );
  15995. size_t count = 0;
  15996. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  15997. if( itemdb_group.item_exists( IG_ELEMENT, sd.inventory.u.items_inventory[i].nameid ) ){
  15998. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  15999. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16000. count++;
  16001. }
  16002. }
  16003. if( count > 0 ){
  16004. clif_send( p, p->packetLength, &sd.bl, SELF );
  16005. sd.menuskill_id = NC_MAGICDECOY;
  16006. sd.menuskill_val = skill_lv;
  16007. sd.sc.comet_x = x;
  16008. sd.sc.comet_y = y;
  16009. }else{
  16010. clif_skill_fail( sd, NC_MAGICDECOY );
  16011. }
  16012. }
  16013. /*==========================================
  16014. * Guillotine Cross Poisons List
  16015. *------------------------------------------*/
  16016. void clif_poison_list( map_session_data& sd, uint16 skill_lv ){
  16017. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16018. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16019. p->packetLength = sizeof( *p );
  16020. size_t count = 0;
  16021. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  16022. if( itemdb_group.item_exists( IG_POISON, sd.inventory.u.items_inventory[i].nameid ) ){
  16023. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  16024. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16025. count++;
  16026. }
  16027. }
  16028. if( count > 0 ){
  16029. clif_send( p, p->packetLength, &sd.bl, SELF );
  16030. sd.menuskill_id = GC_POISONINGWEAPON;
  16031. sd.menuskill_val = skill_lv;
  16032. }else{
  16033. clif_skill_fail( sd, GC_POISONINGWEAPON, USESKILL_FAIL_GUILLONTINE_POISON );
  16034. }
  16035. }
  16036. /// 0442 <Length>.W <why>.L { <Skill_Id>.W }* (ZC_SKILL_SELECT_REQUEST).
  16037. void clif_autoshadowspell_list( map_session_data& sd ){
  16038. #if PACKETVER >= 20081210
  16039. if( sd.menuskill_id == SC_AUTOSHADOWSPELL )
  16040. return;
  16041. PACKET_ZC_SKILL_SELECT_REQUEST* p = reinterpret_cast<PACKET_ZC_SKILL_SELECT_REQUEST*>( packet_buffer );
  16042. p->packetType = HEADER_ZC_SKILL_SELECT_REQUEST;
  16043. p->packetLength = sizeof( *p );
  16044. p->flag = 1; // enum PACKET_ZC_SKILL_SELECT_REQUEST::enumWHY::WHY_SC_AUTOSHADOWSPELL = 0x1
  16045. size_t count = 0;
  16046. for( size_t i = 0; i < MAX_SKILL; i++ ){
  16047. if( sd.status.skill[i].flag == SKILL_FLAG_PLAGIARIZED && sd.status.skill[i].id > 0 &&
  16048. skill_get_inf2(sd.status.skill[i].id, INF2_ISAUTOSHADOWSPELL))
  16049. {
  16050. p->skillIds[count] = sd.status.skill[i].id;
  16051. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skillIds[0] ) );
  16052. count++;
  16053. }
  16054. }
  16055. if( count > 0 ) {
  16056. clif_send( p, p->packetLength, &sd.bl, SELF );
  16057. sd.menuskill_id = SC_AUTOSHADOWSPELL;
  16058. sd.menuskill_val = static_cast<decltype(sd.menuskill_val)>( count );
  16059. } else {
  16060. status_change_end( &sd.bl, SC_STOP );
  16061. clif_skill_fail( sd, SC_AUTOSHADOWSPELL, USESKILL_FAIL_IMITATION_SKILL_NONE );
  16062. }
  16063. #endif
  16064. }
  16065. /*===========================================
  16066. * Skill list for Four Elemental Analysis
  16067. * and Change Material skills.
  16068. * 07e3 <type>.L (ZC_ITEMLISTWIN_OPEN).
  16069. *------------------------------------------*/
  16070. int clif_skill_itemlistwindow( map_session_data *sd, uint16 skill_id, uint16 skill_lv ) {
  16071. #if PACKETVER >= 20090922
  16072. int fd = sd->fd;
  16073. nullpo_ret(sd);
  16074. sd->menuskill_id = skill_id; // To prevent hacking.
  16075. sd->menuskill_val = skill_lv;
  16076. if( skill_id == GN_CHANGEMATERIAL )
  16077. skill_lv = 0; // Changematerial
  16078. WFIFOHEAD(fd, packet_len(0x7e3));
  16079. WFIFOW(fd,0) = 0x7e3;
  16080. WFIFOL(fd,2) = skill_lv;
  16081. WFIFOL(fd,4) = 0;
  16082. WFIFOSET(fd, packet_len(0x7e3));
  16083. #endif
  16084. return 1;
  16085. }
  16086. /*==========================================
  16087. * Select a skill into a given list (used by SA_AUTOSPELL/SC_AUTOSHADOWSPELL)
  16088. * 0443 <type>.L <skill_id>.W (CZ_SKILL_SELECT_RESPONSE)
  16089. *------------------------------------------*/
  16090. void clif_parse_SkillSelectMenu(int fd, map_session_data *sd) {
  16091. #if PACKETVER >= 20081210
  16092. if( sd == nullptr ){
  16093. return;
  16094. }
  16095. const PACKET_CZ_SKILL_SELECT_RESPONSE* p = reinterpret_cast<PACKET_CZ_SKILL_SELECT_RESPONSE*>( RFIFOP( fd, 0 ) );
  16096. if (sd->menuskill_id == SA_AUTOSPELL) {
  16097. sd->state.workinprogress = WIP_DISABLE_NONE;
  16098. skill_autospell(sd, p->selectedSkillId);
  16099. } else if (sd->menuskill_id == SC_AUTOSHADOWSPELL) {
  16100. if (pc_istrading(sd)) {
  16101. clif_skill_fail( *sd, sd->ud.skill_id );
  16102. clif_menuskill_clear(sd);
  16103. return;
  16104. }
  16105. skill_select_menu(*sd, p->selectedSkillId);
  16106. } else
  16107. return;
  16108. clif_menuskill_clear(sd);
  16109. #endif
  16110. }
  16111. /// Kagerou/Oboro amulet spirit.
  16112. /// 08cf <id>.L <type>.W <num>.W (ZC_SPIRITS_ATTRIBUTE)
  16113. void clif_spiritcharm( map_session_data& sd ){
  16114. #if PACKETVER >= 20111102
  16115. PACKET_ZC_SPIRITS_ATTRIBUTE packet{};
  16116. packet.packetType = HEADER_ZC_SPIRITS_ATTRIBUTE;
  16117. packet.aid = sd.bl.id;
  16118. packet.spiritsType = static_cast<decltype(packet.spiritsType)>(sd.spiritcharm_type);
  16119. packet.num = static_cast<decltype(packet.num)>(sd.spiritcharm);
  16120. clif_send( &packet, sizeof( packet ), &sd.bl, AREA );
  16121. #endif
  16122. }
  16123. /// Move Item from or to Personal Tab
  16124. /// 0907 <index>.W <type>.B (CZ_INVENTORY_TAB)
  16125. /// type:
  16126. /// 0 = move item to personal tab
  16127. /// 1 = move item to normal tab
  16128. void clif_parse_MoveItem( int fd, map_session_data* sd ){
  16129. // TODO: Check for correct packet version
  16130. #if PACKETVER >= 20120410
  16131. /* can't move while dead. */
  16132. if(pc_isdead(sd)) {
  16133. return;
  16134. }
  16135. const PACKET_CZ_INVENTORY_TAB* p = reinterpret_cast<PACKET_CZ_INVENTORY_TAB*>( RFIFOP( fd, 0 ) );
  16136. uint16 index = server_index( p->index );
  16137. if( index >= MAX_INVENTORY ){
  16138. return;
  16139. }
  16140. if ( sd->inventory.u.items_inventory[index].favorite != 0 && p->favorite == true )
  16141. sd->inventory.u.items_inventory[index].favorite = 0;
  16142. else if( p->favorite == false )
  16143. sd->inventory.u.items_inventory[index].favorite = 1;
  16144. else
  16145. return;/* nothing to do. */
  16146. clif_favorite_item( *sd, index );
  16147. #endif
  16148. }
  16149. /// Items that are in favorite tab of inventory.
  16150. /// 0908 <index>.W <favorite>.B (ZC_INVENTORY_TAB)
  16151. static void clif_favorite_item( map_session_data& sd, uint16 index ){
  16152. // TODO: Check for correct packet version
  16153. #if PACKETVER >= 20111122
  16154. PACKET_ZC_INVENTORY_TAB p = {};
  16155. p.packetType = HEADER_ZC_INVENTORY_TAB;
  16156. p.index = client_index( index );
  16157. p.favorite = ( sd.inventory.u.items_inventory[index].favorite == 1 ) ? false : true;
  16158. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16159. #endif
  16160. }
  16161. /// 08d2 <id>.L <Pos X>.W <Pos Y>.W (ZC_FASTMOVE).
  16162. void clif_snap( struct block_list *bl, short x, short y ) {
  16163. unsigned char buf[10];
  16164. WBUFW(buf,0) = 0x8d2;
  16165. WBUFL(buf,2) = bl->id;
  16166. WBUFW(buf,6) = x;
  16167. WBUFW(buf,8) = y;
  16168. if( disguised(bl) )
  16169. {
  16170. clif_send(buf, packet_len(0x8d2), bl, AREA_WOS);
  16171. WBUFL(buf,2) = disguised_bl_id(bl->id);
  16172. clif_send(buf, packet_len(0x8d2), bl, SELF);
  16173. } else
  16174. clif_send(buf,packet_len(0x8d2),bl, AREA);
  16175. }
  16176. /// 0977 <id>.L <HP>.L <maxHP>.L (ZC_HP_INFO).
  16177. void clif_monster_hp_bar( struct mob_data* md, int fd ) {
  16178. #if PACKETVER >= 20120404
  16179. WFIFOHEAD(fd,packet_len(0x977));
  16180. WFIFOW(fd,0) = 0x977;
  16181. WFIFOL(fd,2) = md->bl.id;
  16182. WFIFOL(fd,6) = md->status.hp;
  16183. WFIFOL(fd,10) = md->status.max_hp;
  16184. WFIFOSET(fd,packet_len(0x977));
  16185. #endif
  16186. }
  16187. /* [Ind] placeholder for unsupported incoming packets (avoids server disconnecting client) */
  16188. void __attribute__ ((unused)) clif_parse_dull(int fd, map_session_data *sd) {
  16189. return;
  16190. }
  16191. /// Ack world info (ZC_ACK_BEFORE_WORLD_INFO)
  16192. /// 0979 <world name>.24B <char name>.24B
  16193. void clif_ackworldinfo(map_session_data* sd) {
  16194. int fd;
  16195. nullpo_retv(sd);
  16196. fd = sd->fd;
  16197. WFIFOHEAD(fd,packet_len(0x979));
  16198. WFIFOW(fd,0)=0x979;
  16199. //AID -> world name ?
  16200. safestrncpy(WFIFOCP(fd,2), "" /* World name */, 24);
  16201. safestrncpy(WFIFOCP(fd,26), sd->status.name, NAME_LENGTH);
  16202. WFIFOSET(fd,packet_len(0x979));
  16203. }
  16204. /// req world info (CZ_REQ_BEFORE_WORLD_INFO)
  16205. /// 0978 <AID>.L
  16206. void clif_parse_reqworldinfo(int fd,map_session_data *sd) {
  16207. //uint32 aid = RFIFOL(fd,2); //should we trust client ?
  16208. if(sd)
  16209. clif_ackworldinfo(sd);
  16210. }
  16211. static void clif_loadConfirm( map_session_data *sd ){
  16212. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410
  16213. nullpo_retv( sd );
  16214. PACKET_ZC_NOTIFY_ACTORINIT p = {};
  16215. p.packetType = HEADER_ZC_NOTIFY_ACTORINIT;
  16216. clif_send( &p, sizeof(p), &sd->bl, SELF );
  16217. clif_instance_info( *sd );
  16218. #endif
  16219. }
  16220. /// unknown usage (CZ_BLOCKING_PLAY_CANCEL)
  16221. /// 0447
  16222. void clif_parse_blocking_playcancel( int fd, map_session_data *sd ){
  16223. clif_loadConfirm( sd );
  16224. int32 mf = map_getmapflag(sd->bl.m, MF_SPECIALPOPUP);
  16225. if (mf > 0) {
  16226. clif_specialpopup(*sd, mf);
  16227. }
  16228. }
  16229. /// req world info (CZ_CLIENT_VERSION)
  16230. /// 044A <version>.L
  16231. void clif_parse_client_version(int fd, map_session_data *sd) {
  16232. //if(sd)
  16233. ;
  16234. }
  16235. /// Ranking list
  16236. /// ranking pointlist { <name>.24B <point>.L }*10
  16237. void clif_sub_ranklist(unsigned char *buf,int idx,map_session_data* sd, enum e_rank rankingtype){
  16238. struct fame_list* list;
  16239. int i, size = MAX_FAME_LIST;
  16240. switch(rankingtype) {
  16241. case RANK_BLACKSMITH: list = smith_fame_list; break;
  16242. case RANK_ALCHEMIST: list = chemist_fame_list; break;
  16243. case RANK_TAEKWON: list = taekwon_fame_list; break;
  16244. // PK currently unsupported
  16245. case RANK_KILLER: list = nullptr; size = 0; break;
  16246. default:
  16247. ShowError( "clif_sub_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16248. return;
  16249. }
  16250. //Packet size limits this list to 10 elements. [Skotlex]
  16251. for (i = 0; i < min(10,size); i++) {
  16252. if (list[i].id > 0) {
  16253. const char* name;
  16254. if (strcmp(list[i].name, "-") == 0 &&
  16255. (name = map_charid2nick(list[i].id)) != nullptr)
  16256. {
  16257. safestrncpy(WBUFCP(buf,idx + NAME_LENGTH * i), name, NAME_LENGTH);
  16258. } else {
  16259. safestrncpy(WBUFCP(buf,idx + NAME_LENGTH * i), list[i].name, NAME_LENGTH);
  16260. }
  16261. } else {
  16262. safestrncpy(WBUFCP(buf, idx + NAME_LENGTH * i), "None", NAME_LENGTH);
  16263. }
  16264. WBUFL(buf, idx+NAME_LENGTH*10 + i * 4) = list[i].fame; //points
  16265. }
  16266. for(;i < 10; i++) { //In case the MAX is less than 10.
  16267. safestrncpy(WBUFCP(buf, idx + NAME_LENGTH * i), "Unavailable", NAME_LENGTH);
  16268. WBUFL(buf, idx+NAME_LENGTH*10 + i * 4) = 0;
  16269. }
  16270. }
  16271. /// Request for the blacksmith ranklist.
  16272. /// /blacksmith command sends this packet to the server.
  16273. /// 0217 (CZ_BLACKSMITH_RANK)
  16274. void clif_parse_Blacksmith( int fd, map_session_data *sd ){
  16275. clif_ranklist(sd,RANK_BLACKSMITH);
  16276. }
  16277. /// Request for the alchemist ranklist.
  16278. /// /alchemist command sends this packet to the server.
  16279. /// 0218 (CZ_ALCHEMIST_RANK)
  16280. void clif_parse_Alchemist( int fd, map_session_data *sd ){
  16281. clif_ranklist(sd,RANK_ALCHEMIST);
  16282. }
  16283. /// Request for the taekwon ranklist.
  16284. /// /taekwon command sends this packet to the server.
  16285. /// 0225 (CZ_TAEKWON_RANK)
  16286. void clif_parse_Taekwon( int fd, map_session_data *sd ){
  16287. clif_ranklist(sd,RANK_TAEKWON);
  16288. }
  16289. /// Request for the killer ranklist.
  16290. /// /pk command sends this packet to the server.
  16291. /// 0237 (CZ_KILLER_RANK)
  16292. void clif_parse_RankingPk( int fd, map_session_data *sd ){
  16293. clif_ranklist(sd,RANK_KILLER);
  16294. }
  16295. /// 0219 { <name>.24B }*10 { <point>.L }*10 (ZC_BLACKSMITH_RANK)
  16296. /// 021a { <name>.24B }*10 { <point>.L }*10 (ZC_ALCHEMIST_RANK)
  16297. /// 0226 { <name>.24B }*10 { <point>.L }*10 (ZC_TAEKWON_RANK)
  16298. /// 0238 { <name>.24B }*10 { <point>.L }*10 (ZC_KILLER_RANK)
  16299. /// 097d <RankingType>.W {<CharName>.24B <point>L}*10 <mypoint>L (ZC_ACK_RANKING)
  16300. void clif_ranklist(map_session_data *sd, int16 rankingType) {
  16301. enum e_rank rank;
  16302. #if PACKETVER < 20130710
  16303. unsigned char buf[MAX_FAME_LIST * sizeof(struct fame_list)+2];
  16304. short cmd;
  16305. switch( rankingType ){
  16306. case RANK_BLACKSMITH: cmd = 0x219; break;
  16307. case RANK_ALCHEMIST: cmd = 0x21a; break;
  16308. case RANK_TAEKWON: cmd = 0x226; break;
  16309. case RANK_KILLER: cmd = 0x238; break;
  16310. default:
  16311. ShowError( "clif_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingType );
  16312. return;
  16313. }
  16314. // Can be safely casted here, since we validated it before
  16315. rank = (enum e_rank)rankingType;
  16316. WBUFW(buf,0) = cmd;
  16317. clif_sub_ranklist(buf,2,sd,rank);
  16318. clif_send(buf, packet_len(cmd), &sd->bl, SELF);
  16319. #else
  16320. unsigned char buf[MAX_FAME_LIST * sizeof(struct fame_list)+8];
  16321. int mypoint = 0;
  16322. switch( rankingType ){
  16323. case RANK_BLACKSMITH:
  16324. case RANK_ALCHEMIST:
  16325. case RANK_TAEKWON:
  16326. case RANK_KILLER:
  16327. break;
  16328. default:
  16329. ShowError( "clif_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingType );
  16330. return;
  16331. }
  16332. // Can be safely casted here, since we validated it before
  16333. rank = (enum e_rank)rankingType;
  16334. WBUFW(buf,0) = 0x97d;
  16335. WBUFW(buf,2) = rank;
  16336. clif_sub_ranklist(buf,4,sd,rank);
  16337. switch(sd->class_&MAPID_UPPERMASK){ //mypoint (checking if valid type)
  16338. case MAPID_BLACKSMITH:
  16339. case MAPID_ALCHEMIST:
  16340. case MAPID_TAEKWON:
  16341. mypoint = sd->status.fame;
  16342. }
  16343. WBUFL(buf,284) = mypoint; //mypoint
  16344. clif_send(buf, 288, &sd->bl, SELF);
  16345. #endif
  16346. }
  16347. /*
  16348. * 097c <type> (CZ_REQ_RANKING)
  16349. * type
  16350. * 0: /blacksmith
  16351. * 1: /alchemist
  16352. * 2: /taekwon
  16353. * 3: /pk
  16354. * */
  16355. void clif_parse_ranklist(int fd,map_session_data *sd) {
  16356. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  16357. int16 rankingtype = RFIFOW(fd,info->pos[0]); //type
  16358. clif_ranklist(sd,rankingtype);
  16359. }
  16360. /// Updates the fame rank points for the given ranking.
  16361. /// 021b <points>.L <total points>.L (ZC_BLACKSMITH_POINT)
  16362. /// 021c <points>.L <total points>.L (ZC_ALCHEMIST_POINT)
  16363. /// 0224 <points>.L <total points>.L (ZC_TAEKWON_POINT)
  16364. /// 097e <RankingType>.W <point>.L <TotalPoint>.L (ZC_UPDATE_RANKING_POINT)
  16365. void clif_update_rankingpoint(map_session_data &sd, int rankingtype, int point) {
  16366. int fd = sd.fd;
  16367. #if PACKETVER < 20130710
  16368. short cmd;
  16369. switch(rankingtype){
  16370. case RANK_BLACKSMITH: cmd = 0x21b; break;
  16371. case RANK_ALCHEMIST: cmd = 0x21c; break;
  16372. case RANK_TAEKWON: cmd = 0x224; break;
  16373. default:
  16374. ShowError( "clif_update_rankingpoint: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16375. return;
  16376. }
  16377. WFIFOHEAD(fd,packet_len(cmd));
  16378. WFIFOW(fd,0) = cmd;
  16379. WFIFOL(fd,2) = point;
  16380. WFIFOL(fd,6) = sd.status.fame;
  16381. WFIFOSET(fd, packet_len(cmd));
  16382. #else
  16383. WFIFOHEAD(fd,packet_len(0x97e));
  16384. WFIFOW(fd,0) = 0x97e;
  16385. WFIFOW(fd,2) = rankingtype;
  16386. WFIFOL(fd,4) = point;
  16387. WFIFOL(fd,8) = sd.status.fame;
  16388. WFIFOSET(fd,packet_len(0x97e));
  16389. #endif
  16390. }
  16391. /**
  16392. * Transmit personal information to player. (rates)
  16393. * 0x08cb <packet len>.W <exp>.W <death>.W <drop>.W <DETAIL_EXP_INFO>7B (ZC_PERSONAL_INFOMATION)
  16394. * <InfoType>.B <Exp>.W <Death>.W <Drop>.W (DETAIL_EXP_INFO 0x08cb)
  16395. * 0x097b <packet len>.W <exp>.L <death>.L <drop>.L <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION2)
  16396. * 0x0981 <packet len>.W <exp>.W <death>.W <drop>.W <activity rate>.W <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION_CHN)
  16397. * <InfoType>.B <Exp>.L <Death>.L <Drop>.L (DETAIL_EXP_INFO 0x97b|0981)
  16398. * FIXME!
  16399. * - Find/decide for details of EXP, Drop, and Death penalty rates
  16400. * - For now, we're assuming values for DETAIL_EXP_INFO are:
  16401. * 0 - map adjustment (bexp mapflag), 1 - Premium/VIP adjustment, 2 - Server rate adjustment, 3 - None
  16402. */
  16403. #ifdef VIP_ENABLE
  16404. void clif_display_pinfo( map_session_data& sd ){
  16405. #if PACKETVER_MAIN_NUM >= 20110627 || PACKETVER_RE_NUM >= 20110628 || defined(PACKETVER_ZERO)
  16406. // 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]
  16407. int details_bexp[PINFO_MAX], details_drop[PINFO_MAX], details_penalty[PINFO_MAX];
  16408. /**
  16409. * EXP
  16410. */
  16411. //0:PCRoom
  16412. details_bexp[PINFO_BASIC] = map_getmapflag( sd.bl.m, MF_BEXP );
  16413. if (details_bexp[PINFO_BASIC] == 100 || !details_bexp[PINFO_BASIC])
  16414. details_bexp[PINFO_BASIC] = 0;
  16415. else {
  16416. if (details_bexp[PINFO_BASIC] < 100) {
  16417. details_bexp[PINFO_BASIC] = 100 - details_bexp[PINFO_BASIC];
  16418. details_bexp[PINFO_BASIC] = 0 - details_bexp[PINFO_BASIC];
  16419. } else
  16420. details_bexp[PINFO_BASIC] = details_bexp[PINFO_BASIC] - 100;
  16421. }
  16422. //1:Premium
  16423. if( pc_isvip( &sd ) ){
  16424. details_bexp[PINFO_PREMIUM] = battle_config.vip_base_exp_increase * battle_config.base_exp_rate / 100;
  16425. if (details_bexp[PINFO_PREMIUM] < 0)
  16426. details_bexp[PINFO_PREMIUM] = 0 - details_bexp[PINFO_PREMIUM];
  16427. } else
  16428. details_bexp[PINFO_PREMIUM] = 0;
  16429. //2:Server
  16430. details_bexp[PINFO_SERVER] = battle_config.base_exp_rate;
  16431. if (details_bexp[PINFO_SERVER] == 100)
  16432. details_bexp[PINFO_SERVER] = 0;
  16433. else {
  16434. if (details_bexp[PINFO_SERVER] < 100) {
  16435. details_bexp[PINFO_SERVER] = 100 - details_bexp[PINFO_SERVER];
  16436. details_bexp[PINFO_SERVER] = 0 - details_bexp[PINFO_SERVER];
  16437. } else
  16438. details_bexp[PINFO_SERVER] = details_bexp[PINFO_SERVER] - 100;
  16439. }
  16440. //3:TPLUS
  16441. details_bexp[PINFO_CAFE] = 0;
  16442. /**
  16443. * Drop rate
  16444. */
  16445. //0:PCRoom
  16446. details_drop[PINFO_BASIC] = 0;
  16447. //1:Premium
  16448. if( pc_isvip( &sd ) ){
  16449. details_drop[PINFO_PREMIUM] = (battle_config.vip_drop_increase * battle_config.item_rate_common) / 100;
  16450. if (details_drop[PINFO_PREMIUM] < 0)
  16451. details_drop[PINFO_PREMIUM] = 0 - details_drop[PINFO_PREMIUM];
  16452. } else
  16453. details_drop[PINFO_PREMIUM] = 0;
  16454. //2:Server
  16455. details_drop[PINFO_SERVER] = battle_config.item_rate_common;
  16456. if (details_drop[PINFO_SERVER] == 100)
  16457. details_drop[PINFO_SERVER] = 0;
  16458. else {
  16459. if (details_drop[PINFO_SERVER] < 100) {
  16460. details_drop[PINFO_SERVER] = 100 - details_drop[PINFO_SERVER];
  16461. details_drop[PINFO_SERVER] = 0 - details_drop[PINFO_SERVER];
  16462. } else
  16463. details_drop[PINFO_SERVER] = details_drop[PINFO_SERVER] - 100;
  16464. }
  16465. //3:TPLUS
  16466. details_drop[PINFO_CAFE] = 0;
  16467. /**
  16468. * Penalty rate
  16469. */
  16470. //! FIXME: Current penalty system makes this announcement unable to give info on + or - rate
  16471. //0:PCRoom
  16472. details_penalty[PINFO_BASIC] = 0;
  16473. //1:Premium
  16474. if( pc_isvip( &sd ) ){
  16475. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base;
  16476. if (details_penalty[PINFO_PREMIUM] == 100)
  16477. details_penalty[PINFO_PREMIUM] = 0;
  16478. else {
  16479. if (details_penalty[PINFO_PREMIUM] < 100) {
  16480. details_penalty[PINFO_PREMIUM] = 100 - details_penalty[PINFO_PREMIUM];
  16481. details_penalty[PINFO_PREMIUM] = 0 - details_penalty[PINFO_PREMIUM];
  16482. } else
  16483. details_penalty[PINFO_PREMIUM] = details_penalty[PINFO_PREMIUM] - 100;
  16484. }
  16485. if (battle_config.death_penalty_base > battle_config.vip_exp_penalty_base)
  16486. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base - battle_config.death_penalty_base;
  16487. } else
  16488. details_penalty[PINFO_PREMIUM] = 0;
  16489. //2:Server
  16490. details_penalty[PINFO_SERVER] = battle_config.death_penalty_base;
  16491. if (details_penalty[PINFO_SERVER] == 100)
  16492. details_penalty[PINFO_SERVER] = 0;
  16493. else {
  16494. if (details_penalty[PINFO_SERVER] < 100) {
  16495. details_penalty[PINFO_SERVER] = 100 - details_penalty[PINFO_SERVER];
  16496. details_penalty[PINFO_SERVER] = 0 - details_penalty[PINFO_SERVER];
  16497. } else
  16498. details_penalty[PINFO_SERVER] = details_penalty[PINFO_SERVER] - 100;
  16499. }
  16500. //3:TPLUS
  16501. details_penalty[PINFO_CAFE] = 0;
  16502. PACKET_ZC_PERSONAL_INFOMATION* p = reinterpret_cast<PACKET_ZC_PERSONAL_INFOMATION*>( packet_buffer );
  16503. p->packetType = HEADER_ZC_PERSONAL_INFOMATION;
  16504. p->length = sizeof( *p );
  16505. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16506. p->total_exp = 100 * 1000;
  16507. p->total_death = 100 * 1000;
  16508. p->total_drop = 100 * 1000;
  16509. #else
  16510. p->total_exp = 100;
  16511. p->total_death = 100;
  16512. p->total_drop = 100;
  16513. #endif
  16514. for( int i = PINFO_BASIC; i < PINFO_MAX; i++ ){
  16515. p->details[i].type = i; // infotype 0 PCRoom, 1 Premium, 2 Server, 3 TPlus
  16516. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16517. p->details[i].exp = details_bexp[i] * 1000;
  16518. p->details[i].death = details_penalty[i] * 1000;
  16519. p->details[i].drop = details_drop[i] * 1000;
  16520. #else
  16521. p->details[i].exp = details_bexp[i];
  16522. p->details[i].death = details_penalty[i];
  16523. p->details[i].drop = details_drop[i];
  16524. #endif
  16525. p->total_exp += p->details[i].exp;
  16526. p->total_death += p->details[i].death;
  16527. p->total_drop += p->details[i].drop;
  16528. p->length += static_cast<decltype(p->length)>( sizeof( p->details[0] ) );
  16529. }
  16530. clif_send( p, p->length, &sd.bl, SELF );
  16531. #endif
  16532. }
  16533. #endif
  16534. void clif_parse_GMFullStrip(int fd, map_session_data *sd) {
  16535. char cmd[30];
  16536. int t_aid = RFIFOL(fd,2);
  16537. safesnprintf(cmd,sizeof(cmd),"%cfullstrip %d",atcommand_symbol,t_aid);
  16538. is_atcommand(fd, sd, cmd, 1);
  16539. }
  16540. /**
  16541. * Marks Crimson Marker target on mini-map to the caster
  16542. * 09C1 <id>.L <x>.W <y>.W (ZC_C_MARKERINFO)
  16543. * @param fd
  16544. * @param bl Crimson Marker target
  16545. **/
  16546. void clif_crimson_marker( map_session_data& sd, struct block_list& bl, bool remove ){
  16547. #if PACKETVER_MAIN_NUM >= 20130731 || PACKETVER_RE_NUM >= 20130707 || defined(PACKETVER_ZERO)
  16548. PACKET_ZC_C_MARKERINFO p = {};
  16549. p.PacketType = HEADER_ZC_C_MARKERINFO;
  16550. p.AID = bl.id;
  16551. if( remove ){
  16552. p.xPos = -1;
  16553. p.yPos = -1;
  16554. }else{
  16555. p.xPos = bl.x;
  16556. p.yPos = bl.y;
  16557. }
  16558. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16559. #endif
  16560. }
  16561. /**
  16562. * 02d3 <index>.W (ZC_NOTIFY_BIND_ON_EQUIP)
  16563. **/
  16564. void clif_notify_bindOnEquip( map_session_data& sd, int16 index ){
  16565. #if PACKETVER >= 20070227
  16566. PACKET_ZC_NOTIFY_BIND_ON_EQUIP p = {};
  16567. p.packetType = HEADER_ZC_NOTIFY_BIND_ON_EQUIP;
  16568. p.index = client_index( index );
  16569. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16570. #endif
  16571. }
  16572. /**
  16573. * [Ind/Hercules]
  16574. * 08b3 <Length>.W <id>.L <message>.?B (ZC_SHOWSCRIPT)
  16575. **/
  16576. void clif_showscript(struct block_list* bl, const char* message, enum send_target flag) {
  16577. char buf[256];
  16578. size_t len;
  16579. nullpo_retv(bl);
  16580. if(!message)
  16581. return;
  16582. len = strlen(message)+1;
  16583. if( len > sizeof(buf)-8 ) {
  16584. ShowWarning("clif_showscript: Truncating too long message '%s' (len=%" PRIuPTR ").\n", message, len);
  16585. len = sizeof(buf)-8;
  16586. }
  16587. WBUFW(buf,0) = 0x8b3;
  16588. WBUFW(buf,2) = (uint16)(len+8);
  16589. WBUFL(buf,4) = bl->id;
  16590. safestrncpy(WBUFCP(buf,8), message, len);
  16591. clif_send((unsigned char *) buf, WBUFW(buf,2), bl, flag);
  16592. }
  16593. /**
  16594. * [Ind/Hercules]
  16595. * 07fc <OldMasterAID>.L <NewMasterAID>.L (ZC_CHANGE_GROUP_MASTER)
  16596. **/
  16597. void clif_party_leaderchanged(map_session_data *sd, int prev_leader_aid, int new_leader_aid) {
  16598. unsigned char buf[10];
  16599. nullpo_retv(sd);
  16600. WBUFW(buf,0) = 0x7fc;
  16601. WBUFL(buf,2) = prev_leader_aid;
  16602. WBUFL(buf,6) = new_leader_aid;
  16603. clif_send(buf, packet_len(0x7fc), &sd->bl, PARTY);
  16604. }
  16605. /**
  16606. * Sends a clan message to a player
  16607. * 098e <length>.W <name>.24B <message>.?B (ZC_NOTIFY_CLAN_CHAT)
  16608. **/
  16609. void clif_clan_message( struct clan& clan, const char *mes, size_t len ){
  16610. #if PACKETVER >= 20131223
  16611. map_session_data* sd = clan_getavailablesd( clan );
  16612. if( len == 0 ){
  16613. return;
  16614. }
  16615. PACKET_ZC_NOTIFY_CLAN_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CLAN_CHAT*>( packet_buffer );
  16616. // Maximum message length = size of our packet buffer minus fixed size of the packet and 1 byte zero termination
  16617. static size_t len_max = sizeof( packet_buffer ) - sizeof( *p ) - 1;
  16618. // Is the length bigger than the maximum message length
  16619. if( len > len_max ){
  16620. ShowWarning( "clif_clan_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", clan_id=%d).\n", mes, len, len_max, clan.id );
  16621. len = len_max;
  16622. }
  16623. p->PacketType = HEADER_ZC_NOTIFY_CLAN_CHAT;
  16624. p->PacketLength = sizeof( *p );
  16625. // 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
  16626. safestrncpy( p->MemberName, "", sizeof( p->MemberName ) );
  16627. safestrncpy( p->Message, mes, len + 1 );
  16628. p->PacketLength += static_cast<decltype(p->PacketLength)>( len + 1 );
  16629. clif_send( p, p->PacketLength, &sd->bl, CLAN );
  16630. #endif
  16631. }
  16632. /**
  16633. * Parses a clan message from a player.
  16634. * 098d <length>.W <text>.?B (<name> : <message>) (CZ_CLAN_CHAT)
  16635. **/
  16636. void clif_parse_clan_chat( int fd, map_session_data* sd ){
  16637. #if PACKETVER >= 20131223
  16638. if( sd == nullptr ){
  16639. return;
  16640. }
  16641. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  16642. // validate packet and retrieve name and message
  16643. if( !clif_process_message(sd, false, name, message, output ) )
  16644. return;
  16645. clan_send_message( *sd, RFIFOCP(fd,4), RFIFOW(fd,2) - 4 );
  16646. #endif
  16647. }
  16648. /**
  16649. * Sends the basic clan informations to the client.
  16650. * 098a <length>.W <clan id>.L <clan name>.24B <clan master>.24B <clan map>.16B <alliance count>.B
  16651. * <antagonist count>.B { <alliance>.24B } * alliance count { <antagonist>.24B } * antagonist count (ZC_CLANINFO)
  16652. **/
  16653. void clif_clan_basicinfo( map_session_data& sd ){
  16654. #if PACKETVER_MAIN_NUM >= 20130626 || PACKETVER_RE_NUM >= 20130605 || defined(PACKETVER_ZERO)
  16655. struct clan* clan = sd.clan;
  16656. if( clan == nullptr ){
  16657. return;
  16658. }
  16659. PACKET_ZC_CLANINFO* p = reinterpret_cast<PACKET_ZC_CLANINFO*>( packet_buffer );
  16660. p->PacketType = HEADER_ZC_CLANINFO;
  16661. p->PacketLength = sizeof( *p );
  16662. p->ClanID = clan->id;
  16663. safestrncpy( p->ClanName, clan->name, sizeof( p->ClanName ) );
  16664. safestrncpy( p->MasterName, clan->master, sizeof( p->MasterName ) );
  16665. mapindex_getmapname_ext( clan->map, p->Map );
  16666. p->AllyCount = clan_get_alliance_count( *clan, 0 );
  16667. p->AntagonistCount = clan_get_alliance_count( *clan, 1 );
  16668. for( int flag = 0; flag < 2; flag++ ){
  16669. for( int i = 0; i < MAX_CLANALLIANCE; i++ ){
  16670. if( clan->alliance[i].clan_id > 0 && clan->alliance[i].opposition == flag ){
  16671. char* name = reinterpret_cast<char*>( WBUFP( p, p->PacketLength ) );
  16672. safestrncpy( name, clan->alliance[i].name, NAME_LENGTH );
  16673. p->PacketLength += static_cast<decltype(p->PacketLength)>( NAME_LENGTH );
  16674. }
  16675. }
  16676. }
  16677. clif_send( p, p->PacketLength, &sd.bl, SELF );
  16678. #endif
  16679. }
  16680. /**
  16681. * Updates the online and maximum player count of a clan.
  16682. * 0988 <online count>.W <maximum member amount>.W (ZC_NOTIFY_CLAN_CONNECTINFO)
  16683. **/
  16684. void clif_clan_onlinecount( struct clan& clan ){
  16685. #if PACKETVER >= 20131223
  16686. map_session_data* sd = clan_getavailablesd( clan );
  16687. if( sd == nullptr){
  16688. return;
  16689. }
  16690. PACKET_ZC_NOTIFY_CLAN_CONNECTINFO p = {};
  16691. p.PacketType = HEADER_ZC_NOTIFY_CLAN_CONNECTINFO;
  16692. p.NumConnect = clan.connect_member;
  16693. p.NumTotal = clan.max_member;
  16694. clif_send( &p, sizeof( p ), &sd->bl, CLAN );
  16695. #endif
  16696. }
  16697. /**
  16698. * Notifies the client that the player has left his clan.
  16699. * 0989 (ZC_ACK_CLAN_LEAVE)
  16700. **/
  16701. void clif_clan_leave( map_session_data& sd ){
  16702. #if PACKETVER >= 20131223
  16703. PACKET_ZC_ACK_CLAN_LEAVE p = {};
  16704. p.PacketType = HEADER_ZC_ACK_CLAN_LEAVE;
  16705. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16706. #endif
  16707. }
  16708. /**
  16709. * Acknowledge the client about change title result (ZC_ACK_CHANGE_TITLE).
  16710. * 0A2F <result>.B <title_id>.L
  16711. */
  16712. void clif_change_title_ack(map_session_data *sd, unsigned char result, unsigned long title_id)
  16713. {
  16714. #if PACKETVER >= 20150513
  16715. int fd;
  16716. nullpo_retv(sd);
  16717. if (!clif_session_isValid(sd))
  16718. return;
  16719. fd = sd->fd;
  16720. WFIFOHEAD(fd, packet_len(0xa2f));
  16721. WFIFOW(fd, 0) = 0xa2f;
  16722. WFIFOB(fd, 2) = result;
  16723. WFIFOL(fd, 3) = title_id;
  16724. WFIFOSET(fd, packet_len(0xa2f));
  16725. #endif
  16726. }
  16727. /**
  16728. * Parsing a request from the client change title (CZ_REQ_CHANGE_TITLE).
  16729. * 0A2E <title_id>.L
  16730. */
  16731. void clif_parse_change_title(int fd, map_session_data *sd)
  16732. {
  16733. int title_id;
  16734. nullpo_retv(sd);
  16735. title_id = RFIFOL(fd, 2);
  16736. if( title_id == sd->status.title_id ){
  16737. // It is exactly the same as the old one
  16738. return;
  16739. }else if( title_id <= 0 ){
  16740. sd->status.title_id = 0;
  16741. }else{
  16742. if (std::find(sd->titles.begin(), sd->titles.end(), title_id) == sd->titles.end()) {
  16743. clif_change_title_ack(sd, 1, title_id);
  16744. return;
  16745. }
  16746. sd->status.title_id = title_id;
  16747. }
  16748. clif_name_area(&sd->bl);
  16749. clif_change_title_ack(sd, 0, title_id);
  16750. }
  16751. #ifdef DUMP_UNKNOWN_PACKET
  16752. void DumpUnknown(int fd,TBL_PC *sd,int cmd,int packet_len)
  16753. {
  16754. const char* packet_txt = "save/packet.txt";
  16755. FILE* fp;
  16756. time_t time_server;
  16757. struct tm *datetime;
  16758. char datestr[512];
  16759. time(&time_server); // get time in seconds since 1/1/1970
  16760. datetime = localtime(&time_server); // convert seconds in structure
  16761. // like sprintf, but only for date/time (Sunday, November 02 2003 15:12:52)
  16762. strftime(datestr, sizeof(datestr)-1, "%A, %B %d %Y %X.", datetime); // Server time (normal time): %A, %B %d %Y %X.
  16763. if( ( fp = fopen( packet_txt , "a" ) ) != nullptr ) {
  16764. if( sd ) {
  16765. 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);
  16766. } else {
  16767. fprintf(fp, "Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16768. }
  16769. WriteDump(fp, RFIFOP(fd,0), packet_len);
  16770. fprintf(fp, "\n");
  16771. fclose(fp);
  16772. } else {
  16773. ShowError("Failed to write '%s'.\n", packet_txt);
  16774. // Dump on console instead
  16775. if( sd ) {
  16776. 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);
  16777. } else {
  16778. ShowDebug("Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16779. }
  16780. ShowDump(RFIFOP(fd,0), packet_len);
  16781. }
  16782. }
  16783. #endif
  16784. /// Roulette System
  16785. /// Author: Yommy
  16786. void roulette_generate_bonus( map_session_data& sd ){
  16787. if( battle_config.feature_roulette_bonus_reward && sd.roulette.bonusItemID == 0 ){
  16788. int next_stage = 0;
  16789. if( sd.roulette_point.bronze > 0 ){
  16790. next_stage = 0;
  16791. }else if( sd.roulette_point.silver > 9 ){
  16792. next_stage = 2;
  16793. }else if( sd.roulette_point.gold > 9 ){
  16794. next_stage = 4;
  16795. }
  16796. // Get bonus item stage only from current stage or higher
  16797. int reward_stage = rnd_value( next_stage, MAX_ROULETTE_LEVEL - 1 );
  16798. sd.roulette.bonusItemID = rd.nameid[reward_stage][rnd()%rd.items[reward_stage]];
  16799. }else{
  16800. sd.roulette.bonusItemID = 0;
  16801. }
  16802. }
  16803. /// Opens the roulette window
  16804. /// 0A1A <result>.B <serial>.L <stage>.B <price index>.B <additional item id>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_OPEN_ROULETTE)
  16805. void clif_roulette_open( map_session_data* sd ){
  16806. nullpo_retv( sd );
  16807. roulette_generate_bonus( *sd );
  16808. struct packet_roulette_open_ack p;
  16809. p.PacketType = 0xa1a;
  16810. p.Result = 0; // result
  16811. p.Serial = 0; // serial
  16812. p.Step = (sd->roulette.claimPrize) ? sd->roulette.stage - 1 : 0;
  16813. p.Idx = (sd->roulette.claimPrize) ? sd->roulette.prizeIdx : -1;
  16814. p.AdditionItemID = sd->roulette.bonusItemID;
  16815. p.GoldPoint = sd->roulette_point.gold;
  16816. p.SilverPoint = sd->roulette_point.silver;
  16817. p.BronzePoint = sd->roulette_point.bronze;
  16818. sd->state.roulette_open = true;
  16819. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  16820. }
  16821. /// Request to open the roulette window
  16822. /// 0A19 (CZ_REQ_OPEN_ROULETTE)
  16823. void clif_parse_roulette_open( int fd, map_session_data* sd ){
  16824. nullpo_retv(sd);
  16825. if (!battle_config.feature_roulette) {
  16826. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16827. return;
  16828. }
  16829. clif_roulette_open(sd);
  16830. }
  16831. /// Sends the info about the available roulette rewards to the client
  16832. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.W <amount>.W } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO)
  16833. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.L <amount>.L } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO) >= 20180516
  16834. void clif_roulette_info( map_session_data* sd ){
  16835. nullpo_retv(sd);
  16836. struct packet_roulette_info_ack p;
  16837. p.PacketType = rouletteinfoackType;
  16838. p.PacketLength = sizeof(p);
  16839. p.RouletteSerial = 1;
  16840. for( int i = 0, count = 0; i < MAX_ROULETTE_LEVEL; i++ ){
  16841. for( int j = 0; j < MAX_ROULETTE_COLUMNS - i; j++ ){
  16842. p.ItemInfo[count].Row = i;
  16843. p.ItemInfo[count].Position = j;
  16844. p.ItemInfo[count].ItemId = client_nameid( rd.nameid[i][j] );
  16845. p.ItemInfo[count].Count = rd.qty[i][j];
  16846. #if PACKETVER >= 20180511
  16847. p.ItemInfo[count].unused = 0;
  16848. #endif
  16849. count++;
  16850. }
  16851. }
  16852. clif_send( &p, sizeof(p), &sd->bl, SELF );
  16853. }
  16854. /// Request the roulette reward data
  16855. /// 0A1B (CZ_REQ_ROULETTE_INFO)
  16856. void clif_parse_roulette_info( int fd, map_session_data* sd ){
  16857. nullpo_retv(sd);
  16858. if (!battle_config.feature_roulette) {
  16859. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16860. return;
  16861. }
  16862. clif_roulette_info(sd);
  16863. }
  16864. /// Notification of the client that the roulette window was closed
  16865. /// 0A1D (CZ_REQ_CLOSE_ROULETTE)
  16866. void clif_parse_roulette_close( int fd, map_session_data* sd ){
  16867. nullpo_retv(sd);
  16868. if (!battle_config.feature_roulette) {
  16869. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16870. return;
  16871. }
  16872. sd->state.roulette_open = false;
  16873. }
  16874. /// Response to a item reward request
  16875. /// 0A22 <type>.B <bonus item>.W (ZC_RECV_ROULETTE_ITEM)
  16876. static void clif_roulette_recvitem_ack(map_session_data *sd, enum RECV_ROULETTE_ITEM_REQ type) {
  16877. #if PACKETVER >= 20141016
  16878. nullpo_retv(sd);
  16879. struct packet_roulette_itemrecv_ack p;
  16880. p.PacketType = roulettercvitemackType;
  16881. p.Result = type;
  16882. p.AdditionItemID = sd->roulette.bonusItemID;
  16883. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  16884. #endif
  16885. }
  16886. /**
  16887. * Claim roulette reward
  16888. * @param sd Player
  16889. * @return 0:Success
  16890. **/
  16891. static uint8 clif_roulette_getitem(map_session_data *sd) {
  16892. struct item it;
  16893. uint8 res = 1;
  16894. unsigned short factor = 1;
  16895. nullpo_retr(1, sd);
  16896. if (sd->roulette.prizeIdx < 0)
  16897. return RECV_ITEM_FAILED;
  16898. memset(&it, 0, sizeof(it));
  16899. it.nameid = rd.nameid[sd->roulette.prizeStage][sd->roulette.prizeIdx];
  16900. it.identify = 1;
  16901. if( sd->roulette.bonusItemID != 0 ){
  16902. if( sd->roulette.bonusItemID == it.nameid && battle_config.feature_roulette_bonus_reward && !( rd.flag[sd->roulette.prizeStage][sd->roulette.prizeIdx]&1 ) ){
  16903. factor = 2;
  16904. // Reset the Bonus Item to trigger new calculation
  16905. sd->roulette.bonusItemID = 0;
  16906. }else{
  16907. for( int i = 0; i < MAX_ROULETTE_LEVEL && sd->roulette.bonusItemID != 0; i++ ){
  16908. for( int j = 0; j < MAX_ROULETTE_COLUMNS - i && sd->roulette.bonusItemID != 0; j++ ){
  16909. if( rd.nameid[i][j] == sd->roulette.bonusItemID ){
  16910. // 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
  16911. if( sd->roulette.prizeStage >= i ){
  16912. // Reset the Bonus Item to trigger new calculation and to cancel the loops
  16913. sd->roulette.bonusItemID = 0;
  16914. }
  16915. }
  16916. }
  16917. }
  16918. }
  16919. }
  16920. if ((res = pc_additem(sd, &it, rd.qty[sd->roulette.prizeStage][sd->roulette.prizeIdx] * factor, LOG_TYPE_ROULETTE)) == 0) {
  16921. ; // onSuccess
  16922. }
  16923. if( sd->roulette.bonusItemID == 0 ){
  16924. roulette_generate_bonus( *sd );
  16925. }
  16926. sd->roulette.claimPrize = false;
  16927. sd->roulette.prizeStage = 0;
  16928. sd->roulette.prizeIdx = -1;
  16929. sd->roulette.stage = 0;
  16930. return res;
  16931. }
  16932. /// Update Roulette window with current stats
  16933. /// 0A20 <result>.B <stage>.W <price index>.W <bonus item>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_GENERATE_ROULETTE)
  16934. void clif_roulette_generate( map_session_data *sd, unsigned char result, short stage, short prizeIdx, t_itemid bonusItemID ){
  16935. nullpo_retv( sd );
  16936. struct packet_roulette_generate_ack p;
  16937. p.PacketType = roulettgenerateackType;
  16938. p.Result = result;
  16939. p.Step = stage;
  16940. p.Idx = prizeIdx;
  16941. p.AdditionItemID = battle_config.feature_roulette_bonus_reward ? bonusItemID : 0;
  16942. p.RemainGold = sd->roulette_point.gold;
  16943. p.RemainSilver = sd->roulette_point.silver;
  16944. p.RemainBronze = sd->roulette_point.bronze;
  16945. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  16946. }
  16947. /// Request to start the roulette
  16948. /// 0A1F (CZ_REQ_GENERATE_ROULETTE)
  16949. void clif_parse_roulette_generate( int fd, map_session_data* sd ){
  16950. enum GENERATE_ROULETTE_ACK result;
  16951. nullpo_retv(sd);
  16952. if (!battle_config.feature_roulette) {
  16953. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16954. return;
  16955. }
  16956. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  16957. return;
  16958. }
  16959. // Player has not claimed his prize yet
  16960. if( sd->roulette.claimPrize ){
  16961. clif_roulette_getitem( sd );
  16962. }
  16963. if (sd->roulette.stage >= MAX_ROULETTE_LEVEL){
  16964. // Make sure everything is reset
  16965. sd->roulette.stage = 0;
  16966. sd->roulette.claimPrize = false;
  16967. sd->roulette.prizeStage = 0;
  16968. sd->roulette.prizeIdx = -1;
  16969. }
  16970. if( !sd->roulette.stage && sd->roulette_point.bronze <= 0 && sd->roulette_point.silver < 10 && sd->roulette_point.gold < 10 ){
  16971. result = GENERATE_ROULETTE_NO_ENOUGH_POINT;
  16972. }else{
  16973. if (!sd->roulette.stage) {
  16974. if (sd->roulette_point.bronze > 0) {
  16975. sd->roulette_point.bronze -= 1;
  16976. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  16977. } else if (sd->roulette_point.silver > 9) {
  16978. sd->roulette_point.silver -= 10;
  16979. sd->roulette.stage = 2;
  16980. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  16981. } else if (sd->roulette_point.gold > 9) {
  16982. sd->roulette_point.gold -= 10;
  16983. sd->roulette.stage = 4;
  16984. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  16985. }
  16986. }
  16987. sd->roulette.prizeStage = sd->roulette.stage;
  16988. sd->roulette.prizeIdx = rnd()%rd.items[sd->roulette.stage];
  16989. sd->roulette.claimPrize = true;
  16990. sd->roulette.tick = gettick() + i64max( 1, ( MAX_ROULETTE_COLUMNS - sd->roulette.prizeStage - 3 ) ) * 1000;
  16991. if( rd.flag[sd->roulette.stage][sd->roulette.prizeIdx]&1 ){
  16992. result = GENERATE_ROULETTE_LOSING;
  16993. sd->roulette.stage = 0;
  16994. }else{
  16995. result = GENERATE_ROULETTE_SUCCESS;
  16996. sd->roulette.stage++;
  16997. }
  16998. }
  16999. clif_roulette_generate(sd,result,sd->roulette.prizeStage,(sd->roulette.prizeIdx == -1 ? 0 : sd->roulette.prizeIdx), sd->roulette.bonusItemID);
  17000. }
  17001. /// Request to claim a prize
  17002. /// 0A21 (CZ_RECV_ROULETTE_ITEM)
  17003. void clif_parse_roulette_item( int fd, map_session_data* sd ){
  17004. enum RECV_ROULETTE_ITEM_REQ type = RECV_ITEM_FAILED;
  17005. nullpo_retv(sd);
  17006. if (!battle_config.feature_roulette) {
  17007. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17008. return;
  17009. }
  17010. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  17011. return;
  17012. }
  17013. if (sd->roulette.claimPrize && sd->roulette.prizeIdx != -1) {
  17014. switch (clif_roulette_getitem(sd)) {
  17015. case ADDITEM_SUCCESS:
  17016. type = RECV_ITEM_SUCCESS;
  17017. break;
  17018. case ADDITEM_INVALID:
  17019. case ADDITEM_OVERITEM:
  17020. case ADDITEM_OVERAMOUNT:
  17021. type = RECV_ITEM_OVERCOUNT;
  17022. break;
  17023. case ADDITEM_OVERWEIGHT:
  17024. type = RECV_ITEM_OVERWEIGHT;
  17025. break;
  17026. case ADDITEM_STACKLIMIT:
  17027. default:
  17028. type = RECV_ITEM_FAILED;
  17029. break;
  17030. }
  17031. }
  17032. clif_roulette_recvitem_ack(sd,type);
  17033. }
  17034. /// MERGE ITEM
  17035. /**
  17036. * Acknowledge the client about item merger result
  17037. * ZC 096F <index>.W <total>.W <result>.B (ZC_ACK_MERGE_ITEM)
  17038. * @param fd
  17039. * @param sd
  17040. **/
  17041. void clif_merge_item_ack( map_session_data &sd, enum MERGE_ITEM_ACK type, uint16 index = 0, uint16 count = 0 ){
  17042. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17043. PACKET_ZC_ACK_MERGE_ITEM p = {};
  17044. p.packetType = HEADER_ZC_ACK_MERGE_ITEM;
  17045. if( type == MERGE_ITEM_SUCCESS ){
  17046. p.index = client_index( index );
  17047. }else{
  17048. p.index = index;
  17049. }
  17050. p.amount = count;
  17051. p.reason = type;
  17052. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17053. #endif
  17054. }
  17055. /**
  17056. * Check if item has the pair to be merged
  17057. * @param sd
  17058. * @param it Item
  17059. * @return True if has pair, False if not
  17060. **/
  17061. static bool clif_merge_item_has_pair(map_session_data *sd, struct item *it) {
  17062. struct item *it_;
  17063. unsigned short i;
  17064. nullpo_retr(false, sd);
  17065. 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);
  17066. if (i < MAX_INVENTORY)
  17067. return true;
  17068. return false;
  17069. }
  17070. /**
  17071. * Check if item can be merged
  17072. * @param id Item Data
  17073. * @param it Item
  17074. * @return True if can be merged, False if not
  17075. **/
  17076. static bool clif_merge_item_check(struct item_data *id, struct item *it) {
  17077. if (!id || !it)
  17078. return false;
  17079. if (id->type == IT_CASH)
  17080. return false;
  17081. if (!itemdb_isstackable2(id))
  17082. return false;
  17083. if (itemdb_isspecial(it->card[0]))
  17084. return false;
  17085. if (!it->unique_id)
  17086. return false;
  17087. return true;
  17088. }
  17089. /**
  17090. * Open available item to be merged.
  17091. * Only show 1 item in 1 process.
  17092. * ZC 096D <size>.W { <index>.W }* (ZC_MERGE_ITEM_OPEN)
  17093. * @param sd
  17094. **/
  17095. void clif_merge_item_open( map_session_data& sd ){
  17096. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17097. PACKET_ZC_MERGE_ITEM_OPEN* p = reinterpret_cast<PACKET_ZC_MERGE_ITEM_OPEN*>( packet_buffer );
  17098. p->packetType = HEADER_ZC_MERGE_ITEM_OPEN;
  17099. p->packetLen = sizeof( *p );
  17100. int n = 0;
  17101. // Get entries
  17102. for( int i = 0; i < MAX_INVENTORY; i++ ){
  17103. struct item* it = &sd.inventory.u.items_inventory[i];
  17104. if( !clif_merge_item_check( sd.inventory_data[i], it ) ){
  17105. continue;
  17106. }
  17107. if( clif_merge_item_has_pair( &sd, it ) ){
  17108. p->items[n++].index = client_index( i );
  17109. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( p->items[0] ) );
  17110. }
  17111. }
  17112. // No item need to be merged
  17113. if( n < 2 ){
  17114. clif_msg( &sd, MSI_NOT_EXIST_MERGE_ITEM );
  17115. return;
  17116. }
  17117. clif_send( p, p->packetLen, &sd.bl, SELF );
  17118. #endif
  17119. }
  17120. /**
  17121. * Process item merge request.
  17122. * 096E <size>.W { <index>.W }* (CZ_REQ_MERGE_ITEM)
  17123. * @param fd
  17124. * @param sd
  17125. **/
  17126. void clif_parse_merge_item_req( int fd, map_session_data* sd ){
  17127. const PACKET_CZ_REQ_MERGE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_MERGE_ITEM*>( RFIFOP( fd, 0 ) );
  17128. int count = ( p->packetLength - sizeof( *p ) ) / sizeof( p->indices[0] );
  17129. // No item need to be merged
  17130. if( count < 2 ){
  17131. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17132. return;
  17133. }
  17134. uint16 idx_main = server_index( p->indices[0] );
  17135. if( idx_main >= MAX_INVENTORY ){
  17136. return;
  17137. }
  17138. if( !clif_merge_item_check( sd->inventory_data[idx_main], &sd->inventory.u.items_inventory[idx_main] ) ){
  17139. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17140. return;
  17141. }
  17142. // Ensure each index only comes once
  17143. std::unordered_set<uint16> indices;
  17144. for( int i = 1; i < count; i++ ){
  17145. uint16 idx = server_index( p->indices[i] );
  17146. if( idx >= MAX_INVENTORY ){
  17147. return;
  17148. }
  17149. if( sd->inventory_data[idx] == nullptr ){
  17150. return;
  17151. }
  17152. // Check if it is the same item
  17153. if( sd->inventory_data[idx]->nameid != sd->inventory_data[idx_main]->nameid ){
  17154. return;
  17155. }
  17156. if( !clif_merge_item_check( sd->inventory_data[idx], &sd->inventory.u.items_inventory[idx] ) ){
  17157. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17158. return;
  17159. }
  17160. indices.insert( idx );
  17161. }
  17162. if( indices.empty() ){
  17163. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17164. return;
  17165. }
  17166. uint32 total_amount = sd->inventory.u.items_inventory[idx_main].amount;
  17167. for( uint16 idx : indices ){
  17168. total_amount += sd->inventory.u.items_inventory[idx].amount;
  17169. }
  17170. uint16 stack = sd->inventory_data[idx_main]->stack.amount;
  17171. if( stack == 0 ){
  17172. stack = MAX_AMOUNT;
  17173. }
  17174. if( total_amount >= stack ){
  17175. clif_merge_item_ack( *sd, MERGE_ITEM_FAILED_MAX_COUNT );
  17176. return;
  17177. }
  17178. // Merrrrge!!!!
  17179. for( uint16 idx : indices ){
  17180. uint16 amount = sd->inventory.u.items_inventory[idx].amount;
  17181. log_pick_pc( sd, LOG_TYPE_MERGE_ITEM, -amount, &sd->inventory.u.items_inventory[idx] );
  17182. memset( &sd->inventory.u.items_inventory[idx], 0, sizeof( sd->inventory.u.items_inventory[0] ) );
  17183. sd->inventory_data[idx] = nullptr;
  17184. clif_delitem( *sd, idx, amount, 0 );
  17185. }
  17186. sd->inventory.u.items_inventory[idx_main].amount = total_amount;
  17187. clif_merge_item_ack( *sd, MERGE_ITEM_SUCCESS, idx_main, total_amount );
  17188. }
  17189. /**
  17190. * Cancel item merge
  17191. * CZ 0974 (CZ_CANCEL_MERGE_ITEM)
  17192. * @param fd
  17193. * @param sd
  17194. **/
  17195. void clif_parse_merge_item_cancel(int fd, map_session_data* sd) {
  17196. return; // Nothing todo yet
  17197. }
  17198. static std::string clif_hide_name(const char* original_name)
  17199. {
  17200. std::string censored(original_name);
  17201. size_t hide = zmin(battle_config.broadcast_hide_name, censored.length() - 1);
  17202. censored.replace(censored.length() - hide, hide, hide, '*');
  17203. return censored;
  17204. }
  17205. /**
  17206. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <containerid>.W (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17207. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <srcname>.24B (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17208. * type: ITEMOBTAIN_TYPE_BOXITEM & ITEMOBTAIN_TYPE_MONSTER_ITEM "[playername] ... [sourcename] ... [itemname]" -> MsgStringTable[1629]
  17209. * type: ITEMOBTAIN_TYPE_NPC "[playername] ... [itemname]" -> MsgStringTable[1870]
  17210. **/
  17211. void clif_broadcast_obtain_special_item( const char *char_name, t_itemid nameid, t_itemid container, enum BROADCASTING_SPECIAL_ITEM_OBTAIN type ){
  17212. char name[NAME_LENGTH];
  17213. if( battle_config.broadcast_hide_name ){
  17214. std::string dispname = clif_hide_name(char_name);
  17215. safestrncpy(name, dispname.c_str(), sizeof(name));
  17216. }else{
  17217. safestrncpy( name, char_name, sizeof( name ) );
  17218. }
  17219. switch( type ){
  17220. case ITEMOBTAIN_TYPE_BOXITEM:
  17221. #if PACKETVER >= 20091201
  17222. {
  17223. PACKET_ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN_item p = {};
  17224. p.PacketType = package_item_announceType;
  17225. p.PacketLength = sizeof( p );
  17226. p.type = type;
  17227. p.ItemID = client_nameid( nameid );
  17228. p.len = NAME_LENGTH;
  17229. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17230. p.boxItemID_len = (int8)sizeof( p.BoxItemID );
  17231. p.BoxItemID = client_nameid( container );
  17232. #if PACKETVER_MAIN_NUM >= 20220518 || PACKETVER_ZERO_NUM >= 20220518
  17233. p.refineLevel_len = (int8)sizeof( p.refineLevel );
  17234. p.refineLevel = 0; // TODO: implement
  17235. #endif
  17236. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17237. }
  17238. #endif
  17239. break;
  17240. case ITEMOBTAIN_TYPE_MONSTER_ITEM:
  17241. case ITEMOBTAIN_TYPE_NPC:
  17242. {
  17243. struct packet_item_drop_announce p;
  17244. p.PacketType = item_drop_announceType;
  17245. p.type = type;
  17246. p.ItemID = client_nameid( nameid );
  17247. p.len = NAME_LENGTH;
  17248. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17249. if( type == ITEMOBTAIN_TYPE_MONSTER_ITEM ){
  17250. std::shared_ptr<s_mob_db> db = mob_db.find( container );
  17251. p.monsterNameLen = NAME_LENGTH;
  17252. safestrncpy( p.monsterName, db->name.c_str(), NAME_LENGTH );
  17253. }else{
  17254. p.monsterNameLen = 0;
  17255. p.monsterName[0] = '\0';
  17256. }
  17257. #if PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  17258. p.PacketLength = 12 + p.len + p.monsterNameLen;
  17259. #else
  17260. p.PacketLength = 8 + p.len + p.monsterNameLen;
  17261. #endif
  17262. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17263. }
  17264. break;
  17265. }
  17266. }
  17267. /// Show body view windows (ZC_DRESSROOM_OPEN).
  17268. /// 0A02 <view>.W
  17269. /// Value <flag> has the following effects:
  17270. /// 1: Open a Dress Room window.
  17271. void clif_dressing_room(map_session_data *sd, int flag) {
  17272. #if PACKETVER >= 20150513
  17273. int fd = sd->fd;
  17274. nullpo_retv(sd);
  17275. WFIFOHEAD(fd, packet_len(0xa02));
  17276. WFIFOW(fd,0) = 0xa02;
  17277. WFIFOW(fd,2) = flag;
  17278. WFIFOSET(fd, packet_len(0xa02));
  17279. #endif
  17280. }
  17281. /// Parsing a request from the client item identify oneclick (CZ_REQ_ONECLICK_ITEMIDENTIFY).
  17282. /// 0A35 <result>.W
  17283. void clif_parse_Oneclick_Itemidentify(int fd, map_session_data *sd) {
  17284. #if PACKETVER >= 20150513
  17285. short idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2, magnifier_idx;
  17286. // Ignore the request
  17287. // - Invalid item index
  17288. // - Invalid item ID or item doesn't exist
  17289. // - Item is already identified
  17290. if (idx < 0 || idx >= MAX_INVENTORY ||
  17291. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  17292. sd->inventory.u.items_inventory[idx].identify)
  17293. return;
  17294. // Ignore the request - No magnifiers in inventory
  17295. if ((magnifier_idx = pc_search_inventory(sd, ITEMID_MAGNIFIER)) == -1 &&
  17296. (magnifier_idx = pc_search_inventory(sd, ITEMID_NOVICE_MAGNIFIER)) == -1)
  17297. return;
  17298. if (pc_delitem(sd, magnifier_idx, 1, 0, 0, LOG_TYPE_OTHER) != 0) // Deleting of magnifier failed, for whatever reason...
  17299. return;
  17300. skill_identify(sd, idx);
  17301. #endif
  17302. }
  17303. /// Starts navigation to the given target on client side
  17304. void clif_navigateTo(map_session_data *sd, const char* mapname, uint16 x, uint16 y, uint8 flag, bool hideWindow, uint16 mob_id ){
  17305. #if PACKETVER >= 20111010
  17306. int fd = sd->fd;
  17307. WFIFOHEAD(fd,27);
  17308. WFIFOW(fd,0) = 0x08e2;
  17309. // How detailed will our navigation be?
  17310. if( mob_id > 0 ){
  17311. x = 0;
  17312. y = 0;
  17313. WFIFOB(fd,2) = 3; // monster with destination field
  17314. }else if( x > 0 && y > 0 ){
  17315. WFIFOB(fd,2) = 0; // with coordinates
  17316. }else{
  17317. x = 0;
  17318. y = 0;
  17319. WFIFOB(fd,2) = 1; // without coordinates(will fail if you are already on the map)
  17320. }
  17321. // Which services can be used for transportation?
  17322. WFIFOB(fd,3) = flag;
  17323. // If this flag is set, the navigation window will not be opened up
  17324. WFIFOB(fd,4) = hideWindow;
  17325. // Target map
  17326. safestrncpy( WFIFOCP(fd,5),mapname,MAP_NAME_LENGTH_EXT);
  17327. // Target x
  17328. WFIFOW(fd,21) = x;
  17329. // Target y
  17330. WFIFOW(fd,23) = y;
  17331. // Target monster ID
  17332. WFIFOW(fd,25) = mob_id;
  17333. WFIFOSET(fd,27);
  17334. #endif
  17335. }
  17336. /// Send hat effects to the client.
  17337. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17338. void clif_hat_effects( map_session_data& sd, block_list& bl, enum send_target target ){
  17339. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17340. map_session_data *tsd;
  17341. block_list* tbl;
  17342. if( target == SELF ){
  17343. tsd = BL_CAST(BL_PC,&bl);
  17344. tbl = &sd.bl;
  17345. }else{
  17346. tsd = &sd;
  17347. tbl = &bl;
  17348. }
  17349. if( tsd == nullptr ){
  17350. return;
  17351. }
  17352. if( tsd->hatEffects.empty() || map_getmapdata(tbl->m)->getMapFlag(MF_NOCOSTUME) ){
  17353. return;
  17354. }
  17355. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17356. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17357. p->packetLength = sizeof( *p );
  17358. p->aid = tsd->bl.id;
  17359. p->status = 1;
  17360. for( size_t i = 0; i < tsd->hatEffects.size(); i++ ){
  17361. p->effects[i] = tsd->hatEffects[i];
  17362. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17363. }
  17364. clif_send( p, p->packetLength, tbl, target );
  17365. #endif
  17366. }
  17367. /// Send a single hat effect to the client.
  17368. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17369. void clif_hat_effect_single( map_session_data& sd, uint16 effectId, bool enable ){
  17370. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17371. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17372. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17373. p->packetLength = sizeof( *p );
  17374. p->aid = sd.bl.id;
  17375. p->status = enable;
  17376. p->effects[0] = effectId;
  17377. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17378. clif_send( p, p->packetLength, &sd.bl, AREA );
  17379. #endif
  17380. }
  17381. /// Notify the client that a sale has started
  17382. /// 09b2 <item id>.W <remaining time>.L (ZC_NOTIFY_BARGAIN_SALE_SELLING)
  17383. void clif_sale_start( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17384. #if PACKETVER_SUPPORTS_SALES
  17385. PACKET_ZC_NOTIFY_BARGAIN_SALE_SELLING p = {};
  17386. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_SELLING;
  17387. p.itemId = client_nameid( sale_item->nameid );
  17388. p.remainingTime = (uint32)(sale_item->end - time(nullptr)); // time in S
  17389. clif_send( &p, sizeof( p ), bl, target );
  17390. #endif
  17391. }
  17392. /// Notify the client that a sale has ended
  17393. /// 09b3 <item id>.W (ZC_NOTIFY_BARGAIN_SALE_CLOSE)
  17394. void clif_sale_end( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17395. #if PACKETVER_SUPPORTS_SALES
  17396. PACKET_ZC_NOTIFY_BARGAIN_SALE_CLOSE p = {};
  17397. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_CLOSE;
  17398. p.itemId = client_nameid( sale_item->nameid );
  17399. clif_send( &p, sizeof( p ), bl, target );
  17400. #endif
  17401. }
  17402. /// Update the remaining amount of a sale item.
  17403. /// 09c4 <item id>.W <amount>.L (ZC_ACK_COUNT_BARGAIN_SALE_ITEM)
  17404. void clif_sale_amount( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17405. #if PACKETVER_SUPPORTS_SALES
  17406. PACKET_ZC_ACK_COUNT_BARGAIN_SALE_ITEM p = {};
  17407. p.packetType = HEADER_ZC_ACK_COUNT_BARGAIN_SALE_ITEM;
  17408. p.itemId = client_nameid( sale_item->nameid );
  17409. p.amount = sale_item->amount;
  17410. clif_send( &p, sizeof( p ), bl, target );
  17411. #endif
  17412. }
  17413. /// The client requested a refresh of the current remaining count of a sale item
  17414. /// 09ac <account id>.L <item id>.W (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17415. void clif_parse_sale_refresh( int fd, map_session_data* sd ){
  17416. #if PACKETVER_SUPPORTS_SALES
  17417. const PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO* p = reinterpret_cast<PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO*>( RFIFOP( fd, 0 ) );
  17418. struct sale_item_data* sale;
  17419. if( p->AID != sd->status.account_id ){
  17420. return;
  17421. }
  17422. sale = sale_find_item( p->itemId, true );
  17423. if( sale == nullptr ){
  17424. return;
  17425. }
  17426. clif_sale_amount(sale, &sd->bl, SELF);
  17427. #endif
  17428. }
  17429. /// Opens the sale administration window on the client
  17430. /// 09b5 (ZC_OPEN_BARGAIN_SALE_TOOL)
  17431. void clif_sale_open( map_session_data* sd ){
  17432. #if PACKETVER_SUPPORTS_SALES
  17433. nullpo_retv(sd);
  17434. if( sd->state.sale_open ){
  17435. return;
  17436. }
  17437. sd->state.sale_open = true;
  17438. int fd = sd->fd;
  17439. WFIFOHEAD(fd, 2);
  17440. WFIFOW(fd, 0) = 0x9b5;
  17441. WFIFOSET(fd, 2);
  17442. #endif
  17443. }
  17444. /// Client request to open the sale administration window.
  17445. /// This is sent by /limitedsale
  17446. /// 09b4 <account id>.L (CZ_OPEN_BARGAIN_SALE_TOOL)
  17447. void clif_parse_sale_open( int fd, map_session_data* sd ){
  17448. #if PACKETVER_SUPPORTS_SALES
  17449. nullpo_retv(sd);
  17450. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17451. return;
  17452. }
  17453. char command[CHAT_SIZE_MAX];
  17454. safesnprintf( command, sizeof(command), "%climitedsale", atcommand_symbol );
  17455. is_atcommand(fd, sd, command, 1);
  17456. #endif
  17457. }
  17458. /// Closes the sale administration window on the client.
  17459. /// 09bd (ZC_CLOSE_BARGAIN_SALE_TOOL)
  17460. void clif_sale_close(map_session_data* sd) {
  17461. #if PACKETVER_SUPPORTS_SALES
  17462. nullpo_retv(sd);
  17463. if( !sd->state.sale_open ){
  17464. return;
  17465. }
  17466. sd->state.sale_open = false;
  17467. int fd = sd->fd;
  17468. WFIFOHEAD(fd, 2);
  17469. WFIFOW(fd, 0) = 0x9bd;
  17470. WFIFOSET(fd, 2);
  17471. #endif
  17472. }
  17473. /// Client request to close the sale administration window.
  17474. /// 09bc (CZ_CLOSE_BARGAIN_SALE_TOOL)
  17475. void clif_parse_sale_close(int fd, map_session_data* sd) {
  17476. #if PACKETVER_SUPPORTS_SALES
  17477. nullpo_retv(sd);
  17478. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17479. return;
  17480. }
  17481. clif_sale_close(sd);
  17482. #endif
  17483. }
  17484. /// Reply to a item search request for item sale administration.
  17485. /// 09ad <result>.W <item id>.W <price>.L (ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO)
  17486. void clif_sale_search_reply( map_session_data* sd, std::shared_ptr<s_cash_item> item ){
  17487. #if PACKETVER_SUPPORTS_SALES
  17488. PACKET_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO p = {};
  17489. p.packetType = HEADER_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO;
  17490. if( item != nullptr ){
  17491. p.result = 0;
  17492. p.itemId = client_nameid( item->nameid );
  17493. p.price = item->price;
  17494. }else{
  17495. p.result = 1;
  17496. p.itemId = 0;
  17497. p.price = 0;
  17498. }
  17499. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17500. #endif
  17501. }
  17502. /// Search request for an item sale administration.
  17503. /// 09ac <length>.W <account id>.L <item name>.?B (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17504. void clif_parse_sale_search( int fd, map_session_data* sd ){
  17505. #if PACKETVER_SUPPORTS_SALES
  17506. char item_name[ITEM_NAME_LENGTH];
  17507. nullpo_retv(sd);
  17508. if( RFIFOL(fd, 4) != sd->status.account_id ){
  17509. return;
  17510. }
  17511. if( !sd->state.sale_open ){
  17512. return;
  17513. }
  17514. safestrncpy( item_name, RFIFOCP(fd, 8), min(RFIFOW(fd, 2) - 7, ITEM_NAME_LENGTH) );
  17515. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  17516. // not found
  17517. if( id == nullptr ){
  17518. clif_sale_search_reply( sd, nullptr );
  17519. return;
  17520. }
  17521. clif_sale_search_reply( sd, cash_shop_db.findItemInTab( CASHSHOP_TAB_SALE, id->nameid ) );
  17522. #endif
  17523. }
  17524. /// Reply if an item was successfully put on sale or not.
  17525. /// 09af <result>.W (ZC_ACK_APPLY_BARGAIN_SALE_ITEM)
  17526. void clif_sale_add_reply( map_session_data* sd, enum e_sale_add_result result ){
  17527. #if PACKETVER_SUPPORTS_SALES
  17528. int fd = sd->fd;
  17529. WFIFOHEAD(fd, 4);
  17530. WFIFOW(fd, 0) = 0x9af;
  17531. WFIFOW(fd, 2) = (uint16)result;
  17532. WFIFOSET(fd, 4);
  17533. #endif
  17534. }
  17535. /// A client request to put an item on sale.
  17536. /// 09ae <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.B (CZ_REQ_APPLY_BARGAIN_SALE_ITEM)
  17537. /// 0a3d <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.W (CZ_REQ_APPLY_BARGAIN_SALE_ITEM2)
  17538. void clif_parse_sale_add( int fd, map_session_data* sd ){
  17539. #if PACKETVER_SUPPORTS_SALES
  17540. nullpo_retv( sd );
  17541. const PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17542. if( p->AID != sd->status.account_id ){
  17543. return;
  17544. }
  17545. if( !sd->state.sale_open ){
  17546. return;
  17547. }
  17548. time_t endTime = p->startTime + p->hours * 60 * 60;
  17549. clif_sale_add_reply( sd, sale_add_item( p->itemId, p->amount, p->startTime, endTime ) );
  17550. #endif
  17551. }
  17552. /// Reply to an item removal from sale.
  17553. /// 09b1 <result>.W (ZC_ACK_REMOVE_BARGAIN_SALE_ITEM)
  17554. void clif_sale_remove_reply( map_session_data* sd, bool failed ){
  17555. #if PACKETVER_SUPPORTS_SALES
  17556. int fd = sd->fd;
  17557. WFIFOHEAD(fd, 4);
  17558. WFIFOW(fd, 0) = 0x9b1;
  17559. WFIFOW(fd, 2) = failed;
  17560. WFIFOSET(fd, 4);
  17561. #endif
  17562. }
  17563. /// Request to remove an item from sale.
  17564. /// 09b0 <account id>.L <item id>.W (CZ_REQ_REMOVE_BARGAIN_SALE_ITEM)
  17565. void clif_parse_sale_remove( int fd, map_session_data* sd ){
  17566. #if PACKETVER_SUPPORTS_SALES
  17567. nullpo_retv( sd );
  17568. const PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17569. if( p->AID != sd->status.account_id ){
  17570. return;
  17571. }
  17572. if( !sd->state.sale_open ){
  17573. return;
  17574. }
  17575. clif_sale_remove_reply( sd, !sale_remove_item( p->itemId ) );
  17576. #endif
  17577. }
  17578. /// Achievement System
  17579. /// Author: Luxuri, Aleos
  17580. /**
  17581. * Sends all achievement data to the client (ZC_ALL_ACH_LIST).
  17582. * 0a23 <packetLength>.W <ACHCount>.L <ACHPoint>.L
  17583. */
  17584. void clif_achievement_list_all(map_session_data *sd)
  17585. {
  17586. nullpo_retv(sd);
  17587. if (!battle_config.feature_achievement) {
  17588. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17589. return;
  17590. }
  17591. int count = sd->achievement_data.count; // All achievements should be sent to the client
  17592. if (count == 0)
  17593. return;
  17594. int len = (50 * count) + 22;
  17595. int fd = sd->fd;
  17596. int *info = achievement_level(sd, true);
  17597. WFIFOHEAD(fd,len);
  17598. WFIFOW(fd, 0) = 0xa23;
  17599. WFIFOW(fd, 2) = len;
  17600. WFIFOL(fd, 4) = count; // Amount of achievements the player has in their list (started/completed)
  17601. WFIFOL(fd, 8) = sd->achievement_data.total_score; // Top number
  17602. WFIFOW(fd, 12) = sd->achievement_data.level; // Achievement Level (gold circle)
  17603. WFIFOL(fd, 14) = info[0]; // Achievement EXP (left number in bar)
  17604. WFIFOL(fd, 18) = info[1]; // Achievement EXP TNL (right number in bar)
  17605. for (int i = 0; i < count; i++) {
  17606. WFIFOL(fd, i * 50 + 22) = (uint32)sd->achievement_data.achievements[i].achievement_id;
  17607. WFIFOB(fd, i * 50 + 26) = (uint32)sd->achievement_data.achievements[i].completed > 0;
  17608. for (int j = 0; j < MAX_ACHIEVEMENT_OBJECTIVES; j++)
  17609. WFIFOL(fd, (i * 50) + 27 + (j * 4)) = (uint32)sd->achievement_data.achievements[i].count[j];
  17610. WFIFOL(fd, i * 50 + 67) = (uint32)sd->achievement_data.achievements[i].completed;
  17611. WFIFOB(fd, i * 50 + 71) = sd->achievement_data.achievements[i].rewarded > 0;
  17612. }
  17613. WFIFOSET(fd, len);
  17614. }
  17615. /**
  17616. * Sends a single achievement's data to the client (ZC_ACH_UPDATE).
  17617. * 0a24 <ACHPoint>.L
  17618. */
  17619. void clif_achievement_update(map_session_data *sd, struct achievement *ach, int count)
  17620. {
  17621. nullpo_retv(sd);
  17622. if (!battle_config.feature_achievement) {
  17623. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17624. return;
  17625. }
  17626. int fd = sd->fd;
  17627. int *info = achievement_level(sd, true);
  17628. WFIFOHEAD(fd, packet_len(0xa24));
  17629. WFIFOW(fd, 0) = 0xa24;
  17630. WFIFOL(fd, 2) = sd->achievement_data.total_score; // Total Achievement Points (top of screen)
  17631. WFIFOW(fd, 6) = sd->achievement_data.level; // Achievement Level (gold circle)
  17632. WFIFOL(fd, 8) = info[0]; // Achievement EXP (left number in bar)
  17633. WFIFOL(fd, 12) = info[1]; // Achievement EXP TNL (right number in bar)
  17634. if (ach) {
  17635. WFIFOL(fd, 16) = ach->achievement_id; // Achievement ID
  17636. WFIFOB(fd, 20) = ach->completed > 0; // Is it complete?
  17637. for (int i = 0; i < MAX_ACHIEVEMENT_OBJECTIVES; i++)
  17638. WFIFOL(fd, 21 + (i * 4)) = (uint32)ach->count[i]; // 1~10 pre-reqs
  17639. WFIFOL(fd, 61) = (uint32)ach->completed; // Epoch time
  17640. WFIFOB(fd, 65) = ach->rewarded > 0; // Got reward?
  17641. } else
  17642. memset(WFIFOP(fd, 16), 0, 40);
  17643. WFIFOSET(fd, packet_len(0xa24));
  17644. }
  17645. /**
  17646. * Checks if an achievement reward can be rewarded (CZ_REQ_ACH_REWARD).
  17647. * 0a25 <achievementID>.L
  17648. */
  17649. void clif_parse_AchievementCheckReward(int fd, map_session_data *sd)
  17650. {
  17651. nullpo_retv(sd);
  17652. if( sd->achievement_data.save )
  17653. intif_achievement_save(sd);
  17654. achievement_check_reward(sd, RFIFOL(fd,2));
  17655. }
  17656. /**
  17657. * Returns the result of achievement_check_reward (ZC_REQ_ACH_REWARD_ACK).
  17658. * 0a26 <result>.W <achievementID>.L
  17659. */
  17660. void clif_achievement_reward_ack(int fd, unsigned char result, int achievement_id)
  17661. {
  17662. WFIFOHEAD(fd, packet_len(0xa26));
  17663. WFIFOW(fd, 0) = 0xa26;
  17664. WFIFOB(fd, 2) = result;
  17665. WFIFOL(fd, 3) = achievement_id;
  17666. WFIFOSET(fd, packet_len(0xa26));
  17667. }
  17668. /// Process the pet evolution request
  17669. /// 09fb <packetType>.W <packetLength>.W <evolutionPetEggITID>.W (CZ_PET_EVOLUTION)
  17670. void clif_parse_pet_evolution( int fd, map_session_data *sd ){
  17671. #if PACKETVER >= 20141008
  17672. const PACKET_CZ_PET_EVOLUTION* p = reinterpret_cast<PACKET_CZ_PET_EVOLUTION*>( RFIFOP( fd, 0 ) );
  17673. auto pet = pet_db_search( p->EvolvedPetEggID, PET_EGG );
  17674. if (!pet) {
  17675. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_NOT_PETEGG);
  17676. return;
  17677. }
  17678. pet_evolution(sd, pet->class_);
  17679. #endif
  17680. }
  17681. /// Sends the result of the evolution to the client.
  17682. /// 09fc <result>.L (ZC_PET_EVOLUTION_RESULT)
  17683. void clif_pet_evolution_result( map_session_data* sd, e_pet_evolution_result result ){
  17684. #if PACKETVER >= 20141008
  17685. int fd = sd->fd;
  17686. WFIFOHEAD(fd, packet_len(0x9fc));
  17687. WFIFOW(fd, 0) = 0x9fc;
  17688. WFIFOL(fd, 2) = static_cast<uint32>(result);
  17689. WFIFOSET(fd, packet_len(0x9fc));
  17690. #endif
  17691. }
  17692. /*
  17693. * This packet is sent by /changedress or /nocosplay
  17694. *
  17695. * 0ae8
  17696. */
  17697. void clif_parse_changedress( int fd, map_session_data* sd ){
  17698. #if PACKETVER >= 20180103
  17699. char command[CHAT_SIZE_MAX];
  17700. safesnprintf( command, sizeof(command), "%cchangedress", atcommand_symbol );
  17701. is_atcommand( fd, sd, command, 1 );
  17702. #endif
  17703. }
  17704. /// Opens an UI window of the given type and initializes it with the given data
  17705. /// 0AE2 <type>.B <data>.L
  17706. void clif_ui_open( map_session_data& sd, enum out_ui_type ui_type, int32 data ){
  17707. #if PACKETVER >= 20151202
  17708. // If the UI requires state tracking
  17709. switch( ui_type ){
  17710. case OUT_UI_STYLIST:
  17711. sd.state.stylist_open = true;
  17712. break;
  17713. case OUT_UI_ENCHANTGRADE:
  17714. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  17715. sd.state.enchantgrade_open = true;
  17716. break;
  17717. #else
  17718. return;
  17719. #endif
  17720. }
  17721. PACKET_ZC_UI_OPEN p = {};
  17722. p.PacketType = HEADER_ZC_UI_OPEN;
  17723. p.UIType = ui_type;
  17724. #if PACKETVER >= 20171122
  17725. p.data = data;
  17726. #endif
  17727. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17728. #endif
  17729. }
  17730. /// Request to open an UI window of the given type
  17731. /// 0A68 <type>.B
  17732. void clif_parse_open_ui( int fd, map_session_data* sd ){
  17733. switch( RFIFOB(fd,2) ){
  17734. case IN_UI_ATTENDANCE:
  17735. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  17736. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 791 ), false, SELF ); // You are not allowed to use the attendance system.
  17737. }else if( pc_attendance_enabled() ){
  17738. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  17739. }else{
  17740. clif_msg_color( sd, MSI_CHECK_ATTENDANCE_NOT_EVENT, color_table[COLOR_RED] );
  17741. }
  17742. break;
  17743. #if PACKETVER >= 20160316
  17744. case IN_UI_MACRO_REGISTER:
  17745. clif_ui_open(*sd, OUT_UI_CAPTCHA, 0);
  17746. break;
  17747. case IN_UI_MACRO_DETECTOR:
  17748. clif_ui_open(*sd, OUT_UI_MACRO, 0);
  17749. break;
  17750. #endif
  17751. }
  17752. }
  17753. /// Response for attedance request
  17754. /// 0AF0 <unknown>.L <data>.L
  17755. void clif_attendence_response( map_session_data *sd, int32 data ){
  17756. nullpo_retv(sd);
  17757. int fd = sd->fd;
  17758. WFIFOHEAD(fd,packet_len(0xAF0));
  17759. WFIFOW(fd,0) = 0xAF0;
  17760. WFIFOL(fd,2) = 0;
  17761. WFIFOL(fd,6) = data;
  17762. WFIFOSET(fd,packet_len(0xAF0));
  17763. }
  17764. /// Request from the client to retrieve today's attendance reward
  17765. /// 0AEF
  17766. void clif_parse_attendance_request( int fd, map_session_data* sd ){
  17767. pc_attendance_claim_reward(sd);
  17768. }
  17769. /// Send out the percentage of weight that causes it to be displayed in red.
  17770. /// 0ADE <percentage>.L
  17771. void clif_weight_limit( map_session_data* sd ){
  17772. #if PACKETVER >= 20171025
  17773. nullpo_retv(sd);
  17774. int fd = sd->fd;
  17775. WFIFOHEAD(fd, packet_len(0xADE));
  17776. WFIFOW(fd, 0) = 0xADE;
  17777. #ifdef RENEWAL
  17778. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate_renewal;
  17779. #else
  17780. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate;
  17781. #endif
  17782. WFIFOSET(fd, packet_len(0xADE));
  17783. #endif
  17784. }
  17785. enum e_private_airship_response : uint32{
  17786. PRIVATEAIRSHIP_OK,
  17787. PRIVATEAIRSHIP_RETRY,
  17788. PRIVATEAIRSHIP_ITEM_NOT_ENOUGH,
  17789. PRIVATEAIRSHIP_DESTINATION_MAP_INVALID,
  17790. PRIVATEAIRSHIP_SOURCE_MAP_INVALID,
  17791. PRIVATEAIRSHIP_ITEM_UNAVAILABLE
  17792. };
  17793. /// Send out the response to a private airship request
  17794. /// 0A4A <response>.L
  17795. void clif_private_airship_response( map_session_data* sd, enum e_private_airship_response response ){
  17796. #if PACKETVER >= 20180321
  17797. nullpo_retv( sd );
  17798. int fd = sd->fd;
  17799. WFIFOHEAD( fd, packet_len( 0xA4A ) );
  17800. WFIFOW( fd, 0 ) = 0xA4A;
  17801. WFIFOL( fd, 2 ) = response;
  17802. WFIFOSET( fd, packet_len( 0xA4A ) );
  17803. #endif
  17804. }
  17805. /// Parses a request for a private airship
  17806. /// 0A49 <mapname>.16B <itemid>.W
  17807. void clif_parse_private_airship_request( int fd, map_session_data* sd ){
  17808. #if PACKETVER >= 20180321
  17809. // Check if the feature is enabled
  17810. if( !battle_config.feature_privateairship ){
  17811. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 792 ), false, SELF ); // The private airship system is disabled.
  17812. return;
  17813. }
  17814. // Check if the player is allowed to warp from the source map
  17815. if( !map_getmapflag( sd->bl.m, MF_PRIVATEAIRSHIP_SOURCE ) ){
  17816. clif_private_airship_response( sd, PRIVATEAIRSHIP_SOURCE_MAP_INVALID );
  17817. return;
  17818. }
  17819. const PACKET_CZ_PRIVATE_AIRSHIP_REQUEST* p = reinterpret_cast<PACKET_CZ_PRIVATE_AIRSHIP_REQUEST*>( RFIFOP( fd, 0 ) );
  17820. char mapname[MAP_NAME_LENGTH_EXT];
  17821. safestrncpy( mapname, p->mapName, MAP_NAME_LENGTH_EXT );
  17822. int16 mapindex = mapindex_name2id( mapname );
  17823. // Check if we know the mapname
  17824. if( mapindex < 0 ){
  17825. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  17826. return;
  17827. }
  17828. int16 mapid = map_mapindex2mapid( mapindex );
  17829. // Check if the map is available on this server
  17830. if( mapid < 0 ){
  17831. // TODO: add multi map-server support, cant validate the mapflags of the other server
  17832. return;
  17833. }
  17834. // Check if the player tried to warp to the map he is on
  17835. if( sd->bl.m == mapid ){
  17836. // This is blocked by the client, but just to be sure
  17837. return;
  17838. }
  17839. // This can only be a hack, so we prevent it
  17840. if( map_getmapdata( mapid )->instance_id ){
  17841. // Ignore requests to warp directly into a running instance
  17842. return;
  17843. }
  17844. // Check if the player is allowed to warp to the target map
  17845. if( !map_getmapflag( mapid, MF_PRIVATEAIRSHIP_DESTINATION ) ){
  17846. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  17847. return;
  17848. }
  17849. t_itemid item_id = p->ItemID;
  17850. // Check if the item sent by the client is known to us
  17851. if( !itemdb_group.item_exists(IG_PRIVATE_AIRSHIP, item_id) ){
  17852. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_UNAVAILABLE );
  17853. return;
  17854. }
  17855. int idx = pc_search_inventory( sd, item_id );
  17856. // Check if the player has the item at all
  17857. if( idx < 0 ){
  17858. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_NOT_ENOUGH );
  17859. return;
  17860. }
  17861. // Delete the chosen item
  17862. if( pc_delitem( sd, idx, 1, 0, 0, LOG_TYPE_PRIVATE_AIRSHIP ) ){
  17863. clif_private_airship_response( sd, PRIVATEAIRSHIP_RETRY );
  17864. return;
  17865. }
  17866. // Warp the player to a random spot on the destination map
  17867. pc_setpos( sd, mapindex, 0, 0, CLR_TELEPORT );
  17868. #endif
  17869. }
  17870. /// Sends out the usage history of the guild storage
  17871. /// 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
  17872. /// { <card item id>.W }*4 <name>.24B <time>.24B <attribute>.B }*count (ZC_ACK_GUILDSTORAGE_LOG)
  17873. void clif_guild_storage_log( map_session_data& sd, std::vector<struct guild_log_entry>& log, enum e_guild_storage_log result ){
  17874. #if PACKETVER >= 20140205
  17875. PACKET_ZC_ACK_GUILDSTORAGE_LOG* p = reinterpret_cast<PACKET_ZC_ACK_GUILDSTORAGE_LOG*>( packet_buffer );
  17876. p->packetType = HEADER_ZC_ACK_GUILDSTORAGE_LOG;
  17877. p->PacketLength = sizeof( *p );
  17878. p->result = result;
  17879. if( result == GUILDSTORAGE_LOG_FINAL_SUCCESS ){
  17880. p->amount = (uint16)log.size();
  17881. for( int i = 0; i < log.size(); i++ ){
  17882. PACKET_ZC_ACK_GUILDSTORAGE_LOG_sub& item = p->items[i];
  17883. struct guild_log_entry& entry = log[i];
  17884. item.id = entry.id;
  17885. item.itemId = client_nameid( entry.item.nameid );
  17886. item.amount = (uint16)( entry.amount > 0 ? entry.amount : ( entry.amount * -1 ) );
  17887. item.action = entry.amount > 0; // action = true(put), false(get)
  17888. item.refine = entry.item.refine;
  17889. item.uniqueId = entry.item.unique_id;
  17890. item.IsIdentified = entry.item.identify;
  17891. item.itemType = itemtype( entry.item.nameid );
  17892. clif_addcards( &item.slot, &entry.item );
  17893. safestrncpy( item.name, entry.name, NAME_LENGTH );
  17894. safestrncpy( item.time, entry.time, NAME_LENGTH );
  17895. item.attribute = entry.item.attribute;
  17896. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof ( item ) );
  17897. }
  17898. }else{
  17899. p->amount = 0;
  17900. }
  17901. clif_send( p, p->PacketLength, &sd.bl, SELF );
  17902. #endif
  17903. }
  17904. /// Activates the client camera info or updates the client camera with the given values.
  17905. /// 0A78 <type>.B <range>.F <rotation>.F <latitude>.F
  17906. void clif_camerainfo( map_session_data* sd, bool show, float range, float rotation, float latitude ){
  17907. #if PACKETVER >= 20160525
  17908. int fd = sd->fd;
  17909. WFIFOHEAD(fd, packet_len(0xa78));
  17910. WFIFOW(fd, 0) = 0xa78;
  17911. WFIFOB(fd, 2) = show;
  17912. WFIFOF(fd, 3) = range;
  17913. WFIFOF(fd, 7) = rotation;
  17914. WFIFOF(fd, 11) = latitude;
  17915. WFIFOSET(fd, packet_len(0xa78));
  17916. #endif
  17917. }
  17918. /// Activates or deactives the client camera info or updates the camera settings.
  17919. /// This packet is triggered by /viewpointvalue or /setcamera
  17920. /// 0A77 <type>.B <range>.F <rotation>.F <latitude>.F
  17921. void clif_parse_camerainfo( int fd, map_session_data* sd ){
  17922. char command[CHAT_SIZE_MAX];
  17923. // /viewpointvalue
  17924. if( RFIFOB( fd, 2 ) == 1 ){
  17925. safesnprintf( command, sizeof( command ), "%ccamerainfo", atcommand_symbol );
  17926. // /setcamera
  17927. }else{
  17928. safesnprintf( command, sizeof( command ), "%ccamerainfo %03.03f %03.03f %03.03f", atcommand_symbol, RFIFOF( fd, 3 ), RFIFOF( fd, 7 ), RFIFOF( fd, 11 ) );
  17929. }
  17930. is_atcommand( fd, sd, command, 1 );
  17931. }
  17932. /// Send the full list of items in the equip switch window
  17933. /// 0a9b <length>.W { <index>.W <position>.L }*
  17934. void clif_equipswitch_list( map_session_data* sd ){
  17935. #if PACKETVER >= 20170208
  17936. int fd = sd->fd;
  17937. int offset, i, position;
  17938. WFIFOW(fd, 0) = 0xa9b;
  17939. for( i = 0, offset = 4, position = 0; i < EQI_MAX; i++ ){
  17940. short index = sd->equip_switch_index[i];
  17941. if( index >= 0 && !( position & equip_bitmask[i] ) ){
  17942. WFIFOW(fd, offset) = index + 2;
  17943. WFIFOL(fd, offset + 2) = sd->inventory.u.items_inventory[index].equipSwitch;
  17944. position |= sd->inventory.u.items_inventory[index].equipSwitch;
  17945. offset += 6;
  17946. }
  17947. }
  17948. WFIFOW(fd, 2) = offset;
  17949. WFIFOSET(fd, offset);
  17950. #endif
  17951. }
  17952. /// Acknowledgement for removing an equip to the equip switch window
  17953. /// 0a9a <index>.W <position.>.L <failure>.W
  17954. void clif_equipswitch_remove( map_session_data* sd, uint16 index, uint32 pos, bool failed ){
  17955. #if PACKETVER >= 20170208
  17956. int fd = sd->fd;
  17957. WFIFOHEAD(fd, packet_len(0xa9a));
  17958. WFIFOW(fd, 0) = 0xa9a;
  17959. WFIFOW(fd, 2) = index + 2;
  17960. WFIFOL(fd, 4) = pos;
  17961. WFIFOW(fd, 8) = failed;
  17962. WFIFOSET(fd, packet_len(0xa9a));
  17963. #endif
  17964. }
  17965. /// Request to remove an equip from the equip switch window
  17966. /// 0a99 <index>.W <position>.L <= 20170502
  17967. /// 0a99 <index>.W
  17968. void clif_parse_equipswitch_remove( int fd, map_session_data* sd ){
  17969. #if PACKETVER >= 20170208
  17970. uint16 index = RFIFOW(fd, 2) - 2;
  17971. bool removed = false;
  17972. if( !battle_config.feature_equipswitch ){
  17973. return;
  17974. }
  17975. // Check if the index is valid
  17976. if( index >= MAX_INVENTORY ){
  17977. return;
  17978. }
  17979. pc_equipswitch_remove( sd, index );
  17980. #endif
  17981. }
  17982. /// Acknowledgement for adding an equip to the equip switch window
  17983. /// 0a98 <index>.W <position.>.L <flag>.L <= 20170502
  17984. /// 0a98 <index>.W <position.>.L <flag>.W
  17985. void clif_equipswitch_add( map_session_data* sd, uint16 index, uint32 pos, uint8 flag ){
  17986. #if PACKETVER >= 20170208
  17987. int fd = sd->fd;
  17988. WFIFOHEAD(fd, packet_len(0xa98));
  17989. WFIFOW(fd, 0) = 0xa98;
  17990. WFIFOW(fd, 2) = index + 2;
  17991. WFIFOL(fd, 4) = pos;
  17992. #if PACKETVER <= 20170502
  17993. WFIFOL(fd, 8) = flag;
  17994. #else
  17995. WFIFOW(fd, 8) = flag;
  17996. #endif
  17997. WFIFOSET(fd,packet_len(0xa98));
  17998. #endif
  17999. }
  18000. /// Request to add an equip to the equip switch window
  18001. /// 0a97 <index>.W <position>.L
  18002. void clif_parse_equipswitch_add( int fd, map_session_data* sd ){
  18003. #if PACKETVER >= 20170208
  18004. uint16 index = RFIFOW(fd, 2) - 2;
  18005. uint32 position = RFIFOL(fd, 4);
  18006. if( !battle_config.feature_equipswitch ){
  18007. return;
  18008. }
  18009. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  18010. return;
  18011. }
  18012. if( sd->state.trading || sd->npc_shopid ){
  18013. clif_equipswitch_add( sd, index, position, ITEM_EQUIP_ACK_OK );
  18014. return;
  18015. }
  18016. if( sd->inventory_data[index]->type == IT_AMMO ){
  18017. position = EQP_AMMO;
  18018. }
  18019. pc_equipitem( sd, index, position, true );
  18020. #endif
  18021. }
  18022. /// Acknowledgement packet for the full equip switch
  18023. /// 0a9d <failed>.W
  18024. void clif_equipswitch_reply( map_session_data* sd, bool failed ){
  18025. #if PACKETVER >= 20170208
  18026. int fd = sd->fd;
  18027. WFIFOHEAD(fd, packet_len(0xa9d));
  18028. WFIFOW(fd, 0) = 0xa9d;
  18029. WFIFOW(fd, 2) = failed;
  18030. WFIFOSET(fd, packet_len(0xa9d));
  18031. #endif
  18032. }
  18033. /// Request to do a full equip switch
  18034. /// 0a9c
  18035. void clif_parse_equipswitch_request( int fd, map_session_data* sd ){
  18036. #if PACKETVER >= 20170208
  18037. int i;
  18038. t_tick tick = gettick();
  18039. uint16 skill_id = ALL_EQSWITCH, skill_lv = 1;
  18040. if( DIFF_TICK(tick, sd->equipswitch_tick) < 0 ) {
  18041. // Client will not let you send a request
  18042. return;
  18043. }
  18044. sd->equipswitch_tick = tick + skill_get_cooldown( skill_id, skill_lv );
  18045. if( !battle_config.feature_equipswitch ){
  18046. return;
  18047. }
  18048. ARR_FIND( 0, EQI_MAX, i, sd->equip_switch_index[i] >= 0 );
  18049. if( i == EQI_MAX ){
  18050. // client will show: "There is no item to replace." and should not even come here
  18051. clif_equipswitch_reply( sd, false );
  18052. return;
  18053. }
  18054. if( pc_issit(sd) && !pc_setstand( sd, false ) ){
  18055. return;
  18056. }
  18057. clif_parse_skill_toid( sd, skill_id, skill_lv, sd->bl.id );
  18058. #endif
  18059. }
  18060. /// Request to do a single equip switch
  18061. /// 0ace <index>.W
  18062. void clif_parse_equipswitch_request_single( int fd, map_session_data* sd ){
  18063. #if PACKETVER >= 20170502
  18064. uint16 index = RFIFOW(fd, 2) - 2;
  18065. if( !battle_config.feature_equipswitch ){
  18066. return;
  18067. }
  18068. // Check if the index is valid
  18069. if( index >= MAX_INVENTORY ){
  18070. return;
  18071. }
  18072. // Check if the item exists
  18073. if (sd->inventory_data[index] == nullptr)
  18074. return;
  18075. // Check if the item was already added to equip switch
  18076. if( sd->inventory.u.items_inventory[index].equipSwitch ){
  18077. if( sd->npc_id ){
  18078. #ifdef RENEWAL
  18079. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) ){
  18080. clif_msg( sd, MSI_BUSY);
  18081. return;
  18082. }
  18083. #endif
  18084. if( !sd->npc_item_flag ){
  18085. return;
  18086. }
  18087. }
  18088. pc_equipswitch( sd, index );
  18089. return;
  18090. }
  18091. pc_equipitem( sd, index, pc_equippoint(sd, index), true );
  18092. #endif
  18093. }
  18094. void clif_parse_StartUseSkillToId( int fd, map_session_data* sd ){
  18095. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18096. const PACKET_CZ_USE_SKILL_START* p = reinterpret_cast<PACKET_CZ_USE_SKILL_START*>( RFIFOP( fd, 0 ) );
  18097. // Only rolling cutter is supported for now
  18098. if( p->skillId != GC_ROLLINGCUTTER ){
  18099. return;
  18100. }
  18101. // Already running - cant happen on officials, since only one skill is supported
  18102. if( sd->skill_keep_using.skill_id != 0 ){
  18103. return;
  18104. }
  18105. sd->skill_keep_using.tid = INVALID_TIMER;
  18106. sd->skill_keep_using.skill_id = p->skillId;
  18107. sd->skill_keep_using.level = p->skillLv;
  18108. sd->skill_keep_using.target = p->targetId;
  18109. clif_parse_skill_toid( sd, sd->skill_keep_using.skill_id, sd->skill_keep_using.level, sd->skill_keep_using.target );
  18110. #endif
  18111. }
  18112. void clif_parse_StopUseSkillToId( int fd, map_session_data* sd ){
  18113. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18114. const PACKET_CZ_USE_SKILL_END* p = reinterpret_cast<PACKET_CZ_USE_SKILL_END*>( RFIFOP( fd, 0 ) );
  18115. // Not running
  18116. if( sd->skill_keep_using.skill_id == 0 ){
  18117. return;
  18118. }
  18119. #if 0
  18120. // Hack
  18121. if( p->skillId != sd->skill_keep_using.skill_id ){
  18122. return;
  18123. }
  18124. #endif
  18125. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  18126. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  18127. sd->skill_keep_using.tid = INVALID_TIMER;
  18128. }
  18129. sd->skill_keep_using.skill_id = 0;
  18130. sd->skill_keep_using.level = 0;
  18131. sd->skill_keep_using.target = 0;
  18132. #endif
  18133. }
  18134. void clif_ping( map_session_data* sd ){
  18135. #if PACKETVER_MAIN_NUM >= 20190213 || PACKETVER_RE_NUM >= 20190213 || PACKETVER_ZERO_NUM >= 20190130
  18136. nullpo_retv( sd );
  18137. int fd = sd->fd;
  18138. if( !session_isActive( fd ) ){
  18139. return;
  18140. }
  18141. PACKET_ZC_PING_LIVE p = {};
  18142. p.packetType = HEADER_ZC_PING_LIVE;
  18143. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18144. #endif
  18145. }
  18146. int clif_ping_timer_sub( map_session_data *sd, va_list ap ){
  18147. nullpo_ret( sd );
  18148. int fd = sd->fd;
  18149. if( !session_isActive( fd ) ){
  18150. return 0;
  18151. }
  18152. t_tick tick = va_arg( ap, t_tick );
  18153. if( session[fd]->wdata_tick + battle_config.ping_time < tick ){
  18154. clif_ping( sd );
  18155. }
  18156. return 0;
  18157. }
  18158. TIMER_FUNC( clif_ping_timer ){
  18159. map_foreachpc( clif_ping_timer_sub, gettick() );
  18160. return 0;
  18161. }
  18162. /**
  18163. * Opens the refine UI on the designated client.
  18164. * 0aa0
  18165. */
  18166. void clif_refineui_open( map_session_data* sd ){
  18167. #if PACKETVER >= 20161012
  18168. nullpo_retv( sd );
  18169. PACKET_ZC_OPEN_REFINING_UI p = {};
  18170. p.packetType = HEADER_ZC_OPEN_REFINING_UI;
  18171. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18172. sd->state.refineui_open = true;
  18173. #endif
  18174. }
  18175. /**
  18176. * Parses cancel/close of the refine UI on client side.
  18177. * 0aa4
  18178. */
  18179. void clif_parse_refineui_close( int fd, map_session_data* sd ){
  18180. #if PACKETVER >= 20161012
  18181. sd->state.refineui_open = false;
  18182. #endif
  18183. }
  18184. /**
  18185. * Adds the selected item into the refine UI and sends the possible materials
  18186. * to the client.
  18187. * 0aa2 <length>.W <index>.W <catalyst count>.B { <material>.W <chance>.B <price>.L }*
  18188. */
  18189. void clif_refineui_info( map_session_data* sd, uint16 index ){
  18190. #if PACKETVER >= 20161012
  18191. int fd = sd->fd;
  18192. // Get the item db reference
  18193. struct item_data* id = sd->inventory_data[index];
  18194. // No item data was found
  18195. if( id == nullptr ){
  18196. return;
  18197. }
  18198. // Check the inventory
  18199. struct item* item = &sd->inventory.u.items_inventory[index];
  18200. // No item was found at the given index
  18201. if( item == nullptr ){
  18202. return;
  18203. }
  18204. // Check if the item is identified
  18205. if( !item->identify ){
  18206. return;
  18207. }
  18208. // Check if the item is broken
  18209. if( item->attribute ){
  18210. return;
  18211. }
  18212. PACKET_ZC_REFINING_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_REFINING_MATERIAL_LIST*>( packet_buffer );
  18213. p->packetType = HEADER_ZC_REFINING_MATERIAL_LIST;
  18214. p->packetLength = sizeof( *p );
  18215. p->itemIndex = client_index( index );
  18216. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18217. // No possibilities were found
  18218. if( info == nullptr ){
  18219. p->blacksmithBlessing = 0;
  18220. }else{
  18221. p->blacksmithBlessing = static_cast<decltype(p->blacksmithBlessing)>( info->blessing_amount );
  18222. for( uint16 i = REFINE_COST_NORMAL, count = 0; i < REFINE_COST_MAX; i++ ){
  18223. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, i );
  18224. if( cost == nullptr ){
  18225. continue;
  18226. }
  18227. PACKET_ZC_REFINING_MATERIAL_LIST_SUB& entry = p->req[count];
  18228. entry.itemId = client_nameid( cost->nameid );
  18229. entry.chance = static_cast<decltype(entry.chance)>( cost->chance / 100 );
  18230. entry.zeny = cost->zeny;
  18231. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  18232. count++;
  18233. }
  18234. }
  18235. clif_send( p, p->packetLength, &sd->bl, SELF );
  18236. #endif
  18237. }
  18238. /**
  18239. * Client request to add an item to the refine UI.
  18240. * 0aa1 <index>.W
  18241. */
  18242. void clif_parse_refineui_add( int fd, map_session_data* sd ){
  18243. #if PACKETVER >= 20161012
  18244. const PACKET_CZ_REFINING_SELECT_ITEM* p = reinterpret_cast<PACKET_CZ_REFINING_SELECT_ITEM*>( RFIFOP( fd, 0 ) );
  18245. uint16 index = server_index( p->index );
  18246. // Check if the refine UI is open
  18247. if( !sd->state.refineui_open ){
  18248. return;
  18249. }
  18250. // Check if the index is valid
  18251. if( index >= MAX_INVENTORY ){
  18252. return;
  18253. }
  18254. // Send out the requirements for the refine process
  18255. clif_refineui_info( sd, index );
  18256. #endif
  18257. }
  18258. /**
  18259. * Client requests to try to refine an item.
  18260. * 0aa3 <index>.W <material>.W <catalyst>.B
  18261. */
  18262. void clif_parse_refineui_refine( int fd, map_session_data* sd ){
  18263. #if PACKETVER >= 20161012
  18264. const PACKET_CZ_REQ_REFINING* p = reinterpret_cast<PACKET_CZ_REQ_REFINING*>( RFIFOP( fd, 0 ) );
  18265. uint16 index = server_index( p->index );
  18266. t_itemid material = p->itemId;
  18267. int16 j;
  18268. // Check if the refine UI is open
  18269. if( !sd->state.refineui_open ){
  18270. return;
  18271. }
  18272. // Check if the index is valid
  18273. if( index >= MAX_INVENTORY ){
  18274. return;
  18275. }
  18276. // Get the item db reference
  18277. struct item_data* id = sd->inventory_data[index];
  18278. // No item data was found
  18279. if( id == nullptr ){
  18280. return;
  18281. }
  18282. // Check the inventory
  18283. struct item* item = &sd->inventory.u.items_inventory[index];
  18284. // No item was found at the given index
  18285. if( item == nullptr ){
  18286. return;
  18287. }
  18288. // Check if the item is identified
  18289. if( !item->identify ){
  18290. return;
  18291. }
  18292. // Check if the item is broken
  18293. if( item->attribute ){
  18294. return;
  18295. }
  18296. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18297. // No refine possible
  18298. if( info == nullptr ){
  18299. return;
  18300. }
  18301. // No possibilities were found
  18302. if( info->costs.empty() ){
  18303. return;
  18304. }
  18305. // Check if the player has the selected material
  18306. if( ( j = pc_search_inventory( sd, material ) ) < 0 ){
  18307. return;
  18308. }
  18309. int16 blacksmith_index = -1;
  18310. uint16 blacksmith_amount = 0;
  18311. // Check if the player has enough blacksmith blessings
  18312. if( p->blacksmithBlessing != 0 ){
  18313. blacksmith_amount = info->blessing_amount;
  18314. // Player tried to use blacksmith blessing on a refine level, where it is not available
  18315. if( blacksmith_amount == 0 ){
  18316. return;
  18317. }
  18318. // Check if the player has blacksmith blessings
  18319. if( ( blacksmith_index = pc_search_inventory( sd, ITEMID_BLACKSMITH_BLESSING ) ) < 0 ){
  18320. return;
  18321. }
  18322. // Check if the player has enough blacksmith blessings
  18323. if( sd->inventory.u.items_inventory[blacksmith_index].amount < blacksmith_amount ){
  18324. return;
  18325. }
  18326. }
  18327. std::shared_ptr<s_refine_cost> cost = nullptr;
  18328. for( const auto& pair : info->costs ){
  18329. if( pair.second->nameid == material ){
  18330. cost = pair.second;
  18331. break;
  18332. }
  18333. }
  18334. // The material was not in the list of possible materials
  18335. if( cost == nullptr ){
  18336. return;
  18337. }
  18338. // Try to pay for the refine
  18339. if( pc_payzeny( sd, cost->zeny, LOG_TYPE_CONSUME ) ){
  18340. clif_npc_buy_result( sd, e_purchase_result::PURCHASE_FAIL_MONEY ); // "You do not have enough zeny."
  18341. return;
  18342. }
  18343. // Delete the required material
  18344. if( pc_delitem( sd, j, 1, 0, 0, LOG_TYPE_CONSUME ) ){
  18345. return;
  18346. }
  18347. // Delete the required blacksmith blessings
  18348. if( blacksmith_amount > 0 && pc_delitem( sd, blacksmith_index, blacksmith_amount, 0, 0, LOG_TYPE_CONSUME ) ){
  18349. return;
  18350. }
  18351. // Try to refine the item
  18352. if( cost->chance >= ( rnd() % 10000 ) ){
  18353. log_pick_pc( sd, LOG_TYPE_OTHER, -1, item );
  18354. // Success
  18355. item->refine = cap_value( item->refine + 1, 0, MAX_REFINE );
  18356. log_pick_pc( sd, LOG_TYPE_OTHER, 1, item );
  18357. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_SUCCESS );
  18358. clif_refine( *sd, index, ITEMREFINING_SUCCESS );
  18359. if (info->broadcast_success) {
  18360. clif_broadcast_refine_result(*sd, item->nameid, item->refine, true);
  18361. }
  18362. if( id->type == IT_WEAPON ){
  18363. achievement_update_objective( sd, AG_ENCHANT_SUCCESS, 2, id->weapon_level, item->refine );
  18364. }
  18365. clif_refineui_info( sd, index );
  18366. }else{
  18367. // Failure
  18368. if (info->broadcast_failure) {
  18369. clif_broadcast_refine_result(*sd, item->nameid, item->refine, false);
  18370. }
  18371. // Blacksmith blessings were used to prevent breaking and downgrading
  18372. if( blacksmith_amount > 0 ){
  18373. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18374. clif_refineui_info( sd, index );
  18375. // Delete the item if it is breakable
  18376. }else if( cost->breaking_rate > 0 && ( rnd() % 10000 ) < cost->breaking_rate ){
  18377. clif_refine( *sd, index, ITEMREFINING_FAILURE );
  18378. pc_delitem( sd, index, 1, 0, 2, LOG_TYPE_CONSUME );
  18379. // Downgrade the item if necessary
  18380. }else if( cost->downgrade_amount > 0 ){
  18381. item->refine = cap_value( item->refine - cost->downgrade_amount, 0, MAX_REFINE );
  18382. clif_refine( *sd, index, ITEMREFINING_DOWNGRADE );
  18383. clif_refineui_info(sd, index);
  18384. // Only show failure, but dont do anything
  18385. }else{
  18386. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18387. clif_refineui_info( sd, index );
  18388. }
  18389. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_FAILURE );
  18390. achievement_update_objective( sd, AG_ENCHANT_FAIL, 1, 1 );
  18391. }
  18392. #endif
  18393. }
  18394. void clif_unequipall_reply( map_session_data* sd, bool failed ){
  18395. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18396. PACKET_ZC_ACK_TAKEOFF_EQUIP_ALL p = {};
  18397. p.PacketType = HEADER_ZC_ACK_TAKEOFF_EQUIP_ALL;
  18398. p.result = failed;
  18399. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18400. #endif // PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18401. }
  18402. void clif_parse_unequipall( int fd, map_session_data* sd ){
  18403. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20210818
  18404. if( pc_cant_act( sd ) ){
  18405. clif_unequipall_reply( sd, true );
  18406. return;
  18407. }
  18408. const PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL* p = reinterpret_cast<PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL*>( RFIFOP( fd, 0 ) );
  18409. #if PACKETVER_MAIN_NUM >= 20230906
  18410. uint32 location = p->location;
  18411. int max = EQI_MAX;
  18412. #else
  18413. uint32 location = 0xFFFFFFFF;
  18414. int max = EQI_COSTUME_HEAD_TOP;
  18415. #endif
  18416. for( int i = 0; i < max; i++ ){
  18417. if( sd->equip_index[i] >= 0 && ( location & equip_bitmask[i] ) != 0 ){
  18418. pc_unequipitem( sd, sd->equip_index[i], 1 );
  18419. }
  18420. }
  18421. clif_unequipall_reply( sd, false );
  18422. #endif
  18423. }
  18424. void clif_stylist_response( map_session_data* sd, bool failed ){
  18425. #if PACKETVER >= 20151104
  18426. PACKET_ZC_STYLE_CHANGE_RES p = {};
  18427. p.PacketType = HEADER_ZC_STYLE_CHANGE_RES;
  18428. p.flag = failed;
  18429. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18430. if( !failed ){
  18431. sd->state.stylist_open = false;
  18432. }
  18433. #endif
  18434. }
  18435. bool clif_parse_stylist_buy_sub( map_session_data* sd, _look look, int16 index ){
  18436. std::shared_ptr<s_stylist_list> list = stylist_db.find( look );
  18437. if( list == nullptr ){
  18438. return false;
  18439. }
  18440. std::shared_ptr<s_stylist_entry> entry = util::umap_find( list->entries, index );
  18441. if( entry == nullptr ){
  18442. return false;
  18443. }
  18444. std::shared_ptr<s_stylist_costs> costs;
  18445. if( ( sd->class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  18446. costs = entry->doram;
  18447. }else{
  18448. costs = entry->human;
  18449. }
  18450. if( costs == nullptr ){
  18451. return false;
  18452. }
  18453. if( sd->status.zeny < costs->price ){
  18454. return false;
  18455. }
  18456. int16 inventoryIndex = -1;
  18457. if( costs->requiredItem != 0 ){
  18458. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18459. if( inventoryIndex < 0 ){
  18460. // No other option
  18461. if( costs->requiredItemBox == 0 ){
  18462. return false;
  18463. }
  18464. // Check if the box that contains the item is in the inventory
  18465. inventoryIndex = pc_search_inventory( sd, costs->requiredItemBox );
  18466. // The box containing the item also does not exist
  18467. if( inventoryIndex < 0 ){
  18468. return false;
  18469. }
  18470. }
  18471. }else if( costs->requiredItemBox != 0 ){
  18472. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18473. if( inventoryIndex < 0 ){
  18474. return false;
  18475. }
  18476. }
  18477. if( inventoryIndex >= 0 && pc_delitem( sd, inventoryIndex, 1, 0, 0, LOG_TYPE_OTHER ) != 0 ){
  18478. return false;
  18479. }
  18480. if( costs->price > 0 && pc_payzeny( sd, costs->price, LOG_TYPE_OTHER ) != 0 ){
  18481. return false;
  18482. }
  18483. switch( look ){
  18484. case LOOK_HAIR:
  18485. case LOOK_HAIR_COLOR:
  18486. case LOOK_CLOTHES_COLOR:
  18487. case LOOK_BODY2:
  18488. pc_changelook( sd, look, entry->value );
  18489. break;
  18490. case LOOK_HEAD_BOTTOM:
  18491. case LOOK_HEAD_MID:
  18492. case LOOK_HEAD_TOP: {
  18493. struct mail_message msg = {};
  18494. msg.dest_id = sd->status.char_id;
  18495. safestrncpy( msg.send_name, "Styling Shop", NAME_LENGTH );
  18496. safestrncpy( msg.title, "<MSG>2949</MSG>", MAIL_TITLE_LENGTH );
  18497. safestrncpy( msg.body, "<MSG>2950</MSG>", MAIL_BODY_LENGTH );
  18498. msg.item[0].nameid = entry->value;
  18499. msg.item[0].identify = 1;
  18500. msg.item[0].amount = 1;
  18501. msg.status = MAIL_NEW;
  18502. msg.type = MAIL_INBOX_NORMAL;
  18503. msg.timestamp = time( nullptr );
  18504. intif_Mail_send( 0, &msg );
  18505. } break;
  18506. }
  18507. return true;
  18508. }
  18509. void clif_parse_stylist_buy( int fd, map_session_data* sd ){
  18510. #if PACKETVER >= 20151104
  18511. #if PACKETVER >= 20180516
  18512. const PACKET_CZ_REQ_STYLE_CHANGE2* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE2*>( RFIFOP( fd, 0 ) );
  18513. #else
  18514. const PACKET_CZ_REQ_STYLE_CHANGE* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE*>( RFIFOP( fd, 0 ) );
  18515. #endif
  18516. if( p->HeadPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR_COLOR, p->HeadPalette ) ){
  18517. clif_stylist_response( sd, true );
  18518. return;
  18519. }
  18520. if( p->HeadStyle != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR, p->HeadStyle ) ){
  18521. clif_stylist_response( sd, true );
  18522. return;
  18523. }
  18524. if( p->BodyPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_CLOTHES_COLOR, p->BodyPalette ) ){
  18525. clif_stylist_response( sd, true );
  18526. return;
  18527. }
  18528. if( p->TopAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_TOP, p->TopAccessory ) ){
  18529. clif_stylist_response( sd, true );
  18530. return;
  18531. }
  18532. if( p->MidAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_MID, p->MidAccessory ) ){
  18533. clif_stylist_response( sd, true );
  18534. return;
  18535. }
  18536. if( p->BottomAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_BOTTOM, p->BottomAccessory ) ){
  18537. clif_stylist_response( sd, true );
  18538. return;
  18539. }
  18540. #if PACKETVER >= 20180516
  18541. if( p->BodyStyle != 0 && ( sd->class_ & JOBL_THIRD ) != 0 && ( sd->class_ & JOBL_FOURTH ) == 0 && !clif_parse_stylist_buy_sub( sd, LOOK_BODY2, p->BodyStyle ) ){
  18542. clif_stylist_response( sd, true );
  18543. return;
  18544. }
  18545. #endif
  18546. clif_stylist_response( sd, false );
  18547. #endif
  18548. }
  18549. void clif_parse_stylist_close( int fd, map_session_data* sd ){
  18550. #if PACKETVER >= 20151104
  18551. sd->state.stylist_open = false;
  18552. #endif
  18553. }
  18554. void clif_inventory_expansion_info( map_session_data* sd ){
  18555. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18556. nullpo_retv( sd );
  18557. PACKET_ZC_EXTEND_BODYITEM_SIZE p = {};
  18558. p.packetType = HEADER_ZC_EXTEND_BODYITEM_SIZE;
  18559. p.expansionSize = sd->status.inventory_slots - INVENTORY_BASE_SIZE;
  18560. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18561. #endif
  18562. }
  18563. enum class e_inventory_expansion_response : uint8{
  18564. ASK_CONFIRMATION = 0,
  18565. FAILED,
  18566. BUSY,
  18567. MISSING_ITEM,
  18568. MAXIMUM_REACHED
  18569. };
  18570. void clif_inventory_expansion_response( map_session_data* sd, e_inventory_expansion_response response ){
  18571. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18572. nullpo_retv( sd );
  18573. PACKET_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE p = {};
  18574. p.packetType = HEADER_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE;
  18575. p.result = (uint8)response;
  18576. p.itemId = sd->state.inventory_expansion_confirmation;
  18577. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18578. #endif
  18579. }
  18580. void clif_parse_inventory_expansion_request( int fd, map_session_data* sd ){
  18581. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18582. // Check if player is dead or busy with other stuff
  18583. if( pc_isdead( sd ) || pc_cant_act( sd ) ){
  18584. clif_inventory_expansion_response( sd, e_inventory_expansion_response::BUSY );
  18585. return;
  18586. }
  18587. // Check if the player already reached the maximum
  18588. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18589. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18590. return;
  18591. }
  18592. static std::map<t_itemid, uint16> items = {
  18593. // The order of entries in this list defines which will be used first
  18594. // This order and the usable items are hardcoded into the client
  18595. // The number of increased slots is "hardcoded" in the message of the client and cannot be sent per item
  18596. { ITEMID_INVENTORY_EX_EVT, 10 },
  18597. { ITEMID_INVENTORY_EX_DIS, 10 },
  18598. { ITEMID_INVENTORY_EX, 10 },
  18599. };
  18600. int16 index = -1;
  18601. bool found_over_limit = false;
  18602. for( const auto& entry : items ){
  18603. // Check if the player has the required item
  18604. index = pc_search_inventory( sd, entry.first );
  18605. // Found an item
  18606. if( index >= 0 ){
  18607. // Check if the player would exceed the maximum
  18608. if( sd->status.inventory_slots + entry.second > MAX_INVENTORY ){
  18609. found_over_limit = true;
  18610. }else{
  18611. found_over_limit = false;
  18612. sd->state.inventory_expansion_confirmation = entry.first;
  18613. sd->state.inventory_expansion_amount = entry.second;
  18614. break;
  18615. }
  18616. }
  18617. }
  18618. // Check if an item was found
  18619. if( sd->state.inventory_expansion_confirmation == 0 ){
  18620. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MISSING_ITEM );
  18621. return;
  18622. }
  18623. // Check if an item would have been found, but the player would exceed the maximum
  18624. if( found_over_limit ){
  18625. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18626. return;
  18627. }
  18628. // The player met all requirements => ask him for confirmation
  18629. clif_inventory_expansion_response( sd, e_inventory_expansion_response::ASK_CONFIRMATION );
  18630. #endif
  18631. }
  18632. enum class e_inventory_expansion_result : uint8{
  18633. SUCCESS = 0,
  18634. FAILED,
  18635. BUSY,
  18636. MISSING_ITEM,
  18637. MAXIMUM_REACHED
  18638. };
  18639. void clif_inventory_expansion_result( map_session_data* sd, e_inventory_expansion_result result ){
  18640. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18641. nullpo_retv( sd );
  18642. PACKET_ZC_ACK_EXTEND_BODYITEM_SIZE p = {};
  18643. p.packetType = HEADER_ZC_ACK_EXTEND_BODYITEM_SIZE;
  18644. p.result = (uint8)result;
  18645. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18646. // Reset the state tracking
  18647. sd->state.inventory_expansion_confirmation = 0;
  18648. sd->state.inventory_expansion_amount = 0;
  18649. #endif
  18650. }
  18651. void clif_parse_inventory_expansion_confirm( int fd, map_session_data* sd ){
  18652. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18653. if( sd->state.inventory_expansion_confirmation == 0 ){
  18654. return;
  18655. }
  18656. // Check if player is dead
  18657. if( pc_isdead( sd ) ){
  18658. clif_inventory_expansion_result( sd, e_inventory_expansion_result::BUSY );
  18659. return;
  18660. }
  18661. // Check if the player already reached the maximum
  18662. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18663. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18664. return;
  18665. }
  18666. // Check if the player has the required item
  18667. int index = pc_search_inventory( sd, sd->state.inventory_expansion_confirmation );
  18668. // The player did not have the item anymore
  18669. if( index < 0 ){
  18670. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MISSING_ITEM );
  18671. return;
  18672. }
  18673. // Check if the player would exceed the maximum
  18674. if( sd->status.inventory_slots + sd->state.inventory_expansion_amount > MAX_INVENTORY ){
  18675. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18676. return;
  18677. }
  18678. // Delete the required item
  18679. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_OTHER ) ){
  18680. clif_inventory_expansion_result( sd, e_inventory_expansion_result::FAILED );
  18681. return;
  18682. }
  18683. // Increase the slots
  18684. sd->status.inventory_slots += sd->state.inventory_expansion_amount;
  18685. // Save player data (slots) and inventory data (removed item)
  18686. chrif_save( sd, CSAVE_NORMAL | CSAVE_INVENTORY );
  18687. // Inform the player of success
  18688. clif_inventory_expansion_result( sd, e_inventory_expansion_result::SUCCESS );
  18689. clif_inventory_expansion_info( sd );
  18690. #endif
  18691. }
  18692. void clif_parse_inventory_expansion_reject( int fd, map_session_data* sd ){
  18693. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18694. sd->state.inventory_expansion_confirmation = 0;
  18695. sd->state.inventory_expansion_amount = 0;
  18696. #endif
  18697. }
  18698. void clif_barter_open( map_session_data& sd, struct npc_data& nd ){
  18699. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18700. if( nd.subtype != NPCTYPE_BARTER || nd.u.barter.extended || sd.state.barter_open ){
  18701. return;
  18702. }
  18703. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18704. if( barter == nullptr ){
  18705. return;
  18706. }
  18707. sd.state.barter_open = true;
  18708. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  18709. p->packetType = HEADER_ZC_NPC_BARTER_MARKET_ITEMINFO;
  18710. p->packetLength = sizeof( *p );
  18711. int16 count = 0;
  18712. for( const auto& itemPair : barter->items ){
  18713. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  18714. if( id == nullptr ){
  18715. continue;
  18716. }
  18717. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO_sub& item = p->list[count];
  18718. item.nameid = client_nameid( id->nameid );
  18719. item.type = itemtype( id->nameid );
  18720. if( itemPair.second->stockLimited ){
  18721. item.amount = itemPair.second->stock;
  18722. }else{
  18723. item.amount = -1;
  18724. }
  18725. item.weight = id->weight;
  18726. item.index = itemPair.second->index;
  18727. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  18728. item.viewSprite = id->look;
  18729. item.location = pc_equippoint_sub( &sd, id.get() );
  18730. #endif
  18731. // Use a loop if someone did not start with index 0
  18732. for( const auto& requirementPair : itemPair.second->requirements ){
  18733. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  18734. item.currencyNameid = client_nameid( requirement->nameid );
  18735. item.currencyAmount = requirement->amount;
  18736. // It is a normal barter, cancel after first entry
  18737. break;
  18738. }
  18739. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( item ) );
  18740. count++;
  18741. }
  18742. clif_send( p, p->packetLength, &sd.bl, SELF );
  18743. #endif
  18744. }
  18745. void clif_parse_barter_close( int fd, map_session_data* sd ){
  18746. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18747. if( sd->state.barter_open ){
  18748. sd->npc_shopid = 0;
  18749. sd->state.barter_open = false;
  18750. }
  18751. #endif
  18752. }
  18753. void clif_parse_barter_buy( int fd, map_session_data* sd ){
  18754. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18755. // No shop open
  18756. if( sd->npc_shopid == 0 || !sd->state.barter_open ){
  18757. return;
  18758. }
  18759. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  18760. // Unknown shop
  18761. if( nd == nullptr ){
  18762. return;
  18763. }
  18764. // Not a barter
  18765. if( nd->subtype != NPCTYPE_BARTER ){
  18766. return;
  18767. }
  18768. // It is an extended barter
  18769. if( nd->u.barter.extended ){
  18770. return;
  18771. }
  18772. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  18773. if( barter == nullptr ){
  18774. return;
  18775. }
  18776. const PACKET_CZ_NPC_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  18777. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  18778. // Empty purchase list
  18779. if( entries == 0 ){
  18780. return;
  18781. }
  18782. std::vector<s_barter_purchase> purchases;
  18783. purchases.reserve( entries );
  18784. // Make sure each shop index and target item id is only used once
  18785. for( int i = 0; i < entries; i++ ){
  18786. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  18787. // Invalid shop index
  18788. if( item == nullptr ){
  18789. return;
  18790. }
  18791. for( int j = i + 1; j < entries; j++ ){
  18792. // Same shop index
  18793. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  18794. return;
  18795. }
  18796. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  18797. // Invalid shop index
  18798. if( item2 == nullptr ){
  18799. return;
  18800. }
  18801. // Same target item id
  18802. if( item->nameid == item2->nameid ){
  18803. return;
  18804. }
  18805. }
  18806. s_barter_purchase purchase = {};
  18807. purchase.item = item;
  18808. purchase.amount = p->list[i].amount;
  18809. purchases.push_back( purchase );
  18810. }
  18811. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  18812. #endif
  18813. }
  18814. void clif_barter_extended_open( map_session_data& sd, struct npc_data& nd ){
  18815. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18816. if( nd.subtype != NPCTYPE_BARTER || !nd.u.barter.extended || sd.state.barter_extended_open ){
  18817. return;
  18818. }
  18819. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18820. if( barter == nullptr ){
  18821. return;
  18822. }
  18823. sd.state.barter_extended_open = true;
  18824. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  18825. p->packetType = HEADER_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO;
  18826. p->packetLength = sizeof( *p );
  18827. p->items_count = 0;
  18828. for( const auto& itemPair : barter->items ){
  18829. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  18830. if( id == nullptr ){
  18831. continue;
  18832. }
  18833. // Needs dynamic calculation, because of variable currencies
  18834. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub* item = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub*>( WBUFP( p, p->packetLength ) );
  18835. item->nameid = client_nameid( id->nameid );
  18836. item->type = itemtype( id->nameid );
  18837. if( itemPair.second->stockLimited ){
  18838. item->amount = itemPair.second->stock;
  18839. }else{
  18840. item->amount = -1;
  18841. }
  18842. item->weight = id->weight;
  18843. item->index = itemPair.second->index;
  18844. item->zeny = itemPair.second->price;
  18845. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  18846. item->viewSprite = id->look;
  18847. item->location = pc_equippoint_sub( &sd, id.get() );
  18848. #endif
  18849. // Because of a MSVS bug, the currencies have been defined with a fixed array entry, which has to be substracted
  18850. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *item ) - sizeof( item->currencies ) );
  18851. p->items_count++;
  18852. item->currency_count = 0;
  18853. for( const auto& requirementPair : itemPair.second->requirements ){
  18854. // Needs dynamic calculation, because of variable currencies
  18855. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2* req = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2*>( WBUFP( p, p->packetLength ) );
  18856. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  18857. req->nameid = requirement->nameid;
  18858. if( requirement->refine >= 0 ){
  18859. req->refine_level = requirement->refine;
  18860. }else{
  18861. req->refine_level = 0;
  18862. }
  18863. req->amount = requirement->amount;
  18864. req->type = itemtype( requirement->nameid );
  18865. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *req ) );
  18866. item->currency_count++;
  18867. }
  18868. }
  18869. clif_send( p, p->packetLength, &sd.bl, SELF );
  18870. #endif
  18871. }
  18872. void clif_parse_barter_extended_close( int fd, map_session_data* sd ){
  18873. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18874. if( sd->state.barter_extended_open ){
  18875. sd->npc_shopid = 0;
  18876. sd->state.barter_extended_open = false;
  18877. }
  18878. #endif
  18879. }
  18880. void clif_parse_barter_extended_buy( int fd, map_session_data* sd ){
  18881. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18882. // No shop open
  18883. if( sd->npc_shopid == 0 || !sd->state.barter_extended_open ){
  18884. return;
  18885. }
  18886. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  18887. // Unknown shop
  18888. if( nd == nullptr ){
  18889. return;
  18890. }
  18891. // Not a barter
  18892. if( nd->subtype != NPCTYPE_BARTER ){
  18893. return;
  18894. }
  18895. // Not an extended barter
  18896. if( !nd->u.barter.extended ){
  18897. return;
  18898. }
  18899. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  18900. if( barter == nullptr ){
  18901. return;
  18902. }
  18903. const PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  18904. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  18905. // Empty purchase list
  18906. if( entries == 0 ){
  18907. return;
  18908. }
  18909. std::vector<s_barter_purchase> purchases;
  18910. purchases.reserve( entries );
  18911. // Make sure each shop index and target item id is only used once
  18912. for( int i = 0; i < entries; i++ ){
  18913. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  18914. // Invalid shop index
  18915. if( item == nullptr ){
  18916. return;
  18917. }
  18918. for( int j = i + 1; j < entries; j++ ){
  18919. // Same shop index
  18920. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  18921. return;
  18922. }
  18923. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  18924. // Invalid shop index
  18925. if( item2 == nullptr ){
  18926. return;
  18927. }
  18928. // Same target item id
  18929. if( item->nameid == item2->nameid ){
  18930. return;
  18931. }
  18932. }
  18933. s_barter_purchase purchase = {};
  18934. purchase.item = item;
  18935. purchase.amount = p->list[i].amount;
  18936. purchases.push_back( purchase );
  18937. }
  18938. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  18939. #endif
  18940. }
  18941. void clif_summon_init(struct mob_data& md) {
  18942. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  18943. struct block_list* master_bl = battle_get_master(&md.bl);
  18944. if( master_bl == nullptr ){
  18945. return;
  18946. }
  18947. PACKET_ZC_SUMMON_HP_INIT p = {};
  18948. p.PacketType = HEADER_ZC_SUMMON_HP_INIT;
  18949. p.summonAID = md.bl.id;
  18950. p.CurrentHP = md.status.hp;
  18951. p.MaxHP = md.status.max_hp;
  18952. clif_send( &p, sizeof( p ), master_bl, SELF );
  18953. #endif
  18954. }
  18955. void clif_summon_hp_bar(struct mob_data& md) {
  18956. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  18957. struct block_list* master_bl = battle_get_master(&md.bl);
  18958. if( master_bl == nullptr ){
  18959. return;
  18960. }
  18961. PACKET_ZC_SUMMON_HP_UPDATE p = {};
  18962. p.PacketType = HEADER_ZC_SUMMON_HP_UPDATE;
  18963. p.summonAID = md.bl.id;
  18964. p.VarId = SP_HP; // HP parameter
  18965. p.Value = md.status.hp;
  18966. clif_send( &p, sizeof( p ), master_bl, SELF );
  18967. #endif
  18968. }
  18969. void clif_laphine_synthesis_open( map_session_data *sd, std::shared_ptr<s_laphine_synthesis> synthesis ){
  18970. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  18971. nullpo_retv( sd );
  18972. sd->state.laphine_synthesis = synthesis->item_id;
  18973. PACKET_ZC_RANDOM_COMBINE_ITEM_UI_OPEN p = {};
  18974. p.packetType = HEADER_ZC_RANDOM_COMBINE_ITEM_UI_OPEN;
  18975. p.itemId = client_nameid( synthesis->item_id );
  18976. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18977. #endif
  18978. }
  18979. void clif_parse_laphine_synthesis_close( int fd, map_session_data* sd ){
  18980. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  18981. sd->state.laphine_synthesis = 0;
  18982. #endif
  18983. }
  18984. enum e_laphine_synthesis_result : int16{
  18985. LAPHINE_SYNTHESIS_SUCCESS = 0,
  18986. LAPHINE_SYNTHESIS_AMOUNT = 5,
  18987. LAPHINE_SYNTHESIS_ITEM = 7
  18988. };
  18989. void clif_laphine_synthesis_result( map_session_data* sd, enum e_laphine_synthesis_result result ){
  18990. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  18991. nullpo_retv( sd );
  18992. PACKET_ZC_ACK_RANDOM_COMBINE_ITEM p = {};
  18993. p.packetType = HEADER_ZC_ACK_RANDOM_COMBINE_ITEM;
  18994. p.result = result;
  18995. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18996. #endif
  18997. }
  18998. void clif_parse_laphine_synthesis( int fd, map_session_data* sd ){
  18999. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19000. if( sd->state.laphine_synthesis == 0 ){
  19001. return;
  19002. }
  19003. const PACKET_CZ_REQ_RANDOM_COMBINE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_COMBINE_ITEM*>( RFIFOP( fd, 0 ) );
  19004. if( sd->state.laphine_synthesis != p->itemId ){
  19005. return;
  19006. }
  19007. std::shared_ptr<s_laphine_synthesis> synthesis = laphine_synthesis_db.find( sd->state.laphine_synthesis );
  19008. if( synthesis == nullptr ){
  19009. return;
  19010. }
  19011. size_t count = ( p->packetLength - sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM ) ) / sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM_sub );
  19012. // Player sent more or less than actually required
  19013. if( count != synthesis->requiredRequirements ){
  19014. return;
  19015. }
  19016. // Check for duplicates
  19017. for( size_t i = 0; i < count; i++ ){
  19018. for( size_t j = i + 1; j < count; j++ ){
  19019. if( p->items[i].index == p->items[j].index ){
  19020. return;
  19021. }
  19022. }
  19023. }
  19024. for( size_t i = 0; i < count; i++ ){
  19025. int16 index = server_index( p->items[i].index );
  19026. if( index >= MAX_INVENTORY ){
  19027. return;
  19028. }
  19029. if( sd->inventory_data[index] == nullptr ){
  19030. return;
  19031. }
  19032. struct item* item = &sd->inventory.u.items_inventory[index];
  19033. std::shared_ptr<s_laphine_synthesis_requirement> requirement = util::umap_find( synthesis->requirements, item->nameid );
  19034. if( requirement == nullptr ){
  19035. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19036. return;
  19037. }
  19038. if( p->items[i].count != requirement->amount ){
  19039. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19040. return;
  19041. }
  19042. if( item->amount < requirement->amount ){
  19043. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19044. return;
  19045. }
  19046. if( item->refine < synthesis->minimumRefine ){
  19047. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19048. return;
  19049. }
  19050. if( item->refine > synthesis->maximumRefine ){
  19051. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19052. return;
  19053. }
  19054. if( item->equip != 0 ){
  19055. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19056. return;
  19057. }
  19058. if( item->equipSwitch != 0 ){
  19059. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19060. return;
  19061. }
  19062. }
  19063. // If triggered from item
  19064. if( sd->itemid == sd->state.laphine_synthesis ){
  19065. int16 index = pc_search_inventory( sd, sd->state.laphine_synthesis );
  19066. if( index < 0 ){
  19067. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19068. return;
  19069. }
  19070. if( ( sd->inventory_data[index]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19071. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19072. return;
  19073. }
  19074. }
  19075. }
  19076. for( size_t i = 0; i < count; i++ ){
  19077. int16 index = server_index( p->items[i].index );
  19078. if( pc_delitem( sd, index, p->items[i].count, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19079. return;
  19080. }
  19081. }
  19082. itemdb_group.pc_get_itemgroup( synthesis->rewardGroupId, true, *sd );
  19083. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_SUCCESS );
  19084. #endif
  19085. }
  19086. void clif_laphine_upgrade_open( map_session_data* sd, std::shared_ptr<s_laphine_upgrade> upgrade ){
  19087. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19088. nullpo_retv( sd );
  19089. sd->state.laphine_upgrade = upgrade->item_id;
  19090. PACKET_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN p = {};
  19091. p.packetType = HEADER_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN;
  19092. p.itemId = client_nameid( upgrade->item_id );
  19093. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19094. #endif
  19095. }
  19096. void clif_parse_laphine_upgrade_close( int fd, map_session_data* sd ){
  19097. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19098. sd->state.laphine_upgrade = 0;
  19099. #endif
  19100. }
  19101. void clif_laphine_upgrade_result( map_session_data *sd, bool failed ){
  19102. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19103. PACKET_ZC_ACK_RANDOM_UPGRADE_ITEM p = {};
  19104. p.packetType = HEADER_ZC_ACK_RANDOM_UPGRADE_ITEM;
  19105. p.result = failed;
  19106. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19107. #endif
  19108. }
  19109. static void clif_item_preview( map_session_data *sd, int16 index ){
  19110. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19111. nullpo_retv( sd );
  19112. struct item* item = &sd->inventory.u.items_inventory[index];
  19113. PACKET_ZC_CHANGE_ITEM_OPTION p = {};
  19114. p.packetType = HEADER_ZC_CHANGE_ITEM_OPTION;
  19115. p.index = client_index( index );
  19116. #if PACKETVER_MAIN_NUM >= 20181017 || PACKETVER_RE_NUM >= 20181017 || PACKETVER_ZERO_NUM >= 20181024
  19117. p.isDamaged = item->attribute != 0;
  19118. #endif
  19119. p.refiningLevel = item->refine;
  19120. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723
  19121. p.grade = item->enchantgrade;
  19122. #endif
  19123. clif_addcards( &p.slot, item );
  19124. clif_add_random_options( p.option_data, *item );
  19125. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19126. #endif
  19127. }
  19128. void clif_parse_laphine_upgrade( int fd, map_session_data* sd ){
  19129. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19130. if( sd->state.laphine_upgrade == 0 ){
  19131. return;
  19132. }
  19133. const PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM*>( RFIFOP( fd, 0 ) );
  19134. if( sd->state.laphine_upgrade != p->itemId ){
  19135. return;
  19136. }
  19137. std::shared_ptr<s_laphine_upgrade> upgrade = laphine_upgrade_db.find( sd->state.laphine_upgrade );
  19138. if( upgrade == nullptr ){
  19139. return;
  19140. }
  19141. uint16 index = server_index( p->index );
  19142. if( index >= MAX_INVENTORY ){
  19143. return;
  19144. }
  19145. if( sd->inventory_data[index] == nullptr ){
  19146. return;
  19147. }
  19148. struct item* item = &sd->inventory.u.items_inventory[index];
  19149. // Not a valid target item
  19150. if( !util::vector_exists( upgrade->target_item_ids, item->nameid ) ){
  19151. clif_laphine_upgrade_result( sd, true );
  19152. return;
  19153. }
  19154. // If target item is not identified
  19155. if( item->identify == 0 ){
  19156. clif_laphine_upgrade_result( sd, true );
  19157. return;
  19158. }
  19159. // If target item is equipped
  19160. if( item->equip != 0 ){
  19161. clif_laphine_upgrade_result( sd, true );
  19162. return;
  19163. }
  19164. // If target item is in equipswitch
  19165. if( item->equipSwitch != 0 ){
  19166. clif_laphine_upgrade_result( sd, true );
  19167. return;
  19168. }
  19169. // Check minimum refine requirement
  19170. if( item->refine < upgrade->minimumRefine ){
  19171. clif_laphine_upgrade_result( sd, true );
  19172. return;
  19173. }
  19174. // Check maximum refine requirement
  19175. if( item->refine > upgrade->maximumRefine ){
  19176. clif_laphine_upgrade_result( sd, true );
  19177. return;
  19178. }
  19179. // If no cards are allowed
  19180. if( !upgrade->cardsAllowed ){
  19181. for( int i = 0; i < MAX_SLOTS; i++ ){
  19182. if( item->card[i] != 0 ){
  19183. clif_laphine_upgrade_result( sd, true );
  19184. return;
  19185. }
  19186. }
  19187. }
  19188. // If random options are required
  19189. if( upgrade->requiredRandomOptions > 0 ){
  19190. int i;
  19191. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19192. if( item->option[i].id != 0 ){
  19193. break;
  19194. }
  19195. }
  19196. if( ( i + 1 ) < upgrade->requiredRandomOptions ){
  19197. clif_laphine_upgrade_result( sd, true );
  19198. return;
  19199. }
  19200. }
  19201. int16 index2 = pc_search_inventory( sd, sd->state.laphine_upgrade );
  19202. if( index2 < 0 ){
  19203. clif_laphine_upgrade_result( sd, true );
  19204. return;
  19205. }
  19206. if( ( sd->inventory_data[index2]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19207. if( pc_delitem( sd, index2, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19208. return;
  19209. }
  19210. }
  19211. // Log removal of item
  19212. log_pick_pc( sd, LOG_TYPE_LAPHINE, -1, item );
  19213. // Visually remove it from the client
  19214. clif_delitem( *sd, index, 1, 0 );
  19215. // Apply the random options
  19216. if( upgrade->randomOptionGroup != nullptr ){
  19217. upgrade->randomOptionGroup->apply( *item );
  19218. }
  19219. // Change the refine rate if needed
  19220. if( upgrade->resultRefine > 0 ){
  19221. // Absolute refine level change
  19222. item->refine = max( item->refine, upgrade->resultRefine );
  19223. }else if( upgrade->resultRefineMaximum > 0 ){
  19224. // If a minimum is specified it can also downgrade
  19225. if( upgrade->resultRefineMinimum ){
  19226. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, upgrade->resultRefineMaximum ) );
  19227. }else{
  19228. // Otherwise it can only be upgraded until the maximum, but not downgraded
  19229. item->refine = static_cast<uint8>( rnd_value<uint16>( item->refine, upgrade->resultRefineMaximum ) );
  19230. }
  19231. }else if( upgrade->resultRefineMinimum > 0 ){
  19232. // No maximum has been specified, so it can be anything between minimum and MAX_REFINE
  19233. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, MAX_REFINE ) );
  19234. }
  19235. // Log retrieving the item again -> with the new options
  19236. log_pick_pc( sd, LOG_TYPE_LAPHINE, 1, item );
  19237. // Make it visible for the client again
  19238. clif_additem( sd, index, 1, 0 );
  19239. // Open a preview of the item
  19240. clif_item_preview( sd, index );
  19241. // Tell the client we are done
  19242. clif_laphine_upgrade_result( sd, false );
  19243. #endif
  19244. }
  19245. void clif_enchantgrade_add( map_session_data& sd, uint16 index = UINT16_MAX, std::shared_ptr<s_enchantgradelevel> gradeLevel = nullptr ){
  19246. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19247. PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST*>( packet_buffer );
  19248. p->PacketType = HEADER_ZC_GRADE_ENCHANT_MATERIAL_LIST;
  19249. p->PacketLength = sizeof( *p );
  19250. if( index < UINT16_MAX ){
  19251. p->index = client_index( index );
  19252. p->success_chance = gradeLevel->chances[sd.inventory.u.items_inventory[index].refine] / 100;
  19253. p->blessing_info.id = client_nameid( gradeLevel->catalyst.item );
  19254. p->blessing_info.amount = gradeLevel->catalyst.amountPerStep;
  19255. p->blessing_info.max_blessing = gradeLevel->catalyst.maximumSteps;
  19256. p->blessing_info.bonus = gradeLevel->catalyst.chanceIncrease / 100;
  19257. // Not displayed by client
  19258. p->protect_itemid = 0;
  19259. p->protect_amount = 0;
  19260. int i = 0;
  19261. for( const auto& pair : gradeLevel->options ){
  19262. GRADE_ENCHANT_MATERIAL& mat = p->material_info[i];
  19263. std::shared_ptr<s_enchantgradeoption> option = pair.second;
  19264. mat.nameid = client_nameid( option->item );
  19265. mat.amount = option->amount;
  19266. mat.price = option->zeny;
  19267. mat.downgrade = option->downgrade_amount > 0;
  19268. mat.breakable = option->breaking_rate > 0;
  19269. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( mat ) );
  19270. i++;
  19271. }
  19272. }else{
  19273. p->index = -1;
  19274. p->success_chance = 0;
  19275. p->blessing_info.id = 0;
  19276. p->blessing_info.amount = 0;
  19277. p->blessing_info.max_blessing = 0;
  19278. p->blessing_info.bonus = 0;
  19279. p->protect_itemid = 0;
  19280. p->protect_amount = 0;
  19281. }
  19282. clif_send( p, p->PacketLength, &sd.bl, SELF );
  19283. #endif
  19284. }
  19285. void clif_parse_enchantgrade_add( int fd, map_session_data* sd ){
  19286. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19287. nullpo_retv( sd );
  19288. if( !sd->state.enchantgrade_open ){
  19289. return;
  19290. }
  19291. const PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT*>( RFIFOP( fd, 0 ) );
  19292. uint16 index = server_index( p->index );
  19293. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19294. return;
  19295. }
  19296. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19297. // Unsupported item type - no answer, because client should have actually prevented this request
  19298. if( enchantgrade == nullptr ){
  19299. return;
  19300. }
  19301. // Item can't be enhanced
  19302. if( !sd->inventory_data[index]->flag.gradable ){
  19303. return;
  19304. }
  19305. uint16 level = 0;
  19306. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19307. level = sd->inventory_data[index]->weapon_level;
  19308. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19309. level = sd->inventory_data[index]->armor_level;
  19310. }
  19311. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19312. // Cannot upgrade this weapon or armor level
  19313. if( enchantgradelevels == enchantgrade->levels.end() ){
  19314. clif_enchantgrade_add( *sd );
  19315. return;
  19316. }
  19317. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19318. // Cannot increase enchantgrade any further - no answer, because client should have actually prevented this request
  19319. if( enchantgradelevel == nullptr ){
  19320. return;
  19321. }
  19322. clif_enchantgrade_add( *sd, index, enchantgradelevel );
  19323. #endif
  19324. }
  19325. /// <summary>
  19326. /// Sends the result for trying to enchant an item
  19327. /// </summary>
  19328. /// <param name="sd">The player session</param>
  19329. /// <param name="index">The target item</param>
  19330. /// <param name="result">
  19331. /// 0= The grade has been successfully upgraded.
  19332. /// 1= Refinement failed.
  19333. /// 2= The refine level has decreased.
  19334. /// 3= Equipment destroyed.
  19335. /// 4= The equipment is protected.
  19336. /// </param>
  19337. void clif_enchantgrade_result( map_session_data& sd, uint16 index, e_enchantgrade_result result ){
  19338. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19339. PACKET_ZC_GRADE_ENCHANT_ACK p = {};
  19340. p.PacketType = HEADER_ZC_GRADE_ENCHANT_ACK;
  19341. p.index = client_index( index );
  19342. p.grade = sd.inventory.u.items_inventory[index].enchantgrade;
  19343. p.result = result;
  19344. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19345. #endif
  19346. }
  19347. void clif_enchantgrade_announce( map_session_data& sd, struct item& item, bool success ){
  19348. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19349. PACKET_ZC_GRADE_ENCHANT_BROADCAST_RESULT p = {};
  19350. p.packetType = HEADER_ZC_GRADE_ENCHANT_BROADCAST_RESULT;
  19351. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  19352. p.itemId = client_nameid( item.nameid );
  19353. p.grade = item.enchantgrade;
  19354. p.status = success;
  19355. clif_send( &p, sizeof( p ), nullptr, ALL_CLIENT );
  19356. #endif
  19357. }
  19358. void clif_parse_enchantgrade_start( int fd, map_session_data* sd ){
  19359. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19360. nullpo_retv( sd );
  19361. if( !sd->state.enchantgrade_open ){
  19362. return;
  19363. }
  19364. const PACKET_CZ_GRADE_ENCHANT_REQUEST* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_REQUEST*>( RFIFOP( fd, 0 ) );
  19365. uint16 index = server_index( p->index );
  19366. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19367. return;
  19368. }
  19369. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19370. // Unsupported item type - no answer
  19371. if( enchantgrade == nullptr ){
  19372. return;
  19373. }
  19374. uint16 level = 0;
  19375. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19376. level = sd->inventory_data[index]->weapon_level;
  19377. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19378. level = sd->inventory_data[index]->armor_level;
  19379. }
  19380. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19381. // Cannot upgrade this weapon or armor level - no answer
  19382. if( enchantgradelevels == enchantgrade->levels.end() ){
  19383. return;
  19384. }
  19385. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19386. // Cannot increase enchantgrade any further - no answer
  19387. if( enchantgradelevel == nullptr ){
  19388. return;
  19389. }
  19390. uint16 totalChance = enchantgradelevel->chances[sd->inventory.u.items_inventory[index].refine];
  19391. // No chance to increase the enchantgrade
  19392. if( totalChance == 0 ){
  19393. return;
  19394. }
  19395. std::shared_ptr<s_enchantgradeoption> option = util::map_find( enchantgradelevel->options, (uint16)p->material_index );
  19396. // Unknown option id - no answer
  19397. if( option == nullptr ){
  19398. return;
  19399. }
  19400. // Not enough zeny
  19401. if( sd->status.zeny < option->zeny ){
  19402. return;
  19403. }
  19404. uint16 steps = min( p->blessing_amount, enchantgradelevel->catalyst.maximumSteps );
  19405. std::unordered_map<uint16, uint16> requiredItems;
  19406. if( p->blessing_flag ){
  19407. // If the catalysator item is the same as the option item build the sum of amounts
  19408. if( enchantgradelevel->catalyst.item == option->item ){
  19409. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps + option->amount;
  19410. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19411. if( index < 0 ){
  19412. return;
  19413. }
  19414. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19415. return;
  19416. }
  19417. requiredItems[index] = amount;
  19418. }else{
  19419. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps;
  19420. // Check catalysator item
  19421. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19422. if( index < 0 ){
  19423. return;
  19424. }
  19425. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19426. return;
  19427. }
  19428. requiredItems[index] = amount;
  19429. // Check option item
  19430. index = pc_search_inventory( sd, option->item );
  19431. if( index < 0 ){
  19432. return;
  19433. }
  19434. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19435. return;
  19436. }
  19437. requiredItems[index] = option->amount;
  19438. }
  19439. totalChance += steps * enchantgradelevel->catalyst.chanceIncrease;
  19440. }else{
  19441. // Check option item
  19442. int16 index = pc_search_inventory( sd, option->item );
  19443. if( index < 0 ){
  19444. return;
  19445. }
  19446. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19447. return;
  19448. }
  19449. requiredItems[index] = option->amount;
  19450. }
  19451. // All items should be there, start deleting
  19452. for( const auto& pair : requiredItems ){
  19453. if( pc_delitem( sd, pair.first, pair.second, 0, 0, LOG_TYPE_ENCHANTGRADE ) != 0 ){
  19454. return;
  19455. }
  19456. }
  19457. if( pc_payzeny( sd, option->zeny, LOG_TYPE_ENCHANTGRADE ) > 0 ){
  19458. return;
  19459. }
  19460. if( rnd()%10000 < totalChance ){
  19461. // Log removal of item
  19462. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19463. // Increase enchantgrade
  19464. sd->inventory.u.items_inventory[index].enchantgrade = min( sd->inventory.u.items_inventory[index].enchantgrade + 1, MAX_ENCHANTGRADE );
  19465. // On successful enchantgrade increase the refine is reset
  19466. sd->inventory.u.items_inventory[index].refine = 0;
  19467. // Log retrieving the item again -> with the new refine and enchantgrade
  19468. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19469. // Show success
  19470. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_SUCCESS );
  19471. // Check if it has to be announced
  19472. if( enchantgradelevel->announceSuccess ){
  19473. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], true );
  19474. }
  19475. }else{
  19476. // Check if it has to be announced (has to be done before deleting the item from inventory)
  19477. if( enchantgradelevel->announceFail ){
  19478. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], false );
  19479. }
  19480. // Delete the item if it is breakable
  19481. if( option->breaking_rate > 0 && ( rnd() % 10000 ) < option->breaking_rate ){
  19482. // Delete the item
  19483. pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_ENCHANTGRADE );
  19484. // Show failure
  19485. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_BREAK );
  19486. // Downgrade the item if necessary
  19487. }else if( option->downgrade_amount > 0 ){
  19488. // Log removal of item
  19489. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19490. // Decrease refine level
  19491. sd->inventory.u.items_inventory[index].refine = cap_value( sd->inventory.u.items_inventory[index].refine - option->downgrade_amount, 0, MAX_REFINE );
  19492. // Log retrieving the item again -> with the new refine
  19493. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19494. // Show downgrade
  19495. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_DOWNGRADE );
  19496. // Only show failure, but dont do anything
  19497. }else{
  19498. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_FAILED );
  19499. }
  19500. }
  19501. #endif
  19502. }
  19503. void clif_parse_enchantgrade_close( int fd, map_session_data* sd ){
  19504. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19505. nullpo_retv( sd );
  19506. sd->state.enchantgrade_open = false;
  19507. #endif
  19508. }
  19509. void clif_reputation_type( map_session_data& sd, int64 type, int64 points ){
  19510. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19511. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19512. p->packetType = HEADER_ZC_REPUTE_INFO;
  19513. p->packetLength = sizeof( *p );
  19514. p->success = true;
  19515. PACKET_ZC_REPUTE_INFO_sub& entry = p->list[0];
  19516. entry.type = type;
  19517. entry.points = points;
  19518. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  19519. clif_send( p, p->packetLength, &sd.bl, SELF );
  19520. #endif
  19521. }
  19522. void clif_reputation_list( map_session_data& sd ){
  19523. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19524. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19525. p->packetType = HEADER_ZC_REPUTE_INFO;
  19526. p->packetLength = sizeof( *p );
  19527. p->success = true;
  19528. size_t index = 0;
  19529. for( const auto& entry : reputation_db ){
  19530. std::shared_ptr<s_reputation> reputation = entry.second;
  19531. PACKET_ZC_REPUTE_INFO_sub& list_entry = p->list[index];
  19532. list_entry.type = reputation->id;
  19533. list_entry.points = pc_readreg2( &sd, reputation->variable.c_str() );
  19534. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( list_entry ) );
  19535. index++;
  19536. }
  19537. clif_send( p, p->packetLength, &sd.bl, SELF );
  19538. #endif
  19539. }
  19540. void clif_item_reform_open( map_session_data& sd, t_itemid item ){
  19541. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19542. PACKET_ZC_OPEN_REFORM_UI p = {};
  19543. p.PacketType = HEADER_ZC_OPEN_REFORM_UI;
  19544. p.ITID = item;
  19545. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19546. sd.state.item_reform = item;
  19547. #endif
  19548. }
  19549. void clif_parse_item_reform_close( int fd, map_session_data* sd ){
  19550. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19551. sd->state.item_reform = 0;
  19552. #endif
  19553. }
  19554. void clif_item_reform_result( map_session_data& sd, uint16 index, uint8 result ){
  19555. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19556. PACKET_ZC_ITEM_REFORM_ACK p = {};
  19557. p.PacketType = HEADER_ZC_ITEM_REFORM_ACK;
  19558. p.index = client_index( index );
  19559. p.result = result;
  19560. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19561. if( result == 0 ){
  19562. // Client closes the window on success
  19563. sd.state.item_reform = 0;
  19564. }
  19565. #endif
  19566. }
  19567. void clif_parse_item_reform_start( int fd, map_session_data* sd ){
  19568. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19569. // Not opened
  19570. if( sd->state.item_reform == 0 ){
  19571. return;
  19572. }
  19573. const PACKET_CZ_ITEM_REFORM* p = reinterpret_cast<PACKET_CZ_ITEM_REFORM*>( RFIFOP( fd, 0 ) );
  19574. // Item mismatch
  19575. if( p->ITID != sd->state.item_reform ){
  19576. return;
  19577. }
  19578. uint16 index = server_index( p->index );
  19579. if( index >= MAX_INVENTORY ){
  19580. return;
  19581. }
  19582. if( sd->inventory_data[index] == nullptr ){
  19583. return;
  19584. }
  19585. std::shared_ptr<s_item_reform> reform = item_reform_db.find( sd->state.item_reform );
  19586. if( reform == nullptr ){
  19587. return;
  19588. }
  19589. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19590. std::shared_ptr<s_item_reform_base> base = util::umap_find( reform->base_items, selected_item.nameid );
  19591. if( base == nullptr ){
  19592. return;
  19593. }
  19594. // If target item is not identified
  19595. if( selected_item.identify == 0 ){
  19596. return;
  19597. }
  19598. // If target item is equipped
  19599. if( selected_item.equip != 0 ){
  19600. return;
  19601. }
  19602. // Check minimum refine requirement
  19603. if( selected_item.refine < base->minimumRefine ){
  19604. return;
  19605. }
  19606. // Check maximum refine requirement
  19607. if( selected_item.refine > base->maximumRefine ){
  19608. return;
  19609. }
  19610. // If no cards are allowed
  19611. if( !base->cardsAllowed ){
  19612. for( int i = 0; i < MAX_SLOTS; i++ ){
  19613. if( selected_item.card[i] != 0 ){
  19614. return;
  19615. }
  19616. }
  19617. }
  19618. // If random options are required
  19619. if( base->requiredRandomOptions > 0 ){
  19620. int i;
  19621. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19622. if( selected_item.option[i].id != 0 ){
  19623. break;
  19624. }
  19625. }
  19626. if( ( i + 1 ) < base->requiredRandomOptions ){
  19627. return;
  19628. }
  19629. }
  19630. std::unordered_map<uint16, uint16> materials;
  19631. // Check if all materials exist
  19632. for( const auto& material : base->materials ){
  19633. int16 material_index = pc_search_inventory( sd, material.first );
  19634. if( material_index < 0 ){
  19635. return;
  19636. }
  19637. if( sd->inventory.u.items_inventory[material_index].amount < material.second ){
  19638. return;
  19639. }
  19640. materials[material_index] = material.second;
  19641. }
  19642. // Remove the material
  19643. for( const auto& material : materials ){
  19644. if( pc_delitem( sd, material.first, material.second, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19645. return;
  19646. }
  19647. }
  19648. // If triggered from item
  19649. if( sd->itemid == sd->state.item_reform && pc_delitem( sd, sd->itemindex, 1, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19650. return;
  19651. }
  19652. // Log removal of item
  19653. log_pick_pc( sd, LOG_TYPE_REFORM, -1, &selected_item );
  19654. // Visually remove it from the client
  19655. clif_delitem( *sd, index, 1, 0 );
  19656. // Apply the random options
  19657. if( base->randomOptionGroup != nullptr ){
  19658. base->randomOptionGroup->apply( selected_item );
  19659. }
  19660. // Change the refine rate if needed
  19661. if( base->refineChange != 0 ){
  19662. selected_item.refine = cap_value( selected_item.refine + base->refineChange, 0, MAX_REFINE );
  19663. }
  19664. // Remove all cards and socket enchants
  19665. if( base->clearSlots ){
  19666. for( int i = 0; i < MAX_SLOTS; i++ ){
  19667. selected_item.card[i] = 0;
  19668. }
  19669. }
  19670. // Remove the current enchantgrade
  19671. if( base->removeEnchantgrade ){
  19672. selected_item.enchantgrade = 0;
  19673. }
  19674. // Finally change the item id
  19675. selected_item.nameid = base->resultItemId;
  19676. // Link inventory data cache to the new item
  19677. sd->inventory_data[index] = itemdb_search( base->resultItemId );
  19678. // Log retrieving the item again -> with the new options
  19679. log_pick_pc( sd, LOG_TYPE_REFORM, 1, &selected_item );
  19680. // Make it visible for the client again
  19681. clif_additem( sd, index, 1, 0 );
  19682. clif_item_reform_result( *sd, index, 0 );
  19683. #endif
  19684. }
  19685. void clif_enchantwindow_open( map_session_data& sd, uint64 clientLuaIndex ){
  19686. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19687. // Hardcoded clientside check
  19688. if( sd.weight > ( ( sd.max_weight * 70 ) / 100 ) ){
  19689. clif_msg_color( &sd, MSI_ENCHANT_FAILED_OVER_WEIGHT, color_table[COLOR_RED] );
  19690. sd.state.item_enchant_index = 0;
  19691. return;
  19692. }
  19693. PACKET_ZC_UI_OPEN_V3 p = {};
  19694. p.packetType = HEADER_ZC_UI_OPEN_V3;
  19695. p.type = OUT_UI_ENCHANT;
  19696. p.data = clientLuaIndex;
  19697. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19698. sd.state.item_enchant_index = clientLuaIndex;
  19699. #endif
  19700. }
  19701. void clif_enchantwindow_result( map_session_data& sd, bool success, t_itemid enchant = 0 ){
  19702. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19703. PACKET_ZC_RESPONSE_ENCHANT p = {};
  19704. p.PacketType = HEADER_ZC_RESPONSE_ENCHANT;
  19705. if( success ){
  19706. p.msgId = MSI_ENCHANT_SUCCESS;
  19707. }else{
  19708. p.msgId = MSI_ENCHANT_FAILED;
  19709. }
  19710. p.ITID = enchant;
  19711. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19712. sd.state.item_enchant_index = 0;
  19713. #endif
  19714. }
  19715. bool clif_parse_enchant_basecheck( struct item& selected_item, std::shared_ptr<s_item_enchant> enchant ){
  19716. if( selected_item.equip != 0 ){
  19717. return false;
  19718. }
  19719. if( selected_item.equipSwitch != 0 ){
  19720. return false;
  19721. }
  19722. if( selected_item.attribute != 0 ){
  19723. return false;
  19724. }
  19725. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  19726. return false;
  19727. }
  19728. if( selected_item.refine < enchant->minimumRefine ){
  19729. return false;
  19730. }
  19731. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  19732. return false;
  19733. }
  19734. if( !enchant->allowRandomOptions ){
  19735. for( const s_item_randomoption& option : selected_item.option ){
  19736. if( option.id != 0 ){
  19737. return false;
  19738. }
  19739. }
  19740. }
  19741. return true;
  19742. }
  19743. void clif_parse_enchantwindow_general( int fd, map_session_data* sd ){
  19744. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19745. const PACKET_CZ_REQUEST_RANDOM_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RANDOM_ENCHANT*>( RFIFOP( fd, 0 ) );
  19746. if( sd->state.item_enchant_index != p->enchant_group ){
  19747. return;
  19748. }
  19749. uint16 index = server_index( p->index );
  19750. if( index >= MAX_INVENTORY ){
  19751. return;
  19752. }
  19753. if( sd->inventory_data[index] == nullptr ){
  19754. return;
  19755. }
  19756. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19757. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19758. if( enchant == nullptr ){
  19759. return;
  19760. }
  19761. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19762. return;
  19763. }
  19764. uint16 slot = MAX_SLOTS;
  19765. for( uint16 nextSlot : enchant->order ){
  19766. if( selected_item.card[nextSlot] == 0 ){
  19767. slot = nextSlot;
  19768. break;
  19769. }
  19770. }
  19771. if( slot == MAX_SLOTS ){
  19772. return;
  19773. }
  19774. if( slot < sd->inventory_data[index]->slots ){
  19775. return;
  19776. }
  19777. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19778. if( enchant_slot == nullptr ){
  19779. return;
  19780. }
  19781. std::shared_ptr<s_item_enchant_normal> enchants_for_enchantgrade = util::umap_find( enchant_slot->normal.enchants, (uint16)selected_item.enchantgrade );
  19782. if( enchants_for_enchantgrade == nullptr ){
  19783. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 829 ), false, SELF); // Enchanting is not possible for your item's enchant grade.
  19784. clif_enchantwindow_result( *sd, false );
  19785. return;
  19786. }
  19787. if( sd->status.zeny < enchant_slot->normal.zeny ){
  19788. return;
  19789. }
  19790. std::unordered_map<uint16, uint16> materials;
  19791. for( const auto& entry : enchant_slot->normal.materials ){
  19792. int16 idx = pc_search_inventory( sd, entry.first );
  19793. if( idx < 0 ){
  19794. return;
  19795. }
  19796. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19797. return;
  19798. }
  19799. materials[idx] = entry.second;
  19800. }
  19801. if( pc_payzeny( sd, enchant_slot->normal.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19802. return;
  19803. }
  19804. for( const auto& entry : materials ){
  19805. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19806. return;
  19807. }
  19808. }
  19809. uint32 chance = enchant_slot->normal.chance;
  19810. for( int i = 0; i <= MAX_ENCHANTGRADE; i++ ){
  19811. chance += enchant_slot->normal.enchantgradeChanceIncrease[i];
  19812. }
  19813. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  19814. clif_enchantwindow_result( *sd, false );
  19815. return;
  19816. }
  19817. // Log removal of item
  19818. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19819. size_t maximum = 3 * enchant_slot->normal.enchants.size();
  19820. bool enchanted = false;
  19821. for( int i = 0; i < maximum; i++ ){
  19822. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  19823. if( rnd_value( 0, 10000 ) < normal_enchant->chance ){
  19824. selected_item.card[slot] = normal_enchant->item_id;
  19825. enchanted = true;
  19826. break;
  19827. }
  19828. }
  19829. if( !enchanted ){
  19830. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  19831. selected_item.card[slot] = normal_enchant->item_id;
  19832. }
  19833. // Log retrieving the item again -> with the new enchant
  19834. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  19835. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  19836. #endif
  19837. }
  19838. void clif_parse_enchantwindow_perfect( int fd, map_session_data* sd ){
  19839. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19840. const PACKET_CZ_REQUEST_PERFECT_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_PERFECT_ENCHANT*>( RFIFOP( fd, 0 ) );
  19841. if( sd->state.item_enchant_index != p->enchant_group ){
  19842. return;
  19843. }
  19844. uint16 index = server_index( p->index );
  19845. if( index >= MAX_INVENTORY ){
  19846. return;
  19847. }
  19848. if( sd->inventory_data[index] == nullptr ){
  19849. return;
  19850. }
  19851. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19852. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19853. if( enchant == nullptr ){
  19854. return;
  19855. }
  19856. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19857. return;
  19858. }
  19859. uint16 slot = MAX_SLOTS;
  19860. for( uint16 nextSlot : enchant->order ){
  19861. if( selected_item.card[nextSlot] == 0 ){
  19862. slot = nextSlot;
  19863. break;
  19864. }
  19865. }
  19866. if( slot == MAX_SLOTS ){
  19867. return;
  19868. }
  19869. if( slot < sd->inventory_data[index]->slots ){
  19870. return;
  19871. }
  19872. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19873. if( enchant_slot == nullptr ){
  19874. return;
  19875. }
  19876. std::shared_ptr<s_item_enchant_perfect> perfect_enchant = util::umap_find( enchant_slot->perfect.enchants, p->ITID );
  19877. if( perfect_enchant == nullptr ){
  19878. return;
  19879. }
  19880. if( sd->status.zeny < perfect_enchant->zeny ){
  19881. return;
  19882. }
  19883. std::unordered_map<uint16, uint16> materials;
  19884. for( const auto& entry : perfect_enchant->materials ){
  19885. int16 idx = pc_search_inventory( sd, entry.first );
  19886. if( idx < 0 ){
  19887. return;
  19888. }
  19889. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19890. return;
  19891. }
  19892. materials[idx] = entry.second;
  19893. }
  19894. if( pc_payzeny( sd, perfect_enchant->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19895. return;
  19896. }
  19897. for( const auto& entry : materials ){
  19898. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19899. return;
  19900. }
  19901. }
  19902. // Log removal of item
  19903. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19904. selected_item.card[slot] = perfect_enchant->item_id;
  19905. // Log retrieving the item again -> with the new enchant
  19906. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  19907. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  19908. #endif
  19909. }
  19910. void clif_parse_enchantwindow_upgrade( int fd, map_session_data* sd ){
  19911. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19912. const PACKET_CZ_REQUEST_UPGRADE_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_UPGRADE_ENCHANT*>( RFIFOP( fd, 0 ) );
  19913. if( sd->state.item_enchant_index != p->enchant_group ){
  19914. return;
  19915. }
  19916. uint16 index = server_index( p->index );
  19917. if( index >= MAX_INVENTORY ){
  19918. return;
  19919. }
  19920. if( sd->inventory_data[index] == nullptr ){
  19921. return;
  19922. }
  19923. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19924. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19925. if( enchant == nullptr ){
  19926. return;
  19927. }
  19928. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19929. return;
  19930. }
  19931. uint16 slot = p->slot;
  19932. if( slot >= MAX_SLOTS ){
  19933. return;
  19934. }
  19935. if( slot < sd->inventory_data[index]->slots ){
  19936. return;
  19937. }
  19938. if( selected_item.card[slot] == 0 ){
  19939. return;
  19940. }
  19941. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19942. if( enchant_slot == nullptr ){
  19943. return;
  19944. }
  19945. std::shared_ptr<s_item_enchant_upgrade> upgrade = util::umap_find( enchant_slot->upgrade.enchants, selected_item.card[slot] );
  19946. if( upgrade == nullptr ){
  19947. return;
  19948. }
  19949. if( sd->status.zeny < upgrade->zeny ){
  19950. return;
  19951. }
  19952. std::unordered_map<uint16, uint16> materials;
  19953. for( const auto& entry : upgrade->materials ){
  19954. int16 idx = pc_search_inventory( sd, entry.first );
  19955. if( idx < 0 ){
  19956. return;
  19957. }
  19958. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19959. return;
  19960. }
  19961. materials[idx] = entry.second;
  19962. }
  19963. if( pc_payzeny( sd, upgrade->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19964. return;
  19965. }
  19966. for( const auto& entry : materials ){
  19967. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19968. return;
  19969. }
  19970. }
  19971. // Log removal of item
  19972. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19973. selected_item.card[slot] = upgrade->upgrade_item_id;
  19974. // Log retrieving the item again -> with the new enchant
  19975. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  19976. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  19977. #endif
  19978. }
  19979. void clif_parse_enchantwindow_reset( int fd, map_session_data* sd ){
  19980. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19981. const PACKET_CZ_REQUEST_RESET_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RESET_ENCHANT*>( RFIFOP( fd, 0 ) );
  19982. if( sd->state.item_enchant_index != p->enchant_group ){
  19983. return;
  19984. }
  19985. uint16 index = server_index( p->index );
  19986. if( index >= MAX_INVENTORY ){
  19987. return;
  19988. }
  19989. if( sd->inventory_data[index] == nullptr ){
  19990. return;
  19991. }
  19992. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19993. if( selected_item.equip != 0 ){
  19994. return;
  19995. }
  19996. if( selected_item.equipSwitch != 0 ){
  19997. return;
  19998. }
  19999. if( selected_item.attribute != 0 ){
  20000. return;
  20001. }
  20002. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  20003. if( enchant == nullptr ){
  20004. return;
  20005. }
  20006. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  20007. return;
  20008. }
  20009. if( selected_item.refine < enchant->minimumRefine ){
  20010. return;
  20011. }
  20012. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  20013. return;
  20014. }
  20015. bool is_enchanted = false;
  20016. for( int i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20017. if( selected_item.card[i] != 0 ){
  20018. is_enchanted = true;
  20019. break;
  20020. }
  20021. }
  20022. if( !is_enchanted ){
  20023. return;
  20024. }
  20025. if( sd->status.zeny < enchant->reset.zeny ){
  20026. return;
  20027. }
  20028. std::unordered_map<uint16, uint16> materials;
  20029. for( const auto& entry : enchant->reset.materials ){
  20030. int16 idx = pc_search_inventory( sd, entry.first );
  20031. if( idx < 0 ){
  20032. return;
  20033. }
  20034. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20035. return;
  20036. }
  20037. materials[idx] = entry.second;
  20038. }
  20039. if( pc_payzeny( sd, enchant->reset.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20040. return;
  20041. }
  20042. for( const auto& entry : materials ){
  20043. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20044. return;
  20045. }
  20046. }
  20047. uint32 chance = enchant->reset.chance;
  20048. if( chance == 0 ){
  20049. return;
  20050. }
  20051. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  20052. clif_enchantwindow_result( *sd, false );
  20053. return;
  20054. }
  20055. // Log removal of item
  20056. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20057. for( int i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20058. selected_item.card[i] = 0;
  20059. }
  20060. // Log retrieving the item again -> with the new enchant
  20061. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20062. clif_enchantwindow_result( *sd, true );
  20063. #endif
  20064. }
  20065. void clif_parse_enchantwindow_close( int fd, map_session_data* sd ){
  20066. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20067. sd->state.item_enchant_index = 0;
  20068. #endif
  20069. }
  20070. void clif_parse_itempackage_select( int fd, map_session_data* sd ){
  20071. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220316
  20072. const PACKET_CZ_USE_PACKAGEITEM* p = reinterpret_cast<PACKET_CZ_USE_PACKAGEITEM*>( RFIFOP( fd, 0 ) );
  20073. if( p->AID != sd->status.account_id ){
  20074. return;
  20075. }
  20076. uint16 index = server_index( p->index );
  20077. if( index >= MAX_INVENTORY ){
  20078. return;
  20079. }
  20080. if( sd->inventory_data[index] == nullptr ){
  20081. return;
  20082. }
  20083. if( sd->inventory_data[index]->nameid != p->itemID ){
  20084. return;
  20085. }
  20086. if( sd->inventory_data[index]->elv > sd->status.base_level ){
  20087. return;
  20088. }
  20089. std::shared_ptr<s_item_package> package = item_package_db.find( p->itemID );
  20090. if( package == nullptr ){
  20091. return;
  20092. }
  20093. std::shared_ptr<s_item_package_group> group = util::umap_find( package->groups, p->BoxIndex );
  20094. if( group == nullptr ){
  20095. return;
  20096. }
  20097. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_PACKAGE ) != 0 ){
  20098. return;
  20099. }
  20100. for( const auto& entry : group->items ){
  20101. struct item item = {};
  20102. item.nameid = entry.second->item_id;
  20103. item.identify = 1;
  20104. item.refine = (char)entry.second->refine;
  20105. if( entry.second->rentalhours ){
  20106. item.expire_time = (uint32)( time( nullptr ) + entry.second->rentalhours * 3600 );
  20107. }
  20108. // Check if it is a pet egg
  20109. std::shared_ptr<s_pet_db> pet = pet_db_search( item.nameid, PET_EGG );
  20110. if( pet != nullptr ){
  20111. for( int i = 0; i < entry.second->amount; i++ ){
  20112. pet_create_egg( sd, item.nameid );
  20113. }
  20114. }else if( entry.second->amount > 1 && ( !itemdb_isstackable( item.nameid ) || item.expire_time > 0 ) ){
  20115. for( int i = 0; i < entry.second->amount; i++ ){
  20116. // New random options on each iteration
  20117. if( entry.second->randomOptionGroup != nullptr ){
  20118. entry.second->randomOptionGroup->apply( item );
  20119. }
  20120. pc_additem( sd, &item, 1, LOG_TYPE_PACKAGE );
  20121. }
  20122. }else{
  20123. if( entry.second->randomOptionGroup != nullptr ){
  20124. entry.second->randomOptionGroup->apply( item );
  20125. }
  20126. pc_additem( sd, &item, entry.second->amount, LOG_TYPE_PACKAGE );
  20127. }
  20128. }
  20129. #endif
  20130. }
  20131. void clif_broadcast_refine_result(map_session_data& sd, t_itemid itemId, int8 level, bool success)
  20132. {
  20133. #if PACKETVER_MAIN_NUM >= 20170906 || PACKETVER_RE_NUM >= 20170830 || defined(PACKETVER_ZERO)
  20134. PACKET_ZC_BROADCAST_ITEMREFINING_RESULT p{};
  20135. p.packetType = HEADER_ZC_BROADCAST_ITEMREFINING_RESULT;
  20136. p.itemId = itemId;
  20137. p.refine_level = level;
  20138. p.status = (int8)success;
  20139. if (battle_config.broadcast_hide_name) {
  20140. std::string dispname = clif_hide_name(sd.status.name);
  20141. safestrncpy(p.name, dispname.c_str(), sizeof(p.name));
  20142. }
  20143. else {
  20144. safestrncpy(p.name, sd.status.name, sizeof(p.name));
  20145. }
  20146. clif_send(&p, sizeof(p), &sd.bl, ALL_CLIENT);
  20147. #endif
  20148. }
  20149. void clif_parse_captcha_register(int fd, map_session_data *sd) {
  20150. #if PACKETVER >= 20160316
  20151. nullpo_retv(sd);
  20152. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20153. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20154. return;
  20155. }
  20156. const PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20157. pc_macro_captcha_register(*sd, p->imageSize, p->answer);
  20158. #endif
  20159. }
  20160. void clif_captcha_upload_request(map_session_data &sd) {
  20161. #if PACKETVER >= 20160330
  20162. PACKET_ZC_ACK_UPLOAD_MACRO_DETECTOR p = {};
  20163. p.PacketType = HEADER_ZC_ACK_UPLOAD_MACRO_DETECTOR;
  20164. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20165. if (sd.captcha_upload.cd != nullptr) {
  20166. p.captchaFlag = 0;
  20167. } else {
  20168. p.captchaFlag = 1;
  20169. }
  20170. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20171. #endif
  20172. }
  20173. void clif_parse_captcha_upload(int fd, map_session_data *sd) {
  20174. #if PACKETVER >= 20160316
  20175. nullpo_retv(sd);
  20176. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20177. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20178. return;
  20179. }
  20180. const PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20181. int16 upload_size = p->PacketLength - sizeof( * p );
  20182. if (upload_size < 1 || upload_size > MAX_CAPTCHA_CHUNK_SIZE)
  20183. return;
  20184. if (sd->captcha_upload.upload_size + upload_size > sd->captcha_upload.cd->image_size)
  20185. return;
  20186. pc_macro_captcha_register_upload(*sd, upload_size, p->imageData);
  20187. #endif
  20188. }
  20189. void clif_captcha_upload_end(map_session_data &sd) {
  20190. #if PACKETVER >= 20160330
  20191. PACKET_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA p = {};
  20192. p.PacketType = HEADER_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA;
  20193. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20194. #endif
  20195. }
  20196. void clif_parse_captcha_preview_request(int fd, map_session_data *sd) {
  20197. #if PACKETVER >= 20160323
  20198. nullpo_retv(sd);
  20199. if (!(pc_has_permission(sd, PC_PERM_MACRO_REGISTER) && pc_has_permission(sd, PC_PERM_MACRO_DETECT))) {
  20200. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20201. return;
  20202. }
  20203. const PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20204. clif_captcha_preview_response(*sd, captcha_db.find(p->captchaID));
  20205. #endif
  20206. }
  20207. void clif_captcha_preview_response(map_session_data &sd, std::shared_ptr<s_captcha_data> cd) {
  20208. #if PACKETVER >= 20160330
  20209. PACKET_ZC_ACK_PREVIEW_MACRO_DETECTOR p = {};
  20210. p.PacketType = HEADER_ZC_ACK_PREVIEW_MACRO_DETECTOR;
  20211. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20212. if (cd == nullptr) {
  20213. p.captchaFlag = 1;
  20214. p.imageSize = 0;
  20215. } else {
  20216. p.captchaFlag = 0;
  20217. p.imageSize = cd->image_size;
  20218. }
  20219. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20220. if (cd != nullptr) {
  20221. for (uint16 offset = 0; offset < cd->image_size;) {
  20222. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20223. PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20224. p2->PacketType = HEADER_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA;
  20225. p2->PacketLength = sizeof( *p2 );
  20226. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20227. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20228. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20229. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20230. offset += chunk_size;
  20231. }
  20232. }
  20233. #endif
  20234. }
  20235. void clif_macro_detector_request(map_session_data &sd) {
  20236. #if PACKETVER >= 20160330
  20237. std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  20238. if (cd == nullptr) {
  20239. return;
  20240. }
  20241. // Send preview initialization request to the client.
  20242. PACKET_ZC_APPLY_MACRO_DETECTOR p = {};
  20243. p.PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR;
  20244. p.imageSize = cd->image_size;
  20245. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20246. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20247. for (uint16 offset = 0; offset < cd->image_size;) {
  20248. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20249. PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20250. p2->PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR_CAPTCHA;
  20251. p2->PacketLength = sizeof( *p2 );
  20252. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20253. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20254. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20255. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20256. offset += chunk_size;
  20257. }
  20258. #endif
  20259. }
  20260. void clif_macro_detector_request_show(map_session_data &sd) {
  20261. #if PACKETVER >= 20160330
  20262. PACKET_ZC_REQ_ANSWER_MACRO_DETECTOR p = {};
  20263. p.PacketType = HEADER_ZC_REQ_ANSWER_MACRO_DETECTOR;
  20264. p.retryCount = sd.macro_detect.retry;
  20265. p.timeout = battle_config.macro_detection_timeout;
  20266. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20267. #endif
  20268. }
  20269. void clif_parse_macro_detector_download_ack(int fd, map_session_data *sd) {
  20270. #if PACKETVER >= 20160316
  20271. nullpo_retv(sd);
  20272. if (sd->macro_detect.retry != 0) {
  20273. //const PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20274. clif_macro_detector_request_show(*sd);
  20275. }
  20276. #endif
  20277. }
  20278. void clif_parse_macro_detector_answer(int fd, map_session_data *sd) {
  20279. #if PACKETVER >= 20160316
  20280. nullpo_retv(sd);
  20281. const PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20282. pc_macro_detector_process_answer(*sd, p->answer);
  20283. #endif
  20284. }
  20285. void clif_macro_detector_status(map_session_data &sd, e_macro_detect_status stype) {
  20286. #if PACKETVER >= 20160330
  20287. PACKET_ZC_CLOSE_MACRO_DETECTOR p = {};
  20288. p.PacketType = HEADER_ZC_CLOSE_MACRO_DETECTOR;
  20289. p.status = stype;
  20290. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20291. #endif
  20292. }
  20293. void clif_parse_macro_reporter_select(int fd, map_session_data *sd) {
  20294. #if PACKETVER >= 20160330
  20295. nullpo_retv(sd);
  20296. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20297. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20298. return;
  20299. }
  20300. const PACKET_CZ_REQ_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_CZ_REQ_PLAYER_AID_IN_RANGE*>( RFIFOP( fd, 0 ) );
  20301. pc_macro_reporter_area_select(*sd, p->xPos, p->yPos, p->RadiusRange);
  20302. #endif
  20303. }
  20304. void clif_macro_reporter_select(map_session_data &sd, const std::vector<uint32> &aid_list) {
  20305. #if PACKETVER >= 20160330
  20306. PACKET_ZC_ACK_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_ZC_ACK_PLAYER_AID_IN_RANGE*>( packet_buffer );
  20307. p->PacketType = HEADER_ZC_ACK_PLAYER_AID_IN_RANGE;
  20308. p->PacketLength = sizeof( *p );
  20309. for (size_t i = 0; i < aid_list.size(); i++){
  20310. p->AID[i] = aid_list[static_cast<int32>(i)];
  20311. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->AID[0] ) );
  20312. }
  20313. clif_send(p, p->PacketLength, &sd.bl, SELF);
  20314. #endif
  20315. }
  20316. void clif_parse_macro_reporter_ack(int fd, map_session_data *sd) {
  20317. #if PACKETVER >= 20160316
  20318. nullpo_retv(sd);
  20319. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20320. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20321. return;
  20322. }
  20323. const PACKET_CZ_REQ_APPLY_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20324. map_session_data *tsd = map_id2sd(p->AID);
  20325. if (tsd == nullptr) {
  20326. clif_displaymessage(fd, msg_txt(sd, 3)); // Character not found.
  20327. return;
  20328. }
  20329. if (tsd->macro_detect.retry != 0) {
  20330. clif_macro_reporter_status(*sd, MCR_INPROGRESS);
  20331. return;
  20332. }
  20333. if (captcha_db.empty()) {
  20334. clif_macro_reporter_status(*sd, MCR_NO_DATA);
  20335. return;
  20336. }
  20337. pc_macro_reporter_process(*tsd, sd->status.account_id);
  20338. clif_macro_reporter_status(*sd, MCR_MONITORING);
  20339. #endif
  20340. }
  20341. void clif_macro_reporter_status(map_session_data &sd, e_macro_report_status stype) {
  20342. #if PACKETVER >= 20160330
  20343. PACKET_ZC_ACK_APPLY_MACRO_DETECTOR p = {};
  20344. p.PacketType = HEADER_ZC_ACK_APPLY_MACRO_DETECTOR;
  20345. p.status = stype;
  20346. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20347. #endif
  20348. }
  20349. void clif_dynamicnpc_result( map_session_data& sd, e_dynamicnpc_result result ){
  20350. #if PACKETVER_MAIN_NUM >= 20140430 || PACKETVER_RE_NUM >= 20140430 || defined(PACKETVER_ZERO)
  20351. PACKET_ZC_DYNAMICNPC_CREATE_RESULT p = {};
  20352. p.PacketType = HEADER_ZC_DYNAMICNPC_CREATE_RESULT;
  20353. p.result = result;
  20354. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20355. #endif
  20356. }
  20357. void clif_partybooking_ask( map_session_data* sd, map_session_data* joining_sd ){
  20358. #if PACKETVER >= 20191204
  20359. PACKET_ZC_PARTY_REQ_MASTER_TO_JOIN p = {};
  20360. p.packetType = HEADER_ZC_PARTY_REQ_MASTER_TO_JOIN;
  20361. p.CID = joining_sd->status.char_id;
  20362. p.AID = joining_sd->status.account_id;
  20363. safestrncpy( p.name, joining_sd->status.name, NAME_LENGTH );
  20364. p.x = joining_sd->status.base_level;
  20365. p.y = joining_sd->status.class_;
  20366. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20367. #endif
  20368. }
  20369. void clif_parse_partybooking_join( int fd, map_session_data* sd ){
  20370. #if PACKETVER >= 20191204
  20371. const PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20372. // Character is already in a party
  20373. if( sd->status.party_id != 0 ){
  20374. return;
  20375. }
  20376. map_session_data* tsd = map_charid2sd( p->CID );
  20377. // Target player is offline
  20378. if( tsd == nullptr ){
  20379. return;
  20380. }
  20381. if( tsd->status.account_id != p->AID ){
  20382. return;
  20383. }
  20384. struct s_party_booking_requirement requirement;
  20385. if( !party_booking_load( tsd->status.account_id, tsd->status.char_id, &requirement ) ){
  20386. return;
  20387. }
  20388. if( sd->status.base_level < requirement.minimum_level ){
  20389. return;
  20390. }
  20391. if( sd->status.base_level > requirement.maximum_level ){
  20392. return;
  20393. }
  20394. // Already requested to join this party
  20395. if( util::vector_exists( tsd->party_booking_requests, sd->status.char_id ) ){
  20396. return;
  20397. }
  20398. // Store information that the player tried to join the party
  20399. tsd->party_booking_requests.push_back( sd->status.char_id );
  20400. clif_partybooking_ask( tsd, sd );
  20401. #endif
  20402. }
  20403. void clif_partybooking_reply( map_session_data* sd, map_session_data* party_leader_sd, bool accepted ){
  20404. #if PACKETVER >= 20191204
  20405. PACKET_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER p = {};
  20406. if( party_leader_sd->status.party_id == 0 ){
  20407. return;
  20408. }
  20409. struct party_data* party = party_search( party_leader_sd->status.party_id );
  20410. if( party == nullptr ){
  20411. return;
  20412. }
  20413. p.packetType = HEADER_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER;
  20414. safestrncpy( p.player_name, party_leader_sd->status.name, NAME_LENGTH );
  20415. safestrncpy( p.party_name, party->party.name, NAME_LENGTH );
  20416. p.AID = party_leader_sd->status.account_id;
  20417. p.refused = !accepted;
  20418. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20419. #endif
  20420. }
  20421. void clif_parse_partybooking_reply( int fd, map_session_data* sd ){
  20422. #if PACKETVER >= 20191204
  20423. const PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20424. map_session_data* tsd = map_charid2sd( p->CID );
  20425. // Target player is offline
  20426. if( tsd == nullptr ){
  20427. return;
  20428. }
  20429. if( tsd->status.account_id != p->AID ){
  20430. return;
  20431. }
  20432. // Check if the player even requested to join the party
  20433. if( !util::vector_exists( sd->party_booking_requests, p->CID ) ){
  20434. return;
  20435. }
  20436. util::vector_erase_if_exists( sd->party_booking_requests, p->CID );
  20437. // Only party leaders can reply
  20438. if( !party_isleader( sd ) ){
  20439. return;
  20440. }
  20441. struct party_data* party = party_search( sd->status.party_id );
  20442. if( party == nullptr ){
  20443. return;
  20444. }
  20445. if( party->instance_id > 0 && battle_config.instance_block_invite ){
  20446. return;
  20447. }
  20448. if( p->accept ){
  20449. party_join( *tsd, sd->status.party_id );
  20450. }
  20451. clif_partybooking_reply( tsd, sd, p->accept );
  20452. #endif
  20453. }
  20454. void clif_parse_reset_skill( int fd, map_session_data* sd ){
  20455. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220203
  20456. const PACKET_CZ_RESET_SKILL* p = reinterpret_cast<PACKET_CZ_RESET_SKILL*>( RFIFOP( fd, 0 ) );
  20457. #endif
  20458. }
  20459. void clif_set_dialog_align(map_session_data& sd, int npcid, e_say_dialog_align align)
  20460. {
  20461. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20462. PACKET_ZC_DIALOG_TEXT_ALIGN p = {};
  20463. p.PacketType = HEADER_ZC_DIALOG_TEXT_ALIGN;
  20464. p.align = align;
  20465. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20466. #endif // PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20467. }
  20468. void clif_set_npc_window_size(map_session_data& sd, int width, int height)
  20469. {
  20470. #if PACKETVER_MAIN_NUM >= 20220504
  20471. PACKET_ZC_DIALOG_WINDOW_SIZE p = {};
  20472. p.PacketType = HEADER_ZC_DIALOG_WINDOW_SIZE;
  20473. p.width = width;
  20474. p.height = height;
  20475. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20476. #endif // PACKETVER_MAIN_NUM >= 20220504
  20477. }
  20478. void clif_set_npc_window_pos(map_session_data& sd, int x, int y)
  20479. {
  20480. #if PACKETVER_MAIN_NUM >= 20220504
  20481. PACKET_ZC_DIALOG_WINDOW_POS p = {};
  20482. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS;
  20483. p.x = x;
  20484. p.y = y;
  20485. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20486. #endif // PACKETVER_MAIN_NUM >= 20220504
  20487. }
  20488. void clif_set_npc_window_pos_percent(map_session_data& sd, int x, int y)
  20489. {
  20490. #if PACKETVER_MAIN_NUM >= 20220504
  20491. PACKET_ZC_DIALOG_WINDOW_POS2 p = {};
  20492. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS2;
  20493. p.x = x;
  20494. p.y = y;
  20495. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20496. #endif // PACKETVER_MAIN_NUM >= 20220504
  20497. }
  20498. /// Displays a special popup.
  20499. /// Works only if player moved from one map to another.
  20500. /// 0bbe <popup id>.L (ZC_SPECIALPOPUP)
  20501. void clif_specialpopup(map_session_data& sd, int32 id ){
  20502. #if PACKETVER >= 20221005
  20503. PACKET_ZC_SPECIALPOPUP p = {};
  20504. p.PacketType = HEADER_ZC_SPECIALPOPUP;
  20505. p.ppId = id;
  20506. clif_send( &p, sizeof( p ), &sd.bl, SELF);
  20507. #endif
  20508. }
  20509. /*==========================================
  20510. * Main client packet processing function
  20511. *------------------------------------------*/
  20512. static int clif_parse(int fd)
  20513. {
  20514. int cmd, packet_len;
  20515. TBL_PC* sd;
  20516. int pnum;
  20517. #ifdef PACKET_OBFUSCATION
  20518. int cmd2;
  20519. #endif
  20520. //TODO apply delays or disconnect based on packet throughput [FlavioJS]
  20521. // Note: "click masters" can do 80+ clicks in 10 seconds
  20522. 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]
  20523. { // begin main client packet processing loop
  20524. sd = (TBL_PC *)session[fd]->session_data;
  20525. if (session[fd]->flag.eof) {
  20526. if (sd) {
  20527. if (sd->state.autotrade) {
  20528. //Disassociate character from the socket connection.
  20529. session[fd]->session_data = nullptr;
  20530. sd->fd = 0;
  20531. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off (using @autotrade).\n", sd->status.name);
  20532. } else
  20533. if (sd->state.active) {
  20534. // Player logout display [Valaris]
  20535. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off.\n", sd->status.name);
  20536. clif_quitsave(fd, sd);
  20537. } else {
  20538. //Unusual logout (during log on/off/map-changer procedure)
  20539. ShowInfo("Player AID:%d/CID:%d logged off.\n", sd->status.account_id, sd->status.char_id);
  20540. map_quit(sd);
  20541. }
  20542. } else {
  20543. ShowInfo("Closed connection from '" CL_WHITE "%s" CL_RESET "'.\n", ip2str(session[fd]->client_addr, nullptr));
  20544. }
  20545. do_close(fd);
  20546. return 0;
  20547. }
  20548. if (RFIFOREST(fd) < 2)
  20549. return 0;
  20550. cmd = RFIFOW(fd, 0);
  20551. #ifdef PACKET_OBFUSCATION
  20552. // Check if it is a player that tries to connect to the map server.
  20553. if( sd ){
  20554. // Decrypt the current packet ID with the last key stored in the session.
  20555. cmd = (cmd ^ ((sd->cryptKey >> 16) & 0x7FFF));
  20556. }else{
  20557. // Store the original value for checking
  20558. cmd2 = cmd;
  20559. // A player tries to connect - use the initial keys for the decryption of the packet ID.
  20560. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20561. }
  20562. #endif
  20563. // filter out invalid / unsupported packets
  20564. if (cmd > MAX_PACKET_DB || cmd < MIN_PACKET_DB || packet_db[cmd].len == 0) {
  20565. ShowWarning("clif_parse: Received unsupported packet (packet 0x%04x, %d bytes received), disconnecting session #%d.\n", cmd, RFIFOREST(fd), fd);
  20566. #ifdef DUMP_INVALID_PACKET
  20567. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20568. #endif
  20569. #ifdef PACKET_OBFUSCATION_WARN
  20570. // If it is a connection attempt, check if the packet would have been valid without decrypting it
  20571. if( !sd ){
  20572. #ifdef PACKET_OBFUSCATION
  20573. if( cmd2 >= MIN_PACKET_DB && cmd2 < MAX_PACKET_DB && packet_db[cmd2].len != 0 && packet_db[cmd2].func == clif_parse_WantToConnection ){
  20574. ShowWarning( "clif_parse: It looks like you have enabled PACKET_OBFUSCATION on server side, but disabled it on client side.\n" );
  20575. }
  20576. #else
  20577. // Try to use the initial keys for the decryption of the packet ID.
  20578. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20579. if( cmd >= MIN_PACKET_DB && cmd < MAX_PACKET_DB && packet_db[cmd].len != 0 && packet_db[cmd].func == clif_parse_WantToConnection ){
  20580. ShowWarning( "clif_parse: It looks like you have disabled PACKET_OBFUSCATION on server side, but enabled it on client side.\n" );
  20581. }
  20582. #endif
  20583. }
  20584. #endif
  20585. set_eof(fd);
  20586. return 0;
  20587. }
  20588. // determine real packet length
  20589. packet_len = packet_db[cmd].len;
  20590. if (packet_len == -1) { // variable-length packet
  20591. if (RFIFOREST(fd) < 4)
  20592. return 0;
  20593. packet_len = RFIFOW(fd,2);
  20594. if (packet_len < 4 || packet_len > 32768) {
  20595. ShowWarning("clif_parse: Received packet 0x%04x specifies invalid packet_len (%d), disconnecting session #%d.\n", cmd, packet_len, fd);
  20596. #ifdef DUMP_INVALID_PACKET
  20597. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20598. #endif
  20599. set_eof(fd);
  20600. return 0;
  20601. }
  20602. }
  20603. if ((int)RFIFOREST(fd) < packet_len){
  20604. 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 );
  20605. #ifdef DUMP_INVALID_PACKET
  20606. ShowDump( RFIFOP( fd, 0 ), RFIFOREST( fd ) );
  20607. #endif
  20608. set_eof( fd );
  20609. return 0; // not enough data received to form the packet
  20610. }
  20611. #ifdef PACKET_OBFUSCATION
  20612. RFIFOW(fd, 0) = cmd;
  20613. if (sd)
  20614. sd->cryptKey = ((sd->cryptKey * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF; // Update key for the next packet
  20615. #endif
  20616. if( packet_db[cmd].func == clif_parse_debug )
  20617. packet_db[cmd].func(fd, sd);
  20618. else if( packet_db[cmd].func != nullptr ) {
  20619. if( !sd && packet_db[cmd].func != clif_parse_WantToConnection )
  20620. ; //Only valid packet when there is no session
  20621. else
  20622. if( sd && sd->bl.prev == nullptr && packet_db[cmd].func != clif_parse_LoadEndAck )
  20623. ; //Only valid packet when player is not on a map
  20624. else
  20625. packet_db[cmd].func(fd, sd);
  20626. }
  20627. #ifdef DUMP_UNKNOWN_PACKET
  20628. else DumpUnknown(fd,sd,cmd,packet_len);
  20629. #endif
  20630. RFIFOSKIP(fd, packet_len);
  20631. }; // main loop end
  20632. return 0;
  20633. }
  20634. void packetdb_addpacket( uint16 cmd, uint16 length, void (*func)(int, map_session_data *), ... ){
  20635. va_list argp;
  20636. int i;
  20637. if(cmd <= 0 || cmd > MAX_PACKET_DB)
  20638. return;
  20639. packet_db[cmd].len = length;
  20640. packet_db[cmd].func = func;
  20641. va_start(argp, func);
  20642. for( i = 0; i < MAX_PACKET_POS; i++ ){
  20643. int offset = va_arg(argp, int);
  20644. if( offset == 0 ){
  20645. break;
  20646. }
  20647. packet_db[cmd].pos[i] = offset;
  20648. }
  20649. if( i == MAX_PACKET_POS ){
  20650. ShowError( "Too many positions found for packet 0x%04x (max=%d).\n", cmd, MAX_PACKET_POS );
  20651. }
  20652. va_end(argp);
  20653. }
  20654. /*==========================================
  20655. * Reads packets and setups its array reference
  20656. *------------------------------------------*/
  20657. void packetdb_readdb(){
  20658. memset(packet_db,0,sizeof(packet_db));
  20659. #include "clif_packetdb.hpp"
  20660. #include "clif_shuffle.hpp"
  20661. ShowStatus("Using packet version: " CL_WHITE "%d" CL_RESET ".\n", PACKETVER);
  20662. #ifdef PACKET_OBFUSCATION
  20663. 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]);
  20664. #else
  20665. ShowStatus("Packet Obfuscation: " CL_RED "Disabled" CL_RESET ".\n");
  20666. #endif
  20667. }
  20668. /*==========================================
  20669. *
  20670. *------------------------------------------*/
  20671. void do_init_clif(void) {
  20672. const int colors[COLOR_MAX] = {
  20673. 0x00FF00, // COLOR_DEFAULT
  20674. 0xFF0000, // COLOR_RED
  20675. 0xFFFFFF, // COLOR_WHITE
  20676. 0xFFFF00, // COLOR_YELLOW
  20677. 0x00FFFF, // COLOR_CYAN
  20678. 0xB5FFB5, // COLOR_LIGHT_GREEN
  20679. 0xFFFF63, // COLOR_LIGHT_YELLOW
  20680. };
  20681. /**
  20682. * Setup Color Table (saves unnecessary load of strtoul on every call)
  20683. **/
  20684. for( int i = 0; i < COLOR_MAX; i++ ){
  20685. color_table[i] = ( colors[i] & 0x0000FF ) << 16 | ( colors[i] & 0x00FF00 ) | ( colors[i] & 0xFF0000 ) >> 16; //RGB to BGR
  20686. }
  20687. packetdb_readdb();
  20688. set_defaultparse(clif_parse);
  20689. if( make_listen_bind(bind_ip,map_port) == -1 ) {
  20690. ShowFatalError("Failed to bind to port '" CL_WHITE "%d" CL_RESET "'\n",map_port);
  20691. exit(EXIT_FAILURE);
  20692. }
  20693. add_timer_func_list(clif_clearunit_delayed_sub, "clif_clearunit_delayed_sub");
  20694. add_timer_func_list(clif_delayquit, "clif_delayquit");
  20695. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320
  20696. add_timer_func_list( clif_ping_timer, "clif_ping_timer" );
  20697. if( battle_config.ping_timer_interval > 0 ){
  20698. add_timer_interval( gettick() + battle_config.ping_timer_interval * 1000, clif_ping_timer, 0, 0, battle_config.ping_timer_interval * 1000 );
  20699. }
  20700. #endif
  20701. delay_clearunit_ers = ers_new(sizeof(struct block_list),"clif.cpp::delay_clearunit_ers",ERS_OPT_CLEAR);
  20702. }
  20703. void do_final_clif(void) {
  20704. ers_destroy(delay_clearunit_ers);
  20705. }