clif.cpp 744 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479
  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 uint32 mes_len_check(char* mes, uint32 len, uint32 max) {
  213. if( len > max )
  214. len = max;
  215. mes[len-1] = '\0';
  216. return len;
  217. }
  218. static char map_ip_str[128];
  219. static uint32 map_ip;
  220. static uint32 bind_ip = INADDR_ANY;
  221. static uint16 map_port = 5121;
  222. static bool clif_ally_only = false;
  223. 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 a non player object.
  1303. /// 01b0 <id>.L <type>.B <value>.L (ZC_NPCSPRITE_CHANGE)
  1304. void clif_class_change( block_list& bl, int32 class_, enum send_target target, map_session_data* sd ){
  1305. if( pcdb_checkid( class_ ) ){
  1306. // player classes yield missing sprites
  1307. return;
  1308. }
  1309. PACKET_ZC_NPCSPRITE_CHANGE p = {};
  1310. p.packetType = HEADER_ZC_NPCSPRITE_CHANGE;
  1311. p.GID = bl.id;
  1312. // Unused
  1313. p.type = 0;
  1314. p.class_ = class_;
  1315. if( sd != nullptr ){
  1316. bl = sd->bl;
  1317. }
  1318. clif_send( &p, sizeof( p ), &bl, target );
  1319. }
  1320. void clif_servantball( map_session_data& sd, struct block_list* target, enum send_target send_target ){
  1321. PACKET_ZC_SPIRITS p = {};
  1322. p.PacketType = HEADER_ZC_SPIRITS;
  1323. p.AID = sd.bl.id;
  1324. p.num = sd.servantball;
  1325. if( target == nullptr ){
  1326. target = &sd.bl;
  1327. }
  1328. clif_send( &p, sizeof( p ), target, send_target );
  1329. }
  1330. void clif_abyssball( map_session_data& sd, struct block_list* target, enum send_target send_target ){
  1331. PACKET_ZC_SPIRITS p = {};
  1332. p.PacketType = HEADER_ZC_SPIRITS;
  1333. p.AID = sd.bl.id;
  1334. p.num = sd.abyssball;
  1335. if( target == nullptr ){
  1336. target = &sd.bl;
  1337. }
  1338. clif_send( &p, sizeof( p ), target, send_target );
  1339. }
  1340. /// Notifies the client of an object's Millenium Shields.
  1341. static void clif_millenniumshield_single( map_session_data& sd, map_session_data& tsd ){
  1342. #if PACKETVER >= 20081126
  1343. status_change_entry* sce = sd.sc.getSCE( SC_MILLENNIUMSHIELD );
  1344. if( sce == nullptr ){
  1345. return;
  1346. }
  1347. PACKET_ZC_MILLENNIUMSHIELD packet{};
  1348. packet.packetType = HEADER_ZC_MILLENNIUMSHIELD;
  1349. packet.aid = sd.bl.id;
  1350. packet.num = sce->val2;
  1351. packet.state = 0;
  1352. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  1353. #endif
  1354. }
  1355. /*==========================================
  1356. * Kagerou/Oboro amulet spirit
  1357. *------------------------------------------*/
  1358. static void clif_spiritcharm_single( map_session_data& sd, map_session_data& tsd ){
  1359. #if PACKETVER >= 20111102
  1360. if( sd.spiritcharm_type == CHARM_TYPE_NONE ){
  1361. return;
  1362. }
  1363. if( sd.spiritcharm <= 0 ){
  1364. return;
  1365. }
  1366. PACKET_ZC_SPIRITS_ATTRIBUTE packet{};
  1367. packet.packetType = HEADER_ZC_SPIRITS_ATTRIBUTE;
  1368. packet.aid = sd.bl.id;
  1369. packet.spiritsType = static_cast<decltype(packet.spiritsType)>(sd.spiritcharm_type);
  1370. packet.num = static_cast<decltype(packet.num)>(sd.spiritcharm);
  1371. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  1372. #endif
  1373. }
  1374. /*==========================================
  1375. * Enchanting Shadow / Shadow Scar Spirit
  1376. *------------------------------------------*/
  1377. void clif_enchantingshadow_spirit(unit_data &ud) {
  1378. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191120 || PACKETVER_ZERO_NUM >= 20191127
  1379. PACKET_ZC_TARGET_SPIRITS p = {};
  1380. p.packetType = HEADER_ZC_TARGET_SPIRITS;
  1381. p.GID = ud.bl->id;
  1382. p.unknown_val = 0;
  1383. p.amount = static_cast<uint16>(ud.shadow_scar_timer.size());
  1384. clif_send(&p, sizeof(p), ud.bl, AREA);
  1385. #endif
  1386. }
  1387. /*==========================================
  1388. * Run when player changes map / refreshes
  1389. * Tells its client to display all weather settings being used by this map
  1390. *------------------------------------------*/
  1391. static void clif_weather_check(map_session_data *sd)
  1392. {
  1393. int16 m = sd->bl.m;
  1394. int fd = sd->fd;
  1395. if (map_getmapflag(m, MF_SNOW)
  1396. || map_getmapflag(m, MF_CLOUDS)
  1397. || map_getmapflag(m, MF_FOG)
  1398. || map_getmapflag(m, MF_FIREWORKS)
  1399. || map_getmapflag(m, MF_SAKURA)
  1400. || map_getmapflag(m, MF_LEAVES)
  1401. || map_getmapflag(m, MF_CLOUDS2))
  1402. {
  1403. if (map_getmapflag(m, MF_SNOW))
  1404. clif_specialeffect_single(&sd->bl, EF_SNOW, fd);
  1405. if (map_getmapflag(m, MF_CLOUDS))
  1406. clif_specialeffect_single(&sd->bl, EF_CLOUD3, fd);
  1407. if (map_getmapflag(m, MF_CLOUDS2))
  1408. clif_specialeffect_single(&sd->bl, EF_CLOUD5, fd);
  1409. if (map_getmapflag(m, MF_FOG))
  1410. clif_specialeffect_single(&sd->bl, EF_CLOUD4, fd);
  1411. if (map_getmapflag(m, MF_FIREWORKS)) {
  1412. clif_specialeffect_single(&sd->bl, EF_POKJUK, fd);
  1413. clif_specialeffect_single(&sd->bl, EF_THROWITEM2, fd);
  1414. clif_specialeffect_single(&sd->bl, EF_POKJUK_SOUND, fd);
  1415. }
  1416. if (map_getmapflag(m, MF_SAKURA))
  1417. clif_specialeffect_single(&sd->bl, EF_SAKURA, fd);
  1418. if (map_getmapflag(m, MF_LEAVES))
  1419. clif_specialeffect_single(&sd->bl, EF_MAPLE, fd);
  1420. }
  1421. }
  1422. /**
  1423. * Run when the weather on a map changes, throws all players in map id 'm' to clif_weather_check function
  1424. **/
  1425. void clif_weather(int16 m)
  1426. {
  1427. struct s_mapiterator* iter;
  1428. map_session_data *sd=nullptr;
  1429. iter = mapit_getallusers();
  1430. for( sd = (map_session_data*)mapit_first(iter); mapit_exists(iter); sd = (map_session_data*)mapit_next(iter) )
  1431. {
  1432. if( sd->bl.m == m )
  1433. clif_weather_check(sd);
  1434. }
  1435. mapit_free(iter);
  1436. }
  1437. /**
  1438. * Hide a NPC from the effects of Maya Purple card.
  1439. * @param bl: Block data
  1440. * @return True if NPC is disabled or false otherwise
  1441. */
  1442. static inline bool clif_npc_mayapurple(block_list *bl) {
  1443. nullpo_retr(false, bl);
  1444. if (bl->type == BL_NPC) {
  1445. npc_data *nd = map_id2nd(bl->id);
  1446. // TODO: Confirm if waitingroom cause any special cases
  1447. if (/* nd->chat_id == 0 && */ nd->is_invisible)
  1448. return true;
  1449. }
  1450. return false;
  1451. }
  1452. /**
  1453. * Main function to spawn a unit on the client (player/mob/pet/etc)
  1454. **/
  1455. int clif_spawn( struct block_list *bl, bool walking ){
  1456. struct view_data *vd;
  1457. vd = status_get_viewdata(bl);
  1458. if( !vd || vd->class_ == JT_INVISIBLE )
  1459. return 0;
  1460. // Hide NPC from Maya Purple card
  1461. if (clif_npc_mayapurple(bl))
  1462. return 0;
  1463. if( bl->type == BL_NPC && !vd->dead_sit ){
  1464. clif_set_unit_idle( bl, walking, AREA_WOS, bl );
  1465. }else{
  1466. clif_spawn_unit( bl, AREA_WOS );
  1467. }
  1468. if (vd->cloth_color)
  1469. clif_refreshlook(bl,bl->id,LOOK_CLOTHES_COLOR,vd->cloth_color,AREA_WOS);
  1470. if (vd->body_style)
  1471. clif_refreshlook(bl,bl->id,LOOK_BODY2,vd->body_style,AREA_WOS);
  1472. switch (bl->type)
  1473. {
  1474. case BL_PC:
  1475. {
  1476. TBL_PC *sd = ((TBL_PC*)bl);
  1477. if (sd->spiritball > 0)
  1478. clif_spiritball(&sd->bl);
  1479. if (sd->sc.getSCE(SC_MILLENNIUMSHIELD))
  1480. clif_millenniumshield( sd->bl, sd->sc.getSCE( SC_MILLENNIUMSHIELD )->val2 );
  1481. if (sd->soulball > 0)
  1482. clif_soulball(sd);
  1483. if (sd->servantball > 0)
  1484. clif_servantball( *sd );
  1485. if (sd->abyssball > 0)
  1486. clif_abyssball( *sd );
  1487. if(sd->state.size==SZ_BIG) // tiny/big players [Valaris]
  1488. clif_specialeffect(bl,EF_GIANTBODY2,AREA);
  1489. else if(sd->state.size==SZ_MEDIUM)
  1490. clif_specialeffect(bl,EF_BABYBODY2,AREA);
  1491. if( sd->bg_id && map_getmapflag(sd->bl.m, MF_BATTLEGROUND) )
  1492. clif_sendbgemblem_area(sd);
  1493. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  1494. clif_spiritcharm( *sd );
  1495. if (sd->status.robe)
  1496. clif_refreshlook(bl,bl->id,LOOK_ROBE,sd->status.robe,AREA);
  1497. clif_efst_status_change_sub(bl, bl, AREA);
  1498. clif_hat_effects( *sd, sd->bl, AREA );
  1499. }
  1500. break;
  1501. case BL_MOB:
  1502. {
  1503. TBL_MOB *md = ((TBL_MOB*)bl);
  1504. if(md->special_state.size==SZ_BIG) // tiny/big mobs [Valaris]
  1505. clif_specialeffect(&md->bl,EF_GIANTBODY2,AREA);
  1506. else if(md->special_state.size==SZ_MEDIUM)
  1507. clif_specialeffect(&md->bl,EF_BABYBODY2,AREA);
  1508. if ( md->special_state.ai == AI_ABR || md->special_state.ai == AI_BIONIC )
  1509. clif_summon_init(*md);
  1510. }
  1511. break;
  1512. case BL_NPC:
  1513. {
  1514. TBL_NPC *nd = ((TBL_NPC*)bl);
  1515. if( nd->size == SZ_BIG )
  1516. clif_specialeffect(&nd->bl,EF_GIANTBODY2,AREA);
  1517. else if( nd->size == SZ_MEDIUM )
  1518. clif_specialeffect(&nd->bl,EF_BABYBODY2,AREA);
  1519. clif_efst_status_change_sub(bl, bl, AREA);
  1520. clif_progressbar_npc_area(nd);
  1521. }
  1522. break;
  1523. case BL_PET:
  1524. if (vd->head_bottom)
  1525. clif_pet_equip_area((TBL_PET*)bl); // needed to display pet equip properly
  1526. break;
  1527. }
  1528. return 0;
  1529. }
  1530. /// Notifies client of a change in an homunculus' status parameter.
  1531. /// 0x7db <type>.W <value>.L (ZC_HO_PAR_CHANGE)
  1532. /// 0xba5 <type>.W <value>.Q (ZC_HO_PAR_CHANGE2)
  1533. void clif_homunculus_updatestatus(map_session_data& sd, _sp type) {
  1534. #if PACKETVER >= 20090610
  1535. if( !hom_is_active(sd.hd) )
  1536. return;
  1537. PACKET_ZC_HO_PAR_CHANGE p = {};
  1538. p.packetType = HEADER_ZC_HO_PAR_CHANGE;
  1539. p.type = static_cast<decltype(p.type)>(type);
  1540. status_data* status = &sd.hd->battle_status;
  1541. switch (type) {
  1542. case SP_BASEEXP:
  1543. p.value = static_cast<decltype(p.value)>( std::min<decltype(sd.hd->homunculus.exp)>( sd.hd->homunculus.exp, std::numeric_limits<decltype(p.value)>::max() ) );
  1544. break;
  1545. case SP_HP:
  1546. // Homunculus HP and SP bars will screw up if the percentage calculation exceeds signed values
  1547. // Any value above will screw up the bars
  1548. // Since max values depend on PACKET_ZC_PROPERTY_HOMUN we check for each version of that packet
  1549. #if PACKETVER_MAIN_NUM >= 20200819 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20190626
  1550. // Tested maximum: 2147483647 (INT32_MAX)
  1551. if (status->max_hp > INT32_MAX)
  1552. #elif PACKETVER_MAIN_NUM >= 20131230 || PACKETVER_RE_NUM >= 20131230 || defined(PACKETVER_ZERO)
  1553. // Tested maximum: 21474836 (INT32_MAX / 100)
  1554. if (status->max_hp > INT32_MAX / 100)
  1555. #else
  1556. // Tested maximum: 32767 (INT16_MAX)
  1557. if (status->max_hp > INT16_MAX)
  1558. #endif
  1559. p.value = status->hp / (status->max_hp / 100);
  1560. else
  1561. p.value = static_cast<decltype(p.value)>(status->hp);
  1562. break;
  1563. case SP_SP:
  1564. #if PACKETVER_MAIN_NUM >= 20200819 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20190626
  1565. // Tested maximum: 2147483647 (INT32_MAX)
  1566. if (status->max_sp > INT32_MAX)
  1567. #else
  1568. // Tested maximum: 32767 (INT16_MAX)
  1569. if (status->max_sp > INT16_MAX)
  1570. #endif
  1571. p.value = status->sp / (status->max_sp / 100);
  1572. else
  1573. p.value = static_cast<decltype(p.value)>(status->sp);
  1574. break;
  1575. default:
  1576. ShowError("clif_homunculus_updatestatus: Unsupported type %d.\n", type);
  1577. return;
  1578. }
  1579. clif_send(&p, sizeof(p), &sd.bl, SELF);
  1580. #endif
  1581. }
  1582. /// Sends information about owned homunculus to the client . [orn]
  1583. /// 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)
  1584. /// 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)
  1585. void clif_hominfo( map_session_data *sd, struct homun_data *hd, int flag ){
  1586. #if PACKETVER_MAIN_NUM >= 20101005 || PACKETVER_RE_NUM >= 20080827 || defined(PACKETVER_ZERO)
  1587. nullpo_retv( sd );
  1588. nullpo_retv( hd );
  1589. struct status_data *status = &hd->battle_status;
  1590. PACKET_ZC_PROPERTY_HOMUN p = {};
  1591. p.packetType = HEADER_ZC_PROPERTY_HOMUN;
  1592. safestrncpy( p.name, hd->homunculus.name, sizeof( p.name ) );
  1593. // Bit field, bit 0 : rename_flag (1 = already renamed), bit 1 : homunc vaporized (1 = true), bit 2 : homunc dead (1 = true)
  1594. 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 );
  1595. p.level = hd->homunculus.level;
  1596. p.hunger = hd->homunculus.hunger;
  1597. p.intimacy = hd->homunculus.intimacy / 100;
  1598. #if !(PACKETVER_MAIN_NUM >= 20190619 || PACKETVER_RE_NUM >= 20190605 || PACKETVER_ZERO_NUM >= 20190626)
  1599. p.itemId = 0; // equip id
  1600. #endif
  1601. p.atk2 = cap_value( status->rhw.atk2 + status->batk, 0, INT16_MAX );
  1602. p.matk = i16min( status->matk_max, INT16_MAX ); //FIXME capping to INT16 here is too late
  1603. p.hit = status->hit;
  1604. if( battle_config.hom_setting&HOMSET_DISPLAY_LUK ){
  1605. p.crit = status->luk / 3 + 1; //crit is a +1 decimal value! Just display purpose.[Vicious]
  1606. }else{
  1607. p.crit = status->cri / 10;
  1608. }
  1609. #ifdef RENEWAL
  1610. p.def = status->def + status->def2;
  1611. p.mdef = status->mdef + status->mdef2;
  1612. #else
  1613. p.def = status->def + status->vit;
  1614. p.mdef = status->mdef;
  1615. #endif
  1616. p.flee = status->flee;
  1617. p.amotion = (flag) ? 0 : status->amotion;
  1618. // Homunculus HP and SP bars will screw up if the percentage calculation exceeds signed values
  1619. // Any value above will screw up the bars
  1620. #if PACKETVER_MAIN_NUM >= 20200819 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20190626
  1621. // Tested maximum: 2147483647 (INT32_MAX)
  1622. if (status->max_hp > INT32_MAX)
  1623. #elif PACKETVER_MAIN_NUM >= 20131230 || PACKETVER_RE_NUM >= 20131230 || defined(PACKETVER_ZERO)
  1624. // Tested maximum: 21474836 (INT32_MAX / 100)
  1625. if (status->max_hp > INT32_MAX / 100)
  1626. #else
  1627. // Tested maximum: 32767 (INT16_MAX)
  1628. if (status->max_hp > INT16_MAX)
  1629. #endif
  1630. {
  1631. p.hp = status->hp / ( status->max_hp / 100 );
  1632. p.maxHp = 100;
  1633. }else{
  1634. p.hp = static_cast<decltype(p.hp)>(status->hp);
  1635. p.maxHp = static_cast<decltype(p.maxHp)>(status->max_hp);
  1636. }
  1637. #if PACKETVER_MAIN_NUM >= 20200819 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20190626
  1638. // Tested maximum: 2147483647 (INT32_MAX)
  1639. if (status->max_sp > INT32_MAX)
  1640. #else
  1641. // Tested maximum: 32767 (INT16_MAX)
  1642. if (status->max_sp > INT16_MAX)
  1643. #endif
  1644. {
  1645. p.sp = status->sp / ( status->max_sp / 100 );
  1646. p.maxSp = 100;
  1647. }else{
  1648. p.sp = static_cast<decltype(p.sp)>(status->sp);
  1649. p.maxSp = static_cast<decltype(p.maxSp)>(status->max_sp);
  1650. }
  1651. p.exp = static_cast<decltype(p.exp)>( std::min<decltype(hd->homunculus.exp)>( hd->homunculus.exp, std::numeric_limits<decltype(p.exp)>::max() ) );
  1652. p.expNext = static_cast<decltype(p.expNext)>( std::min<decltype(hd->exp_next)>( hd->exp_next, std::numeric_limits<decltype(p.expNext)>::max() ) );
  1653. switch( hom_class2type( hd->homunculus.class_ ) ){
  1654. case HT_REG:
  1655. case HT_EVO:
  1656. if( hd->homunculus.level >= battle_config.hom_max_level ){
  1657. p.expNext = 0;
  1658. }
  1659. break;
  1660. case HT_S:
  1661. if( hd->homunculus.level >= battle_config.hom_S_max_level ){
  1662. p.expNext = 0;
  1663. }
  1664. break;
  1665. }
  1666. p.skillPoints = hd->homunculus.skillpts;
  1667. p.range = status_get_range( &hd->bl );
  1668. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  1669. #endif
  1670. }
  1671. /// Notification about a change in homunuculus' state.
  1672. /// 0230 <type>.B <state>.B <id>.L <data>.L (ZC_CHANGESTATE_MER)
  1673. /// type:
  1674. /// unused
  1675. /// state:
  1676. /// 0 = pre-init
  1677. /// 1 = intimacy
  1678. /// 2 = hunger
  1679. /// 3 = accessory?
  1680. /// ? = ignored
  1681. void clif_send_homdata( homun_data& hd, e_hom_state2 state ){
  1682. #if PACKETVER >= 20050523
  1683. if( hd.master == nullptr ){
  1684. return;
  1685. }
  1686. uint32 param;
  1687. switch( state ){
  1688. case SP_ACK:
  1689. param = 0;
  1690. break;
  1691. case SP_INTIMATE:
  1692. // TODO: Why is something like this here? [Lemongrass]
  1693. if( hd.homunculus.class_ == hd.homunculusDB->evo_class && hom_intimacy_intimacy2grade( hd.homunculus.intimacy ) >= HOMGRADE_LOYAL ){
  1694. hom_calc_skilltree( &hd );
  1695. }
  1696. param = hd.homunculus.intimacy / 100;
  1697. break;
  1698. case SP_HUNGRY:
  1699. param = hd.homunculus.hunger;
  1700. break;
  1701. default:
  1702. return;
  1703. }
  1704. PACKET_ZC_CHANGESTATE_MER packet{};
  1705. packet.packetType = HEADER_ZC_CHANGESTATE_MER;
  1706. packet.type = 0;
  1707. packet.state = state;
  1708. packet.gid = hd.bl.id;
  1709. packet.data = param;
  1710. clif_send( &packet, sizeof( packet ), &hd.master->bl, SELF );
  1711. #endif
  1712. }
  1713. void clif_homskillinfoblock( homun_data& hd ){
  1714. #if PACKETVER >= 20050530
  1715. map_session_data* sd = hd.master;
  1716. if( sd == nullptr ){
  1717. return;
  1718. }
  1719. PACKET_ZC_HOSKILLINFO_LIST* packet = reinterpret_cast<PACKET_ZC_HOSKILLINFO_LIST*>(packet_buffer);
  1720. packet->packetType = HEADER_ZC_HOSKILLINFO_LIST;
  1721. packet->packetLength = sizeof( *packet );
  1722. for(int i = 0, count = 0; i < MAX_HOMUNSKILL; i++) {
  1723. int id = hd.homunculus.hskill[i].id;
  1724. if (id != 0) {
  1725. int combo = (hd.homunculus.hskill[i].flag)&SKILL_FLAG_TMP_COMBO;
  1726. short idx = hom_skill_get_index(id);
  1727. if (idx == -1)
  1728. continue;
  1729. packet->skills[count].id = id;
  1730. packet->skills[count].inf = (combo) ? INF_SELF_SKILL : skill_get_inf(id);
  1731. packet->skills[count].unknown = 0;
  1732. packet->skills[count].level = hd.homunculus.hskill[idx].lv;
  1733. packet->skills[count].sp = skill_get_sp(id,hd.homunculus.hskill[idx].lv);
  1734. packet->skills[count].range = skill_get_range2(&hd.bl,id,hd.homunculus.hskill[idx].lv,false);
  1735. safestrncpy(packet->skills[count].name, skill_get_name(id), NAME_LENGTH);
  1736. 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;
  1737. packet->packetLength += sizeof( packet->skills[0] );
  1738. count++;
  1739. }
  1740. }
  1741. clif_send( packet, packet->packetLength, &sd->bl, SELF );
  1742. #endif
  1743. }
  1744. void clif_homskillup( homun_data& hd, uint16 skill_id ){
  1745. #if PACKETVER >= 20050531
  1746. short idx = hom_skill_get_index( skill_id );
  1747. if( idx == -1 ){
  1748. return;
  1749. }
  1750. map_session_data* sd = hd.master;
  1751. if( sd == nullptr ){
  1752. return;
  1753. }
  1754. PACKET_ZC_HOSKILLINFO_UPDATE packet{};
  1755. packet.packetType = HEADER_ZC_HOSKILLINFO_UPDATE;
  1756. packet.skill_id = skill_id;
  1757. packet.Level = hd.homunculus.hskill[idx].lv;
  1758. packet.SP = skill_get_sp(skill_id,hd.homunculus.hskill[idx].lv);
  1759. packet.AttackRange = skill_get_range2(&hd.bl,skill_id,hd.homunculus.hskill[idx].lv,false);
  1760. 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;
  1761. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  1762. #endif
  1763. }
  1764. /// Result of request to feed a homun/merc.
  1765. /// 022f <result>.B <name id>.W (ZC_FEED_MER)
  1766. /// result:
  1767. /// 0 = failure
  1768. /// 1 = success
  1769. void clif_hom_food( map_session_data& sd, int32 foodid, bool success ){
  1770. PACKET_ZC_FEED_MER packet{};
  1771. packet.packetType = HEADER_ZC_FEED_MER;
  1772. packet.result = success;
  1773. packet.itemId = client_nameid( foodid );
  1774. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1775. }
  1776. /// Notifies the client, that it is walking.
  1777. /// 0087 <walk start time>.L <walk data>.6B (ZC_NOTIFY_PLAYERMOVE)
  1778. void clif_walkok( map_session_data& sd ){
  1779. PACKET_ZC_NOTIFY_PLAYERMOVE packet{};
  1780. packet.packetType = HEADER_ZC_NOTIFY_PLAYERMOVE;
  1781. packet.moveStartTime = client_tick(gettick());
  1782. WBUFPOS2(packet.moveData, 0, sd.bl.x, sd.bl.y, sd.ud.to_x, sd.ud.to_y, 8, 8);
  1783. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1784. }
  1785. /// Notifies clients in an area, that an other visible object is walking.
  1786. /// Note: unit must not be self
  1787. void clif_move( struct unit_data& ud )
  1788. {
  1789. struct block_list* bl = ud.bl;
  1790. struct view_data* vd = status_get_viewdata(bl);
  1791. if (bl == nullptr || vd == nullptr)
  1792. return;
  1793. // This performance check is needed to keep GM-hidden objects from being notified to bots.
  1794. if (vd->class_ == JT_INVISIBLE)
  1795. return;
  1796. // Hide NPC from Maya Purple card
  1797. if (clif_npc_mayapurple(bl))
  1798. return;
  1799. status_change* sc = nullptr;
  1800. if ((sc = status_get_sc(bl)) && sc->option & (OPTION_HIDE | OPTION_CLOAK | OPTION_INVISIBLE | OPTION_CHASEWALK))
  1801. clif_ally_only = true;
  1802. clif_set_unit_walking( *bl, nullptr, ud, AREA_WOS );
  1803. if (vd->cloth_color)
  1804. clif_refreshlook(bl, bl->id, LOOK_CLOTHES_COLOR, vd->cloth_color, AREA_WOS);
  1805. if (vd->body_style)
  1806. clif_refreshlook(bl, bl->id, LOOK_BODY2, vd->body_style, AREA_WOS);
  1807. switch (bl->type) {
  1808. case BL_PC:
  1809. {
  1810. map_session_data* sd = reinterpret_cast<map_session_data*>( bl );
  1811. if (sd->state.size == SZ_BIG) // tiny/big players [Valaris]
  1812. clif_specialeffect(&sd->bl, EF_GIANTBODY2, AREA);
  1813. else if (sd->state.size == SZ_MEDIUM)
  1814. clif_specialeffect(&sd->bl, EF_BABYBODY2, AREA);
  1815. if (sd->status.robe)
  1816. clif_refreshlook(bl, bl->id, LOOK_ROBE, sd->status.robe, AREA);
  1817. }
  1818. break;
  1819. case BL_MOB:
  1820. {
  1821. mob_data* md = reinterpret_cast<mob_data*>( bl );
  1822. if (md->special_state.size == SZ_BIG) // tiny/big mobs [Valaris]
  1823. clif_specialeffect(&md->bl, EF_GIANTBODY2, AREA);
  1824. else if (md->special_state.size == SZ_MEDIUM)
  1825. clif_specialeffect(&md->bl, EF_BABYBODY2, AREA);
  1826. }
  1827. break;
  1828. case BL_PET:
  1829. if (vd->head_bottom) // needed to display pet equip properly
  1830. clif_pet_equip_area(BL_CAST(BL_PET, bl));
  1831. break;
  1832. }
  1833. clif_ally_only = false;
  1834. }
  1835. /*==========================================
  1836. * Delays the map_quit of a player after they are disconnected. [Skotlex]
  1837. *------------------------------------------*/
  1838. static TIMER_FUNC(clif_delayquit){
  1839. map_session_data *sd = nullptr;
  1840. //Remove player from map server
  1841. if ((sd = map_id2sd(id)) != nullptr && sd->fd == 0) //Should be a disconnected player.
  1842. map_quit(sd);
  1843. return 0;
  1844. }
  1845. /*==========================================
  1846. *
  1847. *------------------------------------------*/
  1848. void clif_quitsave(int fd,map_session_data *sd) {
  1849. if (!battle_config.prevent_logout ||
  1850. sd->canlog_tick == 0 ||
  1851. DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout)
  1852. map_quit(sd);
  1853. else if (session_isValid(sd->fd)) {
  1854. //Disassociate session from player (session is deleted after this function was called)
  1855. //And set a timer to make him quit later.
  1856. session[sd->fd]->session_data = nullptr;
  1857. sd->fd = 0;
  1858. add_timer(gettick() + 10000, clif_delayquit, sd->bl.id, 0);
  1859. }
  1860. }
  1861. /// Notifies the client of a position change to coordinates on given map (ZC_NPCACK_MAPMOVE).
  1862. /// 0091 <map name>.16B <x>.W <y>.W
  1863. void clif_changemap( map_session_data& sd, short m, uint16 x, uint16 y ){
  1864. PACKET_ZC_NPCACK_MAPMOVE packet{};
  1865. packet.packetType = HEADER_ZC_NPCACK_MAPMOVE;
  1866. mapindex_getmapname_ext(map_mapid2mapname(m), packet.mapName);
  1867. packet.xPos = x;
  1868. packet.yPos = y;
  1869. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1870. }
  1871. /// Notifies the client of a position change to coordinates on given map, which is on another map-server.
  1872. /// 0092 <map name>.16B <x>.W <y>.W <ip>.L <port>.W (ZC_NPCACK_SERVERMOVE)
  1873. /// 0ac7 <map name>.16B <x>.W <y>.W <ip>.L <port>.W <unknown>.128B (ZC_NPCACK_SERVERMOVE_DOMAIN)
  1874. void clif_changemapserver( map_session_data& sd, const char* map, uint16 x, uint16 y, uint32 ip, uint16 port ){
  1875. PACKET_ZC_NPCACK_SERVERMOVE packet{};
  1876. packet.packetType = HEADER_ZC_NPCACK_SERVERMOVE;
  1877. mapindex_getmapname_ext(map, packet.mapName);
  1878. packet.xPos = x;
  1879. packet.yPos = y;
  1880. packet.ip = htonl(ip);
  1881. packet.port = ntows(htons(port)); // [!] LE byte order here [!]
  1882. #if PACKETVER >= 20170315
  1883. safestrncpy( packet.domain, "", sizeof( packet.domain ) );
  1884. #endif
  1885. #ifdef DEBUG
  1886. 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 );
  1887. #endif
  1888. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1889. }
  1890. /// In many situations (knockback, backslide, etc.) Aegis sends both clif_slide and clif_fixpos
  1891. /// This function combines both calls and allows to simplify the calling code
  1892. void clif_blown(struct block_list *bl)
  1893. {
  1894. clif_slide(*bl, bl->x, bl->y);
  1895. clif_fixpos( *bl );
  1896. }
  1897. /// Visually moves(slides) a character to x,y. If the target cell
  1898. /// isn't walkable, the char doesn't move at all. If the char is
  1899. /// sitting it will stand up.
  1900. /// 0088 <id>.L <x>.W <y>.W (ZC_STOPMOVE)
  1901. void clif_fixpos( block_list& bl ){
  1902. PACKET_ZC_STOPMOVE packet = {};
  1903. packet.packetType = HEADER_ZC_STOPMOVE;
  1904. packet.AID = bl.id;
  1905. packet.xPos = bl.x;
  1906. packet.yPos = bl.y;
  1907. clif_send( &packet, sizeof( packet ), &bl, AREA);
  1908. if( disguised( &bl ) ){
  1909. packet.AID = disguised_bl_id( bl.id );
  1910. clif_send( &packet, sizeof( packet ), &bl, SELF );
  1911. }
  1912. }
  1913. /// Displays the buy/sell dialog of an NPC shop.
  1914. /// 00c4 <shop id>.L (ZC_SELECT_DEALTYPE)
  1915. void clif_npcbuysell( map_session_data& sd, npc_data& nd ){
  1916. PACKET_ZC_SELECT_DEALTYPE packet{};
  1917. packet.packetType = HEADER_ZC_SELECT_DEALTYPE;
  1918. packet.npcId = nd.bl.id;
  1919. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  1920. }
  1921. /// Presents list of items, that can be bought in an NPC shop.
  1922. /// 00c6 <packet len>.W { <price>.L <discount price>.L <item type>.B <name id>.W }* (ZC_PC_PURCHASE_ITEMLIST)
  1923. void clif_buylist( map_session_data& sd, npc_data& nd ){
  1924. PACKET_ZC_PC_PURCHASE_ITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_ITEMLIST*>( packet_buffer );
  1925. p->packetType = HEADER_ZC_PC_PURCHASE_ITEMLIST;
  1926. p->packetLength = sizeof( *p );
  1927. for( int i = 0, count = 0, discount = npc_shop_discount( &nd ); i < nd.u.shop.count; i++ ){
  1928. int val = nd.u.shop.shop_item[i].value;
  1929. p->items[count].price = val;
  1930. p->items[count].discountPrice = ( discount ) ? pc_modifybuyvalue( &sd, val ) : val;
  1931. p->items[count].itemType = itemtype( nd.u.shop.shop_item[i].nameid );
  1932. p->items[count].itemId = client_nameid( nd.u.shop.shop_item[i].nameid );
  1933. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  1934. std::shared_ptr<item_data> id = item_db.find(nd.u.shop.shop_item[i].nameid);
  1935. p->items[count].viewSprite = id->look;
  1936. p->items[count].location = pc_equippoint_sub( &sd, id.get() );
  1937. #endif
  1938. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  1939. count++;
  1940. }
  1941. clif_send( p, p->packetLength, &sd.bl, SELF );
  1942. }
  1943. /// Presents list of items, that can be sold to an NPC shop.
  1944. /// 00c7 <packet len>.W { <index>.W <price>.L <overcharge price>.L }* (ZC_PC_SELL_ITEMLIST)
  1945. void clif_selllist( map_session_data& sd){
  1946. if( !sd.npc_shopid ){
  1947. return;
  1948. }
  1949. npc_data* nd = map_id2nd( sd.npc_shopid );
  1950. if( nd == nullptr ){
  1951. return;
  1952. }
  1953. PACKET_ZC_PC_SELL_ITEMLIST *packet = reinterpret_cast<PACKET_ZC_PC_SELL_ITEMLIST*>(packet_buffer);
  1954. packet->packetType = HEADER_ZC_PC_SELL_ITEMLIST;
  1955. packet->packetLength = sizeof( *packet );
  1956. for( int i = 0, c = 0; i < MAX_INVENTORY; i++ ){
  1957. if( sd.inventory.u.items_inventory[i].nameid <= 0 ){
  1958. continue;
  1959. }
  1960. if( sd.inventory_data[i] == nullptr ){
  1961. continue;
  1962. }
  1963. if( !pc_can_sell_item( &sd, &sd.inventory.u.items_inventory[i], nd->subtype ) ){
  1964. continue;
  1965. }
  1966. int price;
  1967. if( battle_config.rental_item_novalue && sd.inventory.u.items_inventory[i].expire_time ){
  1968. price = 0;
  1969. }else{
  1970. price = sd.inventory_data[i]->value_sell;
  1971. if( price < 0 ){
  1972. continue;
  1973. }
  1974. }
  1975. packet->items[c].index = client_index(i);
  1976. packet->items[c].price = price;
  1977. packet->items[c].overcharge = pc_modifysellvalue( &sd, price );
  1978. packet->packetLength += sizeof( packet->items[0] );
  1979. c++;
  1980. }
  1981. clif_send( packet, packet->packetLength, &sd.bl, SELF );
  1982. }
  1983. /// Closes shop (CZ_NPC_TRADE_QUIT).
  1984. /// 09d4
  1985. void clif_parse_NPCShopClosed(int fd, map_session_data *sd) {
  1986. // TODO: State tracking?
  1987. sd->npc_shopid = 0;
  1988. }
  1989. /**
  1990. * Presents list of items, that can be sold to a Market shop.
  1991. * @author: Ind and Yommy
  1992. **/
  1993. /// 0x9d5 <packet len>.W { <name id>.W <type>.B <price>.L <qty>.L <weight>.W }* (ZC_NPC_MARKET_OPEN)
  1994. void clif_npc_market_open( map_session_data& sd, npc_data& nd ){
  1995. #if PACKETVER >= 20131223
  1996. if( sd.state.trading ){
  1997. return;
  1998. }
  1999. PACKET_ZC_NPC_MARKET_OPEN* p = reinterpret_cast<PACKET_ZC_NPC_MARKET_OPEN*>( packet_buffer );
  2000. p->packetType = HEADER_ZC_NPC_MARKET_OPEN;
  2001. p->packetLength = sizeof( *p );
  2002. for( int i = 0, count = 0; i < nd.u.shop.count; i++ ){
  2003. struct npc_item_list *item = &nd.u.shop.shop_item[i];
  2004. if( !item->nameid ){
  2005. continue;
  2006. }
  2007. std::shared_ptr<item_data> id = item_db.find(item->nameid);
  2008. if( !id ){
  2009. continue;
  2010. }
  2011. PACKET_ZC_NPC_MARKET_OPEN_sub& entry = p->list[count];
  2012. entry.nameid = client_nameid( item->nameid );
  2013. entry.type = itemtype( item->nameid );
  2014. entry.price = item->value;
  2015. entry.qty = item->qty;
  2016. entry.weight = id->weight;
  2017. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  2018. entry.location = pc_equippoint_sub( &sd, id.get() );
  2019. #endif
  2020. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  2021. count++;
  2022. }
  2023. clif_send( p, p->packetLength, &sd.bl, SELF );
  2024. sd.state.trading = 1;
  2025. #endif
  2026. }
  2027. /// Closes the Market shop window.
  2028. void clif_parse_NPCMarketClosed(int fd, map_session_data *sd) {
  2029. nullpo_retv(sd);
  2030. sd->npc_shopid = 0;
  2031. sd->state.trading = 0;
  2032. }
  2033. /// Purchase item from Market shop.
  2034. /// 0x9d7 <packet len>.W <count>.B { <name id>.W <qty>.W <price>.L }* (ZC_NPC_MARKET_PURCHASE_RESULT)
  2035. void clif_npc_market_purchase_ack( map_session_data& sd, e_purchase_result res, std::vector<s_npc_buy_list>& list ){
  2036. #if PACKETVER >= 20131223
  2037. struct npc_data *nd = map_id2nd( sd.npc_shopid );
  2038. if( nd == nullptr ){
  2039. return;
  2040. }
  2041. PACKET_ZC_NPC_MARKET_PURCHASE_RESULT* p = reinterpret_cast<PACKET_ZC_NPC_MARKET_PURCHASE_RESULT*>( packet_buffer );
  2042. p->PacketType = HEADER_ZC_NPC_MARKET_PURCHASE_RESULT;
  2043. p->PacketLength = sizeof( *p );
  2044. #if PACKETVER_MAIN_NUM >= 20190807 || PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190814
  2045. p->result = ( res == e_purchase_result::PURCHASE_SUCCEED ? 0 : -1 );
  2046. #else
  2047. p->result = ( res == e_purchase_result::PURCHASE_SUCCEED ? 1 : 0 );
  2048. #endif
  2049. if( res == e_purchase_result::PURCHASE_SUCCEED ){
  2050. for( int i = 0, j, count = 0; i < list.size(); i++ ){
  2051. ARR_FIND( 0, nd->u.shop.count, j, list[i].nameid == nd->u.shop.shop_item[j].nameid );
  2052. // Not found
  2053. if( j == nd->u.shop.count ){
  2054. continue;
  2055. }
  2056. p->list[count].ITID = client_nameid( list[i].nameid );
  2057. p->list[count].qty = list[i].qty;
  2058. p->list[count].price = nd->u.shop.shop_item[j].value;
  2059. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->list[0] ) );
  2060. count++;
  2061. }
  2062. }
  2063. clif_send( p, p->PacketLength, &sd.bl, SELF );
  2064. #endif
  2065. }
  2066. /// Purchase item from Market shop.
  2067. /// 0x9d6 <len>.W { <name id>.W <qty>.L } (CZ_NPC_MARKET_PURCHASE)
  2068. void clif_parse_NPCMarketPurchase(int fd, map_session_data *sd) {
  2069. #if PACKETVER >= 20131223
  2070. if( sd == nullptr ){
  2071. return;
  2072. }
  2073. if( !sd->npc_shopid ){
  2074. return;
  2075. }
  2076. const PACKET_CZ_NPC_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  2077. int count = ( p->PacketLength - sizeof( *p ) ) / sizeof( p->list[0] );
  2078. std::vector<s_npc_buy_list> items;
  2079. items.reserve( count );
  2080. for( int i = 0; i < count; i++ ){
  2081. s_npc_buy_list item = {};
  2082. item.nameid = p->list[i].ITID;
  2083. item.qty = p->list[i].qty;
  2084. items.push_back( item );
  2085. }
  2086. e_purchase_result res = npc_buylist( sd, items );
  2087. clif_npc_market_purchase_ack( *sd, res, items );
  2088. #endif
  2089. }
  2090. /// Displays an NPC dialog message.
  2091. /// 00b4 <packet len>.W <npc id>.L <message>.?B (ZC_SAY_DIALOG)
  2092. /// Client behavior (dialog window):
  2093. /// - disable mouse targeting
  2094. /// - open the dialog window
  2095. /// - set npcid of dialog window (0 by default)
  2096. /// - if set to clear on next mes, clear contents
  2097. /// - append this text
  2098. void clif_scriptmes( map_session_data& sd, uint32 npcid, const char *mes ){
  2099. PACKET_ZC_SAY_DIALOG* p = reinterpret_cast<PACKET_ZC_SAY_DIALOG*>( packet_buffer );
  2100. int16 length = (int16)( strlen( mes ) + 1 );
  2101. p->PacketType = HEADER_ZC_SAY_DIALOG;
  2102. p->PacketLength = sizeof( *p ) + length;
  2103. p->NpcID = npcid;
  2104. safestrncpy( p->message, mes, length );
  2105. clif_send( p, p->PacketLength, &sd.bl, SELF );
  2106. }
  2107. /// Adds a 'next' button to an NPC dialog.
  2108. /// 00b5 <npc id>.L (ZC_WAIT_DIALOG)
  2109. /// Client behavior (dialog window):
  2110. /// - disable mouse targeting
  2111. /// - open the dialog window
  2112. /// - add 'next' button
  2113. /// When 'next' is pressed:
  2114. /// - 00B9 <npcid of dialog window>.L
  2115. /// - set to clear on next mes
  2116. /// - remove 'next' button
  2117. void clif_scriptnext( map_session_data& sd, uint32 npcid ){
  2118. PACKET_ZC_WAIT_DIALOG p = {};
  2119. p.PacketType = HEADER_ZC_WAIT_DIALOG;
  2120. p.NpcID = npcid;
  2121. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  2122. }
  2123. /// Adds a 'close' button to an NPC dialog.
  2124. /// 00b6 <npc id>.L (ZC_CLOSE_DIALOG)
  2125. /// Client behavior:
  2126. /// - if dialog window is open:
  2127. /// - remove 'next' button
  2128. /// - add 'close' button
  2129. /// - else:
  2130. /// - enable mouse targeting
  2131. /// - close the dialog window
  2132. /// - close the menu window
  2133. /// When 'close' is pressed:
  2134. /// - enable mouse targeting
  2135. /// - close the dialog window
  2136. /// - close the menu window
  2137. /// - 0146 <npcid of dialog window>.L
  2138. void clif_scriptclose( map_session_data& sd, uint32 npcid ){
  2139. PACKET_ZC_CLOSE_DIALOG packet{};
  2140. packet.packetType = HEADER_ZC_CLOSE_DIALOG;
  2141. packet.npcId = npcid;
  2142. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2143. }
  2144. /**
  2145. * Close script when player is idle
  2146. * 08d6 <npc id>.L (ZC_CLEAR_DIALOG)
  2147. * @author [Ind/Hercules]
  2148. * @param sd : player pointer
  2149. * @param npcid : npc gid to close
  2150. */
  2151. void clif_scriptclear( map_session_data& sd, int npcid ){
  2152. PACKET_ZC_CLEAR_DIALOG p = {};
  2153. p.packetType = HEADER_ZC_CLEAR_DIALOG;
  2154. p.GID = npcid;
  2155. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  2156. }
  2157. void clif_sendfakenpc( map_session_data& sd, uint32 npcid ){
  2158. sd.state.using_fake_npc = 1;
  2159. // Code below is a copy of clif_set_unit_idle
  2160. struct packet_idle_unit p;
  2161. p.PacketType = idle_unitType;
  2162. #if PACKETVER >= 20091103
  2163. p.PacketLength = sizeof(p);
  2164. p.objecttype = 0x1; // NPC_TYPE
  2165. #endif
  2166. #if PACKETVER >= 20131223
  2167. p.AID = npcid;
  2168. p.GID = 0;
  2169. #else
  2170. p.GID = npcid;
  2171. #endif
  2172. p.speed = 0;
  2173. p.bodyState = 0;
  2174. p.healthState = 0;
  2175. p.effectState = 0;
  2176. p.job = JT_HIDDEN_NPC;
  2177. p.head = 0;
  2178. p.weapon = 0;
  2179. #if PACKETVER < 7 || PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  2180. p.shield = 0;
  2181. #endif
  2182. p.accessory = 0;
  2183. p.accessory2 = 0;
  2184. p.accessory3 = 0;
  2185. p.headpalette = 0;
  2186. p.bodypalette = 0;
  2187. p.headDir = 0;
  2188. #if PACKETVER >= 20101124
  2189. p.robe = 0;
  2190. #endif
  2191. p.GUID = 0;
  2192. p.GEmblemVer = 0;
  2193. p.honor = 0;
  2194. p.virtue = 0;
  2195. p.isPKModeON = 0;
  2196. p.sex = SEX_FEMALE;
  2197. WBUFPOS( &p.PosDir[0], 0, sd.bl.x, sd.bl.y, sd.ud.dir );
  2198. p.xSize = p.ySize = 5;
  2199. p.state = 0;
  2200. p.clevel = 0;
  2201. #if PACKETVER >= 20080102
  2202. p.font = 0;
  2203. #endif
  2204. #if PACKETVER >= 20120221
  2205. p.maxHP = -1;
  2206. p.HP = -1;
  2207. #endif
  2208. #if PACKETVER >= 20150513
  2209. p.body = 0;
  2210. #endif
  2211. /* Might be earlier, this is when the named item bug began */
  2212. #if PACKETVER >= 20131223
  2213. safestrncpy( p.name, "", NAME_LENGTH);
  2214. #endif
  2215. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  2216. }
  2217. /// Displays an NPC dialog menu.
  2218. /// 00b7 <packet len>.W <npc id>.L <menu items>.?B (ZC_MENU_LIST)
  2219. /// Client behavior:
  2220. /// - disable mouse targeting
  2221. /// - close the menu window
  2222. /// - open the menu window
  2223. /// - add options to the menu (separated in the text by ":")
  2224. /// - set npcid of menu window
  2225. /// - if dialog window is open:
  2226. /// - remove 'next' button
  2227. /// When 'ok' is pressed:
  2228. /// - 00B8 <npcid of menu window>.L <selected option>.B
  2229. /// - close the menu window
  2230. /// When 'cancel' is pressed:
  2231. /// - 00B8 <npcid of menu window>.L <-1>.B
  2232. /// - enable mouse targeting
  2233. /// - close a bunch of windows...
  2234. /// WARNING: the 'cancel' button closes other windows besides the dialog window and the menu window.
  2235. /// Which suggests their have intertwined behavior. (probably the mouse targeting)
  2236. /// TODO investigate behavior of other windows [FlavioJS]
  2237. void clif_scriptmenu( map_session_data& sd, uint32 npcid, const char* mes ){
  2238. struct block_list *bl = nullptr;
  2239. if (!sd.state.using_fake_npc && (npcid == fake_nd->bl.id || ((bl = map_id2bl(npcid)) && (bl->m!=sd.bl.m ||
  2240. bl->x<sd.bl.x-AREA_SIZE-1 || bl->x>sd.bl.x+AREA_SIZE+1 ||
  2241. bl->y<sd.bl.y-AREA_SIZE-1 || bl->y>sd.bl.y+AREA_SIZE+1))))
  2242. clif_sendfakenpc( sd, npcid );
  2243. // String length + 1 byte for zero termination
  2244. size_t mes_length = strlen( mes ) + 1;
  2245. PACKET_ZC_MENU_LIST* packet = reinterpret_cast<PACKET_ZC_MENU_LIST*>( packet_buffer );
  2246. packet->packetType = HEADER_ZC_MENU_LIST;
  2247. packet->packetLength = sizeof( PACKET_ZC_MENU_LIST );
  2248. packet->npcId = npcid;
  2249. safestrncpy( packet->menu, mes, mes_length );
  2250. packet->packetLength += static_cast<decltype(packet->packetLength)>( mes_length );
  2251. clif_send( packet, packet->packetLength, &sd.bl, SELF );
  2252. }
  2253. /// Displays an NPC dialog input box for numbers.
  2254. /// 0142 <npc id>.L (ZC_OPEN_EDITDLG)
  2255. /// Client behavior (inputnum window):
  2256. /// - if npcid exists in the client:
  2257. /// - open the inputnum window
  2258. /// - set npcid of inputnum window
  2259. /// When 'ok' is pressed:
  2260. /// - if inputnum window has text:
  2261. /// - if npcid exists in the client:
  2262. /// - 0143 <npcid of inputnum window>.L <atoi(text)>.L
  2263. /// - close inputnum window
  2264. void clif_scriptinput( map_session_data& sd, uint32 npcid ){
  2265. struct block_list *bl = nullptr;
  2266. if (!sd.state.using_fake_npc && (npcid == fake_nd->bl.id || ((bl = map_id2bl(npcid)) && (bl->m!=sd.bl.m ||
  2267. bl->x<sd.bl.x-AREA_SIZE-1 || bl->x>sd.bl.x+AREA_SIZE+1 ||
  2268. bl->y<sd.bl.y-AREA_SIZE-1 || bl->y>sd.bl.y+AREA_SIZE+1))))
  2269. clif_sendfakenpc( sd, npcid );
  2270. PACKET_ZC_OPEN_EDITDLG packet{};
  2271. packet.packetType = HEADER_ZC_OPEN_EDITDLG;
  2272. packet.npcId = npcid;
  2273. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2274. }
  2275. /// Displays an NPC dialog input box for strings.
  2276. /// 01d4 <npc id>.L (ZC_OPEN_EDITDLGSTR)
  2277. /// Client behavior (inputstr window):
  2278. /// - if npcid is 0 or npcid exists in the client:
  2279. /// - open the inputstr window
  2280. /// - set npcid of inputstr window
  2281. /// When 'ok' is pressed:
  2282. /// - if inputstr window has text and isn't an insult(manner.txt):
  2283. /// - if npcid is 0 or npcid exists in the client:
  2284. /// - 01d5 <packetlen>.W <npcid of inputstr window>.L <text>.?B
  2285. /// - close inputstr window
  2286. void clif_scriptinputstr( map_session_data& sd, uint32 npcid ){
  2287. struct block_list *bl = nullptr;
  2288. if (!sd.state.using_fake_npc && (npcid == fake_nd->bl.id || ((bl = map_id2bl(npcid)) && (bl->m!=sd.bl.m ||
  2289. bl->x<sd.bl.x-AREA_SIZE-1 || bl->x>sd.bl.x+AREA_SIZE+1 ||
  2290. bl->y<sd.bl.y-AREA_SIZE-1 || bl->y>sd.bl.y+AREA_SIZE+1))))
  2291. clif_sendfakenpc( sd, npcid );
  2292. PACKET_ZC_OPEN_EDITDLGSTR packet{};
  2293. packet.packetType = HEADER_ZC_OPEN_EDITDLGSTR;
  2294. packet.npcId = npcid;
  2295. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2296. }
  2297. /// Marks a position on client's minimap.
  2298. /// 0144 <npc id>.L <type>.L <x>.L <y>.L <id>.B <color>.L (ZC_COMPASS)
  2299. /// npc id:
  2300. /// is ignored in the client
  2301. /// type:
  2302. /// 0 = display mark for 15 seconds
  2303. /// 1 = display mark until dead or teleported
  2304. /// 2 = remove mark
  2305. /// color:
  2306. /// 0x00RRGGBB
  2307. void clif_viewpoint( map_session_data& sd, uint32 npc_id, int type, uint16 x, uint16 y, int id, uint32 color ){
  2308. PACKET_ZC_COMPASS packet{};
  2309. packet.packetType = HEADER_ZC_COMPASS;
  2310. packet.npcId = npc_id;
  2311. packet.type = type;
  2312. packet.xPos = x;
  2313. packet.yPos = y;
  2314. packet.id = id;
  2315. packet.color = color;
  2316. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2317. }
  2318. /// Displays an illustration image.
  2319. /// 0145 <image name>.16B <type>.B (ZC_SHOW_IMAGE)
  2320. /// 01b3 <image name>.64B <type>.B (ZC_SHOW_IMAGE2)
  2321. /// type:
  2322. /// 0 = bottom left corner
  2323. /// 1 = bottom middle
  2324. /// 2 = bottom right corner
  2325. /// 3 = middle of screen, inside a movable window
  2326. /// 4 = middle of screen, movable with a close button, chrome-less
  2327. /// 255 = clear all displayed cutins
  2328. void clif_cutin( map_session_data& sd, const char* image, int type ){
  2329. PACKET_ZC_SHOW_IMAGE packet{};
  2330. packet.packetType = HEADER_ZC_SHOW_IMAGE;
  2331. safestrncpy(packet.image, image, sizeof(packet.image));
  2332. packet.type = type;
  2333. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2334. }
  2335. /*==========================================
  2336. * Fills in card data from the given item and into the buffer. [Skotlex]
  2337. *------------------------------------------*/
  2338. static void clif_addcards( struct EQUIPSLOTINFO* buf, struct item* item ){
  2339. nullpo_retv( buf );
  2340. // Blank data
  2341. if( item == nullptr ){
  2342. buf->card[0] = 0;
  2343. buf->card[1] = 0;
  2344. buf->card[2] = 0;
  2345. buf->card[3] = 0;
  2346. return;
  2347. }
  2348. // Pet eggs
  2349. if( item->card[0] == CARD0_PET ){
  2350. buf->card[0] = 0;
  2351. buf->card[1] = 0;
  2352. // Pet intimacy
  2353. // No idea when this was added exactly, but older clients have no problem if we send it anyway
  2354. buf->card[2] = item->card[3] >> 1;
  2355. // Pet renamed flag
  2356. buf->card[3] = item->card[3] & 1;
  2357. return;
  2358. }
  2359. // Forged/created items
  2360. if( item->card[0] == CARD0_FORGE || item->card[0] == CARD0_CREATE ){
  2361. buf->card[0] = item->card[0];
  2362. buf->card[1] = item->card[1];
  2363. buf->card[2] = item->card[2];
  2364. buf->card[3] = item->card[3];
  2365. return;
  2366. }
  2367. int i = 0, j;
  2368. // Client only receives four cards.. so randomly send them a set of cards. [Skotlex]
  2369. if( MAX_SLOTS > 4 && ( j = itemdb_slots( item->nameid ) ) > 4 ){
  2370. i = rnd() % ( j - 3 ); //eg: 6 slots, possible i values: 0->3, 1->4, 2->5 => i = rnd()%3;
  2371. }
  2372. // Normal items.
  2373. for( int k = 0; k < 4; k++, i++ ){
  2374. if( item->card[i] > 0 ){
  2375. buf->card[k] = client_nameid( item->card[i] );
  2376. }else{
  2377. buf->card[k] = 0;
  2378. }
  2379. }
  2380. }
  2381. /// Fills in part of the item buffers that calls for variable bonuses data. [Napster]
  2382. /// A maximum of 5 random options can be supported.
  2383. static void clif_add_random_options( struct ItemOptions buf[MAX_ITEM_RDM_OPT], struct item& it ){
  2384. for( int i = 0; i < MAX_ITEM_RDM_OPT; i++ ){
  2385. if( it.option[i].id ){
  2386. buf[i].index = it.option[i].id; // OptIndex
  2387. buf[i].value = it.option[i].value; // Value
  2388. buf[i].param = it.option[i].param; // Param1
  2389. }else{
  2390. buf[i].index = 0;
  2391. buf[i].value = 0;
  2392. buf[i].param = 0;
  2393. }
  2394. }
  2395. #if MAX_ITEM_RDM_OPT < 5
  2396. for( ; i < 5; i++ ){
  2397. buf[i].index = 0; // OptIndex
  2398. buf[i].value = 0; // Value
  2399. buf[i].param = 0; // Param1
  2400. }
  2401. #endif
  2402. }
  2403. /// Notifies the client, about a received inventory item or the result of a pick-up request.
  2404. /// 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)
  2405. /// 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)
  2406. /// 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)
  2407. /// 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)
  2408. /// 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)
  2409. /// 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)
  2410. void clif_additem( map_session_data *sd, int n, int amount, unsigned char fail ){
  2411. nullpo_retv( sd );
  2412. int fd = sd->fd;
  2413. if( !session_isActive( fd ) ){
  2414. return;
  2415. }
  2416. PACKET_ZC_ITEM_PICKUP_ACK p = {};
  2417. if( fail ){
  2418. p = {};
  2419. }else{
  2420. if( n < 0 || n >= MAX_INVENTORY || sd->inventory.u.items_inventory[n].nameid == 0 || sd->inventory_data[n] == nullptr ){
  2421. return;
  2422. }
  2423. p.nameid = client_nameid( sd->inventory.u.items_inventory[n].nameid );
  2424. p.IsIdentified = sd->inventory.u.items_inventory[n].identify ? 1 : 0;
  2425. p.IsDamaged = ( sd->inventory.u.items_inventory[n].attribute ) != 0 ? 1 : 0;
  2426. p.refiningLevel =sd->inventory.u.items_inventory[n].refine;
  2427. clif_addcards( &p.slot, &sd->inventory.u.items_inventory[n] );
  2428. p.location = pc_equippoint(sd,n);
  2429. p.type = itemtype( sd->inventory.u.items_inventory[n].nameid );
  2430. #if PACKETVER >= 20061218
  2431. p.HireExpireDate = sd->inventory.u.items_inventory[n].expire_time;
  2432. #if PACKETVER >= 20071002
  2433. /* why restrict the flag to non-stackable? because this is the only packet allows stackable to,
  2434. * show the color, and therefore it'd be inconsistent with the rest (aka it'd show yellow, you relog/refresh and boom its gone)
  2435. */
  2436. p.bindOnEquipType = sd->inventory.u.items_inventory[n].bound && !itemdb_isstackable2( sd->inventory_data[n] ) ? 2 : sd->inventory_data[n]->flag.bindOnEquip ? 1 : 0;
  2437. #if PACKETVER >= 20150226
  2438. clif_add_random_options( p.option_data, sd->inventory.u.items_inventory[n] );
  2439. #if PACKETVER >= 20160921
  2440. p.favorite = ( sd->inventory.u.items_inventory[n].favorite != 0 ) ? 1 : 0;
  2441. p.look = sd->inventory_data[n]->look;
  2442. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  2443. p.grade = sd->inventory.u.items_inventory[n].enchantgrade;
  2444. #endif
  2445. #endif
  2446. #endif
  2447. #endif
  2448. #endif
  2449. }
  2450. p.PacketType = HEADER_ZC_ITEM_PICKUP_ACK;
  2451. p.Index = client_index( n );
  2452. p.count = amount;
  2453. p.result = fail;
  2454. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  2455. }
  2456. /// Notifies the client, that an inventory item was deleted or dropped.
  2457. /// 00af <index>.W <amount>.W (ZC_ITEM_THROW_ACK)
  2458. void clif_dropitem( map_session_data& sd, int index, int amount ){
  2459. PACKET_ZC_ITEM_THROW_ACK packet{};
  2460. packet.packetType = HEADER_ZC_ITEM_THROW_ACK;
  2461. packet.index = client_index( index );
  2462. packet.count = amount;
  2463. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2464. }
  2465. /// Notifies the client, that an inventory item was deleted.
  2466. /// 07fa <delete type>.W <index>.W <amount>.W (ZC_DELETE_ITEM_FROM_BODY)
  2467. /// delete type:
  2468. /// 0 = Normal
  2469. /// 1 = Item used for a skill
  2470. /// 2 = Refine failed
  2471. /// 3 = Material changed
  2472. /// 4 = Moved to storage
  2473. /// 5 = Moved to cart
  2474. /// 6 = Item sold
  2475. /// 7 = Consumed by Four Spirit Analysis (SO_EL_ANALYSIS) skill
  2476. void clif_delitem( map_session_data& sd, int index, int amount, short reason ){
  2477. #if PACKETVER >= 20091117
  2478. PACKET_ZC_DELETE_ITEM_FROM_BODY packet{};
  2479. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_BODY;
  2480. packet.deleteType = reason;
  2481. packet.index = client_index( index );
  2482. packet.count = amount;
  2483. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2484. #else
  2485. clif_dropitem( sd, index, amount );
  2486. #endif
  2487. }
  2488. static void clif_item_equip( short idx, struct EQUIPITEM_INFO *p, struct item *it, struct item_data *id, int eqp_pos ){
  2489. nullpo_retv( p );
  2490. nullpo_retv( it );
  2491. nullpo_retv( id );
  2492. p->index = idx;
  2493. p->ITID = client_nameid( it->nameid );
  2494. p->type = itemtype( it->nameid );
  2495. #if PACKETVER < 20120925
  2496. p->IsIdentified = it->identify ? 1 : 0;
  2497. #endif
  2498. p->location = eqp_pos;
  2499. p->WearState = it->equip;
  2500. #if PACKETVER < 20120925
  2501. p->IsDamaged = it->attribute != 0 ? 1 : 0;
  2502. #endif
  2503. p->RefiningLevel = it->refine;
  2504. clif_addcards( &p->slot, it );
  2505. #if PACKETVER >= 20071002
  2506. p->HireExpireDate = it->expire_time;
  2507. #endif
  2508. #if PACKETVER >= 20080102
  2509. p->bindOnEquipType = it->bound ? 2 : id->flag.bindOnEquip ? 1 : 0;
  2510. #endif
  2511. #if PACKETVER >= 20100629
  2512. // TODO: WBUFW(buf,n+8) = (equip == -2 && id->equip == EQP_AMMO) ? id->equip : 0;
  2513. p->wItemSpriteNumber = ( id->equip&EQP_VISIBLE ) ? id->look : 0;
  2514. #endif
  2515. #if PACKETVER >= 20120925
  2516. p->Flag.IsIdentified = it->identify ? 1 : 0;
  2517. p->Flag.IsDamaged = it->attribute ? 1 : 0;
  2518. p->Flag.PlaceETCTab = it->favorite ? 1 : 0;
  2519. p->Flag.SpareBits = 0;
  2520. #endif
  2521. #if PACKETVER >= 20150226
  2522. clif_add_random_options( p->option_data, *it );
  2523. // Client ingores the value anyway and recalculates it
  2524. p->option_count = 0;
  2525. #endif
  2526. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  2527. p->grade = it->enchantgrade;
  2528. #endif
  2529. }
  2530. static void clif_item_normal( short idx, struct NORMALITEM_INFO *p, struct item *i, struct item_data *id ){
  2531. nullpo_retv(p);
  2532. nullpo_retv(i);
  2533. nullpo_retv(id);
  2534. p->index = idx;
  2535. p->ITID = client_nameid( i->nameid );
  2536. p->type = itemtype( i->nameid );
  2537. #if PACKETVER < 20120925
  2538. p->IsIdentified = i->identify ? 1 : 0;
  2539. #endif
  2540. p->count = i->amount;
  2541. p->WearState = id->equip;
  2542. #if PACKETVER >= 5
  2543. clif_addcards( &p->slot, i );
  2544. #endif
  2545. #if PACKETVER >= 20080102
  2546. p->HireExpireDate = i->expire_time;
  2547. #endif
  2548. #if PACKETVER >= 20120925
  2549. p->Flag.IsIdentified = i->identify ? 1 : 0;
  2550. p->Flag.PlaceETCTab = ( i->favorite != 0 ) ? 1 : 0;
  2551. p->Flag.SpareBits = 0;
  2552. #endif
  2553. }
  2554. static void clif_inventoryStart( map_session_data*sd, e_inventory_type type, const char *name ){
  2555. #if PACKETVER_RE_NUM >= 20180829 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2556. nullpo_retv(sd);
  2557. nullpo_retv(name);
  2558. char buf[sizeof(PACKET_ZC_INVENTORY_START) + 24];
  2559. memset(buf, 0, sizeof(buf));
  2560. PACKET_ZC_INVENTORY_START *p = (PACKET_ZC_INVENTORY_START *)buf;
  2561. p->packetType = HEADER_ZC_INVENTORY_START;
  2562. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2563. p->invType = type;
  2564. #endif
  2565. #if PACKETVER_RE_NUM >= 20180919 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2566. int strLen = (int)safestrnlen(name, 24) + 1;
  2567. if (strLen > 24)
  2568. strLen = 24;
  2569. const int len = sizeof(PACKET_ZC_INVENTORY_START) + strLen;
  2570. p->packetLength = len;
  2571. safestrncpy(p->name, name, strLen);
  2572. #else
  2573. const int len = sizeof(PACKET_ZC_INVENTORY_START);
  2574. safestrncpy(p->name, name, NAME_LENGTH);
  2575. #endif
  2576. clif_send( p, len, &sd->bl, SELF );
  2577. #endif
  2578. }
  2579. static void clif_inventoryEnd( map_session_data *sd, e_inventory_type type ){
  2580. #if PACKETVER_RE_NUM >= 20180829 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2581. nullpo_retv(sd);
  2582. PACKET_ZC_INVENTORY_END p = {};
  2583. p.packetType = HEADER_ZC_INVENTORY_END;
  2584. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2585. p.invType = type;
  2586. #endif
  2587. p.flag = 0;
  2588. clif_send( &p, sizeof(p), &sd->bl, SELF );
  2589. #endif
  2590. }
  2591. //Unified inventory function which sends all of the inventory (requires two packets, one for equipable items and one for stackable ones. [Skotlex]
  2592. void clif_inventorylist( map_session_data *sd ){
  2593. nullpo_retv( sd );
  2594. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2595. e_inventory_type type = INVTYPE_INVENTORY;
  2596. clif_inventoryStart( sd, type, "" );
  2597. #endif
  2598. static struct packet_itemlist_normal itemlist_normal;
  2599. static struct packet_itemlist_equip itemlist_equip;
  2600. int equip = 0;
  2601. int normal = 0;
  2602. for( int i = 0; i < MAX_INVENTORY; i++ ){
  2603. if( sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr ){
  2604. continue;
  2605. }
  2606. // Non-stackable (Equippable)
  2607. if( !itemdb_isstackable2( sd->inventory_data[i] ) ){
  2608. clif_item_equip( client_index( i ), &itemlist_equip.list[equip++], &sd->inventory.u.items_inventory[i], sd->inventory_data[i], pc_equippoint( sd, i ) );
  2609. if( equip == MAX_INVENTORY_ITEM_PACKET_NORMAL ){
  2610. itemlist_equip.PacketType = inventorylistequipType;
  2611. itemlist_equip.PacketLength = static_cast<decltype(itemlist_equip.PacketLength)>( ( sizeof( itemlist_equip ) - sizeof( itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2612. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2613. itemlist_equip.invType = type;
  2614. #endif
  2615. clif_send( &itemlist_equip, itemlist_equip.PacketLength, &sd->bl, SELF );
  2616. equip = 0;
  2617. }
  2618. // Stackable (Normal)
  2619. }else{
  2620. clif_item_normal( client_index( i ), &itemlist_normal.list[normal++], &sd->inventory.u.items_inventory[i], sd->inventory_data[i] );
  2621. if( normal == MAX_INVENTORY_ITEM_PACKET_NORMAL ){
  2622. itemlist_normal.PacketType = inventorylistnormalType;
  2623. itemlist_normal.PacketLength = static_cast<decltype(itemlist_normal.PacketLength)>( ( sizeof( itemlist_normal ) - sizeof( itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2624. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2625. itemlist_normal.invType = type;
  2626. #endif
  2627. clif_send( &itemlist_normal, itemlist_normal.PacketLength, &sd->bl, SELF );
  2628. normal = 0;
  2629. }
  2630. }
  2631. }
  2632. if( normal ){
  2633. itemlist_normal.PacketType = inventorylistnormalType;
  2634. itemlist_normal.PacketLength = static_cast<decltype(itemlist_normal.PacketLength)>( ( sizeof( itemlist_normal ) - sizeof( itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2635. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2636. itemlist_normal.invType = type;
  2637. #endif
  2638. clif_send( &itemlist_normal, itemlist_normal.PacketLength, &sd->bl, SELF );
  2639. }
  2640. if( sd->equip_index[EQI_AMMO] >= 0 )
  2641. clif_arrowequip( *sd );
  2642. if( equip ) {
  2643. itemlist_equip.PacketType = inventorylistequipType;
  2644. itemlist_equip.PacketLength = static_cast<decltype(itemlist_equip.PacketLength)>( ( sizeof( itemlist_equip ) - sizeof( itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2645. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2646. itemlist_equip.invType = type;
  2647. #endif
  2648. clif_send( &itemlist_equip, itemlist_equip.PacketLength, &sd->bl, SELF );
  2649. }
  2650. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2651. clif_inventoryEnd( sd, type );
  2652. #endif
  2653. /* on 20120925 onwards this is a field on clif_item_equip/normal */
  2654. #if PACKETVER >= 20111122 && PACKETVER < 20120925
  2655. for( int i = 0; i < MAX_INVENTORY; i++ ){
  2656. if( sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr )
  2657. continue;
  2658. if ( sd->inventory.u.items_inventory[i].favorite != 0 )
  2659. clif_favorite_item( *sd, i );
  2660. }
  2661. #endif
  2662. }
  2663. //Required when items break/get-repaired. Only sends equippable item list.
  2664. void clif_equiplist( map_session_data *sd ){
  2665. // TODO: implement again => only send equip part
  2666. clif_inventorylist( sd );
  2667. }
  2668. void clif_storagelist(map_session_data* sd, struct item* items, int items_length, const char *storename){
  2669. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2670. e_inventory_type type = INVTYPE_STORAGE;
  2671. clif_inventoryStart( sd, type, storename );
  2672. #endif
  2673. static struct ZC_STORE_ITEMLIST_NORMAL storage_itemlist_normal;
  2674. static struct ZC_STORE_ITEMLIST_EQUIP storage_itemlist_equip;
  2675. int equip = 0;
  2676. int normal = 0;
  2677. for( int i = 0; i < items_length; i++ ){
  2678. if( items[i].nameid == 0 ){
  2679. continue;
  2680. }
  2681. struct item_data* id = itemdb_search( items[i].nameid );
  2682. // Non-stackable (Equippable)
  2683. if( !itemdb_isstackable2( id ) ){
  2684. clif_item_equip( client_storage_index( i ), &storage_itemlist_equip.list[equip++], &items[i], id, pc_equippoint_sub( sd, id ) );
  2685. if( equip == MAX_STORAGE_ITEM_PACKET_EQUIP ){
  2686. storage_itemlist_equip.PacketType = storageListEquipType;
  2687. 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 ) );
  2688. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2689. storage_itemlist_equip.invType = type;
  2690. #elif PACKETVER >= 20120925
  2691. safestrncpy( storage_itemlist_equip.name, storename, NAME_LENGTH );
  2692. #endif
  2693. clif_send( &storage_itemlist_equip, storage_itemlist_equip.PacketLength, &sd->bl, SELF );
  2694. equip = 0;
  2695. }
  2696. // Stackable (Normal)
  2697. }else{
  2698. clif_item_normal( client_storage_index( i ), &storage_itemlist_normal.list[normal++], &items[i], id );
  2699. if( normal == MAX_STORAGE_ITEM_PACKET_NORMAL ){
  2700. storage_itemlist_normal.PacketType = storageListNormalType;
  2701. 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 ) );
  2702. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2703. storage_itemlist_normal.invType = type;
  2704. #elif PACKETVER >= 20120925
  2705. safestrncpy( storage_itemlist_normal.name, storename, NAME_LENGTH );
  2706. #endif
  2707. clif_send( &storage_itemlist_normal, storage_itemlist_normal.PacketLength, &sd->bl, SELF );
  2708. normal = 0;
  2709. }
  2710. }
  2711. }
  2712. if( normal ){
  2713. storage_itemlist_normal.PacketType = storageListNormalType;
  2714. 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 ) );
  2715. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2716. storage_itemlist_normal.invType = type;
  2717. #elif PACKETVER >= 20120925
  2718. safestrncpy( storage_itemlist_normal.name, storename, NAME_LENGTH );
  2719. #endif
  2720. clif_send( &storage_itemlist_normal, storage_itemlist_normal.PacketLength, &sd->bl, SELF );
  2721. }
  2722. if( equip ) {
  2723. storage_itemlist_equip.PacketType = storageListEquipType;
  2724. 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 ) );
  2725. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2726. storage_itemlist_equip.invType = type;
  2727. #elif PACKETVER >= 20120925
  2728. safestrncpy( storage_itemlist_equip.name, storename, NAME_LENGTH );
  2729. #endif
  2730. clif_send( &storage_itemlist_equip, storage_itemlist_equip.PacketLength, &sd->bl, SELF );
  2731. }
  2732. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2733. clif_inventoryEnd( sd, type );
  2734. #endif
  2735. }
  2736. void clif_cartlist( map_session_data *sd ){
  2737. nullpo_retv( sd );
  2738. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2739. e_inventory_type type = INVTYPE_CART;
  2740. clif_inventoryStart( sd, type, "" );
  2741. #endif
  2742. static struct packet_itemlist_normal itemlist_normal;
  2743. static struct packet_itemlist_equip itemlist_equip;
  2744. int normal = 0;
  2745. int equip = 0;
  2746. for( int i = 0; i < MAX_CART; i++ ){
  2747. if( sd->cart.u.items_cart[i].nameid == 0 ){
  2748. continue;
  2749. }
  2750. struct item_data* id = itemdb_search( sd->cart.u.items_cart[i].nameid );
  2751. // Non-stackable (Equippable)
  2752. if( !itemdb_isstackable2(id) ){
  2753. clif_item_equip( client_index( i ), &itemlist_equip.list[equip++], &sd->cart.u.items_cart[i], id, id->equip );
  2754. // Stackable (Normal)
  2755. }else{
  2756. clif_item_normal( client_index( i ), &itemlist_normal.list[normal++], &sd->cart.u.items_cart[i], id );
  2757. }
  2758. }
  2759. if( normal ){
  2760. itemlist_normal.PacketType = cartlistnormalType;
  2761. itemlist_normal.PacketLength = static_cast<decltype(itemlist_normal.PacketLength)>( ( sizeof( itemlist_normal ) - sizeof( itemlist_normal.list ) ) + ( sizeof( struct NORMALITEM_INFO ) * normal ) );
  2762. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2763. itemlist_normal.invType = type;
  2764. #endif
  2765. clif_send( &itemlist_normal, itemlist_normal.PacketLength, &sd->bl, SELF );
  2766. }
  2767. if( equip ){
  2768. itemlist_equip.PacketType = cartlistequipType;
  2769. itemlist_equip.PacketLength = static_cast<decltype(itemlist_equip.PacketLength)>( ( sizeof( itemlist_equip ) - sizeof( itemlist_equip.list ) ) + ( sizeof( struct EQUIPITEM_INFO ) * equip ) );
  2770. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2771. itemlist_equip.invType = type;
  2772. #endif
  2773. clif_send( &itemlist_equip, itemlist_equip.PacketLength, &sd->bl, SELF );
  2774. }
  2775. #if PACKETVER_RE_NUM >= 20180912 || PACKETVER_ZERO_NUM >= 20180919 || PACKETVER_MAIN_NUM >= 20181002
  2776. clif_inventoryEnd( sd, type );
  2777. #endif
  2778. }
  2779. /// Removes cart.
  2780. /// 012b (ZC_CARTOFF)
  2781. /// Client behaviour:
  2782. /// Closes the cart storage and removes all it's items from memory.
  2783. /// The Num & Weight values of the cart are left untouched and the cart is NOT removed.
  2784. void clif_clearcart(int fd)
  2785. {
  2786. PACKET_ZC_CARTOFF packet{};
  2787. packet.packetType = HEADER_ZC_CARTOFF;
  2788. socket_send<PACKET_ZC_CARTOFF>(fd, packet);
  2789. }
  2790. /// Sends XY location to all other guild members
  2791. /// 01eb <account id>.L <x>.W <y>.W (ZC_NOTIFY_POSITION_TO_GUILDM)
  2792. void clif_guild_xy( map_session_data& sd ){
  2793. PACKET_ZC_NOTIFY_POSITION_TO_GUILDM packet{};
  2794. packet.packetType = HEADER_ZC_NOTIFY_POSITION_TO_GUILDM;
  2795. packet.aid = sd.status.account_id;
  2796. packet.xPos = sd.bl.x;
  2797. packet.yPos = sd.bl.y;
  2798. clif_send(&packet, sizeof(packet), &sd.bl, GUILD_SAMEMAP_WOS);
  2799. }
  2800. /// Sends XY location to a specific guild member
  2801. /// 01eb <account id>.L <x>.W <y>.W (ZC_NOTIFY_POSITION_TO_GUILDM)
  2802. void clif_guild_xy_single( map_session_data& sd, map_session_data& tsd ){
  2803. if( sd.bg_id ){
  2804. return;
  2805. }
  2806. PACKET_ZC_NOTIFY_POSITION_TO_GUILDM packet{};
  2807. packet.packetType = HEADER_ZC_NOTIFY_POSITION_TO_GUILDM;
  2808. packet.aid = sd.status.account_id;
  2809. packet.xPos = sd.bl.x;
  2810. packet.yPos = sd.bl.y;
  2811. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  2812. }
  2813. /// Removes XY location from all other guild members
  2814. /// 01eb <account id>.L <x>.W <y>.W (ZC_NOTIFY_POSITION_TO_GUILDM)
  2815. void clif_guild_xy_remove( map_session_data& sd ){
  2816. PACKET_ZC_NOTIFY_POSITION_TO_GUILDM packet{};
  2817. packet.packetType = HEADER_ZC_NOTIFY_POSITION_TO_GUILDM;
  2818. packet.aid = sd.status.account_id;
  2819. packet.xPos = -1;
  2820. packet.yPos = -1;
  2821. clif_send( &packet, sizeof( packet ), &sd.bl, GUILD_SAMEMAP_WOS );
  2822. }
  2823. /*==========================================
  2824. * Load castle list for guild UI. [Asheraf] / [Balfear]
  2825. *------------------------------------------*/
  2826. void clif_guild_castle_list(map_session_data& sd){
  2827. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190717 || PACKETVER_ZERO_NUM >= 20190814
  2828. auto &g = sd.guild;
  2829. if (g == nullptr)
  2830. return;
  2831. int castle_count = guild_checkcastles(g->guild);
  2832. if( castle_count <= 0 ){
  2833. return;
  2834. }
  2835. PACKET_ZC_GUILD_AGIT_INFO* p = reinterpret_cast<PACKET_ZC_GUILD_AGIT_INFO*>( packet_buffer );
  2836. p->packetType = HEADER_ZC_GUILD_AGIT_INFO;
  2837. p->packetLength = sizeof( *p );
  2838. int i = 0;
  2839. for (const auto& gc : castle_db) {
  2840. if (gc.second->guild_id == g->guild.guild_id && gc.second->client_id) {
  2841. p->castle_list[i] = static_cast<int8>( gc.second->client_id );
  2842. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->castle_list[0] ) );
  2843. ++i;
  2844. }
  2845. }
  2846. clif_send(p, p->packetLength, &sd.bl, SELF);
  2847. #endif
  2848. }
  2849. /*==========================================
  2850. * Send castle info Economy/Defence. [Asheraf] / [Balfear]
  2851. *------------------------------------------*/
  2852. void clif_guild_castleinfo(map_session_data& sd, std::shared_ptr<guild_castle> castle ){
  2853. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190717 || PACKETVER_ZERO_NUM >= 20190814
  2854. if( castle->client_id == 0 ){
  2855. return;
  2856. }
  2857. PACKET_ZC_REQ_ACK_AGIT_INVESTMENT p = {};
  2858. p.packetType = HEADER_ZC_REQ_ACK_AGIT_INVESTMENT;
  2859. p.castle_id = static_cast<int8>( castle->client_id );
  2860. p.economy = castle->economy;
  2861. p.defense = castle->defense;
  2862. clif_send(&p, sizeof(p), &sd.bl, SELF);
  2863. #endif
  2864. }
  2865. /*==========================================
  2866. * Show teleport request result. [Asheraf] / [Balfear]
  2867. *------------------------------------------*/
  2868. void clif_guild_castle_teleport_res(map_session_data& sd, enum e_siege_teleport_result result){
  2869. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190717 || PACKETVER_ZERO_NUM >= 20190814
  2870. PACKET_ZC_REQ_ACK_MOVE_GUILD_AGIT p = {};
  2871. p.packetType = HEADER_ZC_REQ_ACK_MOVE_GUILD_AGIT;
  2872. p.result = static_cast<int16>( result );
  2873. clif_send(&p, sizeof(p), &sd.bl, SELF);
  2874. #endif
  2875. }
  2876. /*==========================================
  2877. * Request castle info. [Asheraf] / [Balfear]
  2878. *------------------------------------------*/
  2879. void clif_parse_guild_castle_info_request(int fd, map_session_data* sd){
  2880. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190522 || PACKETVER_ZERO_NUM >= 20190515
  2881. if( sd == nullptr ){
  2882. return;
  2883. }
  2884. auto &g = sd->guild;
  2885. if (g == nullptr)
  2886. return;
  2887. const PACKET_CZ_REQ_AGIT_INVESTMENT* p = reinterpret_cast<PACKET_CZ_REQ_AGIT_INVESTMENT*>( RFIFOP( fd, 0 ) );
  2888. std::shared_ptr<guild_castle> gc = castle_db.find_by_clientid( p->castle_id );
  2889. if (gc == nullptr)
  2890. return;
  2891. if (gc->guild_id != g->guild.guild_id)
  2892. return;
  2893. clif_guild_castleinfo(*sd, gc);
  2894. #endif
  2895. }
  2896. /*==========================================
  2897. * Teleport to castle. [Asheraf] / [Balfear]
  2898. *------------------------------------------*/
  2899. void clif_parse_guild_castle_teleport_request(int fd, map_session_data* sd){
  2900. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190522 || PACKETVER_ZERO_NUM >= 20190515
  2901. const PACKET_CZ_REQ_MOVE_GUILD_AGIT* p = reinterpret_cast<PACKET_CZ_REQ_MOVE_GUILD_AGIT*>RFIFOP( fd, 0 );
  2902. auto &g = sd->guild;
  2903. if (g == nullptr)
  2904. return;
  2905. std::shared_ptr<guild_castle> gc = castle_db.find_by_clientid( p->castle_id );
  2906. if (gc == nullptr)
  2907. return;
  2908. if (!gc->warp_enabled)
  2909. return;
  2910. if (gc->guild_id != g->guild.guild_id)
  2911. return;
  2912. if (map_getmapflag(sd->bl.m, MF_GVG_CASTLE)
  2913. || map_getmapflag(sd->bl.m, MF_NOTELEPORT)
  2914. || map_getmapflag(sd->bl.m, MF_NOWARP))
  2915. return;
  2916. uint32 zeny = gc->zeny;
  2917. switch( gc->type ){
  2918. case WOE_FIRST_EDITION:
  2919. if( agit_flag ){
  2920. zeny = gc->zeny_siege;
  2921. }
  2922. break;
  2923. case WOE_SECOND_EDITION:
  2924. if( agit2_flag ){
  2925. zeny = gc->zeny_siege;
  2926. }
  2927. break;
  2928. case WOE_THIRD_EDITION:
  2929. if( agit3_flag ){
  2930. zeny = gc->zeny_siege;
  2931. }
  2932. break;
  2933. }
  2934. if (zeny && pc_payzeny(sd, zeny, LOG_TYPE_OTHER)) {
  2935. clif_guild_castle_teleport_res(*sd, SIEGE_TP_NOT_ENOUGH_ZENY);
  2936. return;
  2937. }
  2938. pc_setpos(sd, gc->mapindex, gc->warp_x, gc->warp_y, CLR_OUTSIGHT);
  2939. #endif
  2940. }
  2941. /*==========================================
  2942. *
  2943. *------------------------------------------*/
  2944. static int clif_hpmeter_sub( struct block_list *bl, va_list ap ){
  2945. map_session_data* sd = va_arg( ap, map_session_data* );
  2946. map_session_data* tsd = BL_CAST( BL_PC, bl );
  2947. nullpo_ret(sd);
  2948. nullpo_ret(tsd);
  2949. if( pc_has_permission( tsd, PC_PERM_VIEW_HPMETER ) ){
  2950. clif_hpmeter_single( *tsd, sd->status.account_id, sd->battle_status.hp, sd->battle_status.max_hp );
  2951. }
  2952. return 0;
  2953. }
  2954. /*==========================================
  2955. * Server tells all players that are allowed to view HP bars
  2956. * and are nearby 'sd' that 'sd' hp bar was updated.
  2957. *------------------------------------------*/
  2958. static int clif_hpmeter(map_session_data *sd)
  2959. {
  2960. nullpo_ret(sd);
  2961. 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);
  2962. return 0;
  2963. }
  2964. /**
  2965. * Send HP bar update to others.
  2966. * @param sd: Player invoking update
  2967. */
  2968. void clif_update_hp(map_session_data &sd) {
  2969. if (map_getmapdata(sd.bl.m)->hpmeter_visible)
  2970. clif_hpmeter(&sd);
  2971. if (!battle_config.party_hp_mode && sd.status.party_id)
  2972. clif_party_hp( sd );
  2973. if (sd.bg_id)
  2974. clif_bg_hp(&sd);
  2975. }
  2976. /// Notifies client of a character parameter change.
  2977. /// 00b0 <var id>.W <value>.L (ZC_PAR_CHANGE)
  2978. static void clif_par_change(map_session_data& sd, uint16 varId, int count) {
  2979. PACKET_ZC_PAR_CHANGE packet{};
  2980. packet.PacketType = HEADER_ZC_PAR_CHANGE;
  2981. packet.varID = varId;
  2982. packet.count = count;
  2983. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2984. }
  2985. /// Notifies client of a character parameter change.
  2986. /// 00b1 <var id>.W <value>.L (ZC_LONGPAR_CHANGE)
  2987. static void clif_longpar_change(map_session_data& sd, uint16 varId, int amount) {
  2988. PACKET_ZC_LONGPAR_CHANGE packet{};
  2989. packet.PacketType = HEADER_ZC_LONGPAR_CHANGE;
  2990. packet.varID = varId;
  2991. packet.amount = amount;
  2992. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  2993. }
  2994. /// Notifies client of a character parameter change.
  2995. /// 00be <status id>.W <value>.B (ZC_STATUS_CHANGE)
  2996. static void clif_zc_status_change(map_session_data& sd, uint16 status_id, uint8 value) {
  2997. PACKET_ZC_STATUS_CHANGE packet{};
  2998. packet.PacketType = HEADER_ZC_STATUS_CHANGE;
  2999. packet.statusID = status_id;
  3000. packet.value = value;
  3001. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3002. }
  3003. /// Notifies client of a character parameter change.
  3004. /// 0121 <current count>.W <max count>.W <current weight>.L <max weight>.L (ZC_NOTIFY_CARTITEM_COUNTINFO)
  3005. static void clif_cartcount( map_session_data& sd ){
  3006. PACKET_ZC_NOTIFY_CARTITEM_COUNTINFO packet{};
  3007. packet.PacketType = HEADER_ZC_NOTIFY_CARTITEM_COUNTINFO;
  3008. packet.curCount = sd.cart_num;
  3009. packet.maxCount = MAX_CART;
  3010. packet.curWeight = sd.cart_weight;
  3011. packet.maxWeight = sd.cart_weight_max;
  3012. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3013. }
  3014. /// Tells the client how far it is allowed to attack (weapon range)
  3015. /// 013a <atk range>.W (ZC_ATTACK_RANGE)
  3016. static void clif_attackrange( map_session_data& sd, int16 range ){
  3017. PACKET_ZC_ATTACK_RANGE packet{};
  3018. packet.PacketType = HEADER_ZC_ATTACK_RANGE;
  3019. packet.currentAttRange = range;
  3020. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3021. }
  3022. /// Notifies client of a character parameter change.
  3023. /// 0141 <status id>.L <base status>.L <plus status>.L (ZC_COUPLESTATUS)
  3024. static void clif_couplestatus(map_session_data& sd, uint32 status_type, int32 defaultStatus, int32 plusStatus) {
  3025. PACKET_ZC_COUPLESTATUS packet{};
  3026. packet.PacketType = HEADER_ZC_COUPLESTATUS;
  3027. packet.statusType = status_type;
  3028. packet.defaultStatus = defaultStatus;
  3029. packet.plusStatus = plusStatus;
  3030. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  3031. }
  3032. /// Notifies client of a character parameter change.
  3033. /// 0acb <var id>.W <value>.Q (ZC_LONGLONGPAR_CHANGE)
  3034. static void clif_longlongpar_change(map_session_data& sd, uint16 varId, int64 amount) {
  3035. #if PACKETVER_MAIN_NUM >= 20170906 || PACKETVER_RE_NUM >= 20170830 || defined(PACKETVER_ZERO)
  3036. PACKET_ZC_LONGLONGPAR_CHANGE packet{};
  3037. packet.PacketType = HEADER_ZC_LONGLONGPAR_CHANGE;
  3038. packet.varID = varId;
  3039. packet.amount = amount;
  3040. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3041. #endif
  3042. }
  3043. /// Notifies client of a character parameter change.
  3044. void clif_updatestatus( map_session_data& sd, enum _sp type ){
  3045. switch(type){
  3046. case SP_WEIGHT:
  3047. pc_updateweightstatus(&sd);
  3048. clif_par_change(sd, type, sd.weight);
  3049. break;
  3050. case SP_MAXWEIGHT:
  3051. clif_par_change(sd, type, sd.max_weight);
  3052. break;
  3053. case SP_SPEED:
  3054. clif_par_change(sd, type, sd.battle_status.speed);
  3055. break;
  3056. case SP_BASELEVEL:
  3057. clif_par_change(sd, type, sd.status.base_level);
  3058. break;
  3059. case SP_JOBLEVEL:
  3060. clif_par_change(sd, type, sd.status.job_level);
  3061. break;
  3062. case SP_KARMA:
  3063. // Adding this back, I wonder if the client intercepts this - [Lance]
  3064. clif_par_change(sd, type, sd.status.karma);
  3065. break;
  3066. case SP_MANNER:
  3067. clif_par_change(sd, type, sd.status.manner);
  3068. break;
  3069. case SP_STATUSPOINT:
  3070. clif_par_change(sd, type, sd.status.status_point);
  3071. break;
  3072. case SP_SKILLPOINT:
  3073. clif_par_change(sd, type, sd.status.skill_point);
  3074. break;
  3075. case SP_HIT:
  3076. clif_par_change(sd, type, sd.battle_status.hit);
  3077. break;
  3078. case SP_FLEE1:
  3079. clif_par_change(sd, type, sd.battle_status.flee);
  3080. break;
  3081. case SP_FLEE2:
  3082. clif_par_change(sd, type, sd.battle_status.flee2/10);
  3083. break;
  3084. case SP_MAXHP:
  3085. clif_par_change(sd, type, sd.battle_status.max_hp);
  3086. break;
  3087. case SP_MAXSP:
  3088. clif_par_change(sd, type, sd.battle_status.max_sp);
  3089. break;
  3090. case SP_HP:
  3091. // On officials the HP never go below 1, even if you die [Lemongrass]
  3092. // On officials the HP Novice class never go below 50%, even if you die [Napster]
  3093. if (sd.battle_status.hp == 0) {
  3094. clif_par_change(sd, type, (sd.class_&MAPID_UPPERMASK) != MAPID_NOVICE ? 1 : sd.battle_status.max_hp/2);
  3095. } else {
  3096. clif_par_change(sd, type, sd.battle_status.hp);
  3097. }
  3098. break;
  3099. case SP_SP:
  3100. clif_par_change(sd, type, sd.battle_status.sp);
  3101. break;
  3102. case SP_ASPD:
  3103. clif_par_change(sd, type, sd.battle_status.amotion);
  3104. break;
  3105. case SP_ATK1:
  3106. clif_par_change(sd, type, pc_leftside_atk(&sd));
  3107. break;
  3108. case SP_DEF1:
  3109. clif_par_change(sd, type, pc_leftside_def(&sd));
  3110. break;
  3111. case SP_MDEF1:
  3112. clif_par_change(sd, type, pc_leftside_mdef(&sd));
  3113. break;
  3114. case SP_ATK2:
  3115. clif_par_change(sd, type, pc_rightside_atk(&sd));
  3116. break;
  3117. case SP_DEF2:
  3118. clif_par_change(sd, type, pc_rightside_def(&sd));
  3119. break;
  3120. case SP_MDEF2: {
  3121. //negative check (in case you have something like Berserk active)
  3122. int mdef2 = pc_rightside_mdef(&sd);
  3123. #ifndef RENEWAL
  3124. clif_par_change(sd, type, mdef2 < 0 ? 0 : mdef2);
  3125. #else
  3126. clif_par_change(sd, type, mdef2);
  3127. #endif
  3128. }
  3129. break;
  3130. case SP_CRITICAL:
  3131. clif_par_change(sd, type, sd.battle_status.cri/10);
  3132. break;
  3133. case SP_MATK1:
  3134. clif_par_change(sd, type, pc_rightside_matk(&sd));
  3135. break;
  3136. case SP_MATK2:
  3137. clif_par_change(sd, type, pc_leftside_matk(&sd));
  3138. break;
  3139. case SP_ZENY:
  3140. clif_longpar_change(sd, type, sd.status.zeny);
  3141. break;
  3142. #if PACKETVER >= 20170830
  3143. case SP_BASEEXP:
  3144. clif_longlongpar_change(sd, type, client_exp(sd.status.base_exp));
  3145. break;
  3146. case SP_JOBEXP:
  3147. clif_longlongpar_change(sd, type, client_exp(sd.status.job_exp));
  3148. break;
  3149. case SP_NEXTBASEEXP:
  3150. clif_longlongpar_change(sd, type, client_exp(pc_nextbaseexp(&sd)));
  3151. break;
  3152. case SP_NEXTJOBEXP:
  3153. clif_longlongpar_change(sd, type, client_exp(pc_nextjobexp(&sd)));
  3154. break;
  3155. #else
  3156. case SP_BASEEXP:
  3157. clif_longpar_change(sd, type, client_exp(sd.status.base_exp));
  3158. break;
  3159. case SP_JOBEXP:
  3160. clif_longpar_change(sd, type, client_exp(sd.status.job_exp));
  3161. break;
  3162. case SP_NEXTBASEEXP:
  3163. clif_longpar_change(sd, type, client_exp(pc_nextbaseexp(&sd)));
  3164. break;
  3165. case SP_NEXTJOBEXP:
  3166. clif_longpar_change(sd, type, client_exp(pc_nextjobexp(&sd)));
  3167. break;
  3168. #endif
  3169. /**
  3170. * SP_U<STAT> are used to update the amount of points necessary to increase that stat
  3171. **/
  3172. case SP_USTR:
  3173. case SP_UAGI:
  3174. case SP_UVIT:
  3175. case SP_UINT:
  3176. case SP_UDEX:
  3177. case SP_ULUK:
  3178. clif_zc_status_change(sd, static_cast<uint16>(type), static_cast<uint8>(pc_need_status_point(&sd, type-SP_USTR+SP_STR, 1)));
  3179. break;
  3180. case SP_ATTACKRANGE:
  3181. clif_attackrange( sd, sd.battle_status.rhw.range );
  3182. break;
  3183. case SP_STR:
  3184. clif_couplestatus(sd, type, sd.status.str, sd.battle_status.str - sd.status.str);
  3185. break;
  3186. case SP_AGI:
  3187. clif_couplestatus(sd, type, sd.status.agi, sd.battle_status.agi - sd.status.agi);
  3188. break;
  3189. case SP_VIT:
  3190. clif_couplestatus(sd, type, sd.status.vit, sd.battle_status.vit - sd.status.vit);
  3191. break;
  3192. case SP_INT:
  3193. clif_couplestatus(sd, type, sd.status.int_, sd.battle_status.int_ - sd.status.int_);
  3194. break;
  3195. case SP_DEX:
  3196. clif_couplestatus(sd, type, sd.status.dex, sd.battle_status.dex - sd.status.dex);
  3197. break;
  3198. case SP_LUK:
  3199. clif_couplestatus(sd, type, sd.status.luk, sd.battle_status.luk - sd.status.luk);
  3200. break;
  3201. case SP_CARTINFO:
  3202. clif_cartcount( sd );
  3203. break;
  3204. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  3205. case SP_AP:
  3206. clif_par_change( sd, SP_AP, sd.battle_status.ap );
  3207. break;
  3208. case SP_TRAITPOINT:
  3209. clif_par_change( sd, SP_TRAITPOINT, sd.status.trait_point );
  3210. break;
  3211. case SP_MAXAP:
  3212. clif_par_change( sd, SP_MAXAP, sd.battle_status.max_ap );
  3213. break;
  3214. case SP_POW:
  3215. clif_couplestatus( sd, SP_POW, sd.status.pow, sd.battle_status.pow - sd.status.pow );
  3216. break;
  3217. case SP_STA:
  3218. clif_couplestatus( sd, SP_STA, sd.status.sta, sd.battle_status.sta - sd.status.sta );
  3219. break;
  3220. case SP_WIS:
  3221. clif_couplestatus( sd, SP_WIS, sd.status.wis, sd.battle_status.wis - sd.status.wis );
  3222. break;
  3223. case SP_SPL:
  3224. clif_couplestatus( sd, SP_SPL, sd.status.spl, sd.battle_status.spl - sd.status.spl );
  3225. break;
  3226. case SP_CON:
  3227. clif_couplestatus( sd, SP_CON, sd.status.con, sd.battle_status.con - sd.status.con );
  3228. break;
  3229. case SP_CRT:
  3230. clif_couplestatus( sd, SP_CRT, sd.status.crt, sd.battle_status.crt - sd.status.crt );
  3231. break;
  3232. case SP_UPOW:
  3233. case SP_USTA:
  3234. case SP_UWIS:
  3235. case SP_USPL:
  3236. case SP_UCON:
  3237. case SP_UCRT:
  3238. clif_zc_status_change( sd, static_cast<uint16>( type ), static_cast<uint8>( pc_need_trait_point( &sd, type - SP_UPOW + SP_POW, 1 ) ) );
  3239. break;
  3240. case SP_PATK:
  3241. clif_par_change( sd, SP_PATK, sd.battle_status.patk );
  3242. break;
  3243. case SP_SMATK:
  3244. clif_par_change( sd, SP_SMATK, sd.battle_status.smatk );
  3245. break;
  3246. case SP_RES:
  3247. clif_par_change( sd, SP_RES, sd.battle_status.res );
  3248. break;
  3249. case SP_MRES:
  3250. clif_par_change( sd, SP_MRES, sd.battle_status.mres );
  3251. break;
  3252. case SP_HPLUS:
  3253. clif_par_change( sd, SP_HPLUS, sd.battle_status.hplus );
  3254. break;
  3255. case SP_CRATE:
  3256. clif_par_change( sd, SP_CRATE, sd.battle_status.crate );
  3257. break;
  3258. #else
  3259. case SP_AP:
  3260. case SP_TRAITPOINT:
  3261. case SP_MAXAP:
  3262. case SP_POW:
  3263. case SP_STA:
  3264. case SP_WIS:
  3265. case SP_SPL:
  3266. case SP_CON:
  3267. case SP_CRT:
  3268. case SP_UPOW:
  3269. case SP_USTA:
  3270. case SP_UWIS:
  3271. case SP_USPL:
  3272. case SP_UCON:
  3273. case SP_UCRT:
  3274. case SP_PATK:
  3275. case SP_SMATK:
  3276. case SP_RES:
  3277. case SP_MRES:
  3278. case SP_HPLUS:
  3279. case SP_CRATE:
  3280. // 4th job status are not supported by older clients
  3281. return;
  3282. #endif
  3283. default:
  3284. ShowError("clif_updatestatus : unrecognized type %d\n",type);
  3285. return;
  3286. }
  3287. // Additional update packets that should be sent right after
  3288. switch( type ){
  3289. case SP_BASELEVEL:
  3290. if( sd.status.party_id ){
  3291. struct party_data* p;
  3292. int i;
  3293. if( ( p = party_search( sd.status.party_id ) ) != nullptr ){
  3294. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd.status.char_id);
  3295. if( i < MAX_PARTY ){
  3296. p->party.member[i].lv = sd.status.base_level;
  3297. clif_party_job_and_level( sd );
  3298. }
  3299. }
  3300. }
  3301. break;
  3302. case SP_HP:
  3303. clif_update_hp( sd );
  3304. break;
  3305. }
  3306. }
  3307. /// Notifies client of a parameter change of an another player.
  3308. /// 01ab <account id>.L <var id>.W <value>.L (ZC_PAR_CHANGE_USER)
  3309. /// var id:
  3310. /// SP_MANNER
  3311. /// ?
  3312. void clif_changemanner( map_session_data& sd ) {
  3313. PACKET_ZC_PAR_CHANGE_USER packet{};
  3314. packet.packetType = HEADER_ZC_PAR_CHANGE_USER;
  3315. packet.gid = sd.bl.id;
  3316. packet.type = static_cast<decltype(packet.type)>(SP_MANNER);
  3317. packet.value = sd.status.manner;
  3318. clif_send( &packet, sizeof( packet ), &sd.bl, AREA_WOS );
  3319. }
  3320. /// 00c3 <id>.L <type>.B <value>.B (ZC_SPRITE_CHANGE)
  3321. /// 01d7 <id>.L <type>.B <value1>.W <value2>.W (ZC_SPRITE_CHANGE2)
  3322. void clif_sprite_change( struct block_list *bl, int id, int type, int val, int val2, enum send_target target ){
  3323. PACKET_ZC_SPRITE_CHANGE p = {};
  3324. p.packetType = sendLookType;
  3325. p.AID = id;
  3326. p.type = type;
  3327. p.val = val;
  3328. #if PACKETVER >= 4
  3329. p.val2 = val2;
  3330. #endif
  3331. clif_send( &p, sizeof( p ), bl, target );
  3332. }
  3333. /// Updates sprite/style properties of an object.
  3334. void clif_changelook(struct block_list *bl, int type, int val) {
  3335. map_session_data* sd;
  3336. status_change* sc;
  3337. struct view_data* vd;
  3338. enum send_target target = AREA;
  3339. int val2 = 0;
  3340. nullpo_retv(bl);
  3341. sd = BL_CAST(BL_PC, bl);
  3342. sc = status_get_sc(bl);
  3343. vd = status_get_viewdata(bl);
  3344. if( vd ) //temp hack to let Warp Portal change appearance
  3345. switch(type) {
  3346. case LOOK_WEAPON:
  3347. if (sd) {
  3348. clif_get_weapon_view(sd, &vd->weapon, &vd->shield);
  3349. val = vd->weapon;
  3350. }
  3351. else
  3352. vd->weapon = val;
  3353. break;
  3354. case LOOK_SHIELD:
  3355. if (sd) {
  3356. clif_get_weapon_view(sd, &vd->weapon, &vd->shield);
  3357. val = vd->shield;
  3358. }
  3359. else
  3360. vd->shield = val;
  3361. break;
  3362. case LOOK_BASE:
  3363. if (!sd)
  3364. break;
  3365. if ( val == JT_INVISIBLE )
  3366. return;
  3367. if (sd->sc.option&OPTION_COSTUME)
  3368. vd->weapon = vd->shield = 0;
  3369. if (!vd->cloth_color)
  3370. break;
  3371. if (sd) {
  3372. if (sd->sc.option&OPTION_WEDDING && battle_config.wedding_ignorepalette)
  3373. vd->cloth_color = 0;
  3374. if (sd->sc.option&OPTION_XMAS && battle_config.xmas_ignorepalette)
  3375. vd->cloth_color = 0;
  3376. if (sd->sc.option&(OPTION_SUMMER|OPTION_SUMMER2) && battle_config.summer_ignorepalette)
  3377. vd->cloth_color = 0;
  3378. if (sd->sc.option&OPTION_HANBOK && battle_config.hanbok_ignorepalette)
  3379. vd->cloth_color = 0;
  3380. if (sd->sc.option&OPTION_OKTOBERFEST && battle_config.oktoberfest_ignorepalette)
  3381. vd->cloth_color = 0;
  3382. if (vd->body_style && sd->sc.option&OPTION_COSTUME)
  3383. vd->body_style = 0;
  3384. }
  3385. break;
  3386. case LOOK_HAIR:
  3387. vd->hair_style = val;
  3388. break;
  3389. case LOOK_HEAD_BOTTOM:
  3390. vd->head_bottom = val;
  3391. break;
  3392. case LOOK_HEAD_TOP:
  3393. vd->head_top = val;
  3394. break;
  3395. case LOOK_HEAD_MID:
  3396. vd->head_mid = val;
  3397. break;
  3398. case LOOK_HAIR_COLOR:
  3399. vd->hair_color = val;
  3400. break;
  3401. case LOOK_CLOTHES_COLOR:
  3402. if (val && sd) {
  3403. if (sd->sc.option&OPTION_WEDDING && battle_config.wedding_ignorepalette)
  3404. val = 0;
  3405. if (sd->sc.option&OPTION_XMAS && battle_config.xmas_ignorepalette)
  3406. val = 0;
  3407. if (sd->sc.option&(OPTION_SUMMER|OPTION_SUMMER2) && battle_config.summer_ignorepalette)
  3408. val = 0;
  3409. if (sd->sc.option&OPTION_HANBOK && battle_config.hanbok_ignorepalette)
  3410. val = 0;
  3411. if (sd->sc.option&OPTION_OKTOBERFEST && battle_config.oktoberfest_ignorepalette)
  3412. val = 0;
  3413. }
  3414. vd->cloth_color = val;
  3415. break;
  3416. case LOOK_SHOES:
  3417. #if PACKETVER > 3
  3418. if (sd) {
  3419. int n;
  3420. if((n = sd->equip_index[EQI_SHOES]) >= 0 && sd->inventory_data[n]) {
  3421. if(sd->inventory_data[n]->view_id > 0)
  3422. val = sd->inventory_data[n]->view_id;
  3423. else
  3424. val = sd->inventory.u.items_inventory[n].nameid;
  3425. } else
  3426. val = 0;
  3427. }
  3428. #endif
  3429. //Shoes? No packet uses this....
  3430. break;
  3431. case LOOK_BODY:
  3432. // unknown purpose
  3433. break;
  3434. case LOOK_ROBE:
  3435. #if PACKETVER < 20110111
  3436. return;
  3437. #else
  3438. vd->robe = val;
  3439. #endif
  3440. break;
  3441. case LOOK_BODY2:
  3442. #if PACKETVER < 20150513
  3443. return;
  3444. #else
  3445. if (val && sd && sd->sc.option&OPTION_COSTUME)
  3446. val = 0;
  3447. vd->body_style = val;
  3448. #endif
  3449. break;
  3450. }
  3451. // prevent leaking the presence of GM-hidden objects
  3452. if( sc && sc->option&OPTION_INVISIBLE )
  3453. target = SELF;
  3454. #if PACKETVER < 4
  3455. if (target != SELF && bl->type == BL_NPC && (((TBL_NPC*)bl)->is_invisible))
  3456. target = SELF;
  3457. clif_sprite_change(bl, bl->id, type, val, 0, target);
  3458. #else
  3459. if (bl->type != BL_NPC) {
  3460. if (type == LOOK_WEAPON || type == LOOK_SHIELD) {
  3461. type = LOOK_WEAPON;
  3462. val = (vd ? vd->weapon : 0);
  3463. val2 = (vd ? vd->shield : 0);
  3464. }
  3465. if (disguised(bl)) {
  3466. clif_sprite_change(bl, bl->id, type, val, val2, AREA_WOS);
  3467. clif_sprite_change(bl, disguised_bl_id(bl->id), type, val, val2, SELF);
  3468. } else
  3469. clif_sprite_change(bl, bl->id, type, val, val2, target);
  3470. } else
  3471. unit_refresh(bl);
  3472. #endif
  3473. }
  3474. //Sends a change-base-look packet required for traps as they are triggered.
  3475. void clif_changetraplook(struct block_list *bl,int val) {
  3476. clif_sprite_change(bl, bl->id, LOOK_BASE, val, 0, AREA);
  3477. }
  3478. /// For the stupid cloth-dye bug. Resends the given view data to the area specified by bl.
  3479. void clif_refreshlook(struct block_list *bl, int id, int type, int val, enum send_target target) {
  3480. clif_sprite_change(bl, id, type, val, 0, target);
  3481. }
  3482. /// Character status (ZC_STATUS).
  3483. /// 00bd <stpoint>.W <str>.B <need str>.B <agi>.B <need agi>.B <vit>.B <need vit>.B
  3484. /// <int>.B <need int>.B <dex>.B <need dex>.B <luk>.B <need luk>.B <atk>.W <atk2>.W
  3485. /// <matk min>.W <matk max>.W <def>.W <def2>.W <mdef>.W <mdef2>.W <hit>.W
  3486. /// <flee>.W <flee2>.W <crit>.W <aspd>.W <aspd2>.W
  3487. void clif_initialstatus( map_session_data& sd ) {
  3488. PACKET_ZC_STATUS packet{};
  3489. packet.packetType = HEADER_ZC_STATUS;
  3490. packet.point = min(sd.status.status_point, INT16_MAX);
  3491. packet.str = min(sd.status.str, UINT8_MAX);
  3492. packet.standardStr = pc_need_status_point( &sd, SP_STR, 1 );
  3493. packet.agi = min(sd.status.agi, UINT8_MAX);
  3494. packet.standardAgi = pc_need_status_point( &sd, SP_AGI,1 );
  3495. packet.vit = min(sd.status.vit, UINT8_MAX);
  3496. packet.standardVit = pc_need_status_point( &sd, SP_VIT,1 );
  3497. packet.int_ = min(sd.status.int_, UINT8_MAX);
  3498. packet.standardInt = pc_need_status_point( &sd, SP_INT,1 );
  3499. packet.dex = min(sd.status.dex, UINT8_MAX);
  3500. packet.standardDex = pc_need_status_point( &sd, SP_DEX,1 );
  3501. packet.luk = min(sd.status.luk, UINT8_MAX);
  3502. packet.standardLuk = pc_need_status_point( &sd, SP_LUK,1 );
  3503. packet.attPower = pc_leftside_atk( &sd );
  3504. packet.refiningPower = pc_rightside_atk( &sd );
  3505. packet.max_mattPower = pc_rightside_matk( &sd );
  3506. packet.min_mattPower = pc_leftside_matk( &sd );
  3507. packet.itemdefPower = pc_leftside_def( &sd );
  3508. packet.plusdefPower = pc_rightside_def( &sd );
  3509. packet.mdefPower = pc_leftside_mdef( &sd );
  3510. #ifdef RENEWAL
  3511. packet.plusmdefPower = pc_rightside_mdef( &sd );
  3512. #else
  3513. // Negative check for Frenzy'ed characters.
  3514. packet.plusmdefPower = std::max( pc_rightside_mdef( &sd ), 0 );
  3515. #endif
  3516. packet.hitSuccessValue = sd.battle_status.hit;
  3517. packet.avoidSuccessValue = sd.battle_status.flee;
  3518. packet.plusAvoidSuccessValue = sd.battle_status.flee2 / 10;
  3519. packet.criticalSuccessValue = sd.battle_status.cri / 10;
  3520. packet.ASPD = sd.battle_status.amotion;
  3521. packet.plusASPD = 0;
  3522. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3523. clif_updatestatus(sd, SP_STR);
  3524. clif_updatestatus(sd, SP_AGI);
  3525. clif_updatestatus(sd, SP_VIT);
  3526. clif_updatestatus(sd, SP_INT);
  3527. clif_updatestatus(sd, SP_DEX);
  3528. clif_updatestatus(sd, SP_LUK);
  3529. clif_updatestatus(sd, SP_ATTACKRANGE);
  3530. clif_updatestatus(sd, SP_ASPD);
  3531. #ifdef RENEWAL
  3532. clif_updatestatus(sd, SP_POW);
  3533. clif_updatestatus(sd, SP_STA);
  3534. clif_updatestatus(sd, SP_WIS);
  3535. clif_updatestatus(sd, SP_SPL);
  3536. clif_updatestatus(sd, SP_CON);
  3537. clif_updatestatus(sd, SP_CRT);
  3538. clif_updatestatus(sd, SP_PATK);
  3539. clif_updatestatus(sd, SP_SMATK);
  3540. clif_updatestatus(sd, SP_RES);
  3541. clif_updatestatus(sd, SP_MRES);
  3542. clif_updatestatus(sd, SP_HPLUS);
  3543. clif_updatestatus(sd, SP_CRATE);
  3544. clif_updatestatus(sd, SP_TRAITPOINT);
  3545. clif_updatestatus(sd, SP_AP);
  3546. clif_updatestatus(sd, SP_MAXAP);
  3547. clif_updatestatus(sd, SP_UPOW);
  3548. clif_updatestatus(sd, SP_USTA);
  3549. clif_updatestatus(sd, SP_UWIS);
  3550. clif_updatestatus(sd, SP_USPL);
  3551. clif_updatestatus(sd, SP_UCON);
  3552. clif_updatestatus(sd, SP_UCRT);
  3553. #endif
  3554. }
  3555. /// Marks an ammunition item in inventory as equipped.
  3556. /// 013c <index>.W (ZC_EQUIP_ARROW)
  3557. void clif_arrowequip( map_session_data& sd ) {
  3558. #if PACKETVER >= 20121128
  3559. clif_status_change(&sd.bl, EFST_CLIENT_ONLY_EQUIP_ARROW, 1, INFINITE_TICK, 0, 0, 0);
  3560. #endif
  3561. PACKET_ZC_EQUIP_ARROW packet{};
  3562. packet.packetType = HEADER_ZC_EQUIP_ARROW;
  3563. // Inventory index of the arrow
  3564. packet.index = client_index( sd.equip_index[EQI_AMMO] );
  3565. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3566. }
  3567. /// Ammunition action message.
  3568. /// 013b <type>.W (ZC_ACTION_FAILURE)
  3569. /// type:
  3570. /// 0 = MsgStringTable[242]="Please equip the proper ammunition first."
  3571. /// 1 = MsgStringTable[243]="You can't Attack or use Skills because your Weight Limit has been exceeded."
  3572. /// 2 = MsgStringTable[244]="You can't use Skills because Weight Limit has been exceeded."
  3573. /// 3 = assassin, baby_assassin, assassin_cross => MsgStringTable[1040]="You have equipped throwing daggers."
  3574. /// gunslinger => MsgStringTable[1175]="Bullets have been equipped."
  3575. /// NOT ninja => MsgStringTable[245]="Ammunition has been equipped."
  3576. void clif_arrow_fail( map_session_data& sd, e_action_failure type ) {
  3577. PACKET_ZC_ACTION_FAILURE packet{};
  3578. packet.packetType = HEADER_ZC_ACTION_FAILURE;
  3579. packet.type = static_cast<decltype(packet.type)>(type);
  3580. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3581. }
  3582. /// Presents a list of items, that can be processed by Arrow Crafting (ZC_MAKINGARROW_LIST).
  3583. /// 01ad <packet len>.W { <name id>.W }*
  3584. void clif_arrow_create_list( map_session_data& sd ){
  3585. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  3586. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  3587. p->packetLength = sizeof( *p );
  3588. int count = 0;
  3589. for (const auto &it : skill_arrow_db) {
  3590. t_itemid nameid = it.second->nameid;
  3591. if( !item_db.exists( nameid ) ){
  3592. continue;
  3593. }
  3594. int index = pc_search_inventory( &sd, nameid );
  3595. if( index < 0 ){
  3596. continue;
  3597. }
  3598. if( sd.inventory.u.items_inventory[index].equip ){
  3599. continue;
  3600. }
  3601. if( !sd.inventory.u.items_inventory[index].identify ){
  3602. continue;
  3603. }
  3604. p->items[count].itemId = client_nameid( nameid );
  3605. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  3606. count++;
  3607. }
  3608. clif_send( p, p->packetLength, &sd.bl, SELF );
  3609. if( count > 0 ){
  3610. sd.menuskill_id = AC_MAKINGARROW;
  3611. sd.menuskill_val = count;
  3612. }
  3613. }
  3614. /// Notifies the client, about the result of an status change request.
  3615. /// 00bc <status id>.W <result>.B <value>.B (ZC_STATUS_CHANGE_ACK)
  3616. /// status id:
  3617. /// SP_STR ~ SP_LUK and SP_POW ~ SP_CRT
  3618. /// result:
  3619. /// 0 = failure
  3620. /// 1 = success
  3621. void clif_statusupack( map_session_data& sd, int32 type, bool success, int32 val ) {
  3622. PACKET_ZC_STATUS_CHANGE_ACK packet{};
  3623. packet.packetType = HEADER_ZC_STATUS_CHANGE_ACK;
  3624. packet.sp = static_cast<decltype(packet.sp)>(type);
  3625. packet.ok = success;
  3626. packet.value = cap_value(val, 0, UINT8_MAX);
  3627. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3628. }
  3629. /// Notifies the client about the result of a request to equip an item.
  3630. /// 00aa <index>.W <equip location>.W <result>.B (ZC_REQ_WEAR_EQUIP_ACK)
  3631. /// 00aa <index>.W <equip location>.W <view id>.W <result>.B (PACKETVER >= 20100629)
  3632. /// 08d0 <index>.W <equip location>.W <view id>.W <result>.B (ZC_REQ_WEAR_EQUIP_ACK2)
  3633. /// 0999 <index>.W <equip location>.L <view id>.W <result>.B (ZC_ACK_WEAR_EQUIP_V5)
  3634. /// @ok: //inversed forv2 v5
  3635. /// 0 = failure
  3636. /// 1 = success
  3637. /// 2 = failure due to low level
  3638. void clif_equipitemack( map_session_data& sd, uint8 flag, int index, int pos ){
  3639. PACKET_ZC_REQ_WEAR_EQUIP_ACK p = {};
  3640. p.PacketType = HEADER_ZC_REQ_WEAR_EQUIP_ACK;
  3641. p.index = client_index( index );
  3642. p.wearLocation = pos;
  3643. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20100629
  3644. if( flag == ITEM_EQUIP_ACK_OK && sd.inventory_data[index]->equip&EQP_VISIBLE ){
  3645. p.wItemSpriteNumber = sd.inventory_data[index]->look;
  3646. }else{
  3647. p.wItemSpriteNumber = 0;
  3648. }
  3649. #endif
  3650. p.result = flag;
  3651. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  3652. }
  3653. /// Notifies the client about the result of a request to take off an item.
  3654. /// 00ac <index>.W <equip location>.W <result>.B (ZC_REQ_TAKEOFF_EQUIP_ACK)
  3655. /// 08d1 <index>.W <equip location>.W <result>.B (ZC_REQ_TAKEOFF_EQUIP_ACK2)
  3656. /// 099a <index>.W <equip location>.L <result>.B (ZC_ACK_TAKEOFF_EQUIP_V5)
  3657. /// @success : //inversed for v2 v5
  3658. /// 0 = failure
  3659. /// 1 = success
  3660. void clif_unequipitemack( map_session_data& sd, uint16 server_index, int32 pos, bool success ){
  3661. #if PACKETVER >= 20110824
  3662. success = !success;
  3663. #endif
  3664. PACKET_ZC_REQ_TAKEOFF_EQUIP_ACK p{};
  3665. p.packetType = HEADER_ZC_REQ_TAKEOFF_EQUIP_ACK;
  3666. p.index = client_index(server_index);
  3667. p.wearLocation = static_cast<decltype(p.wearLocation)>(pos);
  3668. p.flag = success;
  3669. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3670. }
  3671. /// Notifies clients in the area about an special/visual effect.
  3672. /// 019b <id>.L <effect id>.L (ZC_NOTIFY_EFFECT)
  3673. /// effect id:
  3674. /// 0 = base level up
  3675. /// 1 = job level up
  3676. /// 2 = refine failure
  3677. /// 3 = refine success
  3678. /// 4 = game over
  3679. /// 5 = pharmacy success
  3680. /// 6 = pharmacy failure
  3681. /// 7 = base level up (super novice)
  3682. /// 8 = job level up (super novice)
  3683. /// 9 = base level up (taekwon)
  3684. void clif_misceffect( block_list& bl, e_notify_effect type ){
  3685. PACKET_ZC_NOTIFY_EFFECT packet{};
  3686. packet.packetType = HEADER_ZC_NOTIFY_EFFECT;
  3687. packet.aid = bl.id;
  3688. packet.effectId = static_cast<decltype(packet.effectId)>(type);
  3689. clif_send( &packet, sizeof( packet ), &bl, AREA );
  3690. }
  3691. /// Notifies clients in the area of a state change.
  3692. /// 0119 <id>.L <body state>.W <health state>.W <effect state>.W <pk mode>.B (ZC_STATE_CHANGE)
  3693. /// 0229 <id>.L <body state>.W <health state>.W <effect state>.L <pk mode>.B (ZC_STATE_CHANGE3)
  3694. void clif_changeoption_target( struct block_list* bl, struct block_list* target ){
  3695. nullpo_retv( bl );
  3696. status_change* sc = status_get_sc( bl );
  3697. //How can an option change if there's no sc?
  3698. if( sc == nullptr ){
  3699. return;
  3700. }
  3701. map_session_data* sd = BL_CAST( BL_PC, bl );
  3702. PACKET_ZC_STATE_CHANGE p = {};
  3703. p.packetType = HEADER_ZC_STATE_CHANGE;
  3704. p.AID = bl->id;
  3705. p.bodyState = sc->opt1;
  3706. p.healthState = sc->opt2;
  3707. p.effectState = sc->option;
  3708. p.isPKModeON = sd ? sd->status.karma : false;
  3709. if( target == nullptr ){
  3710. if( disguised( bl ) ){
  3711. clif_send( &p, sizeof( p ), bl, AREA_WOS );
  3712. p.AID = disguised_bl_id( p.AID );
  3713. clif_send( &p, sizeof( p ), bl, SELF );
  3714. p.AID = disguised_bl_id( p.AID );
  3715. p.effectState = OPTION_INVISIBLE;
  3716. clif_send( &p, sizeof( p ), bl, SELF );
  3717. }else{
  3718. clif_send( &p, sizeof( p ), bl, AREA );
  3719. }
  3720. //Whenever we send "changeoption" to the client, the provoke icon is lost
  3721. //There is probably an option for the provoke icon, but as we don't know it, we have to do this for now
  3722. if( sc->getSCE(SC_PROVOKE) ){
  3723. const struct TimerData *td = get_timer( sc->getSCE(SC_PROVOKE)->timer );
  3724. clif_status_change( bl, status_db.getIcon(SC_PROVOKE), 1, ( !td ? INFINITE_TICK : DIFF_TICK( td->tick, gettick() ) ), 0, 0, 0 );
  3725. }
  3726. }else{
  3727. if( disguised( bl ) ){
  3728. p.AID = disguised_bl_id( p.AID );
  3729. clif_send( &p, sizeof( p ), target, SELF );
  3730. p.AID = disguised_bl_id( p.AID );
  3731. p.effectState = OPTION_INVISIBLE;
  3732. clif_send( &p, sizeof( p ), target, SELF );
  3733. }else{
  3734. clif_send( &p, sizeof( p ), target, SELF );
  3735. }
  3736. }
  3737. }
  3738. /// Displays status change effects on NPCs/monsters.
  3739. /// 028a <id>.L <effect state>.L <level>.L <showEFST>.L (ZC_NPC_SHOWEFST_UPDATE)
  3740. void clif_changeoption2( block_list& bl ){
  3741. status_change *sc = status_get_sc(&bl);
  3742. if (!sc)
  3743. return; //How can an option change if there's no sc?
  3744. PACKET_ZC_NPC_SHOWEFST_UPDATE packet{};
  3745. packet.packetType = HEADER_ZC_NPC_SHOWEFST_UPDATE;
  3746. packet.gid = bl.id;
  3747. packet.effectState = sc->option;
  3748. packet.level = clif_setlevel(&bl);
  3749. packet.showEFST = sc->opt3;
  3750. if (disguised(&bl)) {
  3751. clif_send( &packet, sizeof( packet ), &bl, AREA_WOS );
  3752. packet.gid = disguised_bl_id( bl.id );
  3753. clif_send( &packet, sizeof( packet ), &bl, SELF );
  3754. packet.gid = bl.id;
  3755. packet.effectState = OPTION_INVISIBLE;
  3756. clif_send( &packet, sizeof( packet ), &bl, SELF );
  3757. } else {
  3758. clif_send( &packet, sizeof( packet ), &bl, AREA );
  3759. }
  3760. }
  3761. /// Notifies the client about the result of an item use request.
  3762. /// 00a8 <index>.W <amount>.W <result>.B (ZC_USE_ITEM_ACK)
  3763. /// 01c8 <index>.W <name id>.W <id>.L <amount>.W <result>.B (ZC_USE_ITEM_ACK2)
  3764. void clif_useitemack( map_session_data *sd, int index, int amount, bool ok ){
  3765. nullpo_retv( sd );
  3766. int fd = sd->fd;
  3767. if( !session_isActive( fd ) ){
  3768. return;
  3769. }
  3770. if( index < 0 || index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0 || sd->inventory_data[index] == nullptr ){
  3771. return;
  3772. }
  3773. PACKET_ZC_USE_ITEM_ACK p = {};
  3774. p.packetType = useItemAckType;
  3775. p.index = index + 2;
  3776. #if PACKETVER > 3
  3777. p.itemId = client_nameid( sd->inventory.u.items_inventory[index].nameid );
  3778. p.AID = sd->bl.id;
  3779. #endif
  3780. p.amount = amount;
  3781. p.result = ok;
  3782. if( !ok ){
  3783. clif_send( &p, sizeof(p), &sd->bl, SELF );
  3784. }else{
  3785. clif_send( &p, sizeof(p), &sd->bl, AREA );
  3786. }
  3787. }
  3788. /// Inform client whether chatroom creation was successful or not.
  3789. /// 00d6 <flag>.B (ZC_ACK_CREATE_CHATROOM)
  3790. /// flag:
  3791. /// 0 = Room has been successfully created (opens chat room)
  3792. /// 1 = Room limit exceeded
  3793. /// 2 = Same room already exists
  3794. void clif_createchat( map_session_data& sd, e_create_chatroom flag ){
  3795. PACKET_ZC_ACK_CREATE_CHATROOM packet{};
  3796. packet.packetType = HEADER_ZC_ACK_CREATE_CHATROOM;
  3797. packet.flag = static_cast<decltype(packet.flag)>(flag);
  3798. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3799. }
  3800. /// Display a chat above the owner (ZC_ROOM_NEWENTRY).
  3801. /// 00d7 <packet len>.W <owner id>.L <char id>.L <limit>.W <users>.W <type>.B <title>.?B
  3802. /// type:
  3803. /// 0 = private (password protected)
  3804. /// 1 = public
  3805. /// 2 = arena (npc waiting room)
  3806. /// 3 = PK zone (non-clickable)
  3807. void clif_dispchat(struct chat_data* cd, int fd)
  3808. {
  3809. unsigned char buf[128];
  3810. uint8 type;
  3811. if( cd == nullptr || cd->owner == nullptr )
  3812. return;
  3813. type = (cd->owner->type == BL_PC ) ? (cd->pub) ? 1 : 0
  3814. : (cd->owner->type == BL_NPC) ? (cd->limit) ? 2 : 3
  3815. : 1;
  3816. WBUFW(buf, 0) = 0xd7;
  3817. WBUFW(buf, 2) = (uint16)(17 + strlen(cd->title));
  3818. WBUFL(buf, 4) = cd->owner->id;
  3819. WBUFL(buf, 8) = cd->bl.id;
  3820. WBUFW(buf,12) = cd->limit;
  3821. WBUFW(buf,14) = (cd->owner->type == BL_NPC) ? cd->users+1 : cd->users;
  3822. WBUFB(buf,16) = type;
  3823. memcpy(WBUFCP(buf,17), cd->title, strlen(cd->title)); // not zero-terminated
  3824. if( session_isActive(fd) ) {
  3825. WFIFOHEAD(fd,WBUFW(buf,2));
  3826. memcpy(WFIFOP(fd,0),buf,WBUFW(buf,2));
  3827. WFIFOSET(fd,WBUFW(buf,2));
  3828. } else {
  3829. clif_send(buf,WBUFW(buf,2),cd->owner,AREA_WOSC);
  3830. }
  3831. }
  3832. /// Chatroom properties adjustment (ZC_CHANGE_CHATROOM).
  3833. /// 00df <packet len>.W <owner id>.L <chat id>.L <limit>.W <users>.W <type>.B <title>.?B
  3834. /// type:
  3835. /// 0 = private (password protected)
  3836. /// 1 = public
  3837. /// 2 = arena (npc waiting room)
  3838. /// 3 = PK zone (non-clickable)
  3839. void clif_changechatstatus(chat_data& cd) {
  3840. if(cd.usersd[0] == nullptr )
  3841. return;
  3842. enum e_chat_flags:uint8 {
  3843. CHAT_PRIVATE = 0,
  3844. CHAT_PUBLIC,
  3845. CHAT_ARENA,
  3846. CHAT_PK
  3847. };
  3848. PACKET_ZC_CHANGE_CHATROOM* p = reinterpret_cast<PACKET_ZC_CHANGE_CHATROOM*>( packet_buffer );
  3849. p->packetType = HEADER_ZC_CHANGE_CHATROOM;
  3850. p->packetSize = sizeof(*p);
  3851. p->ownerId = cd.owner->id;
  3852. p->chatId = cd.bl.id;
  3853. p->limit = cd.limit;
  3854. p->users = cd.users;
  3855. // not zero-terminated
  3856. size_t max = safestrnlen( cd.title, CHATROOM_TITLE_SIZE );
  3857. strncpy( p->title, cd.title, max );
  3858. p->packetSize += static_cast<decltype(p->packetSize)>( max );
  3859. if(cd.owner->type == BL_NPC){
  3860. // NPC itself counts as additional chat user
  3861. p->users++;
  3862. if(cd.limit)
  3863. p->flag = CHAT_ARENA;
  3864. else
  3865. p->flag = CHAT_PK;
  3866. }else if(cd.owner->type == BL_PC && cd.pub == false){
  3867. p->flag = CHAT_PRIVATE;
  3868. }else{
  3869. p->flag = CHAT_PUBLIC;
  3870. }
  3871. clif_send(p,p->packetSize,cd.owner,CHAT);
  3872. }
  3873. /// Removes the chatroom
  3874. /// 00d8 <chat id>.L (ZC_DESTROY_ROOM)
  3875. void clif_clearchat(chat_data &cd){
  3876. PACKET_ZC_DESTROY_ROOM p{};
  3877. p.packetType = HEADER_ZC_DESTROY_ROOM;
  3878. p.chatId = cd.bl.id;
  3879. clif_send(&p,sizeof(p),cd.owner,AREA_WOSC);
  3880. }
  3881. /// Displays messages regarding join chat failures.
  3882. /// 00da <result>.B (ZC_REFUSE_ENTER_ROOM)
  3883. /// result:
  3884. /// 0 = room full
  3885. /// 1 = wrong password
  3886. /// 2 = kicked
  3887. /// 3 = success (no message)
  3888. /// 4 = no enough zeny
  3889. /// 5 = too low level
  3890. /// 6 = too high level
  3891. /// 7 = unsuitable job class
  3892. void clif_joinchatfail( map_session_data& sd, e_refuse_enter_room result ){
  3893. PACKET_ZC_REFUSE_ENTER_ROOM packet{};
  3894. packet.packetType = HEADER_ZC_REFUSE_ENTER_ROOM;
  3895. packet.result = static_cast<decltype(packet.result)>(result);
  3896. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3897. }
  3898. /// Notifies the client about entering a chatroom (ZC_ENTER_ROOM).
  3899. /// 00db <packet len>.W <chat id>.L { <role>.L <name>.24B }*
  3900. /// role:
  3901. /// 0 = owner (menu)
  3902. /// 1 = normal
  3903. void clif_joinchatok(map_session_data& sd, chat_data& cd){
  3904. PACKET_ZC_ENTER_ROOM* p = reinterpret_cast<PACKET_ZC_ENTER_ROOM*>( packet_buffer );
  3905. p->packetType = HEADER_ZC_ENTER_ROOM;
  3906. p->packetSize = sizeof(*p);
  3907. p->chatId = cd.bl.id;
  3908. if(cd.owner->type == BL_NPC){
  3909. PACKET_ZC_ENTER_ROOM_sub& owner = p->members[0];
  3910. owner.flag = 0;
  3911. safestrncpy(owner.name, reinterpret_cast<npc_data*>(cd.owner)->name, sizeof(owner.name));
  3912. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( owner ) );
  3913. for (size_t i = 0; i < cd.users; i++) {
  3914. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i + 1];
  3915. member.flag = 1;
  3916. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3917. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3918. }
  3919. }else{
  3920. for (size_t i = 0; i < cd.users; i++) {
  3921. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i];
  3922. member.flag = i > 0;
  3923. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3924. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3925. }
  3926. }
  3927. clif_send(p,p->packetSize,&sd.bl,SELF);
  3928. }
  3929. /// Notifies clients in a chat about a new member (ZC_MEMBER_NEWENTRY).
  3930. /// 00dc <users>.W <name>.24B
  3931. void clif_addchat(struct chat_data* cd,map_session_data *sd)
  3932. {
  3933. unsigned char buf[29];
  3934. nullpo_retv(sd);
  3935. nullpo_retv(cd);
  3936. WBUFW(buf, 0) = 0xdc;
  3937. WBUFW(buf, 2) = cd->users;
  3938. safestrncpy(WBUFCP(buf, 4),sd->status.name,NAME_LENGTH);
  3939. clif_send(buf,packet_len(0xdc),&sd->bl,CHAT_WOS);
  3940. }
  3941. /// Announce the new owner (ZC_ROLE_CHANGE).
  3942. /// 00e1 <role>.L <nick>.24B
  3943. /// role:
  3944. /// 0 = owner (menu)
  3945. /// 1 = normal
  3946. void clif_changechatowner(struct chat_data* cd, map_session_data* sd)
  3947. {
  3948. unsigned char buf[64];
  3949. nullpo_retv(sd);
  3950. nullpo_retv(cd);
  3951. WBUFW(buf, 0) = 0xe1;
  3952. WBUFL(buf, 2) = 1;
  3953. safestrncpy(WBUFCP(buf,6),cd->usersd[0]->status.name,NAME_LENGTH);
  3954. WBUFW(buf,30) = 0xe1;
  3955. WBUFL(buf,32) = 0;
  3956. safestrncpy(WBUFCP(buf,36),sd->status.name,NAME_LENGTH);
  3957. clif_send(buf,packet_len(0xe1)*2,&sd->bl,CHAT);
  3958. }
  3959. /// Notify about user leaving the chatroom (ZC_MEMBER_EXIT).
  3960. /// 00dd <users>.W <nick>.24B <flag>.B
  3961. /// flag:
  3962. /// 0 = left
  3963. /// 1 = kicked
  3964. void clif_leavechat(struct chat_data* cd, map_session_data* sd, bool flag)
  3965. {
  3966. unsigned char buf[32];
  3967. nullpo_retv(sd);
  3968. nullpo_retv(cd);
  3969. WBUFW(buf, 0) = 0xdd;
  3970. WBUFW(buf, 2) = cd->users-1;
  3971. safestrncpy(WBUFCP(buf,4),sd->status.name,NAME_LENGTH);
  3972. WBUFB(buf,28) = flag;
  3973. clif_send(buf,packet_len(0xdd),&sd->bl,CHAT);
  3974. }
  3975. /// Opens a trade request window from char 'name'.
  3976. /// 00e5 <nick>.24B (ZC_REQ_EXCHANGE_ITEM)
  3977. /// 01f4 <nick>.24B <targetid>.L <baselvl>.W (ZC_REQ_EXCHANGE_ITEM2)
  3978. void clif_traderequest(map_session_data& sd, const char* name){
  3979. PACKET_ZC_REQ_EXCHANGE_ITEM p{};
  3980. p.packetType = HEADER_ZC_REQ_EXCHANGE_ITEM;
  3981. safestrncpy(p.requesterName, name, sizeof(p.requesterName));
  3982. #if PACKETVER > 6
  3983. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  3984. p.targetLv = sd.trade_partner.lv;
  3985. #endif
  3986. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3987. }
  3988. /// Reply to a trade-request.
  3989. /// 00e7 <result>.B (ZC_ACK_EXCHANGE_ITEM)
  3990. /// 01f5 <result>.B <targetid>.L <baselvl>.W (ZC_ACK_EXCHANGE_ITEM2)
  3991. /// result:
  3992. /// 0 = Char is too far
  3993. /// 1 = Character does not exist
  3994. /// 2 = Trade failed
  3995. /// 3 = Accept
  3996. /// 4 = Cancel
  3997. /// 5 = Busy
  3998. void clif_traderesponse( map_session_data& sd, e_ack_trade_response result ){
  3999. PACKET_ZC_ACK_EXCHANGE_ITEM p{};
  4000. p.packetType = HEADER_ZC_ACK_EXCHANGE_ITEM;
  4001. p.result = static_cast<decltype(p.result)>( result );
  4002. #if PACKETVER > 6
  4003. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  4004. p.targetLv = sd.trade_partner.lv;
  4005. #endif
  4006. clif_send(&p,sizeof(p),&sd.bl,SELF);
  4007. }
  4008. /// Notifies the client about an item from other player in current trade.
  4009. /// 00e9 <amount>.L <nameid>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_EXCHANGE_ITEM)
  4010. /// 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)
  4011. /// 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)
  4012. void clif_tradeadditem( map_session_data* sd, map_session_data* tsd, int index, int amount ){
  4013. nullpo_retv( sd );
  4014. nullpo_retv( tsd );
  4015. int fd = tsd->fd;
  4016. if( !session_isActive( fd ) ){
  4017. return;
  4018. }
  4019. PACKET_ZC_ADD_EXCHANGE_ITEM p = {};
  4020. if( index ){
  4021. index = server_index( index );
  4022. if( index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0 || sd->inventory_data[index] == nullptr ){
  4023. return;
  4024. }
  4025. p.itemId = client_nameid( sd->inventory.u.items_inventory[index].nameid );
  4026. #if PACKETVER >= 20100223
  4027. p.itemType = sd->inventory_data[index]->type;
  4028. #endif
  4029. p.identified = sd->inventory.u.items_inventory[index].identify;
  4030. p.damaged = sd->inventory.u.items_inventory[index].attribute;
  4031. p.refine = sd->inventory.u.items_inventory[index].refine;
  4032. clif_addcards( &p.slot, &sd->inventory.u.items_inventory[index] );
  4033. #if PACKETVER >= 20150226
  4034. clif_add_random_options( p.option_data, sd->inventory.u.items_inventory[index] );
  4035. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  4036. p.location = pc_equippoint_sub( sd, sd->inventory_data[index] );
  4037. p.look = sd->inventory_data[index]->look;
  4038. p.grade = sd->inventory.u.items_inventory[index].enchantgrade;
  4039. #endif
  4040. #endif
  4041. }else{
  4042. p = {};
  4043. }
  4044. p.packetType = HEADER_ZC_ADD_EXCHANGE_ITEM;
  4045. p.amount = amount;
  4046. clif_send( &p, sizeof( p ), &tsd->bl, SELF );
  4047. }
  4048. /// Notifies the client about the result of request to add an item to the current trade (ZC_ACK_ADD_EXCHANGE_ITEM).
  4049. /// 00ea <index>.W <result>.B
  4050. /// result:
  4051. /// 0 = success
  4052. /// 1 = overweight
  4053. /// 2 = trade canceled
  4054. void clif_tradeitemok(map_session_data& sd, int index, e_exitem_add_result result)
  4055. {
  4056. PACKET_ZC_ACK_ADD_EXCHANGE_ITEM p = {};
  4057. p.packetType = HEADER_ZC_ACK_ADD_EXCHANGE_ITEM;
  4058. p.index = client_index(index);
  4059. p.result = static_cast<uint8>(result);
  4060. clif_send(&p, sizeof(p), &sd.bl, SELF);
  4061. }
  4062. /// Notifies the client about finishing one side of the current trade.
  4063. /// 00ec <who>.B (ZC_CONCLUDE_EXCHANGE_ITEM)
  4064. /// who:
  4065. /// 0 = self
  4066. /// 1 = other player
  4067. void clif_tradedeal_lock( map_session_data& sd, bool who ){
  4068. PACKET_ZC_CONCLUDE_EXCHANGE_ITEM packet{};
  4069. packet.packetType = HEADER_ZC_CONCLUDE_EXCHANGE_ITEM;
  4070. packet.who = who;
  4071. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4072. }
  4073. /// Notifies the client about the trade being canceled.
  4074. /// 00ee (ZC_CANCEL_EXCHANGE_ITEM)
  4075. void clif_tradecancelled( map_session_data& sd ){
  4076. PACKET_ZC_CANCEL_EXCHANGE_ITEM packet{};
  4077. packet.packetType = HEADER_ZC_CANCEL_EXCHANGE_ITEM;
  4078. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4079. }
  4080. /// Result of a trade.
  4081. /// 00f0 <result>.B (ZC_EXEC_EXCHANGE_ITEM)
  4082. /// result:
  4083. /// 0 = success
  4084. /// 1 = failure
  4085. void clif_tradecompleted( map_session_data& sd ){
  4086. PACKET_ZC_EXEC_EXCHANGE_ITEM packet{};
  4087. packet.packetType = HEADER_ZC_EXEC_EXCHANGE_ITEM;
  4088. packet.result = 0;
  4089. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4090. }
  4091. /// Resets the trade window on the send side.
  4092. /// 00f1 (ZC_EXCHANGEITEM_UNDO)
  4093. /// NOTE: Unknown purpose. Items are not removed until the window is
  4094. /// refreshed (ex. by putting another item in there).
  4095. void clif_tradeundo( map_session_data& sd ){
  4096. PACKET_ZC_EXCHANGEITEM_UNDO packet{};
  4097. packet.packetType = HEADER_ZC_EXCHANGEITEM_UNDO;
  4098. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4099. }
  4100. /// Updates storage total amount.
  4101. /// 00f2 <current count>.W <max count>.W (ZC_NOTIFY_STOREITEM_COUNTINFO)
  4102. void clif_updatestorageamount( map_session_data& sd, uint16 amount, uint16 max_amount ){
  4103. PACKET_ZC_NOTIFY_STOREITEM_COUNTINFO packet{};
  4104. packet.packetType = HEADER_ZC_NOTIFY_STOREITEM_COUNTINFO;
  4105. packet.amount = amount;
  4106. packet.max_amount = max_amount;
  4107. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4108. }
  4109. /// Notifies the client of an item being added to the storage.
  4110. /// 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)
  4111. /// 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)
  4112. /// 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)
  4113. void clif_storageitemadded( map_session_data* sd, struct item* i, int index, int amount ){
  4114. nullpo_retv( sd );
  4115. nullpo_retv( i );
  4116. int fd = sd->fd;
  4117. if( !session_isActive( fd ) ){
  4118. return;
  4119. }
  4120. PACKET_ZC_ADD_ITEM_TO_STORE p = {};
  4121. p.packetType = HEADER_ZC_ADD_ITEM_TO_STORE; // Storage item added
  4122. p.index = client_storage_index( index ); // index
  4123. p.amount = amount; // amount
  4124. p.itemId = client_nameid( i->nameid ); // id
  4125. #if PACKETVER >= 5
  4126. p.itemType = itemtype( i->nameid ); //type
  4127. #endif
  4128. p.identified = i->identify; //identify flag
  4129. p.damaged = i->attribute; // attribute
  4130. p.refine = i->refine; //refine
  4131. clif_addcards( &p.slot, i );
  4132. #if PACKETVER_MAIN_NUM >= 20140813 || PACKETVER_RE_NUM >= 20140402 || defined(PACKETVER_ZERO)
  4133. clif_add_random_options( p.option_data, *i );
  4134. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20221024
  4135. p.grade = i->enchantgrade;
  4136. #endif
  4137. #endif
  4138. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  4139. }
  4140. /// Notifies the client of an item being deleted from the storage.
  4141. /// 00f6 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_STORE)
  4142. void clif_storageitemremoved( map_session_data& sd, uint16 index, uint32 amount ){
  4143. PACKET_ZC_DELETE_ITEM_FROM_STORE packet{};
  4144. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_STORE;
  4145. packet.index = client_storage_index(index);
  4146. packet.amount = amount;
  4147. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4148. }
  4149. /// Closes storage.
  4150. /// 00f8 (ZC_CLOSE_STORE)
  4151. void clif_storageclose( map_session_data& sd ){
  4152. PACKET_ZC_CLOSE_STORE packet{};
  4153. packet.packetType = HEADER_ZC_CLOSE_STORE;
  4154. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4155. }
  4156. /// Notifies clients in an area of a player's souls.
  4157. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  4158. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  4159. /// 0b73 <id>.L <amount>.W (ZC_SOULENERGY)
  4160. void clif_soulball( map_session_data *sd, struct block_list* target, enum send_target send_target ){
  4161. #if PACKETVER_MAIN_NUM >= 20200414 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20200506
  4162. PACKET_ZC_SOULENERGY p = {};
  4163. p.PacketType = HEADER_ZC_SOULENERGY;
  4164. #else
  4165. PACKET_ZC_SPIRITS p = {};
  4166. p.PacketType = HEADER_ZC_SPIRITS;
  4167. #endif
  4168. p.AID = sd->bl.id;
  4169. p.num = sd->soulball;
  4170. clif_send( &p, sizeof( p ), target == nullptr ? &sd->bl : target, send_target );
  4171. }
  4172. /*==========================================
  4173. * Server tells 'sd' player client the abouts of 'dstsd' player
  4174. *------------------------------------------*/
  4175. static void clif_getareachar_pc(map_session_data* sd,map_session_data* dstsd)
  4176. {
  4177. struct block_list *d_bl;
  4178. int i;
  4179. if( dstsd->chatID ) {
  4180. struct chat_data *cd = nullptr;
  4181. if( (cd = (struct chat_data*)map_id2bl(dstsd->chatID)) && cd->usersd[0]==dstsd)
  4182. clif_dispchat(cd,sd->fd);
  4183. } else if( dstsd->state.vending )
  4184. clif_showvendingboard( *dstsd, SELF, &sd->bl );
  4185. else if( dstsd->state.buyingstore )
  4186. clif_buyingstore_entry( *dstsd, &sd->bl );
  4187. if(dstsd->spiritball > 0)
  4188. clif_spiritball( &dstsd->bl, &sd->bl, SELF );
  4189. clif_millenniumshield_single( *dstsd, *sd );
  4190. clif_spiritcharm_single( *dstsd, *sd );
  4191. if (dstsd->soulball > 0)
  4192. clif_soulball( dstsd, &sd->bl, SELF );
  4193. if (dstsd->servantball > 0)
  4194. clif_servantball( *dstsd, &sd->bl, SELF );
  4195. if (dstsd->abyssball > 0)
  4196. clif_abyssball( *dstsd, &sd->bl, SELF );
  4197. if( (sd->status.party_id && dstsd->status.party_id == sd->status.party_id) || //Party-mate, or hpdisp setting.
  4198. (sd->bg_id && sd->bg_id == dstsd->bg_id) || //BattleGround
  4199. pc_has_permission(sd, PC_PERM_VIEW_HPMETER)
  4200. )
  4201. clif_hpmeter_single( *sd, dstsd->bl.id, dstsd->battle_status.hp, dstsd->battle_status.max_hp );
  4202. // display link (sd - dstsd) to sd
  4203. ARR_FIND( 0, MAX_DEVOTION, i, sd->devotion[i] == dstsd->bl.id );
  4204. if( i < MAX_DEVOTION )
  4205. clif_devotion(&sd->bl, sd);
  4206. // display links (dstsd - devotees) to sd
  4207. ARR_FIND( 0, MAX_DEVOTION, i, dstsd->devotion[i] > 0 );
  4208. if( i < MAX_DEVOTION )
  4209. clif_devotion(&dstsd->bl, sd);
  4210. // display link (dstsd - crusader) to sd
  4211. if( dstsd->sc.getSCE(SC_DEVOTION) && (d_bl = map_id2bl(dstsd->sc.getSCE(SC_DEVOTION)->val1)) != nullptr )
  4212. clif_devotion(d_bl, sd);
  4213. }
  4214. void clif_getareachar_unit( map_session_data* sd,struct block_list *bl ){
  4215. struct unit_data *ud;
  4216. struct view_data *vd;
  4217. bool option = false;
  4218. uint32 option_val = 0;
  4219. vd = status_get_viewdata(bl);
  4220. if (!vd || vd->class_ == JT_INVISIBLE)
  4221. return;
  4222. // Hide NPC from Maya Purple card
  4223. if (clif_npc_mayapurple(bl))
  4224. return;
  4225. if( bl->type == BL_NPC && npc_is_hidden_dynamicnpc( *( (struct npc_data*)bl ), *sd ) ){
  4226. // Do not send anything
  4227. return;
  4228. }
  4229. ud = unit_bl2ud(bl);
  4230. if( ud && ud->walktimer != INVALID_TIMER ){
  4231. clif_set_unit_walking( *bl, sd, *ud, SELF );
  4232. }else{
  4233. clif_set_unit_idle( bl, false, SELF, &sd->bl );
  4234. }
  4235. if (vd->cloth_color)
  4236. clif_refreshlook(&sd->bl,bl->id,LOOK_CLOTHES_COLOR,vd->cloth_color,SELF);
  4237. if (vd->body_style)
  4238. clif_refreshlook(&sd->bl,bl->id,LOOK_BODY2,vd->body_style,SELF);
  4239. switch (bl->type)
  4240. {
  4241. case BL_PC:
  4242. {
  4243. TBL_PC* tsd = (TBL_PC*)bl;
  4244. clif_getareachar_pc(sd, tsd);
  4245. if(tsd->state.size==SZ_BIG) // tiny/big players [Valaris]
  4246. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4247. else if(tsd->state.size==SZ_MEDIUM)
  4248. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4249. if( tsd->bg_id && map_getmapflag(tsd->bl.m, MF_BATTLEGROUND) )
  4250. clif_sendbgemblem_single(sd->fd,tsd);
  4251. if ( tsd->status.robe )
  4252. clif_refreshlook(&sd->bl,bl->id,LOOK_ROBE,tsd->status.robe,SELF);
  4253. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4254. clif_hat_effects( *sd, tsd->bl, SELF );
  4255. }
  4256. break;
  4257. case BL_MER: // Devotion Effects
  4258. if( ((TBL_MER*)bl)->devotion_flag )
  4259. clif_devotion(bl, sd);
  4260. break;
  4261. case BL_NPC:
  4262. {
  4263. TBL_NPC* nd = (TBL_NPC*)bl;
  4264. if( nd->chat_id )
  4265. clif_dispchat((struct chat_data*)map_id2bl(nd->chat_id),sd->fd);
  4266. if( nd->size == SZ_BIG )
  4267. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4268. else if( nd->size == SZ_MEDIUM )
  4269. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4270. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4271. clif_progressbar_npc(nd, sd);
  4272. }
  4273. break;
  4274. case BL_MOB:
  4275. {
  4276. TBL_MOB* md = (TBL_MOB*)bl;
  4277. if(md->special_state.size==SZ_BIG) // tiny/big mobs [Valaris]
  4278. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4279. else if(md->special_state.size==SZ_MEDIUM)
  4280. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4281. #if PACKETVER >= 20120404
  4282. if (battle_config.monster_hp_bars_info && !map_getmapflag(bl->m, MF_HIDEMOBHPBAR)) {
  4283. int i;
  4284. for(i = 0; i < DAMAGELOG_SIZE; i++)// must show hp bar to all char who already hit the mob.
  4285. if( md->dmglog[i].id == sd->status.char_id )
  4286. clif_monster_hp_bar(md, sd->fd);
  4287. }
  4288. #endif
  4289. }
  4290. break;
  4291. case BL_PET:
  4292. if (vd->head_bottom)
  4293. clif_pet_equip(sd, (TBL_PET*)bl); // needed to display pet equip properly
  4294. break;
  4295. }
  4296. }
  4297. //Modifies the type of damage according to status changes [Skotlex]
  4298. //Aegis data specifies that: 4 endure against single hit sources, 9 against multi-hit.
  4299. static enum e_damage_type clif_calc_delay(enum e_damage_type type, int div, int64 damage, int delay)
  4300. {
  4301. return ( delay == 0 && damage > 0 ) ? ( div > 1 ? DMG_MULTI_HIT_ENDURE : DMG_ENDURE ) : type;
  4302. }
  4303. /*==========================================
  4304. * Estimates walk delay based on the damage criteria. [Skotlex]
  4305. *------------------------------------------*/
  4306. static int clif_calc_walkdelay(struct block_list *bl,int delay, char type, int64 damage, int div_)
  4307. {
  4308. if (type == DMG_ENDURE || type == DMG_MULTI_HIT_ENDURE || damage <= 0)
  4309. return 0;
  4310. if (bl->type == BL_PC) {
  4311. if (battle_config.pc_walk_delay_rate != 100)
  4312. delay = delay*battle_config.pc_walk_delay_rate/100;
  4313. } else
  4314. if (battle_config.walk_delay_rate != 100)
  4315. delay = delay*battle_config.walk_delay_rate/100;
  4316. if (div_ > 1) //Multi-hit skills mean higher delays.
  4317. delay += battle_config.multihit_delay*(div_-1);
  4318. return (delay > 0) ? delay:1; //Return 1 to specify there should be no noticeable delay, but you should stop walking.
  4319. }
  4320. /*========================================== [Playtester]
  4321. * Returns hallucination damage the client displays
  4322. *------------------------------------------*/
  4323. static int clif_hallucination_damage()
  4324. {
  4325. int digit = rnd() % 5 + 1;
  4326. switch (digit)
  4327. {
  4328. case 1:
  4329. return (rnd() % 10);
  4330. case 2:
  4331. return (rnd() % 100);
  4332. case 3:
  4333. return (rnd() % 1000);
  4334. case 4:
  4335. return (rnd() % 10000);
  4336. }
  4337. return (rnd() % 32767);
  4338. }
  4339. #define DEFAULT_ANIMATION_SPEED 432
  4340. /// Sends a 'damage' packet (src performs action on dst)
  4341. /// 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)
  4342. /// 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)
  4343. /// 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)
  4344. /// type:
  4345. /// 0 = damage [ damage: total damage, div: amount of hits, damage2: assassin dual-wield damage ]
  4346. /// 1 = pick up item
  4347. /// 2 = sit down
  4348. /// 3 = stand up
  4349. /// 4 = damage (endure)
  4350. /// 5 = (splash?)
  4351. /// 6 = (skill?)
  4352. /// 7 = (repeat damage?)
  4353. /// 8 = multi-hit damage
  4354. /// 9 = multi-hit damage (endure)
  4355. /// 10 = critical hit
  4356. /// 11 = lucky dodge
  4357. /// 12 = (touch skill?)
  4358. /// 13 = multi-hit critical
  4359. 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){
  4360. int damage = (int)cap_value(sdamage,INT_MIN,INT_MAX);
  4361. int damage2 = (int)cap_value(sdamage2,INT_MIN,INT_MAX);
  4362. if (type != DMG_MULTI_HIT_CRITICAL)
  4363. type = clif_calc_delay(type,div,damage+damage2,ddelay);
  4364. status_change *sc = status_get_sc(&dst);
  4365. if(sc && sc->count) {
  4366. if(sc->getSCE(SC_HALLUCINATION)) {
  4367. damage = clif_hallucination_damage();
  4368. if(damage2)
  4369. damage2 = clif_hallucination_damage();
  4370. }
  4371. }
  4372. // Calculate what sdelay to send to the client so it applies damage at the same time as the server
  4373. if (battle_config.synchronize_damage && src.type == BL_MOB) {
  4374. // When a clif_damage packet is sent to the client it will also send "sdelay" (amotion) as value.
  4375. // The client however does not interpret this value as AttackMotion but incorrectly as an inverted
  4376. // animation speed modifier, with 432 standing for 1x animation speed.
  4377. // The client will ignore all values above 432, but lower values will speed up the animation.
  4378. // 216 for example means play the animation at double the speed. 108 is quadruple speed.
  4379. // Each monster has an attack animation and may define the frame in the attack animation on which
  4380. // it displays the damage and makes the target flinch / stop. If the damage frame is undefined,
  4381. // it instead displays the damage / flinch / stop at the beginning of the second to last frame.
  4382. // We define the time after which the damage frame shows at 1x speed as clientamotion.
  4383. uint16 clientamotion = std::max((uint16)1, status_get_clientamotion(&src));
  4384. // Knowing when the damage frame happens in the animation allows us to synchronize the timing
  4385. // between client and server using the formula below.
  4386. sdelay = sdelay * DEFAULT_ANIMATION_SPEED / clientamotion;
  4387. // Hint: If amotion is larger than clientamotion this results in a value above 432 which makes the
  4388. // client display the attack at 1x speed. In this case we need to shorten the delay damage timer
  4389. // on the server to clientamotion ms instead (see battle_delay_damage).
  4390. sdelay = std::min(sdelay, DEFAULT_ANIMATION_SPEED);
  4391. }
  4392. PACKET_ZC_NOTIFY_ACT p{};
  4393. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4394. p.srcID = src.id;
  4395. p.targetID = dst.id;
  4396. p.serverTick = client_tick(tick);
  4397. p.srcSpeed = sdelay;
  4398. p.dmgSpeed = ddelay;
  4399. if (battle_config.hide_woe_damage && map_flag_gvg(src.m)) {
  4400. p.damage = damage ? div : 0;
  4401. p.damage2 = damage2 ? div : 0;
  4402. } else {
  4403. p.damage = static_cast<decltype(p.damage)>( std::min( damage, static_cast<decltype(damage)>( std::numeric_limits<decltype(p.damage)>::max() ) ) );
  4404. p.damage2 = static_cast<decltype(p.damage2)>( std::min( damage2, static_cast<decltype(damage2)>( std::numeric_limits<decltype(p.damage2)>::max() ) ) );
  4405. }
  4406. #if PACKETVER >= 20131223
  4407. p.isSPDamage = (spdamage) ? 1 : 0; // IsSPDamage - Displays blue digits.
  4408. #endif
  4409. p.div = div;
  4410. p.type = type;
  4411. if(disguised(&dst)) {
  4412. clif_send( &p, sizeof(p), &dst, AREA_WOS);
  4413. p.targetID = disguised_bl_id( dst.id );
  4414. clif_send( &p, sizeof(p), &dst, SELF);
  4415. } else
  4416. clif_send(&p, sizeof(p), &dst, AREA);
  4417. if(disguised(&src)) {
  4418. p.srcID = disguised_bl_id( src.id );
  4419. if(damage > 0)
  4420. p.damage = -1;
  4421. if(damage2 > 0)
  4422. p.damage2 = -1;
  4423. clif_send( &p, sizeof(p), &src, SELF);
  4424. }
  4425. if(&src == &dst)
  4426. unit_setdir(&src, unit_getdir(&src));
  4427. // In case this assignment is bypassed by DMG_MULTI_HIT_CRITICAL
  4428. type = clif_calc_delay(type, div, damage + damage2, ddelay);
  4429. //Return adjusted can't walk delay for further processing.
  4430. return clif_calc_walkdelay(&dst, ddelay, type, damage+damage2, div);
  4431. }
  4432. /*==========================================
  4433. * src picks up dst
  4434. *------------------------------------------*/
  4435. void clif_takeitem(block_list& src, block_list& dst){
  4436. PACKET_ZC_NOTIFY_ACT p{};
  4437. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4438. p.srcID = src.id;
  4439. p.targetID = dst.id;
  4440. p.type = DMG_PICKUP_ITEM;
  4441. clif_send(&p, sizeof(p), &src, AREA);
  4442. }
  4443. /*==========================================
  4444. * inform clients in area that `bl` is sitting
  4445. *------------------------------------------*/
  4446. void clif_sitting(block_list& bl){
  4447. PACKET_ZC_NOTIFY_ACT p{};
  4448. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4449. p.srcID = bl.id;
  4450. p.type = DMG_SIT_DOWN;
  4451. clif_send(&p, sizeof(p), &bl, AREA);
  4452. if(disguised(&bl)) {
  4453. p.srcID = disguised_bl_id( bl.id );
  4454. clif_send(&p, sizeof(p), &bl, SELF);
  4455. }
  4456. }
  4457. /*==========================================
  4458. * inform clients in area that `bl` is standing
  4459. *------------------------------------------*/
  4460. void clif_standing(block_list& bl){
  4461. PACKET_ZC_NOTIFY_ACT p{};
  4462. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4463. p.srcID = bl.id;
  4464. p.type = DMG_STAND_UP;
  4465. clif_send(&p, sizeof(p), &bl, AREA);
  4466. if(disguised(&bl)) {
  4467. p.srcID = disguised_bl_id( bl.id );
  4468. clif_send(&p, sizeof(p), &bl, SELF);
  4469. }
  4470. }
  4471. /// Inform client(s) about a map-cell change (ZC_UPDATE_MAPINFO).
  4472. /// 0192 <x>.W <y>.W <type>.W <map name>.16B
  4473. void clif_changemapcell(int fd, int16 m, int x, int y, int type, enum send_target target)
  4474. {
  4475. unsigned char buf[32];
  4476. WBUFW(buf,0) = 0x192;
  4477. WBUFW(buf,2) = x;
  4478. WBUFW(buf,4) = y;
  4479. WBUFW(buf,6) = type;
  4480. mapindex_getmapname_ext(map_mapid2mapname(m),WBUFCP(buf,8));
  4481. if( session_isActive(fd) )
  4482. {
  4483. WFIFOHEAD(fd,packet_len(0x192));
  4484. memcpy(WFIFOP(fd,0), buf, packet_len(0x192));
  4485. WFIFOSET(fd,packet_len(0x192));
  4486. }
  4487. else
  4488. {
  4489. struct block_list dummy_bl;
  4490. dummy_bl.type = BL_NUL;
  4491. dummy_bl.x = x;
  4492. dummy_bl.y = y;
  4493. dummy_bl.m = m;
  4494. clif_send(buf,packet_len(0x192),&dummy_bl,target);
  4495. }
  4496. }
  4497. /// Notifies the client about an item on floor (ZC_ITEM_ENTRY).
  4498. /// 009d <id>.L <name id>.W <identified>.B <x>.W <y>.W <amount>.W <subX>.B <subY>.B
  4499. void clif_getareachar_item( map_session_data* sd,struct flooritem_data* fitem ){
  4500. nullpo_retv( sd );
  4501. nullpo_retv( fitem );
  4502. PACKET_ZC_ITEM_ENTRY p = {};
  4503. p.packetType = HEADER_ZC_ITEM_ENTRY;
  4504. p.AID = fitem->bl.id;
  4505. p.itemId = client_nameid( fitem->item.nameid );
  4506. p.identify = fitem->item.identify;
  4507. p.x = fitem->bl.x;
  4508. p.y = fitem->bl.y;
  4509. p.amount = fitem->item.amount;
  4510. p.subX = fitem->subx;
  4511. p.subY = fitem->suby;
  4512. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  4513. }
  4514. /// Notifes client about Graffiti
  4515. /// 01c9 <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B <has msg>.B <msg>.80B (ZC_SKILL_ENTRY2)
  4516. static void clif_graffiti(struct block_list *bl, struct skill_unit *unit, enum send_target target) {
  4517. unsigned char buf[128];
  4518. nullpo_retv(bl);
  4519. nullpo_retv(unit);
  4520. WBUFW(buf, 0) = 0x1c9;
  4521. WBUFL(buf, 2) = unit->bl.id;
  4522. WBUFL(buf, 6) = unit->group->src_id;
  4523. WBUFW(buf,10) = unit->bl.x;
  4524. WBUFW(buf,12) = unit->bl.y;
  4525. WBUFB(buf,14) = unit->group->unit_id;
  4526. WBUFB(buf,15) = 1;
  4527. WBUFB(buf,16) = 1;
  4528. safestrncpy(WBUFCP(buf,17),unit->group->valstr,MESSAGE_SIZE);
  4529. clif_send(buf,packet_len(0x1c9),bl,target);
  4530. }
  4531. /// Notifies the client of a skill unit.
  4532. /// 011f <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B (ZC_SKILL_ENTRY)
  4533. /// 08c7 <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.B <range>.W <visible>.B (ZC_SKILL_ENTRY3)
  4534. /// 099f <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.L <range>.W <visible>.B (ZC_SKILL_ENTRY4)
  4535. /// 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)
  4536. void clif_getareachar_skillunit(struct block_list *bl, struct skill_unit *unit, enum send_target target, bool visible) {
  4537. int header = 0, unit_id = 0, pos = 0, fd = 0, len = -1;
  4538. unsigned char buf[128];
  4539. nullpo_retv(bl);
  4540. nullpo_retv(unit);
  4541. if (bl->type == BL_PC)
  4542. fd = ((TBL_PC*)bl)->fd;
  4543. if (unit->group->state.guildaura)
  4544. return;
  4545. if (unit->group->state.song_dance&0x1 && unit->val2&(1 << UF_ENSEMBLE))
  4546. unit_id = unit->val2&(1 << UF_SONG) ? UNT_DISSONANCE : UNT_UGLYDANCE;
  4547. else if (skill_get_unit_flag(unit->group->skill_id, UF_RANGEDSINGLEUNIT) && !(unit->val2 & (1 << UF_RANGEDSINGLEUNIT)))
  4548. unit_id = UNT_DUMMYSKILL; // Use invisible unit id for other case of rangedsingle unit
  4549. else
  4550. unit_id = unit->group->unit_id;
  4551. if (!visible)
  4552. unit_id = UNT_DUMMYSKILL; // Hack to makes hidden trap really hidden!
  4553. #if PACKETVER >= 3
  4554. if (unit_id == UNT_GRAFFITI) { // Graffiti [Valaris]
  4555. clif_graffiti(bl, unit, target);
  4556. return;
  4557. }
  4558. #endif
  4559. #if PACKETVER <= 20120702
  4560. header = 0x011f;
  4561. //#if PACKETVER < 20110718
  4562. // header = 0x011f;
  4563. //#elif PACKETVER < 20121212
  4564. // header = 0x08c7;
  4565. #elif PACKETVER < 20130731
  4566. header = 0x099f;
  4567. #else
  4568. header = 0x09ca;
  4569. #endif
  4570. len = packet_len(header);
  4571. WBUFW(buf,pos) = header;
  4572. if (header != 0x011f) {
  4573. WBUFW(buf, pos+2) = len;
  4574. pos += 2;
  4575. }
  4576. WBUFL(buf,pos+2) = unit->bl.id;
  4577. WBUFL(buf,pos+6) = unit->group->src_id;
  4578. WBUFW(buf,pos+10) = unit->bl.x;
  4579. WBUFW(buf,pos+12) = unit->bl.y;
  4580. switch (header) {
  4581. case 0x011f:
  4582. WBUFB(buf,pos+14) = unit_id;
  4583. WBUFB(buf,pos+15) = visible;
  4584. break;
  4585. case 0x08c7:
  4586. WBUFB(buf,pos+14) = unit_id;
  4587. WBUFW(buf,pos+15) = unit->range;
  4588. WBUFB(buf,pos+17) = visible;
  4589. break;
  4590. case 0x099f:
  4591. WBUFL(buf,pos+14) = unit_id;
  4592. WBUFW(buf,pos+18) = unit->range;
  4593. WBUFB(buf,pos+20) = visible;
  4594. break;
  4595. case 0x09ca:
  4596. WBUFL(buf,pos+14) = unit_id;
  4597. WBUFB(buf,pos+18) = (unsigned char)unit->range;
  4598. WBUFB(buf,pos+19) = visible;
  4599. WBUFB(buf,pos+20) = (unsigned char)unit->group->skill_lv;
  4600. break;
  4601. }
  4602. clif_send(buf, len, bl, target);
  4603. if (unit->group->skill_id == WZ_ICEWALL)
  4604. clif_changemapcell(fd, unit->bl.m, unit->bl.x, unit->bl.y, 5, SELF);
  4605. }
  4606. /// 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)
  4607. void clif_skill_unit_test(struct block_list *bl, short x, short y, int unit_id, short range, short skill_lv) {
  4608. unsigned char buf[128];
  4609. nullpo_retv(bl);
  4610. WBUFW(buf, 0) = 0x09ca;
  4611. WBUFW(buf, 2) = packet_len(0x09ca);
  4612. WBUFL(buf, 4) = 1000;
  4613. WBUFL(buf, 8) = 2000;
  4614. WBUFW(buf, 12) = x;
  4615. WBUFW(buf, 14) = y;
  4616. WBUFL(buf, 16) = unit_id;
  4617. WBUFB(buf, 20) = (unsigned char)range;
  4618. WBUFB(buf, 21) = 1;
  4619. WBUFB(buf, 22) = (unsigned char)skill_lv;
  4620. clif_send(buf, packet_len(0x09ca), bl, AREA);
  4621. }
  4622. /// Server tells client to remove unit of id 'unit->bl.id'
  4623. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4624. static void clif_clearchar_skillunit( skill_unit& unit, map_session_data& sd ){
  4625. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4626. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4627. packet.GID = unit.bl.id;
  4628. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4629. if( unit.group && unit.group->skill_id == WZ_ICEWALL ){
  4630. clif_changemapcell( sd.fd, unit.bl.m, unit.bl.x, unit.bl.y, unit.val2, SELF );
  4631. }
  4632. }
  4633. /// Removes a skill unit.
  4634. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4635. void clif_skill_delunit( skill_unit& unit ){
  4636. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4637. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4638. packet.GID = unit.bl.id;
  4639. clif_send( &packet, sizeof( packet ), &unit.bl, AREA );
  4640. }
  4641. /// Sent when an object gets ankle-snared.
  4642. /// 01ac <id>.L (ZC_SKILL_UPDATE)
  4643. /// Only affects units with class [139,153] client-side.
  4644. void clif_skillunit_update( block_list& bl ){
  4645. PACKET_ZC_SKILL_UPDATE packet{};
  4646. packet.packetType = HEADER_ZC_SKILL_UPDATE;
  4647. packet.GID = bl.id;
  4648. clif_send( &packet, sizeof( packet ), &bl, AREA );
  4649. }
  4650. /*==========================================
  4651. *
  4652. *------------------------------------------*/
  4653. static int clif_getareachar(struct block_list* bl,va_list ap)
  4654. {
  4655. map_session_data *sd;
  4656. nullpo_ret(bl);
  4657. sd=va_arg(ap,map_session_data*);
  4658. if (!clif_session_isValid(sd))
  4659. return 0;
  4660. switch(bl->type){
  4661. case BL_ITEM:
  4662. clif_getareachar_item(sd,(struct flooritem_data*) bl);
  4663. break;
  4664. case BL_SKILL:
  4665. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &sd->bl);
  4666. break;
  4667. default:
  4668. if(&sd->bl == bl)
  4669. break;
  4670. clif_getareachar_unit(sd,bl);
  4671. break;
  4672. }
  4673. return 0;
  4674. }
  4675. /*==========================================
  4676. * tbl has gone out of view-size of bl
  4677. *------------------------------------------*/
  4678. int clif_outsight(struct block_list *bl,va_list ap)
  4679. {
  4680. struct block_list *tbl;
  4681. struct view_data *vd;
  4682. TBL_PC *sd, *tsd;
  4683. tbl=va_arg(ap,struct block_list*);
  4684. if(bl == tbl) return 0;
  4685. sd = BL_CAST(BL_PC, bl);
  4686. tsd = BL_CAST(BL_PC, tbl);
  4687. if (clif_session_isValid(tsd)) { //tsd has lost sight of the bl object.
  4688. nullpo_ret(bl);
  4689. switch(bl->type){
  4690. case BL_PC:
  4691. if(sd->vd.class_ != JT_INVISIBLE)
  4692. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4693. if(sd->chatID){
  4694. struct chat_data *cd;
  4695. cd=(struct chat_data*)map_id2bl(sd->chatID);
  4696. if(cd->usersd[0]==sd)
  4697. clif_dispchat(cd,tsd->fd);
  4698. }
  4699. if(sd->state.vending)
  4700. clif_closevendingboard(bl,tsd->fd);
  4701. if(sd->state.buyingstore)
  4702. clif_buyingstore_disappear_entry( *sd, &tsd->bl );
  4703. break;
  4704. case BL_ITEM:
  4705. clif_clearflooritem( *reinterpret_cast<flooritem_data*>( bl ), tsd );
  4706. break;
  4707. case BL_SKILL:
  4708. clif_clearchar_skillunit( *((skill_unit *)bl), *tsd );
  4709. break;
  4710. case BL_NPC:
  4711. if(!(((TBL_NPC*)bl)->is_invisible))
  4712. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4713. break;
  4714. default:
  4715. if((vd=status_get_viewdata(bl)) && vd->class_ != JT_INVISIBLE)
  4716. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4717. break;
  4718. }
  4719. }
  4720. if (clif_session_isValid(sd)) { //sd is watching tbl go out of view.
  4721. nullpo_ret(tbl);
  4722. if(tbl->type == BL_SKILL) //Trap knocked out of sight
  4723. clif_clearchar_skillunit( *((skill_unit *)tbl), *sd );
  4724. else if(((vd=status_get_viewdata(tbl)) && vd->class_ != JT_INVISIBLE) &&
  4725. !(tbl->type == BL_NPC && (((TBL_NPC*)tbl)->is_invisible)))
  4726. clif_clearunit_single( tbl->id, CLR_OUTSIGHT, *sd );
  4727. }
  4728. return 0;
  4729. }
  4730. /*==========================================
  4731. * tbl has come into view of bl
  4732. *------------------------------------------*/
  4733. int clif_insight(struct block_list *bl,va_list ap)
  4734. {
  4735. struct block_list *tbl;
  4736. TBL_PC *sd, *tsd;
  4737. tbl=va_arg(ap,struct block_list*);
  4738. if (bl == tbl) return 0;
  4739. sd = BL_CAST(BL_PC, bl);
  4740. tsd = BL_CAST(BL_PC, tbl);
  4741. if (clif_session_isValid(tsd)) { //Tell tsd that bl entered into his view
  4742. switch(bl->type){
  4743. case BL_ITEM:
  4744. clif_getareachar_item(tsd,(struct flooritem_data*)bl);
  4745. break;
  4746. case BL_SKILL:
  4747. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &tsd->bl);
  4748. break;
  4749. default:
  4750. clif_getareachar_unit(tsd,bl);
  4751. break;
  4752. }
  4753. }
  4754. if (clif_session_isValid(sd)) { //Tell sd that tbl walked into his view
  4755. clif_getareachar_unit(sd,tbl);
  4756. }
  4757. return 0;
  4758. }
  4759. /// Updates whole skill tree (ZC_SKILLINFO_LIST).
  4760. /// 010f <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  4761. void clif_skillinfoblock(map_session_data *sd)
  4762. {
  4763. int fd;
  4764. int i,len,id;
  4765. nullpo_retv(sd);
  4766. fd = sd->fd;
  4767. if (!session_isActive(fd))
  4768. return;
  4769. WFIFOHEAD(fd, MAX_SKILL * 37 + 4);
  4770. WFIFOW(fd,0) = 0x10f;
  4771. bool haveCallPartnerSkill = false;
  4772. for ( i = 0, len = 4; i < MAX_SKILL; i++)
  4773. {
  4774. if( (id = sd->status.skill[i].id) != 0 )
  4775. {
  4776. // workaround for bugreport:5348
  4777. if (len + 37 > 8192)
  4778. break;
  4779. // skip WE_CALLPARTNER and send it in special way
  4780. if (id == WE_CALLPARTNER) {
  4781. haveCallPartnerSkill = true;
  4782. continue;
  4783. }
  4784. WFIFOW(fd,len) = id;
  4785. WFIFOL(fd,len+2) = skill_get_inf(id);
  4786. WFIFOW(fd,len+6) = sd->status.skill[i].lv;
  4787. WFIFOW(fd,len+8) = skill_get_sp(id,sd->status.skill[i].lv);
  4788. WFIFOW(fd,len+10)= skill_get_range2(&sd->bl,id,sd->status.skill[i].lv,false);
  4789. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  4790. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  4791. WFIFOB(fd,len+36) = (sd->status.skill[i].lv < skill_tree_get_max(id, sd->status.class_))? 1:0;
  4792. else
  4793. WFIFOB(fd,len+36) = 0;
  4794. len += 37;
  4795. }
  4796. }
  4797. WFIFOW(fd,2)=len;
  4798. WFIFOSET(fd,len);
  4799. // adoption fix
  4800. if (haveCallPartnerSkill) {
  4801. clif_addskill(*sd, WE_CALLPARTNER);
  4802. clif_skillinfo(sd, WE_CALLPARTNER, 0);
  4803. }
  4804. // workaround for bugreport:5348; send the remaining skills one by one to bypass packet size limit
  4805. for ( ; i < MAX_SKILL; i++)
  4806. {
  4807. if( (id = sd->status.skill[i].id) != 0 && ( id != WE_CALLPARTNER || !haveCallPartnerSkill ) )
  4808. {
  4809. clif_addskill(*sd, id);
  4810. clif_skillinfo(sd, id, 0);
  4811. }
  4812. }
  4813. }
  4814. /**
  4815. * Server tells client 'sd' to add skill of id 'id' to it's skill tree (e.g. with Ice Falcion item)
  4816. **/
  4817. /// Adds new skill to the skill tree.
  4818. /// 0111 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B (ZC_ADD_SKILL)
  4819. /// 0b31 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <upgradable>.B <isnew>.B (ZC_ADD_SKILL2)
  4820. void clif_addskill(map_session_data &sd, uint16 skill_id){
  4821. uint16 idx = skill_get_index(skill_id);
  4822. if (idx == 0)
  4823. return;
  4824. if( sd.status.skill[idx].id <= 0 )
  4825. return;
  4826. PACKET_ZC_ADD_SKILL p{};
  4827. p.packetType = HEADER_ZC_ADD_SKILL;
  4828. p.skill.id = skill_id;
  4829. p.skill.inf = skill_get_inf(skill_id);
  4830. p.skill.level = sd.status.skill[idx].lv;
  4831. p.skill.sp = static_cast<decltype(p.skill.sp)>( skill_get_sp(skill_id,sd.status.skill[idx].lv) );
  4832. p.skill.range2 = static_cast<decltype(p.skill.range2)>( skill_get_range2(&sd.bl,skill_id,sd.status.skill[idx].lv,false) );
  4833. #if PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190918
  4834. p.skill.level2 = pc_checkskill(&sd,skill_id);
  4835. #else
  4836. safestrncpy(p.skill.name, skill_get_name(skill_id), sizeof(p.skill.name));
  4837. #endif
  4838. if( sd.status.skill[idx].flag == SKILL_FLAG_PERMANENT && sd.status.skill[idx].lv < skill_tree_get_max(skill_id, sd.status.class_))
  4839. p.skill.upFlag = 1;
  4840. else
  4841. p.skill.upFlag = 0;
  4842. clif_send(&p,sizeof(p),&sd.bl,SELF);
  4843. }
  4844. /// Deletes a skill from the skill tree.
  4845. /// 0441 <skill id>.W (ZC_SKILLINFO_DELETE)
  4846. void clif_deleteskill(map_session_data& sd, uint16 skill_id, bool skip_infoblock){
  4847. #if PACKETVER >= 20081126
  4848. uint16 idx = skill_get_index(skill_id);
  4849. if (idx == 0)
  4850. return;
  4851. PACKET_ZC_SKILLINFO_DELETE p{};
  4852. p.packetType = HEADER_ZC_SKILLINFO_DELETE;
  4853. p.skillID = skill_id;
  4854. clif_send(&p,sizeof(p),&sd.bl,SELF);
  4855. #endif
  4856. #if PACKETVER_MAIN_NUM >= 20190807 || PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190918
  4857. if (!skip_infoblock)
  4858. #endif
  4859. clif_skillinfoblock(&sd);
  4860. }
  4861. /// Updates a skill in the skill tree (ZC_SKILLINFO_UPDATE).
  4862. /// 010e <skill id>.W <level>.W <sp cost>.W <attack range>.W <upgradable>.B
  4863. void clif_skillup(map_session_data *sd, uint16 skill_id, int lv, int range, int upgradable) {
  4864. nullpo_retv(sd);
  4865. int fd = sd->fd;
  4866. uint16 idx = skill_get_index(skill_id);
  4867. if (!session_isActive(fd) || !idx)
  4868. return;
  4869. WFIFOHEAD(fd, packet_len(0x10e));
  4870. WFIFOW(fd, 0) = 0x10e;
  4871. WFIFOW(fd, 2) = skill_id;
  4872. WFIFOW(fd, 4) = lv;
  4873. WFIFOW(fd, 6) = skill_get_sp(skill_id, lv);
  4874. WFIFOW(fd, 8) = range;
  4875. WFIFOB(fd, 10) = upgradable;
  4876. WFIFOSET(fd, packet_len(0x10e));
  4877. }
  4878. /// Updates a skill in the skill tree (ZC_SKILLINFO_UPDATE2).
  4879. /// 07e1 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <upgradable>.B
  4880. void clif_skillinfo(map_session_data *sd,int skill_id, int inf)
  4881. {
  4882. nullpo_retv(sd);
  4883. const int fd = sd->fd;
  4884. uint16 idx = skill_get_index(skill_id);
  4885. if (!session_isActive(fd) || !idx)
  4886. return;
  4887. WFIFOHEAD(fd,packet_len(0x7e1));
  4888. WFIFOW(fd,0) = 0x7e1;
  4889. WFIFOW(fd,2) = skill_id;
  4890. WFIFOL(fd,4) = inf?inf:skill_get_inf(skill_id);
  4891. WFIFOW(fd,8) = sd->status.skill[idx].lv;
  4892. WFIFOW(fd,10) = skill_get_sp(skill_id,sd->status.skill[idx].lv);
  4893. WFIFOW(fd,12) = skill_get_range2(&sd->bl,skill_id,sd->status.skill[idx].lv,false);
  4894. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  4895. WFIFOB(fd,14) = (sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_))? 1:0;
  4896. else
  4897. WFIFOB(fd,14) = 0;
  4898. WFIFOSET(fd,packet_len(0x7e1));
  4899. }
  4900. void clif_skill_scale( struct block_list *bl, int src_id, int x, int y, uint16 skill_id, uint16 skill_lv, int casttime ){
  4901. #if PACKETVER >= 20151223
  4902. if( !battle_config.show_skill_scale ){
  4903. return;
  4904. }
  4905. PACKET_ZC_SKILL_SCALE p = {};
  4906. p.PacketType = skillscale;
  4907. p.AID = src_id;
  4908. p.skill_id = skill_id;
  4909. p.skill_lv = skill_lv;
  4910. p.x = x;
  4911. p.y = y;
  4912. p.casttime = casttime;
  4913. if( disguised( bl ) ){
  4914. clif_send( &p, sizeof( p ), bl, AREA_WOS );
  4915. p.AID = disguised_bl_id( bl->id );
  4916. clif_send( &p, sizeof( p ), bl, SELF );
  4917. }else{
  4918. clif_send( &p, sizeof( p ), bl, AREA );
  4919. }
  4920. #endif
  4921. }
  4922. /// Notifies clients in area, that an object is about to use a skill.
  4923. /// 013e <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L (ZC_USESKILL_ACK)
  4924. /// 07fb <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L <is disposable>.B (ZC_USESKILL_ACK2)
  4925. /// property:
  4926. /// 0 = Yellow cast aura
  4927. /// 1 = Water elemental cast aura
  4928. /// 2 = Earth elemental cast aura
  4929. /// 3 = Fire elemental cast aura
  4930. /// 4 = Wind elemental cast aura
  4931. /// 5 = Poison elemental cast aura
  4932. /// 6 = Holy elemental cast aura
  4933. /// ? = like 0
  4934. /// is disposable:
  4935. /// 0 = yellow chat text "[src name] will use skill [skill name]."
  4936. /// 1 = no text
  4937. 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)
  4938. {
  4939. #if PACKETVER < 20091124
  4940. const int cmd = 0x13e;
  4941. #else
  4942. const int cmd = 0x7fb;
  4943. #endif
  4944. unsigned char buf[32];
  4945. WBUFW(buf,0) = cmd;
  4946. WBUFL(buf,2) = src_id;
  4947. WBUFL(buf,6) = dst_id;
  4948. WBUFW(buf,10) = dst_x;
  4949. WBUFW(buf,12) = dst_y;
  4950. WBUFW(buf,14) = skill_id;
  4951. WBUFL(buf,16) = property<0?0:property; //Avoid sending negatives as element [Skotlex]
  4952. WBUFL(buf,20) = casttime;
  4953. #if PACKETVER >= 20091124
  4954. WBUFB(buf,24) = 0; // isDisposable
  4955. #endif
  4956. if (disguised(bl)) {
  4957. clif_send(buf,packet_len(cmd), bl, AREA_WOS);
  4958. WBUFL(buf,2) = disguised_bl_id( src_id );
  4959. clif_send(buf,packet_len(cmd), bl, SELF);
  4960. } else
  4961. clif_send(buf,packet_len(cmd), bl, AREA);
  4962. if( skill_get_inf2( skill_id, INF2_SHOWSCALE ) ){
  4963. clif_skill_scale( bl, src_id, bl->x, bl->y, skill_id, skill_lv, casttime );
  4964. }
  4965. }
  4966. /// Notifies clients in area, that an object canceled casting.
  4967. /// 01b9 <id>.L (ZC_DISPEL)
  4968. void clif_skillcastcancel( block_list& bl ){
  4969. PACKET_ZC_DISPEL packet{};
  4970. packet.packetType = HEADER_ZC_DISPEL;
  4971. packet.gid = bl.id;
  4972. clif_send( &packet, sizeof(PACKET_ZC_DISPEL), &bl, AREA );
  4973. }
  4974. /// Notifies the client about the result of a skill use request (ZC_ACK_TOUSESKILL).
  4975. /// 0110 <skill id>.W <num>.L <result>.B <cause>.B
  4976. /// num (only used when skill id = NV_BASIC and cause = 0):
  4977. /// 0 = "skill failed" MsgStringTable[159]
  4978. /// 1 = "no emotions" MsgStringTable[160]
  4979. /// 2 = "no sit" MsgStringTable[161]
  4980. /// 3 = "no chat" MsgStringTable[162]
  4981. /// 4 = "no party" MsgStringTable[163]
  4982. /// 5 = "no shout" MsgStringTable[164]
  4983. /// 6 = "no PKing" MsgStringTable[165]
  4984. /// 7 = "no alligning" MsgStringTable[383]
  4985. /// ? = ignored
  4986. /// cause:
  4987. /// 0 = "not enough skill level" MsgStringTable[214] (AL_WARP)
  4988. /// "steal failed" MsgStringTable[205] (TF_STEAL)
  4989. /// "envenom failed" MsgStringTable[207] (TF_POISON)
  4990. /// "skill failed" MsgStringTable[204] (otherwise)
  4991. /// ... = @see enum useskill_fail_cause
  4992. /// ? = ignored
  4993. ///
  4994. /// if(result!=0) doesn't display any of the previous messages
  4995. /// Note: when this packet is received an unknown flag is always set to 0,
  4996. /// suggesting this is an ACK packet for the UseSkill packets and should be sent on success too [FlavioJS]
  4997. void clif_skill_fail( map_session_data& sd, uint16 skill_id, enum useskill_fail_cause cause, int btype, t_itemid itemId ){
  4998. if(battle_config.display_skill_fail&1)
  4999. return; //Disable all skill failed messages
  5000. if(cause==USESKILL_FAIL_SKILLINTERVAL && !sd.state.showdelay)
  5001. return; //Disable delay failed messages
  5002. if(skill_id == RG_SNATCHER && battle_config.display_skill_fail&4)
  5003. return;
  5004. if(skill_id == TF_POISON && battle_config.display_skill_fail&8)
  5005. return;
  5006. PACKET_ZC_ACK_TOUSESKILL p = {};
  5007. p.packetType = HEADER_ZC_ACK_TOUSESKILL;
  5008. p.skillId = skill_id;
  5009. p.btype = btype;
  5010. p.itemId = itemId;
  5011. p.flag = 0; // 0 - failed
  5012. p.cause = cause;
  5013. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  5014. }
  5015. /// Skill cooldown display icon.
  5016. /// 043d <skill ID>.W <tick>.L (ZC_SKILL_POSTDELAY)
  5017. void clif_skill_cooldown( map_session_data &sd, uint16 skill_id, t_tick tick ){
  5018. #if PACKETVER_MAIN_NUM >= 20081112 || PACKETVER_RE_NUM >= 20081111 || defined(PACKETVER_ZERO)
  5019. PACKET_ZC_SKILL_POSTDELAY packet{};
  5020. packet.PacketType = HEADER_ZC_SKILL_POSTDELAY;
  5021. packet.SKID = skill_id;
  5022. packet.DelayTM = client_tick(tick);
  5023. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5024. #endif
  5025. }
  5026. /// Skill attack effect and damage.
  5027. /// 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)
  5028. /// 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)
  5029. 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)
  5030. {
  5031. unsigned char buf[64];
  5032. status_change *sc;
  5033. int damage = (int)cap_value(sdamage,INT_MIN,INT_MAX);
  5034. nullpo_ret(src);
  5035. nullpo_ret(dst);
  5036. type = clif_calc_delay(type,div,damage,ddelay);
  5037. if( ( sc = status_get_sc(dst) ) && sc->count ) {
  5038. if(sc->getSCE(SC_HALLUCINATION) && damage)
  5039. damage = clif_hallucination_damage();
  5040. }
  5041. #if PACKETVER < 3
  5042. WBUFW(buf,0)=0x114;
  5043. WBUFW(buf,2)=skill_id;
  5044. WBUFL(buf,4)=src->id;
  5045. WBUFL(buf,8)=dst->id;
  5046. WBUFL(buf,12)=client_tick(tick);
  5047. WBUFL(buf,16)=sdelay;
  5048. WBUFL(buf,20)=ddelay;
  5049. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5050. WBUFW(buf,24)=damage?div:0;
  5051. } else {
  5052. WBUFW(buf,24)=damage;
  5053. }
  5054. WBUFW(buf,26)=skill_lv;
  5055. WBUFW(buf,28)=div;
  5056. WBUFB(buf,30)=type;
  5057. if (disguised(dst)) {
  5058. clif_send(buf,packet_len(0x114),dst,AREA_WOS);
  5059. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5060. clif_send(buf,packet_len(0x114),dst,SELF);
  5061. } else
  5062. clif_send(buf,packet_len(0x114),dst,AREA);
  5063. if(disguised(src)) {
  5064. WBUFL(buf,4)=disguised_bl_id(src->id);
  5065. if (disguised(dst))
  5066. WBUFL(buf,8)=dst->id;
  5067. if(damage > 0)
  5068. WBUFW(buf,24)=-1;
  5069. clif_send(buf,packet_len(0x114),src,SELF);
  5070. }
  5071. #else
  5072. WBUFW(buf,0)=0x1de;
  5073. WBUFW(buf,2)=skill_id;
  5074. WBUFL(buf,4)=src->id;
  5075. WBUFL(buf,8)=dst->id;
  5076. WBUFL(buf,12)=client_tick(tick);
  5077. WBUFL(buf,16)=sdelay;
  5078. WBUFL(buf,20)=ddelay;
  5079. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5080. WBUFL(buf,24)=damage?div:0;
  5081. } else {
  5082. WBUFL(buf,24)=damage;
  5083. }
  5084. WBUFW(buf,28)=skill_lv;
  5085. WBUFW(buf,30)=div;
  5086. // For some reason, late 2013 and newer clients have
  5087. // a issue that causes players and monsters to endure
  5088. // type 6 (ACTION_SKILL) skills. So we have to do a small
  5089. // hack to set all type 6 to be sent as type 8 ACTION_ATTACK_MULTIPLE
  5090. #if PACKETVER < 20131223
  5091. WBUFB(buf,32)=type;
  5092. #else
  5093. WBUFB(buf,32)=( type == DMG_SINGLE ) ? DMG_MULTI_HIT : type;
  5094. #endif
  5095. if (disguised(dst)) {
  5096. clif_send(buf,packet_len(0x1de),dst,AREA_WOS);
  5097. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5098. clif_send(buf,packet_len(0x1de),dst,SELF);
  5099. } else
  5100. clif_send(buf,packet_len(0x1de),dst,AREA);
  5101. if(disguised(src)) {
  5102. WBUFL(buf,4)=disguised_bl_id(src->id);
  5103. if (disguised(dst))
  5104. WBUFL(buf,8)=dst->id;
  5105. if(damage > 0)
  5106. WBUFL(buf,24)=-1;
  5107. clif_send(buf,packet_len(0x1de),src,SELF);
  5108. }
  5109. #endif
  5110. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5111. return clif_calc_walkdelay(dst,ddelay,type,damage,div);
  5112. }
  5113. /// Ground skill attack effect and damage (ZC_NOTIFY_SKILL_POSITION).
  5114. /// 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
  5115. /*
  5116. 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)
  5117. {
  5118. unsigned char buf[64];
  5119. status_change *sc;
  5120. nullpo_ret(src);
  5121. nullpo_ret(dst);
  5122. type = (type>DMG_NORMAL)?type:skill_get_hit(skill_id);
  5123. type = clif_calc_delay(type,div,damage,ddelay);
  5124. sc = status_get_sc(dst);
  5125. if(sc && sc->count) {
  5126. if(sc->getSCE(SC_HALLUCINATION) && damage)
  5127. damage = clif_hallucination_damage();
  5128. }
  5129. WBUFW(buf,0)=0x115;
  5130. WBUFW(buf,2)=skill_id;
  5131. WBUFL(buf,4)=src->id;
  5132. WBUFL(buf,8)=dst->id;
  5133. WBUFL(buf,12)=client_tick(tick);
  5134. WBUFL(buf,16)=sdelay;
  5135. WBUFL(buf,20)=ddelay;
  5136. WBUFW(buf,24)=dst->x;
  5137. WBUFW(buf,26)=dst->y;
  5138. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5139. WBUFW(buf,28)=damage?div:0;
  5140. } else {
  5141. WBUFW(buf,28)=damage;
  5142. }
  5143. WBUFW(buf,30)=skill_lv;
  5144. WBUFW(buf,32)=div;
  5145. WBUFB(buf,34)=type;
  5146. clif_send(buf,packet_len(0x115),src,AREA);
  5147. if(disguised(src)) {
  5148. WBUFL(buf,4)=disguised_bl_id(src->id);
  5149. if(damage > 0)
  5150. WBUFW(buf,28)=-1;
  5151. clif_send(buf,packet_len(0x115),src,SELF);
  5152. }
  5153. if (disguised(dst)) {
  5154. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5155. if (disguised(src))
  5156. WBUFL(buf,4)=src->id;
  5157. else if(damage > 0)
  5158. WBUFW(buf,28)=-1;
  5159. clif_send(buf,packet_len(0x115),dst,SELF);
  5160. }
  5161. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5162. return clif_calc_walkdelay(dst,ddelay,type,damage,div);
  5163. }
  5164. */
  5165. /// Non-damaging skill effect
  5166. /// 011a <skill id>.W <heal>.W <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL)
  5167. /// 09cb <skill id>.W <heal>.L <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL2)
  5168. bool clif_skill_nodamage( block_list* src, block_list& dst, uint16 skill_id, int32 heal, bool success ){
  5169. PACKET_ZC_USE_SKILL p{};
  5170. p.PacketType = HEADER_ZC_USE_SKILL;
  5171. p.SKID = skill_id;
  5172. p.level = std::min( static_cast<decltype(p.level)>( heal ), std::numeric_limits<decltype(p.level)>::max() );
  5173. p.targetAID = dst.id;
  5174. p.result = success;
  5175. if(src != nullptr){
  5176. p.srcAID = src->id;
  5177. }else{
  5178. p.srcAID = 0;
  5179. }
  5180. if (disguised(&dst)) {
  5181. clif_send(&p, sizeof(p), &dst, AREA_WOS);
  5182. p.targetAID = disguised_bl_id(dst.id);
  5183. clif_send(&p, sizeof(p), &dst, SELF);
  5184. } else
  5185. clif_send(&p, sizeof(p), &dst, AREA);
  5186. if(src != nullptr && disguised(src)) {
  5187. p.srcAID = disguised_bl_id(src->id);
  5188. if (disguised(&dst)){
  5189. // It is necessary to revert the changes done above for the disguised target
  5190. p.targetAID = dst.id;
  5191. }
  5192. clif_send(&p, sizeof(p), src, SELF);
  5193. }
  5194. return success;
  5195. }
  5196. /// Non-damaging ground skill effect (ZC_NOTIFY_GROUNDSKILL).
  5197. /// 0117 <skill id>.W <src id>.L <level>.W <x>.W <y>.W <tick>.L
  5198. void clif_skill_poseffect(struct block_list *src,uint16 skill_id,int val,int x,int y,t_tick tick)
  5199. {
  5200. unsigned char buf[32];
  5201. nullpo_retv(src);
  5202. WBUFW(buf,0)=0x117;
  5203. WBUFW(buf,2)=skill_id;
  5204. WBUFL(buf,4)=src->id;
  5205. WBUFW(buf,8)=val;
  5206. WBUFW(buf,10)=x;
  5207. WBUFW(buf,12)=y;
  5208. WBUFL(buf,14)=client_tick(tick);
  5209. if(disguised(src)) {
  5210. clif_send(buf,packet_len(0x117),src,AREA_WOS);
  5211. WBUFL(buf,4)=disguised_bl_id(src->id);
  5212. clif_send(buf,packet_len(0x117),src,SELF);
  5213. } else
  5214. clif_send(buf,packet_len(0x117),src,AREA);
  5215. }
  5216. /// Presents a list of available warp destinations.
  5217. /// 011c <skill id>.W { <map name>.16B }*4 (ZC_WARPLIST)
  5218. /// 0abe <lenght>.W <skill id>.W { <map name>.16B }*? (ZC_WARPLIST2)
  5219. void clif_skill_warppoint( map_session_data& sd, uint16 skill_id, uint16 skill_lv, std::vector<std::string>& maps ){
  5220. if(maps.empty())
  5221. return;
  5222. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  5223. PACKET_ZC_WARPLIST* p = reinterpret_cast<PACKET_ZC_WARPLIST*>( packet_buffer );
  5224. p->packetType = HEADER_ZC_WARPLIST;
  5225. p->packetLength = sizeof( *p );
  5226. p->skillId = skill_id;
  5227. size_t memoCount = 0;
  5228. for( std::string& map : maps ){
  5229. if( map.empty() ){
  5230. continue;
  5231. }
  5232. PACKET_ZC_WARPLIST_sub& warp = p->maps[memoCount];
  5233. mapindex_getmapname_ext( map.c_str(), warp.map );
  5234. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( warp ) );
  5235. memoCount++;
  5236. }
  5237. clif_send( p, p->packetLength, &sd.bl, SELF );
  5238. #else
  5239. PACKET_ZC_WARPLIST p = {};
  5240. p.packetType = HEADER_ZC_WARPLIST;
  5241. p.skillId = skill_id;
  5242. size_t memoCount = 0, max = 4;
  5243. for( std::string& map : maps ){
  5244. if( map.empty() ){
  5245. continue;
  5246. }
  5247. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5248. mapindex_getmapname_ext( map.c_str(), warp.map );
  5249. if( memoCount++ == max ){
  5250. break;
  5251. }
  5252. }
  5253. for( ; memoCount < max; memoCount++ ){
  5254. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5255. strcpy( warp.map, "" );
  5256. }
  5257. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  5258. #endif
  5259. sd.menuskill_id = skill_id;
  5260. if (skill_id == AL_WARP) {
  5261. sd.menuskill_val = (sd.ud.skillx<<16)|sd.ud.skilly; //Store warp position here.
  5262. sd.state.workinprogress = WIP_DISABLE_ALL;
  5263. } else
  5264. sd.menuskill_val = skill_lv;
  5265. }
  5266. /// Memo message.
  5267. /// 011e <type>.B (ZC_ACK_REMEMBER_WARPPOINT)
  5268. /// type:
  5269. /// 0 = "Saved location as a Memo Point for Warp skill." in color 0xFFFF00 (cyan)
  5270. /// 1 = "Skill Level is not high enough." in color 0x0000FF (red)
  5271. /// 2 = "You haven't learned Warp." in color 0x0000FF (red)
  5272. ///
  5273. /// @param sd Who receives the message
  5274. /// @param type What message
  5275. void clif_skill_memomessage( map_session_data& sd, e_ack_remember_warppoint_result result ){
  5276. PACKET_ZC_ACK_REMEMBER_WARPPOINT packet{};
  5277. packet.packetType = HEADER_ZC_ACK_REMEMBER_WARPPOINT;
  5278. packet.type = static_cast<decltype(packet.type)>(result);
  5279. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5280. }
  5281. /// Teleport message.
  5282. /// 0189 <type>.W (ZC_NOTIFY_MAPINFO)
  5283. /// type:
  5284. /// 0 = "Unable to Teleport in this area" in color 0xFFFF00 (cyan)
  5285. /// 1 = "Saved point cannot be memorized." in color 0x0000FF (red)
  5286. /// 2 = "This skill cannot be used in this area"
  5287. /// 3 = "This item cannot be used in this area"
  5288. ///
  5289. /// @param sd Who receives the message
  5290. /// @param type What message
  5291. void clif_skill_teleportmessage( map_session_data& sd, e_notify_mapinfo_result result ){
  5292. PACKET_ZC_NOTIFY_MAPINFO packet{};
  5293. packet.packetType = HEADER_ZC_NOTIFY_MAPINFO;
  5294. packet.type = static_cast<decltype(packet.type)>(result);
  5295. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5296. }
  5297. /// Displays Sense (WZ_ESTIMATION) information window (ZC_MONSTER_INFO).
  5298. /// 018c <class>.W <level>.W <size>.W <hp>.L <def>.W <race>.W <mdef>.W <element>.W
  5299. /// <water%>.B <earth%>.B <fire%>.B <wind%>.B <poison%>.B <holy%>.B <shadow%>.B <ghost%>.B <undead%>.B
  5300. void clif_skill_estimation(map_session_data *sd,struct block_list *dst)
  5301. {
  5302. unsigned char buf[64];
  5303. int i, fix;
  5304. nullpo_retv(sd);
  5305. nullpo_retv(dst);
  5306. if( dst->type != BL_MOB )
  5307. return;
  5308. status_data* status = status_get_status_data(*dst);
  5309. WBUFW(buf, 0)=0x18c;
  5310. WBUFW(buf, 2)=status_get_class(dst);
  5311. WBUFW(buf, 4)=status_get_lv(dst);
  5312. WBUFW(buf, 6)=status->size;
  5313. WBUFL(buf, 8)=status->hp;
  5314. WBUFW(buf,12)= (battle_config.estimation_type&1?status->def:0)
  5315. +(battle_config.estimation_type&2?status->def2:0);
  5316. WBUFW(buf,14)=status->race;
  5317. WBUFW(buf,16)= (battle_config.estimation_type&1?status->mdef:0)
  5318. +(battle_config.estimation_type&2?status->mdef2:0);
  5319. WBUFW(buf,18)= status->def_ele;
  5320. for(i=0;i<9;i++)
  5321. // The following caps negative attributes to 0 since the client displays them as 255-fix. [Skotlex]
  5322. WBUFB(buf,20+i)= (unsigned char)((fix=elemental_attribute_db.getAttribute(status->ele_lv, i+1, status->def_ele))<0?0:fix);
  5323. clif_send(buf,packet_len(0x18c),&sd->bl,sd->status.party_id>0?PARTY_SAMEMAP:SELF);
  5324. }
  5325. /// Presents a textual list of producable items.
  5326. /// 018d <packet len>.W { <name id>.W { <material id>.W }*3 }* (ZC_MAKABLEITEMLIST)
  5327. /// material id:
  5328. /// unused by the client
  5329. void clif_skill_produce_mix_list( map_session_data& sd, int skill_id, int trigger ){
  5330. // Avoid resending the menu
  5331. if( sd.menuskill_id == skill_id ){
  5332. return;
  5333. }
  5334. if (skill_id == GC_CREATENEWPOISON)
  5335. skill_id = GC_RESEARCHNEWPOISON;
  5336. PACKET_ZC_MAKABLEITEMLIST* p = reinterpret_cast<PACKET_ZC_MAKABLEITEMLIST*>( packet_buffer );
  5337. p->packetType = HEADER_ZC_MAKABLEITEMLIST;
  5338. p->packetLength = sizeof( *p );
  5339. int count = 0;
  5340. for( int i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5341. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, 1 ) ){
  5342. continue;
  5343. }
  5344. if( skill_id > 0 && skill_produce_db[i].req_skill != skill_id ){
  5345. continue;
  5346. }
  5347. PACKET_ZC_MAKABLEITEMLIST_sub& entry = p->items[count];
  5348. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5349. entry.material[0] = 0;
  5350. entry.material[1] = 0;
  5351. entry.material[2] = 0;
  5352. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5353. count++;
  5354. }
  5355. clif_send( p, p->packetLength, &sd.bl, SELF );
  5356. if( count > 0 ){
  5357. sd.menuskill_id = skill_id;
  5358. sd.menuskill_val = trigger;
  5359. }
  5360. }
  5361. /// Present a list of producable items.
  5362. /// 025a <packet len>.W <mk type>.W { <name id>.W }* (ZC_MAKINGITEM_LIST)
  5363. /// mk type:
  5364. /// 1 = cooking
  5365. /// 2 = arrow
  5366. /// 3 = elemental
  5367. /// 4 = GN_MIX_COOKING
  5368. /// 5 = GN_MAKEBOMB
  5369. /// 6 = GN_S_PHARMACY
  5370. /// 7 = MT_M_MACHINE
  5371. /// 8 = BO_BIONIC_PHARMACY
  5372. void clif_cooking_list( map_session_data& sd, int trigger, uint16 skill_id, int qty, int list_type ){
  5373. #if PACKETVER >= 20051010
  5374. // Avoid resending the menu
  5375. if( sd.menuskill_id == skill_id ){
  5376. return;
  5377. }
  5378. PACKET_ZC_MAKINGITEM_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGITEM_LIST*>( packet_buffer );
  5379. p->packetType = HEADER_ZC_MAKINGITEM_LIST;
  5380. p->packetLength = sizeof( *p );
  5381. p->makeItem = list_type;
  5382. int count = 0;
  5383. for( int i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5384. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, qty ) ){
  5385. continue;
  5386. }
  5387. PACKET_ZC_MAKINGITEM_LIST_sub& entry = p->items[count];
  5388. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5389. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5390. count++;
  5391. }
  5392. if( count > 0 || skill_id == AM_PHARMACY ){
  5393. clif_send( p, p->packetLength, &sd.bl, SELF );
  5394. sd.menuskill_id = skill_id;
  5395. sd.menuskill_val = trigger;
  5396. sd.menuskill_val2 = qty; // amount.
  5397. }else{
  5398. clif_menuskill_clear( &sd );
  5399. #if PACKETVER >= 20090922
  5400. clif_msg_skill( &sd, skill_id, MSI_SKILL_INVENTORY_KINDCNT_OVER );
  5401. #else
  5402. clif_send( p, p->packetLength, &sd.bl, SELF );
  5403. #endif
  5404. }
  5405. #endif
  5406. }
  5407. /// Notifies clients of a status change.
  5408. /// 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)]
  5409. /// 043f <index>.W <id>.L <state>.B <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE2) [used exclusively for starting statuses on pcs]
  5410. /// 0983 <index>.W <id>.L <state>.B <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE3) (PACKETVER >= 20120618)
  5411. /// @param bl Sends packet to clients around this object
  5412. /// @param id ID of object that has this effect
  5413. /// @param type Status icon see enum efst_type
  5414. /// @param flag 1:Active, 0:Deactive
  5415. /// @param tick Duration in ms
  5416. /// @param val1
  5417. /// @param val2
  5418. /// @param val3
  5419. 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)
  5420. {
  5421. unsigned char buf[32];
  5422. if (type == EFST_BLANK) //It shows nothing on the client...
  5423. return;
  5424. if (type == EFST_POSTDELAY && tick == 0)
  5425. return;
  5426. nullpo_retv(bl);
  5427. // Statuses with an infinite duration, but still needs a duration sent to display properly.
  5428. if (type == EFST_LUNARSTANCE || type == EFST_UNIVERSESTANCE || type == EFST_SUNSTANCE || type == EFST_STARSTANCE)
  5429. tick = 200;
  5430. #if PACKETVER >= 20120618
  5431. if (flag && battle_config.display_status_timers)
  5432. WBUFW(buf,0) = 0x983;
  5433. else
  5434. #elif PACKETVER >= 20090121
  5435. if (flag && battle_config.display_status_timers)
  5436. WBUFW(buf,0) = 0x43f;
  5437. else
  5438. #endif
  5439. WBUFW(buf,0) = 0x196;
  5440. WBUFW(buf,2) = type;
  5441. WBUFL(buf,4) = id;
  5442. WBUFB(buf,8) = flag;
  5443. #if PACKETVER >= 20120618
  5444. if (flag && battle_config.display_status_timers) {
  5445. if (tick <= 0)
  5446. tick = 9999; // this is indeed what official servers do
  5447. WBUFL(buf,9) = client_tick(tick);/* at this stage remain and total are the same value I believe */
  5448. WBUFL(buf,13) = client_tick(tick);
  5449. WBUFL(buf,17) = val1;
  5450. WBUFL(buf,21) = val2;
  5451. WBUFL(buf,25) = val3;
  5452. }
  5453. #elif PACKETVER >= 20090121
  5454. if (flag && battle_config.display_status_timers) {
  5455. if (tick <= 0)
  5456. tick = 9999; // this is indeed what official servers do
  5457. WBUFL(buf,9) = client_tick(tick);
  5458. WBUFL(buf,13) = val1;
  5459. WBUFL(buf,17) = val2;
  5460. WBUFL(buf,21) = val3;
  5461. }
  5462. #endif
  5463. clif_send(buf, packet_len(WBUFW(buf,0)), bl, target_type);
  5464. }
  5465. /* Sends status effect to clients around the bl
  5466. * @param bl Object that has the effect
  5467. * @param type Status icon see enum efst_type
  5468. * @param flag 1:Active, 0:Deactive
  5469. * @param tick Duration in ms
  5470. * @param val1
  5471. * @param val2
  5472. * @param val3
  5473. */
  5474. void clif_status_change(struct block_list *bl, int type, int flag, t_tick tick, int val1, int val2, int val3) {
  5475. map_session_data *sd = nullptr;
  5476. if (type == EFST_BLANK) //It shows nothing on the client...
  5477. return;
  5478. if (type == EFST_POSTDELAY && tick == 0)
  5479. return;
  5480. if (type == EFST_ILLUSION && !battle_config.display_hallucination) // Disable Hallucination.
  5481. return;
  5482. #if !( PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 )
  5483. // Older clients display normal riding icon.
  5484. if (type == EFST_MADOGEAR_TYPE)
  5485. type = EFST_RIDING;
  5486. #endif
  5487. nullpo_retv(bl);
  5488. sd = BL_CAST(BL_PC, bl);
  5489. // Check if current bl type is in the returned bitmask and only send status changes that actually matter to the client
  5490. if (!(status_efst_get_bl_type(static_cast<efst_type>(type)) & bl->type))
  5491. return;
  5492. clif_status_change_sub(bl, bl->id, type, flag, tick, val1, val2, val3, ((sd ? (pc_isinvisible(sd) ? SELF : AREA) : AREA_WOS)));
  5493. }
  5494. /**
  5495. * Send any active EFST to those around.
  5496. * @param tbl: Unit to send the packet to
  5497. * @param bl: Objects walking into view
  5498. * @param target: Client send type
  5499. */
  5500. void clif_efst_status_change_sub(struct block_list *tbl, struct block_list *bl, enum send_target target) {
  5501. unsigned char i;
  5502. struct sc_display_entry **sc_display;
  5503. unsigned char sc_display_count;
  5504. bool spheres_sent;
  5505. nullpo_retv(bl);
  5506. switch( bl->type ){
  5507. case BL_PC: {
  5508. map_session_data* sd = (map_session_data*)bl;
  5509. sc_display = sd->sc_display;
  5510. sc_display_count = sd->sc_display_count;
  5511. spheres_sent = !sd->state.connect_new;
  5512. }
  5513. break;
  5514. case BL_NPC: {
  5515. struct npc_data* nd = (struct npc_data*)bl;
  5516. sc_display = nd->sc_display;
  5517. sc_display_count = nd->sc_display_count;
  5518. spheres_sent = true;
  5519. }
  5520. break;
  5521. default:
  5522. return;
  5523. }
  5524. for (i = 0; i < sc_display_count; i++) {
  5525. enum sc_type type = sc_display[i]->type;
  5526. status_change *sc = status_get_sc(bl);
  5527. const struct TimerData *td = (sc && sc->getSCE(type) ? get_timer(sc->getSCE(type)->timer) : nullptr);
  5528. t_tick tick = 0;
  5529. if (td)
  5530. tick = DIFF_TICK(td->tick, gettick());
  5531. // Status changes that need special handling
  5532. switch( type ){
  5533. case SC_SPHERE_1:
  5534. case SC_SPHERE_2:
  5535. case SC_SPHERE_3:
  5536. case SC_SPHERE_4:
  5537. case SC_SPHERE_5:
  5538. if( spheres_sent ){
  5539. target = AREA_WOS;
  5540. }
  5541. break;
  5542. case SC_HELLS_PLANT:
  5543. if( sc && sc->getSCE(type) ){
  5544. tick = sc->getSCE(type)->val4;
  5545. }
  5546. break;
  5547. }
  5548. #if PACKETVER > 20120418
  5549. 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);
  5550. #else
  5551. 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);
  5552. #endif
  5553. }
  5554. }
  5555. /// Notifies the client when a player enters the screen with an active EFST.
  5556. /// 08ff <id>.L <index>.W <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER) (PACKETVER >= 20111108)
  5557. /// 0984 <id>.L <index>.W <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER2) (PACKETVER >= 20120618)
  5558. 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) {
  5559. #if PACKETVER >= 20111108
  5560. unsigned char buf[32];
  5561. #if PACKETVER >= 20120618
  5562. const int cmd = 0x984;
  5563. #elif PACKETVER >= 20111108
  5564. const int cmd = 0x8ff;
  5565. #endif
  5566. int offset = 0;
  5567. if (type == EFST_BLANK)
  5568. return;
  5569. nullpo_retv(bl);
  5570. if (tick <= 0)
  5571. tick = 9999;
  5572. WBUFW(buf,offset + 0) = cmd;
  5573. WBUFL(buf,offset + 2) = tid;
  5574. WBUFW(buf,offset + 6) = type;
  5575. #if PACKETVER >= 20111108
  5576. WBUFL(buf,offset + 8) = client_tick(tick); // Set remaining status duration [exneval]
  5577. #if PACKETVER >= 20120618
  5578. WBUFL(buf,offset + 12) = client_tick(tick);
  5579. offset += 4;
  5580. #endif
  5581. WBUFL(buf,offset + 12) = val1;
  5582. WBUFL(buf,offset + 16) = val2;
  5583. WBUFL(buf,offset + 20) = val3;
  5584. #endif
  5585. clif_send(buf,packet_len(cmd),bl,target);
  5586. #endif
  5587. }
  5588. /// Send message (modified by [Yor]) (ZC_NOTIFY_PLAYERCHAT).
  5589. /// 008e <packet len>.W <message>.?B
  5590. void clif_displaymessage(const int fd, const char* mes)
  5591. {
  5592. nullpo_retv(mes);
  5593. if( session_isActive( fd ) ){
  5594. char *message, *line;
  5595. message = aStrdup(mes);
  5596. line = strtok(message, "\n");
  5597. while(line != nullptr) {
  5598. #if PACKETVER == 20141022
  5599. /** for some reason game client crashes depending on message pattern (only for this packet) **/
  5600. /** so we redirect to ZC_NPC_CHAT **/
  5601. //clif_messagecolor(&sd->bl, color_table[COLOR_DEFAULT], mes, false, SELF);
  5602. unsigned long color = (color_table[COLOR_DEFAULT] & 0x0000FF) << 16 | (color_table[COLOR_DEFAULT] & 0x00FF00) | (color_table[COLOR_DEFAULT] & 0xFF0000) >> 16; // RGB to BGR
  5603. unsigned short len = strnlen(line, CHAT_SIZE_MAX);
  5604. if (len > 0) {
  5605. WFIFOHEAD(fd, 13 + len);
  5606. WFIFOW(fd, 0) = 0x2C1;
  5607. WFIFOW(fd, 2) = 13 + len;
  5608. WFIFOL(fd, 4) = 0;
  5609. WFIFOL(fd, 8) = color;
  5610. safestrncpy(WFIFOCP(fd, 12), line, len + 1);
  5611. WFIFOSET(fd, WFIFOW(fd, 2));
  5612. }
  5613. #else
  5614. // Limit message to 255+1 characters (otherwise it causes a buffer overflow in the client)
  5615. int16 len;
  5616. len = static_cast<decltype(len)>( strnlen(line, CHAT_SIZE_MAX) );
  5617. if (len > 0) { // don't send a void message (it's not displaying on the client chat). @help can send void line.
  5618. WFIFOHEAD(fd, 5 + len);
  5619. WFIFOW(fd,0) = 0x8e;
  5620. WFIFOW(fd,2) = 5 + len; // 4 + len + nullptr teminate
  5621. safestrncpy(WFIFOCP(fd,4), line, len + 1);
  5622. WFIFOSET(fd, 5 + len);
  5623. }
  5624. #endif
  5625. line = strtok(nullptr, "\n");
  5626. }
  5627. aFree(message);
  5628. }
  5629. }
  5630. /// Send broadcast message in yellow or blue without font formatting.
  5631. /// 009a <packet len>.W <message>.?B (ZC_BROADCAST)
  5632. void clif_broadcast(struct block_list* bl, const char* mes, size_t len, int type, enum send_target target)
  5633. {
  5634. nullpo_retv(mes);
  5635. if (len < 2)
  5636. return;
  5637. PACKET_ZC_BROADCAST* p = reinterpret_cast<PACKET_ZC_BROADCAST*>( packet_buffer );
  5638. p->packetType = HEADER_ZC_BROADCAST;
  5639. p->PacketLength = sizeof( *p );
  5640. if( ( type&BC_BLUE ) != 0 ){
  5641. // If there's "blue" at the beginning of the message, game client will display it in blue instead of yellow.
  5642. const char* color = "blue";
  5643. size_t length = strlen( color );
  5644. strcpy( p->message, color );
  5645. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  5646. strncpy( &p->message[length], mes, len );
  5647. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5648. }else if( ( type&BC_WOE ) != 0 ){
  5649. // If there's "ssss", game client will recognize message as 'WoE broadcast'.
  5650. const char* color = "ssss";
  5651. size_t length = strlen( color );
  5652. strcpy( p->message, color );
  5653. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  5654. strncpy( &p->message[length], mes, len );
  5655. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5656. }else{
  5657. strncpy( p->message, mes, len );
  5658. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5659. }
  5660. clif_send( p, p->PacketLength, bl, target );
  5661. }
  5662. /*==========================================
  5663. * Displays a message on a 'bl' to all it's nearby clients
  5664. * 008d <PacketLength>.W <GID>.L <message>.?B (ZC_NOTIFY_CHAT)
  5665. *------------------------------------------*/
  5666. void clif_GlobalMessage( block_list& bl, const char* message, enum send_target target ){
  5667. nullpo_retv(message);
  5668. int16 len = (int16)( strlen( message ) + 1 );
  5669. if( len > CHAT_SIZE_MAX ) {
  5670. ShowWarning("clif_GlobalMessage: Truncating too long message '%s' (len=%" PRId16 ").\n", message, len);
  5671. len = CHAT_SIZE_MAX;
  5672. }
  5673. PACKET_ZC_NOTIFY_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT*>( packet_buffer );
  5674. p->PacketType = HEADER_ZC_NOTIFY_CHAT;
  5675. p->PacketLength = sizeof( *p );
  5676. p->GID = bl.id;
  5677. safestrncpy( p->Message, message, len );
  5678. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5679. clif_send( p, p->PacketLength, &bl, target );
  5680. }
  5681. /// Send broadcast message with font formatting.
  5682. /// 01c3 <packet len>.W <fontColor>.L <fontType>.W <fontSize>.W <fontAlign>.W <fontY>.W <message>.?B (ZC_BROADCAST2)
  5683. 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)
  5684. {
  5685. nullpo_retv(mes);
  5686. if (len < 2)
  5687. return;
  5688. PACKET_ZC_BROADCAST2* p = reinterpret_cast<PACKET_ZC_BROADCAST2*>( packet_buffer );
  5689. p->packetType = HEADER_ZC_BROADCAST2;
  5690. p->PacketLength = sizeof( *p );
  5691. p->fontColor = fontColor;
  5692. p->fontType = fontType;
  5693. p->fontSize = fontSize;
  5694. p->fontAlign = fontAlign;
  5695. p->fontY = fontY;
  5696. strncpy( p->message, mes, len );
  5697. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5698. clif_send( p, p->PacketLength, bl, target );
  5699. }
  5700. /*
  5701. * Display *msg to all *users in channel
  5702. */
  5703. void clif_channel_msg(struct Channel *channel, const char *msg, unsigned long color) {
  5704. DBIterator *iter;
  5705. map_session_data *user;
  5706. unsigned short msg_len = 0, len = 0;
  5707. unsigned char buf[CHAT_SIZE_MAX];
  5708. if (!channel || !msg)
  5709. return;
  5710. msg_len = (unsigned short)(strlen(msg) + 1);
  5711. if( msg_len > sizeof(buf)-12 ) {
  5712. ShowWarning("clif_channel_msg: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  5713. msg_len = sizeof(buf)-12;
  5714. }
  5715. WBUFW(buf,0) = 0x2C1;
  5716. WBUFW(buf,2) = (len = msg_len + 12);
  5717. WBUFL(buf,4) = 0;
  5718. WBUFL(buf,8) = color;
  5719. safestrncpy(WBUFCP(buf,12), msg, msg_len);
  5720. iter = db_iterator(channel->users);
  5721. for( user = (map_session_data *)dbi_first(iter); dbi_exists(iter); user = (map_session_data *)dbi_next(iter) ) {
  5722. clif_send(buf, len, &user->bl, SELF);
  5723. }
  5724. dbi_destroy(iter);
  5725. }
  5726. /// Displays heal effect.
  5727. /// 013d <var id>.W <amount>.W (ZC_RECOVERY)
  5728. /// 0a27 <var id>.W <amount>.L (ZC_RECOVERY2)
  5729. /// var id:
  5730. /// 5 = HP (SP_HP)
  5731. /// 7 = SP (SP_SP)
  5732. /// ? = ignored
  5733. void clif_heal( map_session_data& sd, int32 type, uint32 val ) {
  5734. PACKET_ZC_RECOVERY packet{};
  5735. packet.packetType = HEADER_ZC_RECOVERY;
  5736. packet.type = static_cast<decltype(packet.type)>(type);
  5737. packet.amount = std::min( static_cast<decltype(packet.amount)>( val ), std::numeric_limits<decltype(packet.amount)>::max() );
  5738. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5739. }
  5740. /// Displays resurrection effect.
  5741. /// 0148 <id>.L <type>.W (ZC_RESURRECTION)
  5742. /// type:
  5743. /// ignored
  5744. void clif_resurrection( block_list& bl ){
  5745. PACKET_ZC_RESURRECTION packet{};
  5746. packet.packetType = HEADER_ZC_RESURRECTION;
  5747. packet.gid = bl.id;
  5748. packet.type = 0;
  5749. clif_send( &packet, sizeof( packet ), &bl, AREA );
  5750. if (disguised(&bl)) {
  5751. clif_spawn(&bl);
  5752. }
  5753. }
  5754. /// Sets the map property
  5755. /// 0199 <type>.W (ZC_NOTIFY_MAPPROPERTY)
  5756. /// 099b <type>.W <flags>.L (ZC_MAPPROPERTY_R2)
  5757. void clif_map_property(struct block_list *bl, enum map_property property, enum send_target t)
  5758. {
  5759. #if PACKETVER >= 20121010
  5760. short cmd = 0x99b;
  5761. unsigned char buf[8];
  5762. #else
  5763. short cmd = 0x199;
  5764. unsigned char buf[4];
  5765. #endif
  5766. WBUFW(buf,0)=cmd;
  5767. WBUFW(buf,2)=property;
  5768. #if PACKETVER >= 20121010
  5769. struct map_data *mapdata = map_getmapdata(bl->m);
  5770. map_session_data *sd = BL_CAST(BL_PC, bl);
  5771. WBUFL(buf,4) = ((mapdata->getMapFlag(MF_PVP) || (sd && sd->duel_group > 0))<<0)| // PARTY - Show attack cursor on non-party members (PvP)
  5772. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<1)|// GUILD - Show attack cursor on non-guild members (GvG)
  5773. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<2)|// SIEGE - Show emblem over characters heads when in GvG (WoE castle)
  5774. ((mapdata->getMapFlag(MF_FORCEMINEFFECT) || mapdata_flag_gvg2(mapdata))<<3)| // USE_SIMPLE_EFFECT - Forces simpler skill effects, like /mineffect command
  5775. ((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
  5776. ((mapdata->getMapFlag(MF_PVP))<<5)| // COUNT_PK - Show the PvP counter
  5777. ((mapdata->getMapFlag(MF_PARTYLOCK))<<6)| // NO_PARTY_FORMATION - Prevents party creation/modification (Might be used for instance dungeons)
  5778. ((mapdata->getMapFlag(MF_BATTLEGROUND))<<7)| // BATTLEFIELD - Unknown (Does something for battlegrounds areas)
  5779. ((mapdata->getMapFlag(MF_NOCOSTUME))<<8)| // DISABLE_COSTUMEITEM - Disable costume sprites
  5780. ((!mapdata->getMapFlag(MF_NOUSECART))<<9)| // USECART - Allow opening cart inventory (Well force it to always allow it)
  5781. ((!mapdata->getMapFlag(MF_NOSUNMOONSTARMIRACLE))<<10); // SUNMOONSTAR_MIRACLE - Allows Star Gladiator's Miracle to activate
  5782. //(1<<11); // Unused bits. 1 - 10 is 0x1 length and 11 is 0x15 length. May be used for future settings.
  5783. #endif
  5784. clif_send(buf,packet_len(cmd),bl,t);
  5785. }
  5786. /// Set the map type.
  5787. /// 01d6 <type>.W (ZC_NOTIFY_MAPPROPERTY2)
  5788. void clif_map_type( map_session_data& sd, e_map_type type ){
  5789. PACKET_ZC_NOTIFY_MAPPROPERTY2 packet{};
  5790. packet.packetType = HEADER_ZC_NOTIFY_MAPPROPERTY2;
  5791. packet.type = static_cast<decltype(packet.type)>(type);
  5792. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5793. }
  5794. /// Updates PvP ranking (ZC_NOTIFY_RANKING).
  5795. /// 019a <id>.L <ranking>.L <total>.L
  5796. void clif_pvpset(map_session_data *sd,int pvprank,int pvpnum,int type)
  5797. {
  5798. if(type == 2) {
  5799. int fd = sd->fd;
  5800. WFIFOHEAD(fd,packet_len(0x19a));
  5801. WFIFOW(fd,0) = 0x19a;
  5802. WFIFOL(fd,2) = sd->bl.id;
  5803. WFIFOL(fd,6) = pvprank;
  5804. WFIFOL(fd,10) = pvpnum;
  5805. WFIFOSET(fd,packet_len(0x19a));
  5806. } else {
  5807. unsigned char buf[32];
  5808. WBUFW(buf,0) = 0x19a;
  5809. WBUFL(buf,2) = sd->bl.id;
  5810. if(sd->sc.option&(OPTION_HIDE|OPTION_CLOAK))
  5811. WBUFL(buf,6) = UINT32_MAX; //On client displays as --
  5812. else
  5813. WBUFL(buf,6) = pvprank;
  5814. WBUFL(buf,10) = pvpnum;
  5815. if(pc_isinvisible(sd) || sd->disguise) //Causes crashes when a 'mob' with pvp info dies.
  5816. clif_send(buf,packet_len(0x19a),&sd->bl,SELF);
  5817. else if(!type)
  5818. clif_send(buf,packet_len(0x19a),&sd->bl,AREA);
  5819. else
  5820. clif_send(buf,packet_len(0x19a),&sd->bl,ALL_SAMEMAP);
  5821. }
  5822. }
  5823. /*==========================================
  5824. *
  5825. *------------------------------------------*/
  5826. void clif_map_property_mapall(int map_idx, enum map_property property)
  5827. {
  5828. struct block_list bl;
  5829. bl.id = 0;
  5830. bl.type = BL_NUL;
  5831. bl.m = map_idx;
  5832. clif_map_property( &bl, property, ALL_SAMEMAP );
  5833. }
  5834. /// Notifies the client about the result of a refine attempt.
  5835. /// 0188 <result>.W <index>.W <refine>.W (ZC_ACK_ITEMREFINING)
  5836. /// result:
  5837. /// 0 = success
  5838. /// 1 = failure
  5839. /// 2 = downgrade
  5840. /// 3 = failure (?)
  5841. void clif_refine( map_session_data& sd, uint16 index, e_ack_itemrefining result ){
  5842. PACKET_ZC_ACK_ITEMREFINING packet{};
  5843. packet.packetType = HEADER_ZC_ACK_ITEMREFINING;
  5844. packet.result = static_cast<decltype(packet.result)>(result);
  5845. packet.index = client_index( index );
  5846. packet.value = static_cast<decltype(packet.value)>( sd.inventory.u.items_inventory[index].refine );
  5847. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5848. }
  5849. /// Notifies the client about the result of a weapon refine attempt (ZC_ACK_WEAPONREFINE).
  5850. /// 0223 <result>.L <nameid>.W
  5851. /// result:
  5852. /// 0 = "weapon upgraded: %s" MsgStringTable[911] in rgb(0,255,255)
  5853. /// 1 = "weapon upgraded: %s" MsgStringTable[912] in rgb(0,205,205)
  5854. /// 2 = "cannot upgrade %s until you level up the upgrade weapon skill" MsgStringTable[913] in rgb(255,200,200)
  5855. /// 3 = "you lack the item %s to upgrade the weapon" MsgStringTable[914] in rgb(255,200,200)
  5856. void clif_upgrademessage( map_session_data* sd, int result, t_itemid item_id ){
  5857. PACKET_ZC_ACK_WEAPONREFINE p = {};
  5858. p.packetType = HEADER_ZC_ACK_WEAPONREFINE;
  5859. p.result = result;
  5860. p.itemId = client_nameid( item_id );
  5861. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  5862. }
  5863. /// Whisper is transmitted to the destination player (ZC_WHISPER).
  5864. /// 0097 <packet len>.W <nick>.24B <message>.?B
  5865. /// 0097 <packet len>.W <nick>.24B <isAdmin>.L <message>.?B (PACKETVER >= 20091104)
  5866. void clif_wis_message(map_session_data* sd, const char* nick, const char* mes, size_t mes_len, int gmlvl){
  5867. PACKET_ZC_WHISPER* p = reinterpret_cast<PACKET_ZC_WHISPER*>( packet_buffer );
  5868. p->PacketType = HEADER_ZC_WHISPER;
  5869. p->PacketLength = sizeof( *p );
  5870. safestrncpy( p->sender, nick, NAME_LENGTH );
  5871. safestrncpy( p->message, mes, mes_len );
  5872. p->PacketLength += static_cast<decltype( p->PacketLength )>( mes_len );
  5873. #if PACKETVER >= 20091104
  5874. map_session_data* ssd = map_nick2sd(nick, false);
  5875. // If it is not a message from the server or a player from another map-server
  5876. if( ssd != nullptr ){
  5877. gmlvl = pc_get_group_level(ssd);
  5878. }
  5879. p->isAdmin = (gmlvl == 99) ? 1 : 0;
  5880. #if PACKETVER_MAIN_NUM >= 20131204 || PACKETVER_RE_NUM >= 20131120 || defined(PACKETVER_ZERO)
  5881. if( ssd != nullptr ){
  5882. p->senderGID = ssd->bl.id;
  5883. }else{
  5884. p->senderGID = 0;
  5885. }
  5886. #endif
  5887. #endif
  5888. clif_send( p, p->PacketLength, &sd->bl, SELF );
  5889. }
  5890. /// Inform the player about the result of his whisper action
  5891. /// 0098 <result>.B (ZC_ACK_WHISPER).
  5892. /// 09df <result>.B <CID>.L (ZC_ACK_WHISPER02).
  5893. /// result:
  5894. /// 0 = success to send whisper
  5895. /// 1 = target character is not loged in
  5896. /// 2 = ignored by target
  5897. /// 3 = everyone ignored by target
  5898. void clif_wis_end( map_session_data& sd, e_ack_whisper result ){
  5899. PACKET_ZC_ACK_WHISPER packet{};
  5900. packet.packetType = HEADER_ZC_ACK_WHISPER;
  5901. packet.result = static_cast<decltype(packet.result)>(result);
  5902. #if PACKETVER >= 20131223
  5903. packet.CID = sd.status.char_id;
  5904. #endif
  5905. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5906. }
  5907. /// Returns character name requested by char_id (ZC_ACK_REQNAME_BYGID).
  5908. /// 0194 <char id>.L <name>.24B
  5909. /// 0af7 <flag>.W <char id>.L <name>.24B
  5910. void clif_solved_charname(int fd, int charid, const char* name)
  5911. {
  5912. #if PACKETVER >= 20180221
  5913. WFIFOHEAD(fd,packet_len(0xaf7));
  5914. WFIFOW(fd,0) = 0xaf7;
  5915. WFIFOW(fd,2) = name[0] ? 3 : 2;
  5916. WFIFOL(fd,4) = charid;
  5917. safestrncpy(WFIFOCP(fd, 8), name, NAME_LENGTH);
  5918. WFIFOSET(fd,packet_len(0x0af7));
  5919. #else
  5920. WFIFOHEAD(fd,packet_len(0x194));
  5921. WFIFOW(fd,0)=0x194;
  5922. WFIFOL(fd,2)=charid;
  5923. safestrncpy(WFIFOCP(fd,6), name, NAME_LENGTH);
  5924. WFIFOSET(fd,packet_len(0x194));
  5925. #endif
  5926. }
  5927. /// Presents a list of items that can be carded/composed (ZC_ITEMCOMPOSITION_LIST).
  5928. /// 017b <packet len>.W { <name id>.W }*
  5929. void clif_use_card(map_session_data *sd,int idx)
  5930. {
  5931. int i,c,ep;
  5932. int fd=sd->fd;
  5933. nullpo_retv(sd);
  5934. if (idx < 0 || idx >= MAX_INVENTORY) //Crash-fix from bad packets.
  5935. return;
  5936. if (!sd->inventory_data[idx] || sd->inventory_data[idx]->type != IT_CARD)
  5937. return; //Avoid parsing invalid item indexes (no card/no item)
  5938. ep=sd->inventory_data[idx]->equip;
  5939. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5940. WFIFOW(fd,0)=0x17b;
  5941. for(i=c=0;i<MAX_INVENTORY;i++){
  5942. int j;
  5943. if(sd->inventory_data[i] == nullptr)
  5944. continue;
  5945. if(sd->inventory_data[i]->type!=IT_WEAPON && sd->inventory_data[i]->type!=IT_ARMOR)
  5946. continue;
  5947. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0])) //Can't slot it
  5948. continue;
  5949. if(sd->inventory.u.items_inventory[i].identify==0 ) //Not identified
  5950. continue;
  5951. if((sd->inventory_data[i]->equip&ep)==0) //Not equippable on this part.
  5952. continue;
  5953. if(sd->inventory_data[i]->type==IT_WEAPON && ep==EQP_SHIELD) //Shield card won't go on left weapon.
  5954. continue;
  5955. 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.
  5956. continue;
  5957. ARR_FIND( 0, sd->inventory_data[i]->slots, j, sd->inventory.u.items_inventory[i].card[j] == 0 );
  5958. if( j == sd->inventory_data[i]->slots ) // No room
  5959. continue;
  5960. if( sd->inventory.u.items_inventory[i].equip > 0 ) // Do not check items that are already equipped
  5961. continue;
  5962. WFIFOW(fd,4+c*2)=i+2;
  5963. c++;
  5964. }
  5965. if( !c ) return; // no item is available for card insertion
  5966. WFIFOW(fd,2)=4+c*2;
  5967. WFIFOSET(fd,WFIFOW(fd,2));
  5968. }
  5969. /// Notifies the client about the result of item carding/composition.
  5970. /// 017d <equip index>.W <card index>.W <result>.B (ZC_ACK_ITEMCOMPOSITION)
  5971. /// result:
  5972. /// 0 = success
  5973. /// 1 = failure
  5974. void clif_insert_card( map_session_data& sd, int32 idx_equip, int32 idx_card, bool failure ){
  5975. PACKET_ZC_ACK_ITEMCOMPOSITION packet{};
  5976. packet.packetType = HEADER_ZC_ACK_ITEMCOMPOSITION;
  5977. packet.equipIndex = client_index( idx_equip );
  5978. packet.cardIndex = client_index( idx_card );
  5979. packet.result = failure;
  5980. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5981. }
  5982. /// Presents a list of items that can be identified (ZC_ITEMIDENTIFY_LIST).
  5983. /// 0177 <packet len>.W { <index>.W }*
  5984. void clif_item_identify_list(map_session_data *sd)
  5985. {
  5986. int i,c;
  5987. int fd;
  5988. nullpo_retv(sd);
  5989. fd=sd->fd;
  5990. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5991. WFIFOW(fd,0)=0x177;
  5992. for(i=c=0;i<MAX_INVENTORY;i++){
  5993. if(sd->inventory.u.items_inventory[i].nameid > 0 && !sd->inventory.u.items_inventory[i].identify){
  5994. WFIFOW(fd,c*2+4)=client_index(i);
  5995. c++;
  5996. }
  5997. }
  5998. if(c > 0) {
  5999. WFIFOW(fd,2)=c*2+4;
  6000. WFIFOSET(fd,WFIFOW(fd,2));
  6001. sd->menuskill_id = MC_IDENTIFY;
  6002. sd->menuskill_val = c;
  6003. sd->state.workinprogress = WIP_DISABLE_ALL;
  6004. }
  6005. }
  6006. /// Notifies the client about the result of a item identify request.
  6007. /// 0179 <index>.W <result>.B (ZC_ACK_ITEMIDENTIFY)
  6008. /// result:
  6009. /// 0 = success
  6010. /// 1 = failure
  6011. void clif_item_identified( map_session_data& sd, int32 idx, bool failure ){
  6012. PACKET_ZC_ACK_ITEMIDENTIFY packet{};
  6013. packet.packetType = HEADER_ZC_ACK_ITEMIDENTIFY;
  6014. packet.index = client_index( idx );
  6015. packet.result = failure;
  6016. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6017. }
  6018. /// Presents a list of items that can be repaired.
  6019. /// 01fc <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }* (ZC_REPAIRITEMLIST)
  6020. void clif_item_repair_list( map_session_data& sd, map_session_data& dstsd, uint16 lv ){
  6021. PACKET_ZC_REPAIRITEMLIST* p = reinterpret_cast<PACKET_ZC_REPAIRITEMLIST*>( packet_buffer );
  6022. p->packetType = HEADER_ZC_REPAIRITEMLIST;
  6023. p->packetLength = sizeof( *p );
  6024. size_t c = 0;
  6025. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  6026. 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)){
  6027. p->items[c].index = static_cast<decltype( p->items[0].index )>( i );
  6028. p->items[c].itemId = client_nameid( dstsd.inventory.u.items_inventory[i].nameid );
  6029. p->items[c].refine = dstsd.inventory.u.items_inventory[i].refine;
  6030. clif_addcards( &p->items[c].slot, &dstsd.inventory.u.items_inventory[i] );
  6031. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  6032. c++;
  6033. }
  6034. }
  6035. if( c > 0 ){
  6036. clif_send( p, p->packetLength, &sd.bl, SELF );
  6037. sd.menuskill_id = BS_REPAIRWEAPON;
  6038. sd.menuskill_val = dstsd.bl.id;
  6039. sd.menuskill_val2 = lv;
  6040. }else{
  6041. clif_skill_fail( sd, sd.ud.skill_id );
  6042. }
  6043. }
  6044. /// Notifies the client about the result of a item repair request.
  6045. /// 01fe <index>.W <result>.B (ZC_ACK_ITEMREPAIR)
  6046. /// index:
  6047. /// ignored (inventory index)
  6048. /// result:
  6049. /// 0 = Item repair success.
  6050. /// 1 = Item repair failure.
  6051. void clif_item_repaireffect( map_session_data& sd, int32 idx, bool failure ){
  6052. PACKET_ZC_ACK_ITEMREPAIR packet{};
  6053. packet.packetType = HEADER_ZC_ACK_ITEMREPAIR;
  6054. packet.index = client_index( idx );
  6055. packet.result = failure;
  6056. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6057. }
  6058. /// Displays a message, that an equipment got damaged.
  6059. /// 02bb <equip location>.W <account id>.L (ZC_EQUIPITEM_DAMAGED)
  6060. void clif_item_damaged( map_session_data& sd, uint16 position ){
  6061. PACKET_ZC_EQUIPITEM_DAMAGED packet{};
  6062. packet.packetType = HEADER_ZC_EQUIPITEM_DAMAGED;
  6063. packet.equipLocation = position;
  6064. packet.GID = sd.bl.id;
  6065. // TODO: the packet seems to be sent to other people as well, probably party and/or guild.
  6066. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6067. }
  6068. /// Presents a list of weapon items that can be refined [Taken from jAthena] (ZC_NOTIFY_WEAPONITEMLIST).
  6069. /// 0221 <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }*
  6070. void clif_item_refine_list( map_session_data& sd ){
  6071. PACKET_ZC_NOTIFY_WEAPONITEMLIST* p = reinterpret_cast<PACKET_ZC_NOTIFY_WEAPONITEMLIST*>( packet_buffer );
  6072. p->packetType = HEADER_ZC_NOTIFY_WEAPONITEMLIST;
  6073. p->packetLength = sizeof( *p );
  6074. uint16 skill_lv = pc_checkskill( &sd, WS_WEAPONREFINE );
  6075. int refine_item[MAX_WEAPON_LEVEL];
  6076. refine_item[0] = pc_search_inventory( &sd, ITEMID_PHRACON );
  6077. refine_item[1] = pc_search_inventory( &sd, ITEMID_EMVERETARCON );
  6078. refine_item[2] = refine_item[3] = pc_search_inventory( &sd, ITEMID_ORIDECON );
  6079. #ifdef RENEWAL
  6080. refine_item[4] = -1;
  6081. #endif
  6082. int count = 0;
  6083. for( int i = 0; i < MAX_INVENTORY; i++ ){
  6084. if( sd.inventory.u.items_inventory[i].nameid > 0 && sd.inventory.u.items_inventory[i].refine < skill_lv &&
  6085. sd.inventory_data[i] != nullptr && sd.inventory_data[i]->type == IT_WEAPON &&
  6086. sd.inventory.u.items_inventory[i].identify && sd.inventory_data[i]->weapon_level >= 1 &&
  6087. refine_item[sd.inventory_data[i]->weapon_level - 1] != -1 && !( sd.inventory.u.items_inventory[i].equip & EQP_ARMS ) ){
  6088. p->items[count].index = client_index( i );
  6089. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  6090. p->items[count].refine = sd.inventory.u.items_inventory[i].refine;
  6091. clif_addcards( &p->items[count].slot, &sd.inventory.u.items_inventory[i] );
  6092. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  6093. count++;
  6094. }
  6095. }
  6096. clif_send( p, p->packetLength, &sd.bl, SELF );
  6097. if( count > 0 ){
  6098. sd.menuskill_id = WS_WEAPONREFINE;
  6099. sd.menuskill_val = skill_lv;
  6100. }
  6101. }
  6102. /// Notification of an auto-casted skill (ZC_AUTORUN_SKILL).
  6103. /// 0147 <skill id>.W <type>.L <level>.W <sp cost>.W <atk range>.W <skill name>.24B <upgradable>.B
  6104. void clif_item_skill( map_session_data *sd, uint16 skill_id, uint16 skill_lv ){
  6105. nullpo_retv( sd );
  6106. PACKET_ZC_AUTORUN_SKILL p = {};
  6107. p.packetType = HEADER_ZC_AUTORUN_SKILL;
  6108. p.skill_id = skill_id;
  6109. p.skill_type = skill_get_inf( skill_id );
  6110. p.skill_lv = skill_lv;
  6111. p.skill_sp = skill_get_sp( skill_id, skill_lv );
  6112. p.skill_range = skill_get_range2( &sd->bl, skill_id, skill_lv, false );
  6113. safestrncpy( p.skill_name, skill_get_name( skill_id ), NAME_LENGTH );
  6114. p.up_flag = false;
  6115. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6116. }
  6117. /// Adds an item to character's cart.
  6118. /// 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)
  6119. /// 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)
  6120. /// 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)
  6121. void clif_cart_additem( map_session_data *sd, int n, int amount ){
  6122. nullpo_retv( sd );
  6123. int fd = sd->fd;
  6124. if( !session_isActive( fd ) ){
  6125. return;
  6126. }
  6127. if( n < 0 || n >= MAX_CART || sd->cart.u.items_cart[n].nameid == 0 ){
  6128. return;
  6129. }
  6130. PACKET_ZC_ADD_ITEM_TO_CART p = {};
  6131. p.packetType = HEADER_ZC_ADD_ITEM_TO_CART;
  6132. p.index = client_index( n );
  6133. p.amount = amount;
  6134. p.itemId = client_nameid( sd->cart.u.items_cart[n].nameid );
  6135. #if PACKETVER >= 5
  6136. p.itemType = itemdb_type( sd->cart.u.items_cart[n].nameid );
  6137. #endif
  6138. p.identified = sd->cart.u.items_cart[n].identify;
  6139. p.damaged = sd->cart.u.items_cart[n].attribute;
  6140. p.refine = sd->cart.u.items_cart[n].refine;
  6141. clif_addcards( &p.slot, &sd->cart.u.items_cart[n] );
  6142. #if PACKETVER >= 20150226
  6143. clif_add_random_options( p.option_data, sd->cart.u.items_cart[n] );
  6144. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6145. p.grade = sd->cart.u.items_cart[n].enchantgrade;
  6146. #endif
  6147. #endif
  6148. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6149. }
  6150. /// Acknowledge an item have been added to cart
  6151. /// 012c <result>.B (ZC_ACK_ADDITEM_TO_CART)
  6152. /// result:
  6153. /// 0 = ADDITEM_TO_CART_FAIL_WEIGHT
  6154. /// 1 = ADDITEM_TO_CART_FAIL_COUNT
  6155. void clif_cart_additem_ack( map_session_data& sd, e_ack_additem_to_cart flag ){
  6156. PACKET_ZC_ACK_ADDITEM_TO_CART packet{};
  6157. packet.packetType = HEADER_ZC_ACK_ADDITEM_TO_CART;
  6158. packet.result = static_cast<decltype(packet.result)>(flag);
  6159. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6160. }
  6161. // 09B7 <unknow data> (ZC_ACK_OPEN_BANKING)
  6162. void clif_bank_open( map_session_data& sd ){
  6163. #if PACKETVER >= 20130717
  6164. PACKET_ZC_ACK_OPEN_BANKING p = {};
  6165. p.packetType = HEADER_ZC_ACK_OPEN_BANKING;
  6166. p.unknown = 0;
  6167. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6168. #endif
  6169. }
  6170. /*
  6171. * Request to Open the banking system
  6172. * 09B6 <aid>L (CZ_REQ_OPEN_BANKING)
  6173. */
  6174. void clif_parse_BankOpen(int fd, map_session_data* sd) {
  6175. #if PACKETVER >= 20130717
  6176. const PACKET_CZ_REQ_OPEN_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BANKING*>( RFIFOP( fd, 0 ) );
  6177. //TODO check if preventing trade or stuff like that
  6178. //also mark something in case char ain't available for saving, should we check now ?
  6179. nullpo_retv(sd);
  6180. if( !battle_config.feature_banking ) {
  6181. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6182. return;
  6183. }
  6184. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6185. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6186. return;
  6187. }
  6188. else {
  6189. if(sd->status.account_id == p->AID){
  6190. sd->state.banking = 1;
  6191. //request save ?
  6192. //chrif_bankdata_request(sd->status.account_id, sd->status.char_id);
  6193. //on succes open bank ?
  6194. clif_bank_open( *sd );
  6195. }
  6196. }
  6197. #endif
  6198. }
  6199. // 09B9 <unknow data> (ZC_ACK_CLOSE_BANKING)
  6200. void clif_bank_close( map_session_data& sd ){
  6201. #if PACKETVER >= 20130717
  6202. PACKET_ZC_ACK_CLOSE_BANKING p = {};
  6203. p.packetType = HEADER_ZC_ACK_CLOSE_BANKING;
  6204. p.unknown = 0;
  6205. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6206. #endif
  6207. }
  6208. /*
  6209. * Request to close the banking system
  6210. * 09B8 <aid>L (CZ_REQ_CLOSE_BANKING)
  6211. */
  6212. void clif_parse_BankClose(int fd, map_session_data* sd) {
  6213. #if PACKETVER >= 20130717
  6214. const PACKET_CZ_REQ_CLOSE_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_CLOSE_BANKING*>( RFIFOP( fd, 0 ) );
  6215. nullpo_retv(sd);
  6216. if( !battle_config.feature_banking ) {
  6217. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6218. //still allow to go trough to not stuck player if we have disable it while they was in
  6219. }
  6220. if(sd->status.account_id == p->AID){
  6221. sd->state.banking = 0;
  6222. clif_bank_close( *sd );
  6223. }
  6224. #endif
  6225. }
  6226. /*
  6227. * Display how much we got in bank (I suppose)
  6228. * 09A6 <Bank_Vault>Q <Reason>W (ZC_BANKING_CHECK)
  6229. */
  6230. void clif_Bank_Check( map_session_data& sd ){
  6231. #if PACKETVER >= 20130717
  6232. PACKET_ZC_BANKING_CHECK p = {};
  6233. p.packetType = HEADER_ZC_BANKING_CHECK;
  6234. p.money = sd.bank_vault;
  6235. p.reason = 0;
  6236. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6237. #endif
  6238. }
  6239. /*
  6240. * Requesting the data in bank
  6241. * 09AB <aid>L (CZ_REQ_BANKING_CHECK)
  6242. */
  6243. void clif_parse_BankCheck(int fd, map_session_data* sd) {
  6244. #if PACKETVER >= 20130717
  6245. const PACKET_CZ_REQ_BANKING_CHECK* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_CHECK*>( RFIFOP( fd, 0 ) );
  6246. nullpo_retv(sd);
  6247. if( !battle_config.feature_banking ) {
  6248. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6249. return;
  6250. }
  6251. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6252. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6253. return;
  6254. }
  6255. else {
  6256. if(sd->status.account_id == p->AID) //since we have it let check it for extra security
  6257. clif_Bank_Check( *sd );
  6258. }
  6259. #endif
  6260. }
  6261. /*
  6262. * Acknowledge of deposit some money in bank
  6263. 09A8 <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_DEPOSIT)
  6264. */
  6265. void clif_bank_deposit( map_session_data& sd, enum e_BANKING_DEPOSIT_ACK reason ){
  6266. #if PACKETVER >= 20130717
  6267. PACKET_ZC_ACK_BANKING_DEPOSIT p = {};
  6268. p.packetType = HEADER_ZC_ACK_BANKING_DEPOSIT;
  6269. p.money = sd.bank_vault;
  6270. p.zeny = sd.status.zeny;
  6271. p.reason = reason;
  6272. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6273. #endif
  6274. }
  6275. /*
  6276. * Request saving some money in bank
  6277. * @author : original [Yommy/Hercules]
  6278. * 09A7 <AID>L <Money>L (CZ_REQ_BANKING_DEPOSIT)
  6279. */
  6280. void clif_parse_BankDeposit(int fd, map_session_data* sd) {
  6281. #if PACKETVER >= 20130717
  6282. const PACKET_CZ_REQ_BANKING_DEPOSIT* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_DEPOSIT*>( RFIFOP( fd, 0 ) );
  6283. nullpo_retv(sd);
  6284. if( !battle_config.feature_banking ) {
  6285. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6286. return;
  6287. }
  6288. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6289. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6290. return;
  6291. }
  6292. else {
  6293. if(sd->status.account_id == p->AID){
  6294. enum e_BANKING_DEPOSIT_ACK reason = pc_bank_deposit(sd,max(0,p->zeny));
  6295. clif_bank_deposit( *sd, reason );
  6296. }
  6297. }
  6298. #endif
  6299. }
  6300. /*
  6301. * Acknowledge of withdrawing some money from bank
  6302. * 09AA <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_WITHDRAW)
  6303. */
  6304. void clif_bank_withdraw( map_session_data& sd, enum e_BANKING_WITHDRAW_ACK reason ){
  6305. #if PACKETVER >= 20130717
  6306. PACKET_ZC_ACK_BANKING_WITHDRAW p = {};
  6307. p.packetType = HEADER_ZC_ACK_BANKING_WITHDRAW;
  6308. p.reason = reason;
  6309. p.money = sd.bank_vault;
  6310. p.zeny = sd.status.zeny;
  6311. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6312. #endif
  6313. }
  6314. /*
  6315. * Request Withdrawing some money from bank
  6316. * 09A9 <AID>L <Money>L (CZ_REQ_BANKING_WITHDRAW)
  6317. */
  6318. void clif_parse_BankWithdraw(int fd, map_session_data* sd) {
  6319. #if PACKETVER >= 20130717
  6320. const PACKET_CZ_REQ_BANKING_WITHDRAW* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_WITHDRAW*>( RFIFOP( fd, 0 ) );
  6321. nullpo_retv(sd);
  6322. if( !battle_config.feature_banking ) {
  6323. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6324. return;
  6325. }
  6326. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6327. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6328. return;
  6329. }
  6330. else {
  6331. if(sd->status.account_id == p->AID){
  6332. enum e_BANKING_WITHDRAW_ACK reason = pc_bank_withdraw(sd,max(0,p->zeny));
  6333. clif_bank_withdraw( *sd, reason );
  6334. }
  6335. }
  6336. #endif
  6337. }
  6338. /// Deletes an item from character's cart.
  6339. /// 0125 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_CART)
  6340. void clif_cart_delitem( map_session_data& sd, int32 index, int32 amount ){
  6341. PACKET_ZC_DELETE_ITEM_FROM_CART packet{};
  6342. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_CART;
  6343. packet.index = client_index( index );
  6344. packet.amount = amount;
  6345. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6346. }
  6347. /// Opens the shop creation menu.
  6348. /// 012d <num>.W (ZC_OPENSTORE)
  6349. /// num:
  6350. /// number of allowed item slots
  6351. void clif_openvendingreq( map_session_data& sd, uint16 num ){
  6352. PACKET_ZC_OPENSTORE packet{};
  6353. packet.packetType = HEADER_ZC_OPENSTORE;
  6354. packet.num = num;
  6355. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6356. }
  6357. /// Displays a vending board to target/area (ZC_STORE_ENTRY).
  6358. /// 0131 <owner id>.L <message>.80B
  6359. void clif_showvendingboard( map_session_data& sd, enum send_target target, struct block_list* tbl ){
  6360. if( tbl == nullptr ){
  6361. tbl = &sd.bl;
  6362. target = AREA_WOS;
  6363. }
  6364. PACKET_ZC_STORE_ENTRY p = {};
  6365. p.packetType = HEADER_ZC_STORE_ENTRY;
  6366. p.makerAID = sd.status.account_id;
  6367. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  6368. clif_send( &p, sizeof( p ), tbl, target );
  6369. }
  6370. /// Removes a vending board from screen (ZC_DISAPPEAR_ENTRY).
  6371. /// 0132 <owner id>.L
  6372. void clif_closevendingboard(struct block_list* bl, int fd)
  6373. {
  6374. unsigned char buf[16];
  6375. nullpo_retv(bl);
  6376. WBUFW(buf,0) = 0x132;
  6377. WBUFL(buf,2) = bl->id;
  6378. if( session_isActive(fd) ) {
  6379. WFIFOHEAD(fd,packet_len(0x132));
  6380. memcpy(WFIFOP(fd,0),buf,packet_len(0x132));
  6381. WFIFOSET(fd,packet_len(0x132));
  6382. } else {
  6383. clif_send(buf,packet_len(0x132),bl,AREA_WOS);
  6384. }
  6385. }
  6386. /// Sends a list of items in a shop.
  6387. /// 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)
  6388. /// 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)
  6389. void clif_vendinglist( map_session_data& sd, map_session_data& vsd ){
  6390. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC*>( packet_buffer );
  6391. p->packetType = HEADER_ZC_PC_PURCHASE_ITEMLIST_FROMMC;
  6392. p->packetLength = sizeof( *p );
  6393. p->AID = vsd.status.account_id;
  6394. #if PACKETVER >= 20100105
  6395. p->venderId = vsd.vender_id;
  6396. #endif
  6397. for( int i = 0, count = 0; i < vsd.vend_num; i++ ){
  6398. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC_sub& entry = p->items[count];
  6399. int index = vsd.vending[i].index;
  6400. struct item_data* data = itemdb_search( vsd.cart.u.items_cart[index].nameid );
  6401. if( data == nullptr ){
  6402. continue;
  6403. }
  6404. entry.price = vsd.vending[i].value;
  6405. entry.amount = vsd.vending[i].amount;
  6406. entry.index = client_index( index );
  6407. entry.itemType = itemtype( vsd.cart.u.items_cart[index].nameid );
  6408. entry.itemId = client_nameid( vsd.cart.u.items_cart[index].nameid );
  6409. entry.identified = vsd.cart.u.items_cart[index].identify;
  6410. entry.damaged = vsd.cart.u.items_cart[index].attribute;
  6411. entry.refine = vsd.cart.u.items_cart[index].refine;
  6412. clif_addcards( &entry.slot, &vsd.cart.u.items_cart[index] );
  6413. #if PACKETVER >= 20150226
  6414. clif_add_random_options( entry.option_data, vsd.cart.u.items_cart[index] );
  6415. #if PACKETVER >= 20160921
  6416. entry.location = pc_equippoint_sub( &sd, data );
  6417. entry.viewSprite = data->look;
  6418. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6419. entry.grade = vsd.cart.u.items_cart[index].enchantgrade;
  6420. #endif
  6421. #endif
  6422. #endif
  6423. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6424. count++;
  6425. }
  6426. clif_send( p, p->packetLength, &sd.bl, SELF );
  6427. }
  6428. /// Shop purchase failure.
  6429. /// 0135 <index>.W <amount>.W <result>.B (ZC_PC_PURCHASE_RESULT_FROMMC)
  6430. /// result:
  6431. /// 0 = success
  6432. /// 1 = not enough zeny
  6433. /// 2 = overweight
  6434. /// 4 = out of stock
  6435. /// 5 = "cannot use an npc shop while in a trade"
  6436. /// 6 = Because the store information was incorrect the item was not purchased.
  6437. /// 7 = No sales information.
  6438. void clif_buyvending( map_session_data& sd, uint16 index, uint16 amount, e_pc_purchase_result_frommc result ){
  6439. PACKET_ZC_PC_PURCHASE_RESULT_FROMMC packet{};
  6440. packet.packetType = HEADER_ZC_PC_PURCHASE_RESULT_FROMMC;
  6441. packet.index = client_index( index );
  6442. packet.amount = amount;
  6443. packet.result = static_cast<decltype(packet.result)>(result);
  6444. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6445. }
  6446. /// Show's vending player its list of items for sale.
  6447. /// 0a28 <result>.B (ZC_ACK_OPENSTORE2)
  6448. void clif_openvending_ack( map_session_data& sd, e_ack_openstore2 result ){
  6449. #if PACKETVER >= 20141022
  6450. PACKET_ZC_ACK_OPENSTORE2 packet{};
  6451. packet.packetType = HEADER_ZC_ACK_OPENSTORE2;
  6452. packet.result = static_cast<decltype(packet.result)>(result);
  6453. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6454. #endif
  6455. }
  6456. /// Shop creation success.
  6457. /// 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)
  6458. void clif_openvending( map_session_data& sd ){
  6459. PACKET_ZC_PC_PURCHASE_MYITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_MYITEMLIST*>( packet_buffer );
  6460. p->packetType = HEADER_ZC_PC_PURCHASE_MYITEMLIST;
  6461. p->packetLength = sizeof( *p );
  6462. p->AID = sd.bl.id;
  6463. for( int i = 0; i < sd.vend_num; i++ ) {
  6464. PACKET_ZC_PC_PURCHASE_MYITEMLIST_sub& entry = p->items[i];
  6465. int16 index = sd.vending[i].index;
  6466. entry.price = sd.vending[i].value;
  6467. entry.index = client_index( index );
  6468. entry.amount = sd.vending[i].amount;
  6469. entry.itemType = itemtype( sd.cart.u.items_cart[index].nameid );
  6470. entry.itemId = client_nameid( sd.cart.u.items_cart[index].nameid );
  6471. entry.identified = sd.cart.u.items_cart[index].identify;
  6472. entry.damaged = sd.cart.u.items_cart[index].attribute;
  6473. entry.refine = sd.cart.u.items_cart[index].refine;
  6474. clif_addcards( &entry.slot, &sd.cart.u.items_cart[index] );
  6475. #if PACKETVER >= 20150226
  6476. clif_add_random_options( entry.option_data, sd.cart.u.items_cart[index] );
  6477. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6478. entry.grade = sd.cart.u.items_cart[index].enchantgrade;
  6479. #endif
  6480. #endif
  6481. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6482. }
  6483. clif_send( p, p->packetLength, &sd.bl, SELF );
  6484. clif_openvending_ack( sd, OPENSTORE2_SUCCESS );
  6485. }
  6486. /// Inform merchant that someone has bought an item.
  6487. /// 0137 <index>.W <amount>.W (ZC_DELETEITEM_FROM_MCSTORE)
  6488. /// 09e5 <index>.W <amount>.W <GID>.L <Date>.L <zeny>.L (ZC_DELETEITEM_FROM_MCSTORE2)
  6489. void clif_vendingreport( map_session_data& sd, uint16 index, uint16 amount, uint32 char_id, int32 zeny ){
  6490. PACKET_ZC_DELETEITEM_FROM_MCSTORE packet{};
  6491. packet.packetType = HEADER_ZC_DELETEITEM_FROM_MCSTORE;
  6492. packet.index = client_index( index );
  6493. packet.amount = amount;
  6494. // TODO : not sure for client date [Napster]
  6495. #if PACKETVER >= 20141016
  6496. packet.buyerCID = char_id;
  6497. packet.date = client_tick( time(nullptr) );
  6498. packet.zeny = zeny;
  6499. #endif
  6500. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6501. }
  6502. /// Result of organizing a party (ZC_ACK_MAKE_GROUP).
  6503. /// 00fa <result>.B
  6504. /// result:
  6505. /// 0 = opens party window and shows MsgStringTable[77]="party successfully organized"
  6506. /// 1 = MsgStringTable[78]="party name already exists"
  6507. /// 2 = MsgStringTable[79]="already in a party"
  6508. /// 3 = cannot organize parties on this map
  6509. /// ? = nothing
  6510. void clif_party_created( map_session_data& sd, int result ){
  6511. PACKET_ZC_ACK_MAKE_GROUP p = {};
  6512. p.PacketType = HEADER_ZC_ACK_MAKE_GROUP;
  6513. p.result = result;
  6514. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6515. }
  6516. /// Adds new member to a party.
  6517. /// 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)
  6518. /// 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)
  6519. /// 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)
  6520. /// role:
  6521. /// 0 = leader
  6522. /// 1 = normal
  6523. /// state:
  6524. /// 0 = connected
  6525. /// 1 = disconnected
  6526. void clif_party_member_info( struct party_data& party, map_session_data& sd ){
  6527. int id = party_getmemberid( &party, &sd );
  6528. if( id < 0 ){
  6529. return;
  6530. }
  6531. PACKET_ZC_ADD_MEMBER_TO_GROUP p = {};
  6532. p.packetType = partymemberinfo;
  6533. p.AID = sd.status.account_id;
  6534. #if PACKETVER >= 20171207
  6535. p.GID = sd.status.char_id;
  6536. #endif
  6537. p.leader = ( party.party.member[id].leader != 0 ) ? 0 : 1;
  6538. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6539. p.class_ = sd.status.class_;
  6540. p.baseLevel = sd.status.base_level;
  6541. #endif
  6542. p.x = sd.bl.x;
  6543. p.y = sd.bl.y;
  6544. p.offline = ( party.party.member[id].online != 0 ) ? 0 : 1;
  6545. safestrncpy( p.partyName, party.party.name, sizeof( p.partyName ) );
  6546. safestrncpy( p.playerName, sd.status.name, sizeof( p.playerName ) );
  6547. mapindex_getmapname_ext( map_mapid2mapname( sd.bl.m ), p.mapName );
  6548. p.sharePickup = ( party.party.item&1 ) ? 1 : 0;
  6549. p.shareLoot = ( party.party.item&2 )? 1 : 0;
  6550. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6551. }
  6552. /// Sends party information (ZC_GROUP_LIST).
  6553. /// 00fb <packet len>.W <party name>.24B { <account id>.L <nick>.24B <map name>.16B <role>.B <state>.B }*
  6554. /// role:
  6555. /// 0 = leader
  6556. /// 1 = normal
  6557. /// state:
  6558. /// 0 = connected
  6559. /// 1 = disconnected
  6560. /// 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
  6561. void clif_party_info( struct party_data& party, map_session_data* sd ){
  6562. send_target target;
  6563. if( sd == nullptr ){
  6564. sd = party_getavailablesd( &party );
  6565. if( sd == nullptr ){
  6566. // Nobody is online to notify
  6567. return;
  6568. }
  6569. // Send to whole party
  6570. target = PARTY;
  6571. }else{
  6572. // Send only to self
  6573. target = SELF;
  6574. }
  6575. PACKET_ZC_GROUP_LIST* p = reinterpret_cast<PACKET_ZC_GROUP_LIST*>( packet_buffer );
  6576. p->packetType = partyinfo;
  6577. p->packetLen = sizeof( *p );
  6578. safestrncpy( p->partyName, party.party.name, sizeof( p->partyName ) );
  6579. for( int i = 0, c = 0; i < MAX_PARTY; i++ ){
  6580. struct party_member& m = party.party.member[i];
  6581. if( m.account_id == 0 ){
  6582. continue;
  6583. }
  6584. PACKET_ZC_GROUP_LIST_SUB& member = p->members[c];
  6585. member.AID = m.account_id;
  6586. #if PACKETVER >= 20171207
  6587. member.GID = m.char_id;
  6588. #endif
  6589. safestrncpy( member.playerName, m.name, sizeof( member.playerName ) );
  6590. mapindex_getmapname_ext( m.map, member.mapName );
  6591. member.leader = ( m.leader ) ? 0 : 1;
  6592. member.offline = ( m.online ) ? 0 : 1;
  6593. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6594. member.class_ = m.class_;
  6595. member.baseLevel = m.lv;
  6596. #endif
  6597. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( member ) );
  6598. c++;
  6599. }
  6600. clif_send( p, p->packetLen, &sd->bl, target );
  6601. }
  6602. /// The player's 'party invite' state, sent during login (ZC_PARTY_CONFIG).
  6603. /// 02c9 <flag>.B
  6604. /// flag:
  6605. /// 0 = allow party invites
  6606. /// 1 = auto-deny party invites
  6607. void clif_partyinvitationstate( map_session_data& sd ){
  6608. #if PACKETVER_MAIN_NUM >= 20070911 || defined(PACKETVER_RE) || PACKETVER_AD_NUM >= 20070911 || PACKETVER_SAK_NUM >= 20070904 || defined(PACKETVER_ZERO)
  6609. PACKET_ZC_PARTY_CONFIG p = {};
  6610. p.packetType = HEADER_ZC_PARTY_CONFIG;
  6611. p.denyPartyInvites = sd.status.disable_partyinvite;
  6612. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6613. #endif
  6614. }
  6615. /// Party invitation request.
  6616. /// 00fe <party id>.L <party name>.24B (ZC_REQ_JOIN_GROUP)
  6617. /// 02c6 <party id>.L <party name>.24B (ZC_PARTY_JOIN_REQ)
  6618. void clif_party_invite( map_session_data& sd, map_session_data& tsd ){
  6619. struct party_data* party = party_search( sd.status.party_id );
  6620. if( party == nullptr ){
  6621. return;
  6622. }
  6623. PACKET_ZC_PARTY_JOIN_REQ p = {};
  6624. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ;
  6625. p.GRID = party->party.party_id;
  6626. safestrncpy( p.groupName, party->party.name, sizeof( p.groupName ) );
  6627. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6628. }
  6629. /// Party invite result.
  6630. /// 00fd <nick>.24S <result>.B (ZC_ACK_REQ_JOIN_GROUP)
  6631. /// 02c5 <nick>.24S <result>.L (ZC_PARTY_JOIN_REQ_ACK)
  6632. /// result=0 : char is already in a party -> MsgStringTable[80]
  6633. /// result=1 : party invite was rejected -> MsgStringTable[81]
  6634. /// result=2 : party invite was accepted -> MsgStringTable[82]
  6635. /// result=3 : party is full -> MsgStringTable[83]
  6636. /// result=4 : char of the same account already joined the party -> MsgStringTable[608]
  6637. /// result=5 : char blocked party invite -> MsgStringTable[1324] (since 20070904)
  6638. /// result=7 : char is not online or doesn't exist -> MsgStringTable[71] (since 20070904)
  6639. /// result=8 : (%s) are currently in restricted map to join a party. -> MsgStringTable[1388] (since 20080527)
  6640. /// result=9 : Cannot join a party in this map. -> MsgStringTable[1871] (since 20110215)
  6641. /// result=10 : You cannot invite or withdraw while in memorial dungeon -> message: MSG_ID_BD3 (since 20161130)
  6642. /// result=11 : The character is a level that can not join the party -> message: MSG_ID_C9A (since 20170412)
  6643. void clif_party_invite_reply( map_session_data& sd, const char* nick, enum e_party_invite_reply reply ){
  6644. #if PACKETVER < 20070904
  6645. if( reply == PARTY_REPLY_OFFLINE ){
  6646. clif_displaymessage( sd.fd, msg_txt( &sd, 3 ) ); // Character not found.
  6647. return;
  6648. }
  6649. #endif
  6650. PACKET_ZC_PARTY_JOIN_REQ_ACK p = {};
  6651. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ_ACK;
  6652. safestrncpy( p.characterName, nick, sizeof( p.characterName ) );
  6653. p.result = reply;
  6654. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6655. }
  6656. /// Updates party settings.
  6657. /// 0101 <exp option>.L (ZC_GROUPINFO_CHANGE)
  6658. /// 07d8 <exp option>.L <item pick rule>.B <item share rule>.B (ZC_REQ_GROUPINFO_CHANGE_V2)
  6659. /// exp option:
  6660. /// 0 = exp sharing disabled
  6661. /// 1 = exp sharing enabled
  6662. /// 2 = cannot change exp sharing
  6663. ///
  6664. /// flag:
  6665. /// 0 = send to party
  6666. /// 1 = send to sd
  6667. void clif_party_option(struct party_data *p,map_session_data *sd,int flag)
  6668. {
  6669. unsigned char buf[16];
  6670. #if PACKETVER < 20090603
  6671. const int cmd = 0x101;
  6672. #else
  6673. const int cmd = 0x7d8;
  6674. #endif
  6675. nullpo_retv(p);
  6676. if(!sd && flag==0){
  6677. int i;
  6678. ARR_FIND(0,MAX_PARTY,i,p->data[i].sd);
  6679. if (i < MAX_PARTY)
  6680. sd = p->data[i].sd;
  6681. }
  6682. if(!sd) return;
  6683. WBUFW(buf,0)=cmd;
  6684. WBUFL(buf,2)=((flag&0x01)?2:p->party.exp);
  6685. #if PACKETVER >= 20090603
  6686. WBUFB(buf,6)=(p->party.item&1)?1:0;
  6687. WBUFB(buf,7)=(p->party.item&2)?1:0;
  6688. #endif
  6689. if(flag==0)
  6690. clif_send(buf,packet_len(cmd),&sd->bl,PARTY);
  6691. else
  6692. clif_send(buf,packet_len(cmd),&sd->bl,SELF);
  6693. }
  6694. /**
  6695. * 0105 <account id>.L <char name>.24B <result>.B (ZC_DELETE_MEMBER_FROM_GROUP).
  6696. * @param sd Send the notification for this player
  6697. * @param account_id Account ID of kicked member
  6698. * @param name Name of kicked member
  6699. * @param result Party leave result @see PARTY_MEMBER_WITHDRAW
  6700. * @param target Send target
  6701. **/
  6702. void clif_party_withdraw( map_session_data& sd, uint32 account_id, const char* name, enum e_party_member_withdraw result, enum send_target target ){
  6703. PACKET_ZC_DELETE_MEMBER_FROM_GROUP p = {};
  6704. p.PacketType = HEADER_ZC_DELETE_MEMBER_FROM_GROUP;
  6705. p.AID = account_id;
  6706. safestrncpy( p.characterName, name, sizeof( p.characterName ) );
  6707. p.result = result;
  6708. clif_send( &p, sizeof( p ), &sd.bl, target );
  6709. }
  6710. /// Party chat message (ZC_NOTIFY_CHAT_PARTY).
  6711. /// 0109 <packet len>.W <account id>.L <message>.?B
  6712. void clif_party_message( struct party_data& party, uint32 account_id, const char* mes, size_t len ){
  6713. map_session_data* sd = party_getavailablesd( &party );
  6714. if( sd == nullptr ){
  6715. return;
  6716. }
  6717. if (len > CHAT_SIZE_MAX) {
  6718. ShowWarning( "clif_party_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", party_id=%d).\n", mes, len, CHAT_SIZE_MAX, party.party.party_id );
  6719. len = CHAT_SIZE_MAX;
  6720. }
  6721. PACKET_ZC_NOTIFY_CHAT_PARTY* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT_PARTY*>( packet_buffer );
  6722. p->PacketType = HEADER_ZC_NOTIFY_CHAT_PARTY;
  6723. p->PacketLength = sizeof( *p );
  6724. p->AID = account_id;
  6725. safestrncpy( p->chatMsg, mes, len );
  6726. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  6727. clif_send( p, p->PacketLength, &sd->bl, PARTY );
  6728. }
  6729. /// Updates the position of a party member on the minimap (ZC_NOTIFY_POSITION_TO_GROUPM).
  6730. /// 0107 <account id>.L <x>.W <y>.W
  6731. void clif_party_xy( map_session_data& sd ){
  6732. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6733. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6734. p.AID = sd.status.account_id;
  6735. p.xPos = sd.bl.x;
  6736. p.yPos = sd.bl.y;
  6737. clif_send( &p, sizeof( p ), &sd.bl, PARTY_SAMEMAP_WOS );
  6738. }
  6739. /*==========================================
  6740. * Sends x/y dot to a single fd. [Skotlex]
  6741. *------------------------------------------*/
  6742. void clif_party_xy_single( map_session_data& sd, map_session_data& tsd ){
  6743. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6744. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6745. p.AID = sd.status.account_id;
  6746. p.xPos = sd.bl.x;
  6747. p.yPos = sd.bl.y;
  6748. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6749. }
  6750. /// Updates HP bar of a party member.
  6751. /// 0106 <account id>.L <hp>.W <max hp>.W (ZC_NOTIFY_HP_TO_GROUPM)
  6752. /// 080e <account id>.L <hp>.L <max hp>.L (ZC_NOTIFY_HP_TO_GROUPM_R2)
  6753. void clif_party_hp( map_session_data& sd ){
  6754. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6755. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6756. p.AID = sd.status.account_id;
  6757. #if PACKETVER < 20100119
  6758. // To correctly display the %hp bar. [Skotlex]
  6759. if( sd.battle_status.max_hp > INT16_MAX ){
  6760. p.hp = sd.battle_status.hp / ( sd.battle_status.max_hp / 100 );
  6761. p.maxhp = 100;
  6762. }else{
  6763. p.hp = sd.battle_status.hp;
  6764. p.maxhp = sd.battle_status.max_hp;
  6765. }
  6766. #else
  6767. p.hp = sd.battle_status.hp;
  6768. p.maxhp = sd.battle_status.max_hp;
  6769. #endif
  6770. clif_send( &p, sizeof( p ), &sd.bl, PARTY_AREA_WOS );
  6771. }
  6772. /// Notifies the party members of a character's death or revival.
  6773. /// 0AB2 <GID>.L <dead>.B
  6774. void clif_party_dead( map_session_data& sd ){
  6775. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6776. PACKET_ZC_GROUP_ISALIVE p = {};
  6777. p.packetType = HEADER_ZC_GROUP_ISALIVE;
  6778. p.AID = sd.status.account_id;
  6779. p.isDead = pc_isdead( &sd );
  6780. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6781. #endif
  6782. }
  6783. /// Updates the job and level of a party member
  6784. /// 0abd <account id>.L <job>.W <level>.W
  6785. void clif_party_job_and_level( map_session_data& sd ){
  6786. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  6787. PACKET_ZC_NOTIFY_MEMBERINFO_TO_GROUPM p = {};
  6788. p.PacketType = HEADER_ZC_NOTIFY_MEMBERINFO_TO_GROUPM;
  6789. p.AID = sd.status.account_id;
  6790. p.job = sd.status.class_;
  6791. p.level = sd.status.base_level;
  6792. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6793. #endif
  6794. }
  6795. /*==========================================
  6796. * Sends HP bar to a single fd. [Skotlex]
  6797. *------------------------------------------*/
  6798. void clif_hpmeter_single( map_session_data& sd, uint32 id, uint32 hp, uint32 maxhp ){
  6799. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6800. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6801. p.AID = id;
  6802. #if PACKETVER < 20100119
  6803. // To correctly display the %hp bar. [Skotlex]
  6804. if( maxhp > INT16_MAX ){
  6805. p.hp = hp / ( maxhp / 100 );
  6806. p.maxhp = 100;
  6807. }else{
  6808. p.hp = hp;
  6809. p.maxhp = maxhp;
  6810. }
  6811. #else
  6812. p.hp = hp;
  6813. p.maxhp = maxhp;
  6814. #endif
  6815. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6816. }
  6817. /// Notifies the client, that it's attack target is too far.
  6818. /// 0139 <target id>.L <target x>.W <target y>.W <x>.W <y>.W <atk range>.W (ZC_ATTACK_FAILURE_FOR_DISTANCE)
  6819. void clif_movetoattack( map_session_data& sd, block_list& bl ){
  6820. PACKET_ZC_ATTACK_FAILURE_FOR_DISTANCE packet{};
  6821. packet.PacketType = HEADER_ZC_ATTACK_FAILURE_FOR_DISTANCE;
  6822. packet.targetAID = bl.id;
  6823. packet.targetXPos = bl.x;
  6824. packet.targetYPos = bl.y;
  6825. packet.xPos = sd.bl.x;
  6826. packet.yPos = sd.bl.y;
  6827. packet.currentAttRange = sd.battle_status.rhw.range;
  6828. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6829. }
  6830. /// Notifies the client about the result of an item produce request (ZC_ACK_REQMAKINGITEM).
  6831. /// 018f <result>.W <name id>.W
  6832. /// result:
  6833. /// 0 = success
  6834. /// 1 = failure
  6835. /// 2 = success (alchemist)
  6836. /// 3 = failure (alchemist)
  6837. void clif_produceeffect(map_session_data* sd,int flag, t_itemid nameid){
  6838. nullpo_retv( sd );
  6839. clif_solved_charname( sd->fd, sd->status.char_id, sd->status.name );
  6840. PACKET_ZC_ACK_REQMAKINGITEM p = {};
  6841. p.packetType = HEADER_ZC_ACK_REQMAKINGITEM;
  6842. p.result = flag;
  6843. p.itemId = client_nameid( nameid );
  6844. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6845. }
  6846. /// Initiates the pet taming process.
  6847. /// 019e (ZC_START_CAPTURE)
  6848. void clif_catch_process( map_session_data& sd ){
  6849. PACKET_ZC_START_CAPTURE packet{};
  6850. packet.PacketType = HEADER_ZC_START_CAPTURE;
  6851. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6852. }
  6853. /// Displays the result of a pet taming attempt.
  6854. /// 01a0 <result>.B (ZC_TRYCAPTURE_MONSTER)
  6855. /// 0 = failure
  6856. /// 1 = success
  6857. void clif_pet_roulette( map_session_data& sd, bool success ){
  6858. PACKET_ZC_TRYCAPTURE_MONSTER packet{};
  6859. packet.PacketType = HEADER_ZC_TRYCAPTURE_MONSTER;
  6860. packet.result = success;
  6861. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6862. }
  6863. /// Presents a list of pet eggs that can be hatched (ZC_PETEGG_LIST).
  6864. /// 01a6 <packet len>.W { <index>.W }*
  6865. void clif_sendegg(map_session_data *sd)
  6866. {
  6867. int i,n=0,fd;
  6868. nullpo_retv(sd);
  6869. fd=sd->fd;
  6870. if (battle_config.pet_no_gvg && map_flag_gvg2(sd->bl.m)) { //Disable pet hatching in GvG grounds during Guild Wars [Skotlex]
  6871. clif_displaymessage(fd, msg_txt(sd,666));
  6872. return;
  6873. }
  6874. WFIFOHEAD(fd, MAX_INVENTORY * 2 + 4);
  6875. WFIFOW(fd,0)=0x1a6;
  6876. for(i=0,n=0;i<MAX_INVENTORY;i++){
  6877. if(sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr ||
  6878. sd->inventory_data[i]->type != IT_PETEGG ||
  6879. sd->inventory.u.items_inventory[i].amount <= 0)
  6880. continue;
  6881. WFIFOW(fd,n*2+4)=client_index(i);
  6882. n++;
  6883. }
  6884. WFIFOW(fd,2)=4+n*2;
  6885. WFIFOSET(fd,WFIFOW(fd,2));
  6886. sd->menuskill_id = SA_TAMINGMONSTER;
  6887. sd->menuskill_val = -1;
  6888. }
  6889. /// Sends a specific pet data update (ZC_CHANGESTATE_PET).
  6890. /// 01a4 <type>.B <id>.L <data>.L
  6891. /// type:
  6892. /// 0 = pre-init (data = 0)
  6893. /// 1 = intimacy (data = 0~4)
  6894. /// 2 = hunger (data = 0~4)
  6895. /// 3 = accessory
  6896. /// 4 = performance (data = 1~3: normal, 4: special)
  6897. /// 5 = hairstyle
  6898. /// 6 = close egg selection ui and update egg in inventory (PACKETVER >= 20180704)
  6899. ///
  6900. /// If sd is null, the update is sent to nearby objects, otherwise it is sent only to that player.
  6901. void clif_send_petdata(map_session_data* sd, struct pet_data* pd, int type, int param)
  6902. {
  6903. uint8 buf[16];
  6904. nullpo_retv(pd);
  6905. WBUFW(buf,0) = 0x1a4;
  6906. WBUFB(buf,2) = type;
  6907. WBUFL(buf,3) = pd->bl.id;
  6908. WBUFL(buf,7) = param;
  6909. if (sd)
  6910. clif_send(buf, packet_len(0x1a4), &sd->bl, SELF);
  6911. else
  6912. clif_send(buf, packet_len(0x1a4), &pd->bl, AREA);
  6913. }
  6914. /// Pet's base data (ZC_PROPERTY_PET).
  6915. /// 01a2 <name>.24B <renamed>.B <level>.W <hunger>.W <intimacy>.W <accessory id>.W <class>.W
  6916. void clif_send_petstatus(map_session_data *sd)
  6917. {
  6918. int fd;
  6919. struct s_pet *pet;
  6920. nullpo_retv(sd);
  6921. nullpo_retv(sd->pd);
  6922. fd=sd->fd;
  6923. pet = &sd->pd->pet;
  6924. WFIFOHEAD(fd,packet_len(0x1a2));
  6925. WFIFOW(fd,0)=0x1a2;
  6926. safestrncpy(WFIFOCP(fd,2),pet->name,NAME_LENGTH);
  6927. WFIFOB(fd,26)=battle_config.pet_rename?0:pet->rename_flag;
  6928. WFIFOW(fd,27)=pet->level;
  6929. WFIFOW(fd,29)=pet->hungry;
  6930. WFIFOW(fd,31)=pet->intimate;
  6931. WFIFOW(fd,33)=pet->equip;
  6932. #if PACKETVER >= 20081126
  6933. WFIFOW(fd,35)=pet->class_;
  6934. #endif
  6935. WFIFOSET(fd,packet_len(0x1a2));
  6936. }
  6937. /// Notification about a pet's emotion/talk (ZC_PET_ACT).
  6938. /// 01aa <id>.L <data>.L
  6939. /// data:
  6940. /// @see CZ_PET_ACT.
  6941. void clif_pet_emotion(struct pet_data *pd,int param)
  6942. {
  6943. unsigned char buf[16];
  6944. nullpo_retv(pd);
  6945. memset(buf,0,packet_len(0x1aa));
  6946. WBUFW(buf,0)=0x1aa;
  6947. WBUFL(buf,2)=pd->bl.id;
  6948. WBUFL(buf,6)=param;
  6949. clif_send(buf,packet_len(0x1aa),&pd->bl,AREA);
  6950. }
  6951. /// Result of request to feed a pet.
  6952. /// 01a3 <result>.B <name id>.W (ZC_FEED_PET)
  6953. /// result:
  6954. /// 0 = failure
  6955. /// 1 = success
  6956. void clif_pet_food( map_session_data& sd, int32 foodid, bool success ){
  6957. PACKET_ZC_FEED_PET packet{};
  6958. packet.packetType = HEADER_ZC_FEED_PET;
  6959. packet.result = success;
  6960. packet.itemId = client_nameid( foodid );
  6961. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6962. }
  6963. /// Send pet auto feed info.
  6964. void clif_pet_autofeed_status(map_session_data* sd, bool force) {
  6965. #if PACKETVER >= 20141008
  6966. nullpo_retv(sd);
  6967. if (force || battle_config.pet_autofeed_always) {
  6968. // Always send ON or OFF
  6969. if (sd->pd && battle_config.feature_pet_autofeed) {
  6970. clif_configuration(sd, CONFIG_PET_AUTOFEED, sd->pd->pet.autofeed);
  6971. }
  6972. else {
  6973. clif_configuration(sd, CONFIG_PET_AUTOFEED, false);
  6974. }
  6975. }
  6976. else {
  6977. // Only send when enabled
  6978. if (sd->pd && battle_config.feature_pet_autofeed && sd->pd->pet.autofeed) {
  6979. clif_configuration(sd, CONFIG_PET_AUTOFEED, true);
  6980. }
  6981. }
  6982. #endif
  6983. }
  6984. /// Presents a list of skills that can be auto-spelled.
  6985. /// 01cd { <skill id>.L }*7 (ZC_AUTOSPELLLIST)
  6986. void clif_autospell( map_session_data& sd, uint16 skill_lv ){
  6987. struct s_autospell_requirement{
  6988. uint16 skill_id;
  6989. uint16 required_autospell_skill_lv;
  6990. };
  6991. #ifdef RENEWAL
  6992. const std::vector<s_autospell_requirement> autospell_skills = {
  6993. { MG_FIREBOLT, 0 },
  6994. { MG_COLDBOLT, 0 },
  6995. { MG_LIGHTNINGBOLT, 0 },
  6996. { MG_SOULSTRIKE, 3 },
  6997. { MG_FIREBALL, 3 },
  6998. { WZ_EARTHSPIKE, 6 },
  6999. { MG_FROSTDIVER, 6 },
  7000. { MG_THUNDERSTORM, 9 },
  7001. { WZ_HEAVENDRIVE, 9 }
  7002. };
  7003. #else
  7004. const std::vector<s_autospell_requirement> autospell_skills = {
  7005. { MG_NAPALMBEAT, 0 },
  7006. { MG_COLDBOLT, 1 },
  7007. { MG_FIREBOLT, 1 },
  7008. { MG_LIGHTNINGBOLT, 1 },
  7009. { MG_SOULSTRIKE, 4 },
  7010. { MG_FIREBALL, 7 },
  7011. { MG_FROSTDIVER, 9 },
  7012. };
  7013. #endif
  7014. #if PACKETVER_MAIN_NUM >= 20181128 || PACKETVER_RE_NUM >= 20181031
  7015. PACKET_ZC_AUTOSPELLLIST* p = reinterpret_cast<PACKET_ZC_AUTOSPELLLIST*>( packet_buffer );
  7016. p->packetType = HEADER_ZC_AUTOSPELLLIST;
  7017. p->packetLength = sizeof( *p );
  7018. size_t count = 0;
  7019. for( const s_autospell_requirement& requirement : autospell_skills ){
  7020. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  7021. p->skills[count++] = requirement.skill_id;
  7022. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skills[0] ) );
  7023. }
  7024. }
  7025. clif_send( p, p->packetLength, &sd.bl, SELF );
  7026. #elif PACKETVER_MAIN_NUM >= 20090406 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_SAK_NUM >= 20080618
  7027. PACKET_ZC_AUTOSPELLLIST p = {};
  7028. p.packetType = HEADER_ZC_AUTOSPELLLIST;
  7029. size_t count = 0;
  7030. for( const s_autospell_requirement& requirement : autospell_skills ){
  7031. if( count == ARRAYLENGTH( p.skills ) ){
  7032. break;
  7033. }
  7034. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  7035. p.skills[count++] = requirement.skill_id;
  7036. }else{
  7037. p.skills[count++] = 0;
  7038. }
  7039. }
  7040. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7041. #endif
  7042. sd.menuskill_id = SA_AUTOSPELL;
  7043. sd.menuskill_val = skill_lv;
  7044. }
  7045. /// Devotion's visual effect (ZC_DEVOTIONLIST).
  7046. /// 01cf <devoter id>.L { <devotee id>.L }*5 <max distance>.W
  7047. void clif_devotion(struct block_list *src, map_session_data *tsd)
  7048. {
  7049. unsigned char buf[56];
  7050. nullpo_retv(src);
  7051. memset(buf,0,packet_len(0x1cf));
  7052. WBUFW(buf,0) = 0x1cf;
  7053. WBUFL(buf,2) = src->id;
  7054. if( src->type == BL_MER )
  7055. {
  7056. s_mercenary_data *md = BL_CAST(BL_MER,src);
  7057. if( md && md->master && md->devotion_flag )
  7058. WBUFL(buf,6) = md->master->bl.id;
  7059. WBUFW(buf,26) = skill_get_range2(src, ML_DEVOTION, mercenary_checkskill(md, ML_DEVOTION), false);
  7060. }
  7061. else
  7062. {
  7063. int i;
  7064. map_session_data *sd = BL_CAST(BL_PC,src);
  7065. if( sd == nullptr )
  7066. return;
  7067. for( i = 0; i < 5 /*MAX_DEVOTION*/; i++ ) // Client only able show to 5 links
  7068. WBUFL(buf,6+4*i) = sd->devotion[i];
  7069. WBUFW(buf,26) = skill_get_range2(src, CR_DEVOTION, pc_checkskill(sd, CR_DEVOTION), false);
  7070. }
  7071. if( tsd )
  7072. clif_send(buf, packet_len(0x1cf), &tsd->bl, SELF);
  7073. else
  7074. clif_send(buf, packet_len(0x1cf), src, AREA);
  7075. }
  7076. /// Notifies the client of an object's spirits.
  7077. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  7078. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  7079. void clif_spiritball( struct block_list *bl, struct block_list* target, enum send_target send_target ){
  7080. nullpo_retv( bl );
  7081. PACKET_ZC_SPIRITS p = {};
  7082. p.PacketType = HEADER_ZC_SPIRITS;
  7083. p.AID = bl->id;
  7084. switch( bl->type ){
  7085. case BL_PC:
  7086. p.num = ( (map_session_data*)bl )->spiritball;
  7087. break;
  7088. case BL_HOM:
  7089. p.num = ( (struct homun_data*)bl )->homunculus.spiritball;
  7090. break;
  7091. }
  7092. clif_send( &p, sizeof( p ), target == nullptr ? bl : target, send_target );
  7093. }
  7094. /// Notifies clients in area of a character's combo delay (ZC_COMBODELAY).
  7095. /// 01d2 <account id>.L <delay>.L
  7096. void clif_combo_delay(struct block_list *bl,t_tick wait)
  7097. {
  7098. unsigned char buf[32];
  7099. nullpo_retv(bl);
  7100. WBUFW(buf,0)=0x1d2;
  7101. WBUFL(buf,2)=bl->id;
  7102. WBUFL(buf,6)=client_tick(wait);
  7103. clif_send(buf,packet_len(0x1d2),bl,AREA);
  7104. }
  7105. /// Notifies clients in area that a character has blade-stopped another (ZC_BLADESTOP).
  7106. /// 01d1 <src id>.L <dst id>.L <flag>.L
  7107. /// flag:
  7108. /// 0 = inactive
  7109. /// 1 = active
  7110. void clif_bladestop(struct block_list *src, int dst_id, int active)
  7111. {
  7112. unsigned char buf[32];
  7113. nullpo_retv(src);
  7114. WBUFW(buf,0)=0x1d1;
  7115. WBUFL(buf,2)=src->id;
  7116. WBUFL(buf,6)=dst_id;
  7117. WBUFL(buf,10)=active;
  7118. clif_send(buf,packet_len(0x1d1),src,AREA);
  7119. }
  7120. /// MVP effect (ZC_MVP).
  7121. /// 010c <account id>.L
  7122. void clif_mvp_effect(map_session_data *sd)
  7123. {
  7124. unsigned char buf[16];
  7125. nullpo_retv(sd);
  7126. WBUFW(buf,0)=0x10c;
  7127. WBUFL(buf,2)=sd->bl.id;
  7128. clif_send(buf,packet_len(0x10c),&sd->bl,AREA);
  7129. }
  7130. /// MVP item reward message (ZC_MVP_GETTING_ITEM).
  7131. /// 010a <name id>.W
  7132. void clif_mvp_item( map_session_data *sd, t_itemid nameid ){
  7133. PACKET_ZC_MVP_GETTING_ITEM p = {};
  7134. p.packetType = HEADER_ZC_MVP_GETTING_ITEM;
  7135. p.itemId = client_nameid( nameid );
  7136. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  7137. }
  7138. /// MVP EXP reward message (ZC_MVP_GETTING_SPECIAL_EXP).
  7139. /// 010b <exp>.L
  7140. void clif_mvp_exp(map_session_data *sd, t_exp exp) {
  7141. #if PACKETVER >= 20131223 // Kro remove this packet [Napster]
  7142. if (battle_config.mvp_exp_reward_message) {
  7143. char e_msg[CHAT_SIZE_MAX];
  7144. sprintf(e_msg, msg_txt(sd, 717), exp);
  7145. clif_messagecolor(&sd->bl, color_table[COLOR_CYAN], e_msg, false, SELF); // Congratulations! You are the MVP! Your reward EXP Points are %u !!
  7146. }
  7147. #else
  7148. int fd;
  7149. nullpo_retv(sd);
  7150. fd = sd->fd;
  7151. WFIFOHEAD(fd, packet_len(0x10b));
  7152. WFIFOW(fd,0) = 0x10b;
  7153. WFIFOL(fd,2) = (uint32)u64min( exp, MAX_EXP );
  7154. WFIFOSET(fd, packet_len(0x10b));
  7155. #endif
  7156. }
  7157. /// Dropped MVP item reward message (ZC_THROW_MVPITEM).
  7158. /// 010d
  7159. ///
  7160. /// "You are the MVP, but cannot obtain the reward because
  7161. /// you are overweight."
  7162. void clif_mvp_noitem(map_session_data* sd)
  7163. {
  7164. int fd = sd->fd;
  7165. WFIFOHEAD(fd,packet_len(0x10d));
  7166. WFIFOW(fd,0) = 0x10d;
  7167. WFIFOSET(fd,packet_len(0x10d));
  7168. }
  7169. /// Guild creation result.
  7170. /// 0167 <result>.B (ZC_RESULT_MAKE_GUILD)
  7171. /// result:
  7172. /// 0 = "Guild has been created."
  7173. /// 1 = "You are already in a Guild."
  7174. /// 2 = "That Guild Name already exists."
  7175. /// 3 = "You need the neccessary item to create a Guild."
  7176. void clif_guild_created( map_session_data& sd, int flag ){
  7177. PACKET_ZC_RESULT_MAKE_GUILD p = {};
  7178. p.packetType = HEADER_ZC_RESULT_MAKE_GUILD;
  7179. p.result = static_cast<decltype(p.result)>( flag );
  7180. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7181. }
  7182. /// Notifies the client that it is belonging to a guild (ZC_UPDATE_GDID).
  7183. /// 016c <guild id>.L <emblem id>.L <mode>.L <ismaster>.B <inter sid>.L <guild name>.24B
  7184. /// mode:
  7185. /// &0x01 = allow invite
  7186. /// &0x10 = allow expel
  7187. void clif_guild_belonginfo( map_session_data& sd ){
  7188. if( sd.guild == nullptr ){
  7189. return;
  7190. }
  7191. const auto &guild = sd.guild->guild;
  7192. int position = guild_getposition(sd);
  7193. if( position < 0 ){
  7194. return;
  7195. }
  7196. PACKET_ZC_UPDATE_GDID p = {};
  7197. p.PacketType = HEADER_ZC_UPDATE_GDID;
  7198. p.guildId = guild.guild_id;
  7199. p.emblemVersion = guild.emblem_id;
  7200. p.mode = guild.position[position].mode;
  7201. p.isMaster = ( sd.state.gmaster_flag == 1 );
  7202. p.interSid = 0; // InterSID (unknown purpose)
  7203. safestrncpy( p.guildName, guild.name, sizeof( p.guildName ) );
  7204. #if PACKETVER_MAIN_NUM >= 20220216
  7205. p.masterGID = guild.member[0].char_id;
  7206. #endif
  7207. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7208. }
  7209. /// Guild member login notice.
  7210. /// 016d <account id>.L <char id>.L <status>.L (ZC_UPDATE_CHARSTAT)
  7211. /// 01f2 <account id>.L <char id>.L <status>.L <gender>.W <hair style>.W <hair color>.W (ZC_UPDATE_CHARSTAT2)
  7212. /// status:
  7213. /// 0 = offline
  7214. /// 1 = online
  7215. void clif_guild_memberlogin_notice(const struct mmo_guild &g,int idx,int flag)
  7216. {
  7217. unsigned char buf[64];
  7218. map_session_data* sd;
  7219. WBUFW(buf, 0)=0x1f2;
  7220. WBUFL(buf, 2)=g.member[idx].account_id;
  7221. WBUFL(buf, 6)=g.member[idx].char_id;
  7222. WBUFL(buf,10)=flag;
  7223. if( ( sd = g.member[idx].sd ) != nullptr )
  7224. {
  7225. WBUFW(buf,14) = sd->status.sex;
  7226. WBUFW(buf,16) = sd->status.hair;
  7227. WBUFW(buf,18) = sd->status.hair_color;
  7228. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD_WOS);
  7229. }
  7230. else if( ( sd = guild_getavailablesd(g) ) != nullptr )
  7231. {
  7232. WBUFW(buf,14) = 0;
  7233. WBUFW(buf,16) = 0;
  7234. WBUFW(buf,18) = 0;
  7235. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD);
  7236. }
  7237. }
  7238. // Function `clif_guild_memberlogin_notice` sends info about
  7239. // logins and logouts of a guild member to the rest members.
  7240. // But at the 1st time (after a player login or map changing)
  7241. // the client won't show the message.
  7242. // So I suggest use this function for sending "first-time-info"
  7243. // to some player on entering the game or changing location.
  7244. // At next time the client would always show the message.
  7245. // The function sends all the statuses in the single packet
  7246. // to economize traffic. [LuzZza]
  7247. void clif_guild_send_onlineinfo(map_session_data *sd)
  7248. {
  7249. unsigned char buf[14*128];
  7250. int i, count=0, p_len;
  7251. nullpo_retv(sd);
  7252. p_len = packet_len(0x16d);
  7253. auto &g = sd->guild;
  7254. if (!g)
  7255. return;
  7256. for(i=0; i<g->guild.max_member; i++) {
  7257. if(g->guild.member[i].account_id > 0 &&
  7258. g->guild.member[i].account_id != sd->status.account_id) {
  7259. WBUFW(buf,count*p_len) = 0x16d;
  7260. WBUFL(buf,count*p_len+2) = g->guild.member[i].account_id;
  7261. WBUFL(buf,count*p_len+6) = g->guild.member[i].char_id;
  7262. WBUFL(buf,count*p_len+10) = g->guild.member[i].online;
  7263. count++;
  7264. }
  7265. }
  7266. clif_send(buf, p_len*count, &sd->bl, SELF);
  7267. }
  7268. /// Bitmask of enabled guild window tabs
  7269. /// 014e <menu flag>.L (ZC_ACK_GUILD_MENUINTERFACE)
  7270. /// menu flag:
  7271. /// 0x00 = Basic Info (always on)
  7272. /// &0x01 = Member manager
  7273. /// &0x02 = Positions
  7274. /// &0x04 = Skills
  7275. /// &0x10 = Expulsion list
  7276. /// &0x40 = Unknown (GMENUFLAG_ALLGUILDLIST)
  7277. /// &0x80 = Notice
  7278. void clif_guild_masterormember(map_session_data& sd){
  7279. PACKET_ZC_ACK_GUILD_MENUINTERFACE p{};
  7280. p.packetType = HEADER_ZC_ACK_GUILD_MENUINTERFACE;
  7281. if(sd.state.gmaster_flag){
  7282. p.menuFlag = 0xd7;
  7283. }else{
  7284. p.menuFlag = 0x57;
  7285. }
  7286. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7287. }
  7288. /// Guild basic information (Territories [Valaris])
  7289. /// 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)
  7290. /// 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)
  7291. /// 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)
  7292. void clif_guild_basicinfo( map_session_data& sd ){
  7293. if( sd.guild == nullptr ){
  7294. return;
  7295. }
  7296. const auto &guild = sd.guild->guild;
  7297. PACKET_ZC_GUILD_INFO p = {};
  7298. p.PacketType = HEADER_ZC_GUILD_INFO;
  7299. p.GDID = guild.guild_id;
  7300. p.level = guild.guild_lv;
  7301. p.userNum = guild.connect_member;
  7302. p.maxUserNum = guild.max_member;
  7303. p.userAverageLevel = guild.average_lv;
  7304. p.exp = (uint32)cap_value( guild.exp, 0, MAX_GUILD_EXP );
  7305. p.maxExp = (uint32)cap_value( guild.next_exp, 0, MAX_GUILD_EXP );
  7306. p.point = 0; // Tax Points
  7307. p.honor = 0; // Honor: (left) Vulgar [-100,100] Famed (right)
  7308. p.virtue = 0; // Virtue: (down) Wicked [-100,100] Righteous (up)
  7309. p.emblemVersion = guild.emblem_id;
  7310. safestrncpy( p.guildname, guild.name, sizeof( p.guildname ) );
  7311. safestrncpy( p.manageLand, msg_txt( &sd, 300 + guild_checkcastles( guild ) ), sizeof( p.manageLand ) );
  7312. p.zeny = 0;
  7313. #if PACKETVER >= 20200902
  7314. p.masterGID = guild.member[0].char_id; // leader
  7315. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7316. #elif PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7317. p.masterGID = guild.member[0].char_id; // leader
  7318. #else
  7319. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7320. #endif
  7321. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7322. }
  7323. /// Guild alliance and opposition list
  7324. /// 014c <packet len>.W { <relation>.L <guild id>.L <guild name>.24B }* (ZC_MYGUILD_BASIC_INFO)
  7325. void clif_guild_allianceinfo(map_session_data& sd){
  7326. auto &g = sd.guild;
  7327. if (g == nullptr){
  7328. return;
  7329. }
  7330. PACKET_ZC_MYGUILD_BASIC_INFO* p = reinterpret_cast<PACKET_ZC_MYGUILD_BASIC_INFO*>( packet_buffer );
  7331. p->PacketType = HEADER_ZC_MYGUILD_BASIC_INFO;
  7332. p->PacketLength = sizeof(*p);
  7333. for(size_t i=0, c = 0;i<MAX_GUILDALLIANCE;i++){
  7334. guild_alliance &a = g->guild.alliance[i];
  7335. if(a.guild_id<=0){
  7336. continue;
  7337. }
  7338. RELATED_GUILD_INFO& info = p->rgInfo[c];
  7339. info.relation = a.opposition;
  7340. info.GDID = a.guild_id;
  7341. safestrncpy(info.guildname,a.name,sizeof(info.guildname));
  7342. p->PacketLength += static_cast<decltype(p->PacketLength)>(sizeof(info));
  7343. c++;
  7344. }
  7345. clif_send(p,p->PacketLength,&sd.bl,SELF);
  7346. }
  7347. /// Guild member manager information
  7348. /// 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)
  7349. /// state:
  7350. /// 0 = offline
  7351. /// 1 = online
  7352. /// memo:
  7353. /// probably member's self-introduction (unused, no client UI/packets for editing it)
  7354. /// 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)
  7355. void clif_guild_memberlist( map_session_data& sd ){
  7356. if( sd.guild == nullptr ){
  7357. return;
  7358. }
  7359. const auto &guild = sd.guild->guild;
  7360. PACKET_ZC_MEMBERMGR_INFO* p = reinterpret_cast<PACKET_ZC_MEMBERMGR_INFO*>( packet_buffer );
  7361. p->PacketType = HEADER_ZC_MEMBERMGR_INFO;
  7362. p->packetLength = sizeof( *p );
  7363. for( int i = 0, c = 0; i < guild.max_member; i++ ){
  7364. const auto &member = guild.member[i];
  7365. if( member.account_id == 0 ){
  7366. continue;
  7367. }
  7368. GUILD_MEMBER_INFO& member_info = p->guildMemberInfo[c];
  7369. member_info.AID = member.account_id;
  7370. member_info.GID = member.char_id;
  7371. member_info.head = member.hair;
  7372. member_info.headPalette = member.hair_color;
  7373. member_info.sex = member.gender;
  7374. member_info.job = member.class_;
  7375. member_info.level = member.lv;
  7376. member_info.contributionExp = (uint32)cap_value( member.exp, 0, MAX_GUILD_EXP );
  7377. member_info.currentState = member.online;
  7378. member_info.positionID = member.position;
  7379. #if PACKETVER >= 20200902
  7380. member_info.lastLoginTime = member.last_login;
  7381. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7382. #elif PACKETVER_MAIN_NUM >= 20161214 || PACKETVER_RE_NUM >= 20161130 || defined(PACKETVER_ZERO)
  7383. member_info.lastLoginTime = member.last_login;
  7384. #else
  7385. 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.
  7386. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7387. #endif
  7388. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( member_info ) );
  7389. c++;
  7390. }
  7391. clif_send( p, p->packetLength, &sd.bl, SELF );
  7392. }
  7393. /// Guild position name information (ZC_POSITION_ID_NAME_INFO).
  7394. /// 0166 <packet len>.W { <position id>.L <position name>.24B }*
  7395. void clif_guild_positionnamelist(map_session_data *sd)
  7396. {
  7397. int i,fd;
  7398. nullpo_retv(sd);
  7399. auto &g = sd->guild;
  7400. if (!g)
  7401. return;
  7402. fd = sd->fd;
  7403. WFIFOHEAD(fd, MAX_GUILDPOSITION * 28 + 4);
  7404. WFIFOW(fd, 0)=0x166;
  7405. for(i=0;i<MAX_GUILDPOSITION;i++){
  7406. WFIFOL(fd,i*28+4)=i;
  7407. safestrncpy(WFIFOCP(fd,i*28+8),g->guild.position[i].name,NAME_LENGTH);
  7408. }
  7409. WFIFOW(fd,2)=i*28+4;
  7410. WFIFOSET(fd,WFIFOW(fd,2));
  7411. }
  7412. /// Guild position information
  7413. /// 0160 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L }* (ZC_POSITION_INFO)
  7414. /// mode:
  7415. /// See enum e_guild_permission
  7416. /// ranking:
  7417. /// TODO
  7418. static void clif_guild_positioninfolist(map_session_data& sd){
  7419. auto &g = sd.guild;
  7420. if (g == nullptr){
  7421. return;
  7422. }
  7423. PACKET_ZC_POSITION_INFO* p = reinterpret_cast<PACKET_ZC_POSITION_INFO*>( packet_buffer );
  7424. p->PacketType = HEADER_ZC_POSITION_INFO;
  7425. p->PacketLength = sizeof(*p);
  7426. for(size_t i=0;i<MAX_GUILDPOSITION;i++){
  7427. guild_position& gp = g->guild.position[i];
  7428. PACKET_ZC_POSITION_INFO_sub& position = p->posInfo[i];
  7429. position.positionID = static_cast<decltype(position.positionID)>( i );
  7430. position.right = gp.mode;
  7431. position.ranking = static_cast<decltype(position.ranking)>( i );
  7432. position.payRate = gp.exp_mode;
  7433. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( position ) );
  7434. }
  7435. clif_send(p,p->PacketLength,&sd.bl,SELF);
  7436. }
  7437. /// Notifies clients in a guild about updated position information.
  7438. /// 0174 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <position name>.24B }* (ZC_ACK_CHANGE_GUILD_POSITIONINFO)
  7439. /// mode:
  7440. /// &0x01 = allow invite
  7441. /// &0x10 = allow expel
  7442. /// ranking:
  7443. /// TODO
  7444. void clif_guild_positionchanged(const struct mmo_guild &g,int idx)
  7445. {
  7446. // FIXME: This packet is intended to update the clients after a
  7447. // commit of position info changes, not sending one packet per
  7448. // position.
  7449. map_session_data *sd;
  7450. unsigned char buf[128];
  7451. WBUFW(buf, 0)=0x174;
  7452. WBUFW(buf, 2)=44; // packet len
  7453. // GUILD_REG_POSITION_INFO{
  7454. WBUFL(buf, 4)=idx;
  7455. WBUFL(buf, 8)=g.position[idx].mode;
  7456. WBUFL(buf,12)=idx;
  7457. WBUFL(buf,16)=g.position[idx].exp_mode;
  7458. safestrncpy(WBUFCP(buf,20),g.position[idx].name,NAME_LENGTH);
  7459. // }*
  7460. if( (sd=guild_getavailablesd(g))!=nullptr )
  7461. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7462. }
  7463. /// Notifies clients in a guild about updated member position assignments.
  7464. /// 0156 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (ZC_ACK_REQ_CHANGE_MEMBERS)
  7465. void clif_guild_memberpositionchanged(const struct mmo_guild &g, int idx)
  7466. {
  7467. // FIXME: This packet is intended to update the clients after a
  7468. // commit of member position assignment changes, not sending one
  7469. // packet per position.
  7470. map_session_data *sd;
  7471. unsigned char buf[64];
  7472. WBUFW(buf, 0)=0x156;
  7473. WBUFW(buf, 2)=16; // packet len
  7474. // MEMBER_POSITION_INFO{
  7475. WBUFL(buf, 4)=g.member[idx].account_id;
  7476. WBUFL(buf, 8)=g.member[idx].char_id;
  7477. WBUFL(buf,12)=g.member[idx].position;
  7478. // }*
  7479. if( (sd=guild_getavailablesd(g))!=nullptr )
  7480. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7481. }
  7482. /// Sends emblems bitmap data to the client that requested it.
  7483. /// 0152 <packet len>.W <guild id>.L <emblem id>.L <emblem data>.?B (ZC_GUILD_EMBLEM_IMG)
  7484. void clif_guild_emblem(const map_session_data &sd, const struct mmo_guild &g)
  7485. {
  7486. int fd = sd.fd;
  7487. if( g.emblem_len <= 0 )
  7488. return;
  7489. WFIFOHEAD(fd,g.emblem_len+12);
  7490. WFIFOW(fd,0)=0x152;
  7491. WFIFOW(fd,2)=g.emblem_len+12;
  7492. WFIFOL(fd,4)=g.guild_id;
  7493. WFIFOL(fd,8)=g.emblem_id;
  7494. memcpy(WFIFOP(fd,12),g.emblem_data,g.emblem_len);
  7495. WFIFOSET(fd,WFIFOW(fd,2));
  7496. }
  7497. /// Sends update of the guild id/emblem id to everyone in the area.
  7498. /// 01b4 <id>.L <guild id>.L <emblem id>.W (ZC_CHANGE_GUILD)
  7499. /// 0b1f <guild id>.L <version>.L <AID>.L (ZC_NEW_EMBLEM_DOWNLOAD)
  7500. /// 0b47 <guild id>.L <version>.L <AID>.L (ZC_ACK_ADD_NEW_EMBLEM)
  7501. void clif_guild_emblem_area(struct block_list* bl)
  7502. {
  7503. // TODO this packet doesn't force the update of ui components that have the emblem visible
  7504. // (emblem in the flag npcs and emblem over the head in agit maps) [FlavioJS]
  7505. PACKET_ZC_CHANGE_GUILD p{};
  7506. p.packetType = HEADER_ZC_CHANGE_GUILD;
  7507. p.guild_id = status_get_guild_id(bl);
  7508. p.emblem_id = status_get_emblem_id(bl);
  7509. p.AID = bl->id;
  7510. clif_send(&p, sizeof(p), bl, AREA);
  7511. }
  7512. /// Sends guild skills.
  7513. /// 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)
  7514. void clif_guild_skillinfo( map_session_data& sd ){
  7515. auto& g = sd.guild;
  7516. if( g == nullptr ){
  7517. return;
  7518. }
  7519. PACKET_ZC_GUILD_SKILLINFO* p = reinterpret_cast<PACKET_ZC_GUILD_SKILLINFO*>( packet_buffer );
  7520. p->PacketType = HEADER_ZC_GUILD_SKILLINFO;
  7521. p->PacketLength = sizeof( *p );
  7522. p->skillPoint = g->guild.skill_point;
  7523. for( size_t i = 0, c = 0; i < MAX_GUILDSKILL; i++ ){
  7524. if( g->guild.skill[i].id <= 0 ){
  7525. continue;
  7526. }
  7527. if( !guild_check_skill_require( g->guild, g->guild.skill[i].id ) ){
  7528. continue;
  7529. }
  7530. GUILD_SKILLDATA& gs = p->skillInfo[c];
  7531. int skill_id = g->guild.skill[i].id;
  7532. gs.id = skill_id;
  7533. gs.inf = skill_get_inf(skill_id);
  7534. gs.level = g->guild.skill[i].lv;
  7535. if( g->guild.skill[i].lv > 0 ){
  7536. gs.sp = skill_get_sp( skill_id, g->guild.skill[i].lv );
  7537. gs.range2 = skill_get_range( skill_id, g->guild.skill[i].lv );
  7538. }else{
  7539. gs.sp = 0;
  7540. gs.range2 = 0;
  7541. }
  7542. safestrncpy( gs.name, skill_get_name( skill_id ), sizeof( gs.name ) );
  7543. gs.upFlag = ( g->guild.skill[i].lv < guild_skill_get_max( skill_id ) && &sd == g->guild.member[0].sd ) ? 1 : 0;
  7544. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( gs ) );
  7545. c++;
  7546. }
  7547. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7548. }
  7549. /// Sends guild notice to client.
  7550. /// 016f <subject>.60B <notice>.120B (ZC_GUILD_NOTICE)
  7551. void clif_guild_notice( map_session_data& sd ){
  7552. auto& g = sd.guild;
  7553. if( g == nullptr ){
  7554. return;
  7555. }
  7556. if( g->guild.mes1[0] == '\0' && g->guild.mes2[0] == '\0' ){
  7557. return;
  7558. }
  7559. PACKET_ZC_GUILD_NOTICE p = {};
  7560. p.packetType = HEADER_ZC_GUILD_NOTICE;
  7561. safestrncpy( p.subject, g->guild.mes1, sizeof( p.subject ) );
  7562. safestrncpy( p.notice, g->guild.mes2, sizeof( p.notice ) );
  7563. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7564. }
  7565. /// Guild invite.
  7566. /// 016a <guild id>.L <guild name>.24B (ZC_REQ_JOIN_GUILD)
  7567. void clif_guild_invite( map_session_data& sd, const struct mmo_guild& g ){
  7568. PACKET_ZC_REQ_JOIN_GUILD p = {};
  7569. p.packetType = HEADER_ZC_REQ_JOIN_GUILD;
  7570. p.guild_id = g.guild_id;
  7571. safestrncpy( p.guild_name, g.name, sizeof( p.guild_name ) );
  7572. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7573. }
  7574. /// Reply to invite request.
  7575. /// 0169 <answer>.B (ZC_ACK_REQ_JOIN_GUILD)
  7576. /// answer:
  7577. /// 0 = Already in guild.
  7578. /// 1 = Offer rejected.
  7579. /// 2 = Offer accepted.
  7580. /// 3 = Guild full.
  7581. void clif_guild_inviteack( map_session_data& sd, int flag ){
  7582. PACKET_ZC_ACK_REQ_JOIN_GUILD p = {};
  7583. p.packetType = HEADER_ZC_ACK_REQ_JOIN_GUILD;
  7584. p.result = static_cast<decltype(p.result)>( flag );
  7585. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7586. }
  7587. /// Notifies clients of a guild of a leaving member.
  7588. /// 015a <char name>.24B <reason>.40B (ZC_ACK_LEAVE_GUILD)
  7589. /// 0a83 <CID>.L <reason>.40B (ZC_ACK_LEAVE_GUILD_DELNAME)
  7590. void clif_guild_leave( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7591. PACKET_ZC_ACK_LEAVE_GUILD p = {};
  7592. p.packetType = guildLeave;
  7593. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7594. p.GID = char_id;
  7595. #else
  7596. safestrncpy( p.name, name, sizeof( p.name ) );
  7597. #endif
  7598. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7599. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7600. }
  7601. /// Notifies clients of a guild of an expelled member.
  7602. /// 015c <char name>.24B <reason>.40B <account name>.24B (ZC_ACK_BAN_GUILD)
  7603. /// 0839 <char name>.24B <reason>.40B (ZC_ACK_BAN_GUILD_SSO)
  7604. /// 0a82 <CID>.L <reason>.40B (ZC_ACK_BAN_GUILD_DELNAME)
  7605. void clif_guild_expulsion( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7606. PACKET_ZC_ACK_BAN_GUILD p = {};
  7607. p.packetType = guildExpulsion;
  7608. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7609. p.GID = char_id;
  7610. #else
  7611. safestrncpy( p.name, name, sizeof( p.name ) );
  7612. #endif
  7613. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7614. #if PACKETVER < 20100803
  7615. // account name is not sent for security reasons
  7616. safestrncpy( p.account_name, "", sizeof( p.account_name ) );
  7617. #endif
  7618. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7619. }
  7620. /// Guild expulsion list
  7621. /// 0163 <packet len>.W { <char name>.24B <account name>.24B <reason>.40B }* (ZC_BAN_LIST)
  7622. /// 0163 <packet len>.W { <char name>.24B <reason>.40B }* (PACKETVER >= 20100803) (ZC_BAN_LIST)
  7623. /// 0a87 <packet len>.W { <charid>.L <reason>.40B }* (ZC_BAN_LIST2)
  7624. /// 0b7c <packet len>.W { <charid>.L <reason>.40B <char name>.24B }* (ZC_BAN_LIST3)
  7625. static void clif_guild_expulsionlist(map_session_data& sd){
  7626. auto &g = sd.guild;
  7627. if (!g)
  7628. return;
  7629. PACKET_ZC_BAN_LIST* p = reinterpret_cast<PACKET_ZC_BAN_LIST*>( packet_buffer );
  7630. p->packetType = HEADER_ZC_BAN_LIST;
  7631. p->packetLen = sizeof(*p);
  7632. for( size_t i = 0, c = 0; i < MAX_GUILDEXPULSION; i++ ){
  7633. struct guild_expulsion& e = g->guild.expulsion[i];
  7634. if( e.account_id == 0 ){
  7635. continue;
  7636. }
  7637. PACKET_ZC_BAN_LIST_sub& banned = p->chars[c];
  7638. #if PACKETVER >= 20161019
  7639. banned.char_id = e.char_id;
  7640. #elif PACKETVER < 20100803
  7641. // account name (not used for security reasons)
  7642. safestrncpy(banned.account_name, "", sizeof(banned.account_name));
  7643. #endif
  7644. #if PACKETVER >= 20200902 || PACKETVER < 20161019
  7645. safestrncpy(banned.char_name, e.name, sizeof(banned.char_name));
  7646. #endif
  7647. safestrncpy(banned.message, e.mes, sizeof(banned.message));
  7648. p->packetLen += static_cast<decltype(p->packetLen)>(sizeof(banned));
  7649. c++;
  7650. }
  7651. clif_send(p,p->packetLen,&sd.bl,SELF);
  7652. }
  7653. /// Guild chat message
  7654. /// 017f <packet len>.W <message>.?B (ZC_GUILD_CHAT)
  7655. void clif_guild_message( const struct mmo_guild& g, const char* mes, size_t len ){
  7656. PACKET_ZC_GUILD_CHAT *p = reinterpret_cast<PACKET_ZC_GUILD_CHAT*>( packet_buffer );
  7657. // -1 for null terminator
  7658. static const size_t max_len = CHAT_SIZE_MAX - sizeof( *p ) - 1;
  7659. map_session_data* sd = guild_getavailablesd(g);
  7660. // Ignore this message, if no guildmember is available
  7661. if (sd == nullptr)
  7662. return;
  7663. if( len == 0 ){
  7664. return;
  7665. } else if( len > max_len ){
  7666. ShowWarning("clif_guild_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", guild_id=%u).\n", mes, len, max_len, g.guild_id);
  7667. len = max_len;
  7668. }
  7669. p->packetType = HEADER_ZC_GUILD_CHAT;
  7670. p->packetLength = sizeof(*p);
  7671. safestrncpy(p->message, mes, len+1);
  7672. p->packetLength += static_cast<decltype(p->packetLength)>( len + 1 );
  7673. clif_send(p, p->packetLength, &sd->bl, GUILD_NOBG);
  7674. }
  7675. /// Request for guild alliance
  7676. /// 0171 <inviter account id>.L <guild name>.24B (ZC_REQ_ALLY_GUILD).
  7677. void clif_guild_reqalliance(map_session_data& sd,uint32 account_id,const char *name)
  7678. {
  7679. PACKET_ZC_REQ_ALLY_GUILD p{};
  7680. p.packetType = HEADER_ZC_REQ_ALLY_GUILD;
  7681. p.inviterId = account_id;
  7682. safestrncpy(p.inviterGuildName,name,sizeof(p.inviterGuildName));
  7683. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7684. }
  7685. /// Notifies the client about the result of a alliance request.
  7686. /// 0173 <answer>.B (ZC_ACK_REQ_ALLY_GUILD)
  7687. /// answer:
  7688. /// 0 = Already allied.
  7689. /// 1 = You rejected the offer.
  7690. /// 2 = You accepted the offer.
  7691. /// 3 = They have too any alliances.
  7692. /// 4 = You have too many alliances.
  7693. /// 5 = Alliances are disabled.
  7694. void clif_guild_allianceack(map_session_data& sd,uint8 flag){
  7695. PACKET_ZC_ACK_REQ_ALLY_GUILD p{};
  7696. p.packetType = HEADER_ZC_ACK_REQ_ALLY_GUILD;
  7697. p.flag = flag;
  7698. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7699. }
  7700. /// Notifies the client that a alliance or opposition has been removed.
  7701. /// 0184 <other guild id>.L <relation>.L (ZC_DELETE_RELATED_GUILD)
  7702. /// relation:
  7703. /// 0 = Ally
  7704. /// 1 = Enemy
  7705. void clif_guild_delalliance(map_session_data& sd,uint32 guild_id,uint32 flag)
  7706. {
  7707. PACKET_ZC_DELETE_RELATED_GUILD p{};
  7708. p.packetType = HEADER_ZC_DELETE_RELATED_GUILD;
  7709. p.allyID = guild_id;
  7710. p.flag = flag;
  7711. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7712. }
  7713. /// Notifies the client about the result of a opposition request.
  7714. /// 0181 <result>.B (ZC_ACK_REQ_HOSTILE_GUILD)
  7715. /// result:
  7716. /// 0 = Antagonist has been set.
  7717. /// 1 = Guild has too many Antagonists.
  7718. /// 2 = Already set as an Antagonist.
  7719. /// 3 = Antagonists are disabled.
  7720. void clif_guild_oppositionack(map_session_data& sd,uint8 flag){
  7721. PACKET_ZC_ACK_REQ_HOSTILE_GUILD p{};
  7722. p.packetType = HEADER_ZC_ACK_REQ_HOSTILE_GUILD;
  7723. p.flag = flag;
  7724. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7725. }
  7726. /// Adds alliance or opposition (ZC_ADD_RELATED_GUILD).
  7727. /// 0185 <relation>.L <guild id>.L <guild name>.24B
  7728. /*
  7729. void clif_guild_allianceadded(struct guild *g,int idx)
  7730. {
  7731. unsigned char buf[64];
  7732. WBUFW(buf,0)=0x185;
  7733. WBUFL(buf,2)=g->alliance[idx].opposition;
  7734. WBUFL(buf,6)=g->alliance[idx].guild_id;
  7735. safestrncpy(WBUFP(buf,10),g->alliance[idx].name,NAME_LENGTH);
  7736. clif_send(buf,packet_len(0x185),guild_getavailablesd(g),GUILD);
  7737. }
  7738. */
  7739. /// Notifies the client about the result of a guild break.
  7740. /// 015e <reason>.L (ZC_ACK_DISORGANIZE_GUILD_RESULT)
  7741. /// 0 = success
  7742. /// 1 = invalid key (guild name, @see clif_parse_GuildBreak)
  7743. /// 2 = there are still members in the guild
  7744. void clif_guild_broken( map_session_data& sd, int flag ){
  7745. PACKET_ZC_ACK_DISORGANIZE_GUILD_RESULT p = {};
  7746. p.packetType = HEADER_ZC_ACK_DISORGANIZE_GUILD_RESULT;
  7747. p.result = flag;
  7748. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7749. }
  7750. void clif_guild_position_selected(map_session_data& sd)
  7751. {
  7752. #if PACKETVER_MAIN_NUM >= 20180605 || PACKETVER_RE_NUM >= 20180605 || PACKETVER_ZERO_NUM >= 20180605
  7753. PACKET_ZC_GUILD_POSITION* p = reinterpret_cast<PACKET_ZC_GUILD_POSITION*>(packet_buffer);
  7754. p->packetType = HEADER_ZC_GUILD_POSITION;
  7755. p->packetLength = sizeof( PACKET_ZC_GUILD_POSITION );
  7756. p->AID = sd.bl.id;
  7757. if( sd.guild != nullptr ){
  7758. const auto& g = sd.guild->guild;
  7759. if( int ps = guild_getposition( sd ); ps != -1 ){
  7760. safestrncpy( p->position, g.position[ps].name, NAME_LENGTH );
  7761. p->packetLength += static_cast<decltype(p->packetLength)>( NAME_LENGTH );
  7762. }
  7763. }
  7764. clif_send( p, p->packetLength, &sd.bl, AREA );
  7765. #else
  7766. clif_name_area(&sd.bl);
  7767. #endif
  7768. }
  7769. /// Displays emotion on an object (ZC_EMOTION).
  7770. /// 00c0 <id>.L <type>.B
  7771. /// type:
  7772. /// enum emotion_type
  7773. void clif_emotion(struct block_list *bl,int type)
  7774. {
  7775. unsigned char buf[8];
  7776. nullpo_retv(bl);
  7777. WBUFW(buf,0)=0xc0;
  7778. WBUFL(buf,2)=bl->id;
  7779. WBUFB(buf,6)=type;
  7780. clif_send(buf,packet_len(0xc0),bl,AREA);
  7781. }
  7782. /// Displays the contents of a talkiebox trap.
  7783. /// 0191 <id>.L <contents>.80B (ZC_TALKBOX_CHATCONTENTS)
  7784. void clif_talkiebox( struct block_list* bl, const char* talkie ){
  7785. nullpo_retv( bl );
  7786. nullpo_retv( talkie );
  7787. PACKET_ZC_TALKBOX_CHATCONTENTS p = {};
  7788. p.PacketType = HEADER_ZC_TALKBOX_CHATCONTENTS;
  7789. p.aid = bl->id;
  7790. safestrncpy( p.message, talkie, TALKBOX_MESSAGE_SIZE );
  7791. clif_send( &p, sizeof( PACKET_ZC_TALKBOX_CHATCONTENTS ), bl, AREA );
  7792. }
  7793. /// Displays wedding effect centered on an object (ZC_CONGRATULATION).
  7794. /// 01ea <id>.L
  7795. void clif_wedding_effect(struct block_list *bl)
  7796. {
  7797. unsigned char buf[6];
  7798. nullpo_retv(bl);
  7799. WBUFW(buf,0) = 0x1ea;
  7800. WBUFL(buf,2) = bl->id;
  7801. clif_send(buf, packet_len(0x1ea), bl, AREA);
  7802. }
  7803. /// Notifies the client of the name of the partner character (ZC_COUPLENAME).
  7804. /// 01e6 <partner name>.24B
  7805. void clif_callpartner(map_session_data& sd)
  7806. {
  7807. PACKET_ZC_COUPLENAME p = { 0 };
  7808. p.packetType = HEADER_ZC_COUPLENAME;
  7809. if( sd.status.partner_id ) {
  7810. map_session_data *p_sd = pc_get_partner(&sd);
  7811. if (p_sd != nullptr && !p_sd->state.autotrade)
  7812. safestrncpy(p.name, p_sd->status.name, NAME_LENGTH);
  7813. else
  7814. p.name[0] = 0;
  7815. } else {// Send zero-length name if no partner, to initialize the client buffer.
  7816. p.name[0] = 0;
  7817. }
  7818. clif_send(&p, sizeof(p), &sd.bl, AREA);
  7819. }
  7820. /// Initiates the partner "taming" process [DracoRPG] (ZC_START_COUPLE).
  7821. /// 01e4
  7822. /// This packet while still implemented by the client is no longer being officially used.
  7823. /*
  7824. void clif_marriage_process(map_session_data *sd)
  7825. {
  7826. int fd;
  7827. nullpo_retv(sd);
  7828. fd=sd->fd;
  7829. WFIFOHEAD(fd,packet_len(0x1e4));
  7830. WFIFOW(fd,0)=0x1e4;
  7831. WFIFOSET(fd,packet_len(0x1e4));
  7832. }
  7833. */
  7834. /// Notice of divorce (ZC_DIVORCE).
  7835. /// 0205 <partner name>.24B
  7836. void clif_divorced(map_session_data* sd, const char* name)
  7837. {
  7838. int fd;
  7839. nullpo_retv(sd);
  7840. fd=sd->fd;
  7841. WFIFOHEAD(fd,packet_len(0x205));
  7842. WFIFOW(fd,0)=0x205;
  7843. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  7844. WFIFOSET(fd, packet_len(0x205));
  7845. }
  7846. /// Marriage proposal (ZC_REQ_COUPLE).
  7847. /// 01e2 <account id>.L <char id>.L <char name>.24B
  7848. /// This packet while still implemented by the client is no longer being officially used.
  7849. /*
  7850. void clif_marriage_proposal(int fd, map_session_data *sd, map_session_data* ssd)
  7851. {
  7852. nullpo_retv(sd);
  7853. WFIFOHEAD(fd,packet_len(0x1e2));
  7854. WFIFOW(fd,0) = 0x1e2;
  7855. WFIFOL(fd,2) = ssd->status.account_id;
  7856. WFIFOL(fd,6) = ssd->status.char_id;
  7857. safestrncpy(WFIFOCP(fd,10), ssd->status.name, NAME_LENGTH);
  7858. WFIFOSET(fd, packet_len(0x1e2));
  7859. }
  7860. */
  7861. /*==========================================
  7862. * Displays a message using the guild-chat colors to the specified targets. [Skotlex]
  7863. *------------------------------------------*/
  7864. void clif_disp_message(struct block_list* src, const char* mes, size_t len, enum send_target target)
  7865. {
  7866. unsigned char buf[256];
  7867. if( len == 0 ) {
  7868. return;
  7869. } else if( len > sizeof(buf)-5 ) {
  7870. ShowWarning("clif_disp_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", aid=%d).\n", mes, len, sizeof(buf)-5, src->id);
  7871. len = sizeof(buf)-5;
  7872. }
  7873. WBUFW(buf, 0) = 0x17f;
  7874. WBUFW(buf, 2) = static_cast<int16>( len + 5 );
  7875. safestrncpy(WBUFCP(buf,4), mes, len+1);
  7876. clif_send(buf, WBUFW(buf,2), src, target);
  7877. }
  7878. /// Notifies the client about the result of a request to disconnect another player (ZC_ACK_DISCONNECT_CHARACTER).
  7879. /// 00cd <result>.L (unknown packet version or invalid information at packet_len_table)
  7880. /// 00cd <result>.B
  7881. /// result:
  7882. /// 0 = failure
  7883. /// 1 = success
  7884. void clif_GM_kickack(map_session_data *sd, int id)
  7885. {
  7886. int fd;
  7887. nullpo_retv(sd);
  7888. fd = sd->fd;
  7889. WFIFOHEAD(fd,packet_len(0xcd));
  7890. WFIFOW(fd,0) = 0xcd;
  7891. WFIFOB(fd,2) = id; // FIXME: this is not account id
  7892. WFIFOSET(fd, packet_len(0xcd));
  7893. }
  7894. void clif_GM_kick(map_session_data *sd, map_session_data *tsd)
  7895. {
  7896. nullpo_retv(tsd);
  7897. if (sd == nullptr)
  7898. tsd->state.keepshop = true;
  7899. if (session_isActive(tsd->fd))
  7900. clif_authfail_fd(tsd->fd, 15);
  7901. else
  7902. map_quit(tsd);
  7903. if (sd)
  7904. clif_GM_kickack(sd, tsd->status.account_id);
  7905. }
  7906. /// Displays various manner-related status messages (ZC_ACK_GIVE_MANNER_POINT).
  7907. /// 014a <result>.L
  7908. /// result:
  7909. /// 0 = "A manner point has been successfully aligned."
  7910. /// 1 = MP_FAILURE_EXHAUST
  7911. /// 2 = MP_FAILURE_ALREADY_GIVING
  7912. /// 3 = "Chat Block has been applied by GM due to your ill-mannerous action."
  7913. /// 4 = "Automated Chat Block has been applied due to Anti-Spam System."
  7914. /// 5 = "You got a good point from %s."
  7915. void clif_manner_message(map_session_data* sd, uint32 type)
  7916. {
  7917. int fd;
  7918. nullpo_retv(sd);
  7919. fd = sd->fd;
  7920. WFIFOHEAD(fd,packet_len(0x14a));
  7921. WFIFOW(fd,0) = 0x14a;
  7922. WFIFOL(fd,2) = type;
  7923. WFIFOSET(fd, packet_len(0x14a));
  7924. }
  7925. /// Followup to 0x14a type 3/5, informs who did the manner adjustment action (ZC_NOTIFY_MANNER_POINT_GIVEN).
  7926. /// 014b <type>.B <GM name>.24B
  7927. /// type:
  7928. /// 0 = positive (unmute)
  7929. /// 1 = negative (mute)
  7930. void clif_GM_silence(map_session_data* sd, map_session_data* tsd, uint8 type)
  7931. {
  7932. int fd;
  7933. nullpo_retv(sd);
  7934. nullpo_retv(tsd);
  7935. fd = tsd->fd;
  7936. WFIFOHEAD(fd,packet_len(0x14b));
  7937. WFIFOW(fd,0) = 0x14b;
  7938. WFIFOB(fd,2) = type;
  7939. safestrncpy(WFIFOCP(fd,3), sd->status.name, NAME_LENGTH);
  7940. WFIFOSET(fd, packet_len(0x14b));
  7941. }
  7942. /// Notifies the client about the result of a request to allow/deny whispers from a player (ZC_SETTING_WHISPER_PC).
  7943. /// 00d1 <type>.B <result>.B
  7944. /// type:
  7945. /// 0 = /ex (deny)
  7946. /// 1 = /in (allow)
  7947. /// result:
  7948. /// 0 = success
  7949. /// 1 = failure
  7950. /// 2 = too many blocks
  7951. void clif_wisexin(map_session_data *sd,int type,int flag)
  7952. {
  7953. int fd;
  7954. nullpo_retv(sd);
  7955. fd=sd->fd;
  7956. WFIFOHEAD(fd,packet_len(0xd1));
  7957. WFIFOW(fd,0)=0xd1;
  7958. WFIFOB(fd,2)=type;
  7959. WFIFOB(fd,3)=flag;
  7960. WFIFOSET(fd,packet_len(0xd1));
  7961. }
  7962. /// Notifies the client about the result of a request to allow/deny whispers from anyone (ZC_SETTING_WHISPER_STATE).
  7963. /// 00d2 <type>.B <result>.B
  7964. /// type:
  7965. /// 0 = /exall (deny)
  7966. /// 1 = /inall (allow)
  7967. /// result:
  7968. /// 0 = success
  7969. /// 1 = failure
  7970. void clif_wisall(map_session_data *sd,int type,int flag)
  7971. {
  7972. int fd;
  7973. nullpo_retv(sd);
  7974. fd=sd->fd;
  7975. WFIFOHEAD(fd,packet_len(0xd2));
  7976. WFIFOW(fd,0)=0xd2;
  7977. WFIFOB(fd,2)=type;
  7978. WFIFOB(fd,3)=flag;
  7979. WFIFOSET(fd,packet_len(0xd2));
  7980. }
  7981. /// Play a BGM! [Rikter/Yommy] (ZC_PLAY_NPC_BGM).
  7982. /// 07fe <bgm>.24B
  7983. void clif_playBGM( map_session_data& sd, const char* name ){
  7984. #if PACKETVER_MAIN_NUM >= 20220504
  7985. PACKET_ZC_PLAY_NPC_BGM* p = reinterpret_cast<PACKET_ZC_PLAY_NPC_BGM*>( packet_buffer );
  7986. size_t length = strlen( name ) + 1;
  7987. p->PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7988. p->PacketLength = sizeof( *p );
  7989. p->playType = 0; // TODO: implement, send loop for the time being
  7990. safestrncpy( p->bgm, name, length );
  7991. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  7992. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7993. #elif PACKETVER >= 20091201
  7994. PACKET_ZC_PLAY_NPC_BGM p = {};
  7995. p.PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7996. safestrncpy( p.bgm, name, sizeof( p.bgm ) );
  7997. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7998. #endif
  7999. }
  8000. /// Plays/stops a wave sound (ZC_SOUND).
  8001. /// 01d3 <file name>.24B <act>.B <term>.L <npc id>.L
  8002. /// file name:
  8003. /// relative to data\wav
  8004. /// act:
  8005. /// 0 = play (once)
  8006. /// 1 = play (repeat, does not work)
  8007. /// 2 = stops all sound instances of file name (does not work)
  8008. /// term:
  8009. /// unknown purpose, only relevant to act = 1
  8010. /// npc id:
  8011. /// The accustic direction of the sound is determined by the
  8012. /// relative position of the NPC to the player (3D sound).
  8013. void clif_soundeffect( struct block_list& bl, const char* name, int type, enum send_target target ){
  8014. PACKET_ZC_SOUND p = {};
  8015. p.PacketType = HEADER_ZC_SOUND;
  8016. safestrncpy( p.name, name, sizeof( p.name ) );
  8017. p.act = type;
  8018. p.term = 0; // TODO: implement
  8019. p.AID = bl.id;
  8020. clif_send( &p, sizeof( p ), &bl, target );
  8021. }
  8022. /// Displays special effects (npcs, weather, etc) [Valaris] (ZC_NOTIFY_EFFECT2).
  8023. /// 01f3 <id>.L <effect id>.L
  8024. /// effect id:
  8025. /// @see doc/effect_list.txt
  8026. void clif_specialeffect(struct block_list* bl, int type, enum send_target target)
  8027. {
  8028. unsigned char buf[24];
  8029. nullpo_retv(bl);
  8030. memset(buf, 0, packet_len(0x1f3));
  8031. WBUFW(buf,0) = 0x1f3;
  8032. WBUFL(buf,2) = bl->id;
  8033. WBUFL(buf,6) = type;
  8034. clif_send(buf, packet_len(0x1f3), bl, target);
  8035. if (disguised(bl)) {
  8036. WBUFL(buf,2) = disguised_bl_id(bl->id);
  8037. clif_send(buf, packet_len(0x1f3), bl, SELF);
  8038. }
  8039. }
  8040. void clif_specialeffect_single(struct block_list* bl, int type, int fd)
  8041. {
  8042. WFIFOHEAD(fd,10);
  8043. WFIFOW(fd,0) = 0x1f3;
  8044. WFIFOL(fd,2) = bl->id;
  8045. WFIFOL(fd,6) = type;
  8046. WFIFOSET(fd,10);
  8047. }
  8048. /// Notifies clients of an special/visual effect that accepts an value (ZC_NOTIFY_EFFECT3).
  8049. /// 0284 <id>.L <effect id>.L <num data>.L
  8050. /// effect id:
  8051. /// @see doc/effect_list.txt
  8052. /// num data:
  8053. /// effect-dependent value
  8054. void clif_specialeffect_value(struct block_list* bl, int effect_id, int num, send_target target)
  8055. {
  8056. uint8 buf[14];
  8057. WBUFW(buf,0) = 0x284;
  8058. WBUFL(buf,2) = bl->id;
  8059. WBUFL(buf,6) = effect_id;
  8060. WBUFL(buf,10) = num;
  8061. clif_send(buf, packet_len(0x284), bl, target);
  8062. if( disguised(bl) )
  8063. {
  8064. WBUFL(buf,2) = disguised_bl_id(bl->id);
  8065. clif_send(buf, packet_len(0x284), bl, SELF);
  8066. }
  8067. }
  8068. void clif_specialeffect_remove(struct block_list* bl_src, int effect, enum send_target e_target, struct block_list* bl_target)
  8069. {
  8070. #if PACKETVER >= 20181002
  8071. nullpo_retv( bl_src );
  8072. nullpo_retv( bl_target );
  8073. PACKET_ZC_REMOVE_EFFECT p = {};
  8074. p.packetType = HEADER_ZC_REMOVE_EFFECT;
  8075. p.aid = bl_src->id;
  8076. p.effectId = effect;
  8077. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_target, e_target );
  8078. if( disguised(bl_src) )
  8079. {
  8080. p.aid = disguised_bl_id( bl_src->id );
  8081. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_src, SELF );
  8082. }
  8083. #endif
  8084. }
  8085. /// Monster/NPC color chat [SnakeDrak] (ZC_NPC_CHAT).
  8086. /// 02c1 <packet len>.W <id>.L <color>.L <message>.?B
  8087. void clif_messagecolor_target(struct block_list *bl, unsigned long color, const char *msg, bool rgb2bgr, enum send_target type, map_session_data *sd) {
  8088. unsigned short msg_len = (unsigned short)(strlen(msg) + 1);
  8089. uint8 buf[CHAT_SIZE_MAX];
  8090. nullpo_retv(bl);
  8091. if (msg_len > sizeof(buf) - 12) {
  8092. ShowWarning("clif_messagecolor: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  8093. msg_len = sizeof(buf) - 12;
  8094. }
  8095. if (rgb2bgr)
  8096. color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16; // RGB to BGR
  8097. WBUFW(buf,0) = 0x2C1;
  8098. WBUFW(buf,2) = msg_len + 12;
  8099. WBUFL(buf,4) = bl->id;
  8100. WBUFL(buf,8) = color;
  8101. memcpy(WBUFCP(buf,12), msg, msg_len);
  8102. clif_send(buf, WBUFW(buf,2), (sd == nullptr ? bl : &(sd->bl)), type);
  8103. }
  8104. /**
  8105. * Notifies the client that the storage window is still open
  8106. *
  8107. * Should only be used in cases where the client closed the
  8108. * storage window without server's consent
  8109. */
  8110. void clif_refresh_storagewindow(map_session_data *sd) {
  8111. // Notify the client that the storage is open
  8112. if( sd->state.storage_flag == 1 ) {
  8113. storage_sortitem(sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage));
  8114. clif_storagelist(sd, sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage), storage_getName(0));
  8115. clif_updatestorageamount(*sd, sd->storage.amount, sd->storage.max_amount);
  8116. }
  8117. // Notify the client that the gstorage is open otherwise it will
  8118. // remain locked forever and nobody will be able to access it
  8119. if( sd->state.storage_flag == 2 ) {
  8120. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  8121. if( !gstor ) // Shouldn't happen. The information should already be at the map-server
  8122. intif_request_guild_storage(sd->status.account_id, sd->status.guild_id);
  8123. else {
  8124. storage_sortitem(gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild));
  8125. clif_storagelist(sd, gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild), "Guild Storage");
  8126. clif_updatestorageamount(*sd, gstor->amount, gstor->max_amount);
  8127. }
  8128. }
  8129. // Notify the client that the premium storage is open
  8130. if (sd->state.storage_flag == 3) {
  8131. storage_sortitem(sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage));
  8132. clif_storagelist(sd, sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage), storage_getName(sd->premiumStorage.stor_id));
  8133. clif_updatestorageamount(*sd, sd->premiumStorage.amount, sd->premiumStorage.max_amount);
  8134. }
  8135. }
  8136. // refresh the client's screen, getting rid of any effects
  8137. void clif_refresh(map_session_data *sd)
  8138. {
  8139. nullpo_retv(sd);
  8140. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8141. clif_inventorylist(sd);
  8142. clif_equipswitch_list(sd);
  8143. if(pc_iscarton(sd)) {
  8144. clif_cartlist(sd);
  8145. clif_updatestatus(*sd,SP_CARTINFO);
  8146. }
  8147. clif_updatestatus(*sd,SP_WEIGHT);
  8148. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8149. clif_updatestatus(*sd,SP_STR);
  8150. clif_updatestatus(*sd,SP_AGI);
  8151. clif_updatestatus(*sd,SP_VIT);
  8152. clif_updatestatus(*sd,SP_INT);
  8153. clif_updatestatus(*sd,SP_DEX);
  8154. clif_updatestatus(*sd,SP_LUK);
  8155. #ifdef RENEWAL
  8156. clif_updatestatus(*sd,SP_POW);
  8157. clif_updatestatus(*sd,SP_STA);
  8158. clif_updatestatus(*sd,SP_WIS);
  8159. clif_updatestatus(*sd,SP_SPL);
  8160. clif_updatestatus(*sd,SP_CON);
  8161. clif_updatestatus(*sd,SP_CRT);
  8162. #endif
  8163. if (sd->spiritball)
  8164. clif_spiritball( &sd->bl, &sd->bl, SELF );
  8165. clif_millenniumshield_single( *sd, *sd );
  8166. clif_spiritcharm_single( *sd, *sd );
  8167. if (sd->soulball)
  8168. clif_soulball( sd, &sd->bl, SELF );
  8169. if (sd->servantball)
  8170. clif_servantball( *sd, &sd->bl, SELF );
  8171. if (sd->abyssball)
  8172. clif_abyssball( *sd, &sd->bl, SELF );
  8173. if (sd->vd.cloth_color)
  8174. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8175. if (sd->vd.body_style)
  8176. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8177. if(hom_is_active(sd->hd))
  8178. clif_send_homdata( *sd->hd, SP_ACK );
  8179. if( sd->md ) {
  8180. clif_mercenary_info(sd);
  8181. clif_mercenary_skillblock(sd);
  8182. }
  8183. if( sd->ed )
  8184. clif_elemental_info(sd);
  8185. map_foreachinallrange(clif_getareachar,&sd->bl,AREA_SIZE,BL_ALL,sd);
  8186. clif_weather_check(sd);
  8187. if( sd->chatID )
  8188. chat_leavechat(sd,0);
  8189. if( sd->state.vending )
  8190. clif_openvending( *sd );
  8191. if( pc_issit(sd) )
  8192. clif_sitting(sd->bl); // FIXME: just send to self, not area
  8193. if( pc_isdead(sd) ) // When you refresh, resend the death packet.
  8194. clif_clearunit_single( sd->bl.id, CLR_DEAD, *sd );
  8195. else
  8196. clif_changed_dir(sd->bl, SELF);
  8197. clif_efst_status_change_sub(&sd->bl,&sd->bl,SELF);
  8198. //Issue #2143
  8199. //Cancel Trading State
  8200. if (sd->state.trading)
  8201. trade_tradecancel(sd);
  8202. //Cancel Buying/Selling State
  8203. if (sd->state.buyingstore)
  8204. buyingstore_close(sd);
  8205. mail_clear(sd);
  8206. clif_loadConfirm( sd );
  8207. if( disguised(&sd->bl) ) {/* refresh-da */
  8208. short disguise = sd->disguise;
  8209. pc_disguise(sd, 0);
  8210. pc_disguise(sd, disguise);
  8211. }
  8212. clif_refresh_storagewindow(sd);
  8213. }
  8214. /// Updates the object's (bl) name on client.
  8215. /// Used to update when a char leaves a party/guild. [Skotlex]
  8216. /// Needed because when you send a 0x95 packet, the client will not remove the cached party/guild info that is not sent.
  8217. /// 0095 <id>.L <char name>.24B (ZC_ACK_REQNAME)
  8218. /// 0195 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B (ZC_ACK_REQNAMEALL)
  8219. /// 0a30 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B <title ID>.L (ZC_ACK_REQNAMEALL2)
  8220. void clif_name( struct block_list* src, struct block_list *bl, send_target target ){
  8221. nullpo_retv( src );
  8222. nullpo_retv( bl );
  8223. switch( bl->type ){
  8224. case BL_PC: {
  8225. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8226. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8227. packet.gid = bl->id;
  8228. map_session_data *sd = (map_session_data *)bl;
  8229. //Requesting your own "shadow" name. [Skotlex]
  8230. if( src == bl && target == SELF && sd->disguise ){
  8231. packet.gid = -bl->id;
  8232. }
  8233. if( sd->fakename[0] ) {
  8234. safestrncpy( packet.name, sd->fakename, NAME_LENGTH );
  8235. clif_send( &packet, sizeof(packet), src, target );
  8236. return;
  8237. }
  8238. safestrncpy( packet.name, sd->status.name, NAME_LENGTH );
  8239. party_data *p = nullptr;
  8240. if( sd->status.party_id ){
  8241. p = party_search( sd->status.party_id );
  8242. }
  8243. // do not display party unless the player is also in a guild
  8244. if( p && ( sd->guild || battle_config.display_party_name ) ){
  8245. safestrncpy( packet.party_name, p->party.name, NAME_LENGTH );
  8246. }
  8247. if( sd->guild ){
  8248. int position;
  8249. // Will get the position of the guild the player is in
  8250. position = guild_getposition(*sd);
  8251. safestrncpy( packet.guild_name, sd->guild->guild.name, NAME_LENGTH );
  8252. safestrncpy( packet.position_name, sd->guild->guild.position[position].name, NAME_LENGTH );
  8253. }else if( sd->clan ){
  8254. safestrncpy( packet.position_name, sd->clan->name, NAME_LENGTH );
  8255. }
  8256. #if PACKETVER_MAIN_NUM >= 20150225 || PACKETVER_RE_NUM >= 20141126 || defined( PACKETVER_ZERO )
  8257. packet.title_id = sd->status.title_id; // Title ID
  8258. #endif
  8259. clif_send(&packet, sizeof(packet), src, target);
  8260. }
  8261. break;
  8262. //[blackhole89]
  8263. case BL_HOM:
  8264. case BL_MER:
  8265. case BL_PET:
  8266. case BL_NPC:
  8267. case BL_ELEM: {
  8268. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8269. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8270. packet.gid = bl->id;
  8271. switch (bl->type) {
  8272. case BL_HOM:
  8273. memcpy(packet.name, ((TBL_HOM *)bl)->homunculus.name, NAME_LENGTH);
  8274. break;
  8275. case BL_MER:
  8276. memcpy(packet.name, ((TBL_MER *)bl)->db->name.c_str(), NAME_LENGTH);
  8277. break;
  8278. case BL_PET:
  8279. safestrncpy(packet.name, ((TBL_PET *)bl)->pet.name, NAME_LENGTH);
  8280. break;
  8281. case BL_NPC:
  8282. safestrncpy(packet.name, ((TBL_NPC *)bl)->name, NAME_LENGTH);
  8283. break;
  8284. case BL_ELEM:
  8285. safestrncpy(packet.name, ((TBL_ELEM *)bl)->db->name.c_str(), NAME_LENGTH);
  8286. break;
  8287. }
  8288. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8289. unit_data *ud = unit_bl2ud(bl);
  8290. if (ud != nullptr) {
  8291. memcpy(packet.title, ud->title, NAME_LENGTH);
  8292. packet.groupId = ud->group_id;
  8293. }
  8294. #endif
  8295. clif_send(&packet, sizeof(packet), src, target);
  8296. }
  8297. break;
  8298. case BL_MOB: {
  8299. mob_data *md = (mob_data *)bl;
  8300. if( md->guardian_data && md->guardian_data->guild_id ){
  8301. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8302. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8303. packet.gid = bl->id;
  8304. safestrncpy( packet.name, md->name, NAME_LENGTH );
  8305. safestrncpy( packet.guild_name, md->guardian_data->guild_name, NAME_LENGTH );
  8306. safestrncpy( packet.position_name, md->guardian_data->castle->castle_name, NAME_LENGTH );
  8307. clif_send(&packet, sizeof(packet), src, target);
  8308. }else if( battle_config.show_mob_info ){
  8309. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8310. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8311. packet.gid = bl->id;
  8312. safestrncpy( packet.name, md->name, NAME_LENGTH );
  8313. char mobhp[50], *str_p = mobhp;
  8314. if( battle_config.show_mob_info&4 ){
  8315. str_p += sprintf( str_p, "Lv. %d | ", md->level );
  8316. }
  8317. if( battle_config.show_mob_info&1 ){
  8318. str_p += sprintf( str_p, "HP: %u/%u | ", md->status.hp, md->status.max_hp );
  8319. }
  8320. if( battle_config.show_mob_info&2 ){
  8321. str_p += sprintf( str_p, "HP: %u%% | ", get_percentage( md->status.hp, md->status.max_hp ) );
  8322. }
  8323. // Even thought mobhp ain't a name, we send it as one so the client can parse it. [Skotlex]
  8324. if( str_p != mobhp ){
  8325. *(str_p-3) = '\0'; //Remove trailing space + pipe.
  8326. safestrncpy( packet.party_name, mobhp, NAME_LENGTH );
  8327. }
  8328. clif_send(&packet, sizeof(packet), src, target);
  8329. } else {
  8330. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8331. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8332. packet.gid = bl->id;
  8333. safestrncpy(packet.name, md->name, NAME_LENGTH);
  8334. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8335. unit_data *ud = unit_bl2ud(bl);
  8336. if (ud != nullptr) {
  8337. memcpy(packet.title, ud->title, NAME_LENGTH);
  8338. packet.groupId = ud->group_id;
  8339. }
  8340. #endif
  8341. clif_send(&packet, sizeof(packet), src, target);
  8342. }
  8343. }
  8344. break;
  8345. case BL_CHAT:
  8346. case BL_SKILL:
  8347. // Newer clients request this, but do not need an answer
  8348. return;
  8349. default:
  8350. ShowError("clif_name: bad type %d(%d)\n", bl->type, bl->id);
  8351. return;
  8352. }
  8353. }
  8354. /// Taekwon Jump (TK_HIGHJUMP) effect (ZC_HIGHJUMP).
  8355. /// 01ff <id>.L <x>.W <y>.W
  8356. ///
  8357. /// Visually moves(instant) a character to x,y. The char moves even
  8358. /// when the target cell isn't walkable. If the char is sitting it
  8359. /// stays that way.
  8360. void clif_slide(block_list& bl, int x, int y){
  8361. PACKET_ZC_HIGHJUMP p{};
  8362. p.packetType = HEADER_ZC_HIGHJUMP;
  8363. p.srcId = bl.id;
  8364. p.x = x;
  8365. p.y = y;
  8366. clif_send(&p, sizeof(p), &bl, AREA);
  8367. if( disguised(&bl) )
  8368. {
  8369. p.srcId = disguised_bl_id(bl.id);
  8370. clif_send(&p, sizeof(p), &bl, SELF);
  8371. }
  8372. }
  8373. /// Public chat message (ZC_NOTIFY_CHAT). lordalfa/Skotlex - used by @me as well
  8374. /// 008d <packet len>.W <id>.L <message>.?B
  8375. void clif_disp_overhead_(struct block_list *bl, const char* mes, enum send_target flag)
  8376. {
  8377. unsigned char buf[256]; //This should be more than sufficient, the theorical max is CHAT_SIZE + 8 (pads and extra inserted crap)
  8378. int16 len_mes;
  8379. len_mes = static_cast<decltype(len_mes)>( strlen(mes) + 1 ); //Account for \0
  8380. if (len_mes > sizeof(buf)-8) {
  8381. ShowError("clif_disp_overhead: Message too long (length %d)\n", len_mes);
  8382. len_mes = sizeof(buf)-8; //Trunk it to avoid problems.
  8383. }
  8384. // send message to others
  8385. if (flag == AREA) {
  8386. WBUFW(buf,0) = 0x8d;
  8387. WBUFW(buf,2) = len_mes + 8; // len of message + 8 (command+len+id)
  8388. WBUFL(buf,4) = bl->id;
  8389. safestrncpy(WBUFCP(buf,8), mes, len_mes);
  8390. clif_send(buf, WBUFW(buf,2), bl, AREA_CHAT_WOC);
  8391. }
  8392. // send back message to the speaker
  8393. if( bl->type == BL_PC ) {
  8394. WBUFW(buf,0) = 0x8e;
  8395. WBUFW(buf, 2) = len_mes + 4;
  8396. safestrncpy(WBUFCP(buf,4), mes, len_mes);
  8397. clif_send(buf, WBUFW(buf,2), bl, SELF);
  8398. }
  8399. }
  8400. /*==========================
  8401. * Minimap fix [Kevin]
  8402. * Remove dot from minimap
  8403. *--------------------------*/
  8404. void clif_party_xy_remove(map_session_data* sd)
  8405. {
  8406. nullpo_retv(sd);
  8407. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p{};
  8408. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  8409. p.AID = sd->status.account_id;
  8410. p.xPos = -1;
  8411. p.yPos = -1;
  8412. clif_send(&p, sizeof(p), &sd->bl, PARTY_SAMEMAP_WOS);
  8413. }
  8414. /// Displays a skill message (thanks to Rayce) (ZC_SKILLMSG).
  8415. /// 0215 <msg id>.L
  8416. /// msg id:
  8417. /// 0x15 = End all negative status (PA_GOSPEL)
  8418. /// 0x16 = Immunity to all status (PA_GOSPEL)
  8419. /// 0x17 = MaxHP +100% (PA_GOSPEL)
  8420. /// 0x18 = MaxSP +100% (PA_GOSPEL)
  8421. /// 0x19 = All stats +20 (PA_GOSPEL)
  8422. /// 0x1c = Enchant weapon with Holy element (PA_GOSPEL)
  8423. /// 0x1d = Enchant armor with Holy element (PA_GOSPEL)
  8424. /// 0x1e = DEF +25% (PA_GOSPEL)
  8425. /// 0x1f = ATK +100% (PA_GOSPEL)
  8426. /// 0x20 = HIT/Flee +50 (PA_GOSPEL)
  8427. /// 0x28 = Full strip failed because of coating (ST_FULLSTRIP)
  8428. /// ? = nothing
  8429. void clif_gospel_info(map_session_data *sd, int type)
  8430. {
  8431. int fd=sd->fd;
  8432. WFIFOHEAD(fd,packet_len(0x215));
  8433. WFIFOW(fd,0)=0x215;
  8434. WFIFOL(fd,2)=type;
  8435. WFIFOSET(fd, packet_len(0x215));
  8436. }
  8437. /// Multi-purpose mission information packet (ZC_STARSKILL).
  8438. /// 020e <mapname>.24B <monster_id>.L <star>.B <result>.B
  8439. /// result:
  8440. /// 0 = Star Gladiator %s has designed <mapname>'s as the %s.
  8441. /// star:
  8442. /// 0 = Place of the Sun
  8443. /// 1 = Place of the Moon
  8444. /// 2 = Place of the Stars
  8445. /// 1 = Star Gladiator %s's %s: <mapname>
  8446. /// star:
  8447. /// 0 = Place of the Sun
  8448. /// 1 = Place of the Moon
  8449. /// 2 = Place of the Stars
  8450. /// 10 = Star Gladiator %s has designed <mapname>'s as the %s.
  8451. /// star:
  8452. /// 0 = Target of the Sun
  8453. /// 1 = Target of the Moon
  8454. /// 2 = Target of the Stars
  8455. /// 11 = Star Gladiator %s's %s: <mapname used as monster name>
  8456. /// star:
  8457. /// 0 = Monster of the Sun
  8458. /// 1 = Monster of the Moon
  8459. /// 2 = Monster of the Stars
  8460. /// 20 = [TaeKwon Mission] Target Monster : <mapname used as monster name> (<star>%)
  8461. /// 21 = [Taming Mission] Target Monster : <mapname used as monster name>
  8462. /// 22 = [Collector Rank] Target Item : <monster_id used as item id>
  8463. /// 30 = [Sun, Moon and Stars Angel] Designed places and monsters have been reset.
  8464. /// 40 = Target HP : <monster_id used as HP>
  8465. void clif_starskill(map_session_data* sd, const char* mapname, int monster_id, unsigned char star, unsigned char result)
  8466. {
  8467. int fd = sd->fd;
  8468. WFIFOHEAD(fd,packet_len(0x20e));
  8469. WFIFOW(fd,0) = 0x20e;
  8470. safestrncpy(WFIFOCP(fd,2), mapname, NAME_LENGTH);
  8471. WFIFOL(fd,26) = monster_id;
  8472. WFIFOB(fd,30) = star;
  8473. WFIFOB(fd,31) = result;
  8474. WFIFOSET(fd,packet_len(0x20e));
  8475. }
  8476. /*==========================================
  8477. * Info about Star Glaldiator save map [Komurka]
  8478. * type: 1: Information, 0: Map registered
  8479. *------------------------------------------*/
  8480. void clif_feel_info(map_session_data* sd, unsigned char feel_level, unsigned char type)
  8481. {
  8482. char mapname[MAP_NAME_LENGTH_EXT];
  8483. mapindex_getmapname_ext(mapindex_id2name(sd->feel_map[feel_level].index), mapname);
  8484. clif_starskill(sd, mapname, 0, feel_level, type ? 1 : 0);
  8485. }
  8486. /*==========================================
  8487. * Info about Star Glaldiator hate mob [Komurka]
  8488. * type: 1: Register mob, 0: Information.
  8489. *------------------------------------------*/
  8490. void clif_hate_info(map_session_data *sd, unsigned char hate_level,int class_, unsigned char type)
  8491. {
  8492. if( pcdb_checkid(class_) ) {
  8493. clif_starskill(sd, job_name(class_), class_, hate_level, type ? 10 : 11);
  8494. } else if( mobdb_checkid(class_) ) {
  8495. clif_starskill(sd, mob_db.find(class_)->jname.c_str(), class_, hate_level, type ? 10 : 11);
  8496. } else {
  8497. ShowWarning("clif_hate_info: Received invalid class %d for this packet (char_id=%d, hate_level=%u, type=%u).\n", class_, sd->status.char_id, (uint32)hate_level, (uint32)type);
  8498. }
  8499. }
  8500. /*==========================================
  8501. * Info about TaeKwon Do TK_MISSION mob [Skotlex]
  8502. *------------------------------------------*/
  8503. void clif_mission_info(map_session_data *sd, int mob_id, unsigned char progress)
  8504. {
  8505. clif_starskill(sd, mob_db.find(mob_id)->jname.c_str(), mob_id, progress, 20);
  8506. }
  8507. /*==========================================
  8508. * Feel/Hate reset (thanks to Rayce) [Skotlex]
  8509. *------------------------------------------*/
  8510. void clif_feel_hate_reset(map_session_data *sd)
  8511. {
  8512. clif_starskill(sd, "", 0, 0, 30);
  8513. }
  8514. /// Send out reply to configuration change
  8515. /// 02d9 <type>.L <value>.L (ZC_CONFIG)
  8516. /// type: see enum e_config_type
  8517. /// value:
  8518. /// false = disabled
  8519. /// true = enabled
  8520. void clif_configuration( map_session_data* sd, enum e_config_type type, bool enabled ){
  8521. int fd;
  8522. nullpo_retv(sd);
  8523. fd = sd->fd;
  8524. WFIFOHEAD(fd, packet_len(0x2d9));
  8525. WFIFOW(fd, 0) = 0x2d9;
  8526. WFIFOL(fd, 2) = type;
  8527. WFIFOL(fd, 6) = enabled;
  8528. WFIFOSET(fd, packet_len(0x2d9));
  8529. }
  8530. /// The player's 'view equip' state, sent during login (ZC_CONFIG_NOTIFY).
  8531. /// 02da <open equip window>.B
  8532. /// open equip window:
  8533. /// 0 = disabled
  8534. /// 1 = enabled
  8535. void clif_equipcheckbox(map_session_data* sd)
  8536. {
  8537. int fd;
  8538. nullpo_retv(sd);
  8539. fd = sd->fd;
  8540. WFIFOHEAD(fd, packet_len(0x2da));
  8541. WFIFOW(fd, 0) = 0x2da;
  8542. WFIFOB(fd, 2) = (sd->status.show_equip ? 1 : 0);
  8543. WFIFOSET(fd, packet_len(0x2da));
  8544. }
  8545. /// Sends info about a player's equipped items.
  8546. /// 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)
  8547. /// 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)
  8548. /// 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)
  8549. /// 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)
  8550. /// 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)
  8551. /// 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)
  8552. void clif_viewequip_ack( map_session_data& sd, map_session_data& tsd ){
  8553. #if PACKETVER_AD_NUM >= 20071211 || PACKETVER_SAK_NUM >= 20071127 || PACKETVER_MAIN_NUM >= 20071211 || defined(PACKETVER_RE)
  8554. PACKET_ZC_EQUIPWIN_MICROSCOPE* p = reinterpret_cast<PACKET_ZC_EQUIPWIN_MICROSCOPE*>( packet_buffer );
  8555. p->PacketType = HEADER_ZC_EQUIPWIN_MICROSCOPE;
  8556. p->PacketLength = sizeof( *p );
  8557. safestrncpy( p->characterName, tsd.status.name, NAME_LENGTH );
  8558. p->job = tsd.status.class_;
  8559. p->head = tsd.vd.hair_style;
  8560. p->accessory = tsd.vd.head_bottom;
  8561. p->accessory2 = tsd.vd.head_mid;
  8562. p->accessory3 = tsd.vd.head_top;
  8563. #if PACKETVER >= 20110111
  8564. p->robe = tsd.vd.robe;
  8565. #endif
  8566. p->headpalette = tsd.vd.hair_color;
  8567. p->bodypalette = tsd.vd.cloth_color;
  8568. #if PACKETVER_MAIN_NUM >= 20180801 || PACKETVER_RE_NUM >= 20180801 || PACKETVER_ZERO_NUM >= 20180808
  8569. p->body2 = tsd.vd.body_style;
  8570. #endif
  8571. p->sex = tsd.vd.sex;
  8572. for( int i = 0, equip = 0; i < EQI_MAX; i++ ){
  8573. int k = tsd.equip_index[i];
  8574. if( k >= 0 ){
  8575. if( tsd.inventory.u.items_inventory[k].nameid == 0 || tsd.inventory_data[k] == nullptr ){ // Item doesn't exist
  8576. continue;
  8577. }
  8578. if( !tsd.inventory.u.items_inventory[k].equip ){
  8579. continue;
  8580. }
  8581. if( !itemdb_isequip2( tsd.inventory_data[k] ) ){ // Is not equippable
  8582. continue;
  8583. }
  8584. clif_item_equip( client_index( k ), &p->list[equip], &tsd.inventory.u.items_inventory[k], tsd.inventory_data[k], pc_equippoint( &tsd, k ) );
  8585. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->list[0] ) );
  8586. equip++;
  8587. }
  8588. }
  8589. clif_send( p, p->PacketLength, &sd.bl, SELF );
  8590. #endif
  8591. }
  8592. /// Display msgstringtable.txt string (ZC_MSG).
  8593. /// 0291 <message>.W
  8594. void clif_msg(map_session_data* sd, unsigned short id)
  8595. {
  8596. int fd;
  8597. nullpo_retv(sd);
  8598. fd = sd->fd;
  8599. WFIFOHEAD(fd, packet_len(0x291));
  8600. WFIFOW(fd, 0) = 0x291;
  8601. WFIFOW(fd, 2) = id; // zero-based msgstringtable.txt index
  8602. WFIFOSET(fd, packet_len(0x291));
  8603. }
  8604. /// Display msgstringtable.txt string and fill in a valid for %d format (ZC_MSG_VALUE).
  8605. /// 0x7e2 <message>.W <value>.L
  8606. void clif_msg_value(map_session_data* sd, unsigned short id, int value)
  8607. {
  8608. int fd = sd->fd;
  8609. WFIFOHEAD(fd, packet_len(0x7e2));
  8610. WFIFOW(fd,0) = 0x7e2;
  8611. WFIFOW(fd,2) = id;
  8612. WFIFOL(fd,4) = value;
  8613. WFIFOSET(fd, packet_len(0x7e2));
  8614. }
  8615. /// Displays msgstringtable.txt string, prefixed with a skill name. (ZC_MSG_SKILL).
  8616. /// 07e6 <skill id>.W <msg id>.L
  8617. ///
  8618. /// NOTE: Message has following format and is printed in color 0xCDCDFF (purple):
  8619. /// "[SkillName] Message"
  8620. void clif_msg_skill(map_session_data* sd, uint16 skill_id, int msg_id)
  8621. {
  8622. int fd = sd->fd;
  8623. WFIFOHEAD(fd, packet_len(0x7e6));
  8624. WFIFOW(fd,0) = 0x7e6;
  8625. WFIFOW(fd,2) = skill_id;
  8626. WFIFOL(fd,4) = msg_id;
  8627. WFIFOSET(fd, packet_len(0x7e6));
  8628. }
  8629. /// Displays msgstringtable.txt string in a color. (ZC_MSG_COLOR).
  8630. /// 09cd <msg id>.W <color>.L
  8631. void clif_msg_color( map_session_data *sd, uint16 msg_id, uint32 color ){
  8632. nullpo_retv(sd);
  8633. int fd = sd->fd;
  8634. WFIFOHEAD(fd, packet_len(0x9cd));
  8635. WFIFOW(fd, 0) = 0x9cd;
  8636. WFIFOW(fd, 2) = msg_id;
  8637. WFIFOL(fd, 4) = color;
  8638. WFIFOSET(fd, packet_len(0x9cd));
  8639. }
  8640. /// Validates one global/guild/party/whisper message packet and tries to recognize its components.
  8641. /// Returns true if the packet was parsed successfully.
  8642. /// Formats: false - <packet id>.w <packet len>.w (<name> : <message>).?B 00
  8643. /// true - <packet id>.w <packet len>.w <name>.24B <message>.?B 00
  8644. static bool clif_process_message(map_session_data* sd, bool whisperFormat, char* out_name, char* out_message, char* out_full_message ){
  8645. const char* seperator = " : ";
  8646. int fd;
  8647. struct s_packet_db* info;
  8648. uint16 packetLength, inputLength;
  8649. const char *input, *name, *message;
  8650. size_t nameLength, messageLength;
  8651. fd = sd->fd;
  8652. info = &packet_db[RFIFOW(fd,0)];
  8653. packetLength = RFIFOW(fd,info->pos[0]);
  8654. input = RFIFOCP(fd,info->pos[1]);
  8655. // basic structure check for the 4-byte header
  8656. if( packetLength < 4 ){
  8657. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no message data)!\n", sd->status.name);
  8658. return false;
  8659. }else{
  8660. inputLength = packetLength - 4;
  8661. }
  8662. // process <name> part of the packet
  8663. if( whisperFormat ) {
  8664. // name has fixed width
  8665. if( inputLength < NAME_LENGTH + 1 ) {
  8666. ShowWarning("clif_process_message: Received malformed packet from player '%s' (packet length is incorrect)!\n", sd->status.name);
  8667. return false;
  8668. }
  8669. name = input;
  8670. // validate name
  8671. nameLength = strnlen( name, NAME_LENGTH - 1 );
  8672. // only restriction is that the name must be zero-terminated
  8673. if( name[nameLength] != '\0' ) {
  8674. ShowWarning("clif_process_message: Player '%s' sent an unterminated name!\n", sd->status.name);
  8675. return false;
  8676. }
  8677. message = input + NAME_LENGTH;
  8678. messageLength = inputLength - NAME_LENGTH;
  8679. }else{
  8680. // name and message are separated by ' : '
  8681. size_t seperatorLength = strnlen( seperator, NAME_LENGTH );
  8682. nameLength = strnlen( sd->status.name, NAME_LENGTH - 1 ); // name length (w/o zero byte)
  8683. // check if there's enough data provided
  8684. if( inputLength < nameLength + seperatorLength + 1 ){
  8685. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no username data)!\n", sd->status.name);
  8686. return false;
  8687. }
  8688. name = input;
  8689. // validate name
  8690. if( strncmp( name, sd->status.name, nameLength ) || // the text must start with the speaker's name
  8691. strncmp( name + nameLength, seperator, seperatorLength ) ) // followed by the seperator
  8692. {
  8693. //Hacked message, or infamous "client desynch" issue where they pick one char while loading another.
  8694. ShowWarning("clif_process_message: Player '%s' sent a message using an incorrect name! Forcing a relog...\n", sd->status.name);
  8695. set_eof(sd->fd); // Just kick them out to correct it.
  8696. return false;
  8697. }
  8698. message = input + nameLength + seperatorLength;
  8699. messageLength = inputLength - nameLength - seperatorLength;
  8700. }
  8701. #if PACKETVER < 20151001
  8702. // the declared length must match real length
  8703. if( messageLength != strnlen(message, messageLength)+1 ) {
  8704. ShowWarning("clif_process_message: Received malformed packet from player '%s' (length is incorrect)!\n", sd->status.name);
  8705. return false;
  8706. }
  8707. // verify <message> part of the packet
  8708. if( message[messageLength-1] != '\0' ) { // message must be zero-terminated
  8709. ShowWarning("clif_process_message: Player '%s' sent an unterminated message string!\n", sd->status.name);
  8710. return false;
  8711. }
  8712. #else
  8713. // No zero termination anymore
  8714. messageLength += 1;
  8715. #endif
  8716. // messages mustn't be too long
  8717. if( messageLength > CHAT_SIZE_MAX-1 ) {
  8718. // Normally you can only enter CHATBOX_SIZE-1 letters into the chat box, but Frost Joke / Dazzler's text can be longer.
  8719. // Also, the physical size of strings that use multibyte encoding can go multiple times over the chatbox capacity.
  8720. // Neither the official client nor server place any restriction on the length of the data in the packet,
  8721. // but we'll only allow reasonably long strings here. This also makes sure that they fit into the `chatlog` table.
  8722. ShowWarning("clif_process_message: Player '%s' sent a message too long ('%.*s')!\n", sd->status.name, CHAT_SIZE_MAX-1, message);
  8723. return false;
  8724. }
  8725. // If it is not a whisper message, set the name to the fakename of the player
  8726. if( whisperFormat == false && sd->fakename[0] != '\0' ){
  8727. strcpy( out_name, sd->fakename );
  8728. }else{
  8729. safestrncpy( out_name, name, nameLength + 1 );
  8730. }
  8731. safestrncpy( out_message, message, messageLength );
  8732. if( whisperFormat ){
  8733. sprintf( out_full_message, "%-24s%s", out_name, out_message );
  8734. out_full_message[nameLength] = '\0';
  8735. }else{
  8736. sprintf( out_full_message, "%s%s%s", out_name, seperator, out_message );
  8737. }
  8738. if( is_atcommand( fd, sd, out_message, 1 ) )
  8739. return false;
  8740. if (sd->sc.cant.chat || (sd->state.block_action & PCBLOCK_CHAT))
  8741. return false; //no "chatting" while muted.
  8742. if( battle_config.min_chat_delay ) { //[Skotlex]
  8743. if (DIFF_TICK(sd->cantalk_tick, gettick()) > 0)
  8744. return false;
  8745. sd->cantalk_tick = gettick() + battle_config.min_chat_delay;
  8746. }
  8747. if (battle_config.idletime_option&IDLE_CHAT)
  8748. sd->idletime = last_tick;
  8749. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_CHAT)
  8750. sd->idletime_hom = last_tick;
  8751. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_CHAT)
  8752. sd->idletime_mer = last_tick;
  8753. //achievement_update_objective(sd, AG_CHATTING, 1, 1); // !TODO: Confirm how this achievement is triggered
  8754. return true;
  8755. }
  8756. /**
  8757. * Displays a message if the player enters a PK Zone (during pk_mode)
  8758. * @param sd: Player data
  8759. */
  8760. inline void clif_pk_mode_message(map_session_data * sd)
  8761. {
  8762. if (battle_config.pk_mode && battle_config.pk_mode_mes &&
  8763. sd && map_getmapflag(sd->bl.m, MF_PVP)) {
  8764. if( (int)sd->status.base_level < battle_config.pk_min_level ) {
  8765. char output[CHAT_SIZE_MAX];
  8766. // 1504: You've entered a PK Zone (safe until level %d).
  8767. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(sd,1504),
  8768. battle_config.pk_min_level);
  8769. clif_showscript(&sd->bl, output, SELF);
  8770. } else {
  8771. // 1503: You've entered a PK Zone.
  8772. clif_showscript(&sd->bl, msg_txt(sd,1503), SELF);
  8773. }
  8774. }
  8775. return;
  8776. }
  8777. // ---------------------
  8778. // clif_parse_wanttoconnect
  8779. // ---------------------
  8780. // Parses a WantToConnection packet to try to identify which is the packet version used. [Skotlex]
  8781. // error codes:
  8782. // 0 - Success
  8783. // 1 - Unknown packet_ver
  8784. // 2 - Invalid account_id
  8785. // 3 - Invalid char_id
  8786. // 4 - Invalid login_id1 (reserved)
  8787. // 5 - Invalid client_tick (reserved)
  8788. // 6 - Invalid sex
  8789. // Only the first 'invalid' error that appears is used.
  8790. static int clif_parse_WantToConnection_sub(int fd)
  8791. {
  8792. int value; //Value is used to temporarily store account/char_id/sex
  8793. //By default, start searching on the default one.
  8794. uint16 cmd = RFIFOW(fd, 0);
  8795. int16 packet_len;
  8796. packet_len = static_cast<decltype(packet_len)>( RFIFOREST( fd ) );
  8797. // FIXME: If the packet is not received at once, this will FAIL.
  8798. // Figure out, when it happens, that only part of the packet is
  8799. // received, or fix the function to be able to deal with that
  8800. // case.
  8801. if( packet_len != packet_db[cmd].len )
  8802. return 1; /* wrong length */
  8803. else if( (value=(int)RFIFOL(fd, packet_db[cmd].pos[0])) < START_ACCOUNT_NUM || value > END_ACCOUNT_NUM )
  8804. return 2; /* invalid account_id */
  8805. else if( (value=(int)RFIFOL(fd, packet_db[cmd].pos[1])) <= 0 )
  8806. return 3; /* invalid char_id */
  8807. /* RFIFOL(fd, packet_db[cmd].pos[2]) - don't care about login_id1 */
  8808. /* RFIFOL(fd, packet_db[cmd].pos[3]) - don't care about client_tick */
  8809. else if( (value=(int)RFIFOB(fd, packet_db[cmd].pos[4])) != 0 && value != 1 )
  8810. return 6; /* invalid sex */
  8811. else
  8812. return 0;
  8813. }
  8814. /// Request to connect to map-server.
  8815. /// 0072 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER)
  8816. /// 0436 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER2)
  8817. /// There are various variants of this packet, some of them have padding between fields.
  8818. void clif_parse_WantToConnection(int fd, map_session_data* sd)
  8819. {
  8820. struct block_list* bl;
  8821. struct auth_node* node;
  8822. int cmd, account_id, char_id, login_id1, sex, err;
  8823. t_tick client_tick; //The client tick is a tick, therefore it needs be unsigned. [Skotlex]
  8824. if (sd) {
  8825. ShowError("clif_parse_WantToConnection : invalid request (character already logged in)\n");
  8826. return;
  8827. }
  8828. cmd = RFIFOW(fd, 0);
  8829. account_id = RFIFOL(fd, packet_db[cmd].pos[0]);
  8830. char_id = RFIFOL(fd, packet_db[cmd].pos[1]);
  8831. login_id1 = RFIFOL(fd, packet_db[cmd].pos[2]);
  8832. client_tick = RFIFOL(fd, packet_db[cmd].pos[3]);
  8833. sex = RFIFOB(fd, packet_db[cmd].pos[4]);
  8834. err = clif_parse_WantToConnection_sub(fd);
  8835. if( err ){ // connection rejected
  8836. ShowInfo("clif_parse: Disconnecting session #%d with unknown connect packet 0x%04x(length:%d)%s\n", fd, cmd, RFIFOREST(fd), (
  8837. err == 1 ? "." :
  8838. err == 2 ? ", possibly for having an invalid account_id." :
  8839. err == 3 ? ", possibly for having an invalid char_id." :
  8840. /* Uncomment when checks are added in clif_parse_WantToConnection_sub. [FlavioJS]
  8841. err == 4 ? ", possibly for having an invalid login_id1." :
  8842. err == 5 ? ", possibly for having an invalid client_tick." :
  8843. */
  8844. err == 6 ? ", possibly for having an invalid sex." :
  8845. ". ERROR invalid error code"));
  8846. WFIFOHEAD(fd,packet_len(0x6a));
  8847. WFIFOW(fd,0) = 0x6a;
  8848. WFIFOB(fd,2) = err;
  8849. WFIFOSET(fd,packet_len(0x6a));
  8850. #ifdef DUMP_INVALID_PACKET
  8851. ShowDump(RFIFOP(fd, 0), RFIFOREST(fd));
  8852. #endif
  8853. RFIFOSKIP(fd, RFIFOREST(fd));
  8854. set_eof(fd);
  8855. return;
  8856. }
  8857. if( !global_core->is_running() ){ // not allowed
  8858. clif_authfail_fd(fd,1);// server closed
  8859. return;
  8860. }
  8861. //Check for double login.
  8862. bl = map_id2bl(account_id);
  8863. if(bl && bl->type != BL_PC) {
  8864. ShowError("clif_parse_WantToConnection: a non-player object already has id %d, please increase the starting account number\n", account_id);
  8865. WFIFOHEAD(fd,packet_len(0x6a));
  8866. WFIFOW(fd,0) = 0x6a;
  8867. WFIFOB(fd,2) = 3; // Rejected by server
  8868. WFIFOSET(fd,packet_len(0x6a));
  8869. set_eof(fd);
  8870. return;
  8871. }
  8872. if (bl ||
  8873. ((node=chrif_search(account_id)) && //An already existing node is valid only if it is for this login.
  8874. !(node->account_id == account_id && node->char_id == char_id && node->state == ST_LOGIN)))
  8875. {
  8876. clif_authfail_fd(fd, 8); //Still recognizes last connection
  8877. return;
  8878. }
  8879. CREATE(sd, TBL_PC, 1);
  8880. new(sd) map_session_data();
  8881. sd->fd = fd;
  8882. #ifdef PACKET_OBFUSCATION
  8883. sd->cryptKey = (((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF) * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF;
  8884. #endif
  8885. session[fd]->session_data = sd;
  8886. pc_setnewpc(sd, account_id, char_id, login_id1, client_tick, sex, fd);
  8887. #if PACKETVER < 20070521
  8888. WFIFOHEAD(fd,4);
  8889. WFIFOL(fd,0) = sd->bl.id;
  8890. WFIFOSET(fd,4);
  8891. #else
  8892. WFIFOHEAD(fd,packet_len(0x283));
  8893. WFIFOW(fd,0) = 0x283;
  8894. WFIFOL(fd,2) = sd->bl.id;
  8895. WFIFOSET(fd,packet_len(0x283));
  8896. #endif
  8897. chrif_authreq(sd,false);
  8898. }
  8899. /// Notification from the client, that it has finished map loading and is about to display player's character (CZ_NOTIFY_ACTORINIT).
  8900. /// 007d
  8901. void clif_parse_LoadEndAck(int fd,map_session_data *sd)
  8902. {
  8903. bool guild_notice = false;
  8904. if(sd->bl.prev != nullptr)
  8905. return;
  8906. // Autotraders should ignore this entirely, clif_parse_LoadEndAck is always invoked manually for them
  8907. if (!sd->state.active || (!sd->state.autotrade && !sd->state.pc_loaded)) { //Character loading is not complete yet!
  8908. //Let pc_reg_received or pc_scdata_received reinvoke this when ready.
  8909. sd->state.connect_new = 0;
  8910. return;
  8911. }
  8912. if (sd->state.rewarp) { //Rewarp player.
  8913. sd->state.rewarp = 0;
  8914. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8915. return;
  8916. }
  8917. sd->state.warping = 0;
  8918. // look
  8919. #if PACKETVER < 4
  8920. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8921. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8922. #else
  8923. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8924. #endif
  8925. pc_set_costume_view(sd);
  8926. if(sd->vd.cloth_color)
  8927. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8928. if(sd->vd.body_style)
  8929. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8930. // item
  8931. clif_inventorylist(sd); // inventory list first, otherwise deleted items in pc_checkitem show up as 'unknown item'
  8932. pc_checkitem(sd);
  8933. clif_equipswitch_list(sd);
  8934. // cart
  8935. if(pc_iscarton(sd)) {
  8936. clif_cartlist(sd);
  8937. clif_updatestatus(*sd,SP_CARTINFO);
  8938. }
  8939. // weight
  8940. clif_updatestatus(*sd,SP_WEIGHT);
  8941. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8942. // guild
  8943. // (needs to go before clif_spawn() to show guild emblems correctly)
  8944. if(sd->status.guild_id)
  8945. guild_send_memberinfoshort(sd,1);
  8946. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8947. if(battle_config.pc_invincible_time > 0) {
  8948. if(mapdata_flag_gvg(mapdata))
  8949. pc_setinvincibletimer(sd,battle_config.pc_invincible_time<<1);
  8950. else
  8951. pc_setinvincibletimer(sd,battle_config.pc_invincible_time);
  8952. }
  8953. if( mapdata->users++ == 0 && battle_config.dynamic_mobs )
  8954. map_spawnmobs(sd->bl.m);
  8955. if( !pc_isinvisible(sd) ) { // increment the number of pvp players on the map
  8956. mapdata->users_pvp++;
  8957. }
  8958. sd->state.debug_remove_map = 0; // temporary state to track double remove_map's [FlavioJS]
  8959. // reset the callshop flag if the player changes map
  8960. sd->state.callshop = 0;
  8961. if(map_addblock(&sd->bl))
  8962. return;
  8963. clif_spawn(&sd->bl);
  8964. // Party
  8965. // (needs to go after clif_spawn() to show hp bars correctly)
  8966. if(sd->status.party_id) {
  8967. party_send_movemap(sd);
  8968. clif_party_hp( *sd ); // Show hp after displacement [LuzZza]
  8969. }
  8970. if( sd->bg_id ) clif_bg_hp(sd); // BattleGround System
  8971. if(!pc_isinvisible(sd) && mapdata->getMapFlag(MF_PVP)) {
  8972. if(!battle_config.pk_mode) { // remove pvp stuff for pk_mode [Valaris]
  8973. if (!mapdata->getMapFlag(MF_PVP_NOCALCRANK))
  8974. sd->pvp_timer = add_timer(gettick()+200, pc_calc_pvprank_timer, sd->bl.id, 0);
  8975. sd->pvp_rank = 0;
  8976. sd->pvp_lastusers = 0;
  8977. sd->pvp_point = 5;
  8978. sd->pvp_won = 0;
  8979. sd->pvp_lost = 0;
  8980. }
  8981. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8982. } else if(sd->duel_group) // set flag, if it's a duel [LuzZza]
  8983. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8984. else if (mapdata->getMapFlag(MF_GVG_DUNGEON))
  8985. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF); //TODO: Figure out the real packet to send here.
  8986. else if( mapdata_flag_gvg(mapdata) )
  8987. clif_map_property(&sd->bl, MAPPROPERTY_AGITZONE, SELF);
  8988. else
  8989. clif_map_property(&sd->bl, MAPPROPERTY_NOTHING, SELF);
  8990. // info about nearby objects
  8991. // must use foreachinarea (CIRCULAR_AREA interferes with foreachinrange)
  8992. 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);
  8993. // pet
  8994. if( sd->pd ) {
  8995. if( battle_config.pet_no_gvg && mapdata_flag_gvg(mapdata) ) { //Return the pet to egg. [Skotlex]
  8996. clif_displaymessage(sd->fd, msg_txt(sd,666));
  8997. pet_return_egg( sd, sd->pd );
  8998. } else {
  8999. if(map_addblock(&sd->pd->bl))
  9000. return;
  9001. clif_spawn(&sd->pd->bl);
  9002. clif_send_petdata(sd,sd->pd,0,0);
  9003. clif_send_petstatus(sd);
  9004. // skill_unit_move(&sd->pd->bl,gettick(),1);
  9005. }
  9006. }
  9007. //homunculus [blackhole89]
  9008. if( hom_is_active(sd->hd) ) {
  9009. if(map_addblock(&sd->hd->bl))
  9010. return;
  9011. clif_spawn(&sd->hd->bl);
  9012. clif_send_homdata( *sd->hd, SP_ACK );
  9013. // For some reason, official servers send the homunculus info twice, then update the HP/SP again.
  9014. clif_hominfo(sd, sd->hd, 1);
  9015. clif_hominfo(sd, sd->hd, 0);
  9016. clif_homunculus_updatestatus(*sd, SP_HP);
  9017. clif_homunculus_updatestatus(*sd, SP_SP);
  9018. clif_homskillinfoblock( *sd->hd );
  9019. status_calc_bl(&sd->hd->bl, { SCB_SPEED });
  9020. if( !(battle_config.hom_setting&HOMSET_NO_INSTANT_LAND_SKILL) )
  9021. skill_unit_move(&sd->hd->bl,gettick(),1); // apply land skills immediately
  9022. }
  9023. if( sd->md ) {
  9024. if(map_addblock(&sd->md->bl))
  9025. return;
  9026. clif_spawn(&sd->md->bl);
  9027. clif_mercenary_info(sd);
  9028. clif_mercenary_skillblock(sd);
  9029. status_calc_bl(&sd->md->bl, { SCB_SPEED }); // Mercenary mimic their master's speed on each map change
  9030. }
  9031. if( sd->ed ) {
  9032. if (map_addblock(&sd->ed->bl))
  9033. return;
  9034. clif_spawn(&sd->ed->bl);
  9035. clif_elemental_info(sd);
  9036. clif_elemental_updatestatus(*sd, SP_HP);
  9037. clif_hpmeter_single( *sd, sd->ed->bl.id, sd->ed->battle_status.hp, sd->ed->battle_status.max_hp );
  9038. clif_elemental_updatestatus(*sd, SP_SP);
  9039. status_calc_bl(&sd->ed->bl, { SCB_SPEED }); //Elemental mimic their master's speed on each map change
  9040. }
  9041. if(sd->state.connect_new) {
  9042. int lv;
  9043. guild_notice = true;
  9044. clif_skillinfoblock(sd);
  9045. clif_hotkeys_send(sd,0);
  9046. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9047. clif_hotkeys_send(sd,1);
  9048. #endif
  9049. clif_updatestatus(*sd,SP_BASEEXP);
  9050. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  9051. clif_updatestatus(*sd,SP_JOBEXP);
  9052. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  9053. clif_updatestatus(*sd,SP_SKILLPOINT);
  9054. clif_initialstatus( *sd );
  9055. if (sd->sc.option&OPTION_FALCON)
  9056. clif_status_load(&sd->bl, EFST_FALCON, 1);
  9057. else if (sd->sc.option&(OPTION_RIDING|OPTION_DRAGON))
  9058. clif_status_load(&sd->bl, EFST_RIDING, 1);
  9059. else if (sd->sc.option&OPTION_WUGRIDER)
  9060. clif_status_load(&sd->bl, EFST_WUGRIDER, 1);
  9061. else if (sd->sc.getSCE(SC_ALL_RIDING))
  9062. clif_status_load(&sd->bl, EFST_ALL_RIDING, 1);
  9063. if(sd->status.manner < 0)
  9064. sc_start(&sd->bl,&sd->bl,SC_NOCHAT,100,0,0);
  9065. //Auron reported that This skill only triggers when you logon on the map o.O [Skotlex]
  9066. if ((lv = pc_checkskill(sd,SG_KNOWLEDGE)) > 0) {
  9067. if(sd->bl.m == sd->feel_map[0].m
  9068. || sd->bl.m == sd->feel_map[1].m
  9069. || sd->bl.m == sd->feel_map[2].m)
  9070. sc_start(&sd->bl,&sd->bl, SC_KNOWLEDGE, 100, lv, skill_get_time(SG_KNOWLEDGE, lv));
  9071. }
  9072. if(sd->pd && sd->pd->pet.intimate > 900)
  9073. clif_pet_emotion(sd->pd,(sd->pd->pet.class_ - 100)*100 + 50 + pet_hungry_val(sd->pd));
  9074. if(hom_is_active(sd->hd))
  9075. hom_init_timers(sd->hd);
  9076. if (night_flag && mapdata->getMapFlag(MF_NIGHTENABLED)) {
  9077. sd->state.night = 1;
  9078. clif_status_load(&sd->bl, EFST_SKE, 1);
  9079. }
  9080. // Notify everyone that this char logged in [Skotlex].
  9081. map_foreachpc(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 1);
  9082. if (!sd->state.autotrade) { // Don't trigger NPC event or opening vending/buyingstore will be failed
  9083. npc_script_event( *sd, NPCE_LOGIN );
  9084. }
  9085. // Set facing direction before check below to update client
  9086. if (battle_config.spawn_direction)
  9087. unit_setdir(&sd->bl, sd->status.body_direction, false);
  9088. } else {
  9089. //For some reason the client "loses" these on warp/map-change.
  9090. clif_updatestatus(*sd,SP_STR);
  9091. clif_updatestatus(*sd,SP_AGI);
  9092. clif_updatestatus(*sd,SP_VIT);
  9093. clif_updatestatus(*sd,SP_INT);
  9094. clif_updatestatus(*sd,SP_DEX);
  9095. clif_updatestatus(*sd,SP_LUK);
  9096. #ifdef RENEWAL
  9097. clif_updatestatus(*sd,SP_POW);
  9098. clif_updatestatus(*sd,SP_STA);
  9099. clif_updatestatus(*sd,SP_WIS);
  9100. clif_updatestatus(*sd,SP_SPL);
  9101. clif_updatestatus(*sd,SP_CON);
  9102. clif_updatestatus(*sd,SP_CRT);
  9103. #endif
  9104. // abort currently running script
  9105. sd->state.using_fake_npc = 0;
  9106. sd->state.menu_or_input = 0;
  9107. sd->npc_menu = 0;
  9108. if(sd->npc_id)
  9109. npc_event_dequeue(sd);
  9110. }
  9111. if( sd->state.changemap ) {// restore information that gets lost on map-change
  9112. clif_partyinvitationstate( *sd );
  9113. #if PACKETVER >= 20070918
  9114. clif_equipcheckbox(sd);
  9115. #endif
  9116. clif_pet_autofeed_status(sd,false);
  9117. clif_configuration( sd, CONFIG_CALL, sd->status.disable_call );
  9118. #if PACKETVER >= 20170920
  9119. if( battle_config.homunculus_autofeed_always ){
  9120. // Always send ON or OFF
  9121. if( sd->hd && battle_config.feature_homunculus_autofeed ){
  9122. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, sd->hd->homunculus.autofeed );
  9123. }else{
  9124. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, false );
  9125. }
  9126. }else{
  9127. // Only send when enabled
  9128. if( sd->hd && battle_config.feature_homunculus_autofeed && sd->hd->homunculus.autofeed ){
  9129. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, true );
  9130. }
  9131. }
  9132. #endif
  9133. clif_reputation_list( *sd );
  9134. if (sd->guild && battle_config.guild_notice_changemap == 1){
  9135. // Displays after VIP
  9136. clif_guild_notice( *sd );
  9137. guild_notice = false; // Do not display it twice
  9138. }
  9139. if (battle_config.bg_flee_penalty != 100 || battle_config.gvg_flee_penalty != 100) {
  9140. struct map_data *pmap = map_getmapdata(sd->state.pmap);
  9141. if ((pmap != nullptr && (mapdata_flag_gvg(pmap) || pmap->getMapFlag(MF_BATTLEGROUND))) || (mapdata != nullptr && (mapdata_flag_gvg(mapdata) || mapdata->getMapFlag(MF_BATTLEGROUND))))
  9142. status_calc_bl(&sd->bl, { SCB_FLEE }); //Refresh flee penalty
  9143. }
  9144. if( night_flag && mapdata->getMapFlag(MF_NIGHTENABLED) )
  9145. { //Display night.
  9146. if( !sd->state.night )
  9147. {
  9148. sd->state.night = 1;
  9149. clif_status_load(&sd->bl, EFST_SKE, 1);
  9150. }
  9151. }
  9152. else if( sd->state.night )
  9153. { //Clear night display.
  9154. sd->state.night = 0;
  9155. clif_status_load(&sd->bl, EFST_SKE, 0);
  9156. }
  9157. if( mapdata->getMapFlag(MF_BATTLEGROUND) )
  9158. {
  9159. clif_map_type( *sd, MAPTYPE_BATTLEFIELD ); // Battleground Mode
  9160. if( map_getmapflag(sd->bl.m, MF_BATTLEGROUND) == 2 )
  9161. clif_bg_updatescore_single(sd);
  9162. }
  9163. if( mapdata->getMapFlag(MF_ALLOWKS) && !mapdata_flag_ks(mapdata) )
  9164. {
  9165. char output[128];
  9166. sprintf(output, "[ Kill Steal Protection Disable. KS is allowed in this map ]");
  9167. clif_broadcast(&sd->bl, output, strlen(output) + 1, BC_BLUE, SELF);
  9168. }
  9169. if( pc_has_permission(sd,PC_PERM_VIEW_HPMETER) ) {
  9170. mapdata->hpmeter_visible++;
  9171. sd->state.hpmeter_visible = 1;
  9172. }
  9173. status_change_clear_onChangeMap(&sd->bl, &sd->sc);
  9174. map_iwall_get(sd); // Updates Walls Info on this Map to Client
  9175. status_calc_pc(sd, sd->state.autotrade ? SCO_FIRST : SCO_NONE); // Some conditions are map-dependent so we must recalculate
  9176. #ifdef VIP_ENABLE
  9177. if (!sd->state.connect_new &&
  9178. !sd->vip.disableshowrate &&
  9179. sd->state.pmap != sd->bl.m &&
  9180. map_getmapflag(sd->state.pmap, MF_BEXP) != mapdata->getMapFlag(MF_BEXP)
  9181. )
  9182. {
  9183. clif_display_pinfo( *sd );
  9184. }
  9185. #endif
  9186. // Instances do not need their own channels
  9187. if( channel_config.map_tmpl.name[0] && (channel_config.map_tmpl.opt&CHAN_OPT_AUTOJOIN) && !mapdata->instance_id && !mapdata->getMapFlag(MF_NOMAPCHANNELAUTOJOIN) )
  9188. channel_mjoin(sd); //join new map
  9189. clif_pk_mode_message(sd);
  9190. }
  9191. if( sd->guild && ( battle_config.guild_notice_changemap == 2 || guild_notice ) ){
  9192. // Displays at end
  9193. clif_guild_notice( *sd );
  9194. }
  9195. mail_clear(sd);
  9196. /* Guild Aura Init */
  9197. if( sd->guild && sd->state.gmaster_flag ) {
  9198. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  9199. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  9200. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  9201. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  9202. }
  9203. if( sd->state.vending ) { /* show we have a vending */
  9204. clif_openvending( *sd );
  9205. clif_showvendingboard( *sd );
  9206. }
  9207. // Don't trigger NPC event or opening vending/buyingstore will be failed
  9208. if(!sd->state.autotrade && mapdata->getMapFlag(MF_LOADEVENT)) // Lance
  9209. npc_script_event( *sd, NPCE_LOADMAP );
  9210. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  9211. clif_status_load(&sd->bl, EFST_DEVIL1, 1); //blindness [Komurka]
  9212. if (sd->sc.opt2) //Client loses these on warp.
  9213. clif_changeoption(&sd->bl);
  9214. 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)) {
  9215. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM);
  9216. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM);
  9217. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  9218. }
  9219. clif_weather_check(sd);
  9220. // For automatic triggering of NPCs after map loading (so you don't need to walk 1 step first)
  9221. if (map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNPC))
  9222. npc_touch_area_allnpc(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  9223. else
  9224. sd->areanpc.clear();
  9225. /* it broke at some point (e.g. during a crash), so we make it visibly dead again. */
  9226. if( !sd->status.hp && !pc_isdead(sd) && status_isdead(sd->bl) )
  9227. pc_setdead(sd);
  9228. // If player is dead, and is spawned (such as @refresh) send death packet. [Valaris]
  9229. if(pc_isdead(sd))
  9230. clif_clearunit_area( sd->bl, CLR_DEAD );
  9231. else {
  9232. skill_usave_trigger(sd);
  9233. if (battle_config.spawn_direction)
  9234. clif_changed_dir(sd->bl, SELF);
  9235. }
  9236. // Trigger skill effects if you appear standing on them
  9237. if(!battle_config.pc_invincible_time)
  9238. skill_unit_move(&sd->bl,gettick(),1);
  9239. pc_show_questinfo_reinit(sd);
  9240. pc_show_questinfo(sd);
  9241. #if PACKETVER >= 20150513
  9242. if( sd->mail.inbox.unread ){
  9243. clif_Mail_new(sd, 0, nullptr, nullptr);
  9244. }
  9245. #endif
  9246. sd->state.connect_new = 0;
  9247. sd->state.changemap = false;
  9248. }
  9249. /// Server's tick (ZC_NOTIFY_TIME).
  9250. /// 007f <time>.L
  9251. void clif_notify_time(map_session_data* sd, t_tick time)
  9252. {
  9253. int fd = sd->fd;
  9254. WFIFOHEAD(fd,packet_len(0x7f));
  9255. WFIFOW(fd,0) = 0x7f;
  9256. WFIFOL(fd,2) = client_tick(time);
  9257. WFIFOSET(fd,packet_len(0x7f));
  9258. }
  9259. /// Request for server's tick.
  9260. /// 007e <client tick>.L (CZ_REQUEST_TIME)
  9261. /// 0360 <client tick>.L (CZ_REQUEST_TIME2)
  9262. /// There are various variants of this packet, some of them have padding between fields.
  9263. void clif_parse_TickSend(int fd, map_session_data *sd)
  9264. {
  9265. sd->client_tick = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9266. clif_notify_time(sd, gettick());
  9267. }
  9268. /// Sends hotkey bar.
  9269. /// 02b9 { <is skill>.B <id>.L <count>.W }*27 (ZC_SHORTCUT_KEY_LIST)
  9270. /// 07d9 { <is skill>.B <id>.L <count>.W }*36 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090603)
  9271. /// 07d9 { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090617)
  9272. /// 0a00 <rotate>.B { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V3, PACKETVER >= 20141022)
  9273. void clif_hotkeys_send( map_session_data *sd, int tab ){
  9274. #if defined(HOTKEY_SAVING) && ( PACKETVER_MAIN_NUM >= 20070711 || PACKETVER_RE_NUM >= 20080827 || PACKETVER_AD_NUM >= 20070711 || PACKETVER_SAK_NUM >= 20070628 )
  9275. nullpo_retv( sd );
  9276. PACKET_ZC_SHORTCUT_KEY_LIST p = {};
  9277. p.packetType = HEADER_ZC_SHORTCUT_KEY_LIST;
  9278. #if PACKETVER_MAIN_NUM >= 20141022 || PACKETVER_RE_NUM >= 20141015 || defined(PACKETVER_ZERO)
  9279. if( tab == 0 ){
  9280. p.rotate = sd->status.hotkey_rowshift;
  9281. }else{
  9282. p.rotate = sd->status.hotkey_rowshift2;
  9283. }
  9284. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9285. p.tab = tab;
  9286. #endif
  9287. #endif
  9288. for( int i = 0, offset = tab * MAX_HOTKEYS; i < MAX_HOTKEYS_PACKET; i++ ){
  9289. p.hotkey[i].isSkill = sd->status.hotkeys[i + offset].type;
  9290. p.hotkey[i].id = sd->status.hotkeys[i + offset].id;
  9291. p.hotkey[i].count = sd->status.hotkeys[i + offset].lv;
  9292. }
  9293. clif_send( &p, sizeof(PACKET_ZC_SHORTCUT_KEY_LIST), &sd->bl, SELF );
  9294. #endif
  9295. }
  9296. /// Request to update a position on the hotkey row bar
  9297. void clif_parse_HotkeyRowShift( int fd, map_session_data *sd ){
  9298. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9299. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE2* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE2*>( RFIFOP( fd, 0 ) );
  9300. if( p->tab == 0 ){
  9301. sd->status.hotkey_rowshift = p->rowshift;
  9302. }else{
  9303. sd->status.hotkey_rowshift2 = p->rowshift;
  9304. }
  9305. #elif PACKETVER_MAIN_NUM >= 20140129 || PACKETVER_RE_NUM >= 20140129 || defined(PACKETVER_ZERO)
  9306. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE1* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE1*>( RFIFOP( fd, 0 ) );
  9307. sd->status.hotkey_rowshift = p->rowshift;
  9308. #endif
  9309. }
  9310. /// Request to update a position on the hotkey bar.
  9311. /// 02ba <index>.W <is skill>.B <id>.L <count>.W (CZ_SHORTCUT_KEY_CHANGE)
  9312. void clif_parse_Hotkey(int fd, map_session_data *sd) {
  9313. #ifdef HOTKEY_SAVING
  9314. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9315. const PACKET_CZ_SHORTCUT_KEY_CHANGE2* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE2*>( RFIFOP( fd, 0 ) );
  9316. const unsigned short idx = p->index + p->tab * MAX_HOTKEYS;
  9317. if( idx >= MAX_HOTKEYS_DB ){
  9318. return;
  9319. }
  9320. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9321. sd->status.hotkeys[idx].id = p->hotkey.id;
  9322. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9323. #elif PACKETVER_MAIN_NUM >= 20070618 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_AD_NUM >= 20070618 || PACKETVER_SAK_NUM >= 20070618
  9324. const PACKET_CZ_SHORTCUT_KEY_CHANGE1* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE1*>( RFIFOP( fd, 0 ) );
  9325. const unsigned short idx = p->index;
  9326. if( idx >= MAX_HOTKEYS_DB ){
  9327. return;
  9328. }
  9329. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9330. sd->status.hotkeys[idx].id = p->hotkey.id;
  9331. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9332. #endif
  9333. #endif
  9334. }
  9335. /// Displays cast-like progress bar (ZC_PROGRESS).
  9336. /// 02f0 <color>.L <time>.L
  9337. void clif_progressbar(map_session_data * sd, unsigned long color, uint32 second)
  9338. {
  9339. int fd = sd->fd;
  9340. WFIFOHEAD(fd,packet_len(0x2f0));
  9341. WFIFOW(fd,0) = 0x2f0;
  9342. WFIFOL(fd,2) = color;
  9343. WFIFOL(fd,6) = second;
  9344. WFIFOSET(fd,packet_len(0x2f0));
  9345. }
  9346. /// Removes an ongoing progress bar (ZC_PROGRESS_CANCEL).
  9347. /// 02f2
  9348. void clif_progressbar_abort(map_session_data * sd)
  9349. {
  9350. int fd = sd->fd;
  9351. WFIFOHEAD(fd,packet_len(0x2f2));
  9352. WFIFOW(fd,0) = 0x2f2;
  9353. WFIFOSET(fd,packet_len(0x2f2));
  9354. }
  9355. /// Notification from the client, that the progress bar has reached 100%.
  9356. /// 02f1 (CZ_PROGRESS)
  9357. void clif_parse_progressbar(int fd, map_session_data * sd){
  9358. // No progressbar active, ignore it
  9359. if( !sd->progressbar.npc_id ){
  9360. return;
  9361. }
  9362. int npc_id = sd->progressbar.npc_id;
  9363. bool closing = false;
  9364. // Check if the progress was canceled
  9365. if( gettick() < sd->progressbar.timeout && sd->st ){
  9366. closing = true;
  9367. sd->st->state = CLOSE; // will result in END in npc_scriptcont
  9368. // If a message window was open, offer a close button to the user
  9369. if( sd->st->mes_active ){
  9370. clif_scriptclose( *sd, npc_id );
  9371. }
  9372. }
  9373. sd->progressbar.npc_id = 0;
  9374. sd->progressbar.timeout = 0;
  9375. sd->state.workinprogress = WIP_DISABLE_NONE;
  9376. if( battle_config.idletime_option&IDLE_NPC_PROGRESS ){
  9377. sd->idletime = last_tick;
  9378. }
  9379. npc_scriptcont(sd, npc_id, closing);
  9380. }
  9381. /// Displays cast-like progress bar on a NPC
  9382. /// 09d1 <id>.L <color>.L <time>.L (ZC_PROGRESS_ACTOR)
  9383. void clif_progressbar_npc( struct npc_data *nd, map_session_data* sd ){
  9384. #if PACKETVER >= 20130821
  9385. unsigned char buf[14];
  9386. if( nd->progressbar.timeout > 0 ){
  9387. WBUFW(buf,0) = 0x9d1;
  9388. WBUFL(buf,2) = nd->bl.id;
  9389. WBUFL(buf,6) = nd->progressbar.color;
  9390. WBUFL(buf,10) = client_tick( ( nd->progressbar.timeout - gettick() ) / 1000 );
  9391. if( sd ){
  9392. clif_send(buf, packet_len(0x9d1), &sd->bl, SELF);
  9393. }else{
  9394. clif_send(buf, packet_len(0x9d1), &nd->bl, AREA);
  9395. }
  9396. }
  9397. #endif
  9398. }
  9399. /// Request to walk to a certain position on the current map.
  9400. /// 0085 <dest>.3B (CZ_REQUEST_MOVE)
  9401. /// 035f <dest>.3B (CZ_REQUEST_MOVE2)
  9402. /// There are various variants of this packet, some of them have padding between fields.
  9403. void clif_parse_WalkToXY(int fd, map_session_data *sd)
  9404. {
  9405. short x, y;
  9406. if (pc_isdead(sd)) {
  9407. clif_clearunit_area( sd->bl, CLR_DEAD );
  9408. return;
  9409. }
  9410. if (sd->sc.opt1 && ( sd->sc.opt1 == OPT1_STONEWAIT || sd->sc.opt1 == OPT1_BURNING ))
  9411. ; //You CAN walk on this OPT1 value.
  9412. else if (sd->progressbar.npc_id) {
  9413. clif_progressbar_abort(sd);
  9414. return; // First walk attempt cancels the progress bar
  9415. } else if (pc_cant_act(sd))
  9416. return;
  9417. if(sd->sc.getSCE(SC_RUN) || sd->sc.getSCE(SC_WUGDASH))
  9418. return;
  9419. RFIFOPOS(fd, packet_db[RFIFOW(fd,0)].pos[0], &x, &y, nullptr);
  9420. //A move command one cell west is only valid if the target cell is free
  9421. if(battle_config.official_cell_stack_limit > 0
  9422. && sd->bl.x == x+1 && sd->bl.y == y && map_count_oncell(sd->bl.m, x, y, BL_CHAR|BL_NPC, 1))
  9423. return;
  9424. // Cloaking wall check is actually updated when you click to process next movement
  9425. // not when you move each cell. This is official behaviour.
  9426. if (sd->sc.getSCE(SC_CLOAKING))
  9427. skill_check_cloaking(&sd->bl, sd->sc.getSCE(SC_CLOAKING));
  9428. status_change_end(&sd->bl, SC_ROLLINGCUTTER); // If you move, you lose your counters. [malufett]
  9429. status_change_end(&sd->bl, SC_CRESCIVEBOLT);
  9430. pc_delinvincibletimer(sd);
  9431. //Set last idle time... [Skotlex]
  9432. if (battle_config.idletime_option&IDLE_WALK)
  9433. sd->idletime = last_tick;
  9434. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_WALK)
  9435. sd->idletime_hom = last_tick;
  9436. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_WALK)
  9437. sd->idletime_mer = last_tick;
  9438. unit_walktoxy(&sd->bl, x, y, 4);
  9439. }
  9440. /// Notification about the result of a disconnect request (ZC_ACK_REQ_DISCONNECT).
  9441. /// 018b <result>.W
  9442. /// result:
  9443. /// 0 = disconnect (quit)
  9444. /// 1 = cannot disconnect (wait 10 seconds)
  9445. /// ? = ignored
  9446. void clif_disconnect_ack(map_session_data* sd, short result)
  9447. {
  9448. int fd = sd->fd;
  9449. WFIFOHEAD(fd,packet_len(0x18b));
  9450. WFIFOW(fd,0) = 0x18b;
  9451. WFIFOW(fd,2) = result;
  9452. WFIFOSET(fd,packet_len(0x18b));
  9453. }
  9454. /// Request to disconnect from server (CZ_REQ_DISCONNECT).
  9455. /// 018a <type>.W
  9456. /// type:
  9457. /// 0 = quit
  9458. void clif_parse_QuitGame(int fd, map_session_data *sd)
  9459. {
  9460. /* Rovert's prevent logout option fixed [Valaris] */
  9461. 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) &&
  9462. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9463. {
  9464. pc_damage_log_clear(sd,0);
  9465. clif_disconnect_ack(sd, 0);
  9466. flush_fifo( fd );
  9467. if( battle_config.drop_connection_on_quit ){
  9468. set_eof( fd );
  9469. }
  9470. } else {
  9471. clif_disconnect_ack(sd, 1);
  9472. }
  9473. }
  9474. /// Requesting unit's name.
  9475. /// 0094 <id>.L (CZ_REQNAME)
  9476. /// 0368 <id>.L (CZ_REQNAME2)
  9477. /// There are various variants of this packet, some of them have padding between fields.
  9478. void clif_parse_GetCharNameRequest(int fd, map_session_data *sd)
  9479. {
  9480. int id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9481. struct block_list* bl;
  9482. //status_change *sc;
  9483. if( id < 0 && -id == sd->bl.id ) // for disguises [Valaris]
  9484. id = sd->bl.id;
  9485. bl = map_id2bl(id);
  9486. if( bl == nullptr )
  9487. return; // Lagged clients could request names of already gone mobs/players. [Skotlex]
  9488. if( sd->bl.m != bl->m || !check_distance_bl(&sd->bl, bl, AREA_SIZE) )
  9489. return; // Block namerequests past view range
  9490. // 'see people in GM hide' cheat detection
  9491. /* disabled due to false positives (network lag + request name of char that's about to hide = race condition)
  9492. sc = status_get_sc(bl);
  9493. if (sc && sc->option&OPTION_INVISIBLE && !disguised(bl) &&
  9494. bl->type != BL_NPC && //Skip hidden NPCs which can be seen using Maya Purple
  9495. pc_get_group_level(sd) < battle_config.hack_info_GM_level
  9496. ) {
  9497. char gm_msg[256];
  9498. 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);
  9499. ShowWarning(gm_msg);
  9500. // information is sent to all online GMs
  9501. intif_wis_message_to_gm(wisp_server_name, battle_config.hack_info_GM_level, gm_msg);
  9502. return;
  9503. }
  9504. */
  9505. clif_name(&sd->bl,bl,SELF);
  9506. }
  9507. /// Validates and processes global messages
  9508. /// 008c <packet len>.W <text>.?B (<name> : <message>) 00 (CZ_REQUEST_CHAT)
  9509. /// There are various variants of this packet.
  9510. void clif_parse_GlobalMessage(int fd, map_session_data* sd)
  9511. {
  9512. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9513. size_t length;
  9514. // validate packet and retrieve name and message
  9515. if( !clif_process_message(sd, false, name, message, output ) )
  9516. return;
  9517. if( sd->gcbind && ((sd->gcbind->opt&CHAN_OPT_CAN_CHAT) || pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN)) ) {
  9518. channel_send(sd->gcbind,sd,message);
  9519. return;
  9520. }
  9521. // send message to others (using the send buffer for temp. storage)
  9522. clif_GlobalMessage( sd->bl, output, sd->chatID ? CHAT_WOS : AREA_CHAT_WOC );
  9523. length = strlen(output) + 1;
  9524. // send back message to the speaker
  9525. WFIFOHEAD(fd,4+length);
  9526. WFIFOW(fd,0) = 0x8e;
  9527. WFIFOW(fd,2) = (uint16)(4+length);
  9528. safestrncpy(WFIFOCP(fd,4), output, length );
  9529. WFIFOSET(fd, WFIFOW(fd,2));
  9530. #ifdef PCRE_SUPPORT
  9531. // trigger listening npcs
  9532. map_foreachinallrange(npc_chat_sub, &sd->bl, AREA_SIZE, BL_NPC, output, strlen(output), &sd->bl);
  9533. #endif
  9534. // Chat logging type 'O' / Global Chat
  9535. 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);
  9536. //achievement_update_objective(sd, AG_CHAT, 1, sd->bl.m); //! TODO: What's the official use of this achievement type?
  9537. }
  9538. /// /mm /mapmove (as @rura GM command) (CZ_MOVETO_MAP).
  9539. /// Request to warp to a map on given coordinates.
  9540. /// 0140 <map name>.16B <x>.W <y>.W
  9541. void clif_parse_MapMove(int fd, map_session_data *sd)
  9542. {
  9543. char command[MAP_NAME_LENGTH_EXT+25];
  9544. char* map_name;
  9545. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9546. map_name = RFIFOCP(fd,info->pos[0]);
  9547. map_name[MAP_NAME_LENGTH_EXT-1]='\0';
  9548. safesnprintf(command,sizeof(command),"%cmapmove %s %d %d", atcommand_symbol, map_name,
  9549. RFIFOW(fd,info->pos[1]), //x
  9550. RFIFOW(fd,info->pos[2])); //y
  9551. is_atcommand(fd, sd, command, 1);
  9552. }
  9553. /// Updates body and head direction of an object (ZC_CHANGE_DIRECTION).
  9554. /// 009c <id>.L <head dir>.W <dir>.B
  9555. /// head dir:
  9556. /// 0 = straight
  9557. /// 1 = turned CW
  9558. /// 2 = turned CCW
  9559. /// dir:
  9560. /// 0 = north
  9561. /// 1 = northwest
  9562. /// 2 = west
  9563. /// 3 = southwest
  9564. /// 4 = south
  9565. /// 5 = southeast
  9566. /// 6 = east
  9567. /// 7 = northeast
  9568. void clif_changed_dir(block_list& bl, enum send_target target){
  9569. PACKET_ZC_CHANGE_DIRECTION p{};
  9570. p.packetType = HEADER_ZC_CHANGE_DIRECTION;
  9571. p.srcId = bl.id;
  9572. if( bl.type == BL_PC ){
  9573. p.headDir = reinterpret_cast<map_session_data*>(&bl)->head_dir;
  9574. }else{
  9575. p.headDir = 0;
  9576. }
  9577. p.dir = unit_getdir(&bl);
  9578. clif_send(&p, sizeof(p), &bl, target);
  9579. if (disguised(&bl)) {
  9580. p.srcId = disguised_bl_id(bl.id);
  9581. p.headDir = 0;
  9582. clif_send(&p, sizeof(p), &bl, SELF);
  9583. }
  9584. }
  9585. /// Request to change own body and head direction.
  9586. /// 009b <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION)
  9587. /// 0361 <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION2)
  9588. /// There are various variants of this packet, some of them have padding between fields.
  9589. void clif_parse_ChangeDir(int fd, map_session_data *sd)
  9590. {
  9591. unsigned char headdir, dir;
  9592. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9593. headdir = RFIFOB(fd,info->pos[0]);
  9594. dir = RFIFOB(fd,info->pos[1]);
  9595. pc_setdir(sd, dir, headdir);
  9596. clif_changed_dir(sd->bl, AREA_WOS);
  9597. }
  9598. /// Request to show an emotion (CZ_REQ_EMOTION).
  9599. /// 00bf <type>.B
  9600. /// type:
  9601. /// @see enum emotion_type
  9602. void clif_parse_Emotion(int fd, map_session_data *sd){
  9603. if( sd == nullptr ){
  9604. return;
  9605. }
  9606. int emoticon = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9607. if (battle_config.basic_skill_check == 0 || pc_checkskill(sd, NV_BASIC) >= 2 || pc_checkskill(sd, SU_BASIC_SKILL) >= 1) {
  9608. if (emoticon == ET_CHAT_PROHIBIT) {// prevent use of the mute emote [Valaris]
  9609. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9610. return;
  9611. }
  9612. // fix flood of emotion icon (ro-proxy): flood only the hacker player
  9613. if (sd->emotionlasttime + 1 >= time(nullptr)) { // not more than 1 per second
  9614. sd->emotionlasttime = time(nullptr);
  9615. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9616. return;
  9617. }
  9618. sd->emotionlasttime = time(nullptr);
  9619. if (battle_config.idletime_option&IDLE_EMOTION)
  9620. sd->idletime = last_tick;
  9621. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_EMOTION)
  9622. sd->idletime_hom = last_tick;
  9623. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_EMOTION)
  9624. sd->idletime_mer = last_tick;
  9625. if (sd->state.block_action & PCBLOCK_EMOTION) {
  9626. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9627. return;
  9628. }
  9629. if(battle_config.client_reshuffle_dice && emoticon>=ET_DICE1 && emoticon<=ET_DICE6) {// re-roll dice
  9630. emoticon = rnd()%6+ET_DICE1;
  9631. }
  9632. clif_emotion(&sd->bl, emoticon);
  9633. } else
  9634. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9635. }
  9636. /// Amount of currently online players, reply to /w /who
  9637. /// 00c2 <count>.L (ZC_USER_COUNT)
  9638. static void clif_user_count(map_session_data& sd)
  9639. {
  9640. PACKET_ZC_USER_COUNT p{};
  9641. p.packetType = HEADER_ZC_USER_COUNT;
  9642. p.playersCount = map_getusers();
  9643. clif_send(&p,sizeof(p),&sd.bl,SELF);
  9644. }
  9645. /// /w /who (CZ_REQ_USER_COUNT).
  9646. /// Request to display amount of currently connected players.
  9647. /// 00c1
  9648. void clif_parse_HowManyConnections(int fd, map_session_data *sd) {
  9649. if( sd == nullptr ){
  9650. return;
  9651. }
  9652. clif_user_count(*sd);
  9653. }
  9654. void clif_parse_ActionRequest_sub( map_session_data& sd, uint8 action_type, int target_id, t_tick tick ){
  9655. if (pc_isdead(&sd)) {
  9656. clif_clearunit_area(sd.bl, CLR_DEAD);
  9657. return;
  9658. }
  9659. // Statuses that don't let the player sit / stand / talk with NPCs (targeted)
  9660. if (action_type != DMG_NORMAL && action_type != DMG_REPEAT) {
  9661. if (sd.sc.cant.interact)
  9662. return;
  9663. pc_stop_walking(&sd, 1);
  9664. }
  9665. pc_stop_attack(&sd);
  9666. if(target_id<0 && -target_id == sd.bl.id) // for disguises [Valaris]
  9667. target_id = sd.bl.id;
  9668. switch(action_type)
  9669. {
  9670. case DMG_NORMAL: // once attack
  9671. case DMG_REPEAT: // continuous attack
  9672. if( pc_cant_act(&sd) )
  9673. return;
  9674. if (!battle_config.sdelay_attack_enable && pc_checkskill(&sd, SA_FREECAST) <= 0) {
  9675. if (DIFF_TICK(tick, sd.ud.canact_tick) < 0) {
  9676. clif_skill_fail( sd, 1, USESKILL_FAIL_SKILLINTERVAL );
  9677. return;
  9678. }
  9679. }
  9680. pc_delinvincibletimer(&sd);
  9681. if (battle_config.idletime_option&IDLE_ATTACK)
  9682. sd.idletime = last_tick;
  9683. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_ATTACK)
  9684. sd.idletime_hom = last_tick;
  9685. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_ATTACK)
  9686. sd.idletime_mer = last_tick;
  9687. unit_attack(&sd.bl, target_id, action_type != 0);
  9688. break;
  9689. case DMG_SIT_DOWN: // sitdown
  9690. if (battle_config.basic_skill_check && pc_checkskill(&sd, NV_BASIC) < 3 && pc_checkskill(&sd, SU_BASIC_SKILL) < 1) {
  9691. clif_skill_fail( sd, 1, USESKILL_FAIL_LEVEL, 2 );
  9692. break;
  9693. }
  9694. if(pc_issit(&sd)) {
  9695. //Bugged client? Just refresh them.
  9696. clif_sitting(sd.bl);
  9697. return;
  9698. }
  9699. if (sd.ud.skilltimer != INVALID_TIMER || (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING))
  9700. break;
  9701. if (sd.sc.count && (
  9702. sd.sc.getSCE(SC_DANCING) ||
  9703. (sd.sc.getSCE(SC_GRAVITATION) && sd.sc.getSCE(SC_GRAVITATION)->val3 == BCT_SELF)
  9704. )) //No sitting during these states either.
  9705. break;
  9706. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9707. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9708. break;
  9709. }
  9710. if (battle_config.idletime_option&IDLE_SIT)
  9711. sd.idletime = last_tick;
  9712. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9713. sd.idletime_hom = last_tick;
  9714. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9715. sd.idletime_mer = last_tick;
  9716. pc_setsit(&sd);
  9717. skill_sit(&sd, true);
  9718. clif_sitting(sd.bl);
  9719. break;
  9720. case DMG_STAND_UP: // standup
  9721. if (!pc_issit(&sd)) {
  9722. //Bugged client? Just refresh them.
  9723. clif_standing(sd.bl);
  9724. return;
  9725. }
  9726. if (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING)
  9727. break;
  9728. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9729. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9730. break;
  9731. }
  9732. if (pc_setstand(&sd, false)) {
  9733. if (battle_config.idletime_option&IDLE_SIT)
  9734. sd.idletime = last_tick;
  9735. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9736. sd.idletime_hom = last_tick;
  9737. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9738. sd.idletime_mer = last_tick;
  9739. skill_sit(&sd, false);
  9740. clif_standing(sd.bl);
  9741. }
  9742. break;
  9743. }
  9744. }
  9745. /// Request for an action.
  9746. /// 0089 <target id>.L <action>.B (CZ_REQUEST_ACT)
  9747. /// 0437 <target id>.L <action>.B (CZ_REQUEST_ACT2)
  9748. /// action:
  9749. /// 0 = attack
  9750. /// 1 = pick up item
  9751. /// 2 = sit down
  9752. /// 3 = stand up
  9753. /// 7 = continous attack
  9754. /// 12 = (touch skill?)
  9755. /// There are various variants of this packet, some of them have padding between fields.
  9756. void clif_parse_ActionRequest(int fd, map_session_data *sd)
  9757. {
  9758. if( sd == nullptr ){
  9759. return;
  9760. }
  9761. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9762. clif_parse_ActionRequest_sub( *sd,
  9763. RFIFOB(fd,info->pos[1]),
  9764. RFIFOL(fd,info->pos[0]),
  9765. gettick()
  9766. );
  9767. }
  9768. /// Response to the death/system menu (CZ_RESTART).
  9769. /// 00b2 <type>.B
  9770. /// type:
  9771. /// 0 = restart (respawn)
  9772. /// 1 = char-select (disconnect)
  9773. void clif_parse_Restart(int fd, map_session_data *sd)
  9774. {
  9775. switch(RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0])) {
  9776. case 0x00:
  9777. pc_respawn(sd,CLR_OUTSIGHT);
  9778. break;
  9779. case 0x01:
  9780. /* Rovert's Prevent logout option - Fixed [Valaris] */
  9781. 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) &&
  9782. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9783. { //Send to char-server for character selection.
  9784. pc_damage_log_clear(sd,0);
  9785. chrif_charselectreq(sd, session[fd]->client_addr);
  9786. } else {
  9787. clif_disconnect_ack(sd, 1);
  9788. }
  9789. break;
  9790. }
  9791. }
  9792. /// Validates and processes whispered messages (CZ_WHISPER).
  9793. /// 0096 <packet len>.W <nick>.24B <message>.?B
  9794. void clif_parse_WisMessage(int fd, map_session_data* sd)
  9795. {
  9796. map_session_data* dstsd;
  9797. int i;
  9798. char target[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9799. // validate packet and retrieve name and message
  9800. if( !clif_process_message( sd, true, target, message, output ) )
  9801. return;
  9802. // Chat logging type 'W' / Whisper
  9803. 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);
  9804. //-------------------------------------------------------//
  9805. // Lordalfa - Paperboy - To whisper NPC commands //
  9806. //-------------------------------------------------------//
  9807. if (target[0] && (strncasecmp(target,"NPC:",4) == 0) && (strlen(target) > 4))
  9808. {
  9809. char* str = target+4; //Skip the NPC: string part.
  9810. struct npc_data* npc;
  9811. if ((npc = npc_name2id(str))) {
  9812. char split_data[NUM_WHISPER_VAR][CHAT_SIZE_MAX];
  9813. char *split;
  9814. char event[EVENT_NAME_LENGTH];
  9815. str = message;
  9816. // skip codepage indicator, if detected
  9817. if( str[0] == '|' && strlen(str) >= 4 )
  9818. str += 3;
  9819. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {// Splits the message using '#' as separators
  9820. split = strchr(str,'#');
  9821. if( split == nullptr ) { // use the remaining string
  9822. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9823. for( ++i; i < NUM_WHISPER_VAR; ++i )
  9824. split_data[i][0] = '\0';
  9825. break;
  9826. }
  9827. *split = '\0';
  9828. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9829. str = split+1;
  9830. }
  9831. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {
  9832. char variablename[CHAT_SIZE_MAX];
  9833. safesnprintf(variablename,sizeof(variablename),"@whispervar%d$", i);
  9834. set_var_str( sd, variablename, split_data[i] );
  9835. }
  9836. safesnprintf(event,sizeof(event),"%s::%s", npc->exname,script_config.onwhisper_event_name);
  9837. npc_event(sd,event,0); // Calls the NPC label
  9838. return;
  9839. }
  9840. } else if( target[0] == '#' ) {
  9841. struct Channel *channel = nullptr;
  9842. char* chname = target;
  9843. channel = channel_name2channel(chname,sd,3);
  9844. if(channel && (pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) || ((channel->opt&CHAN_OPT_CAN_CHAT) && channel_pccheckgroup(channel,sd->group_id)))){
  9845. if(channel_pc_haschan(sd,channel)>=0){ //we are in the chan
  9846. channel_send(channel,sd,message);
  9847. }
  9848. else if( channel->pass[0] == '\0') { //no pass needed
  9849. if (channel_join(channel,sd)==0)
  9850. channel_send(channel,sd,message); //join success
  9851. }
  9852. else {
  9853. clif_displaymessage(fd, msg_txt(sd,1402)); //You're not in that channel, type '@join <#channel_name>'
  9854. }
  9855. return;
  9856. }
  9857. }
  9858. // searching destination character
  9859. dstsd = map_nick2sd(target,false);
  9860. if (dstsd == nullptr || strcmp(dstsd->status.name, target) != 0) {
  9861. // player is not on this map-server
  9862. // At this point, don't send wisp/page if it's not exactly the same name, because (example)
  9863. // if there are 'Test' player on an other map-server and 'test' player on this map-server,
  9864. // and if we ask for 'Test', we must not contact 'test' player
  9865. // so, we send information to inter-server, which is the only one which decide (and copy correct name).
  9866. intif_wis_message(sd, target, message, strlen(message) + 1);
  9867. return;
  9868. }
  9869. // if player ignores everyone
  9870. if (dstsd->state.ignoreAll && pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9871. if (pc_isinvisible(dstsd) && pc_get_group_level(sd) < pc_get_group_level(dstsd))
  9872. clif_wis_end( *sd, ACKWHISPER_TARGET_OFFLINE );
  9873. else
  9874. clif_wis_end( *sd, ACKWHISPER_ALL_IGNORED );
  9875. return;
  9876. }
  9877. // if player is autotrading
  9878. if (dstsd->state.autotrade == 1){
  9879. safesnprintf(output,sizeof(output),"%s is in autotrade mode and cannot receive whispered messages.", dstsd->status.name);
  9880. clif_wis_message(sd, wisp_server_name, output, strlen(output) + 1, 0);
  9881. return;
  9882. }
  9883. if (pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9884. // if player ignores the source character
  9885. ARR_FIND(0, MAX_IGNORE_LIST, i, dstsd->ignore[i].name[0] == '\0' || strcmp(dstsd->ignore[i].name, sd->status.name) == 0);
  9886. if(i < MAX_IGNORE_LIST && dstsd->ignore[i].name[0] != '\0') { // source char present in ignore list
  9887. clif_wis_end( *sd, ACKWHISPER_IGNORED );
  9888. return;
  9889. }
  9890. }
  9891. // notify sender of success
  9892. clif_wis_end( *sd, ACKWHISPER_SUCCESS );
  9893. // Normal message
  9894. clif_wis_message(dstsd, sd->status.name, message, strlen(message)+1, 0);
  9895. }
  9896. /// /b /nb (CZ_BROADCAST).
  9897. /// Request to broadcast a message on whole server.
  9898. /// 0099 <packet len>.W <text>.?B 00
  9899. void clif_parse_Broadcast(int fd, map_session_data* sd) {
  9900. char command[CHAT_SIZE_MAX+11];
  9901. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9902. uint32 len = RFIFOW(fd,info->pos[0])-4;
  9903. char* msg = RFIFOCP(fd,info->pos[1]);
  9904. // as the length varies depending on the command used, just block unreasonably long strings
  9905. mes_len_check(msg, len, CHAT_SIZE_MAX);
  9906. safesnprintf(command,sizeof(command),"%ckami %s", atcommand_symbol, msg);
  9907. is_atcommand(fd, sd, command, 1);
  9908. }
  9909. /// Request to pick up an item.
  9910. /// 009f <id>.L (CZ_ITEM_PICKUP)
  9911. /// 0362 <id>.L (CZ_ITEM_PICKUP2)
  9912. /// There are various variants of this packet, some of them have padding between fields.
  9913. void clif_parse_TakeItem(int fd, map_session_data *sd)
  9914. {
  9915. struct flooritem_data *fitem;
  9916. int map_object_id;
  9917. map_object_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9918. fitem = (struct flooritem_data*)map_id2bl(map_object_id);
  9919. do {
  9920. if (pc_isdead(sd)) {
  9921. clif_clearunit_area( sd->bl, CLR_DEAD );
  9922. break;
  9923. }
  9924. if (fitem == nullptr || fitem->bl.type != BL_ITEM || fitem->bl.m != sd->bl.m)
  9925. break;
  9926. if (pc_cant_act(sd))
  9927. break;
  9928. if (!pc_takeitem(sd, fitem))
  9929. break;
  9930. return;
  9931. } while (0);
  9932. // Client REQUIRES a fail packet or you can no longer pick items.
  9933. clif_additem(sd,0,0,6);
  9934. }
  9935. /// Request to drop an item.
  9936. /// 00a2 <index>.W <amount>.W (CZ_ITEM_THROW)
  9937. /// 0363 <index>.W <amount>.W (CZ_ITEM_THROW2)
  9938. /// There are various variants of this packet, some of them have padding between fields.
  9939. void clif_parse_DropItem(int fd, map_session_data *sd){
  9940. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9941. int item_index = RFIFOW(fd,info->pos[0]) -2;
  9942. int item_amount = RFIFOW(fd,info->pos[1]) ;
  9943. for(;;) {
  9944. if (pc_isdead(sd))
  9945. break;
  9946. if (pc_cant_act2(sd) || sd->npc_id)
  9947. break;
  9948. if (sd->sc.cant.drop)
  9949. break;
  9950. if (!pc_dropitem(sd, item_index, item_amount))
  9951. break;
  9952. if (battle_config.idletime_option&IDLE_DROPITEM)
  9953. sd->idletime = last_tick;
  9954. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_DROPITEM)
  9955. sd->idletime_hom = last_tick;
  9956. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_DROPITEM)
  9957. sd->idletime_mer = last_tick;
  9958. return;
  9959. }
  9960. //Because the client does not like being ignored.
  9961. clif_dropitem( *sd, item_index, 0 );
  9962. }
  9963. /// Request to use an item.
  9964. /// 00a7 <index>.W <account id>.L (CZ_USE_ITEM)
  9965. /// 0439 <index>.W <account id>.L (CZ_USE_ITEM2)
  9966. /// There are various variants of this packet, some of them have padding between fields.
  9967. void clif_parse_UseItem(int fd, map_session_data *sd)
  9968. {
  9969. int n;
  9970. if (pc_isdead(sd)) {
  9971. clif_clearunit_area( sd->bl, CLR_DEAD );
  9972. return;
  9973. }
  9974. if ( (!sd->npc_id && pc_istrading(sd)) || sd->chatID || (sd->state.block_action & PCBLOCK_USEITEM) ) {
  9975. clif_msg(sd, MSI_BUSY);
  9976. return;
  9977. }
  9978. //Whether the item is used or not is irrelevant, the char ain't idle. [Skotlex]
  9979. if (battle_config.idletime_option&IDLE_USEITEM)
  9980. sd->idletime = last_tick;
  9981. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9982. sd->idletime_hom = last_tick;
  9983. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  9984. sd->idletime_mer = last_tick;
  9985. n = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  9986. if(n <0 || n >= MAX_INVENTORY)
  9987. return;
  9988. if (!pc_useitem(sd,n))
  9989. clif_useitemack(sd,n,0,false); //Send an empty ack packet or the client gets stuck.
  9990. }
  9991. /// Request to equip an item
  9992. /// 00a9 <index>.W <position>.W (CZ_REQ_WEAR_EQUIP).
  9993. /// 0998 <index>.W <position>.L (CZ_REQ_WEAR_EQUIP_V5)
  9994. void clif_parse_EquipItem(int fd,map_session_data *sd)
  9995. {
  9996. int index;
  9997. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9998. if(pc_isdead(sd)) {
  9999. clif_clearunit_area( sd->bl, CLR_DEAD );
  10000. return;
  10001. }
  10002. index = RFIFOW(fd,info->pos[0])-2;
  10003. if (index < 0 || index >= MAX_INVENTORY)
  10004. return; //Out of bounds check.
  10005. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  10006. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  10007. return;
  10008. } else if (sd->state.storage_flag || sd->sc.opt1)
  10009. ; //You can equip/unequip stuff while storage is open/under status changes
  10010. else if (pc_cant_act2(sd))
  10011. return;
  10012. if(!sd->inventory.u.items_inventory[index].identify) {
  10013. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, index ); // fail
  10014. return;
  10015. }
  10016. if(!sd->inventory_data[index])
  10017. return;
  10018. if(sd->inventory_data[index]->type == IT_PETARMOR){
  10019. pet_equipitem(sd,index);
  10020. return;
  10021. }
  10022. if (battle_config.idletime_option&IDLE_USEITEM)
  10023. sd->idletime = last_tick;
  10024. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  10025. sd->idletime_hom = last_tick;
  10026. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  10027. sd->idletime_mer = last_tick;
  10028. //Client doesn't send the position for ammo.
  10029. if(sd->inventory_data[index]->type == IT_AMMO)
  10030. pc_equipitem(sd,index,EQP_AMMO);
  10031. else {
  10032. int req_pos;
  10033. #if PACKETVER >= 20120925
  10034. req_pos = RFIFOL(fd,info->pos[1]);
  10035. #else
  10036. req_pos = (int)RFIFOW(fd,info->pos[1]);
  10037. #endif
  10038. pc_equipitem(sd,index,req_pos);
  10039. }
  10040. }
  10041. /// Request to take off an equip (CZ_REQ_TAKEOFF_EQUIP).
  10042. /// 00ab <index>.W
  10043. void clif_parse_UnequipItem(int fd,map_session_data *sd)
  10044. {
  10045. int index;
  10046. if(pc_isdead(sd)) {
  10047. clif_clearunit_area( sd->bl, CLR_DEAD );
  10048. return;
  10049. }
  10050. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  10051. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  10052. return;
  10053. } else if (sd->state.storage_flag || sd->sc.opt1)
  10054. ; //You can equip/unequip stuff while storage is open/under status changes
  10055. else if (pc_cant_act2(sd))
  10056. return;
  10057. index = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  10058. if (battle_config.idletime_option&IDLE_USEITEM)
  10059. sd->idletime = last_tick;
  10060. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  10061. sd->idletime_hom = last_tick;
  10062. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  10063. sd->idletime_mer = last_tick;
  10064. pc_unequipitem(sd,index,1);
  10065. }
  10066. /// Request to start a conversation with an NPC (CZ_CONTACTNPC).
  10067. /// 0090 <id>.L <type>.B
  10068. /// type:
  10069. /// 1 = click
  10070. void clif_parse_NpcClicked(int fd,map_session_data *sd)
  10071. {
  10072. if( sd == nullptr ){
  10073. return;
  10074. }
  10075. struct block_list *bl;
  10076. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10077. if(pc_isdead(sd)) {
  10078. clif_clearunit_area( sd->bl, CLR_DEAD );
  10079. return;
  10080. }
  10081. if( pc_cant_act2(sd) || sd->npc_id || pc_hasprogress( sd, WIP_DISABLE_NPC ) ){
  10082. #ifdef RENEWAL
  10083. clif_msg( sd, MSI_BUSY);
  10084. #endif
  10085. return;
  10086. }
  10087. if( sd->state.mail_writing )
  10088. return;
  10089. bl = map_id2bl(RFIFOL(fd,info->pos[0]));
  10090. //type = RFIFOB(fd,info->pos[1]);
  10091. if (!bl) return;
  10092. switch (bl->type) {
  10093. case BL_MOB:
  10094. case BL_PC:
  10095. clif_parse_ActionRequest_sub( *sd, DMG_REPEAT, bl->id, gettick() );
  10096. break;
  10097. case BL_NPC:
  10098. #ifdef RENEWAL
  10099. 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
  10100. clif_msg(sd, MSI_BUSY);
  10101. break;
  10102. }
  10103. #endif
  10104. if( bl->m != -1 ){ // the user can't click floating npcs directly (hack attempt)
  10105. struct npc_data* nd = (struct npc_data*)bl;
  10106. // Progressbar is running
  10107. if( nd->progressbar.timeout > 0 ){
  10108. return;
  10109. }
  10110. npc_click(sd,nd);
  10111. }
  10112. break;
  10113. }
  10114. }
  10115. /// Selection between buy/sell was made (CZ_ACK_SELECT_DEALTYPE).
  10116. /// 00c5 <id>.L <type>.B
  10117. /// type:
  10118. /// 0 = buy
  10119. /// 1 = sell
  10120. void clif_parse_NpcBuySellSelected(int fd,map_session_data *sd)
  10121. {
  10122. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10123. if (sd->state.trading)
  10124. return;
  10125. npc_buysellsel(sd,RFIFOL(fd,info->pos[0]),RFIFOB(fd,info->pos[1]));
  10126. }
  10127. /// Notification about the result of a purchase attempt from an NPC shop (ZC_PC_PURCHASE_RESULT).
  10128. /// 00ca <result>.B
  10129. /// result:
  10130. /// 0 = "The deal has successfully completed."
  10131. /// 1 = "You do not have enough zeny."
  10132. /// 2 = "You are over your Weight Limit."
  10133. /// 3 = "Out of the maximum capacity, you have too many items."
  10134. /// 9 = "Amounts are exceeded the possession of the item is not available for purchase."
  10135. /// 10 = "Props open-air store sales will be traded in RODEX"
  10136. /// 11 = "The exchange failed."
  10137. /// 12 = "The exchange was well done."
  10138. /// 13 = "The item is already sold and out of stock."
  10139. /// 14 = "There is not enough goods to exchange."
  10140. void clif_npc_buy_result( map_session_data* sd, e_purchase_result result ){
  10141. PACKET_ZC_PC_PURCHASE_RESULT p = {};
  10142. p.packetType = HEADER_ZC_PC_PURCHASE_RESULT;
  10143. p.result = (uint8)result;
  10144. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  10145. }
  10146. /// Request to buy chosen items from npc shop.
  10147. /// 00c8 <packet len>.W { <amount>.W <name id>.W }* (CZ_PC_PURCHASE_ITEMLIST)
  10148. void clif_parse_NpcBuyListSend( int fd, map_session_data* sd ){
  10149. const PACKET_CZ_PC_PURCHASE_ITEMLIST *p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10150. uint16 n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->items[0] );
  10151. e_purchase_result result;
  10152. if( sd->state.trading || !sd->npc_shopid )
  10153. result = e_purchase_result::PURCHASE_FAIL_MONEY;
  10154. else{
  10155. std::vector<s_npc_buy_list> items = {};
  10156. items.reserve( n );
  10157. for( uint16 i = 0; i < n; i++ ){
  10158. s_npc_buy_list item = {};
  10159. item.nameid = p->items[i].itemId;
  10160. item.qty = p->items[i].amount;
  10161. items.push_back( item );
  10162. }
  10163. result = npc_buylist( sd, items );
  10164. }
  10165. sd->npc_shopid = 0; //Clear shop data.
  10166. clif_npc_buy_result(sd, result);
  10167. }
  10168. /// Notification about the result of a sell attempt to an NPC shop (ZC_PC_SELL_RESULT).
  10169. /// 00cb <result>.B
  10170. /// result:
  10171. /// 0 = "The deal has successfully completed."
  10172. /// 1 = "The deal has failed."
  10173. void clif_npc_sell_result(map_session_data* sd, unsigned char result)
  10174. {
  10175. int fd = sd->fd;
  10176. WFIFOHEAD(fd,packet_len(0xcb));
  10177. WFIFOW(fd,0) = 0xcb;
  10178. WFIFOB(fd,2) = result;
  10179. WFIFOSET(fd,packet_len(0xcb));
  10180. }
  10181. /// Request to sell chosen items to npc shop (CZ_PC_SELL_ITEMLIST).
  10182. /// 00c9 <packet len>.W { <index>.W <amount>.W }*
  10183. void clif_parse_NpcSellListSend(int fd,map_session_data *sd)
  10184. {
  10185. int fail=0;
  10186. const PACKET_CZ_PC_SELL_ITEMLIST* p = reinterpret_cast<PACKET_CZ_PC_SELL_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10187. int n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->sellList[0] );
  10188. if (sd->state.trading || !sd->npc_shopid)
  10189. fail = 1;
  10190. else
  10191. fail = npc_selllist(sd, n, p->sellList);
  10192. sd->npc_shopid = 0; //Clear shop data.
  10193. clif_npc_sell_result(sd, fail);
  10194. }
  10195. /// Chatroom creation request (CZ_CREATE_CHATROOM).
  10196. /// 00d5 <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B
  10197. /// type:
  10198. /// 0 = private
  10199. /// 1 = public
  10200. void clif_parse_CreateChatRoom(int fd, map_session_data* sd)
  10201. {
  10202. if( sd == nullptr ){
  10203. return;
  10204. }
  10205. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10206. int len = RFIFOW(fd,info->pos[0])-15;
  10207. int limit = RFIFOW(fd,info->pos[1]);
  10208. bool pub = (RFIFOB(fd,info->pos[2]) != 0);
  10209. const char* password = RFIFOCP(fd,info->pos[3]); //not zero-terminated
  10210. const char* title = RFIFOCP(fd,info->pos[4]); // not zero-terminated
  10211. char s_password[CHATROOM_PASS_SIZE];
  10212. char s_title[CHATROOM_TITLE_SIZE];
  10213. if (sd->sc.getSCE(SC_NOCHAT) && sd->sc.getSCE(SC_NOCHAT)->val1&MANNER_NOROOM)
  10214. return;
  10215. if(battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 4 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10216. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 3 );
  10217. return;
  10218. }
  10219. if( npc_isnear(&sd->bl) ) {
  10220. // uncomment to send msg_txt.
  10221. //char output[150];
  10222. //sprintf(output, msg_txt(662), battle_config.min_npc_vendchat_distance);
  10223. //clif_displaymessage(sd->fd, output);
  10224. clif_skill_fail( *sd, 1, USESKILL_FAIL_THERE_ARE_NPC_AROUND );
  10225. return;
  10226. }
  10227. if( len <= 0 )
  10228. return; // invalid input
  10229. safestrncpy(s_password, password, CHATROOM_PASS_SIZE);
  10230. safestrncpy(s_title, title, min(len+1,CHATROOM_TITLE_SIZE)); //NOTE: assumes that safestrncpy will not access the len+1'th byte
  10231. chat_createpcchat(sd, s_title, s_password, limit, pub);
  10232. }
  10233. /// Chatroom join request (CZ_REQ_ENTER_ROOM).
  10234. /// 00d9 <chat ID>.L <passwd>.8B
  10235. void clif_parse_ChatAddMember(int fd, map_session_data* sd){
  10236. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10237. int chatid = RFIFOL(fd,info->pos[0]);
  10238. const char* password = RFIFOCP(fd,info->pos[1]); // not zero-terminated
  10239. chat_joinchat(sd,chatid,password);
  10240. }
  10241. /// Chatroom properties adjustment request (CZ_CHANGE_CHATROOM).
  10242. /// 00de <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B
  10243. /// type:
  10244. /// 0 = private
  10245. /// 1 = public
  10246. void clif_parse_ChatRoomStatusChange(int fd, map_session_data* sd){
  10247. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10248. int len = RFIFOW(fd,info->pos[0])-15;
  10249. int limit = RFIFOW(fd,info->pos[1]);
  10250. bool pub = (RFIFOB(fd,info->pos[2]) != 0);
  10251. const char* password = RFIFOCP(fd,info->pos[3]); // not zero-terminated
  10252. const char* title = RFIFOCP(fd,info->pos[4]); // not zero-terminated
  10253. char s_password[CHATROOM_PASS_SIZE];
  10254. char s_title[CHATROOM_TITLE_SIZE];
  10255. if( len <= 0 )
  10256. return; // invalid input
  10257. safestrncpy(s_password, password, CHATROOM_PASS_SIZE);
  10258. safestrncpy(s_title, title, min(len+1,CHATROOM_TITLE_SIZE)); //NOTE: assumes that safestrncpy will not access the len+1'th byte
  10259. chat_changechatstatus(sd, s_title, s_password, limit, pub);
  10260. }
  10261. /// Request to change the chat room ownership (CZ_REQ_ROLE_CHANGE).
  10262. /// 00e0 <role>.L <nick>.24B
  10263. /// role:
  10264. /// 0 = owner
  10265. /// 1 = normal
  10266. void clif_parse_ChangeChatOwner(int fd, map_session_data* sd)
  10267. {
  10268. //int role = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10269. chat_changechatowner(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]));
  10270. }
  10271. /// Request to expel a player from chat room (CZ_REQ_EXPEL_MEMBER).
  10272. /// 00e2 <name>.24B
  10273. void clif_parse_KickFromChat(int fd,map_session_data *sd)
  10274. {
  10275. chat_kickchat(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10276. }
  10277. /// Request to leave the current chatroom (CZ_EXIT_ROOM).
  10278. /// 00e3
  10279. void clif_parse_ChatLeave(int fd, map_session_data* sd)
  10280. {
  10281. chat_leavechat(sd,0);
  10282. }
  10283. // Handles notifying asker and rejecter of what has just ocurred.
  10284. // Type is used to determine the correct msg_txt to use
  10285. void clif_noask_sub( map_session_data& sd, map_session_data& tsd, int type ){
  10286. char output[CHAT_SIZE_MAX];
  10287. // Your request has been rejected by autoreject option.
  10288. clif_messagecolor( &sd.bl, color_table[COLOR_LIGHT_GREEN], msg_txt( &sd, 392 ), false, SELF );
  10289. // Notice that a request was rejected.
  10290. safesnprintf( output, CHAT_SIZE_MAX, msg_txt( &tsd, type ), sd.status.name );
  10291. clif_messagecolor( &tsd.bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  10292. }
  10293. /// Request to begin a trade (CZ_REQ_EXCHANGE_ITEM).
  10294. /// 00e4 <account id>.L
  10295. void clif_parse_TradeRequest(int fd,map_session_data *sd)
  10296. {
  10297. map_session_data *t_sd;
  10298. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10299. if(!sd->chatID && pc_cant_act(sd))
  10300. return; //You can trade while in a chatroom.
  10301. if(t_sd){
  10302. // @noask [LuzZza]
  10303. if(t_sd->state.noask) {
  10304. clif_noask_sub( *sd, *t_sd, 393 ); // Autorejected trade request from %s.
  10305. return;
  10306. }
  10307. if (t_sd->state.mail_writing) {
  10308. // Fake trading
  10309. clif_traderesponse(*sd,TRADE_ACK_BUSY);
  10310. return;
  10311. }
  10312. }
  10313. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 1 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10314. clif_skill_fail( *sd, 1 );
  10315. return;
  10316. }
  10317. trade_traderequest(sd,t_sd);
  10318. }
  10319. /// Answer to a trade request (CZ_ACK_EXCHANGE_ITEM).
  10320. /// 00e6 <result>.B
  10321. /// result:
  10322. /// 3 = accepted
  10323. /// 4 = rejected
  10324. void clif_parse_TradeAck(int fd,map_session_data *sd)
  10325. {
  10326. trade_tradeack(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10327. }
  10328. /// Request to add an item to current trade (CZ_ADD_EXCHANGE_ITEM).
  10329. /// 00e8 <index>.W <amount>.L
  10330. void clif_parse_TradeAddItem(int fd,map_session_data *sd)
  10331. {
  10332. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10333. short index = RFIFOW(fd,info->pos[0]);
  10334. int amount = RFIFOL(fd,info->pos[1]);
  10335. if( index == 0 )
  10336. trade_tradeaddzeny(sd, amount);
  10337. else
  10338. trade_tradeadditem(sd, server_index(index), (short)amount);
  10339. }
  10340. /// Request to lock items in current trade (CZ_CONCLUDE_EXCHANGE_ITEM).
  10341. /// 00eb
  10342. void clif_parse_TradeOk(int fd,map_session_data *sd)
  10343. {
  10344. trade_tradeok(sd);
  10345. }
  10346. /// Request to cancel current trade (CZ_CANCEL_EXCHANGE_ITEM).
  10347. /// 00ed
  10348. void clif_parse_TradeCancel(int fd,map_session_data *sd)
  10349. {
  10350. trade_tradecancel(sd);
  10351. }
  10352. /// Request to commit current trade (CZ_EXEC_EXCHANGE_ITEM).
  10353. /// 00ef
  10354. void clif_parse_TradeCommit(int fd,map_session_data *sd)
  10355. {
  10356. trade_tradecommit(sd);
  10357. }
  10358. /// Request to stop chasing/attacking an unit (CZ_CANCEL_LOCKON).
  10359. /// 0118
  10360. void clif_parse_StopAttack(int fd,map_session_data *sd)
  10361. {
  10362. pc_stop_attack(sd);
  10363. if (sd) sd->ud.state.attack_continue = 0;
  10364. }
  10365. /// Request to move an item from inventory to cart.
  10366. /// 0126 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_CART)
  10367. void clif_parse_PutItemToCart( int fd, map_session_data *sd ){
  10368. if (pc_istrading(sd) || !pc_iscarton(sd) || pc_cant_act2(sd))
  10369. return;
  10370. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10371. return;
  10372. const PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART* p = reinterpret_cast<PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART*>( RFIFOP( fd, 0 ) );
  10373. pc_putitemtocart( sd, server_index( p->index ), p->count);
  10374. }
  10375. /// Request to move an item from cart to inventory (CZ_MOVE_ITEM_FROM_CART_TO_BODY).
  10376. /// 0127 <index>.W <amount>.L
  10377. void clif_parse_GetItemFromCart(int fd,map_session_data *sd)
  10378. {
  10379. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10380. if (!pc_iscarton(sd) || pc_cant_act2(sd))
  10381. return;
  10382. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10383. return;
  10384. pc_getitemfromcart(sd,RFIFOW(fd,info->pos[0])-2,RFIFOL(fd,info->pos[1]));
  10385. }
  10386. /// Request to remove cart/falcon/peco/dragon/mado (CZ_REQ_CARTOFF).
  10387. /// 012a
  10388. void clif_parse_RemoveOption(int fd,map_session_data *sd)
  10389. {
  10390. if( !(sd->sc.option&(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR))
  10391. #ifdef NEW_CARTS
  10392. && sd->sc.getSCE(SC_PUSH_CART) )
  10393. pc_setcart(sd,0);
  10394. #else
  10395. )
  10396. pc_setoption(sd,sd->sc.option&~OPTION_CART);
  10397. #endif
  10398. else // priority to remove this option before we can clear cart
  10399. pc_setoption(sd,sd->sc.option&~(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR));
  10400. }
  10401. /// Request to select cart's visual look for new cart design (ZC_SELECTCART).
  10402. /// 097f <Length>.W <identity>.L <type>.B
  10403. void clif_SelectCart(map_session_data *sd) {
  10404. #if PACKETVER >= 20150826
  10405. int i = 0, carts = 3;
  10406. int fd = sd->fd;
  10407. WFIFOHEAD(fd,8 + carts);
  10408. WFIFOW(fd,0) = 0x97f;
  10409. WFIFOW(fd,2) = 8 + carts;
  10410. WFIFOL(fd,4) = sd->status.account_id;
  10411. // Right now we have 10-12, tested it you can also enable selection for all cart styles here(1-12)
  10412. for( i = 0; i < carts; i++ ) {
  10413. WFIFOB(fd,8 + i) = 10 + i;
  10414. }
  10415. WFIFOSET(fd,8 + carts);
  10416. #endif
  10417. }
  10418. /// Request to select cart's visual look for new cart design (CZ_SELECTCART).
  10419. /// 0980 <identity>.L <type>.B
  10420. void clif_parse_SelectCart(int fd,map_session_data *sd) {
  10421. #if PACKETVER >= 20150826
  10422. int type;
  10423. // Check identity
  10424. if( !sd || pc_checkskill(sd,MC_CARTDECORATE) < 1 || RFIFOL(fd,2) != sd->status.account_id )
  10425. return;
  10426. type = (int)RFIFOB(fd,6);
  10427. // Check type
  10428. if( type < 10 || type > 12 )
  10429. return;
  10430. pc_setcart(sd, type);
  10431. #endif
  10432. }
  10433. /// Request to change cart's visual look (CZ_REQ_CHANGECART).
  10434. /// 01af <num>.W
  10435. void clif_parse_ChangeCart(int fd,map_session_data *sd)
  10436. {// TODO: State tracking?
  10437. int type;
  10438. if( !sd || pc_checkskill(sd, MC_CHANGECART) < 1 )
  10439. return;
  10440. #ifdef RENEWAL
  10441. if (sd->npc_id || pc_hasprogress(sd, WIP_DISABLE_SKILLITEM)) {
  10442. clif_msg(sd, MSI_BUSY);
  10443. return;
  10444. }
  10445. #endif
  10446. type = (int)RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10447. if(
  10448. #ifdef NEW_CARTS
  10449. #if PACKETVER >= 20191106
  10450. (type == 13 && sd->status.base_level > 100) ||
  10451. #endif
  10452. (type == 9 && sd->status.base_level > 130) ||
  10453. (type == 8 && sd->status.base_level > 120) ||
  10454. (type == 7 && sd->status.base_level > 110) ||
  10455. (type == 6 && sd->status.base_level > 100) ||
  10456. #endif
  10457. (type == 5 && sd->status.base_level > 90) ||
  10458. (type == 4 && sd->status.base_level > 80) ||
  10459. (type == 3 && sd->status.base_level > 65) ||
  10460. (type == 2 && sd->status.base_level > 40) ||
  10461. (type == 1))
  10462. pc_setcart(sd, type);
  10463. }
  10464. /// Request to increase status (CZ_STATUS_CHANGE).
  10465. /// 00bb <status id>.W <amount>.B
  10466. /// status id:
  10467. /// SP_STR ~ SP_LUK
  10468. /// amount:
  10469. /// Old clients always send 1 for this, even when using /str+ and the like.
  10470. /// Newer clients (2013-12-23 and newer) send the correct amount.
  10471. void clif_parse_StatusUp(int fd,map_session_data *sd)
  10472. {
  10473. int increase_amount = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  10474. if( increase_amount < 0 ) {
  10475. ShowDebug("clif_parse_StatusUp: Negative 'increase' value sent by client! (fd: %d, value: %d)\n",
  10476. fd, increase_amount);
  10477. }
  10478. pc_statusup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]),increase_amount);
  10479. }
  10480. /// Request to increase trait status.
  10481. /// 0b24 <status id>.W <amount>.W (CZ_ADVANCED_STATUS_CHANGE)
  10482. /// status id:
  10483. /// SP_POW ~ SP_CON
  10484. /// amount:
  10485. /// The amount to increase the trait status
  10486. void clif_parse_traitstatus_up( int fd, map_session_data *sd ){
  10487. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10488. const PACKET_CZ_ADVANCED_STATUS_CHANGE* p = reinterpret_cast<PACKET_CZ_ADVANCED_STATUS_CHANGE*>( RFIFOP( fd, 0 ) );
  10489. if( p->amount < 0 ){
  10490. 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 );
  10491. return;
  10492. }
  10493. pc_traitstatusup( sd, p->type, p->amount );
  10494. #endif
  10495. }
  10496. /// Request to increase level of a skill (CZ_UPGRADE_SKILLLEVEL).
  10497. /// 0112 <skill id>.W
  10498. void clif_parse_SkillUp(int fd,map_session_data *sd)
  10499. {
  10500. pc_skillup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10501. }
  10502. 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)
  10503. {
  10504. int lv;
  10505. if( !hd )
  10506. return;
  10507. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10508. clif_emotion(&hd->bl, ET_THINK);
  10509. return;
  10510. }
  10511. if( hd->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10512. target_id = hd->bl.id;
  10513. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10514. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10515. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10516. clif_emotion(&hd->bl, ET_THINK);
  10517. if (hd->master)
  10518. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10519. return;
  10520. }
  10521. lv = hom_checkskill(hd, skill_id);
  10522. if( skill_lv > lv )
  10523. skill_lv = lv;
  10524. if( skill_lv )
  10525. unit_skilluse_id(&hd->bl, target_id, skill_id, skill_lv);
  10526. }
  10527. 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)
  10528. {
  10529. int lv;
  10530. if( !hd )
  10531. return;
  10532. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10533. clif_emotion(&hd->bl, ET_THINK);
  10534. return;
  10535. }
  10536. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10537. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10538. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10539. clif_emotion(&hd->bl, ET_THINK);
  10540. if (hd->master)
  10541. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10542. return;
  10543. }
  10544. #ifdef RENEWAL
  10545. if (hd->sc.getSCE(SC_BASILICA_CELL))
  10546. #else
  10547. if (hd->sc.getSCE(SC_BASILICA))
  10548. #endif
  10549. return;
  10550. lv = hom_checkskill(hd, skill_id);
  10551. if( skill_lv > lv )
  10552. skill_lv = lv;
  10553. if( skill_lv )
  10554. unit_skilluse_pos(&hd->bl, x, y, skill_id, skill_lv);
  10555. }
  10556. 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)
  10557. {
  10558. int lv;
  10559. if( !md )
  10560. return;
  10561. if( skill_isNotOk_mercenary(skill_id, *md) )
  10562. return;
  10563. if( md->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10564. target_id = md->bl.id;
  10565. if( md->ud.skilltimer != INVALID_TIMER )
  10566. {
  10567. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10568. }
  10569. else if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10570. return;
  10571. lv = mercenary_checkskill(md, skill_id);
  10572. if( skill_lv > lv )
  10573. skill_lv = lv;
  10574. if( skill_lv )
  10575. unit_skilluse_id(&md->bl, target_id, skill_id, skill_lv);
  10576. }
  10577. 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)
  10578. {
  10579. int lv;
  10580. if( !md )
  10581. return;
  10582. if( skill_isNotOk_mercenary(skill_id, *md) )
  10583. return;
  10584. if( md->ud.skilltimer != INVALID_TIMER )
  10585. return;
  10586. if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10587. {
  10588. if (md->master)
  10589. clif_skill_fail( *md->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10590. return;
  10591. }
  10592. #ifdef RENEWAL
  10593. if (md->sc.getSCE(SC_BASILICA_CELL))
  10594. #else
  10595. if (md->sc.getSCE(SC_BASILICA))
  10596. #endif
  10597. return;
  10598. lv = mercenary_checkskill(md, skill_id);
  10599. if( skill_lv > lv )
  10600. skill_lv = lv;
  10601. if( skill_lv )
  10602. unit_skilluse_pos(&md->bl, x, y, skill_id, skill_lv);
  10603. }
  10604. void clif_parse_skill_toid( map_session_data* sd, uint16 skill_id, uint16 skill_lv, int target_id ){
  10605. if( sd == nullptr ){
  10606. return;
  10607. }
  10608. t_tick tick = gettick();
  10609. if( skill_lv < 1 ) skill_lv = 1; //No clue, I have seen the client do this with guild skills :/ [Skotlex]
  10610. int inf = skill_get_inf(skill_id);
  10611. if (inf&INF_GROUND_SKILL || !inf)
  10612. return; //Using a ground/passive skill on a target? WRONG.
  10613. if (sd->state.block_action & PCBLOCK_SKILL) {
  10614. clif_msg(sd, MSI_BUSY);
  10615. return;
  10616. }
  10617. if( SKILL_CHK_HOMUN(skill_id) ) {
  10618. clif_parse_UseSkillToId_homun(sd->hd, sd, tick, skill_id, skill_lv, target_id);
  10619. return;
  10620. }
  10621. if( SKILL_CHK_MERC(skill_id) ) {
  10622. clif_parse_UseSkillToId_mercenary(sd->md, sd, tick, skill_id, skill_lv, target_id);
  10623. return;
  10624. }
  10625. // Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10626. // This is done here, because homunculi and mercenaries can be triggered by AI and not by the player itself
  10627. if (battle_config.idletime_option&IDLE_USESKILLTOID)
  10628. sd->idletime = last_tick;
  10629. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USESKILLTOID)
  10630. sd->idletime_hom = last_tick;
  10631. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USESKILLTOID)
  10632. sd->idletime_mer = last_tick;
  10633. if( sd->npc_id ){
  10634. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag || !( inf & INF_SELF_SKILL ) ){
  10635. #ifdef RENEWAL
  10636. clif_msg( sd, MSI_BUSY);
  10637. #endif
  10638. return;
  10639. }
  10640. }
  10641. if ((pc_cant_act2(sd) || sd->chatID) &&
  10642. skill_id != RK_REFRESH &&
  10643. !( ( skill_id == SR_GENTLETOUCH_CURE || skill_id == SU_GROOMING ) && (sd->sc.opt1 == OPT1_STONE || sd->sc.opt1 == OPT1_FREEZE || sd->sc.opt1 == OPT1_STUN)) &&
  10644. !(sd->state.storage_flag && (inf&INF_SELF_SKILL))) //SELF skills can be used with the storage open, issue: 8027
  10645. return;
  10646. if( pc_issit(sd) )
  10647. return;
  10648. if( skill_isNotOk(skill_id, *sd) )
  10649. return;
  10650. if( sd->bl.id != target_id && inf&INF_SELF_SKILL )
  10651. target_id = sd->bl.id; // never trust the client
  10652. if( target_id < 0 && -target_id == sd->bl.id ) // for disguises [Valaris]
  10653. target_id = sd->bl.id;
  10654. if( sd->ud.skilltimer != INVALID_TIMER ) {
  10655. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST )
  10656. return;
  10657. } else if( DIFF_TICK(tick, sd->ud.canact_tick) < 0 ) {
  10658. if( sd->skillitem != skill_id ) {
  10659. clif_skill_fail( *sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10660. return;
  10661. }
  10662. }
  10663. if( sd->sc.option&OPTION_COSTUME )
  10664. return;
  10665. #ifndef RENEWAL
  10666. if( sd->sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd->sc.getSCE(SC_BASILICA)->val4 != sd->bl.id) )
  10667. return; // On basilica only caster can use Basilica again to stop it.
  10668. #endif
  10669. if( sd->menuskill_id ) {
  10670. if( sd->menuskill_id == SA_TAMINGMONSTER ) {
  10671. clif_menuskill_clear(sd); //Cancel pet capture.
  10672. }else if( sd->menuskill_id == SG_FEEL ){
  10673. clif_menuskill_clear( sd ); // Cancel selection
  10674. }else if( sd->menuskill_id != SA_AUTOSPELL )
  10675. return; //Can't use skills while a menu is open.
  10676. }
  10677. if( sd->skillitem == skill_id ) {
  10678. if( skill_lv != sd->skillitemlv )
  10679. skill_lv = sd->skillitemlv;
  10680. if( !(inf&INF_SELF_SKILL) )
  10681. pc_delinvincibletimer(sd); // Target skills thru items cancel invincibility. [Inkfish]
  10682. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10683. return;
  10684. }
  10685. sd->skillitem = sd->skillitemlv = 0;
  10686. if( SKILL_CHK_GUILD(skill_id) ) {
  10687. if (sd->guild != nullptr && (sd->state.gmaster_flag || skill_id == GD_CHARGESHOUT_BEATING))
  10688. skill_lv = guild_checkskill(sd->guild->guild, skill_id);
  10689. else
  10690. skill_lv = 0;
  10691. } else {
  10692. if( skill_id != ALL_EQSWITCH ){
  10693. skill_lv = min(pc_checkskill(sd, skill_id),skill_lv); //never trust client
  10694. }
  10695. }
  10696. pc_delinvincibletimer(sd);
  10697. if( skill_lv )
  10698. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10699. }
  10700. /// Request to use a targeted skill.
  10701. /// 0113 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL)
  10702. /// 0438 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL2)
  10703. /// There are various variants of this packet, some of them have padding between fields.
  10704. void clif_parse_UseSkillToId( int fd, map_session_data *sd ){
  10705. struct s_packet_db* info = &packet_db[RFIFOW(fd, 0)];
  10706. clif_parse_skill_toid( sd, RFIFOW(fd, info->pos[1]), RFIFOW(fd, info->pos[0]), RFIFOL(fd, info->pos[2]) );
  10707. }
  10708. /*==========================================
  10709. * Client tells server he'd like to use AoE skill id 'skill_id' of level 'skill_lv' on 'x','y' location
  10710. *------------------------------------------*/
  10711. static void clif_parse_UseSkillToPosSub( int fd, map_session_data& sd, uint16 skill_lv, uint16 skill_id, short x, short y, int skillmoreinfo ){
  10712. t_tick tick = gettick();
  10713. if( !(skill_get_inf(skill_id)&INF_GROUND_SKILL) )
  10714. return; //Using a target skill on the ground? WRONG.
  10715. if (sd.state.block_action & PCBLOCK_SKILL) {
  10716. clif_msg(&sd, MSI_BUSY);
  10717. return;
  10718. }
  10719. if( SKILL_CHK_HOMUN(skill_id) ) {
  10720. clif_parse_UseSkillToPos_homun(sd.hd, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10721. return;
  10722. }
  10723. if( SKILL_CHK_MERC(skill_id) ) {
  10724. clif_parse_UseSkillToPos_mercenary(sd.md, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10725. return;
  10726. }
  10727. if( pc_hasprogress( &sd, WIP_DISABLE_SKILLITEM ) ){
  10728. #ifdef RENEWAL
  10729. clif_msg( &sd, MSI_BUSY);
  10730. #endif
  10731. return;
  10732. }
  10733. //Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10734. if (battle_config.idletime_option&IDLE_USESKILLTOPOS)
  10735. sd.idletime = last_tick;
  10736. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_USESKILLTOPOS)
  10737. sd.idletime_hom = last_tick;
  10738. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_USESKILLTOPOS)
  10739. sd.idletime_mer = last_tick;
  10740. if( skill_isNotOk(skill_id, sd) )
  10741. return;
  10742. if( skillmoreinfo != -1 ) {
  10743. if( pc_issit(&sd) ) {
  10744. clif_skill_fail( sd, skill_id );
  10745. return;
  10746. }
  10747. //You can't use Graffiti/TalkieBox AND have a vending open, so this is safe.
  10748. safestrncpy(sd.message, RFIFOCP(fd,skillmoreinfo), MESSAGE_SIZE);
  10749. }
  10750. if( sd.ud.skilltimer != INVALID_TIMER )
  10751. return;
  10752. if( DIFF_TICK(tick, sd.ud.canact_tick) < 0 ) {
  10753. if( sd.skillitem != skill_id ) {
  10754. clif_skill_fail( sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10755. return;
  10756. }
  10757. }
  10758. if( sd.sc.option&OPTION_COSTUME )
  10759. return;
  10760. #ifndef RENEWAL
  10761. if( sd.sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd.sc.getSCE(SC_BASILICA)->val4 != sd.bl.id) )
  10762. return; // On basilica only caster can use Basilica again to stop it.
  10763. #endif
  10764. if( sd.menuskill_id ) {
  10765. if( sd.menuskill_id == SA_TAMINGMONSTER ) {
  10766. clif_menuskill_clear(&sd); //Cancel pet capture.
  10767. }else if( sd.menuskill_id == SG_FEEL ){
  10768. clif_menuskill_clear( &sd ); // Cancel selection
  10769. } else if( sd.menuskill_id != SA_AUTOSPELL )
  10770. return; //Can't use skills while a menu is open.
  10771. }
  10772. pc_delinvincibletimer(&sd);
  10773. if( sd.skillitem == skill_id ) {
  10774. if( skill_lv != sd.skillitemlv )
  10775. skill_lv = sd.skillitemlv;
  10776. unit_skilluse_pos(&sd.bl, x, y, skill_id, skill_lv);
  10777. } else {
  10778. int lv;
  10779. sd.skillitem = sd.skillitemlv = 0;
  10780. if( (lv = pc_checkskill(&sd, skill_id)) > 0 ) {
  10781. if( skill_lv > lv )
  10782. skill_lv = lv;
  10783. unit_skilluse_pos(&sd.bl, x, y, skill_id,skill_lv);
  10784. }
  10785. }
  10786. }
  10787. /// Request to use a ground skill.
  10788. /// 0116 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND)
  10789. /// 0366 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND2)
  10790. /// 0AF4 <skill lv>.W <skill id>.W <x>.W <y>.W <unknown>.B (CZ_USE_SKILL_TOGROUND3)
  10791. /// There are various variants of this packet, some of them have padding between fields.
  10792. void clif_parse_UseSkillToPos(int fd, map_session_data *sd)
  10793. {
  10794. if( sd == nullptr ){
  10795. return;
  10796. }
  10797. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10798. if (pc_cant_act(sd))
  10799. return;
  10800. if (pc_issit(sd))
  10801. return;
  10802. clif_parse_UseSkillToPosSub(fd, *sd,
  10803. RFIFOW(fd,info->pos[0]), //skill lv
  10804. RFIFOW(fd,info->pos[1]), //skill num
  10805. RFIFOW(fd,info->pos[2]), //pos x
  10806. RFIFOW(fd,info->pos[3]), //pos y
  10807. // TODO: find out what this is intended to do
  10808. //RFIFOB(fd, info->pos[4])
  10809. -1 //Skill more info.
  10810. );
  10811. }
  10812. /// Request to use a ground skill with text.
  10813. /// 0190 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX)
  10814. /// 0367 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX2)
  10815. /// There are various variants of this packet, some of them have padding between fields.
  10816. void clif_parse_UseSkillToPosMoreInfo(int fd, map_session_data *sd)
  10817. {
  10818. if( sd == nullptr ){
  10819. return;
  10820. }
  10821. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10822. if (pc_cant_act(sd))
  10823. return;
  10824. if (pc_issit(sd))
  10825. return;
  10826. clif_parse_UseSkillToPosSub(fd, *sd,
  10827. RFIFOW(fd,info->pos[0]), //Skill lv
  10828. RFIFOW(fd,info->pos[1]), //Skill num
  10829. RFIFOW(fd,info->pos[2]), //pos x
  10830. RFIFOW(fd,info->pos[3]), //pos y
  10831. info->pos[4] //skill more info
  10832. );
  10833. }
  10834. /// Answer to map selection dialog (CZ_SELECT_WARPPOINT).
  10835. /// 011b <skill id>.W <map name>.16B
  10836. void clif_parse_UseSkillMap(int fd, map_session_data* sd)
  10837. {
  10838. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10839. uint16 skill_id = RFIFOW(fd,info->pos[0]);
  10840. char map_name[MAP_NAME_LENGTH];
  10841. mapindex_getmapname(RFIFOCP(fd,info->pos[1]), map_name);
  10842. sd->state.workinprogress = WIP_DISABLE_NONE;
  10843. if(skill_id != sd->menuskill_id)
  10844. return;
  10845. //It is possible to use teleport with the storage window open bugreport:8027
  10846. if (pc_cant_act(sd) && !sd->state.storage_flag && skill_id != AL_TELEPORT) {
  10847. clif_menuskill_clear(sd);
  10848. return;
  10849. }
  10850. pc_delinvincibletimer(sd);
  10851. skill_castend_map(sd,skill_id,map_name);
  10852. }
  10853. /// Request to set a memo on current map (CZ_REMEMBER_WARPPOINT).
  10854. /// 011d
  10855. void clif_parse_RequestMemo(int fd,map_session_data *sd)
  10856. {
  10857. if (!pc_isdead(sd))
  10858. pc_memo(sd,-1);
  10859. }
  10860. /// Answer to pharmacy item selection dialog.
  10861. /// 018e <name id>.W { <material id>.W }*3 (CZ_REQMAKINGITEM)
  10862. void clif_parse_ProduceMix(int fd,map_session_data *sd){
  10863. if( sd == nullptr ){
  10864. return;
  10865. }
  10866. const PACKET_CZ_REQMAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQMAKINGITEM*>( RFIFOP( fd, 0 ) );
  10867. switch( sd->menuskill_id ) {
  10868. case -1:
  10869. case AM_PHARMACY:
  10870. case RK_RUNEMASTERY:
  10871. case GC_RESEARCHNEWPOISON:
  10872. break;
  10873. default:
  10874. return;
  10875. }
  10876. if (pc_istrading(sd)) {
  10877. //Make it fail to avoid shop exploits where you sell something different than you see.
  10878. clif_skill_fail( *sd, sd->ud.skill_id );
  10879. clif_menuskill_clear(sd);
  10880. return;
  10881. }
  10882. int produce_idx;
  10883. if( (produce_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, 1)) )
  10884. skill_produce_mix(sd,0,p->itemId,p->material[0],p->material[1],p->material[2],1,produce_idx-1);
  10885. clif_menuskill_clear(sd);
  10886. }
  10887. /// Answer to mixing item selection dialog.
  10888. /// 025b <mk type>.W <name id>.W (CZ_REQ_MAKINGITEM)
  10889. /// mk type:
  10890. /// 1 = cooking
  10891. /// 2 = arrow
  10892. /// 3 = elemental
  10893. /// 4 = GN_MIX_COOKING
  10894. /// 5 = GN_MAKEBOMB
  10895. /// 6 = GN_S_PHARMACY
  10896. /// 7 = MT_M_MACHINE - Unconfirmed
  10897. /// 8 = BO_BIONIC_PHARMACY - Unconfirmed
  10898. void clif_parse_Cooking(int fd,map_session_data *sd) {
  10899. #if PACKETVER >= 20051010
  10900. if( sd == nullptr ){
  10901. return;
  10902. }
  10903. const PACKET_CZ_REQ_MAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGITEM*>( RFIFOP( fd, 0 ) );
  10904. int amount = sd->menuskill_val2 ? sd->menuskill_val2 : 1;
  10905. short food_idx = -1;
  10906. if( p->type == 6 && sd->menuskill_id != GN_MIX_COOKING && sd->menuskill_id != GN_S_PHARMACY )
  10907. return;
  10908. if (pc_istrading(sd)) {
  10909. //Make it fail to avoid shop exploits where you sell something different than you see.
  10910. clif_skill_fail( *sd, sd->ud.skill_id );
  10911. clif_menuskill_clear(sd);
  10912. return;
  10913. }
  10914. if( (food_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, amount)) )
  10915. skill_produce_mix(sd,(p->type>1?sd->menuskill_id:0),p->itemId,0,0,0,amount,food_idx-1);
  10916. clif_menuskill_clear(sd);
  10917. #endif
  10918. }
  10919. /// Answer to repair weapon item selection dialog
  10920. /// 01fd <index> W (CZ_REQ_ITEMREPAIR)
  10921. /// 01fd <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W ???
  10922. void clif_parse_RepairItem( int fd, map_session_data *sd ){
  10923. if( sd == nullptr ){
  10924. return;
  10925. }
  10926. // Hercules has wrong date -> use correct one here
  10927. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10928. const PACKET_CZ_REQ_ITEMREPAIR2* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR2*>( RFIFOP( fd, 0 ) );
  10929. #else
  10930. const PACKET_CZ_REQ_ITEMREPAIR1* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR1*>( RFIFOP( fd, 0 ) );
  10931. #endif
  10932. if (sd->menuskill_id != BS_REPAIRWEAPON)
  10933. return;
  10934. if (pc_istrading(sd)) {
  10935. //Make it fail to avoid shop exploits where you sell something different than you see.
  10936. clif_skill_fail( *sd, sd->ud.skill_id );
  10937. clif_menuskill_clear(sd);
  10938. return;
  10939. }
  10940. skill_repairweapon( *sd, p->item.index );
  10941. clif_menuskill_clear(sd);
  10942. }
  10943. /// Answer to refine weapon item selection dialog (CZ_REQ_WEAPONREFINE).
  10944. /// 0222 <index>.L
  10945. void clif_parse_WeaponRefine( int fd, map_session_data *sd ){
  10946. if( sd == nullptr ){
  10947. return;
  10948. }
  10949. if (sd->menuskill_id != WS_WEAPONREFINE) //Packet exploit?
  10950. return;
  10951. if (pc_istrading(sd)) {
  10952. //Make it fail to avoid shop exploits where you sell something different than you see.
  10953. clif_skill_fail( *sd, sd->ud.skill_id );
  10954. clif_menuskill_clear(sd);
  10955. return;
  10956. }
  10957. skill_weaponrefine( *sd, server_index( RFIFOL( fd, 2 ) ) );
  10958. clif_menuskill_clear(sd);
  10959. }
  10960. /// Answer to script menu dialog (CZ_CHOOSE_MENU).
  10961. /// 00b8 <npc id>.L <choice>.B
  10962. /// choice:
  10963. /// 1~254 = menu item
  10964. /// 255 = cancel
  10965. /// NOTE: If there were more than 254 items in the list, choice
  10966. /// overflows to choice%256.
  10967. void clif_parse_NpcSelectMenu(int fd,map_session_data *sd){
  10968. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10969. int npc_id = RFIFOL(fd,info->pos[0]);
  10970. uint8 select = RFIFOB(fd,info->pos[1]);
  10971. #ifdef SECURE_NPCTIMEOUT
  10972. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  10973. return;
  10974. #endif
  10975. if( (select > sd->npc_menu && select != 0xff) || select == 0 ) {
  10976. TBL_NPC* nd = map_id2nd(npc_id);
  10977. 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);
  10978. clif_GM_kick(nullptr,sd);
  10979. return;
  10980. }
  10981. sd->npc_menu = select;
  10982. if( battle_config.idletime_option&IDLE_NPC_MENU ){
  10983. sd->idletime = last_tick;
  10984. }
  10985. npc_scriptcont(sd,npc_id, false);
  10986. }
  10987. /// NPC dialog 'next' click (CZ_REQ_NEXT_SCRIPT).
  10988. /// 00b9 <npc id>.L
  10989. void clif_parse_NpcNextClicked(int fd,map_session_data *sd)
  10990. {
  10991. if( battle_config.idletime_option&IDLE_NPC_NEXT ){
  10992. sd->idletime = last_tick;
  10993. }
  10994. npc_scriptcont(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), false);
  10995. }
  10996. /// NPC numeric input dialog value (CZ_INPUT_EDITDLG).
  10997. /// 0143 <npc id>.L <value>.L
  10998. void clif_parse_NpcAmountInput(int fd,map_session_data *sd){
  10999. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11000. int npcid = RFIFOL(fd,info->pos[0]);
  11001. int amount = (int)RFIFOL(fd,info->pos[1]);
  11002. sd->npc_amount = amount;
  11003. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  11004. sd->idletime = last_tick;
  11005. }
  11006. npc_scriptcont(sd, npcid, false);
  11007. }
  11008. /// NPC text input dialog value (CZ_INPUT_EDITDLGSTR).
  11009. /// 01d5 <packet len>.W <npc id>.L <string>.?B
  11010. void clif_parse_NpcStringInput(int fd, map_session_data* sd){
  11011. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11012. int message_len = RFIFOW(fd,info->pos[0])-8;
  11013. int npcid = RFIFOL(fd,info->pos[1]);
  11014. const char* message = RFIFOCP(fd,info->pos[2]);
  11015. if( message_len <= 0 )
  11016. return; // invalid input
  11017. #if PACKETVER >= 20151001
  11018. message_len++;
  11019. #endif
  11020. safestrncpy(sd->npc_str, message, min(message_len,CHATBOX_SIZE));
  11021. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  11022. sd->idletime = last_tick;
  11023. }
  11024. npc_scriptcont(sd, npcid, false);
  11025. }
  11026. /// NPC dialog 'close' click (CZ_CLOSE_DIALOG).
  11027. /// 0146 <npc id>.L
  11028. void clif_parse_NpcCloseClicked(int fd,map_session_data *sd)
  11029. {
  11030. if (!sd->npc_id) //Avoid parsing anything when the script was done with. [Skotlex]
  11031. return;
  11032. if( battle_config.idletime_option&IDLE_NPC_CLOSE ){
  11033. sd->idletime = last_tick;
  11034. }
  11035. npc_scriptcont(sd, RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), true);
  11036. }
  11037. /// Answer to identify item selection dialog (CZ_REQ_ITEMIDENTIFY).
  11038. /// 0178 <index>.W
  11039. /// index:
  11040. /// -1 = cancel
  11041. void clif_parse_ItemIdentify(int fd,map_session_data *sd) {
  11042. short idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2;
  11043. if (sd->menuskill_id != MC_IDENTIFY)
  11044. return;
  11045. // Ignore the request
  11046. // - Invalid item index
  11047. // - Invalid item ID or item doesn't exist
  11048. // - Item is already identified
  11049. if (idx < 0 || idx >= MAX_INVENTORY ||
  11050. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  11051. sd->inventory.u.items_inventory[idx].identify) {// cancel pressed
  11052. sd->state.workinprogress = WIP_DISABLE_NONE;
  11053. clif_menuskill_clear(sd);
  11054. return;
  11055. }
  11056. skill_identify(sd, idx);
  11057. clif_menuskill_clear(sd);
  11058. }
  11059. /// Answer to arrow crafting item selection dialog.
  11060. /// 01ae <name id>.W (CZ_REQ_MAKINGARROW)
  11061. void clif_parse_SelectArrow(int fd,map_session_data *sd) {
  11062. if( sd == nullptr ){
  11063. return;
  11064. }
  11065. if (pc_istrading(sd)) {
  11066. //Make it fail to avoid shop exploits where you sell something different than you see.
  11067. clif_skill_fail( *sd, sd->ud.skill_id );
  11068. clif_menuskill_clear(sd);
  11069. return;
  11070. }
  11071. const PACKET_CZ_REQ_MAKINGARROW* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGARROW*>( RFIFOP( fd, 0 ) );
  11072. switch (sd->menuskill_id) {
  11073. case AC_MAKINGARROW:
  11074. skill_arrow_create(sd,p->itemId);
  11075. break;
  11076. case SA_CREATECON:
  11077. skill_produce_mix(sd,SA_CREATECON,p->itemId,0,0,0,1,-1);
  11078. break;
  11079. case GC_POISONINGWEAPON:
  11080. skill_poisoningweapon(*sd,p->itemId);
  11081. break;
  11082. case NC_MAGICDECOY:
  11083. skill_magicdecoy(*sd,p->itemId);
  11084. break;
  11085. }
  11086. clif_menuskill_clear(sd);
  11087. }
  11088. /// Answer to SA_AUTOSPELL skill selection dialog (CZ_SELECTAUTOSPELL).
  11089. /// 01ce <skill id>.L
  11090. void clif_parse_AutoSpell(int fd,map_session_data *sd)
  11091. {
  11092. if (sd->menuskill_id != SA_AUTOSPELL)
  11093. return;
  11094. sd->state.workinprogress = WIP_DISABLE_NONE;
  11095. skill_autospell(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11096. clif_menuskill_clear(sd);
  11097. }
  11098. /// Request to display item carding/composition list (CZ_REQ_ITEMCOMPOSITION_LIST).
  11099. /// 017a <card index>.W
  11100. void clif_parse_UseCard(int fd,map_session_data *sd)
  11101. {
  11102. if (sd->state.trading != 0)
  11103. return;
  11104. clif_use_card(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  11105. }
  11106. /// Answer to carding/composing item selection dialog (CZ_REQ_ITEMCOMPOSITION).
  11107. /// 017c <card index>.W <equip index>.W
  11108. void clif_parse_InsertCard(int fd,map_session_data *sd)
  11109. {
  11110. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11111. if (sd->state.trading != 0)
  11112. return;
  11113. pc_insert_card(sd,RFIFOW(fd,info->pos[0])-2,RFIFOW(fd,info->pos[1])-2);
  11114. }
  11115. /// Request of character's name by char ID.
  11116. /// 0193 <char id>.L (CZ_REQNAME_BYGID)
  11117. /// 0369 <char id>.L (CZ_REQNAME_BYGID2)
  11118. /// There are various variants of this packet, some of them have padding between fields.
  11119. void clif_parse_SolveCharName(int fd, map_session_data *sd)
  11120. {
  11121. int charid;
  11122. charid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11123. map_reqnickdb(sd, charid);
  11124. }
  11125. /// /resetskill /resetstate (CZ_RESET).
  11126. /// Request to reset stats or skills.
  11127. /// 0197 <type>.W
  11128. /// type:
  11129. /// 0 = state
  11130. /// 1 = skill
  11131. void clif_parse_ResetChar(int fd, map_session_data *sd) {
  11132. char cmd[15];
  11133. if( RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) )
  11134. safesnprintf(cmd,sizeof(cmd),"%cresetskill",atcommand_symbol);
  11135. else
  11136. safesnprintf(cmd,sizeof(cmd),"%cresetstat",atcommand_symbol);
  11137. is_atcommand(fd, sd, cmd, 1);
  11138. }
  11139. /// /lb /nlb (CZ_LOCALBROADCAST).
  11140. /// Request to broadcast a message on current map.
  11141. /// 019c <packet len>.W <text>.?B
  11142. void clif_parse_LocalBroadcast(int fd, map_session_data* sd)
  11143. {
  11144. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11145. char command[CHAT_SIZE_MAX+16];
  11146. uint32 len = RFIFOW(fd,info->pos[0])-4;
  11147. char* msg = RFIFOCP(fd,info->pos[1]);
  11148. // as the length varies depending on the command used, just block unreasonably long strings
  11149. mes_len_check(msg, len, CHAT_SIZE_MAX);
  11150. safesnprintf(command,sizeof(command),"%clkami %s", atcommand_symbol, msg);
  11151. is_atcommand(fd, sd, command, 1);
  11152. }
  11153. /// Request to move an item from inventory to storage.
  11154. /// 00f3 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE)
  11155. /// 0364 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE2)
  11156. /// There are various variants of this packet, some of them have padding between fields.
  11157. void clif_parse_MoveToKafra(int fd, map_session_data *sd)
  11158. {
  11159. int item_index, item_amount;
  11160. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11161. if (pc_istrading(sd))
  11162. return;
  11163. item_index = RFIFOW(fd,info->pos[0])-2;
  11164. item_amount = RFIFOL(fd,info->pos[1]);
  11165. if (item_index < 0 || item_index >= MAX_INVENTORY || item_amount < 1)
  11166. return;
  11167. if( sd->inventory.u.items_inventory[item_index].equipSwitch ){
  11168. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11169. return;
  11170. }
  11171. if (sd->state.storage_flag == 1)
  11172. storage_storageadd(sd, &sd->storage, item_index, item_amount);
  11173. else
  11174. if (sd->state.storage_flag == 2)
  11175. storage_guild_storageadd(sd, item_index, item_amount);
  11176. else if (sd->state.storage_flag == 3)
  11177. storage_storageadd(sd, &sd->premiumStorage, item_index, item_amount);
  11178. }
  11179. /// Request to move an item from storage to inventory.
  11180. /// 00f5 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY)
  11181. /// 0365 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY2)
  11182. /// There are various variants of this packet, some of them have padding between fields.
  11183. void clif_parse_MoveFromKafra(int fd,map_session_data *sd)
  11184. {
  11185. int item_index, item_amount;
  11186. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11187. item_index = RFIFOW(fd,info->pos[0])-1;
  11188. item_amount = RFIFOL(fd,info->pos[1]);
  11189. if (sd->state.storage_flag == 1)
  11190. storage_storageget(sd, &sd->storage, item_index, item_amount);
  11191. else if(sd->state.storage_flag == 2)
  11192. storage_guild_storageget(sd, item_index, item_amount);
  11193. else if(sd->state.storage_flag == 3)
  11194. storage_storageget(sd, &sd->premiumStorage, item_index, item_amount);
  11195. }
  11196. /// Request to move an item from cart to storage (CZ_MOVE_ITEM_FROM_CART_TO_STORE).
  11197. /// 0129 <index>.W <amount>.L
  11198. void clif_parse_MoveToKafraFromCart(int fd, map_session_data *sd){
  11199. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11200. int idx = RFIFOW(fd,info->pos[0]) - 2;
  11201. int amount = RFIFOL(fd,info->pos[1]);
  11202. if( sd->state.vending )
  11203. return;
  11204. if (!pc_iscarton(sd))
  11205. return;
  11206. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11207. return;
  11208. if (idx < 0 || idx >= MAX_INVENTORY || amount < 1)
  11209. return;
  11210. if( sd->inventory.u.items_inventory[idx].equipSwitch ){
  11211. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11212. return;
  11213. }
  11214. if (sd->state.storage_flag == 1)
  11215. storage_storageaddfromcart(sd, &sd->storage, idx, amount);
  11216. else if (sd->state.storage_flag == 2)
  11217. storage_guild_storageaddfromcart(sd, idx, amount);
  11218. else if (sd->state.storage_flag == 3)
  11219. storage_storageaddfromcart(sd, &sd->premiumStorage, idx, amount);
  11220. }
  11221. /// Request to move an item from storage to cart (CZ_MOVE_ITEM_FROM_STORE_TO_CART).
  11222. /// 0128 <index>.W <amount>.L
  11223. void clif_parse_MoveFromKafraToCart(int fd, map_session_data *sd){
  11224. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11225. int idx = RFIFOW(fd,info->pos[0]) - 1;
  11226. int amount = RFIFOL(fd,info->pos[1]);
  11227. if( sd->state.vending )
  11228. return;
  11229. if (!pc_iscarton(sd))
  11230. return;
  11231. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11232. return;
  11233. if (sd->state.storage_flag == 1)
  11234. storage_storagegettocart(sd, &sd->storage, idx, amount);
  11235. else
  11236. if (sd->state.storage_flag == 2)
  11237. storage_guild_storagegettocart(sd, idx, amount);
  11238. else if (sd->state.storage_flag == 3)
  11239. storage_storagegettocart(sd, &sd->premiumStorage, idx, amount);
  11240. }
  11241. /// Request to close storage (CZ_CLOSE_STORE).
  11242. /// 00f7
  11243. void clif_parse_CloseKafra(int fd, map_session_data *sd)
  11244. {
  11245. if( sd->state.storage_flag == 1 )
  11246. storage_storageclose(sd);
  11247. else
  11248. if( sd->state.storage_flag == 2 )
  11249. storage_guild_storageclose(sd);
  11250. else if( sd->state.storage_flag == 3 )
  11251. storage_premiumStorage_close(sd);
  11252. }
  11253. /// Displays kafra storage password dialog (ZC_REQ_STORE_PASSWORD).
  11254. /// 023a <info>.W
  11255. /// info:
  11256. /// 0 = password has not been set yet
  11257. /// 1 = storage is password-protected
  11258. /// 8 = too many wrong passwords
  11259. /// ? = ignored
  11260. /// NOTE: This packet is only available on certain non-kRO clients.
  11261. void clif_storagepassword(map_session_data* sd, short info)
  11262. {
  11263. int fd = sd->fd;
  11264. WFIFOHEAD(fd,packet_len(0x23a));
  11265. WFIFOW(fd,0) = 0x23a;
  11266. WFIFOW(fd,2) = info;
  11267. WFIFOSET(fd,packet_len(0x23a));
  11268. }
  11269. /// Answer to the kafra storage password dialog (CZ_ACK_STORE_PASSWORD).
  11270. /// 023b <type>.W <password>.16B <new password>.16B
  11271. /// type:
  11272. /// 2 = change password
  11273. /// 3 = check password
  11274. /// NOTE: This packet is only available on certain non-kRO clients.
  11275. void clif_parse_StoragePassword(int fd, map_session_data *sd){ //@TODO
  11276. // struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11277. // int type = RFIFOW(fd,info->pos[0]);
  11278. // char* password = RFIFOP(fd,info->pos[1]);
  11279. // char* new_password = RFIFOP(fd,info->pos[2]);
  11280. // storage_changepass(sd,password,new_password);
  11281. }
  11282. /// Result of kafra storage password validation (ZC_RESULT_STORE_PASSWORD).
  11283. /// 023c <result>.W <error count>.W
  11284. /// result:
  11285. /// 4 = password change success
  11286. /// 5 = password change failure
  11287. /// 6 = password check success
  11288. /// 7 = password check failure
  11289. /// 8 = too many wrong passwords
  11290. /// ? = ignored
  11291. /// NOTE: This packet is only available on certain non-kRO clients.
  11292. void clif_storagepassword_result(map_session_data* sd, short result, short error_count)
  11293. {
  11294. int fd = sd->fd;
  11295. WFIFOHEAD(fd,packet_len(0x23c));
  11296. WFIFOW(fd,0) = 0x23c;
  11297. WFIFOW(fd,2) = result;
  11298. WFIFOW(fd,4) = error_count;
  11299. WFIFOSET(fd,packet_len(0x23c));
  11300. }
  11301. /// Party creation request
  11302. /// 00f9 <party name>.24B (CZ_MAKE_GROUP)
  11303. void clif_parse_CreateParty(int fd, map_session_data *sd){
  11304. if( sd == nullptr ){
  11305. return;
  11306. }
  11307. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11308. name[NAME_LENGTH-1] = '\0';
  11309. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11310. clif_displaymessage(fd, msg_txt(sd,227));
  11311. return;
  11312. }
  11313. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11314. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11315. return;
  11316. }
  11317. party_create( *sd, name, 0, 0 );
  11318. }
  11319. /// 01e8 <party name>.24B <item pickup rule>.B <item share rule>.B (CZ_MAKE_GROUP2)
  11320. void clif_parse_CreateParty2(int fd, map_session_data *sd){
  11321. if( sd == nullptr ){
  11322. return;
  11323. }
  11324. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11325. char* name = RFIFOCP(fd,info->pos[0]);
  11326. int item1 = RFIFOB(fd,info->pos[1]);
  11327. int item2 = RFIFOB(fd,info->pos[2]);
  11328. name[NAME_LENGTH-1] = '\0';
  11329. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11330. clif_displaymessage(fd, msg_txt(sd,227));
  11331. return;
  11332. }
  11333. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11334. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11335. return;
  11336. }
  11337. party_create( *sd, name, item1, item2 );
  11338. }
  11339. /// Party invitation request by account id
  11340. /// 00fc <account id>.L (CZ_REQ_JOIN_GROUP)
  11341. void clif_parse_PartyInvite( int fd, map_session_data *sd ){
  11342. if( sd == nullptr ){
  11343. return;
  11344. }
  11345. const PACKET_CZ_REQ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11346. party_invite( *sd, map_id2sd( p->AID ) );
  11347. }
  11348. /// Party invitation request by name
  11349. /// 02c4 <char name>.24B (CZ_PARTY_JOIN_REQ)
  11350. void clif_parse_PartyInvite2( int fd, map_session_data *sd ){
  11351. #if PACKETVER >= 20070227
  11352. if( sd == nullptr ){
  11353. return;
  11354. }
  11355. const PACKET_CZ_PARTY_JOIN_REQ* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ*>( RFIFOP( fd, 0 ) );
  11356. char name[NAME_LENGTH];
  11357. safestrncpy( name, p->name, sizeof( name ) );
  11358. party_invite( *sd, map_nick2sd( name, false ) );
  11359. #endif
  11360. }
  11361. /// Party invitation reply
  11362. /// 00ff <party id>.L <flag>.L (CZ_JOIN_GROUP)
  11363. /// flag:
  11364. /// 0 = reject
  11365. /// 1 = accept
  11366. void clif_parse_ReplyPartyInvite( int fd, map_session_data *sd ){
  11367. if( sd == nullptr ){
  11368. return;
  11369. }
  11370. const PACKET_CZ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11371. party_reply_invite( *sd, p->party_id, p->flag );
  11372. }
  11373. /// Party invitation reply
  11374. /// 02c7 <party id>.L <flag>.B (CZ_PARTY_JOIN_REQ_ACK)
  11375. /// flag:
  11376. /// 0 = reject
  11377. /// 1 = accept
  11378. void clif_parse_ReplyPartyInvite2( int fd, map_session_data *sd ){
  11379. #if PACKETVER >= 20070227
  11380. if( sd == nullptr ){
  11381. return;
  11382. }
  11383. const PACKET_CZ_PARTY_JOIN_REQ_ACK* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ_ACK*>( RFIFOP( fd, 0 ) );
  11384. party_reply_invite( *sd, p->party_id, p->flag );
  11385. #endif
  11386. }
  11387. /// Request to leave party.
  11388. /// 0100 (CZ_REQ_LEAVE_GROUP)
  11389. void clif_parse_LeaveParty( int fd, map_session_data *sd ){
  11390. if( sd == nullptr ){
  11391. return;
  11392. }
  11393. //const PACKET_CZ_REQ_LEAVE_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GROUP*>( RFIFOP( fd, 0 ) );
  11394. party_leave( *sd, true );
  11395. }
  11396. /// Request to expel a party member.
  11397. /// 0103 <account id>.L <char name>.24B (CZ_REQ_EXPEL_GROUP_MEMBER)
  11398. void clif_parse_RemovePartyMember( int fd, map_session_data* sd ){
  11399. if( sd == nullptr ){
  11400. return;
  11401. }
  11402. const PACKET_CZ_REQ_EXPEL_GROUP_MEMBER* p = reinterpret_cast<PACKET_CZ_REQ_EXPEL_GROUP_MEMBER*>( RFIFOP( fd, 0 ) );
  11403. party_removemember( *sd, p->AID, p->name );
  11404. }
  11405. /// Request to change party options.
  11406. /// 0102 <exp share rule>.L (CZ_CHANGE_GROUPEXPOPTION)
  11407. /// 07d7 <exp share rule>.L <item pickup rule>.B <item share rule>.B (CZ_GROUPINFO_CHANGE_V2)
  11408. void clif_parse_PartyChangeOption(int fd, map_session_data *sd)
  11409. {
  11410. struct party_data *p;
  11411. int i,expflag;
  11412. int cmd = RFIFOW(fd,0);
  11413. struct s_packet_db* info = &packet_db[cmd];
  11414. if( !sd->status.party_id )
  11415. return;
  11416. p = party_search(sd->status.party_id);
  11417. if( p == nullptr )
  11418. return;
  11419. ARR_FIND( 0, MAX_PARTY, i, p->data[i].sd == sd );
  11420. if( i == MAX_PARTY )
  11421. return; //Shouldn't happen
  11422. if( !p->party.member[i].leader )
  11423. return;
  11424. expflag = RFIFOL(fd,info->pos[0]);
  11425. if(cmd == 0x0102){ //Client can't change the item-field
  11426. party_changeoption(sd, expflag, p->party.item);
  11427. }
  11428. else {
  11429. int itemflag = (RFIFOB(fd,info->pos[1])?1:0)|(RFIFOB(fd,info->pos[2])?2:0);
  11430. party_changeoption(sd, expflag, itemflag);
  11431. }
  11432. }
  11433. /// Validates and processes party messages (CZ_REQUEST_CHAT_PARTY).
  11434. /// 0108 <packet len>.W <text>.?B (<name> : <message>) 00
  11435. void clif_parse_PartyMessage(int fd, map_session_data* sd){
  11436. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11437. // validate packet and retrieve name and message
  11438. if( !clif_process_message( sd, false, name, message, output ) )
  11439. return;
  11440. party_send_message(sd, output, strlen(output) + 1 );
  11441. }
  11442. /// Changes Party Leader (CZ_CHANGE_GROUP_MASTER).
  11443. /// 07da <account id>.L
  11444. void clif_parse_PartyChangeLeader(int fd, map_session_data* sd){
  11445. party_changeleader(sd, map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0])),nullptr);
  11446. }
  11447. /// Party Booking in KRO [Spiria]
  11448. ///
  11449. /// Request to register a party booking advertisment (CZ_PARTY_BOOKING_REQ_REGISTER).
  11450. /// 0802 <level>.W <map id>.W { <job>.W }*6
  11451. void clif_parse_PartyBookingRegisterReq(int fd, map_session_data* sd){
  11452. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11453. short level = RFIFOW(fd,info->pos[0]);
  11454. short mapid = RFIFOW(fd,info->pos[1]);
  11455. int idxpbj = info->pos[2];
  11456. short job[MAX_PARTY_BOOKING_JOBS];
  11457. int i;
  11458. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11459. job[i] = RFIFOB(fd,idxpbj+i*2);
  11460. party_booking_register(sd, level, mapid, job);
  11461. }
  11462. /// Result of request to register a party booking advertisment (ZC_PARTY_BOOKING_ACK_REGISTER).
  11463. /// 0803 <result>.W
  11464. /// result:
  11465. /// 0 = success
  11466. /// 1 = failure
  11467. /// 2 = already registered
  11468. void clif_PartyBookingRegisterAck(map_session_data *sd, int flag)
  11469. {
  11470. int fd = sd->fd;
  11471. WFIFOHEAD(fd,packet_len(0x803));
  11472. WFIFOW(fd,0) = 0x803;
  11473. WFIFOW(fd,2) = flag;
  11474. WFIFOSET(fd,packet_len(0x803));
  11475. }
  11476. /// Request to search for party booking advertisments (CZ_PARTY_BOOKING_REQ_SEARCH).
  11477. /// 0804 <level>.W <map id>.W <job>.W <last index>.L <result count>.W
  11478. void clif_parse_PartyBookingSearchReq(int fd, map_session_data* sd){
  11479. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11480. short level = RFIFOW(fd,info->pos[0]);
  11481. short mapid = RFIFOW(fd,info->pos[1]);
  11482. short job = RFIFOW(fd,info->pos[2]);
  11483. unsigned long lastindex = RFIFOL(fd,info->pos[3]);
  11484. short resultcount = RFIFOW(fd,info->pos[4]);
  11485. party_booking_search(sd, level, mapid, job, lastindex, resultcount);
  11486. }
  11487. /// Party booking search results (ZC_PARTY_BOOKING_ACK_SEARCH).
  11488. /// 0805 <packet len>.W <more results>.B { <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6 }*
  11489. /// more results:
  11490. /// 0 = no
  11491. /// 1 = yes
  11492. void clif_PartyBookingSearchAck(int fd, struct party_booking_ad_info** results, int count, bool more_result)
  11493. {
  11494. int i, j;
  11495. int size = sizeof(struct party_booking_ad_info); // structure size (48)
  11496. struct party_booking_ad_info *pb_ad;
  11497. WFIFOHEAD(fd,size*count + 5);
  11498. WFIFOW(fd,0) = 0x805;
  11499. WFIFOW(fd,2) = size*count + 5;
  11500. WFIFOB(fd,4) = more_result;
  11501. for(i=0; i<count; i++)
  11502. {
  11503. pb_ad = results[i];
  11504. WFIFOL(fd,i*size+5) = pb_ad->index;
  11505. safestrncpy(WFIFOCP(fd,i*size+9),pb_ad->charname,NAME_LENGTH);
  11506. WFIFOL(fd,i*size+33) = pb_ad->starttime; // FIXME: This is expire time
  11507. WFIFOW(fd,i*size+37) = pb_ad->p_detail.level;
  11508. WFIFOW(fd,i*size+39) = pb_ad->p_detail.mapid;
  11509. for(j=0; j<MAX_PARTY_BOOKING_JOBS; j++)
  11510. WFIFOW(fd,i*size+41+j*2) = pb_ad->p_detail.job[j];
  11511. }
  11512. WFIFOSET(fd,WFIFOW(fd,2));
  11513. }
  11514. /// Request to delete own party booking advertisment (CZ_PARTY_BOOKING_REQ_DELETE).
  11515. /// 0806
  11516. void clif_parse_PartyBookingDeleteReq(int fd, map_session_data* sd)
  11517. {
  11518. if(party_booking_delete(sd))
  11519. clif_PartyBookingDeleteAck(sd, 0);
  11520. }
  11521. /// Result of request to delete own party booking advertisment (ZC_PARTY_BOOKING_ACK_DELETE).
  11522. /// 0807 <result>.W
  11523. /// result:
  11524. /// 0 = success
  11525. /// 1 = success (auto-removed expired ad)
  11526. /// 2 = failure
  11527. /// 3 = nothing registered
  11528. void clif_PartyBookingDeleteAck(map_session_data* sd, int flag)
  11529. {
  11530. int fd = sd->fd;
  11531. WFIFOHEAD(fd,packet_len(0x807));
  11532. WFIFOW(fd,0) = 0x807;
  11533. WFIFOW(fd,2) = flag;
  11534. WFIFOSET(fd,packet_len(0x807));
  11535. }
  11536. /// Request to update party booking advertisment (CZ_PARTY_BOOKING_REQ_UPDATE).
  11537. /// 0808 { <job>.W }*6
  11538. void clif_parse_PartyBookingUpdateReq(int fd, map_session_data* sd)
  11539. {
  11540. short job[MAX_PARTY_BOOKING_JOBS];
  11541. int i;
  11542. int idxpbu = packet_db[RFIFOW(fd,0)].pos[0];
  11543. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11544. job[i] = RFIFOW(fd,idxpbu+i*2);
  11545. party_booking_update(sd, job);
  11546. }
  11547. /// Notification about new party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_INSERT).
  11548. /// 0809 <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6
  11549. void clif_PartyBookingInsertNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11550. {
  11551. int i;
  11552. uint8 buf[38+MAX_PARTY_BOOKING_JOBS*2];
  11553. if(pb_ad == nullptr) return;
  11554. WBUFW(buf,0) = 0x809;
  11555. WBUFL(buf,2) = pb_ad->index;
  11556. safestrncpy(WBUFCP(buf,6),pb_ad->charname,NAME_LENGTH);
  11557. WBUFL(buf,30) = pb_ad->starttime; // FIXME: This is expire time
  11558. WBUFW(buf,34) = pb_ad->p_detail.level;
  11559. WBUFW(buf,36) = pb_ad->p_detail.mapid;
  11560. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11561. WBUFW(buf,38+i*2) = pb_ad->p_detail.job[i];
  11562. clif_send(buf, packet_len(0x809), &sd->bl, ALL_CLIENT);
  11563. }
  11564. /// Notification about updated party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_UPDATE).
  11565. /// 080a <index>.L { <job>.W }*6
  11566. void clif_PartyBookingUpdateNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11567. {
  11568. int i;
  11569. uint8 buf[6+MAX_PARTY_BOOKING_JOBS*2];
  11570. if(pb_ad == nullptr) return;
  11571. WBUFW(buf,0) = 0x80a;
  11572. WBUFL(buf,2) = pb_ad->index;
  11573. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11574. WBUFW(buf,6+i*2) = pb_ad->p_detail.job[i];
  11575. clif_send(buf,packet_len(0x80a),&sd->bl,ALL_CLIENT); // Now UPDATE all client.
  11576. }
  11577. /// Notification about deleted party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_DELETE).
  11578. /// 080b <index>.L
  11579. void clif_PartyBookingDeleteNotify(map_session_data* sd, int index)
  11580. {
  11581. uint8 buf[6];
  11582. WBUFW(buf,0) = 0x80b;
  11583. WBUFL(buf,2) = index;
  11584. clif_send(buf, packet_len(0x80b), &sd->bl, ALL_CLIENT); // Now UPDATE all client.
  11585. }
  11586. /// Request to close own vending (CZ_REQ_CLOSESTORE).
  11587. /// 012e
  11588. void clif_parse_CloseVending(int fd, map_session_data* sd)
  11589. {
  11590. vending_closevending(sd);
  11591. }
  11592. /// Request to open a vending shop (CZ_REQ_BUY_FROMMC).
  11593. /// 0130 <account id>.L
  11594. void clif_parse_VendingListReq(int fd, map_session_data* sd)
  11595. {
  11596. if( sd->npc_id ) {// using an NPC
  11597. return;
  11598. }
  11599. vending_vendinglistreq(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11600. }
  11601. /// Shop item(s) purchase request.
  11602. /// 0134 <packet len>.W <account id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC)
  11603. void clif_parse_PurchaseReq(int fd, map_session_data* sd){
  11604. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC*>( RFIFOP( fd, 0 ) );
  11605. vending_purchasereq( sd, p->AID, sd->vended_id, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11606. // whether it fails or not, the buy window is closed
  11607. sd->vended_id = 0;
  11608. }
  11609. /// Shop item(s) purchase request.
  11610. /// 0801 <packet len>.W <account id>.L <unique id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC2)
  11611. void clif_parse_PurchaseReq2(int fd, map_session_data* sd){
  11612. #if PACKETVER >= 20100105
  11613. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2*>( RFIFOP( fd, 0 ) );
  11614. vending_purchasereq( sd, p->AID, p->UniqueID, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11615. // whether it fails or not, the buy window is closed
  11616. sd->vended_id = 0;
  11617. #endif
  11618. }
  11619. /// Confirm or cancel the shop preparation window.
  11620. /// 012f <packet len>.W <shop name>.80B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE)
  11621. /// 01b2 <packet len>.W <shop name>.80B <result>.B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE2)
  11622. /// result:
  11623. /// 0 = canceled
  11624. /// 1 = open
  11625. void clif_parse_OpenVending(int fd, map_session_data* sd){
  11626. if( sd == nullptr ){
  11627. return;
  11628. }
  11629. int cmd = RFIFOW(fd,0);
  11630. struct s_packet_db* info = &packet_db[cmd];
  11631. short len = (short)RFIFOW(fd,info->pos[0]);
  11632. const char* message = RFIFOCP(fd,info->pos[1]);
  11633. const uint8* data = (uint8*)RFIFOP(fd,info->pos[3]);
  11634. if(cmd == 0x12f){ // (CZ_REQ_OPENSTORE)
  11635. len -= 84;
  11636. }
  11637. else { //(CZ_REQ_OPENSTORE2)
  11638. bool flag;
  11639. len -= 85;
  11640. flag = RFIFOB(fd,info->pos[2]) != 0;
  11641. if (!flag) {
  11642. sd->state.prevend = 0;
  11643. sd->state.workinprogress = WIP_DISABLE_NONE;
  11644. }
  11645. }
  11646. if( sd->sc.getSCE(SC_NOCHAT) && sd->sc.getSCE(SC_NOCHAT)->val1&MANNER_NOROOM )
  11647. return;
  11648. if( map_getmapflag(sd->bl.m, MF_NOVENDING) ) {
  11649. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  11650. return;
  11651. }
  11652. if( map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNOVENDING) ) {
  11653. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  11654. return;
  11655. }
  11656. if( message[0] == '\0' ) // invalid input
  11657. return;
  11658. vending_openvending(*sd, message, data, len/8, nullptr);
  11659. }
  11660. /// Guild creation request (CZ_REQ_MAKE_GUILD).
  11661. /// 0165 <char id>.L <guild name>.24B
  11662. void clif_parse_CreateGuild(int fd,map_session_data *sd){
  11663. //int charid = RFIFOL(fd,packet_db[cmd].pos[0]);
  11664. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11665. name[NAME_LENGTH-1] = '\0';
  11666. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11667. clif_displaymessage(fd, msg_txt(sd,228));
  11668. return;
  11669. }
  11670. if(sd->clan){
  11671. // Should display a clientside message "You are currently joined in Clan !!" so we ignore it
  11672. return;
  11673. }
  11674. guild_create( *sd, name );
  11675. }
  11676. /// Request for guild window interface permissions
  11677. /// 014d (CZ_REQ_GUILD_MENUINTERFACE)
  11678. static void clif_parse_GuildCheckMaster(int fd, map_session_data *sd){
  11679. if(sd == nullptr)
  11680. return;
  11681. clif_guild_masterormember(*sd);
  11682. }
  11683. /// Request for guild window information (CZ_REQ_GUILD_MENU).
  11684. /// 014f <type>.L
  11685. /// type:
  11686. /// 0 = basic info
  11687. /// 1 = member manager
  11688. /// 2 = positions
  11689. /// 3 = skills
  11690. /// 4 = expulsion list
  11691. /// 5 = unknown (GM_ALLGUILDLIST)
  11692. /// 6 = notice
  11693. void clif_parse_GuildRequestInfo(int fd, map_session_data *sd)
  11694. {
  11695. int type = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11696. if( !sd->status.guild_id && !sd->bg_id )
  11697. return;
  11698. switch( type )
  11699. {
  11700. case 0: // Basic Information Guild, hostile alliance information
  11701. clif_guild_basicinfo( *sd );
  11702. clif_guild_allianceinfo(*sd);
  11703. clif_guild_castle_list(*sd);
  11704. break;
  11705. case 1: // Members list, list job title
  11706. clif_guild_positionnamelist(sd);
  11707. clif_guild_memberlist( *sd );
  11708. break;
  11709. case 2: // List job title, title information list
  11710. clif_guild_positionnamelist(sd);
  11711. clif_guild_positioninfolist(*sd);
  11712. break;
  11713. case 3: // Skill list
  11714. clif_guild_skillinfo( *sd );
  11715. break;
  11716. case 4: // Expulsion list
  11717. clif_guild_expulsionlist(*sd);
  11718. break;
  11719. default:
  11720. ShowError("clif: guild request info: unknown type %d\n", type);
  11721. break;
  11722. }
  11723. }
  11724. /// Request to update guild positions (CZ_REG_CHANGE_GUILD_POSITIONINFO).
  11725. /// 0161 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <name>.24B }*
  11726. void clif_parse_GuildChangePositionInfo(int fd, map_session_data *sd)
  11727. {
  11728. int i;
  11729. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11730. int len = RFIFOW(fd,info->pos[0]);
  11731. int idxgpos = info->pos[1];
  11732. if(!sd->state.gmaster_flag)
  11733. return;
  11734. for(i = idxgpos; i < len; i += 40 ){
  11735. guild_change_position(sd->status.guild_id, RFIFOL(fd,i), RFIFOL(fd,i+4), RFIFOL(fd,i+12), RFIFOCP(fd,i+16));
  11736. }
  11737. }
  11738. /// Request to update the position of guild members.
  11739. /// 0155 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (CZ_REQ_CHANGE_MEMBERPOS)
  11740. void clif_parse_GuildChangeMemberPosition( int fd, map_session_data *sd ){
  11741. if(!sd->state.gmaster_flag)
  11742. return;
  11743. const PACKET_CZ_REQ_CHANGE_MEMBERPOS* p = reinterpret_cast<PACKET_CZ_REQ_CHANGE_MEMBERPOS*>( RFIFOP( fd, 0 ) );
  11744. int16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  11745. for( int16 i = 0; i < entries; i++ ){
  11746. const PACKET_CZ_REQ_CHANGE_MEMBERPOS_sub& entry = p->list[i];
  11747. // Guild leadership change
  11748. if( entry.position == 0 ){
  11749. if( !battle_config.guild_leaderchange_woe && is_agit_start() ){
  11750. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_IN_SIEGE_TIME );
  11751. return;
  11752. }
  11753. if( battle_config.guild_leaderchange_delay && DIFF_TICK( time( nullptr ),sd->guild->guild.last_leader_change ) < battle_config.guild_leaderchange_delay ){
  11754. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_NOT_TIME );
  11755. return;
  11756. }
  11757. guild_gm_change( sd->status.guild_id, entry.CID );
  11758. // No further entries will be processed - the requesting player lost his guild master status
  11759. return;
  11760. }else if( entry.position > 0 ){
  11761. guild_change_memberposition( sd->status.guild_id, entry.AID, entry.CID, entry.position );
  11762. }
  11763. }
  11764. }
  11765. /// Request for guild emblem data (CZ_REQ_GUILD_EMBLEM_IMG).
  11766. /// 0151 <guild id>.L
  11767. void clif_parse_GuildRequestEmblem(int fd,map_session_data *sd)
  11768. {
  11769. int guild_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11770. auto g = guild_search(guild_id);
  11771. if (g)
  11772. clif_guild_emblem(*sd, g->guild);
  11773. }
  11774. /// Validates data of a guild emblem (compressed bitmap)
  11775. enum e_result_validate_emblem { // Used as Result for clif_validate_emblem
  11776. EMBVALIDATE_SUCCESS = 0,
  11777. EMBVALIDATE_ERR_RAW_FILEFORMAT, // Invalid File Format (Error in zlib/decompression or malformed BMP header)
  11778. EMBVALIDATE_ERR_TRANSPARENCY // uploaded emblem does not met the requirements of inter_config.emblem_transparency_limit
  11779. };
  11780. static enum e_result_validate_emblem clif_validate_emblem(const uint8* emblem, unsigned long emblem_len)
  11781. {
  11782. uint8 buf[1800]; // no well-formed emblem bitmap is larger than 1782 (24 bit) / 1654 (8 bit) bytes
  11783. unsigned long buf_len = sizeof(buf);
  11784. int offset = 0;
  11785. if(!(( decode_zip(buf, &buf_len, emblem, emblem_len) == 0 && buf_len >= 18 ) // sizeof(BITMAPFILEHEADER) + sizeof(biSize) of the following info header struct
  11786. && RBUFW(buf,0) == 0x4d42 // BITMAPFILEHEADER.bfType (signature)
  11787. && RBUFL(buf,2) == buf_len // BITMAPFILEHEADER.bfSize (file size)
  11788. && (offset = RBUFL(buf,10)) < buf_len // BITMAPFILEHEADER.bfOffBits (offset to bitmap bits)
  11789. ))
  11790. return EMBVALIDATE_ERR_RAW_FILEFORMAT;
  11791. if(inter_config.emblem_transparency_limit != 100) {
  11792. int i, transcount = 1, tmp[3];
  11793. for(i = offset; i < buf_len-1; i++) {
  11794. int j = i%3;
  11795. tmp[j] = RBUFL(buf,i);
  11796. if(j == 2 && (tmp[0] == 0xFFFF00FF) && (tmp[1] == 0xFFFF00) && (tmp[2] == 0xFF00FFFF)) //if pixel is transparent
  11797. transcount++;
  11798. }
  11799. if(((transcount*300)/(buf_len-offset)) > inter_config.emblem_transparency_limit) //convert in % to chk
  11800. return EMBVALIDATE_ERR_TRANSPARENCY;
  11801. }
  11802. return EMBVALIDATE_SUCCESS;
  11803. }
  11804. /// Request to update the guild emblem (CZ_REGISTER_GUILD_EMBLEM_IMG).
  11805. /// 0153 <packet len>.W <emblem data>.?B
  11806. void clif_parse_GuildChangeEmblem(int fd,map_session_data *sd){
  11807. if( sd == nullptr ){
  11808. return;
  11809. }
  11810. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11811. unsigned long emblem_len = RFIFOW(fd,info->pos[0])-4;
  11812. const uint8* emblem = RFIFOP(fd,info->pos[1]);
  11813. int emb_val=0;
  11814. if( !emblem_len || !sd->state.gmaster_flag )
  11815. return;
  11816. if(!(inter_config.emblem_woe_change) && is_agit_start() ){
  11817. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,385),false,SELF); //"You not allowed to change emblem during woe"
  11818. return;
  11819. }
  11820. emb_val = clif_validate_emblem(emblem, emblem_len);
  11821. if(emb_val == EMBVALIDATE_ERR_RAW_FILEFORMAT){
  11822. 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);
  11823. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,386),false,SELF); //"The chosen emblem was detected invalid\n"
  11824. return;
  11825. } else if(emb_val == EMBVALIDATE_ERR_TRANSPARENCY){
  11826. char output[128];
  11827. safesnprintf(output,sizeof(output),msg_txt(sd,387),inter_config.emblem_transparency_limit);
  11828. 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"
  11829. return;
  11830. }
  11831. guild_change_emblem( *sd, emblem_len, (const char*)emblem );
  11832. }
  11833. /// Request to update the guild emblem id
  11834. /// 0b46 <guild id>.L <version>.L
  11835. void clif_parse_GuildChangeEmblem2(int fd, map_session_data* sd) {
  11836. if( sd == nullptr ){
  11837. return;
  11838. }
  11839. #if PACKETVER >= 20190724
  11840. const PACKET_CZ_REQ_ADD_NEW_EMBLEM* p = reinterpret_cast<PACKET_CZ_REQ_ADD_NEW_EMBLEM*>( RFIFOP( fd, 0 ) );
  11841. auto &g = sd->guild;
  11842. if (!g || g->guild.guild_id != p->guild_id)
  11843. return;
  11844. if (!sd->state.gmaster_flag)
  11845. return;
  11846. if (!inter_config.emblem_woe_change && is_agit_start()) {
  11847. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 385), false, SELF); //"You not allowed to change emblem during woe"
  11848. return;
  11849. }
  11850. guild_change_emblem_version( *sd, p->version );
  11851. #endif
  11852. }
  11853. /// Guild notice update request (CZ_GUILD_NOTICE).
  11854. /// 016e <guild id>.L <msg1>.60B <msg2>.120B
  11855. void clif_parse_GuildChangeNotice(int fd, map_session_data* sd){
  11856. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11857. int guild_id = RFIFOL(fd,info->pos[0]);
  11858. char* msg1 = RFIFOCP(fd,info->pos[1]);
  11859. char* msg2 = RFIFOCP(fd,info->pos[2]);
  11860. if(!sd->state.gmaster_flag)
  11861. return;
  11862. // compensate for some client defects when using multilanguage mode
  11863. if (msg1[0] == '|' && msg1[3] == '|') msg1+= 3; // skip duplicate marker
  11864. if (msg2[0] == '|' && msg2[3] == '|') msg2+= 3; // skip duplicate marker
  11865. if (msg2[0] == '|') msg2[strnlen(msg2, MAX_GUILDMES2)-1] = '\0'; // delete extra space at the end of string
  11866. guild_change_notice(sd, guild_id, msg1, msg2);
  11867. }
  11868. /// Guild invite request.
  11869. /// 0168 <account id>.L <inviter account id>.L <inviter char id>.L (CZ_REQ_JOIN_GUILD)
  11870. void clif_parse_GuildInvite( int fd,map_session_data *sd ){
  11871. if( sd == nullptr ){
  11872. return;
  11873. }
  11874. const PACKET_CZ_REQ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11875. guild_invite( *sd, map_id2sd( p->AID ) );
  11876. }
  11877. /// Guild invite request (/guildinvite)
  11878. /// 0916 <char name>.24B (CZ_REQ_JOIN_GUILD2)
  11879. void clif_parse_GuildInvite2( int fd, map_session_data *sd ){
  11880. #if PACKETVER >= 20120410
  11881. if( sd == nullptr ){
  11882. return;
  11883. }
  11884. const PACKET_CZ_REQ_JOIN_GUILD2* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD2*>( RFIFOP( fd, 0 ) );
  11885. char nick[NAME_LENGTH] = {0};
  11886. safestrncpy( nick, p->name, NAME_LENGTH );
  11887. guild_invite( *sd, map_nick2sd( nick, false ) );
  11888. #endif
  11889. }
  11890. /// Answer to guild invitation.
  11891. /// 016b <guild id>.L <answer>.L (CZ_JOIN_GUILD)
  11892. /// answer:
  11893. /// 0 = refuse
  11894. /// 1 = accept
  11895. void clif_parse_GuildReplyInvite( int fd, map_session_data *sd ){
  11896. if( sd == nullptr ){
  11897. return;
  11898. }
  11899. const PACKET_CZ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11900. guild_reply_invite( *sd, p->guild_id, p->answer );
  11901. }
  11902. /// Request to leave guild.
  11903. /// 0159 <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_LEAVE_GUILD)
  11904. void clif_parse_GuildLeave(int fd,map_session_data *sd){
  11905. if( sd == nullptr ){
  11906. return;
  11907. }
  11908. const PACKET_CZ_REQ_LEAVE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GUILD*>( RFIFOP( fd, 0 ) );
  11909. guild_leave( *sd, p->guild_id, p->AID, p->CID, p->message );
  11910. }
  11911. /// Request to expel a member of a guild.
  11912. /// 015b <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_BAN_GUILD)
  11913. void clif_parse_GuildExpulsion(int fd,map_session_data *sd){
  11914. if( sd == nullptr ){
  11915. return;
  11916. }
  11917. const PACKET_CZ_REQ_BAN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_BAN_GUILD*>( RFIFOP( fd, 0 ) );
  11918. guild_expulsion( *sd, p->guild_id, p->AID, p->CID, p->message );
  11919. }
  11920. /// Validates and processes guild messages (CZ_GUILD_CHAT).
  11921. /// 017e <packet len>.W <text>.?B (<name> : <message>) 00
  11922. void clif_parse_GuildMessage(int fd, map_session_data* sd){
  11923. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11924. // validate packet and retrieve name and message
  11925. if( !clif_process_message( sd, false, name, message, output ) )
  11926. return;
  11927. if( sd->bg_id )
  11928. bg_send_message(sd, output, strlen(output) );
  11929. else
  11930. guild_send_message(sd, output, strlen(output) );
  11931. }
  11932. /// Guild alliance request (CZ_REQ_ALLY_GUILD).
  11933. /// 0170 <account id>.L <inviter account id>.L <inviter char id>.L
  11934. void clif_parse_GuildRequestAlliance(int fd, map_session_data *sd)
  11935. {
  11936. map_session_data *t_sd;
  11937. if(!sd->state.gmaster_flag)
  11938. return;
  11939. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11940. clif_displaymessage(fd, msg_txt(sd,228));
  11941. return;
  11942. }
  11943. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11944. //inv_aid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11945. //inv_cid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[2]);
  11946. // @noask [LuzZza]
  11947. if(t_sd && t_sd->state.noask) {
  11948. clif_noask_sub( *sd, *t_sd, 396 ); // Autorejected alliance request from %s.
  11949. return;
  11950. }
  11951. guild_reqalliance(sd,t_sd);
  11952. }
  11953. /// Answer to a guild alliance request (CZ_ALLY_GUILD).
  11954. /// 0172 <inviter account id>.L <answer>.L
  11955. /// answer:
  11956. /// 0 = refuse
  11957. /// 1 = accept
  11958. void clif_parse_GuildReplyAlliance(int fd, map_session_data *sd){
  11959. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11960. guild_reply_reqalliance(sd,
  11961. RFIFOL(fd,info->pos[0]),
  11962. RFIFOL(fd,info->pos[1]));
  11963. }
  11964. /// Request to delete a guild alliance or opposition (CZ_REQ_DELETE_RELATED_GUILD).
  11965. /// 0183 <opponent guild id>.L <relation>.L
  11966. /// relation:
  11967. /// 0 = Ally
  11968. /// 1 = Enemy
  11969. void clif_parse_GuildDelAlliance(int fd, map_session_data *sd){
  11970. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11971. if(!sd->state.gmaster_flag)
  11972. return;
  11973. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11974. clif_displaymessage(fd, msg_txt(sd,228));
  11975. return;
  11976. }
  11977. guild_delalliance(sd,
  11978. RFIFOL(fd,info->pos[0]),
  11979. RFIFOL(fd,info->pos[1]));
  11980. }
  11981. /// Request to set a guild as opposition (CZ_REQ_HOSTILE_GUILD).
  11982. /// 0180 <account id>.L
  11983. void clif_parse_GuildOpposition(int fd, map_session_data *sd)
  11984. {
  11985. map_session_data *t_sd;
  11986. if(!sd->state.gmaster_flag)
  11987. return;
  11988. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11989. clif_displaymessage(fd, msg_txt(sd,228));
  11990. return;
  11991. }
  11992. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11993. // @noask [LuzZza]
  11994. if(t_sd && t_sd->state.noask) {
  11995. clif_noask_sub( *sd, *t_sd, 397 ); // Autorejected opposition request from %s.
  11996. return;
  11997. }
  11998. guild_opposition(sd,t_sd);
  11999. }
  12000. /// Request to delete own guild.
  12001. /// 015d <key>.40B (CZ_REQ_DISORGANIZE_GUILD)
  12002. /// key:
  12003. /// now guild name; might have been (intended) email, since the
  12004. /// field name and size is same as the one in CH_DELETE_CHAR.
  12005. void clif_parse_GuildBreak( int fd, map_session_data *sd ){
  12006. if( sd == nullptr ){
  12007. return;
  12008. }
  12009. const PACKET_CZ_REQ_DISORGANIZE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_DISORGANIZE_GUILD*>( RFIFOP( fd, 0 ) );
  12010. guild_break( *sd, p->key );
  12011. }
  12012. /// Pet
  12013. ///
  12014. /// Request to invoke a pet menu action (CZ_COMMAND_PET).
  12015. /// 01a1 <type>.B
  12016. /// type:
  12017. /// 0 = pet information
  12018. /// 1 = feed
  12019. /// 2 = performance
  12020. /// 3 = return to egg
  12021. /// 4 = unequip accessory
  12022. void clif_parse_PetMenu(int fd, map_session_data *sd){
  12023. pet_menu(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12024. }
  12025. /// Attempt to tame a monster (CZ_TRYCAPTURE_MONSTER).
  12026. /// 019f <id>.L
  12027. void clif_parse_CatchPet(int fd, map_session_data *sd){
  12028. pet_catch_process2(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12029. }
  12030. /// Answer to pet incubator egg selection dialog (CZ_SELECT_PETEGG).
  12031. /// 01a7 <index>.W
  12032. void clif_parse_SelectEgg(int fd, map_session_data *sd){
  12033. if (sd->menuskill_id != SA_TAMINGMONSTER || sd->menuskill_val != -1)
  12034. return;
  12035. pet_select_egg(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  12036. clif_menuskill_clear(sd);
  12037. }
  12038. /// Request to display pet's emotion/talk (CZ_PET_ACT).
  12039. /// 01a9 <data>.L
  12040. /// data:
  12041. /// is either emotion (@see enum emotion_type) or a compound value
  12042. /// (((mob id)-100)*100+(act id)*10+(hunger)) that describes an
  12043. /// entry (given in parentheses) in data\pettalktable.xml
  12044. /// act id:
  12045. /// 0 = feeding
  12046. /// 1 = hunting
  12047. /// 2 = danger
  12048. /// 3 = dead
  12049. /// 4 = normal (stand)
  12050. /// 5 = special performance (perfor_s)
  12051. /// 6 = level up (levelup)
  12052. /// 7 = performance 1 (perfor_1)
  12053. /// 8 = performance 2 (perfor_2)
  12054. /// 9 = performance 3 (perfor_3)
  12055. /// 10 = log-in greeting (connect)
  12056. /// hungry value:
  12057. /// 0 = very hungry (hungry)
  12058. /// 1 = hungry (bit_hungry)
  12059. /// 2 = satisfied (noting)
  12060. /// 3 = stuffed (full)
  12061. /// 4 = full (so_full)
  12062. void clif_parse_SendEmotion(int fd, map_session_data *sd)
  12063. {
  12064. if(sd->pd)
  12065. clif_pet_emotion(sd->pd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12066. }
  12067. /// Request to change pet's name (CZ_RENAME_PET).
  12068. /// 01a5 <name>.24B
  12069. void clif_parse_ChangePetName(int fd, map_session_data *sd)
  12070. {
  12071. pet_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12072. }
  12073. /// /kill (CZ_DISCONNECT_CHARACTER).
  12074. /// Request to disconnect a character.
  12075. /// 00cc <account id>.L
  12076. /// NOTE: Also sent when using GM right click menu "(name) force to quit"
  12077. void clif_parse_GMKick(int fd, map_session_data *sd)
  12078. {
  12079. struct block_list *target;
  12080. int tid;
  12081. tid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12082. target = map_id2bl(tid);
  12083. if (!target) {
  12084. clif_GM_kickack(sd, 0);
  12085. return;
  12086. }
  12087. switch (target->type) {
  12088. case BL_PC:
  12089. {
  12090. char command[NAME_LENGTH+6];
  12091. safesnprintf(command,sizeof(command),"%ckick %s", atcommand_symbol, status_get_name(target));
  12092. is_atcommand(fd, sd, command, 1);
  12093. }
  12094. break;
  12095. /**
  12096. * This one does not invoke any atcommand, so we need to check for permissions.
  12097. */
  12098. case BL_MOB:
  12099. {
  12100. char command[100];
  12101. if( !pc_can_use_command(sd, "killmonster", COMMAND_ATCOMMAND)) {
  12102. clif_GM_kickack(sd, 0);
  12103. return;
  12104. }
  12105. safesnprintf(command,sizeof(command),"/kick %s (%d)", status_get_name(target), status_get_class(target));
  12106. log_atcommand(sd, command);
  12107. status_percent_damage(&sd->bl, target, 100, 0, true); // can invalidate 'target'
  12108. }
  12109. break;
  12110. case BL_NPC:
  12111. {
  12112. struct npc_data* nd = (struct npc_data *)target;
  12113. if( pc_can_use_command(sd, "unloadnpc", COMMAND_ATCOMMAND)) {
  12114. npc_unload_duplicates(nd);
  12115. npc_unload(nd,true);
  12116. npc_read_event_script();
  12117. }
  12118. }
  12119. break;
  12120. default:
  12121. clif_GM_kickack(sd, 0);
  12122. }
  12123. }
  12124. /// /killall (CZ_DISCONNECT_ALL_CHARACTER).
  12125. /// Request to disconnect all characters.
  12126. /// 00ce
  12127. void clif_parse_GMKickAll(int fd, map_session_data* sd) {
  12128. char cmd[15];
  12129. safesnprintf(cmd,sizeof(cmd),"%ckickall",atcommand_symbol);
  12130. is_atcommand(fd, sd, cmd, 1);
  12131. }
  12132. /// /remove (CZ_REMOVE_AID).
  12133. /// Request to warp to a character with given login ID.
  12134. /// 01ba <account name>.24B
  12135. /// /shift (CZ_SHIFT).
  12136. /// Request to warp to a character with given name.
  12137. /// 01bb <char name>.24B
  12138. void clif_parse_GMShift(int fd, map_session_data *sd)
  12139. {// FIXME: remove is supposed to receive account name for clients prior 20100803RE
  12140. char *player_name;
  12141. char command[NAME_LENGTH+8];
  12142. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12143. player_name[NAME_LENGTH-1] = '\0';
  12144. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, player_name);
  12145. is_atcommand(fd, sd, command, 1);
  12146. }
  12147. /// /remove (CZ_REMOVE_AID_SSO).
  12148. /// Request to warp to a character with given account ID.
  12149. /// 0843 <account id>.L
  12150. void clif_parse_GMRemove2(int fd, map_session_data* sd)
  12151. {
  12152. uint32 account_id;
  12153. map_session_data* pl_sd;
  12154. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12155. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12156. char command[NAME_LENGTH+8];
  12157. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, pl_sd->status.name);
  12158. is_atcommand(fd, sd, command, 1);
  12159. }
  12160. }
  12161. /// /recall (CZ_RECALL).
  12162. /// Request to summon a player with given login ID to own position.
  12163. /// 01bc <account name>.24B
  12164. /// /summon (CZ_RECALL_GID).
  12165. /// Request to summon a player with given name to own position.
  12166. /// 01bd <char name>.24B
  12167. void clif_parse_GMRecall(int fd, map_session_data *sd)
  12168. {// FIXME: recall is supposed to receive account name for clients prior 20100803RE
  12169. char *player_name;
  12170. char command [NAME_LENGTH+8];
  12171. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12172. player_name[NAME_LENGTH-1] = '\0';
  12173. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, player_name);
  12174. is_atcommand(fd, sd, command, 1);
  12175. }
  12176. /// /recall (CZ_RECALL_SSO).
  12177. /// Request to summon a player with given account ID to own position.
  12178. /// 0842 <account id>.L
  12179. void clif_parse_GMRecall2(int fd, map_session_data* sd)
  12180. {
  12181. uint32 account_id;
  12182. map_session_data* pl_sd;
  12183. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12184. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12185. char command[NAME_LENGTH+8];
  12186. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, pl_sd->status.name);
  12187. is_atcommand(fd, sd, command, 1);
  12188. }
  12189. }
  12190. /// /item /monster (CZ_ITEM_CREATE).
  12191. /// Request to execute GM commands.
  12192. /// Usage:
  12193. /// /item n - summon n monster or acquire n item/s
  12194. /// /item money - grants 2147483647 zenies
  12195. /// /item whereisboss - locate boss mob in current map.(not yet implemented)
  12196. /// /item regenboss_n t - regenerate n boss monster by t millisecond.(not yet implemented)
  12197. /// /item onekillmonster - toggle an ability to kill mobs in one hit.(not yet implemented)
  12198. /// /item bossinfo - display the information of a boss monster in current map.(not yet implemented)
  12199. /// /item cap_n - capture n monster as pet.(not yet implemented)
  12200. /// /item agitinvest - reset current global agit investments.(not yet implemented)
  12201. /// 013f <item/mob name>.24B
  12202. /// 09ce <item/mob name>.100B [Ind/Yommy]
  12203. void clif_parse_GM_Item_Monster(int fd, map_session_data *sd)
  12204. {
  12205. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12206. int mob_id = 0;
  12207. StringBuf command;
  12208. char *str;
  12209. //#if PACKETVER >= 20131218
  12210. // char str[100];
  12211. //#else
  12212. // char str[24];
  12213. //#endif
  12214. str = RFIFOCP(fd,info->pos[0]);
  12215. if (!str || str[0] == '\0')
  12216. return;
  12217. if (strcmpi(str,"SPPOINT") == 0 || strcmpi(str,"JOBLEVEL") == 0) //! TODO /sp sends these values
  12218. return;
  12219. trim(str);
  12220. // Zeny
  12221. if( strcmpi(str, "money") == 0 ) {
  12222. StringBuf_Init(&command);
  12223. StringBuf_Printf(&command, "%czeny %d", atcommand_symbol, INT_MAX);
  12224. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12225. StringBuf_Destroy(&command);
  12226. return;
  12227. }
  12228. std::shared_ptr<item_data> id = item_db.searchname( str );
  12229. // Item
  12230. if( id ){
  12231. StringBuf_Init(&command);
  12232. if( !itemdb_isstackable2( id.get() ) ){ //Nonstackable
  12233. StringBuf_Printf(&command, "%citem2 %u 1 0 0 0 0 0 0 0", atcommand_symbol, id->nameid);
  12234. }else{
  12235. if (id->flag.guid)
  12236. StringBuf_Printf(&command, "%citem %u 1", atcommand_symbol, id->nameid);
  12237. else
  12238. StringBuf_Printf(&command, "%citem %u 20", atcommand_symbol, id->nameid);
  12239. }
  12240. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12241. StringBuf_Destroy(&command);
  12242. return;
  12243. }
  12244. // Monster
  12245. if ((mob_id = mobdb_searchname(str)) == 0)
  12246. mob_id = mobdb_checkid(atoi(str));
  12247. std::shared_ptr<s_mob_db> mob = mob_db.find(mob_id);
  12248. if( mob != nullptr ) {
  12249. StringBuf_Init(&command);
  12250. StringBuf_Printf(&command, "%cmonster %s", atcommand_symbol, mob->sprite.c_str());
  12251. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12252. StringBuf_Destroy(&command);
  12253. return;
  12254. }
  12255. }
  12256. /// /hide (CZ_CHANGE_EFFECTSTATE).
  12257. /// 019d <effect state>.L
  12258. /// effect state:
  12259. /// TODO: Any OPTION_* ?
  12260. void clif_parse_GMHide(int fd, map_session_data *sd) {
  12261. char cmd[6];
  12262. //int eff_st = RFIFOL(packet_db[RFIFOW(fd,0)].pos[0]);
  12263. safesnprintf(cmd,sizeof(cmd),"%chide",atcommand_symbol);
  12264. is_atcommand(fd, sd, cmd, 1);
  12265. }
  12266. /// /resetcooltime
  12267. /// 0a88 (CZ_CMD_RESETCOOLTIME).
  12268. void clif_parse_gm_resetcooltime( int fd, map_session_data* sd ){
  12269. #if PACKETVER_MAIN_NUM >= 20160622 || PACKETVER_RE_NUM >= 20160622 || defined(PACKETVER_ZERO)
  12270. const PACKET_CZ_CMD_RESETCOOLTIME* p = reinterpret_cast<const PACKET_CZ_CMD_RESETCOOLTIME*>( RFIFOP( fd, 0 ) );
  12271. char cmd[CHAT_SIZE_MAX];
  12272. safesnprintf(cmd,sizeof(cmd),"%cresetcooltime",atcommand_symbol);
  12273. is_atcommand(fd, sd, cmd, 1);
  12274. #endif
  12275. }
  12276. /// Request to adjust player's manner points (CZ_REQ_GIVE_MANNER_POINT).
  12277. /// 0149 <account id>.L <type>.B <value>.W
  12278. /// type:
  12279. /// 0 = positive points
  12280. /// 1 = negative points
  12281. /// 2 = self mute (+10 minutes)
  12282. void clif_parse_GMReqNoChat(int fd,map_session_data *sd)
  12283. {
  12284. int id, type, value;
  12285. map_session_data *dstsd;
  12286. char command[NAME_LENGTH+15];
  12287. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12288. id = RFIFOL(fd,info->pos[0]);
  12289. type = RFIFOB(fd,info->pos[1]);
  12290. value = RFIFOW(fd,info->pos[2]);
  12291. if( type == 0 )
  12292. value = -value;
  12293. //If type is 2 and the ids don't match, this is a crafted hacked packet!
  12294. //Disabled because clients keep self-muting when you give players public @ commands... [Skotlex]
  12295. if (type == 2 /* && (pc_get_group_level(sd) > 0 || sd->bl.id != id)*/)
  12296. return;
  12297. dstsd = map_id2sd(id);
  12298. if( dstsd == nullptr )
  12299. return;
  12300. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, value, dstsd->status.name);
  12301. is_atcommand(fd, sd, command, 1);
  12302. }
  12303. /// /rc (CZ_REQ_GIVE_MANNER_BYNAME).
  12304. /// GM adjustment of a player's manner value by -60.
  12305. /// 0212 <char name>.24B
  12306. void clif_parse_GMRc(int fd, map_session_data* sd)
  12307. {
  12308. char command[NAME_LENGTH+15];
  12309. char *name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12310. name[NAME_LENGTH-1] = '\0';
  12311. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, 60, name);
  12312. is_atcommand(fd, sd, command, 1);
  12313. }
  12314. /// Result of request to resolve account name (ZC_ACK_ACCOUNTNAME).
  12315. /// 01e0 <account id>.L <account name>.24B
  12316. void clif_account_name(int fd, uint32 account_id, const char* accname)
  12317. {
  12318. WFIFOHEAD(fd,packet_len(0x1e0));
  12319. WFIFOW(fd,0) = 0x1e0;
  12320. WFIFOL(fd,2) = account_id;
  12321. safestrncpy(WFIFOCP(fd,6), accname, NAME_LENGTH);
  12322. WFIFOSET(fd,packet_len(0x1e0));
  12323. }
  12324. /// GM requesting account name (for right-click gm menu) (CZ_REQ_ACCOUNTNAME).
  12325. /// 01df <account id>.L
  12326. //! TODO: Figure out how does this actually work
  12327. void clif_parse_GMReqAccountName(int fd, map_session_data *sd)
  12328. {
  12329. uint32 account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12330. /*
  12331. char query[30];
  12332. safesnprintf(query,sizeof(query),"%d", account_id);
  12333. intif_request_accinfo(sd->fd, sd->bl.id, pc_get_group_level(sd), query, 1); //will call clif_account_name at return
  12334. */
  12335. clif_account_name(fd, account_id, "");
  12336. }
  12337. /// /changemaptype <x> <y> <type> (CZ_CHANGE_MAPTYPE).
  12338. /// GM single cell type change request.
  12339. /// 0198 <x>.W <y>.W <type>.W
  12340. /// type:
  12341. /// 0 = not walkable
  12342. /// 1 = walkable
  12343. void clif_parse_GMChangeMapType(int fd, map_session_data *sd)
  12344. {
  12345. int x,y,type;
  12346. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12347. if(! pc_has_permission(sd, PC_PERM_USE_CHANGEMAPTYPE) )
  12348. return;
  12349. x = RFIFOW(fd,info->pos[0]);
  12350. y = RFIFOW(fd,info->pos[1]);
  12351. type = RFIFOW(fd,info->pos[2]);
  12352. map_setgatcell(sd->bl.m,x,y,type);
  12353. clif_changemapcell(0,sd->bl.m,x,y,type,ALL_SAMEMAP);
  12354. //FIXME: once players leave the map, the client 'forgets' this information.
  12355. }
  12356. /// /in /ex (CZ_SETTING_WHISPER_PC).
  12357. /// Request to allow/deny whispers from a nick.
  12358. /// 00cf <nick>.24B <type>.B
  12359. /// type:
  12360. /// 0 = (/ex nick) deny speech from nick
  12361. /// 1 = (/in nick) allow speech from nick
  12362. void clif_parse_PMIgnore(int fd, map_session_data* sd)
  12363. {
  12364. char* nick;
  12365. uint8 type;
  12366. int i;
  12367. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12368. nick = RFIFOCP(fd,info->pos[0]);
  12369. nick[NAME_LENGTH-1] = '\0'; // to be sure that the player name has at most 23 characters
  12370. type = RFIFOB(fd,info->pos[1]);
  12371. if( type == 0 ) { // Add name to ignore list (block)
  12372. if (strcmp(wisp_server_name, nick) == 0) {
  12373. clif_wisexin(sd, type, 1); // fail
  12374. return;
  12375. }
  12376. // try to find a free spot, while checking for duplicates at the same time
  12377. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12378. if( i == MAX_IGNORE_LIST ) {// no space for new entry
  12379. clif_wisexin(sd, type, 2); // too many blocks
  12380. return;
  12381. }
  12382. if( sd->ignore[i].name[0] != '\0' ) {// name already exists
  12383. clif_wisexin(sd, type, 0); // Aegis reports success.
  12384. return;
  12385. }
  12386. //Insert in position i
  12387. safestrncpy(sd->ignore[i].name, nick, NAME_LENGTH);
  12388. } else { // Remove name from ignore list (unblock)
  12389. // find entry
  12390. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12391. if( i == MAX_IGNORE_LIST || sd->ignore[i].name[0] == '\0' ) { //Not found
  12392. clif_wisexin(sd, type, 1); // fail
  12393. return;
  12394. }
  12395. // move everything one place down to overwrite removed entry
  12396. memmove(sd->ignore[i].name, sd->ignore[i+1].name, (MAX_IGNORE_LIST-i-1)*sizeof(sd->ignore[0].name));
  12397. // wipe last entry
  12398. memset(sd->ignore[MAX_IGNORE_LIST-1].name, 0, sizeof(sd->ignore[0].name));
  12399. }
  12400. clif_wisexin(sd, type, 0); // success
  12401. }
  12402. /// /inall /exall (CZ_SETTING_WHISPER_STATE).
  12403. /// Request to allow/deny all whispers.
  12404. /// 00d0 <type>.B
  12405. /// type:
  12406. /// 0 = (/exall) deny all speech
  12407. /// 1 = (/inall) allow all speech
  12408. void clif_parse_PMIgnoreAll(int fd, map_session_data *sd)
  12409. {
  12410. int type = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]), flag;
  12411. if( type == 0 ) {// Deny all
  12412. if( sd->state.ignoreAll ) {
  12413. flag = 1; // fail
  12414. } else {
  12415. sd->state.ignoreAll = 1;
  12416. flag = 0; // success
  12417. }
  12418. } else {//Unblock everyone
  12419. if( sd->state.ignoreAll ) {
  12420. sd->state.ignoreAll = 0;
  12421. flag = 0; // success
  12422. } else {
  12423. if (sd->ignore[0].name[0] != '\0')
  12424. { //Wipe the ignore list.
  12425. memset(sd->ignore, 0, sizeof(sd->ignore));
  12426. flag = 0; // success
  12427. } else {
  12428. flag = 1; // fail
  12429. }
  12430. }
  12431. }
  12432. clif_wisall(sd, type, flag);
  12433. }
  12434. /// Whisper ignore list (ZC_WHISPER_LIST).
  12435. /// 00d4 <packet len>.W { <char name>.24B }*
  12436. void clif_PMIgnoreList(map_session_data* sd)
  12437. {
  12438. int i, fd = sd->fd;
  12439. WFIFOHEAD(fd,4+ARRAYLENGTH(sd->ignore)*NAME_LENGTH);
  12440. WFIFOW(fd,0) = 0xd4;
  12441. for( i = 0; i < ARRAYLENGTH(sd->ignore) && sd->ignore[i].name[0]; i++ ) {
  12442. safestrncpy(WFIFOCP(fd,4+i*NAME_LENGTH), sd->ignore[i].name, NAME_LENGTH);
  12443. }
  12444. WFIFOW(fd,2) = 4+i*NAME_LENGTH;
  12445. WFIFOSET(fd,WFIFOW(fd,2));
  12446. }
  12447. /// Whisper ignore list request (CZ_REQ_WHISPER_LIST).
  12448. /// 00d3
  12449. void clif_parse_PMIgnoreList(int fd,map_session_data *sd)
  12450. {
  12451. clif_PMIgnoreList(sd);
  12452. }
  12453. /// Request to invoke the /doridori recovery bonus (CZ_DORIDORI).
  12454. /// 01e7
  12455. void clif_parse_NoviceDoriDori(int fd, map_session_data *sd)
  12456. {
  12457. if (sd->state.doridori) return;
  12458. switch (sd->class_&MAPID_UPPERMASK) {
  12459. case MAPID_SOUL_LINKER:
  12460. case MAPID_STAR_GLADIATOR:
  12461. case MAPID_TAEKWON:
  12462. if (!sd->state.rest)
  12463. break;
  12464. [[fallthrough]];
  12465. case MAPID_SUPER_NOVICE:
  12466. case MAPID_SUPER_BABY:
  12467. case MAPID_SUPER_NOVICE_E:
  12468. case MAPID_SUPER_BABY_E:
  12469. sd->state.doridori=1;
  12470. break;
  12471. }
  12472. }
  12473. /// Request to invoke the effect of super novice's guardian angel prayer (CZ_CHOPOKGI).
  12474. /// 01ed
  12475. /// Note: This packet is caused by 7 lines of any text, followed by
  12476. /// the prayer and an another line of any text. The prayer is
  12477. /// defined by lines 790~793 in data\msgstringtable.txt
  12478. /// "Dear angel, can you hear my voice?"
  12479. /// "I am" (space separated player name) "Super Novice~"
  12480. /// "Help me out~ Please~ T_T"
  12481. void clif_parse_NoviceExplosionSpirits(int fd, map_session_data *sd)
  12482. {
  12483. if( (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE ) {
  12484. t_exp next = pc_nextbaseexp(sd);
  12485. if( next ) {
  12486. int percent = (int)( ( (double)sd->status.base_exp/(double)next )*1000. );
  12487. if( percent && ( percent%100 ) == 0 ) {// 10.0%, 20.0%, ..., 90.0%
  12488. sc_start(&sd->bl,&sd->bl, SC_EXPLOSIONSPIRITS, 100, 17, skill_get_time(MO_EXPLOSIONSPIRITS, 5)); //Lv17-> +50 critical (noted by Poki) [Skotlex]
  12489. clif_skill_nodamage(&sd->bl, sd->bl, MO_EXPLOSIONSPIRITS, 5); // prayer always shows successful Lv5 cast and disregards noskill restrictions
  12490. }
  12491. }
  12492. }
  12493. }
  12494. /// Friends List
  12495. ///
  12496. /// Toggles a single friend online/offline [Skotlex] (ZC_FRIENDS_STATE).
  12497. /// 0206 <account id>.L <char id>.L <state>.B
  12498. /// 0206 <account id>.L <char id>.L <state>.B <name>.24B >= 20180221
  12499. /// state:
  12500. /// 0 = online
  12501. /// 1 = offline
  12502. void clif_friendslist_toggle(map_session_data *sd,uint32 account_id, uint32 char_id, int online)
  12503. {
  12504. int i, fd = sd->fd;
  12505. //Seek friend.
  12506. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id &&
  12507. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12508. if(i == MAX_FRIENDS || sd->status.friends[i].char_id == 0)
  12509. return; //Not found
  12510. WFIFOHEAD(fd,packet_len(0x206));
  12511. WFIFOW(fd, 0) = 0x206;
  12512. WFIFOL(fd, 2) = sd->status.friends[i].account_id;
  12513. WFIFOL(fd, 6) = sd->status.friends[i].char_id;
  12514. WFIFOB(fd,10) = !online; //Yeah, a 1 here means "logged off", go figure...
  12515. #if PACKETVER >= 20180221
  12516. safestrncpy(WFIFOCP(fd, 11), sd->status.friends[i].name, NAME_LENGTH);
  12517. #endif
  12518. WFIFOSET(fd, packet_len(0x206));
  12519. }
  12520. //Subfunction called from clif_foreachclient to toggle friends on/off [Skotlex]
  12521. int clif_friendslist_toggle_sub(map_session_data *sd,va_list ap)
  12522. {
  12523. uint32 account_id, char_id, online;
  12524. account_id = va_arg(ap, int);
  12525. char_id = va_arg(ap, int);
  12526. online = va_arg(ap, int);
  12527. clif_friendslist_toggle(sd, account_id, char_id, online);
  12528. return 0;
  12529. }
  12530. /// Sends the whole friends list (ZC_FRIENDS_LIST).
  12531. /// 0201 <packet len>.W { <account id>.L <char id>.L <name>.24B }*
  12532. /// 0201 <packet len>.W { <account id>.L <char id>.L }* >= 20180221
  12533. void clif_friendslist_send( map_session_data& sd ){
  12534. PACKET_ZC_FRIENDS_LIST* p = reinterpret_cast<PACKET_ZC_FRIENDS_LIST*>( packet_buffer );
  12535. p->packetType = HEADER_ZC_FRIENDS_LIST;
  12536. p->PacketLength = sizeof( *p );
  12537. for( int i = 0, c = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12538. PACKET_ZC_FRIENDS_LIST_sub& f = p->friends[c];
  12539. f.AID = sd.status.friends[i].account_id;
  12540. f.CID = sd.status.friends[i].char_id;
  12541. #if !( PACKETVER_MAIN_NUM >= 20180307 || PACKETVER_RE_NUM >= 20180221 || PACKETVER_ZERO_NUM >= 20180328 ) || PACKETVER >= 20200902
  12542. safestrncpy( f.name, sd.status.friends[i].name, sizeof( f.name ) );
  12543. #endif
  12544. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( f ) );
  12545. c++;
  12546. }
  12547. clif_send( p, p->PacketLength, &sd.bl, SELF );
  12548. // Sending the online players
  12549. for( int i = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12550. if( map_charid2sd( sd.status.friends[i].char_id ) ){
  12551. clif_friendslist_toggle( &sd, sd.status.friends[i].account_id, sd.status.friends[i].char_id, 1 );
  12552. }
  12553. }
  12554. }
  12555. /// Notification about the result of a friend add request (ZC_ADD_FRIENDS_LIST).
  12556. /// 0209 <result>.W <account id>.L <char id>.L <name>.24B
  12557. /// result:
  12558. /// 0 = MsgStringTable[821]="You have become friends with (%s)."
  12559. /// 1 = MsgStringTable[822]="(%s) does not want to be friends with you."
  12560. /// 2 = MsgStringTable[819]="Your Friend List is full."
  12561. /// 3 = MsgStringTable[820]="(%s)'s Friend List is full."
  12562. void clif_friendslist_reqack(map_session_data *sd, map_session_data *f_sd, int type)
  12563. {
  12564. int fd;
  12565. nullpo_retv(sd);
  12566. fd = sd->fd;
  12567. WFIFOHEAD(fd,packet_len(0x209));
  12568. WFIFOW(fd,0) = 0x209;
  12569. WFIFOW(fd,2) = type;
  12570. if (f_sd) {
  12571. WFIFOL(fd,4) = f_sd->status.account_id;
  12572. WFIFOL(fd,8) = f_sd->status.char_id;
  12573. safestrncpy(WFIFOCP(fd, 12), f_sd->status.name,NAME_LENGTH);
  12574. }
  12575. WFIFOSET(fd, packet_len(0x209));
  12576. }
  12577. /// Asks a player for permission to be added as friend (ZC_REQ_ADD_FRIENDS).
  12578. /// 0207 <req account id>.L <req char id>.L <req char name>.24B
  12579. void clif_friendlist_req(map_session_data* sd, uint32 account_id, uint32 char_id, const char* name)
  12580. {
  12581. int fd = sd->fd;
  12582. WFIFOHEAD(fd,packet_len(0x207));
  12583. WFIFOW(fd,0) = 0x207;
  12584. WFIFOL(fd,2) = account_id;
  12585. WFIFOL(fd,6) = char_id;
  12586. safestrncpy(WFIFOCP(fd,10), name, NAME_LENGTH);
  12587. WFIFOSET(fd,packet_len(0x207));
  12588. }
  12589. /// Request to add a player as friend (CZ_ADD_FRIENDS).
  12590. /// 0202 <name>.24B
  12591. void clif_parse_FriendsListAdd(int fd, map_session_data *sd)
  12592. {
  12593. map_session_data *f_sd;
  12594. int i;
  12595. f_sd = map_nick2sd(RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]),false);
  12596. // Friend doesn't exist (no player with this name)
  12597. if (f_sd == nullptr) {
  12598. clif_displaymessage(fd, msg_txt(sd,3));
  12599. return;
  12600. }
  12601. if( sd->bl.id == f_sd->bl.id ) {// adding oneself as friend
  12602. return;
  12603. }
  12604. // ensure that the request player's friend list is not full
  12605. ARR_FIND(0, MAX_FRIENDS, i, sd->status.friends[i].char_id == 0);
  12606. if( i == MAX_FRIENDS ){
  12607. clif_friendslist_reqack(sd, f_sd, 2);
  12608. return;
  12609. }
  12610. // @noask [LuzZza]
  12611. if(f_sd->state.noask) {
  12612. clif_noask_sub( *sd, *f_sd, 398 ); // Autorejected friend request from %s.
  12613. return;
  12614. }
  12615. // Friend already exists
  12616. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id != 0; i++) {
  12617. if (sd->status.friends[i].char_id == f_sd->status.char_id) {
  12618. clif_displaymessage(fd, msg_txt(sd,671)); //"Friend already exists."
  12619. return;
  12620. }
  12621. }
  12622. f_sd->friend_req = sd->status.char_id;
  12623. sd->friend_req = f_sd->status.char_id;
  12624. clif_friendlist_req(f_sd, sd->status.account_id, sd->status.char_id, sd->status.name);
  12625. }
  12626. /// Answer to a friend add request (CZ_ACK_REQ_ADD_FRIENDS).
  12627. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.B
  12628. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.L (PACKETVER >= 6)
  12629. /// reply:
  12630. /// 0 = rejected
  12631. /// 1 = accepted
  12632. void clif_parse_FriendsListReply(int fd, map_session_data *sd)
  12633. {
  12634. map_session_data *f_sd;
  12635. uint32 account_id;
  12636. char reply;
  12637. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12638. account_id = RFIFOL(fd,info->pos[0]);
  12639. //char_id = RFIFOL(fd,info->pos[1]);
  12640. #if PACKETVER < 6
  12641. reply = RFIFOB(fd,info->pos[2]);
  12642. #else
  12643. reply = RFIFOL(fd,info->pos[2]);
  12644. #endif
  12645. if( sd->bl.id == account_id ) {// adding oneself as friend
  12646. return;
  12647. }
  12648. f_sd = map_id2sd(account_id); //The account id is the same as the bl.id of players.
  12649. if (f_sd == nullptr)
  12650. return;
  12651. if (reply == 0 || !( sd->friend_req == f_sd->status.char_id && f_sd->friend_req == sd->status.char_id ) )
  12652. clif_friendslist_reqack(f_sd, sd, 1);
  12653. else {
  12654. int i;
  12655. // Find an empty slot
  12656. for (i = 0; i < MAX_FRIENDS; i++)
  12657. if (f_sd->status.friends[i].char_id == 0)
  12658. break;
  12659. if (i == MAX_FRIENDS) {
  12660. clif_friendslist_reqack(f_sd, sd, 2);
  12661. return;
  12662. }
  12663. f_sd->status.friends[i].account_id = sd->status.account_id;
  12664. f_sd->status.friends[i].char_id = sd->status.char_id;
  12665. safestrncpy(f_sd->status.friends[i].name, sd->status.name, NAME_LENGTH);
  12666. clif_friendslist_reqack(f_sd, sd, 0);
  12667. achievement_update_objective(f_sd, AG_ADD_FRIEND, 1, i + 1);
  12668. if (battle_config.friend_auto_add) {
  12669. // Also add f_sd to sd's friendlist.
  12670. for (i = 0; i < MAX_FRIENDS; i++) {
  12671. if (sd->status.friends[i].char_id == f_sd->status.char_id)
  12672. return; //No need to add anything.
  12673. if (sd->status.friends[i].char_id == 0)
  12674. break;
  12675. }
  12676. if (i == MAX_FRIENDS) {
  12677. clif_friendslist_reqack(sd, f_sd, 2);
  12678. return;
  12679. }
  12680. sd->status.friends[i].account_id = f_sd->status.account_id;
  12681. sd->status.friends[i].char_id = f_sd->status.char_id;
  12682. safestrncpy(sd->status.friends[i].name, f_sd->status.name, NAME_LENGTH);
  12683. clif_friendslist_reqack(sd, f_sd, 0);
  12684. achievement_update_objective(sd, AG_ADD_FRIEND, 1, i + 1);
  12685. }
  12686. }
  12687. }
  12688. /// Request to delete a friend (CZ_DELETE_FRIENDS).
  12689. /// 0203 <account id>.L <char id>.L
  12690. void clif_parse_FriendsListRemove(int fd, map_session_data *sd)
  12691. {
  12692. map_session_data *f_sd = nullptr;
  12693. uint32 account_id, char_id;
  12694. int i, j;
  12695. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12696. account_id = RFIFOL(fd,info->pos[0]);
  12697. char_id = RFIFOL(fd,info->pos[1]);
  12698. // Search friend
  12699. for (i = 0; i < MAX_FRIENDS &&
  12700. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12701. if (i == MAX_FRIENDS) {
  12702. clif_displaymessage(fd, msg_txt(sd,672)); //"Name not found in list."
  12703. return;
  12704. }
  12705. //remove from friend's list first
  12706. if( (f_sd = map_id2sd(account_id)) && f_sd->status.char_id == char_id) {
  12707. for (i = 0; i < MAX_FRIENDS &&
  12708. (f_sd->status.friends[i].char_id != sd->status.char_id || f_sd->status.friends[i].account_id != sd->status.account_id); i++);
  12709. if (i != MAX_FRIENDS) {
  12710. // move all chars up
  12711. for(j = i + 1; j < MAX_FRIENDS; j++)
  12712. memcpy(&f_sd->status.friends[j-1], &f_sd->status.friends[j], sizeof(f_sd->status.friends[0]));
  12713. memset(&f_sd->status.friends[MAX_FRIENDS-1], 0, sizeof(f_sd->status.friends[MAX_FRIENDS-1]));
  12714. //should the guy be notified of some message? we should add it here if so
  12715. WFIFOHEAD(f_sd->fd,packet_len(0x20a));
  12716. WFIFOW(f_sd->fd,0) = 0x20a;
  12717. WFIFOL(f_sd->fd,2) = sd->status.account_id;
  12718. WFIFOL(f_sd->fd,6) = sd->status.char_id;
  12719. WFIFOSET(f_sd->fd, packet_len(0x20a));
  12720. }
  12721. } else { //friend not online -- ask char server to delete from his friendlist
  12722. if(chrif_removefriend(char_id,sd->status.char_id)) { // char-server offline, abort
  12723. clif_displaymessage(fd, msg_txt(sd,673)); //"This action can't be performed at the moment. Please try again later."
  12724. return;
  12725. }
  12726. }
  12727. // We can now delete from original requester
  12728. for (i = 0; i < MAX_FRIENDS &&
  12729. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12730. // move all chars up
  12731. for(j = i + 1; j < MAX_FRIENDS; j++)
  12732. memcpy(&sd->status.friends[j-1], &sd->status.friends[j], sizeof(sd->status.friends[0]));
  12733. memset(&sd->status.friends[MAX_FRIENDS-1], 0, sizeof(sd->status.friends[MAX_FRIENDS-1]));
  12734. clif_displaymessage(fd, msg_txt(sd,674)); //"Friend removed"
  12735. WFIFOHEAD(fd,packet_len(0x20a));
  12736. WFIFOW(fd,0) = 0x20a;
  12737. WFIFOL(fd,2) = account_id;
  12738. WFIFOL(fd,6) = char_id;
  12739. WFIFOSET(fd, packet_len(0x20a));
  12740. }
  12741. /// /pvpinfo list (ZC_ACK_PVPPOINT).
  12742. /// 0210 <char id>.L <account id>.L <win point>.L <lose point>.L <point>.L
  12743. void clif_PVPInfo(map_session_data* sd)
  12744. {
  12745. int fd = sd->fd;
  12746. WFIFOHEAD(fd,packet_len(0x210));
  12747. WFIFOW(fd,0) = 0x210;
  12748. WFIFOL(fd,2) = sd->status.char_id;
  12749. WFIFOL(fd,6) = sd->status.account_id;
  12750. WFIFOL(fd,10) = sd->pvp_won; // times won
  12751. WFIFOL(fd,14) = sd->pvp_lost; // times lost
  12752. WFIFOL(fd,18) = sd->pvp_point;
  12753. WFIFOSET(fd, packet_len(0x210));
  12754. }
  12755. /// /pvpinfo (CZ_REQ_PVPPOINT).
  12756. /// 020f <char id>.L <account id>.L
  12757. void clif_parse_PVPInfo(int fd,map_session_data *sd)
  12758. {
  12759. // TODO: Is there a way to use this on an another player (char/acc id)?
  12760. //int cid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12761. //int aid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  12762. clif_PVPInfo(sd);
  12763. }
  12764. /// SG Feel save OK [Komurka] (CZ_AGREE_STARPLACE).
  12765. /// 0254 <which>.B
  12766. /// which: (unused atm for security purpose)
  12767. /// 0 = sun
  12768. /// 1 = moon
  12769. /// 2 = star
  12770. void clif_parse_FeelSaveOk(int fd,map_session_data *sd)
  12771. {
  12772. int i;
  12773. //int wich = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12774. if (sd->menuskill_id != SG_FEEL)
  12775. return;
  12776. i = sd->menuskill_val-1;
  12777. if (i<0 || i >= MAX_PC_FEELHATE) return; //Bug?
  12778. sd->feel_map[i].index = map_id2index(sd->bl.m);
  12779. sd->feel_map[i].m = sd->bl.m;
  12780. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), sd->feel_map[i].index);
  12781. //Are these really needed? Shouldn't they show up automatically from the feel save packet?
  12782. // clif_misceffect2(&sd->bl, 0x1b0);
  12783. // clif_misceffect2(&sd->bl, 0x21f);
  12784. clif_feel_info(sd, i, 0);
  12785. clif_menuskill_clear(sd);
  12786. }
  12787. /// Star Gladiator's Feeling map confirmation prompt (ZC_STARPLACE).
  12788. /// 0253 <which>.B
  12789. /// which:
  12790. /// 0 = sun
  12791. /// 1 = moon
  12792. /// 2 = star
  12793. void clif_feel_req(int fd, map_session_data *sd, uint16 skill_lv)
  12794. {
  12795. WFIFOHEAD(fd,packet_len(0x253));
  12796. WFIFOW(fd,0)=0x253;
  12797. WFIFOB(fd,2)=TOB(skill_lv-1);
  12798. WFIFOSET(fd, packet_len(0x253));
  12799. sd->menuskill_id = SG_FEEL;
  12800. sd->menuskill_val = skill_lv;
  12801. }
  12802. /// Request to change homunculus' name (CZ_RENAME_MER).
  12803. /// 0231 <name>.24B
  12804. void clif_parse_ChangeHomunculusName(int fd, map_session_data *sd){
  12805. hom_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12806. }
  12807. /// Request to warp/move homunculus/mercenary to it's owner (CZ_REQUEST_MOVETOOWNER).
  12808. /// 0234 <id>.L
  12809. void clif_parse_HomMoveToMaster(int fd, map_session_data *sd){
  12810. int id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]); // Mercenary or Homunculus
  12811. struct block_list *bl = nullptr;
  12812. struct unit_data *ud = nullptr;
  12813. if( sd->md && sd->md->bl.id == id )
  12814. bl = &sd->md->bl;
  12815. else if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12816. bl = &sd->hd->bl; // Moving Homunculus
  12817. else
  12818. return;
  12819. unit_calc_pos(bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  12820. ud = unit_bl2ud(bl);
  12821. unit_walktoxy(bl, ud->to_x, ud->to_y, 4);
  12822. }
  12823. /// Request to move homunculus/mercenary.
  12824. /// 0232 <id>.L <position data>.3B (CZ_REQUEST_MOVENPC)
  12825. void clif_parse_HomMoveTo(int fd, map_session_data *sd){
  12826. #if PACKETVER >= 20050425
  12827. const PACKET_CZ_REQUEST_MOVENPC* p = reinterpret_cast<const PACKET_CZ_REQUEST_MOVENPC*>( RFIFOP( fd, 0 ) );
  12828. struct block_list *bl = nullptr;
  12829. short x, y;
  12830. RBUFPOS( p->PosDir, 0, &x, &y, nullptr );
  12831. if( sd->md && sd->md->bl.id == p->GID )
  12832. bl = &sd->md->bl; // Moving Mercenary
  12833. else if( hom_is_active(sd->hd) && sd->hd->bl.id == p->GID )
  12834. bl = &sd->hd->bl; // Moving Homunculus
  12835. else
  12836. return;
  12837. if (x < 0 || y < 0 || (x == bl->x && y == bl->y))
  12838. return;
  12839. unit_walktoxy(bl, x, y, 4);
  12840. #endif
  12841. }
  12842. /// Request to do an action with homunculus/mercenary (CZ_REQUEST_ACTNPC).
  12843. /// 0233 <id>.L <target id>.L <action>.B
  12844. /// action:
  12845. /// always 0
  12846. void clif_parse_HomAttack(int fd,map_session_data *sd)
  12847. {
  12848. struct block_list *bl = nullptr;
  12849. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12850. int id = RFIFOL(fd,info->pos[0]);
  12851. int target_id = RFIFOL(fd,info->pos[1]);
  12852. int action_type = RFIFOB(fd,info->pos[2]);
  12853. if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12854. bl = &sd->hd->bl;
  12855. else if( sd->md && sd->md->bl.id == id )
  12856. bl = &sd->md->bl;
  12857. else return;
  12858. unit_attack(bl, target_id, action_type != 0);
  12859. }
  12860. /// Request to invoke a homunculus menu action (CZ_COMMAND_MER).
  12861. /// 022d <type>.W <command>.B
  12862. /// type:
  12863. /// always 0
  12864. /// command:
  12865. /// 0 = homunculus information
  12866. /// 1 = feed
  12867. /// 2 = delete
  12868. void clif_parse_HomMenu(int fd, map_session_data *sd)
  12869. { //[orn]
  12870. int cmd = RFIFOW(fd,0);
  12871. //int type = RFIFOW(fd,packet_db[cmd].pos[0]);
  12872. if(!hom_is_active(sd->hd))
  12873. return;
  12874. hom_menu(sd,RFIFOB(fd,packet_db[cmd].pos[1]));
  12875. }
  12876. /// Request to resurrect oneself using Token of Siegfried (CZ_STANDING_RESURRECTION).
  12877. /// 0292
  12878. void clif_parse_AutoRevive(int fd, map_session_data *sd)
  12879. {
  12880. pc_revive_item(sd);
  12881. }
  12882. /// Information about character's status values (ZC_ACK_STATUS_GM).
  12883. /// 0214 <str>.B <standardStr>.B <agi>.B <standardAgi>.B <vit>.B <standardVit>.B
  12884. /// <int>.B <standardInt>.B <dex>.B <standardDex>.B <luk>.B <standardLuk>.B
  12885. /// <attPower>.W <refiningPower>.W <max_mattPower>.W <min_mattPower>.W
  12886. /// <itemdefPower>.W <plusdefPower>.W <mdefPower>.W <plusmdefPower>.W
  12887. /// <hitSuccessValue>.W <avoidSuccessValue>.W <plusAvoidSuccessValue>.W
  12888. /// <criticalSuccessValue>.W <ASPD>.W <plusASPD>.W
  12889. void clif_check(int fd, map_session_data* pl_sd)
  12890. {
  12891. WFIFOHEAD(fd,packet_len(0x214));
  12892. WFIFOW(fd, 0) = 0x214;
  12893. WFIFOB(fd, 2) = min(pl_sd->status.str, UINT8_MAX);
  12894. WFIFOB(fd, 3) = pc_need_status_point(pl_sd, SP_STR, 1);
  12895. WFIFOB(fd, 4) = min(pl_sd->status.agi, UINT8_MAX);
  12896. WFIFOB(fd, 5) = pc_need_status_point(pl_sd, SP_AGI, 1);
  12897. WFIFOB(fd, 6) = min(pl_sd->status.vit, UINT8_MAX);
  12898. WFIFOB(fd, 7) = pc_need_status_point(pl_sd, SP_VIT, 1);
  12899. WFIFOB(fd, 8) = min(pl_sd->status.int_, UINT8_MAX);
  12900. WFIFOB(fd, 9) = pc_need_status_point(pl_sd, SP_INT, 1);
  12901. WFIFOB(fd,10) = min(pl_sd->status.dex, UINT8_MAX);
  12902. WFIFOB(fd,11) = pc_need_status_point(pl_sd, SP_DEX, 1);
  12903. WFIFOB(fd,12) = min(pl_sd->status.luk, UINT8_MAX);
  12904. WFIFOB(fd,13) = pc_need_status_point(pl_sd, SP_LUK, 1);
  12905. WFIFOW(fd,14) = pl_sd->battle_status.batk+pl_sd->battle_status.rhw.atk+pl_sd->battle_status.lhw.atk;
  12906. WFIFOW(fd,16) = pl_sd->battle_status.rhw.atk2+pl_sd->battle_status.lhw.atk2;
  12907. WFIFOW(fd,18) = pl_sd->battle_status.matk_max;
  12908. WFIFOW(fd,20) = pl_sd->battle_status.matk_min;
  12909. WFIFOW(fd,22) = pl_sd->battle_status.def;
  12910. WFIFOW(fd,24) = pl_sd->battle_status.def2;
  12911. WFIFOW(fd,26) = pl_sd->battle_status.mdef;
  12912. WFIFOW(fd,28) = pl_sd->battle_status.mdef2;
  12913. WFIFOW(fd,30) = pl_sd->battle_status.hit;
  12914. WFIFOW(fd,32) = pl_sd->battle_status.flee;
  12915. WFIFOW(fd,34) = pl_sd->battle_status.flee2/10;
  12916. WFIFOW(fd,36) = pl_sd->battle_status.cri/10;
  12917. WFIFOW(fd,38) = (2000-pl_sd->battle_status.amotion)/10; // aspd
  12918. WFIFOW(fd,40) = 0; // FIXME: What is 'plusASPD' supposed to be? Maybe adelay?
  12919. WFIFOSET(fd,packet_len(0x214));
  12920. }
  12921. /// /check (CZ_REQ_STATUS_GM).
  12922. /// Request character's status values.
  12923. /// 0213 <char name>.24B
  12924. void clif_parse_Check(int fd, map_session_data *sd)
  12925. {
  12926. char charname[NAME_LENGTH];
  12927. map_session_data* pl_sd;
  12928. if(!pc_has_permission(sd, PC_PERM_USE_CHECK))
  12929. return;
  12930. safestrncpy(charname, RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]), sizeof(charname));
  12931. if( ( pl_sd = map_nick2sd(charname,false) ) == nullptr || pc_get_group_level(sd) < pc_get_group_level(pl_sd) )
  12932. {
  12933. return;
  12934. }
  12935. clif_check(fd, pl_sd);
  12936. }
  12937. /// MAIL SYSTEM
  12938. /// By Zephyrus
  12939. ///
  12940. /// Notification about the result of adding an item to mail
  12941. /// 0255 <index>.W <result>.B (ZC_ACK_MAIL_ADD_ITEM)
  12942. /// result:
  12943. /// 0 = success
  12944. /// 1 = failure
  12945. /// 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)
  12946. /// { <card id>.W }*4 { <option index>.W <option value>.W <option parameter>.B }*5
  12947. /// result:
  12948. /// 0 = success
  12949. /// 1 = over weight
  12950. /// 2 = fatal error
  12951. void clif_Mail_setattachment( map_session_data* sd, int index, int amount, uint8 flag ){
  12952. int fd = sd->fd;
  12953. #if PACKETVER < 20150513
  12954. WFIFOHEAD(fd,packet_len(0x255));
  12955. WFIFOW(fd,0) = 0x255;
  12956. WFIFOW(fd,2) = index;
  12957. WFIFOB(fd,4) = flag;
  12958. WFIFOSET(fd,packet_len(0x255));
  12959. #else
  12960. PACKET_ZC_ACK_ADD_ITEM_RODEX p = {};
  12961. if( flag ){
  12962. memset( &p, 0, sizeof( p ) );
  12963. }else{
  12964. struct item* item = &sd->inventory.u.items_inventory[server_index(index)];
  12965. p.index = index;
  12966. p.count = amount;
  12967. p.itemId = client_nameid( item->nameid );
  12968. p.type = itemtype( item->nameid );
  12969. p.IsIdentified = item->identify ? 1 : 0;
  12970. p.IsDamaged = item->attribute ? 1 : 0;
  12971. p.refiningLevel = item->refine;
  12972. clif_addcards( &p.slot, item );
  12973. clif_add_random_options( p.optionData, *item );
  12974. p.weight = 0;
  12975. for( int i = 0; i < MAIL_MAX_ITEM; i++ ){
  12976. if( sd->mail.item[i].nameid == 0 ){
  12977. continue;
  12978. }
  12979. p.weight += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  12980. }
  12981. p.favorite = ( item->favorite != 0 ) ? 1 : 0;
  12982. p.location = pc_equippoint( sd, server_index( index ) );
  12983. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  12984. p.grade = item->enchantgrade;
  12985. #endif
  12986. }
  12987. p.PacketType = HEADER_ZC_ACK_ADD_ITEM_RODEX;
  12988. p.result = flag;
  12989. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  12990. #endif
  12991. }
  12992. /// Notification about the result of retrieving a mail attachment
  12993. /// 0245 <result>.B (ZC_MAIL_REQ_GET_ITEM)
  12994. /// result:
  12995. /// 0 = success
  12996. /// 1 = failure
  12997. /// 2 = too many items
  12998. /// 09f2 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ZENY_FROM_MAIL)
  12999. /// 09f4 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ITEM_FROM_MAIL)
  13000. void clif_mail_getattachment(map_session_data* sd, struct mail_message *msg, uint8 result, enum mail_attachment_type type) {
  13001. #if PACKETVER < 20150513
  13002. int fd = sd->fd;
  13003. WFIFOHEAD(fd,packet_len(0x245));
  13004. WFIFOW(fd,0) = 0x245;
  13005. WFIFOB(fd,2) = result;
  13006. WFIFOSET(fd,packet_len(0x245));
  13007. #else
  13008. switch( type ){
  13009. case MAIL_ATT_ITEM:
  13010. case MAIL_ATT_ZENY:
  13011. break;
  13012. default:
  13013. return;
  13014. }
  13015. PACKET_ZC_ACK_ITEM_FROM_MAIL p = { 0 };
  13016. p.PacketType = type == MAIL_ATT_ZENY ? rodexgetzeny : rodexgetitem;
  13017. p.MailID = msg->id;
  13018. p.opentype = msg->type;
  13019. p.result = result;
  13020. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13021. clif_Mail_refreshinbox( sd, msg->type, 0 );
  13022. #endif
  13023. }
  13024. /// Notification about the result of sending a mail
  13025. /// 0249 <result>.B (ZC_MAIL_REQ_SEND)
  13026. /// result:
  13027. /// 0 = success
  13028. /// 1 = recipinent does not exist
  13029. /// 09ed <result>.B (ZC_ACK_WRITE_MAIL)
  13030. void clif_Mail_send(map_session_data* sd, enum mail_send_result result){
  13031. #if PACKETVER < 20150513
  13032. int fd = sd->fd;
  13033. WFIFOHEAD(fd,packet_len(0x249));
  13034. WFIFOW(fd,0) = 0x249;
  13035. WFIFOB(fd,2) = result != WRITE_MAIL_SUCCESS;
  13036. WFIFOSET(fd,packet_len(0x249));
  13037. #else
  13038. PACKET_ZC_WRITE_MAIL_RESULT p = { 0 };
  13039. p.PacketType = rodexwriteresult;
  13040. p.result = result;
  13041. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13042. #endif
  13043. }
  13044. /// Notification about the result of deleting a mail.
  13045. /// 0257 <mail id>.L <result>.W (ZC_ACK_MAIL_DELETE)
  13046. /// result:
  13047. /// 0 = success
  13048. /// 1 = failure
  13049. // 09f6 <mail tab>.B <mail id>.Q (ZC_ACK_DELETE_MAIL)
  13050. void clif_mail_delete( map_session_data* sd, struct mail_message *msg, bool success ){
  13051. #if PACKETVER < 20150513
  13052. int fd = sd->fd;
  13053. WFIFOHEAD(fd, packet_len(0x257));
  13054. WFIFOW(fd,0) = 0x257;
  13055. WFIFOL(fd,2) = msg->id;
  13056. WFIFOW(fd,6) = !success;
  13057. WFIFOSET(fd, packet_len(0x257));
  13058. #else
  13059. // This is only a success notification
  13060. if( success ){
  13061. PACKET_ZC_ACK_DELETE_MAIL p = { 0 };
  13062. p.PacketType = rodexdelete;
  13063. p.opentype = msg->type;
  13064. p.MailID = msg->id;
  13065. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13066. }
  13067. #endif
  13068. }
  13069. /// Notification about the result of returning a mail (ZC_ACK_MAIL_RETURN).
  13070. /// 0274 <mail id>.L <result>.W
  13071. /// result:
  13072. /// 0 = success
  13073. /// 1 = failure
  13074. void clif_Mail_return(int fd, int mail_id, short fail)
  13075. {
  13076. WFIFOHEAD(fd,packet_len(0x274));
  13077. WFIFOW(fd,0) = 0x274;
  13078. WFIFOL(fd,2) = mail_id;
  13079. WFIFOW(fd,6) = fail;
  13080. WFIFOSET(fd,packet_len(0x274));
  13081. }
  13082. /// Notification about new mail.
  13083. /// 024a <mail id>.L <title>.40B <sender>.24B (ZC_MAIL_RECEIVE)
  13084. /// 09e7 <result>.B (ZC_NOTIFY_UNREADMAIL)
  13085. void clif_Mail_new(map_session_data* sd, int mail_id, const char *sender, const char *title){
  13086. #if PACKETVER < 20150513
  13087. int fd = sd->fd;
  13088. WFIFOHEAD(fd,packet_len(0x24a));
  13089. WFIFOW(fd,0) = 0x24a;
  13090. WFIFOL(fd,2) = mail_id;
  13091. safestrncpy(WFIFOCP(fd,6), title, MAIL_TITLE_LENGTH);
  13092. safestrncpy(WFIFOCP(fd,46), sender, NAME_LENGTH);
  13093. WFIFOSET(fd,packet_len(0x24a));
  13094. #else
  13095. PACKET_ZC_NOTIFY_UNREADMAIL p = { 0 };
  13096. p.PacketType = rodexicon;
  13097. p.result = sd->mail.inbox.unread > 0 || sd->mail.inbox.unchecked > 0; // unread
  13098. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13099. #endif
  13100. }
  13101. /// Opens/closes the mail window (ZC_MAIL_WINDOWS).
  13102. /// 0260 <type>.L
  13103. /// type:
  13104. /// 0 = open
  13105. /// 1 = close
  13106. void clif_Mail_window(int fd, int flag)
  13107. {
  13108. WFIFOHEAD(fd,packet_len(0x260));
  13109. WFIFOW(fd,0) = 0x260;
  13110. WFIFOL(fd,2) = flag;
  13111. WFIFOSET(fd,packet_len(0x260));
  13112. }
  13113. /// Lists mails stored in inbox.
  13114. /// 0240 <packet len>.W <amount>.L { <mail id>.L <title>.40B <read>.B <sender>.24B <time>.L }*amount (ZC_MAIL_REQ_GET_LIST)
  13115. /// read:
  13116. /// 0 = unread
  13117. /// 1 = read
  13118. /// 09f0 <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST)
  13119. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13120. /// 0a7d <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST2)
  13121. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13122. /// 0ac2 <packet len>.W <unknown>.B (ZC_ACK_MAIL_LIST3)
  13123. /// { <type>.B <mail id>.Q <read>.B <type>.B <sender>.24B <expires>.L <title length>.W <title>.?B }*
  13124. void clif_Mail_refreshinbox(map_session_data *sd,enum mail_inbox_type type,int64 mailID){
  13125. #if PACKETVER < 20150513
  13126. int fd = sd->fd;
  13127. struct mail_data *md = &sd->mail.inbox;
  13128. struct mail_message *msg;
  13129. int len, i, j;
  13130. len = 8 + (73 * md->amount);
  13131. WFIFOHEAD(fd,len);
  13132. WFIFOW(fd,0) = 0x240;
  13133. WFIFOW(fd,2) = len;
  13134. WFIFOL(fd,4) = md->amount;
  13135. for( i = j = 0; i < MAIL_MAX_INBOX && j < md->amount; i++ )
  13136. {
  13137. msg = &md->msg[i];
  13138. if (msg->id < 1)
  13139. continue;
  13140. WFIFOL(fd,8+73*j) = msg->id;
  13141. memcpy(WFIFOP(fd,12+73*j), msg->title, MAIL_TITLE_LENGTH);
  13142. WFIFOB(fd,52+73*j) = (msg->status != MAIL_UNREAD);
  13143. safestrncpy(WFIFOCP(fd,53+73*j), msg->send_name, NAME_LENGTH);
  13144. WFIFOL(fd,77+73*j) = (uint32)msg->timestamp;
  13145. j++;
  13146. }
  13147. WFIFOSET(fd,len);
  13148. if( md->full ) {// TODO: is this official?
  13149. char output[100];
  13150. safesnprintf(output,sizeof(output),"Inbox is full (Max %d). Delete some mails.", MAIL_MAX_INBOX);
  13151. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  13152. }
  13153. #else
  13154. int fd = sd->fd;
  13155. struct mail_data *md = &sd->mail.inbox;
  13156. struct mail_message *msg;
  13157. int i, j, k, offset, titleLength;
  13158. uint8 mailType, amount, remaining;
  13159. uint32 now = (uint32)time(nullptr);
  13160. #if PACKETVER >= 20170419
  13161. int cmd = 0xac2;
  13162. #elif PACKETVER >= 20160601
  13163. int cmd = 0xa7d;
  13164. #else
  13165. int cmd = 0x9f0;
  13166. #endif
  13167. if( battle_config.mail_daily_count ){
  13168. mail_refresh_remaining_amount(sd);
  13169. }
  13170. #if PACKETVER >= 20170419
  13171. // Always send all
  13172. i = md->amount;
  13173. #else
  13174. // If a starting mail id was sent
  13175. if( mailID != 0 ){
  13176. ARR_FIND( 0, md->amount, i, md->msg[i].id == mailID );
  13177. // Unknown mail
  13178. if( i == md->amount ){
  13179. // Ignore the request for now
  13180. return; // TODO: Should we just show the first page instead?
  13181. }
  13182. // It was actually the oldest/first mail, there is no further page
  13183. if( i == 0 ){
  13184. return;
  13185. }
  13186. // We actually want the next/older mail
  13187. i -= 1;
  13188. }else{
  13189. i = md->amount;
  13190. }
  13191. #endif
  13192. // Count the remaining mails from the starting mail or the beginning
  13193. // Only count mails of the target type(before 2017-04-19) and those that should not have been deleted already
  13194. for( j = i, remaining = 0; j >= 0; j-- ){
  13195. msg = &md->msg[j];
  13196. if (msg->id < 1)
  13197. continue;
  13198. #if PACKETVER < 20170419
  13199. if (msg->type != type)
  13200. continue;
  13201. #endif
  13202. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13203. continue;
  13204. remaining++;
  13205. }
  13206. #if PACKETVER >= 20170419
  13207. // Always send all
  13208. amount = remaining;
  13209. #else
  13210. if( remaining > MAIL_PAGE_SIZE ){
  13211. amount = MAIL_PAGE_SIZE;
  13212. }else{
  13213. amount = remaining;
  13214. }
  13215. #endif
  13216. WFIFOHEAD(fd, 7 + ((44 + MAIL_TITLE_LENGTH) * amount));
  13217. WFIFOW(fd, 0) = cmd;
  13218. #if PACKETVER >= 20170419
  13219. WFIFOB(fd, 4) = 1; // Unknown
  13220. offset = 5;
  13221. #else
  13222. WFIFOB(fd, 4) = type;
  13223. WFIFOB(fd, 5) = amount;
  13224. WFIFOB(fd, 6) = ( remaining < MAIL_PAGE_SIZE ); // last page
  13225. offset = 7;
  13226. #endif
  13227. for( amount = 0; i >= 0; i-- ){
  13228. msg = &md->msg[i];
  13229. if (msg->id < 1)
  13230. continue;
  13231. #if PACKETVER < 20170419
  13232. if (msg->type != type)
  13233. continue;
  13234. #endif
  13235. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13236. continue;
  13237. #if PACKETVER >= 20170419
  13238. WFIFOB(fd, offset) = msg->type;
  13239. offset += 1;
  13240. #endif
  13241. WFIFOQ(fd, offset + 0) = (uint64)msg->id;
  13242. WFIFOB(fd, offset + 8) = (msg->status != MAIL_UNREAD);
  13243. mailType = MAIL_TYPE_TEXT; // Normal letter
  13244. if( msg->zeny > 0 ){
  13245. mailType |= MAIL_TYPE_ZENY; // Mail contains zeny
  13246. }
  13247. for( k = 0; k < MAIL_MAX_ITEM; k++ ){
  13248. if( msg->item[k].nameid > 0 ){
  13249. mailType |= MAIL_TYPE_ITEM; // Mail contains items
  13250. break;
  13251. }
  13252. }
  13253. #if PACKETVER >= 20170419
  13254. // If it came from an npc?
  13255. if( !msg->send_id ){
  13256. mailType |= MAIL_TYPE_NPC;
  13257. }
  13258. #endif
  13259. WFIFOB(fd, offset + 9) = mailType;
  13260. safestrncpy(WFIFOCP(fd, offset + 10), msg->send_name, NAME_LENGTH);
  13261. #if PACKETVER < 20170419
  13262. // How much time has passed since you received the mail
  13263. WFIFOL(fd, offset + 34 ) = now - (uint32)msg->timestamp;
  13264. offset += 4;
  13265. #endif
  13266. // If automatic return/deletion of mails is enabled, notify the client when it will kick in
  13267. if( msg->scheduled_deletion > 0 ){
  13268. WFIFOL(fd, offset + 34) = (uint32)msg->scheduled_deletion - now;
  13269. }else{
  13270. // Fake the scheduled deletion to one year in the future
  13271. // Sadly the client always displays the scheduled deletion after 24 hours no matter how high this value gets [Lemongrass]
  13272. WFIFOL(fd, offset + 34) = 365 * 24 * 60 * 60;
  13273. }
  13274. WFIFOW(fd, offset + 38) = titleLength = (int16)(strlen(msg->title) + 1);
  13275. safestrncpy(WFIFOCP(fd, offset + 40), msg->title, titleLength);
  13276. offset += 40 + titleLength;
  13277. }
  13278. WFIFOW(fd, 2) = (int16)offset;
  13279. WFIFOSET(fd, offset);
  13280. #endif
  13281. }
  13282. /// Mail inbox list request.
  13283. /// 023f (CZ_MAIL_GET_LIST)
  13284. /// 09e8 <mail tab>.B <mail id>.Q (CZ_OPEN_MAILBOX)
  13285. /// 09ee <mail tab>.B <mail id>.Q (CZ_REQ_NEXT_MAIL_LIST)
  13286. /// 09ef <mail tab>.B <mail id>.Q (CZ_REQ_REFRESH_MAIL_LIST)
  13287. /// 0ac0 <mail id>.Q <unknown>.16B (CZ_OPEN_MAILBOX2)
  13288. /// 0ac1 <mail id>.Q <unknown>.16B (CZ_REQ_REFRESH_MAIL_LIST2)
  13289. void clif_parse_Mail_refreshinbox(int fd, map_session_data *sd){
  13290. if( mail_invalid_operation( sd ) ){
  13291. return;
  13292. }
  13293. #if PACKETVER < 20150513
  13294. struct mail_data* md = &sd->mail.inbox;
  13295. if( md->amount < MAIL_MAX_INBOX && (md->full || sd->mail.changed) )
  13296. intif_Mail_requestinbox(sd->status.char_id, 1, MAIL_INBOX_NORMAL);
  13297. else
  13298. clif_Mail_refreshinbox(sd, MAIL_INBOX_NORMAL,0);
  13299. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13300. mail_removezeny(sd, false);
  13301. #else
  13302. int cmd = RFIFOW(fd, 0);
  13303. #if PACKETVER < 20170419
  13304. uint8 openType = RFIFOB(fd, 2);
  13305. uint64 mailId = RFIFOQ(fd, 3);
  13306. #else
  13307. uint8 openType;
  13308. uint64 mailId = RFIFOQ(fd, 2);
  13309. int i;
  13310. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mailId);
  13311. if( i == MAIL_MAX_INBOX ){
  13312. openType = MAIL_INBOX_NORMAL;
  13313. mailId = 0;
  13314. }else{
  13315. openType = sd->mail.inbox.msg[i].type;
  13316. mailId = 0;
  13317. }
  13318. #endif
  13319. switch( openType ){
  13320. case MAIL_INBOX_NORMAL:
  13321. case MAIL_INBOX_ACCOUNT:
  13322. case MAIL_INBOX_RETURNED:
  13323. break;
  13324. default:
  13325. // Unknown type: ignore request
  13326. return;
  13327. }
  13328. if( sd->mail.changed || ( cmd == 0x9ef || cmd == 0xac1 ) ){
  13329. intif_Mail_requestinbox(sd->status.char_id, 1, (enum mail_inbox_type)openType);
  13330. return;
  13331. }
  13332. // If it is not a next page request
  13333. if( cmd != 0x9ee ){
  13334. mailId = 0;
  13335. }
  13336. clif_Mail_refreshinbox(sd,(enum mail_inbox_type)openType,mailId);
  13337. #endif
  13338. }
  13339. /// Opens a mail
  13340. /// 0242 <packet len>.W <mail id>.L <title>.40B <sender>.24B <time>.L <zeny>.L (ZC_MAIL_REQ_OPEN)
  13341. /// <amount>.L <name id>.W <item type>.W <identified>.B <damaged>.B <refine>.B
  13342. /// <card1>.W <card2>.W <card3>.W <card4>.W <message>.?B
  13343. /// 09eb <packet len>.W <type>.B <mail id>.Q <message length>.W <zeny>.Q <item count>.B (ZC_ACK_READ_MAIL)
  13344. /// { }*n
  13345. // TODO: Packet description => for repeated block
  13346. void clif_Mail_read( map_session_data *sd, int mail_id ){
  13347. int i, fd = sd->fd;
  13348. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13349. if( i == MAIL_MAX_INBOX ) {
  13350. clif_Mail_return(sd->fd, mail_id, 1); // Mail doesn't exist
  13351. ShowWarning("clif_parse_Mail_read: char '%s' trying to read a message not the inbox.\n", sd->status.name);
  13352. return;
  13353. } else {
  13354. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13355. struct item *item;
  13356. int16 msg_len;
  13357. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13358. if( msg_len == 0 ) {
  13359. strcpy(msg->body, "(no message)"); // TODO: confirm for RODEX
  13360. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13361. }
  13362. #if PACKETVER < 20150513
  13363. item = &msg->item[0];
  13364. int16 len = 101 + msg_len;
  13365. WFIFOHEAD(fd,len);
  13366. WFIFOW(fd,0) = 0x242;
  13367. WFIFOW(fd,2) = len;
  13368. WFIFOL(fd,4) = msg->id;
  13369. safestrncpy(WFIFOCP(fd,8), msg->title, MAIL_TITLE_LENGTH + 1);
  13370. safestrncpy(WFIFOCP(fd,48), msg->send_name, NAME_LENGTH + 1);
  13371. WFIFOL(fd,72) = 0;
  13372. WFIFOL(fd,76) = msg->zeny;
  13373. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13374. if( item->nameid && data != nullptr ) {
  13375. WFIFOL(fd,80) = item->amount;
  13376. WFIFOW(fd,84) = client_nameid( item->nameid );
  13377. WFIFOW(fd,86) = itemtype( item->nameid );
  13378. WFIFOB(fd,88) = item->identify;
  13379. WFIFOB(fd,89) = item->attribute;
  13380. WFIFOB(fd,90) = item->refine;
  13381. WFIFOW(fd,91) = item->card[0];
  13382. WFIFOW(fd,93) = item->card[1];
  13383. WFIFOW(fd,95) = item->card[2];
  13384. WFIFOW(fd,97) = item->card[3];
  13385. } else // no item, set all to zero
  13386. memset(WFIFOP(fd,80), 0x00, 19);
  13387. WFIFOB(fd,99) = (unsigned char)msg_len;
  13388. safestrncpy(WFIFOCP(fd,100), msg->body, msg_len + 1);
  13389. WFIFOSET(fd,len);
  13390. #else
  13391. msg_len += 1; // Zero Termination
  13392. PACKET_ZC_ACK_READ_RODEX* p = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX*>( packet_buffer );
  13393. p->PacketType = HEADER_ZC_ACK_READ_RODEX;
  13394. p->PacketLength = sizeof( *p );
  13395. p->opentype = msg->type;
  13396. p->MailID = msg->id;
  13397. p->TextcontentsLength = static_cast<decltype(p->TextcontentsLength)>( msg_len );
  13398. p->zeny = msg->zeny;
  13399. safestrncpy( WBUFCP( p, p->PacketLength ), msg->body, msg_len );
  13400. p->PacketLength += p->TextcontentsLength;
  13401. p->ItemCnt = 0;
  13402. for( int j = 0; j < MAIL_MAX_ITEM; j++ ){
  13403. item = &msg->item[j];
  13404. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13405. if( item->nameid > 0 && item->amount > 0 && data != nullptr ){
  13406. PACKET_ZC_ACK_READ_RODEX_SUB* mailitem = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX_SUB*>( WBUFP( p, p->PacketLength ) );
  13407. mailitem->ITID = client_nameid( item->nameid );
  13408. mailitem->count = item->amount;
  13409. mailitem->type = itemtype( item->nameid );
  13410. mailitem->IsIdentified = item->identify ? 1 : 0;
  13411. mailitem->IsDamaged = item->attribute ? 1 : 0;
  13412. mailitem->refiningLevel = item->refine;
  13413. mailitem->location = pc_equippoint_sub( sd, data.get() );
  13414. mailitem->viewSprite = data->look;
  13415. mailitem->bindOnEquip = item->bound ? 2 : data->flag.bindOnEquip ? 1 : 0;
  13416. clif_addcards( &mailitem->slot, item );
  13417. clif_add_random_options( mailitem->option_data, *item );
  13418. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  13419. mailitem->grade = item->enchantgrade;
  13420. #endif
  13421. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( *mailitem ) );
  13422. p->ItemCnt++;
  13423. }
  13424. }
  13425. clif_send( p, p->PacketLength, &sd->bl, SELF );
  13426. #endif
  13427. if (msg->status == MAIL_UNREAD) {
  13428. msg->status = MAIL_READ;
  13429. intif_Mail_read(mail_id);
  13430. clif_parse_Mail_refreshinbox(fd, sd);
  13431. sd->mail.inbox.unread--;
  13432. clif_Mail_new(sd, 0, msg->send_name, msg->title);
  13433. }
  13434. }
  13435. }
  13436. /// Request to open a mail.
  13437. /// 0241 <mail id>.L (CZ_MAIL_OPEN)
  13438. /// 09ea <mail tab>.B <mail id>.Q (CZ_REQ_READ_MAIL)
  13439. void clif_parse_Mail_read(int fd, map_session_data *sd){
  13440. #if PACKETVER < 20150513
  13441. int mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13442. #else
  13443. //uint8 openType = RFIFOB(fd, 2);
  13444. int mail_id = (int)RFIFOQ(fd, 3);
  13445. #endif
  13446. if( mail_id <= 0 )
  13447. return;
  13448. if( mail_invalid_operation(sd) )
  13449. return;
  13450. clif_Mail_read(sd, mail_id);
  13451. }
  13452. /// Allow a player to begin writing a mail
  13453. /// 0a12 <receiver>.24B <success>.B (ZC_ACK_OPEN_WRITE_MAIL)
  13454. void clif_send_Mail_beginwrite_ack( map_session_data *sd, char* name, bool success ){
  13455. PACKET_ZC_ACK_OPEN_WRITE_MAIL p = { 0 };
  13456. p.PacketType = rodexopenwrite;
  13457. safestrncpy(p.receiveName, name, NAME_LENGTH);
  13458. p.result = success;
  13459. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13460. }
  13461. /// Request to start writing a mail
  13462. /// 0a08 <receiver>.24B (CZ_REQ_OPEN_WRITE_MAIL)
  13463. void clif_parse_Mail_beginwrite( int fd, map_session_data *sd ){
  13464. char name[NAME_LENGTH];
  13465. safestrncpy(name, RFIFOCP(fd, 2), NAME_LENGTH);
  13466. if( mail_invalid_operation( sd ) ){
  13467. return;
  13468. }
  13469. if( sd->state.storage_flag || sd->state.mail_writing || sd->state.trading ){
  13470. clif_send_Mail_beginwrite_ack(sd, name, false);
  13471. return;
  13472. }
  13473. mail_clear(sd);
  13474. sd->state.mail_writing = true;
  13475. clif_send_Mail_beginwrite_ack(sd, name,true);
  13476. }
  13477. /// Notification that the client cancelled writing a mail
  13478. /// 0a03 (CZ_REQ_CANCEL_WRITE_MAIL)
  13479. void clif_parse_Mail_cancelwrite( int fd, map_session_data *sd ){
  13480. sd->state.mail_writing = false;
  13481. }
  13482. /// Give the client information about the recipient, if available
  13483. /// 0a14 <char id>.L <class>.W <base level>.W (ZC_CHECK_RECEIVE_CHARACTER_NAME)
  13484. /// 0a51 <char id>.L <class>.W <base level>.W <name>.24B (ZC_CHECK_RECEIVE_CHARACTER_NAME2)
  13485. void clif_Mail_Receiver_Ack( map_session_data* sd, uint32 char_id, short class_, uint32 level, const char* name ){
  13486. #if PACKETVER >= 20141119
  13487. PACKET_ZC_CHECKNAME p = { 0 };
  13488. p.PacketType = HEADER_ZC_CHECKNAME;
  13489. p.CharId = char_id;
  13490. p.Class = class_;
  13491. p.BaseLevel = level;
  13492. #if PACKETVER >= 20160302
  13493. strncpy(p.Name, name, NAME_LENGTH);
  13494. #endif
  13495. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13496. #endif
  13497. }
  13498. /// Request information about the recipient
  13499. /// 0a13 <name>.24B (CZ_CHECK_RECEIVE_CHARACTER_NAME)
  13500. void clif_parse_Mail_Receiver_Check(int fd, map_session_data *sd) {
  13501. #if PACKETVER >= 20140423
  13502. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13503. const PACKET_CZ_CHECKNAME2* p = reinterpret_cast<PACKET_CZ_CHECKNAME2*>( RFIFOP( fd, 0 ) );
  13504. #else
  13505. const PACKET_CZ_CHECKNAME1* p = reinterpret_cast<PACKET_CZ_CHECKNAME1*>( RFIFOP( fd, 0 ) );
  13506. #endif
  13507. static char name[NAME_LENGTH];
  13508. if( mail_invalid_operation( sd ) ){
  13509. return;
  13510. }
  13511. safestrncpy(name, p->Name, NAME_LENGTH);
  13512. intif_mail_checkreceiver(sd, name);
  13513. #endif
  13514. }
  13515. /// Request to receive mail's attachment.
  13516. /// 0244 <mail id>.L (CZ_MAIL_GET_ITEM)
  13517. /// 09f1 <mail id>.Q <mail tab>.B (CZ_REQ_ZENY_FROM_MAIL)
  13518. /// 09f3 <mail id>.Q <mail tab>.B (CZ_REQ_ITEM_FROM_MAIL)
  13519. void clif_parse_Mail_getattach( int fd, map_session_data *sd ){
  13520. int i;
  13521. struct mail_message* msg;
  13522. #if PACKETVER < 20150513
  13523. int mail_id = RFIFOL(fd, packet_db[RFIFOW(fd, 0)].pos[0]);
  13524. int attachment = MAIL_ATT_ALL;
  13525. #else
  13526. uint16 packet_id = RFIFOW(fd, 0);
  13527. int mail_id = (int)RFIFOQ(fd, 2);
  13528. //int openType = RFIFOB(fd, 10);
  13529. int attachment = packet_id == 0x9f1 ? MAIL_ATT_ZENY : packet_id == 0x9f3 ? MAIL_ATT_ITEM : MAIL_ATT_NONE;
  13530. #endif
  13531. if( !chrif_isconnected() )
  13532. return;
  13533. if( mail_id <= 0 )
  13534. return;
  13535. if( mail_invalid_operation(sd) )
  13536. return;
  13537. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13538. if( i == MAIL_MAX_INBOX )
  13539. return;
  13540. msg = &sd->mail.inbox.msg[i];
  13541. if( attachment&MAIL_ATT_ZENY && msg->zeny < 1 ){
  13542. attachment &= ~MAIL_ATT_ZENY;
  13543. }
  13544. if( attachment&MAIL_ATT_ITEM ){
  13545. ARR_FIND(0, MAIL_MAX_ITEM, i, msg->item[i].nameid > 0 || msg->item[i].amount > 0);
  13546. // No items were found
  13547. if( i == MAIL_MAX_ITEM ){
  13548. attachment &= ~MAIL_ATT_ITEM;
  13549. }
  13550. }
  13551. // Either no attachment requested at all or there are no zeny or items in the mail
  13552. if( attachment == MAIL_ATT_NONE ){
  13553. return;
  13554. }
  13555. if( attachment&MAIL_ATT_ZENY ){
  13556. if( ( msg->zeny + sd->status.zeny + sd->mail.pending_zeny ) > MAX_ZENY ){
  13557. clif_mail_getattachment(sd, msg, 1, MAIL_ATT_ZENY); //too many zeny
  13558. return;
  13559. }else{
  13560. // Store the pending zeny (required for the "retrieve all" feature)
  13561. sd->mail.pending_zeny += msg->zeny;
  13562. // To make sure another request fails
  13563. msg->zeny = 0;
  13564. }
  13565. }
  13566. if( attachment&MAIL_ATT_ITEM ){
  13567. int slots = 0, totalweight = 0;
  13568. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  13569. struct item* item = &msg->item[i];
  13570. if( item->nameid > 0 && item->amount > 0 ){
  13571. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13572. if(data == nullptr)
  13573. continue;
  13574. switch( pc_checkadditem(sd, item->nameid, item->amount) ){
  13575. case CHKADDITEM_NEW:
  13576. slots += data->inventorySlotNeeded( item->amount );
  13577. break;
  13578. case CHKADDITEM_OVERAMOUNT:
  13579. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13580. return;
  13581. }
  13582. totalweight += data->weight * item->amount;
  13583. }
  13584. }
  13585. if( ( totalweight + sd->weight + sd->mail.pending_weight ) > sd->max_weight ){
  13586. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13587. return;
  13588. }else if( pc_inventoryblank(sd) < ( slots + sd->mail.pending_slots ) ){
  13589. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13590. return;
  13591. }
  13592. // To make sure another request fails
  13593. memset(msg->item, 0, MAIL_MAX_ITEM*sizeof(struct item));
  13594. // Store the pending weight (required for the "retrieve all" feature)
  13595. sd->mail.pending_weight += totalweight;
  13596. // Store the pending slots (required for the "retrieve all" feature)
  13597. sd->mail.pending_slots += slots;
  13598. }
  13599. intif_mail_getattach(sd,msg, (enum mail_attachment_type)attachment);
  13600. }
  13601. /// Request to delete a mail.
  13602. /// 0243 <mail id>.L (CZ_MAIL_DELETE)
  13603. /// 09f5 <mail tab>.B <mail id>.Q (CZ_REQ_DELETE_MAIL)
  13604. void clif_parse_Mail_delete(int fd, map_session_data *sd){
  13605. #if PACKETVER < 20150513
  13606. int mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13607. #else
  13608. //int openType = RFIFOB(fd, 2);
  13609. int mail_id = (int)RFIFOQ(fd, 3);
  13610. #endif
  13611. int i, j;
  13612. if( !chrif_isconnected() )
  13613. return;
  13614. if( mail_id <= 0 )
  13615. return;
  13616. if( mail_invalid_operation(sd) )
  13617. return;
  13618. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13619. if (i < MAIL_MAX_INBOX) {
  13620. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13621. // can't delete mail without removing zeny first
  13622. if( msg->zeny > 0 ){
  13623. clif_mail_delete(sd, msg, false);
  13624. return;
  13625. }
  13626. // can't delete mail without removing attachment(s) first
  13627. for( j = 0; j < MAIL_MAX_ITEM; j++ ){
  13628. if( msg->item[j].nameid > 0 && msg->item[j].amount > 0 ){
  13629. clif_mail_delete(sd, msg, false);
  13630. return;
  13631. }
  13632. }
  13633. if( intif_Mail_delete(sd->status.char_id, mail_id) && msg->status == MAIL_UNREAD ){
  13634. sd->mail.inbox.unread--;
  13635. clif_Mail_new(sd,0,nullptr,nullptr);
  13636. }
  13637. }
  13638. }
  13639. /// Request to return a mail.
  13640. /// 0273 <mail id>.L <receive name>.24B (CZ_REQ_MAIL_RETURN)
  13641. void clif_parse_Mail_return(int fd, map_session_data *sd){
  13642. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13643. const PACKET_CZ_RODEX_RETURN* p = reinterpret_cast<PACKET_CZ_RODEX_RETURN*>( RFIFOP( fd, 0 ) );
  13644. //ShowDump( p, sizeof( p ) );
  13645. int mail_id = p->msgId;
  13646. // not supported for now
  13647. return;
  13648. #else
  13649. int mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13650. #endif
  13651. //char *rec_name = RFIFOP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  13652. int i;
  13653. if( mail_id <= 0 )
  13654. return;
  13655. if( mail_invalid_operation(sd) )
  13656. return;
  13657. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13658. if( i < MAIL_MAX_INBOX && sd->mail.inbox.msg[i].send_id != 0 )
  13659. intif_Mail_return(sd->status.char_id, mail_id);
  13660. else
  13661. clif_Mail_return(sd->fd, mail_id, 1);
  13662. }
  13663. /// Request to add an item or Zeny to mail.
  13664. /// 0247 <index>.W <amount>.L (CZ_MAIL_ADD_ITEM)
  13665. /// 0a04 <index>.W <amount>.W (CZ_REQ_ADD_ITEM_TO_MAIL)
  13666. void clif_parse_Mail_setattach(int fd, map_session_data *sd){
  13667. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13668. uint16 idx = RFIFOW(fd,info->pos[0]);
  13669. #if PACKETVER < 20150513
  13670. int amount = RFIFOL(fd,info->pos[1]);
  13671. #else
  13672. int amount = RFIFOW(fd,info->pos[1]);
  13673. #endif
  13674. unsigned char flag;
  13675. if( !chrif_isconnected() )
  13676. return;
  13677. if (amount < 0 || server_index(idx) >= MAX_INVENTORY)
  13678. return;
  13679. if (sd->inventory_data[server_index(idx)] == nullptr)
  13680. return;
  13681. if( mail_invalid_operation( sd ) ){
  13682. return;
  13683. }
  13684. flag = mail_setitem(sd, idx, amount);
  13685. if( flag == MAIL_ATTACH_EQUIPSWITCH ){
  13686. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  13687. }else{
  13688. clif_Mail_setattachment(sd,idx,amount,flag);
  13689. }
  13690. }
  13691. /// Remove an item from a mail
  13692. /// 0a07 <result>.B <index>.W <amount>.W <weight>.W
  13693. void clif_mail_removeitem( map_session_data* sd, bool success, int index, int amount ){
  13694. PACKET_ZC_ACK_REMOVE_ITEM_MAIL p = { 0 };
  13695. p.PacketType = rodexremoveitem;
  13696. p.result = success;
  13697. p.index = index;
  13698. p.cnt = amount;
  13699. int total = 0;
  13700. for( int i = 0; i < MAIL_MAX_ITEM; i++ ){
  13701. if( sd->mail.item[i].nameid == 0 ){
  13702. break;
  13703. }
  13704. total += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  13705. }
  13706. p.weight = total;
  13707. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13708. }
  13709. /// Request to reset mail item and/or Zeny
  13710. /// 0246 <type>.W (CZ_MAIL_RESET_ITEM)
  13711. /// type:
  13712. /// 0 = reset all
  13713. /// 1 = remove item
  13714. /// 2 = remove zeny
  13715. /// 0a06 <index>.W <amount>.W (CZ_REQ_REMOVE_ITEM_MAIL)
  13716. void clif_parse_Mail_winopen(int fd, map_session_data *sd)
  13717. {
  13718. #if PACKETVER < 20150513
  13719. int type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13720. if (type == 0 || type == 1)
  13721. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13722. if (type == 0 || type == 2)
  13723. mail_removezeny(sd, false);
  13724. #else
  13725. uint16 index = RFIFOW(fd, 2);
  13726. uint16 count = RFIFOW(fd, 4);
  13727. mail_removeitem(sd,0,index,count);
  13728. #endif
  13729. }
  13730. /// Request to send mail
  13731. /// 0248 <packet len>.W <recipient>.24B <title>.40B <body len>.B <body>.?B (CZ_MAIL_SEND)
  13732. /// 09ec <packet len>.W <recipient>.24B <sender>.24B <zeny>.Q <title length>.W <body length>.W <title>.?B <body>.?B (CZ_REQ_WRITE_MAIL)
  13733. /// 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)
  13734. void clif_parse_Mail_send(int fd, map_session_data *sd){
  13735. #if PACKETVER < 20150513
  13736. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13737. if( !chrif_isconnected() )
  13738. return;
  13739. if( RFIFOW(fd,info->pos[0]) < 69 ) {
  13740. ShowWarning("Invalid Msg Len from account %d.\n", sd->status.account_id);
  13741. return;
  13742. }
  13743. mail_send(sd, RFIFOCP(fd,info->pos[1]), RFIFOCP(fd,info->pos[2]), RFIFOCP(fd,info->pos[4]), RFIFOB(fd,info->pos[3]));
  13744. #else
  13745. uint16 length = RFIFOW(fd, 2);
  13746. if( length < 0x3e ){
  13747. ShowWarning("Too short...\n");
  13748. clif_Mail_send(sd, WRITE_MAIL_FAILED);
  13749. return;
  13750. }
  13751. if( mail_invalid_operation( sd ) ){
  13752. return;
  13753. }
  13754. // Forged request without a begin writing packet?
  13755. if( !sd->state.mail_writing ){
  13756. return; // Ignore it
  13757. }
  13758. char receiver[NAME_LENGTH];
  13759. safestrncpy(receiver, RFIFOCP(fd, 4), NAME_LENGTH);
  13760. // char sender[NAME_LENGTH];
  13761. // safestrncpy(sender, RFIFOCP(fd, 28), NAME_LENGTH);
  13762. uint64 zeny = RFIFOQ(fd, 52);
  13763. uint16 titleLength = RFIFOW(fd, 60);
  13764. uint16 textLength = RFIFOW(fd, 62);
  13765. uint16 realTitleLength = min(titleLength, MAIL_TITLE_LENGTH);
  13766. uint16 realTextLength = min(textLength, MAIL_BODY_LENGTH);
  13767. char title[MAIL_TITLE_LENGTH];
  13768. char text[MAIL_BODY_LENGTH];
  13769. #if PACKETVER <= 20160330
  13770. safestrncpy(title, RFIFOCP(fd, 64), realTitleLength);
  13771. safestrncpy(text, RFIFOCP(fd, 64 + titleLength), realTextLength);
  13772. #else
  13773. // 64 = <char id>.L
  13774. safestrncpy(title, RFIFOCP(fd, 68), realTitleLength);
  13775. safestrncpy(text, RFIFOCP(fd, 68 + titleLength), realTextLength);
  13776. #endif
  13777. if( zeny > 0 ){
  13778. if( mail_setitem(sd,0,(uint32)zeny) != MAIL_ATTACH_SUCCESS ){
  13779. clif_Mail_send(sd,WRITE_MAIL_FAILED);
  13780. return;
  13781. }
  13782. }
  13783. mail_send(sd, receiver, title, text, realTextLength);
  13784. #endif
  13785. }
  13786. /// AUCTION SYSTEM
  13787. /// By Zephyrus
  13788. ///
  13789. /// Opens/closes the auction window (ZC_AUCTION_WINDOWS).
  13790. /// 025f <type>.L
  13791. /// type:
  13792. /// 0 = open
  13793. /// 1 = close
  13794. void clif_Auction_openwindow(map_session_data *sd)
  13795. {
  13796. int fd = sd->fd;
  13797. if( sd->state.storage_flag || sd->state.vending || sd->state.buyingstore || sd->state.trading )
  13798. return;
  13799. if( !battle_config.feature_auction )
  13800. return;
  13801. WFIFOHEAD(fd,packet_len(0x25f));
  13802. WFIFOW(fd,0) = 0x25f;
  13803. WFIFOL(fd,2) = 0;
  13804. WFIFOSET(fd,packet_len(0x25f));
  13805. }
  13806. /// Returns auction item search results (ZC_AUCTION_ITEM_REQ_SEARCH).
  13807. /// 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 }*
  13808. void clif_Auction_results(map_session_data *sd, short count, short pages, uint8 *buf)
  13809. {
  13810. int i, fd = sd->fd, len = sizeof(struct auction_data);
  13811. struct auction_data auction;
  13812. WFIFOHEAD(fd,12 + (count * 83));
  13813. WFIFOW(fd,0) = 0x252;
  13814. WFIFOW(fd,2) = 12 + (count * 83);
  13815. WFIFOL(fd,4) = pages;
  13816. WFIFOL(fd,8) = count;
  13817. for( i = 0; i < count; i++ ) {
  13818. int k = 12 + (i * 83);
  13819. memcpy(&auction, RBUFP(buf,i * len), len);
  13820. WFIFOL(fd,k) = auction.auction_id;
  13821. safestrncpy(WFIFOCP(fd,4+k), auction.seller_name, NAME_LENGTH);
  13822. WFIFOW(fd,28+k) = client_nameid( auction.item.nameid );
  13823. WFIFOL(fd,30+k) = auction.type;
  13824. WFIFOW(fd,34+k) = auction.item.amount; // Always 1
  13825. WFIFOB(fd,36+k) = auction.item.identify;
  13826. WFIFOB(fd,37+k) = auction.item.attribute;
  13827. WFIFOB(fd,38+k) = auction.item.refine;
  13828. WFIFOW(fd,39+k) = auction.item.card[0];
  13829. WFIFOW(fd,41+k) = auction.item.card[1];
  13830. WFIFOW(fd,43+k) = auction.item.card[2];
  13831. WFIFOW(fd,45+k) = auction.item.card[3];
  13832. WFIFOL(fd,47+k) = auction.price;
  13833. WFIFOL(fd,51+k) = auction.buynow;
  13834. safestrncpy(WFIFOCP(fd,55+k), auction.buyer_name, NAME_LENGTH);
  13835. WFIFOL(fd,79+k) = (uint32)auction.timestamp;
  13836. }
  13837. WFIFOSET(fd,WFIFOW(fd,2));
  13838. }
  13839. /// Result from request to add an item (ZC_ACK_AUCTION_ADD_ITEM).
  13840. /// 0256 <index>.W <result>.B
  13841. /// result:
  13842. /// 0 = success
  13843. /// 1 = failure
  13844. static void clif_Auction_setitem(int fd, int index, bool fail)
  13845. {
  13846. WFIFOHEAD(fd,packet_len(0x256));
  13847. WFIFOW(fd,0) = 0x256;
  13848. WFIFOW(fd,2) = index;
  13849. WFIFOB(fd,4) = fail;
  13850. WFIFOSET(fd,packet_len(0x256));
  13851. }
  13852. /// Request to initialize 'new auction' data (CZ_AUCTION_CREATE).
  13853. /// 024b <type>.W
  13854. /// type:
  13855. /// 0 = create (any other action in auction window)
  13856. /// 1 = cancel (cancel pressed on register tab)
  13857. /// ? = junk, uninitialized value (ex. when switching between list filters)
  13858. void clif_parse_Auction_cancelreg(int fd, map_session_data *sd){
  13859. //int type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13860. if( sd->auction.amount > 0 )
  13861. clif_additem(sd, sd->auction.index, sd->auction.amount, 0);
  13862. sd->auction.amount = 0;
  13863. }
  13864. /// Request to add an item to the action (CZ_AUCTION_ADD_ITEM).
  13865. /// 024c <index>.W <count>.L
  13866. void clif_parse_Auction_setitem(int fd, map_session_data *sd){
  13867. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13868. int idx = RFIFOW(fd,info->pos[0]) - 2;
  13869. int amount = RFIFOL(fd,info->pos[1]); // Always 1
  13870. if( !battle_config.feature_auction )
  13871. return;
  13872. if( sd->auction.amount > 0 )
  13873. sd->auction.amount = 0;
  13874. if( idx < 0 || idx >= MAX_INVENTORY ) {
  13875. ShowWarning("Character %s trying to set invalid item index in auctions.\n", sd->status.name);
  13876. return;
  13877. }
  13878. 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.
  13879. ShowWarning("Character %s trying to set invalid amount in auctions.\n", sd->status.name);
  13880. return;
  13881. }
  13882. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[idx].nameid);
  13883. 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) )
  13884. { // Consumable or pets are not allowed
  13885. clif_Auction_setitem(sd->fd, idx, true);
  13886. return;
  13887. }
  13888. if( !pc_can_give_items(sd) || sd->inventory.u.items_inventory[idx].expire_time ||
  13889. !sd->inventory.u.items_inventory[idx].identify ||
  13890. (sd->inventory.u.items_inventory[idx].bound && !pc_can_give_bounded_items(sd)) ||
  13891. !itemdb_available(sd->inventory.u.items_inventory[idx].nameid) ||
  13892. !itemdb_canauction(&sd->inventory.u.items_inventory[idx],pc_get_group_level(sd)) ) { // Quest Item or something else
  13893. clif_Auction_setitem(sd->fd, idx, true);
  13894. return;
  13895. }
  13896. sd->auction.index = idx;
  13897. sd->auction.amount = amount;
  13898. clif_Auction_setitem(fd, idx + 2, false);
  13899. }
  13900. /// Result from an auction action (ZC_AUCTION_RESULT).
  13901. /// 0250 <result>.B
  13902. /// result:
  13903. /// 0 = You have failed to bid into the auction
  13904. /// 1 = You have successfully bid in the auction
  13905. /// 2 = The auction has been canceled
  13906. /// 3 = An auction with at least one bidder cannot be canceled
  13907. /// 4 = You cannot register more than 5 items in an auction at a time
  13908. /// 5 = You do not have enough Zeny to pay the Auction Fee
  13909. /// 6 = You have won the auction
  13910. /// 7 = You have failed to win the auction
  13911. /// 8 = You do not have enough Zeny
  13912. /// 9 = You cannot place more than 5 bids at a time
  13913. void clif_Auction_message(int fd, unsigned char flag)
  13914. {
  13915. WFIFOHEAD(fd,packet_len(0x250));
  13916. WFIFOW(fd,0) = 0x250;
  13917. WFIFOB(fd,2) = flag;
  13918. WFIFOSET(fd,packet_len(0x250));
  13919. }
  13920. /// Result of the auction close request (ZC_AUCTION_ACK_MY_SELL_STOP).
  13921. /// 025e <result>.W
  13922. /// result:
  13923. /// 0 = You have ended the auction
  13924. /// 1 = You cannot end the auction
  13925. /// 2 = Auction ID is incorrect
  13926. void clif_Auction_close(int fd, unsigned char flag)
  13927. {
  13928. WFIFOHEAD(fd,packet_len(0x25e));
  13929. WFIFOW(fd,0) = 0x25d; // BUG: The client identifies this packet as 0x25d (CZ_AUCTION_REQ_MY_SELL_STOP)
  13930. WFIFOW(fd,2) = flag;
  13931. WFIFOSET(fd,packet_len(0x25e));
  13932. }
  13933. /// Request to add an auction (CZ_AUCTION_ADD).
  13934. /// 024d <now money>.L <max money>.L <delete hour>.W
  13935. void clif_parse_Auction_register(int fd, map_session_data *sd)
  13936. {
  13937. struct auction_data auction;
  13938. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13939. if( !battle_config.feature_auction )
  13940. return;
  13941. auction.price = RFIFOL(fd,info->pos[0]);
  13942. auction.buynow = RFIFOL(fd,info->pos[1]);
  13943. auction.hours = RFIFOW(fd,info->pos[2]);
  13944. // Invalid Situations...
  13945. if( sd->auction.amount < 1 ) {
  13946. ShowWarning("Character %s trying to register auction without item.\n", sd->status.name);
  13947. return;
  13948. }
  13949. if( auction.price >= auction.buynow ) {
  13950. ShowWarning("Character %s trying to alter auction prices.\n", sd->status.name);
  13951. return;
  13952. }
  13953. if( auction.hours < 1 || auction.hours > 48 ) {
  13954. ShowWarning("Character %s trying to enter an invalid time for auction.\n", sd->status.name);
  13955. return;
  13956. }
  13957. // Auction checks...
  13958. if( sd->inventory.u.items_inventory[sd->auction.index].bound && !pc_can_give_bounded_items(sd) ) {
  13959. clif_displaymessage(sd->fd, msg_txt(sd,293));
  13960. clif_Auction_message(fd, 2); // The auction has been canceled
  13961. return;
  13962. }
  13963. if( sd->status.zeny < (auction.hours * battle_config.auction_feeperhour) ) {
  13964. clif_Auction_message(fd, 5); // You do not have enough zeny to pay the Auction Fee.
  13965. return;
  13966. }
  13967. if( auction.buynow > battle_config.auction_maximumprice ) { // Zeny Limits
  13968. auction.buynow = battle_config.auction_maximumprice;
  13969. if( auction.price >= auction.buynow )
  13970. auction.price = auction.buynow - 1;
  13971. }
  13972. auction.auction_id = 0;
  13973. auction.seller_id = sd->status.char_id;
  13974. safestrncpy(auction.seller_name, sd->status.name, sizeof(auction.seller_name));
  13975. auction.buyer_id = 0;
  13976. memset(auction.buyer_name, '\0', sizeof(auction.buyer_name));
  13977. if( sd->inventory.u.items_inventory[sd->auction.index].nameid == 0 || sd->inventory.u.items_inventory[sd->auction.index].amount < sd->auction.amount )
  13978. {
  13979. clif_Auction_message(fd, 2); // The auction has been canceled
  13980. return;
  13981. }
  13982. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[sd->auction.index].nameid);
  13983. if( id == nullptr )
  13984. { // Just in case
  13985. clif_Auction_message(fd, 2); // The auction has been canceled
  13986. return;
  13987. }
  13988. safestrncpy(auction.item_name, id->ename.c_str(), sizeof(auction.item_name));
  13989. auction.type = id->type;
  13990. memcpy(&auction.item, &sd->inventory.u.items_inventory[sd->auction.index], sizeof(struct item));
  13991. auction.item.amount = 1;
  13992. auction.timestamp = 0;
  13993. if( !intif_Auction_register(&auction) )
  13994. clif_Auction_message(fd, 4); // No Char Server? lets say something to the client
  13995. else
  13996. {
  13997. int zeny = auction.hours*battle_config.auction_feeperhour;
  13998. pc_delitem(sd, sd->auction.index, sd->auction.amount, 1, 6, LOG_TYPE_AUCTION);
  13999. sd->auction.amount = 0;
  14000. pc_payzeny(sd, zeny, LOG_TYPE_AUCTION);
  14001. }
  14002. }
  14003. /// Cancels an auction (CZ_AUCTION_ADD_CANCEL).
  14004. /// 024e <auction id>.L
  14005. void clif_parse_Auction_cancel(int fd, map_session_data *sd){
  14006. uint32 auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  14007. intif_Auction_cancel(sd->status.char_id, auction_id);
  14008. }
  14009. /// Closes an auction (CZ_AUCTION_REQ_MY_SELL_STOP).
  14010. /// 025d <auction id>.L
  14011. void clif_parse_Auction_close(int fd, map_session_data *sd){
  14012. uint32 auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  14013. intif_Auction_close(sd->status.char_id, auction_id);
  14014. }
  14015. /// Places a bid on an auction (CZ_AUCTION_BUY).
  14016. /// 024f <auction id>.L <money>.L
  14017. void clif_parse_Auction_bid(int fd, map_session_data *sd){
  14018. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  14019. uint32 auction_id = RFIFOL(fd,info->pos[0]);
  14020. int bid = RFIFOL(fd,info->pos[1]);
  14021. if( !pc_can_give_items(sd) ) { //They aren't supposed to give zeny [Inkfish]
  14022. clif_displaymessage(sd->fd, msg_txt(sd,246));
  14023. return;
  14024. }
  14025. if( bid <= 0 )
  14026. clif_Auction_message(fd, 0); // You have failed to bid into the auction
  14027. else if( bid > sd->status.zeny )
  14028. clif_Auction_message(fd, 8); // You do not have enough zeny
  14029. else if ( CheckForCharServer() ) // char server is down (bugreport:1138)
  14030. clif_Auction_message(fd, 0); // You have failed to bid into the auction
  14031. else {
  14032. pc_payzeny(sd, bid, LOG_TYPE_AUCTION);
  14033. intif_Auction_bid(sd->status.char_id, sd->status.name, auction_id, bid);
  14034. }
  14035. }
  14036. /// Auction Search (CZ_AUCTION_ITEM_SEARCH).
  14037. /// 0251 <search type>.W <auction id>.L <search text>.24B <page number>.W
  14038. /// search type:
  14039. /// 0 = armor
  14040. /// 1 = weapon
  14041. /// 2 = card
  14042. /// 3 = misc
  14043. /// 4 = name search
  14044. /// 5 = auction id search
  14045. void clif_parse_Auction_search(int fd, map_session_data* sd){
  14046. char search_text[NAME_LENGTH];
  14047. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  14048. short type = RFIFOW(fd,info->pos[0]);
  14049. int price = RFIFOL(fd,info->pos[1]); // FIXME: bug #5071
  14050. int page = RFIFOW(fd,info->pos[3]);
  14051. if( !battle_config.feature_auction )
  14052. return;
  14053. clif_parse_Auction_cancelreg(fd, sd);
  14054. safestrncpy(search_text, RFIFOCP(fd,info->pos[2]), sizeof(search_text));
  14055. intif_Auction_requestlist(sd->status.char_id, type, price, search_text, page);
  14056. }
  14057. /// Requests list of own currently active bids or auctions (CZ_AUCTION_REQ_MY_INFO).
  14058. /// 025c <type>.W
  14059. /// type:
  14060. /// 0 = sell (own auctions)
  14061. /// 1 = buy (own bids)
  14062. void clif_parse_Auction_buysell(int fd, map_session_data* sd)
  14063. {
  14064. short type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) + 6;
  14065. if( !battle_config.feature_auction )
  14066. return;
  14067. clif_parse_Auction_cancelreg(fd, sd);
  14068. intif_Auction_requestlist(sd->status.char_id, type, 0, "", 1);
  14069. }
  14070. /// CASH/POINT SHOP
  14071. ///
  14072. void clif_cashshop_open( map_session_data* sd, int tab ){
  14073. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14074. nullpo_retv( sd );
  14075. PACKET_ZC_SE_CASHSHOP_OPEN p = {};
  14076. p.packetType = HEADER_ZC_SE_CASHSHOP_OPEN;
  14077. p.cashPoints = sd->cashPoints;
  14078. p.kafraPoints = sd->kafraPoints;
  14079. #if PACKETVER_MAIN_NUM >= 20200129 || PACKETVER_RE_NUM >= 20200205 || PACKETVER_ZERO_NUM >= 20191224
  14080. p.tab = tab;
  14081. #endif
  14082. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14083. #endif
  14084. }
  14085. void clif_parse_cashshop_open_request( int fd, map_session_data* sd ){
  14086. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14087. nullpo_retv( sd );
  14088. int tab = 0;
  14089. #if PACKETVER >= 20191224
  14090. const PACKET_CZ_SE_CASHSHOP_OPEN2* p = reinterpret_cast<PACKET_CZ_SE_CASHSHOP_OPEN2*>( RFIFOP( fd, 0 ) );
  14091. tab = p->tab;
  14092. #endif
  14093. if (map_getmapflag(sd->bl.m, MF_NOCASHSHOP)) {
  14094. clif_displaymessage(fd, msg_txt(sd, 451)); // Cash Shop is disabled on this map.
  14095. return;
  14096. }
  14097. sd->state.cashshop_open = true;
  14098. sd->npc_shopid = -1; // Set npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14099. clif_cashshop_open( sd, tab );
  14100. #endif
  14101. }
  14102. void clif_parse_cashshop_close( int fd, map_session_data* sd ){
  14103. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14104. sd->state.cashshop_open = false;
  14105. sd->npc_shopid = 0; // Reset npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14106. // No need to do anything here
  14107. #endif
  14108. }
  14109. //0846 <tabid>.W (CZ_REQ_SE_CASH_TAB_CODE))
  14110. //08c0 <len>.W <openIdentity>.L <itemcount>.W (ZC_ACK_SE_CASH_ITEM_LIST2)
  14111. void clif_parse_CashShopReqTab(int fd, map_session_data *sd) {
  14112. #if PACKETVER_MAIN_NUM >= 20100824 || PACKETVER_RE_NUM >= 20100824 || defined(PACKETVER_ZERO)
  14113. const PACKET_CZ_REQ_SE_CASH_TAB_CODE* p_in = reinterpret_cast<PACKET_CZ_REQ_SE_CASH_TAB_CODE*>( RFIFOP( fd, 0 ) );
  14114. // TODO: most likely wrong answer packet. Most likely HEADER_ZC_ACK_SE_CASH_ITEM_LIST (0x847) would be correct [Lemongrass]
  14115. // [4144] packet exists only in 2011 and was dropped after
  14116. #if PACKETVER >= 20110222 && PACKETVER < 20120000
  14117. std::shared_ptr<s_cash_item_tab> tab = cash_shop_db.find( static_cast<e_cash_shop_tab>( p_in->tab ) );
  14118. if( tab == nullptr ){
  14119. return;
  14120. }
  14121. // Skip empty tabs, the client only expects filled ones
  14122. if( tab->items.empty() ){
  14123. return;
  14124. }
  14125. #if !(PACKETVER_SUPPORTS_SALES)
  14126. if( tab->tab == CASHSHOP_TAB_SALE ){
  14127. return;
  14128. }
  14129. #endif
  14130. PACKET_ZC_ACK_SE_CASH_ITEM_LIST2* p = reinterpret_cast<PACKET_ZC_ACK_SE_CASH_ITEM_LIST2*>( packet_buffer );
  14131. p->packetType = HEADER_ZC_ACK_SE_CASH_ITEM_LIST2;
  14132. p->packetLength = sizeof( *p );
  14133. p->tab = tab->tab;
  14134. p->count = 0;
  14135. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14136. p->items[p->count].itemId = client_nameid( item->nameid );
  14137. p->items[p->count].price = item->price;
  14138. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14139. p->count++;
  14140. }
  14141. clif_send( p, p->packetLength, &sd->bl, SELF );
  14142. #endif
  14143. #endif
  14144. }
  14145. //08ca <len>.W <itemcount> W <tabcode>.W (ZC_ACK_SCHEDULER_CASHITEM)
  14146. void clif_cashshop_list( map_session_data& sd ){
  14147. for( const auto& pair : cash_shop_db ){
  14148. std::shared_ptr<s_cash_item_tab> tab = pair.second;
  14149. // Skip empty tabs, the client only expects filled ones
  14150. if( tab->items.empty() ){
  14151. continue;
  14152. }
  14153. #if !(PACKETVER_SUPPORTS_SALES)
  14154. if( tab->tab == CASHSHOP_TAB_SALE ){
  14155. continue;
  14156. }
  14157. #endif
  14158. PACKET_ZC_ACK_SCHEDULER_CASHITEM* p = reinterpret_cast<PACKET_ZC_ACK_SCHEDULER_CASHITEM*>( packet_buffer );
  14159. p->packetType = HEADER_ZC_ACK_SCHEDULER_CASHITEM;
  14160. p->packetLength = sizeof( *p );
  14161. p->count = 0;
  14162. p->tabNum = tab->tab;
  14163. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14164. p->items[p->count].itemId = client_nameid( item->nameid );
  14165. p->items[p->count].price = item->price;
  14166. #ifdef ENABLE_CASHSHOP_PREVIEW_PATCH
  14167. struct item_data* id = itemdb_search( item->nameid );
  14168. if( id == nullptr ){
  14169. p->items[p->count].location = 0;
  14170. p->items[p->count].viewSprite = 0;
  14171. }else{
  14172. p->items[p->count].location = pc_equippoint_sub( &sd, id );
  14173. p->items[p->count].viewSprite = id->look;
  14174. }
  14175. #endif
  14176. p->count++;
  14177. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14178. if( ( static_cast<size_t>( p->packetLength ) + sizeof( p->items[0] ) ) >= INT16_MAX ){
  14179. // Send current data
  14180. clif_send( p, p->packetLength, &sd.bl, SELF );
  14181. // Start a new packet
  14182. p->count = 0;
  14183. p->packetLength = sizeof( *p );
  14184. }
  14185. }
  14186. clif_send( p, p->packetLength, &sd.bl, SELF );
  14187. }
  14188. }
  14189. void clif_parse_cashshop_list_request( int fd, map_session_data* sd ){
  14190. if( !sd->status.cashshop_sent ) {
  14191. clif_cashshop_list( *sd );
  14192. #if PACKETVER_SUPPORTS_SALES
  14193. sale_notify_login(sd);
  14194. #endif
  14195. sd->status.cashshop_sent = true;
  14196. }
  14197. }
  14198. /// List of items offered in a cash shop (ZC_PC_CASH_POINT_ITEMLIST).
  14199. /// 0287 <packet len>.W <cash point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }*
  14200. /// 0287 <packet len>.W <cash point>.L <kafra point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }* (PACKETVER >= 20070711)
  14201. void clif_cashshop_show( map_session_data& sd, npc_data& nd ){
  14202. sd.npc_shopid = nd.bl.id;
  14203. int cost[2] = { 0, 0 };
  14204. npc_shop_currency_type( &sd, &nd, cost, true );
  14205. PACKET_ZC_PC_CASH_POINT_ITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_CASH_POINT_ITEMLIST*>( packet_buffer );
  14206. p->packetType = HEADER_ZC_PC_CASH_POINT_ITEMLIST;
  14207. p->packetLength = sizeof( *p );
  14208. p->cashPoints = cost[0];
  14209. #if PACKETVER >= 20070711
  14210. p->kafraPoints = cost[1];
  14211. #endif
  14212. for( int i = 0; i < nd.u.shop.count; i++ ) {
  14213. struct item_data* id = itemdb_search( nd.u.shop.shop_item[i].nameid );
  14214. p->items[i].price = nd.u.shop.shop_item[i].value;
  14215. p->items[i].discountPrice = nd.u.shop.shop_item[i].value; // Discount Price
  14216. p->items[i].itemType = itemtype( id->nameid );
  14217. p->items[i].itemId = client_nameid( id->nameid );
  14218. #ifdef ENABLE_OLD_CASHSHOP_PREVIEW_PATCH
  14219. p->items[i].location = pc_equippoint_sub( &sd, id );
  14220. p->items[i].viewSprite = id->look;
  14221. memset( p->items[i].unused, 0, sizeof( p->items[i].unused ) );
  14222. #endif
  14223. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14224. }
  14225. clif_send( p, p->packetLength, &sd.bl, SELF );
  14226. }
  14227. /// Cashshop Buy Ack (ZC_PC_CASH_POINT_UPDATE).
  14228. /// 0289 <cash point>.L <error>.W
  14229. /// 0289 <cash point>.L <kafra point>.L <error>.W (PACKETVER >= 20070711)
  14230. /// error:
  14231. /// 0 = The deal has successfully completed. (ERROR_TYPE_NONE)
  14232. /// 1 = The Purchase has failed because the NPC does not exist. (ERROR_TYPE_NPC)
  14233. /// 2 = The Purchase has failed because the Kafra Shop System is not working correctly. (ERROR_TYPE_SYSTEM)
  14234. /// 3 = You are over your Weight Limit. (ERROR_TYPE_INVENTORY_WEIGHT)
  14235. /// 4 = You cannot purchase items while you are in a trade. (ERROR_TYPE_EXCHANGE)
  14236. /// 5 = The Purchase has failed because the Item Information was incorrect. (ERROR_TYPE_ITEM_ID)
  14237. /// 6 = You do not have enough Kafra Credit Points. (ERROR_TYPE_MONEY)
  14238. /// 7 = You can purchase up to 10 items. (ERROR_TYPE_AMOUNT)
  14239. /// 8 = Some items could not be purchased. (ERROR_TYPE_PURCHASE_FAIL)
  14240. void clif_cashshop_ack(map_session_data* sd, int error)
  14241. {
  14242. int fd, cost[2] = { 0, 0 };
  14243. struct npc_data *nd;
  14244. nullpo_retv(sd);
  14245. fd = sd->fd;
  14246. nd = map_id2nd(sd->npc_shopid);
  14247. npc_shop_currency_type(sd, nd, cost, false);
  14248. WFIFOHEAD(fd, packet_len(0x289));
  14249. WFIFOW(fd,0) = 0x289;
  14250. WFIFOL(fd,2) = cost[0];
  14251. #if PACKETVER < 20070711
  14252. WFIFOW(fd,6) = TOW(error);
  14253. #else
  14254. WFIFOL(fd,6) = cost[1];
  14255. WFIFOW(fd,10) = TOW(error);
  14256. #endif
  14257. WFIFOSET(fd, packet_len(0x289));
  14258. }
  14259. void clif_cashshop_result( map_session_data *sd, t_itemid item_id, uint16 result ){
  14260. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined( PACKETVER_ZERO )
  14261. nullpo_retv( sd );
  14262. PACKET_ZC_SE_PC_BUY_CASHITEM_RESULT packet = {};
  14263. packet.packetType = HEADER_ZC_SE_PC_BUY_CASHITEM_RESULT;
  14264. if( item_id != 0 ){
  14265. packet.itemId = client_nameid( item_id );
  14266. }else{
  14267. packet.itemId = 0;
  14268. }
  14269. packet.result = result;
  14270. packet.cashPoints = sd->cashPoints;
  14271. packet.kafraPoints = sd->kafraPoints;
  14272. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  14273. #endif
  14274. }
  14275. /// Request to buy item(s) from cash shop.
  14276. /// 0288 <name id>.W <amount>.W (CZ_PC_BUY_CASH_POINT_ITEM)
  14277. /// 0288 <name id>.W <amount>.W <kafra points>.L (PACKETVER >= 20070711)
  14278. /// 0288 <packet len>.W <kafra points>.L <count>.W { <amount>.W <name id>.W }.4B*count (PACKETVER >= 20100803)
  14279. void clif_parse_npccashshop_buy( int fd, map_session_data *sd ){
  14280. nullpo_retv( sd );
  14281. if( sd->state.trading || !sd->npc_shopid ){
  14282. clif_cashshop_ack( sd, 1 );
  14283. return;
  14284. }
  14285. const PACKET_CZ_PC_BUY_CASH_POINT_ITEM* p = reinterpret_cast<PACKET_CZ_PC_BUY_CASH_POINT_ITEM*>( RFIFOP( fd, 0 ) );
  14286. #if PACKETVER < 20070711
  14287. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, 0 ) );
  14288. #elif PACKETVER < 20101116
  14289. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, p->kafraPoints ) );
  14290. #else
  14291. int s_itl = sizeof( p->items[0] );
  14292. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14293. 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 );
  14294. return;
  14295. }
  14296. std::vector<s_npc_buy_list> item_list = {};
  14297. item_list.reserve( p->count );
  14298. for( int i = 0; i < p->count; i++ ){
  14299. s_npc_buy_list item = {};
  14300. item.nameid = p->items[i].itemId;
  14301. item.qty = p->items[i].amount;
  14302. item_list.push_back( item );
  14303. }
  14304. clif_cashshop_ack( sd, npc_cashshop_buylist( sd, p->kafraPoints, item_list ) );
  14305. #endif
  14306. }
  14307. /// Request to buy item(s) from cash shop.
  14308. /// 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)
  14309. void clif_parse_cashshop_buy( int fd, map_session_data *sd ){
  14310. const PACKET_CZ_SE_PC_BUY_CASHITEM_LIST* p = reinterpret_cast<PACKET_CZ_SE_PC_BUY_CASHITEM_LIST*>( RFIFOP( fd, 0 ) );
  14311. int s_itl = sizeof( p->items[0] );
  14312. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14313. 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 );
  14314. return;
  14315. }
  14316. cashshop_buylist( sd, p->kafraPoints, p->count, p->items );
  14317. }
  14318. /// Adoption System
  14319. ///
  14320. /// Adoption message (ZC_BABYMSG).
  14321. /// 0216 <msg>.L
  14322. /// msg:
  14323. /// ADOPT_REPLY_MORE_CHILDREN = "You cannot adopt more than 1 child."
  14324. /// ADOPT_REPLY_LEVEL_70 = "You must be at least character level 70 in order to adopt someone."
  14325. /// ADOPT_REPLY_MARRIED = "You cannot adopt a married person."
  14326. void clif_Adopt_reply(map_session_data *sd, int type)
  14327. {
  14328. int fd = sd->fd;
  14329. WFIFOHEAD(fd,6);
  14330. WFIFOW(fd,0) = 0x216;
  14331. WFIFOL(fd,2) = type;
  14332. WFIFOSET(fd,6);
  14333. }
  14334. /// Adoption confirmation (ZC_REQ_BABY).
  14335. /// 01f6 <account id>.L <char id>.L <name>.B
  14336. void clif_Adopt_request(map_session_data *sd, map_session_data *src, int p_id)
  14337. {
  14338. int fd = sd->fd;
  14339. WFIFOHEAD(fd,34);
  14340. WFIFOW(fd,0) = 0x1f6;
  14341. WFIFOL(fd,2) = src->status.account_id;
  14342. WFIFOL(fd,6) = p_id;
  14343. safestrncpy(WFIFOCP(fd,10), src->status.name, NAME_LENGTH);
  14344. WFIFOSET(fd,34);
  14345. }
  14346. /// Request to adopt a player (CZ_REQ_JOIN_BABY).
  14347. /// 01f9 <account id>.L
  14348. void clif_parse_Adopt_request(int fd, map_session_data *sd)
  14349. {
  14350. TBL_PC *tsd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  14351. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  14352. if( pc_try_adopt(sd, p_sd, tsd) == ADOPT_ALLOWED )
  14353. {
  14354. tsd->adopt_invite = sd->status.account_id;
  14355. clif_Adopt_request(tsd, sd, p_sd->status.account_id);
  14356. }
  14357. }
  14358. /// Answer to adopt confirmation (CZ_JOIN_BABY).
  14359. /// 01f7 <account id>.L <char id>.L <answer>.L
  14360. /// answer:
  14361. /// 0 = rejected
  14362. /// 1 = accepted
  14363. void clif_parse_Adopt_reply(int fd, map_session_data *sd){
  14364. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  14365. int p1_id = RFIFOL(fd,info->pos[0]);
  14366. int p2_id = RFIFOL(fd,info->pos[1]);
  14367. int result = RFIFOL(fd,info->pos[2]);
  14368. map_session_data* p1_sd = map_id2sd(p1_id);
  14369. map_session_data* p2_sd = map_id2sd(p2_id);
  14370. int pid = sd->adopt_invite;
  14371. sd->adopt_invite = 0;
  14372. if( p1_sd == nullptr || p2_sd == nullptr )
  14373. return; // Both players need to be online
  14374. if( pid != p1_sd->status.account_id )
  14375. return; // Incorrect values
  14376. if( result == 0 )
  14377. return; // Rejected
  14378. pc_adoption(p1_sd, p2_sd, sd);
  14379. }
  14380. /// Convex Mirror
  14381. /// 0293 <infoType>.B <x>.L <y>.L <minHours>.W <minMinutes>.W <maxHours>.W <maxMinutes>.W <monster name>.51B (ZC_BOSS_INFO)
  14382. /// infoType:
  14383. /// BOSS_INFO_NOT = No boss on this map.
  14384. /// BOSS_INFO_ALIVE = Boss is alive (position update).
  14385. /// BOSS_INFO_ALIVE_WITHMSG = Boss is alive (initial announce).
  14386. /// BOSS_INFO_DEAD = Boss is dead.
  14387. void clif_bossmapinfo( map_session_data& sd, mob_data* md, e_bossmap_info flag ){
  14388. PACKET_ZC_BOSS_INFO p = {};
  14389. p.packetType = HEADER_ZC_BOSS_INFO;
  14390. p.type = flag;
  14391. switch (flag) {
  14392. case BOSS_INFO_NOT:
  14393. // No data required
  14394. break;
  14395. case BOSS_INFO_ALIVE_WITHMSG:
  14396. [[fallthrough]];
  14397. case BOSS_INFO_ALIVE:
  14398. p.x = md->bl.x;
  14399. p.y = md->bl.y;
  14400. break;
  14401. case BOSS_INFO_DEAD: {
  14402. const struct TimerData * timer_data = get_timer(md->spawn_timer);
  14403. uint32 seconds;
  14404. int hours, minutes;
  14405. seconds = (uint32)(DIFF_TICK(timer_data->tick, gettick()) / 1000 + 60);
  14406. hours = seconds / (60 * 60);
  14407. seconds = seconds - (60 * 60 * hours);
  14408. minutes = seconds / 60;
  14409. p.minHours = hours;
  14410. p.minMinutes = minutes;
  14411. break;
  14412. }
  14413. }
  14414. if( md != nullptr ){
  14415. safestrncpy( p.name, md->db->jname.c_str(), sizeof( p.name ) );
  14416. }
  14417. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  14418. }
  14419. /// Requesting equip of a player (CZ_EQUIPWIN_MICROSCOPE).
  14420. /// 02d6 <account id>.L
  14421. void clif_parse_ViewPlayerEquip(int fd, map_session_data* sd)
  14422. {
  14423. int aid = RFIFOL(fd, packet_db[RFIFOW(fd,0)].pos[0]);
  14424. map_session_data* tsd = map_id2sd(aid);
  14425. if (!tsd)
  14426. return;
  14427. if (sd->bl.m != tsd->bl.m)
  14428. return;
  14429. else if( tsd->status.show_equip || pc_has_permission(sd, PC_PERM_VIEW_EQUIPMENT) )
  14430. clif_viewequip_ack( *sd, *tsd );
  14431. else
  14432. clif_msg(sd, MSI_OPEN_EQUIPEDITEM_REFUSED);
  14433. }
  14434. /// Request to a configuration.
  14435. /// 02d8 <type>.L <flag>.L (CZ_CONFIG)
  14436. /// type: see enum e_config_type
  14437. /// flag:
  14438. /// false = disabled
  14439. /// true = enabled
  14440. void clif_parse_configuration( int fd, map_session_data* sd ){
  14441. int cmd = RFIFOW(fd,0);
  14442. int type = RFIFOL(fd,packet_db[cmd].pos[0]);
  14443. bool flag = RFIFOL(fd,packet_db[cmd].pos[1]) != 0;
  14444. switch( type ){
  14445. case CONFIG_OPEN_EQUIPMENT_WINDOW:
  14446. sd->status.show_equip = flag;
  14447. break;
  14448. case CONFIG_CALL:
  14449. sd->status.disable_call = flag;
  14450. break;
  14451. case CONFIG_PET_AUTOFEED:
  14452. // Player can not click this if he does not have a pet
  14453. if( sd->pd == nullptr || !battle_config.feature_pet_autofeed || !sd->pd->get_pet_db()->allow_autofeed ){
  14454. return;
  14455. }
  14456. sd->pd->pet.autofeed = flag;
  14457. break;
  14458. case CONFIG_HOMUNCULUS_AUTOFEED:
  14459. // Player can not click this if he does not have a homunculus or it is vaporized
  14460. if( sd->hd == nullptr || sd->hd->homunculus.vaporize || !battle_config.feature_homunculus_autofeed ){
  14461. return;
  14462. }
  14463. sd->hd->homunculus.autofeed = flag;
  14464. break;
  14465. default:
  14466. ShowWarning( "clif_parse_configuration: received unknown configuration type '%d'...\n", type );
  14467. return;
  14468. }
  14469. clif_configuration( sd, static_cast<e_config_type>(type), flag );
  14470. }
  14471. /// Request to change party invitation tick.
  14472. /// 02C8 <enabled>.B (CZ_PARTY_CONFIG)
  14473. /// value:
  14474. /// 0 = disabled (triggered by /accept)
  14475. /// 1 = enabled (triggered by /refuse)
  14476. void clif_parse_PartyTick( int fd, map_session_data* sd ){
  14477. const PACKET_CZ_PARTY_CONFIG* p = reinterpret_cast<PACKET_CZ_PARTY_CONFIG*>( RFIFOP( fd, 0 ) );
  14478. sd->status.disable_partyinvite = p->refuseInvite;
  14479. clif_partyinvitationstate( *sd );
  14480. }
  14481. /// Questlog System [Kevin] [Inkfish]
  14482. ///
  14483. /**
  14484. * Safe check to init packet len & quest num for player.
  14485. * @param def_len
  14486. * @param info_len Length of each quest info.
  14487. * @param avail_quests Avail quests that player has.
  14488. * @param limit_out Limit for quest num, to make sure the quest num is still in allowed value.
  14489. * @param len_out Packet length as initial set => def_len + limit_out * info_len.
  14490. **/
  14491. static void clif_quest_len(int def_len, int info_len, int avail_quests, int *limit_out, int *len_out) {
  14492. (*limit_out) = (0xFFFF - def_len) / info_len;
  14493. (*limit_out) = (avail_quests > (*limit_out)) ? (*limit_out) : avail_quests;
  14494. (*len_out) = ((*limit_out) * info_len) + def_len;
  14495. }
  14496. std::string clif_quest_string( std::shared_ptr<s_quest_objective> objective ){
  14497. std::string race_name;
  14498. switch( objective->race ){
  14499. case RC_FORMLESS: race_name = "Formless"; break;
  14500. case RC_UNDEAD: race_name = "Undead"; break;
  14501. case RC_BRUTE: race_name = "Brute"; break;
  14502. case RC_PLANT: race_name = "Plant"; break;
  14503. case RC_INSECT: race_name = "Insect"; break;
  14504. case RC_FISH: race_name = "Fish"; break;
  14505. case RC_DEMON: race_name = "Demon"; break;
  14506. case RC_DEMIHUMAN: race_name = "Demihuman"; break;
  14507. case RC_ANGEL: race_name = "Angel"; break;
  14508. case RC_DRAGON: race_name = "Dragon"; break;
  14509. default:
  14510. ShowWarning( "clif_quest_string: Unsupported race %d - using empty string...\n", objective->race );
  14511. [[fallthrough]];
  14512. case RC_ALL: race_name = ""; break;
  14513. }
  14514. std::string size_name;
  14515. switch( objective->size ){
  14516. case SZ_SMALL: size_name = "Small"; break;
  14517. case SZ_MEDIUM: size_name = "Medium"; break;
  14518. case SZ_BIG: size_name = "Large"; break;
  14519. default:
  14520. ShowWarning( "clif_quest_string: Unsupported size %d - using empty string...\n", objective->size );
  14521. [[fallthrough]];
  14522. case SZ_ALL: size_name = ""; break;
  14523. }
  14524. std::string ele_name;
  14525. switch( objective->element ){
  14526. case ELE_NEUTRAL: ele_name = "Neutral Element"; break;
  14527. case ELE_WATER: ele_name = "Water Element"; break;
  14528. case ELE_EARTH: ele_name = "Earth Element"; break;
  14529. case ELE_FIRE: ele_name = "Fire Element"; break;
  14530. case ELE_WIND: ele_name = "Wind Element"; break;
  14531. case ELE_POISON: ele_name = "Poison Element"; break;
  14532. case ELE_HOLY: ele_name = "Holy Element"; break;
  14533. case ELE_DARK: ele_name = "Shadow Element"; break;
  14534. case ELE_GHOST: ele_name = "Ghost Element"; break;
  14535. case ELE_UNDEAD: ele_name = "Undead Element"; break;
  14536. default:
  14537. ShowWarning( "clif_quest_string: Unsupported element %d - using empty string...\n", objective->element );
  14538. [[fallthrough]];
  14539. case ELE_ALL: ele_name = ""; break;
  14540. }
  14541. std::string str;
  14542. if( !objective->map_name.empty() ){
  14543. str += objective->map_name;
  14544. }
  14545. if( !race_name.empty() ){
  14546. if( !str.empty() ){
  14547. str += ", ";
  14548. }
  14549. str += race_name;
  14550. }
  14551. if( !size_name.empty() ){
  14552. if( !str.empty() ){
  14553. str += ", ";
  14554. }
  14555. str += size_name;
  14556. }
  14557. if( !ele_name.empty() ){
  14558. if( !str.empty() ){
  14559. str += ", ";
  14560. }
  14561. str += ele_name;
  14562. }
  14563. return str;
  14564. }
  14565. /// Sends list of all quest states
  14566. /// 02b1 <packet len>.W <num>.L { <quest id>.L <active>.B }*num (ZC_ALL_QUEST_LIST)
  14567. /// 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)
  14568. /// 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)
  14569. void clif_quest_send_list(map_session_data *sd)
  14570. {
  14571. nullpo_retv(sd);
  14572. int fd = sd->fd;
  14573. int i;
  14574. int offset = 8;
  14575. int limit = 0;
  14576. #if PACKETVER >= 20141022
  14577. #if PACKETVER >= 20150513
  14578. int size = 22 + NAME_LENGTH;
  14579. #else
  14580. int size = 10 + NAME_LENGTH;
  14581. #endif
  14582. clif_quest_len(offset, 15 + ( size * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &i);
  14583. WFIFOHEAD(fd,i);
  14584. #if PACKETVER >= 20150513
  14585. WFIFOW(fd, 0) = 0x9f8;
  14586. #else
  14587. WFIFOW(fd, 0) = 0x97a;
  14588. #endif
  14589. WFIFOL(fd, 4) = limit;
  14590. for (i = 0; i < limit; i++) {
  14591. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14592. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14593. offset += 4;
  14594. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14595. offset++;
  14596. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14597. offset += 4;
  14598. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time);
  14599. offset += 4;
  14600. WFIFOW(fd, offset) = static_cast<uint16>(qi->objectives.size());
  14601. offset += 2;
  14602. if (!qi->objectives.empty()) {
  14603. std::shared_ptr<s_mob_db> mob;
  14604. for (int j = 0; j < qi->objectives.size(); j++) {
  14605. mob = mob_db.find(qi->objectives[j]->mob_id);
  14606. e_race race = qi->objectives[j]->race;
  14607. e_size size = qi->objectives[j]->size;
  14608. e_element element = qi->objectives[j]->element;
  14609. #if PACKETVER >= 20150513
  14610. WFIFOL(fd, offset) = sd->quest_log[i].quest_id * 1000 + j;
  14611. offset += 4;
  14612. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0)));
  14613. offset += 4;
  14614. #endif
  14615. WFIFOL(fd, offset) = ((mob && qi->objectives[j]->mob_id > 0) ? qi->objectives[j]->mob_id : MOBID_PORING);
  14616. offset += 4;
  14617. #if PACKETVER >= 20150513
  14618. WFIFOW(fd, offset) = qi->objectives[j]->min_level;
  14619. offset += 2;
  14620. WFIFOW(fd, offset) = qi->objectives[j]->max_level;
  14621. offset += 2;
  14622. #endif
  14623. WFIFOW(fd, offset) = sd->quest_log[i].count[j];
  14624. offset += 2;
  14625. WFIFOW(fd, offset) = qi->objectives[j]->count;
  14626. offset += 2;
  14627. if (mob && qi->objectives[j]->mob_id > 0)
  14628. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14629. else
  14630. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14631. offset += NAME_LENGTH;
  14632. }
  14633. }
  14634. }
  14635. WFIFOW(fd, 2) = offset;
  14636. WFIFOSET(fd, offset);
  14637. #else
  14638. clif_quest_len(offset, 5, sd->avail_quests, &limit, &i);
  14639. WFIFOHEAD(fd,i);
  14640. WFIFOW(fd, 0) = 0x2b1;
  14641. WFIFOL(fd, 4) = limit;
  14642. for (i = 0; i < limit; i++) {
  14643. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14644. offset += 4;
  14645. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14646. offset += 1;
  14647. }
  14648. WFIFOW(fd, 2) = offset;
  14649. WFIFOSET(fd, offset);
  14650. #endif
  14651. }
  14652. /// Sends list of all quest missions (ZC_ALL_QUEST_MISSION).
  14653. /// 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
  14654. void clif_quest_send_mission(map_session_data *sd)
  14655. {
  14656. nullpo_retv(sd);
  14657. int fd = sd->fd;
  14658. int limit = 0;
  14659. int len = sd->avail_quests*104+8;
  14660. clif_quest_len(8, 14 + ((6 + NAME_LENGTH) * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &len);
  14661. WFIFOHEAD(fd, len);
  14662. WFIFOW(fd, 0) = 0x2b2;
  14663. WFIFOW(fd, 2) = len;
  14664. WFIFOL(fd, 4) = limit;
  14665. for (int i = 0; i < limit; i++) {
  14666. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14667. WFIFOL(fd, i*104+8) = sd->quest_log[i].quest_id;
  14668. WFIFOL(fd, i*104+12) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14669. WFIFOL(fd, i*104+16) = static_cast<uint32>(sd->quest_log[i].time);
  14670. WFIFOW(fd, i*104+20) = static_cast<uint16>(qi->objectives.size());
  14671. for (int j = 0 ; j < qi->objectives.size(); j++) {
  14672. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[j]->mob_id);
  14673. WFIFOL(fd, i*104+22+j*30) = (mob ? qi->objectives[j]->mob_id : MOBID_PORING);
  14674. WFIFOW(fd, i*104+26+j*30) = sd->quest_log[i].count[j];
  14675. if (mob && qi->objectives[j]->mob_id > 0)
  14676. safestrncpy(WFIFOCP(fd, i*104+28+j*30), mob->jname.c_str(), NAME_LENGTH);
  14677. else
  14678. safestrncpy(WFIFOCP(fd, i*104+28+j*30), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14679. }
  14680. }
  14681. WFIFOSET(fd, len);
  14682. }
  14683. /// Notification about a new quest
  14684. /// 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)
  14685. /// 08fe <packet len>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_HUNTING_QUEST_INFO)
  14686. /// 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)
  14687. void clif_quest_add(map_session_data *sd, struct quest *qd)
  14688. {
  14689. nullpo_retv(sd);
  14690. nullpo_retv(qd);
  14691. int fd = sd->fd;
  14692. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14693. if (!qi)
  14694. return;
  14695. #if PACKETVER >= 20150513
  14696. int cmd = 0x9f9;
  14697. #else
  14698. int cmd = 0x2b3;
  14699. #endif
  14700. WFIFOHEAD(fd, packet_len(cmd));
  14701. WFIFOW(fd, 0) = cmd;
  14702. WFIFOL(fd, 2) = qd->quest_id;
  14703. WFIFOB(fd, 6) = qd->state;
  14704. WFIFOB(fd, 7) = static_cast<uint8>(qd->time - qi->time);
  14705. WFIFOL(fd, 11) = static_cast<uint32>(qd->time);
  14706. WFIFOW(fd, 15) = static_cast<uint16>(qi->objectives.size());
  14707. for (int i = 0, offset = 17; i < qi->objectives.size(); i++) {
  14708. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[i]->mob_id);
  14709. e_race race = qi->objectives[i]->race;
  14710. e_size size = qi->objectives[i]->size;
  14711. e_element element = qi->objectives[i]->element;
  14712. #if PACKETVER >= 20150513
  14713. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14714. offset += 4;
  14715. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0))); // effect ?
  14716. offset += 4;
  14717. #endif
  14718. 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
  14719. offset += 4;
  14720. #if PACKETVER >= 20150513
  14721. WFIFOW(fd, offset) = qi->objectives[i]->min_level;
  14722. offset += 2;
  14723. WFIFOW(fd, offset) = qi->objectives[i]->max_level;
  14724. offset += 2;
  14725. #endif
  14726. WFIFOW(fd, offset) = qd->count[i];
  14727. offset += 2;
  14728. if (mob && qi->objectives[i]->mob_id > 0)
  14729. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14730. else
  14731. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[i]).c_str(), NAME_LENGTH);
  14732. offset += NAME_LENGTH;
  14733. }
  14734. WFIFOSET(fd, packet_len(cmd));
  14735. #if PACKETVER >= 20150513
  14736. int16 len;
  14737. len = static_cast<decltype(len)>( 4 + qi->objectives.size() * 12 );
  14738. WFIFOHEAD(fd, len);
  14739. WFIFOW(fd, 0) = 0x8fe;
  14740. WFIFOW(fd, 2) = len;
  14741. for (int i = 0, offset = 4; i < qi->objectives.size(); i++, offset += 12) {
  14742. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14743. WFIFOL(fd, offset + 4) = qi->objectives[i]->mob_id;
  14744. WFIFOW(fd, offset + 8) = qi->objectives[i]->count;
  14745. WFIFOW(fd, offset + 10) = qd->count[i];
  14746. }
  14747. WFIFOSET(fd, len);
  14748. #endif
  14749. }
  14750. /// Notification about a quest being removed (ZC_DEL_QUEST).
  14751. /// 02b4 <quest id>.L
  14752. void clif_quest_delete(map_session_data *sd, int quest_id)
  14753. {
  14754. int fd = sd->fd;
  14755. WFIFOHEAD(fd, packet_len(0x2b4));
  14756. WFIFOW(fd, 0) = 0x2b4;
  14757. WFIFOL(fd, 2) = quest_id;
  14758. WFIFOSET(fd, packet_len(0x2b4));
  14759. }
  14760. /// Notification of an update to the hunting mission counter
  14761. /// 02b5 <packet len>.W <mobs>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT)
  14762. /// 09fa <packet len>.W <mobs>.W { <quest id>.L <hunt identification>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT_EX)
  14763. void clif_quest_update_objective(map_session_data *sd, struct quest *qd)
  14764. {
  14765. int fd = sd->fd;
  14766. int offset = 6;
  14767. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14768. int16 len;
  14769. len = static_cast<decltype(len)>( qi->objectives.size() * 12 + 6 );
  14770. #if PACKETVER >= 20150513
  14771. int cmd = 0x9fa;
  14772. #else
  14773. int cmd = 0x2b5;
  14774. #endif
  14775. WFIFOHEAD(fd, len);
  14776. WFIFOW(fd, 0) = cmd;
  14777. WFIFOW(fd, 4) = static_cast<uint16>(qi->objectives.size());
  14778. for (int i = 0; i < qi->objectives.size(); i++) {
  14779. WFIFOL(fd, offset) = qd->quest_id;
  14780. offset += 4;
  14781. #if PACKETVER >= 20150513
  14782. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14783. offset += 4;
  14784. #else
  14785. WFIFOL(fd, offset) = qi->objectives[i]->mob_id;
  14786. offset += 4;
  14787. #endif
  14788. WFIFOW(fd, offset) = qi->objectives[i]->count;
  14789. offset += 2;
  14790. WFIFOW(fd, offset) = qd->count[i];
  14791. offset += 2;
  14792. }
  14793. WFIFOW(fd, 2) = offset;
  14794. WFIFOSET(fd, offset);
  14795. }
  14796. /// Request to change the state of a quest (CZ_ACTIVE_QUEST).
  14797. /// 02b6 <quest id>.L <active>.B
  14798. void clif_parse_questStateAck(int fd, map_session_data *sd)
  14799. {
  14800. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  14801. quest_update_status(sd, RFIFOL(fd,info->pos[0]),
  14802. RFIFOB(fd,info->pos[1])?Q_ACTIVE:Q_INACTIVE);
  14803. }
  14804. /// Notification about the change of a quest state (ZC_ACTIVE_QUEST).
  14805. /// 02b7 <quest id>.L <active>.B
  14806. void clif_quest_update_status(map_session_data *sd, int quest_id, bool active)
  14807. {
  14808. int fd = sd->fd;
  14809. WFIFOHEAD(fd, packet_len(0x2b7));
  14810. WFIFOW(fd, 0) = 0x2b7;
  14811. WFIFOL(fd, 2) = quest_id;
  14812. WFIFOB(fd, 6) = active;
  14813. WFIFOSET(fd, packet_len(0x2b7));
  14814. }
  14815. /// Notification about an NPC's quest state (ZC_QUEST_NOTIFY_EFFECT).
  14816. /// 0446 <npc id>.L <x>.W <y>.W <effect>.W <color>.W
  14817. /// effect:
  14818. /// 0 = none
  14819. /// 1 = exclamation mark icon
  14820. /// 2 = question mark icon
  14821. /// color:
  14822. /// 0 = yellow
  14823. /// 1 = orange
  14824. /// 2 = green
  14825. /// 3 = purple
  14826. void clif_quest_show_event(map_session_data *sd, struct block_list *bl, e_questinfo_types effect, e_questinfo_markcolor color)
  14827. {
  14828. #if PACKETVER >= 20090218
  14829. nullpo_retv(sd);
  14830. nullpo_retv(bl);
  14831. int fd = sd->fd;
  14832. WFIFOHEAD(fd, packet_len(0x446));
  14833. WFIFOW(fd, 0) = 0x446;
  14834. WFIFOL(fd, 2) = bl->id;
  14835. WFIFOW(fd, 6) = bl->x;
  14836. WFIFOW(fd, 8) = bl->y;
  14837. WFIFOW(fd, 10) = effect;
  14838. WFIFOW(fd, 12) = color;
  14839. WFIFOSET(fd, packet_len(0x446));
  14840. #endif
  14841. }
  14842. /// Mercenary System
  14843. ///
  14844. /// Notification about a mercenary status parameter change (ZC_MER_PAR_CHANGE).
  14845. /// 02a2 <var id>.W <value>.L
  14846. void clif_mercenary_updatestatus(map_session_data *sd, int type)
  14847. {
  14848. s_mercenary_data *md;
  14849. struct status_data *status;
  14850. int fd;
  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(0x2a2));
  14856. WFIFOW(fd,0) = 0x2a2;
  14857. WFIFOW(fd,2) = type;
  14858. switch( type ) {
  14859. case SP_ATK1:
  14860. {
  14861. int atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14862. WFIFOL(fd,4) = cap_value(atk, 0, INT16_MAX);
  14863. }
  14864. break;
  14865. case SP_MATK1:
  14866. WFIFOL(fd,4) = min(status->matk_max, UINT16_MAX);
  14867. break;
  14868. case SP_HIT:
  14869. WFIFOL(fd,4) = status->hit;
  14870. break;
  14871. case SP_CRITICAL:
  14872. WFIFOL(fd,4) = status->cri/10;
  14873. break;
  14874. case SP_DEF1:
  14875. WFIFOL(fd,4) = status->def;
  14876. break;
  14877. case SP_MDEF1:
  14878. WFIFOL(fd,4) = status->mdef;
  14879. break;
  14880. case SP_MERCFLEE:
  14881. WFIFOL(fd,4) = status->flee;
  14882. break;
  14883. case SP_ASPD:
  14884. WFIFOL(fd,4) = status->amotion;
  14885. break;
  14886. case SP_HP:
  14887. WFIFOL(fd,4) = status->hp;
  14888. break;
  14889. case SP_MAXHP:
  14890. WFIFOL(fd,4) = status->max_hp;
  14891. break;
  14892. case SP_SP:
  14893. WFIFOL(fd,4) = status->sp;
  14894. break;
  14895. case SP_MAXSP:
  14896. WFIFOL(fd,4) = status->max_sp;
  14897. break;
  14898. case SP_MERCKILLS:
  14899. WFIFOL(fd,4) = md->mercenary.kill_count;
  14900. break;
  14901. case SP_MERCFAITH:
  14902. WFIFOL(fd,4) = mercenary_get_faith(md);
  14903. break;
  14904. }
  14905. WFIFOSET(fd,packet_len(0x2a2));
  14906. }
  14907. /// Mercenary base status data (ZC_MER_INIT).
  14908. /// 029b <id>.L <atk>.W <matk>.W <hit>.W <crit>.W <def>.W <mdef>.W <flee>.W <aspd>.W
  14909. /// <name>.24B <level>.W <hp>.L <maxhp>.L <sp>.L <maxsp>.L <expire time>.L <faith>.W
  14910. /// <calls>.L <kills>.L <atk range>.W
  14911. void clif_mercenary_info(map_session_data *sd)
  14912. {
  14913. int fd;
  14914. s_mercenary_data *md;
  14915. struct status_data *status;
  14916. int atk;
  14917. if( !clif_session_isValid(sd) || (md = sd->md) == nullptr )
  14918. return;
  14919. fd = sd->fd;
  14920. status = &md->battle_status;
  14921. WFIFOHEAD(fd,packet_len(0x29b));
  14922. WFIFOW(fd,0) = 0x29b;
  14923. WFIFOL(fd,2) = md->bl.id;
  14924. // Mercenary shows ATK as a random value between ATK ~ ATK2
  14925. atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14926. WFIFOW(fd,6) = cap_value(atk, 0, INT16_MAX);
  14927. WFIFOW(fd,8) = min(status->matk_max, UINT16_MAX);
  14928. WFIFOW(fd,10) = status->hit;
  14929. WFIFOW(fd,12) = status->cri/10;
  14930. WFIFOW(fd,14) = status->def;
  14931. WFIFOW(fd,16) = status->mdef;
  14932. WFIFOW(fd,18) = status->flee;
  14933. WFIFOW(fd,20) = status->amotion;
  14934. safestrncpy(WFIFOCP(fd,22), md->db->name.c_str(), NAME_LENGTH);
  14935. WFIFOW(fd,46) = md->db->lv;
  14936. WFIFOL(fd,48) = status->hp;
  14937. WFIFOL(fd,52) = status->max_hp;
  14938. WFIFOL(fd,56) = status->sp;
  14939. WFIFOL(fd,60) = status->max_sp;
  14940. WFIFOL(fd,64) = client_tick(time(nullptr) + (mercenary_get_lifetime(md) / 1000));
  14941. WFIFOW(fd,68) = mercenary_get_faith(md);
  14942. WFIFOL(fd,70) = mercenary_get_calls(md);
  14943. WFIFOL(fd,74) = md->mercenary.kill_count;
  14944. WFIFOW(fd,78) = md->battle_status.rhw.range;
  14945. WFIFOSET(fd,packet_len(0x29b));
  14946. }
  14947. /// Mercenary skill tree (ZC_MER_SKILLINFO_LIST).
  14948. /// 029d <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  14949. void clif_mercenary_skillblock(map_session_data *sd)
  14950. {
  14951. s_mercenary_data *md;
  14952. int fd, len = 4;
  14953. if( sd == nullptr || (md = sd->md) == nullptr )
  14954. return;
  14955. fd = sd->fd;
  14956. WFIFOHEAD(fd,4+37*MAX_MERCSKILL);
  14957. WFIFOW(fd,0) = 0x29d;
  14958. for (const auto &it : md->db->skill) {
  14959. uint16 id = it.first;
  14960. if (!SKILL_CHK_MERC(id))
  14961. continue;
  14962. uint16 lv = it.second;
  14963. WFIFOW(fd,len) = id;
  14964. WFIFOL(fd,len+2) = skill_get_inf(id);
  14965. WFIFOW(fd,len+6) = lv;
  14966. WFIFOW(fd,len+8) = skill_get_sp(id, lv);
  14967. WFIFOW(fd,len+10) = skill_get_range2(&md->bl, id, lv, false);
  14968. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  14969. WFIFOB(fd,len+36) = 0; // Skillable for Mercenary?
  14970. len += 37;
  14971. }
  14972. WFIFOW(fd,2) = len;
  14973. WFIFOSET(fd,len);
  14974. }
  14975. /// Request to invoke a mercenary menu action (CZ_MER_COMMAND).
  14976. /// 029f <command>.B
  14977. /// 1 = mercenary information
  14978. /// 2 = delete
  14979. void clif_parse_mercenary_action(int fd, map_session_data* sd)
  14980. {
  14981. int option = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  14982. if( sd->md == nullptr )
  14983. return;
  14984. if( option == 2 ) mercenary_delete(sd->md, 2);
  14985. }
  14986. /// Notification about the remaining time of a rental item.
  14987. /// 0298 <name id>.W <seconds>.L (ZC_CASH_TIME_COUNTER)
  14988. void clif_rental_time( map_session_data* sd, t_itemid nameid, int seconds ){
  14989. // '<ItemName>' item will disappear in <seconds/60> minutes.
  14990. PACKET_ZC_CASH_TIME_COUNTER p = {};
  14991. p.packetType = HEADER_ZC_CASH_TIME_COUNTER;
  14992. p.itemId = client_nameid( nameid );
  14993. p.seconds = seconds;
  14994. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14995. }
  14996. /// Deletes a rental item from client's inventory.
  14997. /// 0299 <index>.W <name id>.W (ZC_CASH_ITEM_DELETE)
  14998. void clif_rental_expired( map_session_data* sd, int index, t_itemid nameid ){
  14999. // '<ItemName>' item has been deleted from the Inventory
  15000. PACKET_ZC_CASH_ITEM_DELETE p = {};
  15001. p.packetType = HEADER_ZC_CASH_ITEM_DELETE;
  15002. p.index = client_index( index );
  15003. p.itemId = client_nameid( nameid );
  15004. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15005. }
  15006. /// Book Reading (ZC_READ_BOOK).
  15007. /// 0294 <book id>.L <page>.L
  15008. void clif_readbook(int fd, int book_id, int page)
  15009. {
  15010. WFIFOHEAD(fd,packet_len(0x294));
  15011. WFIFOW(fd,0) = 0x294;
  15012. WFIFOL(fd,2) = book_id;
  15013. WFIFOL(fd,6) = page;
  15014. WFIFOSET(fd,packet_len(0x294));
  15015. }
  15016. /// Battlegrounds
  15017. ///
  15018. /// Updates HP bar of a camp member.
  15019. /// 02e0 <account id>.L <name>.24B <hp>.W <max hp>.W (ZC_BATTLEFIELD_NOTIFY_HP)
  15020. /// 0a0e <account id>.L <hp>.L <max hp>.L (ZC_BATTLEFIELD_NOTIFY_HP2)
  15021. void clif_bg_hp(map_session_data *sd) {
  15022. #if PACKETVER < 20140613
  15023. unsigned char buf[34];
  15024. const int cmd = 0x2e0;
  15025. #else
  15026. unsigned char buf[14];
  15027. const int cmd = 0xa0e;
  15028. #endif
  15029. nullpo_retv(sd);
  15030. WBUFW(buf,0) = cmd;
  15031. WBUFL(buf,2) = sd->status.account_id;
  15032. #if PACKETVER < 20140613
  15033. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  15034. if( sd->battle_status.max_hp > INT16_MAX ) { // To correctly display the %hp bar. [Skotlex]
  15035. WBUFW(buf,30) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  15036. WBUFW(buf,32) = 100;
  15037. } else {
  15038. WBUFW(buf,30) = sd->battle_status.hp;
  15039. WBUFW(buf,32) = sd->battle_status.max_hp;
  15040. }
  15041. #else
  15042. if( sd->battle_status.max_hp > INT32_MAX ) {
  15043. WBUFL(buf,6) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  15044. WBUFL(buf,10) = 100;
  15045. } else {
  15046. WBUFL(buf,6) = sd->battle_status.hp;
  15047. WBUFL(buf,10) = sd->battle_status.max_hp;
  15048. }
  15049. #endif
  15050. clif_send(buf, packet_len(cmd), &sd->bl, BG_AREA_WOS);
  15051. }
  15052. /// Updates the position of a camp member on the minimap (ZC_BATTLEFIELD_NOTIFY_POSITION).
  15053. /// 02df <account id>.L <name>.24B <class>.W <x>.W <y>.W
  15054. void clif_bg_xy(map_session_data *sd)
  15055. {
  15056. unsigned char buf[36];
  15057. nullpo_retv(sd);
  15058. WBUFW(buf,0)=0x2df;
  15059. WBUFL(buf,2)=sd->status.account_id;
  15060. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  15061. WBUFW(buf,30)=sd->status.class_;
  15062. WBUFW(buf,32)=sd->bl.x;
  15063. WBUFW(buf,34)=sd->bl.y;
  15064. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  15065. }
  15066. void clif_bg_xy_remove(map_session_data *sd)
  15067. {
  15068. unsigned char buf[36];
  15069. nullpo_retv(sd);
  15070. WBUFW(buf,0)=0x2df;
  15071. WBUFL(buf,2)=sd->status.account_id;
  15072. memset(WBUFP(buf,6), 0, NAME_LENGTH);
  15073. WBUFW(buf,30)=0;
  15074. WBUFW(buf,32)=-1;
  15075. WBUFW(buf,34)=-1;
  15076. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  15077. }
  15078. /// Notifies clients of a battleground message.
  15079. /// 02DC <packet len>.W <account id>.L <name>.24B <message>.?B (ZC_BATTLEFIELD_CHAT)
  15080. void clif_bg_message( struct s_battleground_data *bg, int src_id, const char *name, const char *mes, size_t len ){
  15081. map_session_data *sd = bg_getavailablesd( bg );
  15082. if( sd == nullptr ){
  15083. return;
  15084. }
  15085. // limit length
  15086. len = zmin( len + 1, CHAT_SIZE_MAX );
  15087. unsigned char buf[8 + NAME_LENGTH + CHAT_SIZE_MAX];
  15088. WBUFW(buf,0) = 0x2dc;
  15089. WBUFW(buf,2) = static_cast<int16>( len + NAME_LENGTH + 8 );
  15090. WBUFL(buf,4) = src_id;
  15091. safestrncpy(WBUFCP(buf,8), name, NAME_LENGTH);
  15092. safestrncpy(WBUFCP(buf,8+NAME_LENGTH), mes, len );
  15093. clif_send(buf,WBUFW(buf,2), &sd->bl, BG);
  15094. }
  15095. /// Validates and processes battlechat messages.
  15096. /// All messages that are sent after enabling battleground chat with /battlechat.
  15097. /// 02DB <packet len>.W <text>.?B (CZ_BATTLEFIELD_CHAT)
  15098. void clif_parse_BattleChat(int fd, map_session_data* sd){
  15099. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  15100. if( !clif_process_message( sd, false, name, message, output ) )
  15101. return;
  15102. bg_send_message(sd, output, strlen(output) );
  15103. }
  15104. /// Notifies client of a battleground score change (ZC_BATTLEFIELD_NOTIFY_POINT).
  15105. /// 02de <camp A points>.W <camp B points>.W
  15106. void clif_bg_updatescore(int16 m)
  15107. {
  15108. struct block_list bl;
  15109. unsigned char buf[6];
  15110. struct map_data *mapdata = map_getmapdata(m);
  15111. bl.id = 0;
  15112. bl.type = BL_NUL;
  15113. bl.m = m;
  15114. WBUFW(buf,0) = 0x2de;
  15115. WBUFW(buf,2) = mapdata->bgscore_lion;
  15116. WBUFW(buf,4) = mapdata->bgscore_eagle;
  15117. clif_send(buf,packet_len(0x2de),&bl,ALL_SAMEMAP);
  15118. }
  15119. void clif_bg_updatescore_single(map_session_data *sd)
  15120. {
  15121. int fd;
  15122. nullpo_retv(sd);
  15123. fd = sd->fd;
  15124. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  15125. WFIFOHEAD(fd,packet_len(0x2de));
  15126. WFIFOW(fd,0) = 0x2de;
  15127. WFIFOW(fd,2) = mapdata->bgscore_lion;
  15128. WFIFOW(fd,4) = mapdata->bgscore_eagle;
  15129. WFIFOSET(fd,packet_len(0x2de));
  15130. }
  15131. /// Battleground camp belong-information (ZC_BATTLEFIELD_NOTIFY_CAMPINFO).
  15132. /// 02dd <account id>.L <name>.24B <camp>.W
  15133. void clif_sendbgemblem_area(map_session_data *sd)
  15134. {
  15135. unsigned char buf[33];
  15136. nullpo_retv(sd);
  15137. WBUFW(buf, 0) = 0x2dd;
  15138. WBUFL(buf,2) = sd->bl.id;
  15139. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH); // name don't show in screen.
  15140. WBUFW(buf,30) = sd->bg_id;
  15141. clif_send(buf,packet_len(0x2dd), &sd->bl, AREA);
  15142. }
  15143. void clif_sendbgemblem_single(int fd, map_session_data *sd)
  15144. {
  15145. nullpo_retv(sd);
  15146. WFIFOHEAD(fd,32);
  15147. WFIFOW(fd,0) = 0x2dd;
  15148. WFIFOL(fd,2) = sd->bl.id;
  15149. safestrncpy(WFIFOCP(fd,6), sd->status.name, NAME_LENGTH);
  15150. WFIFOW(fd,30) = sd->bg_id;
  15151. WFIFOSET(fd,packet_len(0x2dd));
  15152. }
  15153. /// Battlegrounds queue incoming apply request from client.
  15154. /// Queue types: 1 solo queue, 2 party queue, 4 guild queue.
  15155. /// 0x8d7 <queue type>.W <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_APPLY)
  15156. void clif_parse_bg_queue_apply_request(int fd, map_session_data *sd)
  15157. {
  15158. if (!battle_config.feature_bgqueue)
  15159. return;
  15160. nullpo_retv(sd);
  15161. short type = RFIFOW(fd,2);
  15162. char name[NAME_LENGTH];
  15163. safestrncpy(name, RFIFOCP(fd, 4), NAME_LENGTH);
  15164. if (sd->bg_queue_id > 0) {
  15165. //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);
  15166. clif_bg_queue_apply_result(BG_APPLY_DUPLICATE, name, sd); // Duplicate application warning
  15167. return;
  15168. } else if (type == 1) // Solo
  15169. bg_queue_join_solo(name, sd);
  15170. else if (type == 2) // Party
  15171. bg_queue_join_party(name, sd);
  15172. else if (type == 4) // Guild
  15173. bg_queue_join_guild(name, sd);
  15174. else {
  15175. 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);
  15176. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd); // Someone sent an invalid queue type packet
  15177. return;
  15178. }
  15179. }
  15180. /// Outgoing battlegrounds queue apply result.
  15181. /// Result types: @see e_bg_queue_apply_ack
  15182. /// 0x8d8 <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_APPLY)
  15183. void clif_bg_queue_apply_result(e_bg_queue_apply_ack result, const char *name, map_session_data *sd)
  15184. {
  15185. nullpo_retv(sd);
  15186. int fd = sd->fd;
  15187. WFIFOHEAD(fd, packet_len(0x8d8));
  15188. WFIFOW(fd,0) = 0x8d8;
  15189. WFIFOB(fd,2) = result;
  15190. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15191. WFIFOSET(fd, packet_len(0x8d8));
  15192. }
  15193. /// Outgoing battlegrounds queue apply notification.
  15194. /// Sends a notification at the start of the battlegrounds queue and is also used to update the queue number.
  15195. /// 0x8d9 <battleground name>.24B <queue number>.L (ZC_NOTIFY_ENTRY_QUEUE_APPLY)
  15196. void clif_bg_queue_apply_notify(const char *name, map_session_data *sd)
  15197. {
  15198. nullpo_retv(sd);
  15199. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15200. if (queue == nullptr) {
  15201. ShowError("clif_bg_queue_apply_notify: Player is not in a battleground queue.\n");
  15202. return;
  15203. }
  15204. int fd = sd->fd;
  15205. WFIFOHEAD(fd, packet_len(0x8d9));
  15206. WFIFOW(fd,0) = 0x8d9;
  15207. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15208. WFIFOL(fd,2+NAME_LENGTH) = static_cast<int32>( queue->teama_members.size() + queue->teamb_members.size() );
  15209. WFIFOSET(fd, packet_len(0x8d9));
  15210. }
  15211. /// Battlegrounds queue outgoing cancel result.
  15212. /// 0x8db <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_CANCEL)
  15213. void clif_bg_queue_cancel_result(bool success, const char *name, map_session_data *sd)
  15214. {
  15215. nullpo_retv(sd);
  15216. int fd = sd->fd;
  15217. WFIFOHEAD(fd, packet_len(0x8d8));
  15218. WFIFOW(fd,0) = 0x8db;
  15219. WFIFOB(fd,2) = success;
  15220. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15221. WFIFOSET(fd, packet_len(0x8d8));
  15222. }
  15223. /// Battlegrounds queue incoming cancel request from client.
  15224. /// 0x8da <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_CANCEL)
  15225. void clif_parse_bg_queue_cancel_request(int fd, map_session_data *sd)
  15226. {
  15227. if (!battle_config.feature_bgqueue)
  15228. return;
  15229. nullpo_retv(sd);
  15230. bool success;
  15231. if (sd->bg_queue_id > 0) {
  15232. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15233. if (queue && queue->state == QUEUE_STATE_SETUP_DELAY)
  15234. 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.
  15235. else
  15236. success = bg_queue_leave(sd);
  15237. } else {
  15238. 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);
  15239. success = false;
  15240. }
  15241. char name[NAME_LENGTH];
  15242. safestrncpy( name, RFIFOCP( fd, 2 ), NAME_LENGTH );
  15243. clif_bg_queue_cancel_result(success, name, sd);
  15244. }
  15245. /// Battleground is ready to be joined, send a window asking for players to accept or decline.
  15246. /// 0x8df <battleground name>.24B <lobby name>.24B (ZC_NOTIFY_LOBBY_ADMISSION)
  15247. void clif_bg_queue_lobby_notify(const char *name, map_session_data *sd)
  15248. {
  15249. nullpo_retv(sd);
  15250. int fd = sd->fd;
  15251. WFIFOHEAD(fd, packet_len(0x8df));
  15252. WFIFOW(fd,0) = 0x8df;
  15253. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15254. safestrncpy(WFIFOCP(fd,2+NAME_LENGTH), name, NAME_LENGTH);
  15255. WFIFOSET(fd, packet_len(0x8df));
  15256. }
  15257. /// Incoming packet from client telling server whether player wants to enter battleground or cancel.
  15258. /// Result types: 1(Accept), 2(Decline).
  15259. /// 0x8e0 <result>.B <battleground name>.24B <lobby name>.24B (CZ_REPLY_LOBBY_ADMISSION)
  15260. void clif_parse_bg_queue_lobby_reply(int fd, map_session_data *sd)
  15261. {
  15262. nullpo_retv(sd);
  15263. if(sd->bg_queue_id > 0) {
  15264. uint8 result = RFIFOB(fd, 2);
  15265. if(result == 1) { // Accept
  15266. bg_queue_on_accept_invite(sd);
  15267. } else if(result == 2) { // Decline
  15268. bg_queue_leave(sd);
  15269. clif_bg_queue_entry_init(sd);
  15270. }
  15271. }
  15272. }
  15273. /// Plays a gong sound, signaling that someone has accepted the invite to enter a battleground.
  15274. /// 0x8e1 <result>.B <battleground name>.24B <lobby name>.24B (ZC_REPLY_ACK_LOBBY_ADMISSION)
  15275. void clif_bg_queue_ack_lobby(bool result, const char *name, const char *lobbyname, map_session_data *sd)
  15276. {
  15277. nullpo_retv(sd);
  15278. int fd = sd->fd;
  15279. WFIFOHEAD(fd, packet_len(0x8e1));
  15280. WFIFOW(fd,0) = 0x8e1;
  15281. WFIFOB(fd,2) = result;
  15282. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15283. safestrncpy(WFIFOCP(fd,3+NAME_LENGTH), lobbyname, NAME_LENGTH);
  15284. WFIFOSET(fd, packet_len(0x8e1));
  15285. }
  15286. /// Battlegrounds queue incoming queue number request from client.
  15287. /// 0x90a <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_RANKING)
  15288. void clif_parse_bg_queue_request_queue_number(int fd, map_session_data *sd)
  15289. {
  15290. nullpo_retv(sd);
  15291. char name[NAME_LENGTH];
  15292. safestrncpy( name, RFIFOCP(fd, 2), NAME_LENGTH );
  15293. clif_bg_queue_apply_notify(name, sd);
  15294. }
  15295. /// Silently removes all the battlegrounds stuff client side so that you will open the first BG window when you press battle on the interface.
  15296. /// Send this when a player joins a battleground so that it will remove all the queue stuff upon warping in.
  15297. /// 0x90e (ZC_ENTRY_QUEUE_INIT)
  15298. void clif_bg_queue_entry_init(map_session_data *sd)
  15299. {
  15300. nullpo_retv(sd);
  15301. PACKET_ZC_ENTRY_QUEUE_INIT p = {};
  15302. p.packetType = HEADER_ZC_ENTRY_QUEUE_INIT;
  15303. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15304. }
  15305. /// Custom Fonts (ZC_NOTIFY_FONT).
  15306. /// 02ef <account_id>.L <font id>.W
  15307. void clif_font(map_session_data *sd)
  15308. {
  15309. #if PACKETVER >= 20080102
  15310. unsigned char buf[8];
  15311. nullpo_retv(sd);
  15312. WBUFW(buf,0) = 0x2ef;
  15313. WBUFL(buf,2) = sd->bl.id;
  15314. WBUFW(buf,6) = sd->status.font;
  15315. clif_send(buf, packet_len(0x2ef), &sd->bl, AREA);
  15316. #endif
  15317. }
  15318. /// Required to start the instancing information window on Client
  15319. /// This window re-appears each "refresh" of client automatically until the keep_limit reaches 0.
  15320. /// S 0x2cb <Instance name>.61B <Standby Position>.W
  15321. void clif_instance_create( int instance_id, size_t num ){
  15322. #if PACKETVER >= 20071128
  15323. map_session_data *sd = nullptr;
  15324. enum send_target target = PARTY;
  15325. unsigned char buf[65];
  15326. instance_getsd(instance_id, sd, &target);
  15327. if (!sd)
  15328. return;
  15329. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15330. if (!db)
  15331. return;
  15332. WBUFW(buf,0) = 0x2cb;
  15333. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15334. WBUFW( buf, 63 ) = static_cast<int16>( num );
  15335. clif_send(buf,packet_len(0x2cb),&sd->bl,target);
  15336. #endif
  15337. }
  15338. /// To announce Instancing queue creation if no maps available
  15339. /// S 0x2cc <Standby Position>.W
  15340. void clif_instance_changewait(int instance_id, int num)
  15341. {
  15342. #if PACKETVER >= 20071128
  15343. map_session_data *sd = nullptr;
  15344. enum send_target target = PARTY;
  15345. unsigned char buf[4];
  15346. instance_getsd(instance_id, sd, &target);
  15347. if (!sd)
  15348. return;
  15349. WBUFW(buf,0) = 0x2cc;
  15350. WBUFW(buf,2) = num;
  15351. clif_send(buf,packet_len(0x2cc),&sd->bl,target);
  15352. #endif
  15353. return;
  15354. }
  15355. /// Notify the current status to members
  15356. /// S 0x2cd <Instance Name>.61B <Instance Remaining Time>.L <Instance Noplayers close time>.L
  15357. void clif_instance_status(int instance_id, uint32 limit1, uint32 limit2)
  15358. {
  15359. #if PACKETVER >= 20071128
  15360. map_session_data *sd = nullptr;
  15361. enum send_target target = PARTY;
  15362. unsigned char buf[71];
  15363. instance_getsd(instance_id, sd, &target);
  15364. if (!sd)
  15365. return;
  15366. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15367. if (!db)
  15368. return;
  15369. WBUFW(buf,0) = 0x2cd;
  15370. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15371. WBUFL(buf,63) = limit1;
  15372. WBUFL(buf,67) = limit2;
  15373. clif_send(buf,packet_len(0x2cd),&sd->bl,target);
  15374. #endif
  15375. return;
  15376. }
  15377. /// Notify a status change to members
  15378. /// S 0x2ce <Message ID>.L
  15379. /// 0 = Notification (EnterLimitDate update?)
  15380. /// 1 = The Memorial Dungeon expired; it has been destroyed
  15381. /// 2 = The Memorial Dungeon's entry time limit expired; it has been destroyed
  15382. /// 3 = The Memorial Dungeon has been removed.
  15383. /// 4 = Create failure (removes the instance window)
  15384. void clif_instance_changestatus(int instance_id, e_instance_notify type, uint32 limit)
  15385. {
  15386. #if PACKETVER >= 20071128
  15387. map_session_data *sd = nullptr;
  15388. enum send_target target = PARTY;
  15389. unsigned char buf[10];
  15390. instance_getsd(instance_id, sd, &target);
  15391. if (!sd)
  15392. return;
  15393. WBUFW(buf,0) = 0x2ce;
  15394. WBUFL(buf,2) = type;
  15395. WBUFL(buf,6) = limit;
  15396. clif_send(buf,packet_len(0x2ce),&sd->bl,target);
  15397. #endif
  15398. return;
  15399. }
  15400. /// Destroy an instance from the status window
  15401. /// 02cf <command>.L (CZ_MEMORIALDUNGEON_COMMAND)
  15402. void clif_parse_MemorialDungeonCommand(int fd, map_session_data *sd)
  15403. {
  15404. if (pc_istrading(sd) || pc_isdead(sd) || map_getmapdata(sd->bl.m)->instance_id > 0)
  15405. return;
  15406. const PACKET_CZ_MEMORIALDUNGEON_COMMAND* p = reinterpret_cast<PACKET_CZ_MEMORIALDUNGEON_COMMAND*>( RFIFOP( fd, 0 ) );
  15407. switch (p->command) {
  15408. case COMMAND_MEMORIALDUNGEON_DESTROY_FORCE:
  15409. instance_destroy_command(sd);
  15410. break;
  15411. }
  15412. }
  15413. void clif_instance_info( map_session_data& sd ){
  15414. if( sd.instance_id > 0 ){
  15415. instance_reqinfo( &sd, sd.instance_id );
  15416. }
  15417. if( sd.status.party_id > 0 ){
  15418. struct party_data* p = party_search( sd.status.party_id );
  15419. if( p != nullptr && p->instance_id > 0 ){
  15420. instance_reqinfo( &sd, p->instance_id );
  15421. }
  15422. }
  15423. if( sd.guild != nullptr && sd.guild->instance_id > 0 ){
  15424. instance_reqinfo( &sd, sd.guild->instance_id );
  15425. }
  15426. if( sd.clan != nullptr && sd.clan->instance_id > 0 ){
  15427. instance_reqinfo( &sd, sd.clan->instance_id );
  15428. }
  15429. }
  15430. /// Notifies clients about item picked up by a party member.
  15431. /// 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)
  15432. void clif_party_show_picker( map_session_data* sd, struct item* item_data ){
  15433. #if PACKETVER >= 20071002
  15434. nullpo_retv( sd );
  15435. nullpo_retv( item_data );
  15436. struct item_data* id = itemdb_search( item_data->nameid );
  15437. PACKET_ZC_ITEM_PICKUP_PARTY p = {};
  15438. p.packetType = HEADER_ZC_ITEM_PICKUP_PARTY;
  15439. p.AID = sd->status.account_id;
  15440. p.itemId = client_nameid( item_data->nameid );
  15441. p.identified = item_data->identify;
  15442. p.damaged = item_data->attribute;
  15443. p.refine = item_data->refine;
  15444. clif_addcards( &p.slot, item_data );
  15445. p.location = id->equip; // equip location
  15446. p.itemType = itemtype( id->nameid ); // item type
  15447. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15448. p.grade = item_data->enchantgrade;
  15449. #endif
  15450. clif_send( &p, sizeof( p ), &sd->bl, PARTY_SAMEMAP_WOS );
  15451. #endif
  15452. }
  15453. /** Display gained exp.
  15454. * 07f6 <account id>.L <amount>.L <var id>.W <exp type>.W (ZC_NOTIFY_EXP)
  15455. * 0acc <account id>.L <amount>.Q <var id>.W <exp type>.W (ZC_NOTIFY_EXP2)
  15456. * amount: INT32_MIN ~ INT32_MAX
  15457. * var id:
  15458. * SP_BASEEXP, SP_JOBEXP
  15459. * exp type:
  15460. * 0 = normal exp gained/lost
  15461. * 1 = quest exp gained/lost
  15462. * @param sd Player
  15463. * @param exp EXP value gained/loss
  15464. * @param type SP_BASEEXP, SP_JOBEXP
  15465. * @param quest False:Normal EXP; True:Quest EXP (displayed in purple color)
  15466. * @param lost True:if losing EXP
  15467. */
  15468. void clif_displayexp(map_session_data *sd, t_exp exp, char type, bool quest, bool lost)
  15469. {
  15470. int fd;
  15471. int offset;
  15472. #if PACKETVER >= 20170830
  15473. int cmd = 0xacc;
  15474. #else
  15475. int cmd = 0x7f6;
  15476. #endif
  15477. nullpo_retv(sd);
  15478. fd = sd->fd;
  15479. WFIFOHEAD(fd, packet_len(cmd));
  15480. WFIFOW(fd,0) = cmd;
  15481. WFIFOL(fd,2) = sd->bl.id;
  15482. #if PACKETVER >= 20170830
  15483. WFIFOQ(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15484. offset = 4;
  15485. #else
  15486. WFIFOL(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15487. offset = 0;
  15488. #endif
  15489. WFIFOW(fd,10+offset) = type;
  15490. WFIFOW(fd,12+offset) = (quest && type != SP_JOBEXP) ? 1 : 0; // NOTE: Somehow JobEXP always in yellow color
  15491. WFIFOSET(fd,packet_len(cmd));
  15492. }
  15493. /// Displays digital clock digits on top of the screen (ZC_SHOWDIGIT).
  15494. /// type:
  15495. /// 0 = Displays 'value' for 5 seconds.
  15496. /// 1 = Incremental counter (1 tick/second), negated 'value' specifies start value (e.g. using -10 lets the counter start at 10).
  15497. /// 2 = Decremental counter (1 tick/second), negated 'value' specifies start value (does not stop when reaching 0, but overflows).
  15498. /// 3 = Decremental counter (1 tick/second), 'value' specifies start value (stops when reaching 0, displays at most 2 digits).
  15499. /// value:
  15500. /// 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).
  15501. void clif_showdigit(map_session_data* sd, unsigned char type, int value)
  15502. {
  15503. WFIFOHEAD(sd->fd, packet_len(0x1b1));
  15504. WFIFOW(sd->fd,0) = 0x1b1;
  15505. WFIFOB(sd->fd,2) = type;
  15506. WFIFOL(sd->fd,3) = value;
  15507. WFIFOSET(sd->fd, packet_len(0x1b1));
  15508. }
  15509. /// Notification of the state of client command /effect (CZ_LESSEFFECT).
  15510. /// 021d <state>.L
  15511. /// state:
  15512. /// 0 = Full effects
  15513. /// 1 = Reduced effects
  15514. ///
  15515. /// NOTE: The state is used on Aegis for sending skill unit packet
  15516. /// 0x11f (ZC_SKILL_ENTRY) instead of 0x1c9 (ZC_SKILL_ENTRY2)
  15517. /// whenever possible. Due to the way the decision check is
  15518. /// constructed, this state tracking was rendered useless,
  15519. /// as the only skill unit, that is sent with 0x1c9 is
  15520. /// Graffiti.
  15521. void clif_parse_LessEffect(int fd, map_session_data* sd){
  15522. int isLess = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  15523. sd->state.lesseffect = ( isLess != 0 );
  15524. }
  15525. /// 07e4 <length>.w <option>.l <val>.l {<index>.w <amount>.w).4b* (CZ_ITEMLISTWIN_RES)
  15526. void clif_parse_ItemListWindowSelected(int fd, map_session_data* sd) {
  15527. if( sd == nullptr ){
  15528. return;
  15529. }
  15530. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  15531. int n = (RFIFOW(fd,info->pos[0])-12) / 4;
  15532. int type = RFIFOL(fd,info->pos[1]);
  15533. int flag = RFIFOL(fd,info->pos[2]); // Button clicked: 0 = Cancel, 1 = OK
  15534. unsigned short* item_list = (unsigned short*)RFIFOP(fd,info->pos[3]);
  15535. if( sd->state.trading || sd->npc_shopid )
  15536. return;
  15537. if( flag == 0 || n == 0) {
  15538. clif_menuskill_clear(sd);
  15539. return; // Canceled by player.
  15540. }
  15541. if( sd->menuskill_id != SO_EL_ANALYSIS && sd->menuskill_id != GN_CHANGEMATERIAL ) {
  15542. clif_menuskill_clear(sd);
  15543. return; // Prevent hacking.
  15544. }
  15545. switch( type ) {
  15546. case 0: // Change Material
  15547. skill_changematerial(sd,n,item_list);
  15548. break;
  15549. case 1: // Level 1: Pure to Rough
  15550. case 2: // Level 2: Rough to Pure
  15551. skill_elementalanalysis(*sd,n,type,item_list);
  15552. break;
  15553. }
  15554. clif_menuskill_clear(sd);
  15555. return;
  15556. }
  15557. /*==========================================
  15558. * Elemental System
  15559. *==========================================*/
  15560. /// Notifies client of a change in an elemental's status parameter.
  15561. /// 0x81e <type>.W <value>.L (ZC_EL_PAR_CHANGE)
  15562. void clif_elemental_updatestatus(map_session_data& sd, _sp type) {
  15563. #if PACKETVER >= 20100309
  15564. if (sd.ed == nullptr)
  15565. return;
  15566. PACKET_ZC_EL_PAR_CHANGE p = {};
  15567. p.packetType = HEADER_ZC_EL_PAR_CHANGE;
  15568. p.type = static_cast<decltype(p.type)>(type);
  15569. status_data* status = &sd.ed->battle_status;
  15570. switch( type ) {
  15571. case SP_HP:
  15572. p.value = static_cast<decltype(p.value)>(status->hp);
  15573. break;
  15574. case SP_MAXHP:
  15575. p.value = static_cast<decltype(p.value)>(status->max_hp);
  15576. break;
  15577. case SP_SP:
  15578. p.value = static_cast<decltype(p.value)>(status->sp);
  15579. break;
  15580. case SP_MAXSP:
  15581. p.value = static_cast<decltype(p.value)>(status->max_sp);
  15582. break;
  15583. }
  15584. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  15585. #endif
  15586. }
  15587. void clif_elemental_info(map_session_data *sd) {
  15588. int fd;
  15589. s_elemental_data *ed;
  15590. struct status_data *status;
  15591. if( !clif_session_isValid(sd) || (ed = sd->ed) == nullptr )
  15592. return;
  15593. fd = sd->fd;
  15594. status = &ed->battle_status;
  15595. WFIFOHEAD(fd,22);
  15596. WFIFOW(fd, 0) = 0x81d;
  15597. WFIFOL(fd, 2) = ed->bl.id;
  15598. WFIFOL(fd, 6) = status->hp;
  15599. WFIFOL(fd,10) = status->max_hp;
  15600. WFIFOL(fd,14) = status->sp;
  15601. WFIFOL(fd,18) = status->max_sp;
  15602. WFIFOSET(fd,22);
  15603. }
  15604. /// Buying Store System
  15605. ///
  15606. /// Opens preparation window for buying store (ZC_OPEN_BUYING_STORE).
  15607. /// 0810 <slots>.B
  15608. void clif_buyingstore_open(map_session_data* sd)
  15609. {
  15610. int fd = sd->fd;
  15611. WFIFOHEAD(fd,packet_len(0x810));
  15612. WFIFOW(fd,0) = 0x810;
  15613. WFIFOB(fd,2) = sd->buyingstore.slots;
  15614. WFIFOSET(fd,packet_len(0x810));
  15615. }
  15616. /// Request to create a buying store.
  15617. /// 0811 <packet len>.W <limit zeny>.L <result>.B <store name>.80B { <name id>.W <amount>.W <price>.L }* (CZ_REQ_OPEN_BUYING_STORE)
  15618. /// result:
  15619. /// 0 = cancel
  15620. /// 1 = open
  15621. static void clif_parse_ReqOpenBuyingStore( int fd, map_session_data* sd ){
  15622. const PACKET_CZ_REQ_OPEN_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15623. // TODO: Make this check global for all variable length packets.
  15624. // minimum packet length
  15625. if( p->packetLength < sizeof( *p ) ){
  15626. ShowError( "clif_parse_ReqOpenBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15627. return;
  15628. }
  15629. if (map_getmapflag(sd->bl.m, MF_NOBUYINGSTORE)) {
  15630. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  15631. return;
  15632. }
  15633. if (map_getcell(sd->bl.m, sd->bl.x, sd->bl.y, CELL_CHKNOBUYINGSTORE)) {
  15634. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  15635. return;
  15636. }
  15637. char storename[MESSAGE_SIZE];
  15638. safestrncpy( storename, p->storeName, sizeof( storename ) );
  15639. // so that buyingstore_create knows, how many elements it has access to
  15640. int packet_len = p->packetLength - sizeof( *p );
  15641. if( packet_len % sizeof( p->items[0] ) ){
  15642. 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] ) );
  15643. return;
  15644. }
  15645. buyingstore_create( sd, p->zenyLimit, p->result, storename, p->items, packet_len / sizeof( p->items[0] ), nullptr );
  15646. }
  15647. /// Notification, that the requested buying store could not be created (ZC_FAILED_OPEN_BUYING_STORE_TO_BUYER).
  15648. /// 0812 <result>.W <total weight>.L
  15649. /// result:
  15650. /// 1 = "Failed to open buying store." (0x6cd, MSI_BUYINGSTORE_OPEN_FAILED)
  15651. /// 2 = "Total amount of then possessed items exceeds the weight limit by <weight/10-maxweight*90%>. Please re-enter." (0x6ce, MSI_BUYINGSTORE_OVERWEIGHT)
  15652. /// 8 = "No sale (purchase) information available." (0x705)
  15653. /// ? = nothing
  15654. void clif_buyingstore_open_failed(map_session_data* sd, unsigned short result, uint32 weight)
  15655. {
  15656. int fd = sd->fd;
  15657. WFIFOHEAD(fd,packet_len(0x812));
  15658. WFIFOW(fd,0) = 0x812;
  15659. WFIFOW(fd,2) = result;
  15660. WFIFOL(fd,4) = weight;
  15661. WFIFOSET(fd,packet_len(0x812));
  15662. }
  15663. /// Notification, that the requested buying store was created.
  15664. /// 0813 <packet len>.W <account id>.L <limit zeny>.L { <price>.L <count>.W <type>.B <name id>.W }* (ZC_MYITEMLIST_BUYING_STORE)
  15665. void clif_buyingstore_myitemlist( map_session_data& sd ){
  15666. #if PACKETVER >= 20100309
  15667. PACKET_ZC_MYITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_MYITEMLIST_BUYING_STORE*>( packet_buffer );
  15668. p->packetType = HEADER_ZC_MYITEMLIST_BUYING_STORE;
  15669. p->packetLength = sizeof( *p );
  15670. p->AID = sd.bl.id;
  15671. p->zenyLimit = sd.buyingstore.zenylimit;
  15672. for( int i = 0; i < sd.buyingstore.slots; i++ ){
  15673. PACKET_ZC_MYITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15674. entry.price = sd.buyingstore.items[i].price;
  15675. entry.amount = sd.buyingstore.items[i].amount;
  15676. entry.itemType = itemtype( sd.buyingstore.items[i].nameid );
  15677. entry.itemId = client_nameid( sd.buyingstore.items[i].nameid );
  15678. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15679. }
  15680. clif_send( p, p->packetLength, &sd.bl, SELF );
  15681. #endif
  15682. }
  15683. /// Notifies clients in area of a buying store (ZC_BUYING_STORE_ENTRY).
  15684. /// 0814 <account id>.L <store name>.80B
  15685. void clif_buyingstore_entry( map_session_data& sd, struct block_list* tbl ){
  15686. #if PACKETVER >= 20100420
  15687. enum send_target target;
  15688. if( tbl == nullptr ){
  15689. tbl = &sd.bl;
  15690. target = AREA_WOS;
  15691. }else{
  15692. target = SELF;
  15693. }
  15694. PACKET_ZC_BUYING_STORE_ENTRY p = {};
  15695. p.packetType = HEADER_ZC_BUYING_STORE_ENTRY;
  15696. p.makerAID = sd.status.account_id;
  15697. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  15698. clif_send( &p, sizeof( p ), tbl, target );
  15699. #endif
  15700. }
  15701. /// Request to close own buying store (CZ_REQ_CLOSE_BUYING_STORE).
  15702. /// 0815
  15703. static void clif_parse_ReqCloseBuyingStore(int fd, map_session_data* sd)
  15704. {
  15705. buyingstore_close(sd);
  15706. }
  15707. /// Notifies clients in area that a buying store was closed (ZC_DISAPPEAR_BUYING_STORE_ENTRY).
  15708. /// 0816 <account id>.L
  15709. void clif_buyingstore_disappear_entry( map_session_data& sd, struct block_list* tbl ){
  15710. #if PACKETVER >= 20100309
  15711. enum send_target target;
  15712. if( tbl == nullptr ){
  15713. tbl = &sd.bl;
  15714. target = AREA_WOS;
  15715. }else{
  15716. target = SELF;
  15717. }
  15718. PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY p = {};
  15719. p.packetType = HEADER_ZC_DISAPPEAR_BUYING_STORE_ENTRY;
  15720. p.makerAID = sd.status.account_id;
  15721. clif_send( &p, sizeof( p ), tbl, target );
  15722. #endif
  15723. }
  15724. /// Request to open someone else's buying store (CZ_REQ_CLICK_TO_BUYING_STORE).
  15725. /// 0817 <account id>.L
  15726. static void clif_parse_ReqClickBuyingStore(int fd, map_session_data* sd)
  15727. {
  15728. uint32 account_id;
  15729. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  15730. buyingstore_open(sd, account_id);
  15731. }
  15732. /// Sends buying store item list.
  15733. /// 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)
  15734. void clif_buyingstore_itemlist( map_session_data& sd, map_session_data& pl_sd ){
  15735. #if PACKETVER >= 20100309
  15736. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_ACK_ITEMLIST_BUYING_STORE*>( packet_buffer );
  15737. p->packetType = HEADER_ZC_ACK_ITEMLIST_BUYING_STORE;
  15738. p->packetLength = sizeof( *p );
  15739. p->AID = pl_sd.bl.id;
  15740. p->storeId = pl_sd.buyer_id;
  15741. p->zenyLimit = pl_sd.buyingstore.zenylimit;
  15742. for( int i = 0; i < pl_sd.buyingstore.slots; i++ ){
  15743. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15744. entry.price = pl_sd.buyingstore.items[i].price;
  15745. entry.amount = pl_sd.buyingstore.items[i].amount; // TODO: Figure out, if no longer needed items (amount == 0) are listed on official.
  15746. entry.itemType = itemtype(pl_sd.buyingstore.items[i].nameid);
  15747. entry.itemId = client_nameid( pl_sd.buyingstore.items[i].nameid );
  15748. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15749. }
  15750. clif_send( p, p->packetLength, &sd.bl, SELF );
  15751. #endif
  15752. }
  15753. /// Request to sell items to a buying store.
  15754. /// 0819 <packet len>.W <account id>.L <store id>.L { <index>.W <name id>.W <amount>.W }* (CZ_REQ_TRADE_BUYING_STORE)
  15755. static void clif_parse_ReqTradeBuyingStore( int fd, map_session_data* sd ){
  15756. const PACKET_CZ_REQ_TRADE_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_TRADE_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15757. // minimum packet length
  15758. if( p->packetLength < sizeof( *p ) ){
  15759. ShowError( "clif_parse_ReqTradeBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15760. return;
  15761. }
  15762. // so that buyingstore_trade knows, how many elements it has access to
  15763. int packet_len = p->packetLength - sizeof( *p );
  15764. if( packet_len % sizeof( p->items[0] ) ){
  15765. 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] ) );
  15766. return;
  15767. }
  15768. buyingstore_trade( sd, p->AID, p->storeId, p->items, packet_len / sizeof( p->items[0] ) );
  15769. }
  15770. /// Notifies the buyer, that the buying store has been closed due to a post-trade condition (ZC_FAILED_TRADE_BUYING_STORE_TO_BUYER).
  15771. /// 081a <result>.W
  15772. /// result:
  15773. /// 3 = "All items within the buy limit were purchased." (0x6cf, MSI_BUYINGSTORE_TRADE_OVERLIMITZENY)
  15774. /// 4 = "All items were purchased." (0x6d0, MSI_BUYINGSTORE_TRADE_BUYCOMPLETE)
  15775. /// ? = nothing
  15776. void clif_buyingstore_trade_failed_buyer(map_session_data* sd, short result)
  15777. {
  15778. int fd = sd->fd;
  15779. WFIFOHEAD(fd,packet_len(0x81a));
  15780. WFIFOW(fd,0) = 0x81a;
  15781. WFIFOW(fd,2) = result;
  15782. WFIFOSET(fd,packet_len(0x81a));
  15783. }
  15784. /// Updates the zeny limit and an item in the buying store item list.
  15785. /// 081b <name id>.W <amount>.W <limit zeny>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE)
  15786. /// 09e6 <name id>.W <amount>.W <zeny>.L <limit zeny>.L <GID>.L <Date>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE2)
  15787. void clif_buyingstore_update_item( map_session_data* sd, t_itemid nameid, unsigned short amount, uint32 char_id, int zeny ){
  15788. PACKET_ZC_UPDATE_ITEM_FROM_BUYING_STORE p = {};
  15789. p.packetType = buyingStoreUpdateItemType;
  15790. p.itemId = client_nameid( nameid );
  15791. p.amount = amount;
  15792. p.zenyLimit = sd->buyingstore.zenylimit;
  15793. #if PACKETVER >= 20141016
  15794. p.zeny = zeny;
  15795. p.charId = char_id; // GID
  15796. p.updateTime = (int)time(nullptr);
  15797. #endif
  15798. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15799. }
  15800. /// Deletes item from inventory, that was sold to a buying store (ZC_ITEM_DELETE_BUYING_STORE).
  15801. /// 081c <index>.W <amount>.W <price>.L
  15802. /// message:
  15803. /// "%s (%d) were sold at %dz." (0x6d2, MSI_BUYINGSTORE_TRADE_SELLCOMPLETE)
  15804. ///
  15805. /// NOTE: This function has to be called _instead_ of clif_delitem/clif_dropitem.
  15806. void clif_buyingstore_delete_item(map_session_data* sd, short index, unsigned short amount, int price)
  15807. {
  15808. int fd = sd->fd;
  15809. WFIFOHEAD(fd,packet_len(0x81c));
  15810. WFIFOW(fd,0) = 0x81c;
  15811. WFIFOW(fd,2) = client_index( index );
  15812. WFIFOW(fd,4) = amount;
  15813. WFIFOL(fd,6) = price; // price per item, client calculates total Zeny by itself
  15814. WFIFOSET(fd,packet_len(0x81c));
  15815. }
  15816. /// Notifies the seller, that a buying store trade failed.
  15817. /// 0824 <result>.W <name id>.W (ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER)
  15818. /// result:
  15819. /// 5 = "The deal has failed." (0x39, MSI_DEAL_FAIL)
  15820. /// 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)
  15821. /// 7 = "The trade failed, because the buyer is lacking required balance." (0x6d1, MSI_BUYINGSTORE_TRADE_LACKBUYERZENY)
  15822. /// ? = nothing
  15823. void clif_buyingstore_trade_failed_seller( map_session_data* sd, short result, t_itemid nameid ){
  15824. PACKET_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER p = {};
  15825. p.packetType = HEADER_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER;
  15826. p.result = result;
  15827. p.itemId = client_nameid( nameid );
  15828. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15829. }
  15830. /// Search Store Info System
  15831. ///
  15832. /// Request to search for stores.
  15833. /// 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)
  15834. /// type:
  15835. /// 0 = Vending
  15836. /// 1 = Buying Store
  15837. ///
  15838. /// NOTE: The client determines the item ids by specifying a name and optionally,
  15839. /// amount of card slots. If the client does not know about the item it
  15840. /// cannot be searched.
  15841. static void clif_parse_SearchStoreInfo( int fd, map_session_data *sd ){
  15842. if (!battle_config.feature_search_stores)
  15843. return;
  15844. const PACKET_CZ_SEARCH_STORE_INFO* p = reinterpret_cast<PACKET_CZ_SEARCH_STORE_INFO*>( RFIFOP( fd, 0 ) );
  15845. // minimum packet length
  15846. if( p->packetLength < sizeof( *p ) ){
  15847. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15848. return;
  15849. }
  15850. // check, if there is enough data for the claimed count of items
  15851. int packet_len = p->packetLength - sizeof( *p );
  15852. if( packet_len % sizeof( p->items[0] ) ){
  15853. 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] ) );
  15854. return;
  15855. }
  15856. int count = packet_len / sizeof( p->items[0] );
  15857. if( count < p->itemsCount + p->cardsCount ){
  15858. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected count=%u, count=%u, account_id=%d).\n", p->itemsCount + p->cardsCount, count, sd->bl.id );
  15859. return;
  15860. }
  15861. if ( p->searchType > SEARCHTYPE_BUYING_STORE ) {
  15862. ShowError( "clif_parse_SearchStoreInfo: Invalid search type %u (account_id=%d).\n", p->searchType, sd->bl.id );
  15863. return;
  15864. }
  15865. if ( p->minPrice > battle_config.vending_max_value ) {
  15866. ShowError( "clif_parse_SearchStoreInfo: Invalid min price %u (account_id=%d).\n", p->minPrice, sd->bl.id );
  15867. return;
  15868. }
  15869. if ( p->maxPrice > battle_config.vending_max_value ) {
  15870. ShowError( "clif_parse_SearchStoreInfo: Invalid max price %u (account_id=%d).\n", p->maxPrice, sd->bl.id );
  15871. return;
  15872. }
  15873. 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 );
  15874. }
  15875. /// Results for a store search request.
  15876. /// 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)
  15877. /// is first page:
  15878. /// 0 = appends to existing results
  15879. /// 1 = clears previous results before displaying this result set
  15880. /// is next page:
  15881. /// 0 = no "next" label
  15882. /// 1 = "next" label to retrieve more results
  15883. void clif_search_store_info_ack( map_session_data& sd ){
  15884. #if PACKETVER_MAIN_NUM >= 20100817 || PACKETVER_RE_NUM >= 20100706 || defined(PACKETVER_ZERO)
  15885. if (!battle_config.feature_search_stores)
  15886. return;
  15887. uint32 start = sd.searchstore.pages * SEARCHSTORE_RESULTS_PER_PAGE ;
  15888. uint32 end = umin( static_cast<uint32>( sd.searchstore.items.size() ), start + SEARCHSTORE_RESULTS_PER_PAGE );
  15889. PACKET_ZC_SEARCH_STORE_INFO_ACK* p = reinterpret_cast<PACKET_ZC_SEARCH_STORE_INFO_ACK*>( packet_buffer );
  15890. p->packetType = HEADER_ZC_SEARCH_STORE_INFO_ACK;
  15891. p->packetLength = sizeof( *p );
  15892. p->firstPage = !sd.searchstore.pages;
  15893. p->nextPage = searchstore_querynext( sd );
  15894. p->usesCount = static_cast<decltype(p->usesCount)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(p->usesCount)>::max() ) );
  15895. for( int i = 0, count = 0; i < end - start; i++ ){
  15896. std::shared_ptr<s_search_store_info_item> ssitem = sd.searchstore.items[start + i];
  15897. if( ssitem == nullptr ){
  15898. continue;
  15899. }
  15900. PACKET_ZC_SEARCH_STORE_INFO_ACK_sub& entry = p->items[count];
  15901. entry.storeId = ssitem->store_id;
  15902. entry.AID = ssitem->account_id;
  15903. safestrncpy( entry.shopName, ssitem->store_name, MESSAGE_SIZE );
  15904. entry.itemId = client_nameid( ssitem->nameid );
  15905. entry.itemType = itemtype( ssitem->nameid );
  15906. entry.price = ssitem->price;
  15907. entry.amount = ssitem->amount;
  15908. entry.refine = ssitem->refine;
  15909. // make-up an item for clif_addcards
  15910. struct item it = {};
  15911. for( int j = 0; j < MAX_SLOTS; j++ ){
  15912. it.card[j] = ssitem->card[j];
  15913. }
  15914. it.nameid = ssitem->nameid;
  15915. it.amount = ssitem->amount;
  15916. clif_addcards( &entry.slot, &it );
  15917. #if PACKETVER >= 20150226
  15918. clif_add_random_options( entry.option_data, it );
  15919. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15920. entry.grade = ssitem->enchantgrade;
  15921. #endif
  15922. #endif
  15923. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15924. count++;
  15925. }
  15926. clif_send( p, p->packetLength, &sd.bl, SELF );
  15927. #endif
  15928. }
  15929. /// Notification of failure when searching for stores.
  15930. /// 0837 <reason>.B (ZC_SEARCH_STORE_INFO_FAILED)
  15931. void clif_search_store_info_failed(map_session_data& sd, e_searchstore_failure reason){
  15932. #if PACKETVER_MAIN_NUM >= 20100601 || PACKETVER_RE_NUM >= 20100601 || defined(PACKETVER_ZERO)
  15933. if (!battle_config.feature_search_stores)
  15934. return;
  15935. PACKET_ZC_SEARCH_STORE_INFO_FAILED packet{};
  15936. packet.packetType = HEADER_ZC_SEARCH_STORE_INFO_FAILED;
  15937. packet.reason = static_cast<decltype(packet.reason)>(reason);
  15938. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15939. #endif
  15940. }
  15941. /// Request to display next page of results.
  15942. /// 0838 (CZ_SEARCH_STORE_INFO_NEXT_PAGE)
  15943. static void clif_parse_SearchStoreInfoNextPage(int fd, map_session_data* sd){
  15944. if (!battle_config.feature_search_stores)
  15945. return;
  15946. searchstore_next(*sd);
  15947. }
  15948. /// Opens the search store window.
  15949. /// 083a <effect>.W <remaining uses>.B (ZC_OPEN_SEARCH_STORE_INFO)
  15950. void clif_open_search_store_info(map_session_data& sd){
  15951. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15952. if (!battle_config.feature_search_stores)
  15953. return;
  15954. PACKET_ZC_OPEN_SEARCH_STORE_INFO packet{};
  15955. packet.packetType = HEADER_ZC_OPEN_SEARCH_STORE_INFO;
  15956. packet.effect = static_cast<decltype(packet.effect)>(sd.searchstore.effect);
  15957. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15958. packet.remainingUses = static_cast<decltype(packet.remainingUses)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(packet.remainingUses)>::max() ) );
  15959. #endif
  15960. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15961. #endif
  15962. }
  15963. /// Request to close the store search window.
  15964. /// 083b (CZ_CLOSE_SEARCH_STORE_INFO)
  15965. static void clif_parse_CloseSearchStoreInfo(int fd, map_session_data* sd){
  15966. if (!battle_config.feature_search_stores)
  15967. return;
  15968. searchstore_close(*sd);
  15969. }
  15970. /// Request to invoke catalog effect on a store from search results.
  15971. /// 083c <account id>.L <store id>.L <nameid>.W (CZ_SSILIST_ITEM_CLICK)
  15972. static void clif_parse_SearchStoreInfoListItemClick( int fd, map_session_data* sd ){
  15973. if (!battle_config.feature_search_stores)
  15974. return;
  15975. const PACKET_CZ_SSILIST_ITEM_CLICK* p = reinterpret_cast<PACKET_CZ_SSILIST_ITEM_CLICK*>( RFIFOP( fd, 0 ) );
  15976. searchstore_click( *sd, p->AID, p->storeId, p->itemId );
  15977. }
  15978. /// Notification of the store position on current map.
  15979. /// 083d <xPos>.W <yPos>.W (ZC_SSILIST_ITEM_CLICK_ACK)
  15980. void clif_search_store_info_click_ack(map_session_data& sd, int16 x, int16 y){
  15981. #if PACKETVER_MAIN_NUM >= 20100608 || PACKETVER_RE_NUM >= 20100608 || defined(PACKETVER_ZERO)
  15982. if (!battle_config.feature_search_stores)
  15983. return;
  15984. PACKET_ZC_SSILIST_ITEM_CLICK_ACK packet{};
  15985. packet.packetType = HEADER_ZC_SSILIST_ITEM_CLICK_ACK;
  15986. packet.x = x;
  15987. packet.y = y;
  15988. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15989. #endif
  15990. }
  15991. /// Parse function for packet debugging.
  15992. void clif_parse_debug(int fd,map_session_data *sd)
  15993. {
  15994. int16 packet_len;
  15995. // clif_parse ensures, that there is at least 2 bytes of data
  15996. uint16 cmd = RFIFOW(fd,0);
  15997. if( sd ) {
  15998. packet_len = packet_db[cmd].len;
  15999. if( packet_len == 0 )
  16000. {// unknown
  16001. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  16002. }
  16003. else if( packet_len == -1 )
  16004. {// variable length
  16005. packet_len = RFIFOW(fd,2); // clif_parse ensures, that this amount of data is already received
  16006. }
  16007. 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);
  16008. }
  16009. else
  16010. {
  16011. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  16012. ShowDebug("Packet debug of 0x%04X (length %d), session #%d\n", cmd, packet_len, fd);
  16013. }
  16014. ShowDump(RFIFOP(fd,0), packet_len);
  16015. }
  16016. /*==========================================
  16017. * Server tells client to display a window similar to Magnifier (item) one
  16018. * Server populates the window with avilable elemental converter options according to player's inventory
  16019. *------------------------------------------*/
  16020. void clif_elementalconverter_list( map_session_data& sd ){
  16021. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16022. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16023. p->packetLength = sizeof( *p );
  16024. int count = 0;
  16025. for( int i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  16026. if( skill_can_produce_mix( &sd, skill_produce_db[i].nameid, 23, 1 ) ){
  16027. p->items[count].itemId = client_nameid( skill_produce_db[i].nameid );
  16028. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16029. count++;
  16030. }
  16031. }
  16032. clif_send( p, p->packetLength, &sd.bl, SELF );
  16033. if( count > 0 ){
  16034. sd.menuskill_id = SA_CREATECON;
  16035. sd.menuskill_val = count;
  16036. }
  16037. }
  16038. /**
  16039. * Rune Knight
  16040. **/
  16041. void clif_millenniumshield( block_list& bl, int16 shields ){
  16042. #if PACKETVER >= 20081126
  16043. PACKET_ZC_MILLENNIUMSHIELD packet{};
  16044. packet.packetType = HEADER_ZC_MILLENNIUMSHIELD;
  16045. packet.aid = bl.id;
  16046. packet.num = shields;
  16047. packet.state = 0;
  16048. clif_send( &packet, sizeof( packet ), &bl, AREA );
  16049. #endif
  16050. }
  16051. /**
  16052. * Mechanic
  16053. **/
  16054. /*==========================================
  16055. * Magic Decoy Material List
  16056. *------------------------------------------*/
  16057. void clif_magicdecoy_list( map_session_data& sd, uint16 skill_lv, short x, short y ){
  16058. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16059. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16060. p->packetLength = sizeof( *p );
  16061. size_t count = 0;
  16062. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  16063. if( itemdb_group.item_exists( IG_ELEMENT, sd.inventory.u.items_inventory[i].nameid ) ){
  16064. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  16065. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16066. count++;
  16067. }
  16068. }
  16069. if( count > 0 ){
  16070. clif_send( p, p->packetLength, &sd.bl, SELF );
  16071. sd.menuskill_id = NC_MAGICDECOY;
  16072. sd.menuskill_val = skill_lv;
  16073. sd.sc.comet_x = x;
  16074. sd.sc.comet_y = y;
  16075. }else{
  16076. clif_skill_fail( sd, NC_MAGICDECOY );
  16077. }
  16078. }
  16079. /*==========================================
  16080. * Guillotine Cross Poisons List
  16081. *------------------------------------------*/
  16082. void clif_poison_list( map_session_data& sd, uint16 skill_lv ){
  16083. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16084. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16085. p->packetLength = sizeof( *p );
  16086. size_t count = 0;
  16087. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  16088. if( itemdb_group.item_exists( IG_POISON, sd.inventory.u.items_inventory[i].nameid ) ){
  16089. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  16090. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16091. count++;
  16092. }
  16093. }
  16094. if( count > 0 ){
  16095. clif_send( p, p->packetLength, &sd.bl, SELF );
  16096. sd.menuskill_id = GC_POISONINGWEAPON;
  16097. sd.menuskill_val = skill_lv;
  16098. }else{
  16099. clif_skill_fail( sd, GC_POISONINGWEAPON, USESKILL_FAIL_GUILLONTINE_POISON );
  16100. }
  16101. }
  16102. /// 0442 <Length>.W <why>.L { <Skill_Id>.W }* (ZC_SKILL_SELECT_REQUEST).
  16103. void clif_autoshadowspell_list( map_session_data& sd ){
  16104. #if PACKETVER >= 20081210
  16105. if( sd.menuskill_id == SC_AUTOSHADOWSPELL )
  16106. return;
  16107. PACKET_ZC_SKILL_SELECT_REQUEST* p = reinterpret_cast<PACKET_ZC_SKILL_SELECT_REQUEST*>( packet_buffer );
  16108. p->packetType = HEADER_ZC_SKILL_SELECT_REQUEST;
  16109. p->packetLength = sizeof( *p );
  16110. p->flag = 1; // enum PACKET_ZC_SKILL_SELECT_REQUEST::enumWHY::WHY_SC_AUTOSHADOWSPELL = 0x1
  16111. size_t count = 0;
  16112. for( size_t i = 0; i < MAX_SKILL; i++ ){
  16113. if( sd.status.skill[i].flag == SKILL_FLAG_PLAGIARIZED && sd.status.skill[i].id > 0 &&
  16114. skill_get_inf2(sd.status.skill[i].id, INF2_ISAUTOSHADOWSPELL))
  16115. {
  16116. p->skillIds[count] = sd.status.skill[i].id;
  16117. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skillIds[0] ) );
  16118. count++;
  16119. }
  16120. }
  16121. if( count > 0 ) {
  16122. clif_send( p, p->packetLength, &sd.bl, SELF );
  16123. sd.menuskill_id = SC_AUTOSHADOWSPELL;
  16124. sd.menuskill_val = static_cast<decltype(sd.menuskill_val)>( count );
  16125. } else {
  16126. status_change_end( &sd.bl, SC_STOP );
  16127. clif_skill_fail( sd, SC_AUTOSHADOWSPELL, USESKILL_FAIL_IMITATION_SKILL_NONE );
  16128. }
  16129. #endif
  16130. }
  16131. /*===========================================
  16132. * Skill list for Four Elemental Analysis
  16133. * and Change Material skills.
  16134. * 07e3 <type>.L (ZC_ITEMLISTWIN_OPEN).
  16135. *------------------------------------------*/
  16136. int clif_skill_itemlistwindow( map_session_data *sd, uint16 skill_id, uint16 skill_lv ) {
  16137. #if PACKETVER >= 20090922
  16138. int fd = sd->fd;
  16139. nullpo_ret(sd);
  16140. sd->menuskill_id = skill_id; // To prevent hacking.
  16141. sd->menuskill_val = skill_lv;
  16142. if( skill_id == GN_CHANGEMATERIAL )
  16143. skill_lv = 0; // Changematerial
  16144. WFIFOHEAD(fd, packet_len(0x7e3));
  16145. WFIFOW(fd,0) = 0x7e3;
  16146. WFIFOL(fd,2) = skill_lv;
  16147. WFIFOL(fd,4) = 0;
  16148. WFIFOSET(fd, packet_len(0x7e3));
  16149. #endif
  16150. return 1;
  16151. }
  16152. /*==========================================
  16153. * Select a skill into a given list (used by SA_AUTOSPELL/SC_AUTOSHADOWSPELL)
  16154. * 0443 <type>.L <skill_id>.W (CZ_SKILL_SELECT_RESPONSE)
  16155. *------------------------------------------*/
  16156. void clif_parse_SkillSelectMenu(int fd, map_session_data *sd) {
  16157. #if PACKETVER >= 20081210
  16158. if( sd == nullptr ){
  16159. return;
  16160. }
  16161. const PACKET_CZ_SKILL_SELECT_RESPONSE* p = reinterpret_cast<PACKET_CZ_SKILL_SELECT_RESPONSE*>( RFIFOP( fd, 0 ) );
  16162. if (sd->menuskill_id == SA_AUTOSPELL) {
  16163. sd->state.workinprogress = WIP_DISABLE_NONE;
  16164. skill_autospell(sd, p->selectedSkillId);
  16165. } else if (sd->menuskill_id == SC_AUTOSHADOWSPELL) {
  16166. if (pc_istrading(sd)) {
  16167. clif_skill_fail( *sd, sd->ud.skill_id );
  16168. clif_menuskill_clear(sd);
  16169. return;
  16170. }
  16171. skill_select_menu(*sd, p->selectedSkillId);
  16172. } else
  16173. return;
  16174. clif_menuskill_clear(sd);
  16175. #endif
  16176. }
  16177. /// Kagerou/Oboro amulet spirit.
  16178. /// 08cf <id>.L <type>.W <num>.W (ZC_SPIRITS_ATTRIBUTE)
  16179. void clif_spiritcharm( map_session_data& sd ){
  16180. #if PACKETVER >= 20111102
  16181. PACKET_ZC_SPIRITS_ATTRIBUTE packet{};
  16182. packet.packetType = HEADER_ZC_SPIRITS_ATTRIBUTE;
  16183. packet.aid = sd.bl.id;
  16184. packet.spiritsType = static_cast<decltype(packet.spiritsType)>(sd.spiritcharm_type);
  16185. packet.num = static_cast<decltype(packet.num)>(sd.spiritcharm);
  16186. clif_send( &packet, sizeof( packet ), &sd.bl, AREA );
  16187. #endif
  16188. }
  16189. /// Move Item from or to Personal Tab
  16190. /// 0907 <index>.W <type>.B (CZ_INVENTORY_TAB)
  16191. /// type:
  16192. /// 0 = move item to personal tab
  16193. /// 1 = move item to normal tab
  16194. void clif_parse_MoveItem( int fd, map_session_data* sd ){
  16195. // TODO: Check for correct packet version
  16196. #if PACKETVER >= 20120410
  16197. /* can't move while dead. */
  16198. if(pc_isdead(sd)) {
  16199. return;
  16200. }
  16201. const PACKET_CZ_INVENTORY_TAB* p = reinterpret_cast<PACKET_CZ_INVENTORY_TAB*>( RFIFOP( fd, 0 ) );
  16202. uint16 index = server_index( p->index );
  16203. if( index >= MAX_INVENTORY ){
  16204. return;
  16205. }
  16206. if ( sd->inventory.u.items_inventory[index].favorite != 0 && p->favorite == true )
  16207. sd->inventory.u.items_inventory[index].favorite = 0;
  16208. else if( p->favorite == false )
  16209. sd->inventory.u.items_inventory[index].favorite = 1;
  16210. else
  16211. return;/* nothing to do. */
  16212. clif_favorite_item( *sd, index );
  16213. #endif
  16214. }
  16215. /// Items that are in favorite tab of inventory.
  16216. /// 0908 <index>.W <favorite>.B (ZC_INVENTORY_TAB)
  16217. static void clif_favorite_item( map_session_data& sd, uint16 index ){
  16218. // TODO: Check for correct packet version
  16219. #if PACKETVER >= 20111122
  16220. PACKET_ZC_INVENTORY_TAB p = {};
  16221. p.packetType = HEADER_ZC_INVENTORY_TAB;
  16222. p.index = client_index( index );
  16223. p.favorite = ( sd.inventory.u.items_inventory[index].favorite == 1 ) ? false : true;
  16224. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16225. #endif
  16226. }
  16227. /// 08d2 <id>.L <Pos X>.W <Pos Y>.W (ZC_FASTMOVE).
  16228. void clif_snap( struct block_list *bl, short x, short y ) {
  16229. unsigned char buf[10];
  16230. WBUFW(buf,0) = 0x8d2;
  16231. WBUFL(buf,2) = bl->id;
  16232. WBUFW(buf,6) = x;
  16233. WBUFW(buf,8) = y;
  16234. if( disguised(bl) )
  16235. {
  16236. clif_send(buf, packet_len(0x8d2), bl, AREA_WOS);
  16237. WBUFL(buf,2) = disguised_bl_id(bl->id);
  16238. clif_send(buf, packet_len(0x8d2), bl, SELF);
  16239. } else
  16240. clif_send(buf,packet_len(0x8d2),bl, AREA);
  16241. }
  16242. /// 0977 <id>.L <HP>.L <maxHP>.L (ZC_HP_INFO).
  16243. void clif_monster_hp_bar( struct mob_data* md, int fd ) {
  16244. #if PACKETVER >= 20120404
  16245. WFIFOHEAD(fd,packet_len(0x977));
  16246. WFIFOW(fd,0) = 0x977;
  16247. WFIFOL(fd,2) = md->bl.id;
  16248. WFIFOL(fd,6) = md->status.hp;
  16249. WFIFOL(fd,10) = md->status.max_hp;
  16250. WFIFOSET(fd,packet_len(0x977));
  16251. #endif
  16252. }
  16253. /* [Ind] placeholder for unsupported incoming packets (avoids server disconnecting client) */
  16254. void __attribute__ ((unused)) clif_parse_dull(int fd, map_session_data *sd) {
  16255. return;
  16256. }
  16257. /// Ack world info (ZC_ACK_BEFORE_WORLD_INFO)
  16258. /// 0979 <world name>.24B <char name>.24B
  16259. void clif_ackworldinfo(map_session_data* sd) {
  16260. int fd;
  16261. nullpo_retv(sd);
  16262. fd = sd->fd;
  16263. WFIFOHEAD(fd,packet_len(0x979));
  16264. WFIFOW(fd,0)=0x979;
  16265. //AID -> world name ?
  16266. safestrncpy(WFIFOCP(fd,2), "" /* World name */, 24);
  16267. safestrncpy(WFIFOCP(fd,26), sd->status.name, NAME_LENGTH);
  16268. WFIFOSET(fd,packet_len(0x979));
  16269. }
  16270. /// req world info (CZ_REQ_BEFORE_WORLD_INFO)
  16271. /// 0978 <AID>.L
  16272. void clif_parse_reqworldinfo(int fd,map_session_data *sd) {
  16273. //uint32 aid = RFIFOL(fd,2); //should we trust client ?
  16274. if(sd)
  16275. clif_ackworldinfo(sd);
  16276. }
  16277. static void clif_loadConfirm( map_session_data *sd ){
  16278. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410
  16279. nullpo_retv( sd );
  16280. PACKET_ZC_NOTIFY_ACTORINIT p = {};
  16281. p.packetType = HEADER_ZC_NOTIFY_ACTORINIT;
  16282. clif_send( &p, sizeof(p), &sd->bl, SELF );
  16283. clif_instance_info( *sd );
  16284. #endif
  16285. }
  16286. /// unknown usage (CZ_BLOCKING_PLAY_CANCEL)
  16287. /// 0447
  16288. void clif_parse_blocking_playcancel( int fd, map_session_data *sd ){
  16289. clif_loadConfirm( sd );
  16290. int32 mf = map_getmapflag(sd->bl.m, MF_SPECIALPOPUP);
  16291. if (mf > 0) {
  16292. clif_specialpopup(*sd, mf);
  16293. }
  16294. }
  16295. /// req world info (CZ_CLIENT_VERSION)
  16296. /// 044A <version>.L
  16297. void clif_parse_client_version(int fd, map_session_data *sd) {
  16298. //if(sd)
  16299. ;
  16300. }
  16301. /// Ranking list
  16302. /// ranking pointlist { <name>.24B <point>.L }*10
  16303. void clif_sub_ranklist(unsigned char *buf,int idx,map_session_data* sd, enum e_rank rankingtype){
  16304. struct fame_list* list;
  16305. int i, size = MAX_FAME_LIST;
  16306. switch(rankingtype) {
  16307. case RANK_BLACKSMITH: list = smith_fame_list; break;
  16308. case RANK_ALCHEMIST: list = chemist_fame_list; break;
  16309. case RANK_TAEKWON: list = taekwon_fame_list; break;
  16310. // PK currently unsupported
  16311. case RANK_KILLER: list = nullptr; size = 0; break;
  16312. default:
  16313. ShowError( "clif_sub_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16314. return;
  16315. }
  16316. //Packet size limits this list to 10 elements. [Skotlex]
  16317. for (i = 0; i < min(10,size); i++) {
  16318. if (list[i].id > 0) {
  16319. const char* name;
  16320. if (strcmp(list[i].name, "-") == 0 &&
  16321. (name = map_charid2nick(list[i].id)) != nullptr)
  16322. {
  16323. safestrncpy(WBUFCP(buf,idx + NAME_LENGTH * i), name, NAME_LENGTH);
  16324. } else {
  16325. safestrncpy(WBUFCP(buf,idx + NAME_LENGTH * i), list[i].name, NAME_LENGTH);
  16326. }
  16327. } else {
  16328. safestrncpy(WBUFCP(buf, idx + NAME_LENGTH * i), "None", NAME_LENGTH);
  16329. }
  16330. WBUFL(buf, idx+NAME_LENGTH*10 + i * 4) = list[i].fame; //points
  16331. }
  16332. for(;i < 10; i++) { //In case the MAX is less than 10.
  16333. safestrncpy(WBUFCP(buf, idx + NAME_LENGTH * i), "Unavailable", NAME_LENGTH);
  16334. WBUFL(buf, idx+NAME_LENGTH*10 + i * 4) = 0;
  16335. }
  16336. }
  16337. /// Request for the blacksmith ranklist.
  16338. /// /blacksmith command sends this packet to the server.
  16339. /// 0217 (CZ_BLACKSMITH_RANK)
  16340. void clif_parse_Blacksmith( int fd, map_session_data *sd ){
  16341. clif_ranklist(sd,RANK_BLACKSMITH);
  16342. }
  16343. /// Request for the alchemist ranklist.
  16344. /// /alchemist command sends this packet to the server.
  16345. /// 0218 (CZ_ALCHEMIST_RANK)
  16346. void clif_parse_Alchemist( int fd, map_session_data *sd ){
  16347. clif_ranklist(sd,RANK_ALCHEMIST);
  16348. }
  16349. /// Request for the taekwon ranklist.
  16350. /// /taekwon command sends this packet to the server.
  16351. /// 0225 (CZ_TAEKWON_RANK)
  16352. void clif_parse_Taekwon( int fd, map_session_data *sd ){
  16353. clif_ranklist(sd,RANK_TAEKWON);
  16354. }
  16355. /// Request for the killer ranklist.
  16356. /// /pk command sends this packet to the server.
  16357. /// 0237 (CZ_KILLER_RANK)
  16358. void clif_parse_RankingPk( int fd, map_session_data *sd ){
  16359. clif_ranklist(sd,RANK_KILLER);
  16360. }
  16361. /// 0219 { <name>.24B }*10 { <point>.L }*10 (ZC_BLACKSMITH_RANK)
  16362. /// 021a { <name>.24B }*10 { <point>.L }*10 (ZC_ALCHEMIST_RANK)
  16363. /// 0226 { <name>.24B }*10 { <point>.L }*10 (ZC_TAEKWON_RANK)
  16364. /// 0238 { <name>.24B }*10 { <point>.L }*10 (ZC_KILLER_RANK)
  16365. /// 097d <RankingType>.W {<CharName>.24B <point>L}*10 <mypoint>L (ZC_ACK_RANKING)
  16366. void clif_ranklist(map_session_data *sd, int16 rankingType) {
  16367. enum e_rank rank;
  16368. #if PACKETVER < 20130710
  16369. unsigned char buf[MAX_FAME_LIST * sizeof(struct fame_list)+2];
  16370. short cmd;
  16371. switch( rankingType ){
  16372. case RANK_BLACKSMITH: cmd = 0x219; break;
  16373. case RANK_ALCHEMIST: cmd = 0x21a; break;
  16374. case RANK_TAEKWON: cmd = 0x226; break;
  16375. case RANK_KILLER: cmd = 0x238; break;
  16376. default:
  16377. ShowError( "clif_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingType );
  16378. return;
  16379. }
  16380. // Can be safely casted here, since we validated it before
  16381. rank = (enum e_rank)rankingType;
  16382. WBUFW(buf,0) = cmd;
  16383. clif_sub_ranklist(buf,2,sd,rank);
  16384. clif_send(buf, packet_len(cmd), &sd->bl, SELF);
  16385. #else
  16386. unsigned char buf[MAX_FAME_LIST * sizeof(struct fame_list)+8];
  16387. int mypoint = 0;
  16388. switch( rankingType ){
  16389. case RANK_BLACKSMITH:
  16390. case RANK_ALCHEMIST:
  16391. case RANK_TAEKWON:
  16392. case RANK_KILLER:
  16393. break;
  16394. default:
  16395. ShowError( "clif_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingType );
  16396. return;
  16397. }
  16398. // Can be safely casted here, since we validated it before
  16399. rank = (enum e_rank)rankingType;
  16400. WBUFW(buf,0) = 0x97d;
  16401. WBUFW(buf,2) = rank;
  16402. clif_sub_ranklist(buf,4,sd,rank);
  16403. switch(sd->class_&MAPID_UPPERMASK){ //mypoint (checking if valid type)
  16404. case MAPID_BLACKSMITH:
  16405. case MAPID_ALCHEMIST:
  16406. case MAPID_TAEKWON:
  16407. mypoint = sd->status.fame;
  16408. }
  16409. WBUFL(buf,284) = mypoint; //mypoint
  16410. clif_send(buf, 288, &sd->bl, SELF);
  16411. #endif
  16412. }
  16413. /*
  16414. * 097c <type> (CZ_REQ_RANKING)
  16415. * type
  16416. * 0: /blacksmith
  16417. * 1: /alchemist
  16418. * 2: /taekwon
  16419. * 3: /pk
  16420. * */
  16421. void clif_parse_ranklist(int fd,map_session_data *sd) {
  16422. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  16423. int16 rankingtype = RFIFOW(fd,info->pos[0]); //type
  16424. clif_ranklist(sd,rankingtype);
  16425. }
  16426. /// Updates the fame rank points for the given ranking.
  16427. /// 021b <points>.L <total points>.L (ZC_BLACKSMITH_POINT)
  16428. /// 021c <points>.L <total points>.L (ZC_ALCHEMIST_POINT)
  16429. /// 0224 <points>.L <total points>.L (ZC_TAEKWON_POINT)
  16430. /// 097e <RankingType>.W <point>.L <TotalPoint>.L (ZC_UPDATE_RANKING_POINT)
  16431. void clif_update_rankingpoint(map_session_data &sd, int rankingtype, int point) {
  16432. int fd = sd.fd;
  16433. #if PACKETVER < 20130710
  16434. short cmd;
  16435. switch(rankingtype){
  16436. case RANK_BLACKSMITH: cmd = 0x21b; break;
  16437. case RANK_ALCHEMIST: cmd = 0x21c; break;
  16438. case RANK_TAEKWON: cmd = 0x224; break;
  16439. default:
  16440. ShowError( "clif_update_rankingpoint: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16441. return;
  16442. }
  16443. WFIFOHEAD(fd,packet_len(cmd));
  16444. WFIFOW(fd,0) = cmd;
  16445. WFIFOL(fd,2) = point;
  16446. WFIFOL(fd,6) = sd.status.fame;
  16447. WFIFOSET(fd, packet_len(cmd));
  16448. #else
  16449. WFIFOHEAD(fd,packet_len(0x97e));
  16450. WFIFOW(fd,0) = 0x97e;
  16451. WFIFOW(fd,2) = rankingtype;
  16452. WFIFOL(fd,4) = point;
  16453. WFIFOL(fd,8) = sd.status.fame;
  16454. WFIFOSET(fd,packet_len(0x97e));
  16455. #endif
  16456. }
  16457. /**
  16458. * Transmit personal information to player. (rates)
  16459. * 0x08cb <packet len>.W <exp>.W <death>.W <drop>.W <DETAIL_EXP_INFO>7B (ZC_PERSONAL_INFOMATION)
  16460. * <InfoType>.B <Exp>.W <Death>.W <Drop>.W (DETAIL_EXP_INFO 0x08cb)
  16461. * 0x097b <packet len>.W <exp>.L <death>.L <drop>.L <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION2)
  16462. * 0x0981 <packet len>.W <exp>.W <death>.W <drop>.W <activity rate>.W <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION_CHN)
  16463. * <InfoType>.B <Exp>.L <Death>.L <Drop>.L (DETAIL_EXP_INFO 0x97b|0981)
  16464. * FIXME!
  16465. * - Find/decide for details of EXP, Drop, and Death penalty rates
  16466. * - For now, we're assuming values for DETAIL_EXP_INFO are:
  16467. * 0 - map adjustment (bexp mapflag), 1 - Premium/VIP adjustment, 2 - Server rate adjustment, 3 - None
  16468. */
  16469. #ifdef VIP_ENABLE
  16470. void clif_display_pinfo( map_session_data& sd ){
  16471. #if PACKETVER_MAIN_NUM >= 20110627 || PACKETVER_RE_NUM >= 20110628 || defined(PACKETVER_ZERO)
  16472. // 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]
  16473. int details_bexp[PINFO_MAX], details_drop[PINFO_MAX], details_penalty[PINFO_MAX];
  16474. /**
  16475. * EXP
  16476. */
  16477. //0:PCRoom
  16478. details_bexp[PINFO_BASIC] = map_getmapflag( sd.bl.m, MF_BEXP );
  16479. if (details_bexp[PINFO_BASIC] == 100 || !details_bexp[PINFO_BASIC])
  16480. details_bexp[PINFO_BASIC] = 0;
  16481. else {
  16482. if (details_bexp[PINFO_BASIC] < 100) {
  16483. details_bexp[PINFO_BASIC] = 100 - details_bexp[PINFO_BASIC];
  16484. details_bexp[PINFO_BASIC] = 0 - details_bexp[PINFO_BASIC];
  16485. } else
  16486. details_bexp[PINFO_BASIC] = details_bexp[PINFO_BASIC] - 100;
  16487. }
  16488. //1:Premium
  16489. if( pc_isvip( &sd ) ){
  16490. details_bexp[PINFO_PREMIUM] = battle_config.vip_base_exp_increase * battle_config.base_exp_rate / 100;
  16491. if (details_bexp[PINFO_PREMIUM] < 0)
  16492. details_bexp[PINFO_PREMIUM] = 0 - details_bexp[PINFO_PREMIUM];
  16493. } else
  16494. details_bexp[PINFO_PREMIUM] = 0;
  16495. //2:Server
  16496. details_bexp[PINFO_SERVER] = battle_config.base_exp_rate;
  16497. if (details_bexp[PINFO_SERVER] == 100)
  16498. details_bexp[PINFO_SERVER] = 0;
  16499. else {
  16500. if (details_bexp[PINFO_SERVER] < 100) {
  16501. details_bexp[PINFO_SERVER] = 100 - details_bexp[PINFO_SERVER];
  16502. details_bexp[PINFO_SERVER] = 0 - details_bexp[PINFO_SERVER];
  16503. } else
  16504. details_bexp[PINFO_SERVER] = details_bexp[PINFO_SERVER] - 100;
  16505. }
  16506. //3:TPLUS
  16507. details_bexp[PINFO_CAFE] = 0;
  16508. /**
  16509. * Drop rate
  16510. */
  16511. //0:PCRoom
  16512. details_drop[PINFO_BASIC] = 0;
  16513. //1:Premium
  16514. if( pc_isvip( &sd ) ){
  16515. details_drop[PINFO_PREMIUM] = (battle_config.vip_drop_increase * battle_config.item_rate_common) / 100;
  16516. if (details_drop[PINFO_PREMIUM] < 0)
  16517. details_drop[PINFO_PREMIUM] = 0 - details_drop[PINFO_PREMIUM];
  16518. } else
  16519. details_drop[PINFO_PREMIUM] = 0;
  16520. //2:Server
  16521. details_drop[PINFO_SERVER] = battle_config.item_rate_common;
  16522. if (details_drop[PINFO_SERVER] == 100)
  16523. details_drop[PINFO_SERVER] = 0;
  16524. else {
  16525. if (details_drop[PINFO_SERVER] < 100) {
  16526. details_drop[PINFO_SERVER] = 100 - details_drop[PINFO_SERVER];
  16527. details_drop[PINFO_SERVER] = 0 - details_drop[PINFO_SERVER];
  16528. } else
  16529. details_drop[PINFO_SERVER] = details_drop[PINFO_SERVER] - 100;
  16530. }
  16531. //3:TPLUS
  16532. details_drop[PINFO_CAFE] = 0;
  16533. /**
  16534. * Penalty rate
  16535. */
  16536. //! FIXME: Current penalty system makes this announcement unable to give info on + or - rate
  16537. //0:PCRoom
  16538. details_penalty[PINFO_BASIC] = 0;
  16539. //1:Premium
  16540. if( pc_isvip( &sd ) ){
  16541. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base;
  16542. if (details_penalty[PINFO_PREMIUM] == 100)
  16543. details_penalty[PINFO_PREMIUM] = 0;
  16544. else {
  16545. if (details_penalty[PINFO_PREMIUM] < 100) {
  16546. details_penalty[PINFO_PREMIUM] = 100 - details_penalty[PINFO_PREMIUM];
  16547. details_penalty[PINFO_PREMIUM] = 0 - details_penalty[PINFO_PREMIUM];
  16548. } else
  16549. details_penalty[PINFO_PREMIUM] = details_penalty[PINFO_PREMIUM] - 100;
  16550. }
  16551. if (battle_config.death_penalty_base > battle_config.vip_exp_penalty_base)
  16552. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base - battle_config.death_penalty_base;
  16553. } else
  16554. details_penalty[PINFO_PREMIUM] = 0;
  16555. //2:Server
  16556. details_penalty[PINFO_SERVER] = battle_config.death_penalty_base;
  16557. if (details_penalty[PINFO_SERVER] == 100)
  16558. details_penalty[PINFO_SERVER] = 0;
  16559. else {
  16560. if (details_penalty[PINFO_SERVER] < 100) {
  16561. details_penalty[PINFO_SERVER] = 100 - details_penalty[PINFO_SERVER];
  16562. details_penalty[PINFO_SERVER] = 0 - details_penalty[PINFO_SERVER];
  16563. } else
  16564. details_penalty[PINFO_SERVER] = details_penalty[PINFO_SERVER] - 100;
  16565. }
  16566. //3:TPLUS
  16567. details_penalty[PINFO_CAFE] = 0;
  16568. PACKET_ZC_PERSONAL_INFOMATION* p = reinterpret_cast<PACKET_ZC_PERSONAL_INFOMATION*>( packet_buffer );
  16569. p->packetType = HEADER_ZC_PERSONAL_INFOMATION;
  16570. p->length = sizeof( *p );
  16571. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16572. p->total_exp = 100 * 1000;
  16573. p->total_death = 100 * 1000;
  16574. p->total_drop = 100 * 1000;
  16575. #else
  16576. p->total_exp = 100;
  16577. p->total_death = 100;
  16578. p->total_drop = 100;
  16579. #endif
  16580. for( int i = PINFO_BASIC; i < PINFO_MAX; i++ ){
  16581. p->details[i].type = i; // infotype 0 PCRoom, 1 Premium, 2 Server, 3 TPlus
  16582. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16583. p->details[i].exp = details_bexp[i] * 1000;
  16584. p->details[i].death = details_penalty[i] * 1000;
  16585. p->details[i].drop = details_drop[i] * 1000;
  16586. #else
  16587. p->details[i].exp = details_bexp[i];
  16588. p->details[i].death = details_penalty[i];
  16589. p->details[i].drop = details_drop[i];
  16590. #endif
  16591. p->total_exp += p->details[i].exp;
  16592. p->total_death += p->details[i].death;
  16593. p->total_drop += p->details[i].drop;
  16594. p->length += static_cast<decltype(p->length)>( sizeof( p->details[0] ) );
  16595. }
  16596. clif_send( p, p->length, &sd.bl, SELF );
  16597. #endif
  16598. }
  16599. #endif
  16600. void clif_parse_GMFullStrip(int fd, map_session_data *sd) {
  16601. char cmd[30];
  16602. int t_aid = RFIFOL(fd,2);
  16603. safesnprintf(cmd,sizeof(cmd),"%cfullstrip %d",atcommand_symbol,t_aid);
  16604. is_atcommand(fd, sd, cmd, 1);
  16605. }
  16606. /**
  16607. * Marks Crimson Marker target on mini-map to the caster
  16608. * 09C1 <id>.L <x>.W <y>.W (ZC_C_MARKERINFO)
  16609. * @param fd
  16610. * @param bl Crimson Marker target
  16611. **/
  16612. void clif_crimson_marker( map_session_data& sd, struct block_list& bl, bool remove ){
  16613. #if PACKETVER_MAIN_NUM >= 20130731 || PACKETVER_RE_NUM >= 20130707 || defined(PACKETVER_ZERO)
  16614. PACKET_ZC_C_MARKERINFO p = {};
  16615. p.PacketType = HEADER_ZC_C_MARKERINFO;
  16616. p.AID = bl.id;
  16617. if( remove ){
  16618. p.xPos = -1;
  16619. p.yPos = -1;
  16620. }else{
  16621. p.xPos = bl.x;
  16622. p.yPos = bl.y;
  16623. }
  16624. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16625. #endif
  16626. }
  16627. /**
  16628. * 02d3 <index>.W (ZC_NOTIFY_BIND_ON_EQUIP)
  16629. **/
  16630. void clif_notify_bindOnEquip( map_session_data& sd, int16 index ){
  16631. #if PACKETVER >= 20070227
  16632. PACKET_ZC_NOTIFY_BIND_ON_EQUIP p = {};
  16633. p.packetType = HEADER_ZC_NOTIFY_BIND_ON_EQUIP;
  16634. p.index = client_index( index );
  16635. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16636. #endif
  16637. }
  16638. /**
  16639. * [Ind/Hercules]
  16640. * 08b3 <Length>.W <id>.L <message>.?B (ZC_SHOWSCRIPT)
  16641. **/
  16642. void clif_showscript(struct block_list* bl, const char* message, enum send_target flag) {
  16643. char buf[256];
  16644. size_t len;
  16645. nullpo_retv(bl);
  16646. if(!message)
  16647. return;
  16648. len = strlen(message)+1;
  16649. if( len > sizeof(buf)-8 ) {
  16650. ShowWarning("clif_showscript: Truncating too long message '%s' (len=%" PRIuPTR ").\n", message, len);
  16651. len = sizeof(buf)-8;
  16652. }
  16653. WBUFW(buf,0) = 0x8b3;
  16654. WBUFW(buf,2) = (uint16)(len+8);
  16655. WBUFL(buf,4) = bl->id;
  16656. safestrncpy(WBUFCP(buf,8), message, len);
  16657. clif_send((unsigned char *) buf, WBUFW(buf,2), bl, flag);
  16658. }
  16659. /**
  16660. * [Ind/Hercules]
  16661. * 07fc <OldMasterAID>.L <NewMasterAID>.L (ZC_CHANGE_GROUP_MASTER)
  16662. **/
  16663. void clif_party_leaderchanged(map_session_data *sd, int prev_leader_aid, int new_leader_aid) {
  16664. unsigned char buf[10];
  16665. nullpo_retv(sd);
  16666. WBUFW(buf,0) = 0x7fc;
  16667. WBUFL(buf,2) = prev_leader_aid;
  16668. WBUFL(buf,6) = new_leader_aid;
  16669. clif_send(buf, packet_len(0x7fc), &sd->bl, PARTY);
  16670. }
  16671. /**
  16672. * Sends a clan message to a player
  16673. * 098e <length>.W <name>.24B <message>.?B (ZC_NOTIFY_CLAN_CHAT)
  16674. **/
  16675. void clif_clan_message( struct clan& clan, const char *mes, size_t len ){
  16676. #if PACKETVER >= 20131223
  16677. map_session_data* sd = clan_getavailablesd( clan );
  16678. if( len == 0 ){
  16679. return;
  16680. }
  16681. PACKET_ZC_NOTIFY_CLAN_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CLAN_CHAT*>( packet_buffer );
  16682. // Maximum message length = size of our packet buffer minus fixed size of the packet and 1 byte zero termination
  16683. static size_t len_max = sizeof( packet_buffer ) - sizeof( *p ) - 1;
  16684. // Is the length bigger than the maximum message length
  16685. if( len > len_max ){
  16686. ShowWarning( "clif_clan_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", clan_id=%d).\n", mes, len, len_max, clan.id );
  16687. len = len_max;
  16688. }
  16689. p->PacketType = HEADER_ZC_NOTIFY_CLAN_CHAT;
  16690. p->PacketLength = sizeof( *p );
  16691. // 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
  16692. safestrncpy( p->MemberName, "", sizeof( p->MemberName ) );
  16693. safestrncpy( p->Message, mes, len + 1 );
  16694. p->PacketLength += static_cast<decltype(p->PacketLength)>( len + 1 );
  16695. clif_send( p, p->PacketLength, &sd->bl, CLAN );
  16696. #endif
  16697. }
  16698. /**
  16699. * Parses a clan message from a player.
  16700. * 098d <length>.W <text>.?B (<name> : <message>) (CZ_CLAN_CHAT)
  16701. **/
  16702. void clif_parse_clan_chat( int fd, map_session_data* sd ){
  16703. #if PACKETVER >= 20131223
  16704. if( sd == nullptr ){
  16705. return;
  16706. }
  16707. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  16708. // validate packet and retrieve name and message
  16709. if( !clif_process_message(sd, false, name, message, output ) )
  16710. return;
  16711. clan_send_message( *sd, RFIFOCP(fd,4), RFIFOW(fd,2) - 4 );
  16712. #endif
  16713. }
  16714. /**
  16715. * Sends the basic clan informations to the client.
  16716. * 098a <length>.W <clan id>.L <clan name>.24B <clan master>.24B <clan map>.16B <alliance count>.B
  16717. * <antagonist count>.B { <alliance>.24B } * alliance count { <antagonist>.24B } * antagonist count (ZC_CLANINFO)
  16718. **/
  16719. void clif_clan_basicinfo( map_session_data& sd ){
  16720. #if PACKETVER_MAIN_NUM >= 20130626 || PACKETVER_RE_NUM >= 20130605 || defined(PACKETVER_ZERO)
  16721. struct clan* clan = sd.clan;
  16722. if( clan == nullptr ){
  16723. return;
  16724. }
  16725. PACKET_ZC_CLANINFO* p = reinterpret_cast<PACKET_ZC_CLANINFO*>( packet_buffer );
  16726. p->PacketType = HEADER_ZC_CLANINFO;
  16727. p->PacketLength = sizeof( *p );
  16728. p->ClanID = clan->id;
  16729. safestrncpy( p->ClanName, clan->name, sizeof( p->ClanName ) );
  16730. safestrncpy( p->MasterName, clan->master, sizeof( p->MasterName ) );
  16731. mapindex_getmapname_ext( clan->map, p->Map );
  16732. p->AllyCount = clan_get_alliance_count( *clan, 0 );
  16733. p->AntagonistCount = clan_get_alliance_count( *clan, 1 );
  16734. for( int flag = 0; flag < 2; flag++ ){
  16735. for( int i = 0; i < MAX_CLANALLIANCE; i++ ){
  16736. if( clan->alliance[i].clan_id > 0 && clan->alliance[i].opposition == flag ){
  16737. char* name = reinterpret_cast<char*>( WBUFP( p, p->PacketLength ) );
  16738. safestrncpy( name, clan->alliance[i].name, NAME_LENGTH );
  16739. p->PacketLength += static_cast<decltype(p->PacketLength)>( NAME_LENGTH );
  16740. }
  16741. }
  16742. }
  16743. clif_send( p, p->PacketLength, &sd.bl, SELF );
  16744. #endif
  16745. }
  16746. /**
  16747. * Updates the online and maximum player count of a clan.
  16748. * 0988 <online count>.W <maximum member amount>.W (ZC_NOTIFY_CLAN_CONNECTINFO)
  16749. **/
  16750. void clif_clan_onlinecount( struct clan& clan ){
  16751. #if PACKETVER >= 20131223
  16752. map_session_data* sd = clan_getavailablesd( clan );
  16753. if( sd == nullptr){
  16754. return;
  16755. }
  16756. PACKET_ZC_NOTIFY_CLAN_CONNECTINFO p = {};
  16757. p.PacketType = HEADER_ZC_NOTIFY_CLAN_CONNECTINFO;
  16758. p.NumConnect = clan.connect_member;
  16759. p.NumTotal = clan.max_member;
  16760. clif_send( &p, sizeof( p ), &sd->bl, CLAN );
  16761. #endif
  16762. }
  16763. /**
  16764. * Notifies the client that the player has left his clan.
  16765. * 0989 (ZC_ACK_CLAN_LEAVE)
  16766. **/
  16767. void clif_clan_leave( map_session_data& sd ){
  16768. #if PACKETVER >= 20131223
  16769. PACKET_ZC_ACK_CLAN_LEAVE p = {};
  16770. p.PacketType = HEADER_ZC_ACK_CLAN_LEAVE;
  16771. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16772. #endif
  16773. }
  16774. /**
  16775. * Acknowledge the client about change title result (ZC_ACK_CHANGE_TITLE).
  16776. * 0A2F <result>.B <title_id>.L
  16777. */
  16778. void clif_change_title_ack(map_session_data *sd, unsigned char result, unsigned long title_id)
  16779. {
  16780. #if PACKETVER >= 20150513
  16781. int fd;
  16782. nullpo_retv(sd);
  16783. if (!clif_session_isValid(sd))
  16784. return;
  16785. fd = sd->fd;
  16786. WFIFOHEAD(fd, packet_len(0xa2f));
  16787. WFIFOW(fd, 0) = 0xa2f;
  16788. WFIFOB(fd, 2) = result;
  16789. WFIFOL(fd, 3) = title_id;
  16790. WFIFOSET(fd, packet_len(0xa2f));
  16791. #endif
  16792. }
  16793. /**
  16794. * Parsing a request from the client change title (CZ_REQ_CHANGE_TITLE).
  16795. * 0A2E <title_id>.L
  16796. */
  16797. void clif_parse_change_title(int fd, map_session_data *sd)
  16798. {
  16799. int title_id;
  16800. nullpo_retv(sd);
  16801. title_id = RFIFOL(fd, 2);
  16802. if( title_id == sd->status.title_id ){
  16803. // It is exactly the same as the old one
  16804. return;
  16805. }else if( title_id <= 0 ){
  16806. sd->status.title_id = 0;
  16807. }else{
  16808. if (std::find(sd->titles.begin(), sd->titles.end(), title_id) == sd->titles.end()) {
  16809. clif_change_title_ack(sd, 1, title_id);
  16810. return;
  16811. }
  16812. sd->status.title_id = title_id;
  16813. }
  16814. clif_name_area(&sd->bl);
  16815. clif_change_title_ack(sd, 0, title_id);
  16816. }
  16817. #ifdef DUMP_UNKNOWN_PACKET
  16818. void DumpUnknown(int fd,TBL_PC *sd,int cmd,int packet_len)
  16819. {
  16820. const char* packet_txt = "save/packet.txt";
  16821. FILE* fp;
  16822. time_t time_server;
  16823. struct tm *datetime;
  16824. char datestr[512];
  16825. time(&time_server); // get time in seconds since 1/1/1970
  16826. datetime = localtime(&time_server); // convert seconds in structure
  16827. // like sprintf, but only for date/time (Sunday, November 02 2003 15:12:52)
  16828. strftime(datestr, sizeof(datestr)-1, "%A, %B %d %Y %X.", datetime); // Server time (normal time): %A, %B %d %Y %X.
  16829. if( ( fp = fopen( packet_txt , "a" ) ) != nullptr ) {
  16830. if( sd ) {
  16831. 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);
  16832. } else {
  16833. fprintf(fp, "Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16834. }
  16835. WriteDump(fp, RFIFOP(fd,0), packet_len);
  16836. fprintf(fp, "\n");
  16837. fclose(fp);
  16838. } else {
  16839. ShowError("Failed to write '%s'.\n", packet_txt);
  16840. // Dump on console instead
  16841. if( sd ) {
  16842. 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);
  16843. } else {
  16844. ShowDebug("Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16845. }
  16846. ShowDump(RFIFOP(fd,0), packet_len);
  16847. }
  16848. }
  16849. #endif
  16850. /// Roulette System
  16851. /// Author: Yommy
  16852. void roulette_generate_bonus( map_session_data& sd ){
  16853. if( battle_config.feature_roulette_bonus_reward && sd.roulette.bonusItemID == 0 ){
  16854. int next_stage = 0;
  16855. if( sd.roulette_point.bronze > 0 ){
  16856. next_stage = 0;
  16857. }else if( sd.roulette_point.silver > 9 ){
  16858. next_stage = 2;
  16859. }else if( sd.roulette_point.gold > 9 ){
  16860. next_stage = 4;
  16861. }
  16862. // Get bonus item stage only from current stage or higher
  16863. int reward_stage = rnd_value( next_stage, MAX_ROULETTE_LEVEL - 1 );
  16864. sd.roulette.bonusItemID = rd.nameid[reward_stage][rnd()%rd.items[reward_stage]];
  16865. }else{
  16866. sd.roulette.bonusItemID = 0;
  16867. }
  16868. }
  16869. /// Opens the roulette window
  16870. /// 0A1A <result>.B <serial>.L <stage>.B <price index>.B <additional item id>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_OPEN_ROULETTE)
  16871. void clif_roulette_open( map_session_data* sd ){
  16872. nullpo_retv( sd );
  16873. roulette_generate_bonus( *sd );
  16874. struct packet_roulette_open_ack p;
  16875. p.PacketType = 0xa1a;
  16876. p.Result = 0; // result
  16877. p.Serial = 0; // serial
  16878. p.Step = (sd->roulette.claimPrize) ? sd->roulette.stage - 1 : 0;
  16879. p.Idx = (sd->roulette.claimPrize) ? sd->roulette.prizeIdx : -1;
  16880. p.AdditionItemID = sd->roulette.bonusItemID;
  16881. p.GoldPoint = sd->roulette_point.gold;
  16882. p.SilverPoint = sd->roulette_point.silver;
  16883. p.BronzePoint = sd->roulette_point.bronze;
  16884. sd->state.roulette_open = true;
  16885. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  16886. }
  16887. /// Request to open the roulette window
  16888. /// 0A19 (CZ_REQ_OPEN_ROULETTE)
  16889. void clif_parse_roulette_open( int fd, map_session_data* sd ){
  16890. nullpo_retv(sd);
  16891. if (!battle_config.feature_roulette) {
  16892. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16893. return;
  16894. }
  16895. clif_roulette_open(sd);
  16896. }
  16897. /// Sends the info about the available roulette rewards to the client
  16898. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.W <amount>.W } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO)
  16899. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.L <amount>.L } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO) >= 20180516
  16900. void clif_roulette_info( map_session_data* sd ){
  16901. nullpo_retv(sd);
  16902. struct packet_roulette_info_ack p;
  16903. p.PacketType = rouletteinfoackType;
  16904. p.PacketLength = sizeof(p);
  16905. p.RouletteSerial = 1;
  16906. for( int i = 0, count = 0; i < MAX_ROULETTE_LEVEL; i++ ){
  16907. for( int j = 0; j < MAX_ROULETTE_COLUMNS - i; j++ ){
  16908. p.ItemInfo[count].Row = i;
  16909. p.ItemInfo[count].Position = j;
  16910. p.ItemInfo[count].ItemId = client_nameid( rd.nameid[i][j] );
  16911. p.ItemInfo[count].Count = rd.qty[i][j];
  16912. #if PACKETVER >= 20180511
  16913. p.ItemInfo[count].unused = 0;
  16914. #endif
  16915. count++;
  16916. }
  16917. }
  16918. clif_send( &p, sizeof(p), &sd->bl, SELF );
  16919. }
  16920. /// Request the roulette reward data
  16921. /// 0A1B (CZ_REQ_ROULETTE_INFO)
  16922. void clif_parse_roulette_info( int fd, map_session_data* sd ){
  16923. nullpo_retv(sd);
  16924. if (!battle_config.feature_roulette) {
  16925. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16926. return;
  16927. }
  16928. clif_roulette_info(sd);
  16929. }
  16930. /// Notification of the client that the roulette window was closed
  16931. /// 0A1D (CZ_REQ_CLOSE_ROULETTE)
  16932. void clif_parse_roulette_close( int fd, map_session_data* sd ){
  16933. nullpo_retv(sd);
  16934. if (!battle_config.feature_roulette) {
  16935. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  16936. return;
  16937. }
  16938. sd->state.roulette_open = false;
  16939. }
  16940. /// Response to a item reward request
  16941. /// 0A22 <type>.B <bonus item>.W (ZC_RECV_ROULETTE_ITEM)
  16942. static void clif_roulette_recvitem_ack(map_session_data *sd, enum RECV_ROULETTE_ITEM_REQ type) {
  16943. #if PACKETVER >= 20141016
  16944. nullpo_retv(sd);
  16945. struct packet_roulette_itemrecv_ack p;
  16946. p.PacketType = roulettercvitemackType;
  16947. p.Result = type;
  16948. p.AdditionItemID = sd->roulette.bonusItemID;
  16949. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  16950. #endif
  16951. }
  16952. /**
  16953. * Claim roulette reward
  16954. * @param sd Player
  16955. * @return 0:Success
  16956. **/
  16957. static uint8 clif_roulette_getitem(map_session_data *sd) {
  16958. struct item it;
  16959. uint8 res = 1;
  16960. unsigned short factor = 1;
  16961. nullpo_retr(1, sd);
  16962. if (sd->roulette.prizeIdx < 0)
  16963. return RECV_ITEM_FAILED;
  16964. memset(&it, 0, sizeof(it));
  16965. it.nameid = rd.nameid[sd->roulette.prizeStage][sd->roulette.prizeIdx];
  16966. it.identify = 1;
  16967. if( sd->roulette.bonusItemID != 0 ){
  16968. if( sd->roulette.bonusItemID == it.nameid && battle_config.feature_roulette_bonus_reward && !( rd.flag[sd->roulette.prizeStage][sd->roulette.prizeIdx]&1 ) ){
  16969. factor = 2;
  16970. // Reset the Bonus Item to trigger new calculation
  16971. sd->roulette.bonusItemID = 0;
  16972. }else{
  16973. for( int i = 0; i < MAX_ROULETTE_LEVEL && sd->roulette.bonusItemID != 0; i++ ){
  16974. for( int j = 0; j < MAX_ROULETTE_COLUMNS - i && sd->roulette.bonusItemID != 0; j++ ){
  16975. if( rd.nameid[i][j] == sd->roulette.bonusItemID ){
  16976. // 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
  16977. if( sd->roulette.prizeStage >= i ){
  16978. // Reset the Bonus Item to trigger new calculation and to cancel the loops
  16979. sd->roulette.bonusItemID = 0;
  16980. }
  16981. }
  16982. }
  16983. }
  16984. }
  16985. }
  16986. if ((res = pc_additem(sd, &it, rd.qty[sd->roulette.prizeStage][sd->roulette.prizeIdx] * factor, LOG_TYPE_ROULETTE)) == 0) {
  16987. ; // onSuccess
  16988. }
  16989. if( sd->roulette.bonusItemID == 0 ){
  16990. roulette_generate_bonus( *sd );
  16991. }
  16992. sd->roulette.claimPrize = false;
  16993. sd->roulette.prizeStage = 0;
  16994. sd->roulette.prizeIdx = -1;
  16995. sd->roulette.stage = 0;
  16996. return res;
  16997. }
  16998. /// Update Roulette window with current stats
  16999. /// 0A20 <result>.B <stage>.W <price index>.W <bonus item>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_GENERATE_ROULETTE)
  17000. void clif_roulette_generate( map_session_data *sd, unsigned char result, short stage, short prizeIdx, t_itemid bonusItemID ){
  17001. nullpo_retv( sd );
  17002. struct packet_roulette_generate_ack p;
  17003. p.PacketType = roulettgenerateackType;
  17004. p.Result = result;
  17005. p.Step = stage;
  17006. p.Idx = prizeIdx;
  17007. p.AdditionItemID = battle_config.feature_roulette_bonus_reward ? bonusItemID : 0;
  17008. p.RemainGold = sd->roulette_point.gold;
  17009. p.RemainSilver = sd->roulette_point.silver;
  17010. p.RemainBronze = sd->roulette_point.bronze;
  17011. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17012. }
  17013. /// Request to start the roulette
  17014. /// 0A1F (CZ_REQ_GENERATE_ROULETTE)
  17015. void clif_parse_roulette_generate( int fd, map_session_data* sd ){
  17016. enum GENERATE_ROULETTE_ACK result;
  17017. nullpo_retv(sd);
  17018. if (!battle_config.feature_roulette) {
  17019. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17020. return;
  17021. }
  17022. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  17023. return;
  17024. }
  17025. // Player has not claimed his prize yet
  17026. if( sd->roulette.claimPrize ){
  17027. clif_roulette_getitem( sd );
  17028. }
  17029. if (sd->roulette.stage >= MAX_ROULETTE_LEVEL){
  17030. // Make sure everything is reset
  17031. sd->roulette.stage = 0;
  17032. sd->roulette.claimPrize = false;
  17033. sd->roulette.prizeStage = 0;
  17034. sd->roulette.prizeIdx = -1;
  17035. }
  17036. if( !sd->roulette.stage && sd->roulette_point.bronze <= 0 && sd->roulette_point.silver < 10 && sd->roulette_point.gold < 10 ){
  17037. result = GENERATE_ROULETTE_NO_ENOUGH_POINT;
  17038. }else{
  17039. if (!sd->roulette.stage) {
  17040. if (sd->roulette_point.bronze > 0) {
  17041. sd->roulette_point.bronze -= 1;
  17042. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  17043. } else if (sd->roulette_point.silver > 9) {
  17044. sd->roulette_point.silver -= 10;
  17045. sd->roulette.stage = 2;
  17046. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  17047. } else if (sd->roulette_point.gold > 9) {
  17048. sd->roulette_point.gold -= 10;
  17049. sd->roulette.stage = 4;
  17050. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  17051. }
  17052. }
  17053. sd->roulette.prizeStage = sd->roulette.stage;
  17054. sd->roulette.prizeIdx = rnd()%rd.items[sd->roulette.stage];
  17055. sd->roulette.claimPrize = true;
  17056. sd->roulette.tick = gettick() + i64max( 1, ( MAX_ROULETTE_COLUMNS - sd->roulette.prizeStage - 3 ) ) * 1000;
  17057. if( rd.flag[sd->roulette.stage][sd->roulette.prizeIdx]&1 ){
  17058. result = GENERATE_ROULETTE_LOSING;
  17059. sd->roulette.stage = 0;
  17060. }else{
  17061. result = GENERATE_ROULETTE_SUCCESS;
  17062. sd->roulette.stage++;
  17063. }
  17064. }
  17065. clif_roulette_generate(sd,result,sd->roulette.prizeStage,(sd->roulette.prizeIdx == -1 ? 0 : sd->roulette.prizeIdx), sd->roulette.bonusItemID);
  17066. }
  17067. /// Request to claim a prize
  17068. /// 0A21 (CZ_RECV_ROULETTE_ITEM)
  17069. void clif_parse_roulette_item( int fd, map_session_data* sd ){
  17070. enum RECV_ROULETTE_ITEM_REQ type = RECV_ITEM_FAILED;
  17071. nullpo_retv(sd);
  17072. if (!battle_config.feature_roulette) {
  17073. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17074. return;
  17075. }
  17076. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  17077. return;
  17078. }
  17079. if (sd->roulette.claimPrize && sd->roulette.prizeIdx != -1) {
  17080. switch (clif_roulette_getitem(sd)) {
  17081. case ADDITEM_SUCCESS:
  17082. type = RECV_ITEM_SUCCESS;
  17083. break;
  17084. case ADDITEM_INVALID:
  17085. case ADDITEM_OVERITEM:
  17086. case ADDITEM_OVERAMOUNT:
  17087. type = RECV_ITEM_OVERCOUNT;
  17088. break;
  17089. case ADDITEM_OVERWEIGHT:
  17090. type = RECV_ITEM_OVERWEIGHT;
  17091. break;
  17092. case ADDITEM_STACKLIMIT:
  17093. default:
  17094. type = RECV_ITEM_FAILED;
  17095. break;
  17096. }
  17097. }
  17098. clif_roulette_recvitem_ack(sd,type);
  17099. }
  17100. /// MERGE ITEM
  17101. /**
  17102. * Acknowledge the client about item merger result
  17103. * ZC 096F <index>.W <total>.W <result>.B (ZC_ACK_MERGE_ITEM)
  17104. * @param fd
  17105. * @param sd
  17106. **/
  17107. void clif_merge_item_ack( map_session_data &sd, enum MERGE_ITEM_ACK type, uint16 index = 0, uint16 count = 0 ){
  17108. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17109. PACKET_ZC_ACK_MERGE_ITEM p = {};
  17110. p.packetType = HEADER_ZC_ACK_MERGE_ITEM;
  17111. if( type == MERGE_ITEM_SUCCESS ){
  17112. p.index = client_index( index );
  17113. }else{
  17114. p.index = index;
  17115. }
  17116. p.amount = count;
  17117. p.reason = type;
  17118. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17119. #endif
  17120. }
  17121. /**
  17122. * Check if item has the pair to be merged
  17123. * @param sd
  17124. * @param it Item
  17125. * @return True if has pair, False if not
  17126. **/
  17127. static bool clif_merge_item_has_pair(map_session_data *sd, struct item *it) {
  17128. struct item *it_;
  17129. unsigned short i;
  17130. nullpo_retr(false, sd);
  17131. 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);
  17132. if (i < MAX_INVENTORY)
  17133. return true;
  17134. return false;
  17135. }
  17136. /**
  17137. * Check if item can be merged
  17138. * @param id Item Data
  17139. * @param it Item
  17140. * @return True if can be merged, False if not
  17141. **/
  17142. static bool clif_merge_item_check(struct item_data *id, struct item *it) {
  17143. if (!id || !it)
  17144. return false;
  17145. if (id->type == IT_CASH)
  17146. return false;
  17147. if (!itemdb_isstackable2(id))
  17148. return false;
  17149. if (itemdb_isspecial(it->card[0]))
  17150. return false;
  17151. if (!it->unique_id)
  17152. return false;
  17153. return true;
  17154. }
  17155. /**
  17156. * Open available item to be merged.
  17157. * Only show 1 item in 1 process.
  17158. * ZC 096D <size>.W { <index>.W }* (ZC_MERGE_ITEM_OPEN)
  17159. * @param sd
  17160. **/
  17161. void clif_merge_item_open( map_session_data& sd ){
  17162. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17163. PACKET_ZC_MERGE_ITEM_OPEN* p = reinterpret_cast<PACKET_ZC_MERGE_ITEM_OPEN*>( packet_buffer );
  17164. p->packetType = HEADER_ZC_MERGE_ITEM_OPEN;
  17165. p->packetLen = sizeof( *p );
  17166. int n = 0;
  17167. // Get entries
  17168. for( int i = 0; i < MAX_INVENTORY; i++ ){
  17169. struct item* it = &sd.inventory.u.items_inventory[i];
  17170. if( !clif_merge_item_check( sd.inventory_data[i], it ) ){
  17171. continue;
  17172. }
  17173. if( clif_merge_item_has_pair( &sd, it ) ){
  17174. p->items[n++].index = client_index( i );
  17175. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( p->items[0] ) );
  17176. }
  17177. }
  17178. // No item need to be merged
  17179. if( n < 2 ){
  17180. clif_msg( &sd, MSI_NOT_EXIST_MERGE_ITEM );
  17181. return;
  17182. }
  17183. clif_send( p, p->packetLen, &sd.bl, SELF );
  17184. #endif
  17185. }
  17186. /**
  17187. * Process item merge request.
  17188. * 096E <size>.W { <index>.W }* (CZ_REQ_MERGE_ITEM)
  17189. * @param fd
  17190. * @param sd
  17191. **/
  17192. void clif_parse_merge_item_req( int fd, map_session_data* sd ){
  17193. const PACKET_CZ_REQ_MERGE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_MERGE_ITEM*>( RFIFOP( fd, 0 ) );
  17194. int count = ( p->packetLength - sizeof( *p ) ) / sizeof( p->indices[0] );
  17195. // No item need to be merged
  17196. if( count < 2 ){
  17197. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17198. return;
  17199. }
  17200. uint16 idx_main = server_index( p->indices[0] );
  17201. if( idx_main >= MAX_INVENTORY ){
  17202. return;
  17203. }
  17204. if( !clif_merge_item_check( sd->inventory_data[idx_main], &sd->inventory.u.items_inventory[idx_main] ) ){
  17205. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17206. return;
  17207. }
  17208. // Ensure each index only comes once
  17209. std::unordered_set<uint16> indices;
  17210. for( int i = 1; i < count; i++ ){
  17211. uint16 idx = server_index( p->indices[i] );
  17212. if( idx >= MAX_INVENTORY ){
  17213. return;
  17214. }
  17215. if( sd->inventory_data[idx] == nullptr ){
  17216. return;
  17217. }
  17218. // Check if it is the same item
  17219. if( sd->inventory_data[idx]->nameid != sd->inventory_data[idx_main]->nameid ){
  17220. return;
  17221. }
  17222. if( !clif_merge_item_check( sd->inventory_data[idx], &sd->inventory.u.items_inventory[idx] ) ){
  17223. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17224. return;
  17225. }
  17226. indices.insert( idx );
  17227. }
  17228. if( indices.empty() ){
  17229. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17230. return;
  17231. }
  17232. uint32 total_amount = sd->inventory.u.items_inventory[idx_main].amount;
  17233. for( uint16 idx : indices ){
  17234. total_amount += sd->inventory.u.items_inventory[idx].amount;
  17235. }
  17236. uint16 stack = sd->inventory_data[idx_main]->stack.amount;
  17237. if( stack == 0 ){
  17238. stack = MAX_AMOUNT;
  17239. }
  17240. if( total_amount >= stack ){
  17241. clif_merge_item_ack( *sd, MERGE_ITEM_FAILED_MAX_COUNT );
  17242. return;
  17243. }
  17244. // Merrrrge!!!!
  17245. for( uint16 idx : indices ){
  17246. uint16 amount = sd->inventory.u.items_inventory[idx].amount;
  17247. log_pick_pc( sd, LOG_TYPE_MERGE_ITEM, -amount, &sd->inventory.u.items_inventory[idx] );
  17248. memset( &sd->inventory.u.items_inventory[idx], 0, sizeof( sd->inventory.u.items_inventory[0] ) );
  17249. sd->inventory_data[idx] = nullptr;
  17250. clif_delitem( *sd, idx, amount, 0 );
  17251. }
  17252. sd->inventory.u.items_inventory[idx_main].amount = total_amount;
  17253. clif_merge_item_ack( *sd, MERGE_ITEM_SUCCESS, idx_main, total_amount );
  17254. }
  17255. /**
  17256. * Cancel item merge
  17257. * CZ 0974 (CZ_CANCEL_MERGE_ITEM)
  17258. * @param fd
  17259. * @param sd
  17260. **/
  17261. void clif_parse_merge_item_cancel(int fd, map_session_data* sd) {
  17262. return; // Nothing todo yet
  17263. }
  17264. static std::string clif_hide_name(const char* original_name)
  17265. {
  17266. std::string censored(original_name);
  17267. size_t hide = zmin(battle_config.broadcast_hide_name, censored.length() - 1);
  17268. censored.replace(censored.length() - hide, hide, hide, '*');
  17269. return censored;
  17270. }
  17271. /**
  17272. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <containerid>.W (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17273. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <srcname>.24B (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17274. * type: ITEMOBTAIN_TYPE_BOXITEM & ITEMOBTAIN_TYPE_MONSTER_ITEM "[playername] ... [sourcename] ... [itemname]" -> MsgStringTable[1629]
  17275. * type: ITEMOBTAIN_TYPE_NPC "[playername] ... [itemname]" -> MsgStringTable[1870]
  17276. **/
  17277. void clif_broadcast_obtain_special_item( const char *char_name, t_itemid nameid, t_itemid container, enum BROADCASTING_SPECIAL_ITEM_OBTAIN type ){
  17278. char name[NAME_LENGTH];
  17279. if( battle_config.broadcast_hide_name ){
  17280. std::string dispname = clif_hide_name(char_name);
  17281. safestrncpy(name, dispname.c_str(), sizeof(name));
  17282. }else{
  17283. safestrncpy( name, char_name, sizeof( name ) );
  17284. }
  17285. switch( type ){
  17286. case ITEMOBTAIN_TYPE_BOXITEM:
  17287. #if PACKETVER >= 20091201
  17288. {
  17289. PACKET_ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN_item p = {};
  17290. p.PacketType = package_item_announceType;
  17291. p.PacketLength = sizeof( p );
  17292. p.type = type;
  17293. p.ItemID = client_nameid( nameid );
  17294. p.len = NAME_LENGTH;
  17295. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17296. p.boxItemID_len = (int8)sizeof( p.BoxItemID );
  17297. p.BoxItemID = client_nameid( container );
  17298. #if PACKETVER_MAIN_NUM >= 20220518 || PACKETVER_ZERO_NUM >= 20220518
  17299. p.refineLevel_len = (int8)sizeof( p.refineLevel );
  17300. p.refineLevel = 0; // TODO: implement
  17301. #endif
  17302. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17303. }
  17304. #endif
  17305. break;
  17306. case ITEMOBTAIN_TYPE_MONSTER_ITEM:
  17307. case ITEMOBTAIN_TYPE_NPC:
  17308. {
  17309. struct packet_item_drop_announce p;
  17310. p.PacketType = item_drop_announceType;
  17311. p.type = type;
  17312. p.ItemID = client_nameid( nameid );
  17313. p.len = NAME_LENGTH;
  17314. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17315. if( type == ITEMOBTAIN_TYPE_MONSTER_ITEM ){
  17316. std::shared_ptr<s_mob_db> db = mob_db.find( container );
  17317. p.monsterNameLen = NAME_LENGTH;
  17318. safestrncpy( p.monsterName, db->name.c_str(), NAME_LENGTH );
  17319. }else{
  17320. p.monsterNameLen = 0;
  17321. p.monsterName[0] = '\0';
  17322. }
  17323. #if PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  17324. p.PacketLength = 12 + p.len + p.monsterNameLen;
  17325. #else
  17326. p.PacketLength = 8 + p.len + p.monsterNameLen;
  17327. #endif
  17328. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17329. }
  17330. break;
  17331. }
  17332. }
  17333. /// Show body view windows (ZC_DRESSROOM_OPEN).
  17334. /// 0A02 <view>.W
  17335. /// Value <flag> has the following effects:
  17336. /// 1: Open a Dress Room window.
  17337. void clif_dressing_room(map_session_data *sd, int flag) {
  17338. #if PACKETVER >= 20150513
  17339. int fd = sd->fd;
  17340. nullpo_retv(sd);
  17341. WFIFOHEAD(fd, packet_len(0xa02));
  17342. WFIFOW(fd,0) = 0xa02;
  17343. WFIFOW(fd,2) = flag;
  17344. WFIFOSET(fd, packet_len(0xa02));
  17345. #endif
  17346. }
  17347. /// Parsing a request from the client item identify oneclick (CZ_REQ_ONECLICK_ITEMIDENTIFY).
  17348. /// 0A35 <result>.W
  17349. void clif_parse_Oneclick_Itemidentify(int fd, map_session_data *sd) {
  17350. #if PACKETVER >= 20150513
  17351. short idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2, magnifier_idx;
  17352. // Ignore the request
  17353. // - Invalid item index
  17354. // - Invalid item ID or item doesn't exist
  17355. // - Item is already identified
  17356. if (idx < 0 || idx >= MAX_INVENTORY ||
  17357. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  17358. sd->inventory.u.items_inventory[idx].identify)
  17359. return;
  17360. // Ignore the request - No magnifiers in inventory
  17361. if ((magnifier_idx = pc_search_inventory(sd, ITEMID_MAGNIFIER)) == -1 &&
  17362. (magnifier_idx = pc_search_inventory(sd, ITEMID_NOVICE_MAGNIFIER)) == -1)
  17363. return;
  17364. if (pc_delitem(sd, magnifier_idx, 1, 0, 0, LOG_TYPE_OTHER) != 0) // Deleting of magnifier failed, for whatever reason...
  17365. return;
  17366. skill_identify(sd, idx);
  17367. #endif
  17368. }
  17369. /// Starts navigation to the given target on client side
  17370. void clif_navigateTo(map_session_data *sd, const char* mapname, uint16 x, uint16 y, uint8 flag, bool hideWindow, uint16 mob_id ){
  17371. #if PACKETVER >= 20111010
  17372. int fd = sd->fd;
  17373. WFIFOHEAD(fd,27);
  17374. WFIFOW(fd,0) = 0x08e2;
  17375. // How detailed will our navigation be?
  17376. if( mob_id > 0 ){
  17377. x = 0;
  17378. y = 0;
  17379. WFIFOB(fd,2) = 3; // monster with destination field
  17380. }else if( x > 0 && y > 0 ){
  17381. WFIFOB(fd,2) = 0; // with coordinates
  17382. }else{
  17383. x = 0;
  17384. y = 0;
  17385. WFIFOB(fd,2) = 1; // without coordinates(will fail if you are already on the map)
  17386. }
  17387. // Which services can be used for transportation?
  17388. WFIFOB(fd,3) = flag;
  17389. // If this flag is set, the navigation window will not be opened up
  17390. WFIFOB(fd,4) = hideWindow;
  17391. // Target map
  17392. safestrncpy( WFIFOCP(fd,5),mapname,MAP_NAME_LENGTH_EXT);
  17393. // Target x
  17394. WFIFOW(fd,21) = x;
  17395. // Target y
  17396. WFIFOW(fd,23) = y;
  17397. // Target monster ID
  17398. WFIFOW(fd,25) = mob_id;
  17399. WFIFOSET(fd,27);
  17400. #endif
  17401. }
  17402. /// Send hat effects to the client.
  17403. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17404. void clif_hat_effects( map_session_data& sd, block_list& bl, enum send_target target ){
  17405. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17406. map_session_data *tsd;
  17407. block_list* tbl;
  17408. if( target == SELF ){
  17409. tsd = BL_CAST(BL_PC,&bl);
  17410. tbl = &sd.bl;
  17411. }else{
  17412. tsd = &sd;
  17413. tbl = &bl;
  17414. }
  17415. if( tsd == nullptr ){
  17416. return;
  17417. }
  17418. if( tsd->hatEffects.empty() || map_getmapdata(tbl->m)->getMapFlag(MF_NOCOSTUME) ){
  17419. return;
  17420. }
  17421. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17422. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17423. p->packetLength = sizeof( *p );
  17424. p->aid = tsd->bl.id;
  17425. p->status = 1;
  17426. for( size_t i = 0; i < tsd->hatEffects.size(); i++ ){
  17427. p->effects[i] = tsd->hatEffects[i];
  17428. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17429. }
  17430. clif_send( p, p->packetLength, tbl, target );
  17431. #endif
  17432. }
  17433. /// Send a single hat effect to the client.
  17434. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17435. void clif_hat_effect_single( map_session_data& sd, uint16 effectId, bool enable ){
  17436. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17437. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17438. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17439. p->packetLength = sizeof( *p );
  17440. p->aid = sd.bl.id;
  17441. p->status = enable;
  17442. p->effects[0] = effectId;
  17443. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17444. clif_send( p, p->packetLength, &sd.bl, AREA );
  17445. #endif
  17446. }
  17447. /// Notify the client that a sale has started
  17448. /// 09b2 <item id>.W <remaining time>.L (ZC_NOTIFY_BARGAIN_SALE_SELLING)
  17449. void clif_sale_start( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17450. #if PACKETVER_SUPPORTS_SALES
  17451. PACKET_ZC_NOTIFY_BARGAIN_SALE_SELLING p = {};
  17452. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_SELLING;
  17453. p.itemId = client_nameid( sale_item->nameid );
  17454. p.remainingTime = (uint32)(sale_item->end - time(nullptr)); // time in S
  17455. clif_send( &p, sizeof( p ), bl, target );
  17456. #endif
  17457. }
  17458. /// Notify the client that a sale has ended
  17459. /// 09b3 <item id>.W (ZC_NOTIFY_BARGAIN_SALE_CLOSE)
  17460. void clif_sale_end( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17461. #if PACKETVER_SUPPORTS_SALES
  17462. PACKET_ZC_NOTIFY_BARGAIN_SALE_CLOSE p = {};
  17463. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_CLOSE;
  17464. p.itemId = client_nameid( sale_item->nameid );
  17465. clif_send( &p, sizeof( p ), bl, target );
  17466. #endif
  17467. }
  17468. /// Update the remaining amount of a sale item.
  17469. /// 09c4 <item id>.W <amount>.L (ZC_ACK_COUNT_BARGAIN_SALE_ITEM)
  17470. void clif_sale_amount( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17471. #if PACKETVER_SUPPORTS_SALES
  17472. PACKET_ZC_ACK_COUNT_BARGAIN_SALE_ITEM p = {};
  17473. p.packetType = HEADER_ZC_ACK_COUNT_BARGAIN_SALE_ITEM;
  17474. p.itemId = client_nameid( sale_item->nameid );
  17475. p.amount = sale_item->amount;
  17476. clif_send( &p, sizeof( p ), bl, target );
  17477. #endif
  17478. }
  17479. /// The client requested a refresh of the current remaining count of a sale item
  17480. /// 09ac <account id>.L <item id>.W (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17481. void clif_parse_sale_refresh( int fd, map_session_data* sd ){
  17482. #if PACKETVER_SUPPORTS_SALES
  17483. const PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO* p = reinterpret_cast<PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO*>( RFIFOP( fd, 0 ) );
  17484. struct sale_item_data* sale;
  17485. if( p->AID != sd->status.account_id ){
  17486. return;
  17487. }
  17488. sale = sale_find_item( p->itemId, true );
  17489. if( sale == nullptr ){
  17490. return;
  17491. }
  17492. clif_sale_amount(sale, &sd->bl, SELF);
  17493. #endif
  17494. }
  17495. /// Opens the sale administration window on the client
  17496. /// 09b5 (ZC_OPEN_BARGAIN_SALE_TOOL)
  17497. void clif_sale_open( map_session_data* sd ){
  17498. #if PACKETVER_SUPPORTS_SALES
  17499. nullpo_retv(sd);
  17500. if( sd->state.sale_open ){
  17501. return;
  17502. }
  17503. sd->state.sale_open = true;
  17504. int fd = sd->fd;
  17505. WFIFOHEAD(fd, 2);
  17506. WFIFOW(fd, 0) = 0x9b5;
  17507. WFIFOSET(fd, 2);
  17508. #endif
  17509. }
  17510. /// Client request to open the sale administration window.
  17511. /// This is sent by /limitedsale
  17512. /// 09b4 <account id>.L (CZ_OPEN_BARGAIN_SALE_TOOL)
  17513. void clif_parse_sale_open( int fd, map_session_data* sd ){
  17514. #if PACKETVER_SUPPORTS_SALES
  17515. nullpo_retv(sd);
  17516. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17517. return;
  17518. }
  17519. char command[CHAT_SIZE_MAX];
  17520. safesnprintf( command, sizeof(command), "%climitedsale", atcommand_symbol );
  17521. is_atcommand(fd, sd, command, 1);
  17522. #endif
  17523. }
  17524. /// Closes the sale administration window on the client.
  17525. /// 09bd (ZC_CLOSE_BARGAIN_SALE_TOOL)
  17526. void clif_sale_close(map_session_data* sd) {
  17527. #if PACKETVER_SUPPORTS_SALES
  17528. nullpo_retv(sd);
  17529. if( !sd->state.sale_open ){
  17530. return;
  17531. }
  17532. sd->state.sale_open = false;
  17533. int fd = sd->fd;
  17534. WFIFOHEAD(fd, 2);
  17535. WFIFOW(fd, 0) = 0x9bd;
  17536. WFIFOSET(fd, 2);
  17537. #endif
  17538. }
  17539. /// Client request to close the sale administration window.
  17540. /// 09bc (CZ_CLOSE_BARGAIN_SALE_TOOL)
  17541. void clif_parse_sale_close(int fd, map_session_data* sd) {
  17542. #if PACKETVER_SUPPORTS_SALES
  17543. nullpo_retv(sd);
  17544. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17545. return;
  17546. }
  17547. clif_sale_close(sd);
  17548. #endif
  17549. }
  17550. /// Reply to a item search request for item sale administration.
  17551. /// 09ad <result>.W <item id>.W <price>.L (ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO)
  17552. void clif_sale_search_reply( map_session_data* sd, std::shared_ptr<s_cash_item> item ){
  17553. #if PACKETVER_SUPPORTS_SALES
  17554. PACKET_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO p = {};
  17555. p.packetType = HEADER_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO;
  17556. if( item != nullptr ){
  17557. p.result = 0;
  17558. p.itemId = client_nameid( item->nameid );
  17559. p.price = item->price;
  17560. }else{
  17561. p.result = 1;
  17562. p.itemId = 0;
  17563. p.price = 0;
  17564. }
  17565. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17566. #endif
  17567. }
  17568. /// Search request for an item sale administration.
  17569. /// 09ac <length>.W <account id>.L <item name>.?B (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17570. void clif_parse_sale_search( int fd, map_session_data* sd ){
  17571. #if PACKETVER_SUPPORTS_SALES
  17572. char item_name[ITEM_NAME_LENGTH];
  17573. nullpo_retv(sd);
  17574. if( RFIFOL(fd, 4) != sd->status.account_id ){
  17575. return;
  17576. }
  17577. if( !sd->state.sale_open ){
  17578. return;
  17579. }
  17580. safestrncpy( item_name, RFIFOCP(fd, 8), min(RFIFOW(fd, 2) - 7, ITEM_NAME_LENGTH) );
  17581. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  17582. // not found
  17583. if( id == nullptr ){
  17584. clif_sale_search_reply( sd, nullptr );
  17585. return;
  17586. }
  17587. clif_sale_search_reply( sd, cash_shop_db.findItemInTab( CASHSHOP_TAB_SALE, id->nameid ) );
  17588. #endif
  17589. }
  17590. /// Reply if an item was successfully put on sale or not.
  17591. /// 09af <result>.W (ZC_ACK_APPLY_BARGAIN_SALE_ITEM)
  17592. void clif_sale_add_reply( map_session_data* sd, enum e_sale_add_result result ){
  17593. #if PACKETVER_SUPPORTS_SALES
  17594. int fd = sd->fd;
  17595. WFIFOHEAD(fd, 4);
  17596. WFIFOW(fd, 0) = 0x9af;
  17597. WFIFOW(fd, 2) = (uint16)result;
  17598. WFIFOSET(fd, 4);
  17599. #endif
  17600. }
  17601. /// A client request to put an item on sale.
  17602. /// 09ae <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.B (CZ_REQ_APPLY_BARGAIN_SALE_ITEM)
  17603. /// 0a3d <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.W (CZ_REQ_APPLY_BARGAIN_SALE_ITEM2)
  17604. void clif_parse_sale_add( int fd, map_session_data* sd ){
  17605. #if PACKETVER_SUPPORTS_SALES
  17606. nullpo_retv( sd );
  17607. const PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17608. if( p->AID != sd->status.account_id ){
  17609. return;
  17610. }
  17611. if( !sd->state.sale_open ){
  17612. return;
  17613. }
  17614. time_t endTime = p->startTime + p->hours * 60 * 60;
  17615. clif_sale_add_reply( sd, sale_add_item( p->itemId, p->amount, p->startTime, endTime ) );
  17616. #endif
  17617. }
  17618. /// Reply to an item removal from sale.
  17619. /// 09b1 <result>.W (ZC_ACK_REMOVE_BARGAIN_SALE_ITEM)
  17620. void clif_sale_remove_reply( map_session_data* sd, bool failed ){
  17621. #if PACKETVER_SUPPORTS_SALES
  17622. int fd = sd->fd;
  17623. WFIFOHEAD(fd, 4);
  17624. WFIFOW(fd, 0) = 0x9b1;
  17625. WFIFOW(fd, 2) = failed;
  17626. WFIFOSET(fd, 4);
  17627. #endif
  17628. }
  17629. /// Request to remove an item from sale.
  17630. /// 09b0 <account id>.L <item id>.W (CZ_REQ_REMOVE_BARGAIN_SALE_ITEM)
  17631. void clif_parse_sale_remove( int fd, map_session_data* sd ){
  17632. #if PACKETVER_SUPPORTS_SALES
  17633. nullpo_retv( sd );
  17634. const PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17635. if( p->AID != sd->status.account_id ){
  17636. return;
  17637. }
  17638. if( !sd->state.sale_open ){
  17639. return;
  17640. }
  17641. clif_sale_remove_reply( sd, !sale_remove_item( p->itemId ) );
  17642. #endif
  17643. }
  17644. /// Achievement System
  17645. /// Author: Luxuri, Aleos
  17646. /**
  17647. * Sends all achievement data to the client (ZC_ALL_ACH_LIST).
  17648. * 0a23 <packetLength>.W <ACHCount>.L <ACHPoint>.L
  17649. */
  17650. void clif_achievement_list_all(map_session_data *sd)
  17651. {
  17652. nullpo_retv(sd);
  17653. if (!battle_config.feature_achievement) {
  17654. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17655. return;
  17656. }
  17657. int count = sd->achievement_data.count; // All achievements should be sent to the client
  17658. if (count == 0)
  17659. return;
  17660. int len = (50 * count) + 22;
  17661. int fd = sd->fd;
  17662. int *info = achievement_level(sd, true);
  17663. WFIFOHEAD(fd,len);
  17664. WFIFOW(fd, 0) = 0xa23;
  17665. WFIFOW(fd, 2) = len;
  17666. WFIFOL(fd, 4) = count; // Amount of achievements the player has in their list (started/completed)
  17667. WFIFOL(fd, 8) = sd->achievement_data.total_score; // Top number
  17668. WFIFOW(fd, 12) = sd->achievement_data.level; // Achievement Level (gold circle)
  17669. WFIFOL(fd, 14) = info[0]; // Achievement EXP (left number in bar)
  17670. WFIFOL(fd, 18) = info[1]; // Achievement EXP TNL (right number in bar)
  17671. for (int i = 0; i < count; i++) {
  17672. WFIFOL(fd, i * 50 + 22) = (uint32)sd->achievement_data.achievements[i].achievement_id;
  17673. WFIFOB(fd, i * 50 + 26) = (uint32)sd->achievement_data.achievements[i].completed > 0;
  17674. for (int j = 0; j < MAX_ACHIEVEMENT_OBJECTIVES; j++)
  17675. WFIFOL(fd, (i * 50) + 27 + (j * 4)) = (uint32)sd->achievement_data.achievements[i].count[j];
  17676. WFIFOL(fd, i * 50 + 67) = (uint32)sd->achievement_data.achievements[i].completed;
  17677. WFIFOB(fd, i * 50 + 71) = sd->achievement_data.achievements[i].rewarded > 0;
  17678. }
  17679. WFIFOSET(fd, len);
  17680. }
  17681. /**
  17682. * Sends a single achievement's data to the client (ZC_ACH_UPDATE).
  17683. * 0a24 <ACHPoint>.L
  17684. */
  17685. void clif_achievement_update(map_session_data *sd, struct achievement *ach, int count)
  17686. {
  17687. nullpo_retv(sd);
  17688. if (!battle_config.feature_achievement) {
  17689. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17690. return;
  17691. }
  17692. int fd = sd->fd;
  17693. int *info = achievement_level(sd, true);
  17694. WFIFOHEAD(fd, packet_len(0xa24));
  17695. WFIFOW(fd, 0) = 0xa24;
  17696. WFIFOL(fd, 2) = sd->achievement_data.total_score; // Total Achievement Points (top of screen)
  17697. WFIFOW(fd, 6) = sd->achievement_data.level; // Achievement Level (gold circle)
  17698. WFIFOL(fd, 8) = info[0]; // Achievement EXP (left number in bar)
  17699. WFIFOL(fd, 12) = info[1]; // Achievement EXP TNL (right number in bar)
  17700. if (ach) {
  17701. WFIFOL(fd, 16) = ach->achievement_id; // Achievement ID
  17702. WFIFOB(fd, 20) = ach->completed > 0; // Is it complete?
  17703. for (int i = 0; i < MAX_ACHIEVEMENT_OBJECTIVES; i++)
  17704. WFIFOL(fd, 21 + (i * 4)) = (uint32)ach->count[i]; // 1~10 pre-reqs
  17705. WFIFOL(fd, 61) = (uint32)ach->completed; // Epoch time
  17706. WFIFOB(fd, 65) = ach->rewarded > 0; // Got reward?
  17707. } else
  17708. memset(WFIFOP(fd, 16), 0, 40);
  17709. WFIFOSET(fd, packet_len(0xa24));
  17710. }
  17711. /**
  17712. * Checks if an achievement reward can be rewarded (CZ_REQ_ACH_REWARD).
  17713. * 0a25 <achievementID>.L
  17714. */
  17715. void clif_parse_AchievementCheckReward(int fd, map_session_data *sd)
  17716. {
  17717. nullpo_retv(sd);
  17718. if( sd->achievement_data.save )
  17719. intif_achievement_save(sd);
  17720. achievement_check_reward(sd, RFIFOL(fd,2));
  17721. }
  17722. /**
  17723. * Returns the result of achievement_check_reward (ZC_REQ_ACH_REWARD_ACK).
  17724. * 0a26 <result>.W <achievementID>.L
  17725. */
  17726. void clif_achievement_reward_ack(int fd, unsigned char result, int achievement_id)
  17727. {
  17728. WFIFOHEAD(fd, packet_len(0xa26));
  17729. WFIFOW(fd, 0) = 0xa26;
  17730. WFIFOB(fd, 2) = result;
  17731. WFIFOL(fd, 3) = achievement_id;
  17732. WFIFOSET(fd, packet_len(0xa26));
  17733. }
  17734. /// Process the pet evolution request
  17735. /// 09fb <packetType>.W <packetLength>.W <evolutionPetEggITID>.W (CZ_PET_EVOLUTION)
  17736. void clif_parse_pet_evolution( int fd, map_session_data *sd ){
  17737. #if PACKETVER >= 20141008
  17738. const PACKET_CZ_PET_EVOLUTION* p = reinterpret_cast<PACKET_CZ_PET_EVOLUTION*>( RFIFOP( fd, 0 ) );
  17739. auto pet = pet_db_search( p->EvolvedPetEggID, PET_EGG );
  17740. if (!pet) {
  17741. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_NOT_PETEGG);
  17742. return;
  17743. }
  17744. pet_evolution(sd, pet->class_);
  17745. #endif
  17746. }
  17747. /// Sends the result of the evolution to the client.
  17748. /// 09fc <result>.L (ZC_PET_EVOLUTION_RESULT)
  17749. void clif_pet_evolution_result( map_session_data* sd, e_pet_evolution_result result ){
  17750. #if PACKETVER >= 20141008
  17751. int fd = sd->fd;
  17752. WFIFOHEAD(fd, packet_len(0x9fc));
  17753. WFIFOW(fd, 0) = 0x9fc;
  17754. WFIFOL(fd, 2) = static_cast<uint32>(result);
  17755. WFIFOSET(fd, packet_len(0x9fc));
  17756. #endif
  17757. }
  17758. /*
  17759. * This packet is sent by /changedress or /nocosplay
  17760. *
  17761. * 0ae8
  17762. */
  17763. void clif_parse_changedress( int fd, map_session_data* sd ){
  17764. #if PACKETVER >= 20180103
  17765. char command[CHAT_SIZE_MAX];
  17766. safesnprintf( command, sizeof(command), "%cchangedress", atcommand_symbol );
  17767. is_atcommand( fd, sd, command, 1 );
  17768. #endif
  17769. }
  17770. /// Opens an UI window of the given type and initializes it with the given data
  17771. /// 0AE2 <type>.B <data>.L
  17772. void clif_ui_open( map_session_data& sd, enum out_ui_type ui_type, int32 data ){
  17773. #if PACKETVER >= 20151202
  17774. // If the UI requires state tracking
  17775. switch( ui_type ){
  17776. case OUT_UI_STYLIST:
  17777. sd.state.stylist_open = true;
  17778. break;
  17779. case OUT_UI_ENCHANTGRADE:
  17780. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  17781. sd.state.enchantgrade_open = true;
  17782. break;
  17783. #else
  17784. return;
  17785. #endif
  17786. }
  17787. PACKET_ZC_UI_OPEN p = {};
  17788. p.PacketType = HEADER_ZC_UI_OPEN;
  17789. p.UIType = ui_type;
  17790. #if PACKETVER >= 20171122
  17791. p.data = data;
  17792. #endif
  17793. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17794. #endif
  17795. }
  17796. /// Request to open an UI window of the given type
  17797. /// 0A68 <type>.B
  17798. void clif_parse_open_ui( int fd, map_session_data* sd ){
  17799. switch( RFIFOB(fd,2) ){
  17800. case IN_UI_ATTENDANCE:
  17801. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  17802. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 791 ), false, SELF ); // You are not allowed to use the attendance system.
  17803. }else if( pc_attendance_enabled() ){
  17804. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  17805. }else{
  17806. clif_msg_color( sd, MSI_CHECK_ATTENDANCE_NOT_EVENT, color_table[COLOR_RED] );
  17807. }
  17808. break;
  17809. #if PACKETVER >= 20160316
  17810. case IN_UI_MACRO_REGISTER:
  17811. clif_ui_open(*sd, OUT_UI_CAPTCHA, 0);
  17812. break;
  17813. case IN_UI_MACRO_DETECTOR:
  17814. clif_ui_open(*sd, OUT_UI_MACRO, 0);
  17815. break;
  17816. #endif
  17817. }
  17818. }
  17819. /// Response for attedance request
  17820. /// 0AF0 <unknown>.L <data>.L
  17821. void clif_attendence_response( map_session_data *sd, int32 data ){
  17822. nullpo_retv(sd);
  17823. int fd = sd->fd;
  17824. WFIFOHEAD(fd,packet_len(0xAF0));
  17825. WFIFOW(fd,0) = 0xAF0;
  17826. WFIFOL(fd,2) = 0;
  17827. WFIFOL(fd,6) = data;
  17828. WFIFOSET(fd,packet_len(0xAF0));
  17829. }
  17830. /// Request from the client to retrieve today's attendance reward
  17831. /// 0AEF
  17832. void clif_parse_attendance_request( int fd, map_session_data* sd ){
  17833. pc_attendance_claim_reward(sd);
  17834. }
  17835. /// Send out the percentage of weight that causes it to be displayed in red.
  17836. /// 0ADE <percentage>.L
  17837. void clif_weight_limit( map_session_data* sd ){
  17838. #if PACKETVER >= 20171025
  17839. nullpo_retv(sd);
  17840. int fd = sd->fd;
  17841. WFIFOHEAD(fd, packet_len(0xADE));
  17842. WFIFOW(fd, 0) = 0xADE;
  17843. #ifdef RENEWAL
  17844. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate_renewal;
  17845. #else
  17846. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate;
  17847. #endif
  17848. WFIFOSET(fd, packet_len(0xADE));
  17849. #endif
  17850. }
  17851. enum e_private_airship_response : uint32{
  17852. PRIVATEAIRSHIP_OK,
  17853. PRIVATEAIRSHIP_RETRY,
  17854. PRIVATEAIRSHIP_ITEM_NOT_ENOUGH,
  17855. PRIVATEAIRSHIP_DESTINATION_MAP_INVALID,
  17856. PRIVATEAIRSHIP_SOURCE_MAP_INVALID,
  17857. PRIVATEAIRSHIP_ITEM_UNAVAILABLE
  17858. };
  17859. /// Send out the response to a private airship request
  17860. /// 0A4A <response>.L
  17861. void clif_private_airship_response( map_session_data* sd, enum e_private_airship_response response ){
  17862. #if PACKETVER >= 20180321
  17863. nullpo_retv( sd );
  17864. int fd = sd->fd;
  17865. WFIFOHEAD( fd, packet_len( 0xA4A ) );
  17866. WFIFOW( fd, 0 ) = 0xA4A;
  17867. WFIFOL( fd, 2 ) = response;
  17868. WFIFOSET( fd, packet_len( 0xA4A ) );
  17869. #endif
  17870. }
  17871. /// Parses a request for a private airship
  17872. /// 0A49 <mapname>.16B <itemid>.W
  17873. void clif_parse_private_airship_request( int fd, map_session_data* sd ){
  17874. #if PACKETVER >= 20180321
  17875. // Check if the feature is enabled
  17876. if( !battle_config.feature_privateairship ){
  17877. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 792 ), false, SELF ); // The private airship system is disabled.
  17878. return;
  17879. }
  17880. // Check if the player is allowed to warp from the source map
  17881. if( !map_getmapflag( sd->bl.m, MF_PRIVATEAIRSHIP_SOURCE ) ){
  17882. clif_private_airship_response( sd, PRIVATEAIRSHIP_SOURCE_MAP_INVALID );
  17883. return;
  17884. }
  17885. const PACKET_CZ_PRIVATE_AIRSHIP_REQUEST* p = reinterpret_cast<PACKET_CZ_PRIVATE_AIRSHIP_REQUEST*>( RFIFOP( fd, 0 ) );
  17886. char mapname[MAP_NAME_LENGTH_EXT];
  17887. safestrncpy( mapname, p->mapName, MAP_NAME_LENGTH_EXT );
  17888. int16 mapindex = mapindex_name2id( mapname );
  17889. // Check if we know the mapname
  17890. if( mapindex < 0 ){
  17891. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  17892. return;
  17893. }
  17894. int16 mapid = map_mapindex2mapid( mapindex );
  17895. // Check if the map is available on this server
  17896. if( mapid < 0 ){
  17897. // TODO: add multi map-server support, cant validate the mapflags of the other server
  17898. return;
  17899. }
  17900. // Check if the player tried to warp to the map he is on
  17901. if( sd->bl.m == mapid ){
  17902. // This is blocked by the client, but just to be sure
  17903. return;
  17904. }
  17905. // This can only be a hack, so we prevent it
  17906. if( map_getmapdata( mapid )->instance_id ){
  17907. // Ignore requests to warp directly into a running instance
  17908. return;
  17909. }
  17910. // Check if the player is allowed to warp to the target map
  17911. if( !map_getmapflag( mapid, MF_PRIVATEAIRSHIP_DESTINATION ) ){
  17912. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  17913. return;
  17914. }
  17915. t_itemid item_id = p->ItemID;
  17916. // Check if the item sent by the client is known to us
  17917. if( !itemdb_group.item_exists(IG_PRIVATE_AIRSHIP, item_id) ){
  17918. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_UNAVAILABLE );
  17919. return;
  17920. }
  17921. int idx = pc_search_inventory( sd, item_id );
  17922. // Check if the player has the item at all
  17923. if( idx < 0 ){
  17924. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_NOT_ENOUGH );
  17925. return;
  17926. }
  17927. // Delete the chosen item
  17928. if( pc_delitem( sd, idx, 1, 0, 0, LOG_TYPE_PRIVATE_AIRSHIP ) ){
  17929. clif_private_airship_response( sd, PRIVATEAIRSHIP_RETRY );
  17930. return;
  17931. }
  17932. // Warp the player to a random spot on the destination map
  17933. pc_setpos( sd, mapindex, 0, 0, CLR_TELEPORT );
  17934. #endif
  17935. }
  17936. /// Sends out the usage history of the guild storage
  17937. /// 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
  17938. /// { <card item id>.W }*4 <name>.24B <time>.24B <attribute>.B }*count (ZC_ACK_GUILDSTORAGE_LOG)
  17939. void clif_guild_storage_log( map_session_data& sd, std::vector<struct guild_log_entry>& log, enum e_guild_storage_log result ){
  17940. #if PACKETVER >= 20140205
  17941. PACKET_ZC_ACK_GUILDSTORAGE_LOG* p = reinterpret_cast<PACKET_ZC_ACK_GUILDSTORAGE_LOG*>( packet_buffer );
  17942. p->packetType = HEADER_ZC_ACK_GUILDSTORAGE_LOG;
  17943. p->PacketLength = sizeof( *p );
  17944. p->result = result;
  17945. if( result == GUILDSTORAGE_LOG_FINAL_SUCCESS ){
  17946. p->amount = (uint16)log.size();
  17947. for( int i = 0; i < log.size(); i++ ){
  17948. PACKET_ZC_ACK_GUILDSTORAGE_LOG_sub& item = p->items[i];
  17949. struct guild_log_entry& entry = log[i];
  17950. item.id = entry.id;
  17951. item.itemId = client_nameid( entry.item.nameid );
  17952. item.amount = (uint16)( entry.amount > 0 ? entry.amount : ( entry.amount * -1 ) );
  17953. item.action = entry.amount > 0; // action = true(put), false(get)
  17954. item.refine = entry.item.refine;
  17955. item.uniqueId = entry.item.unique_id;
  17956. item.IsIdentified = entry.item.identify;
  17957. item.itemType = itemtype( entry.item.nameid );
  17958. clif_addcards( &item.slot, &entry.item );
  17959. safestrncpy( item.name, entry.name, NAME_LENGTH );
  17960. safestrncpy( item.time, entry.time, NAME_LENGTH );
  17961. item.attribute = entry.item.attribute;
  17962. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof ( item ) );
  17963. }
  17964. }else{
  17965. p->amount = 0;
  17966. }
  17967. clif_send( p, p->PacketLength, &sd.bl, SELF );
  17968. #endif
  17969. }
  17970. /// Activates the client camera info or updates the client camera with the given values.
  17971. /// 0A78 <type>.B <range>.F <rotation>.F <latitude>.F
  17972. void clif_camerainfo( map_session_data* sd, bool show, float range, float rotation, float latitude ){
  17973. #if PACKETVER >= 20160525
  17974. int fd = sd->fd;
  17975. WFIFOHEAD(fd, packet_len(0xa78));
  17976. WFIFOW(fd, 0) = 0xa78;
  17977. WFIFOB(fd, 2) = show;
  17978. WFIFOF(fd, 3) = range;
  17979. WFIFOF(fd, 7) = rotation;
  17980. WFIFOF(fd, 11) = latitude;
  17981. WFIFOSET(fd, packet_len(0xa78));
  17982. #endif
  17983. }
  17984. /// Activates or deactives the client camera info or updates the camera settings.
  17985. /// This packet is triggered by /viewpointvalue or /setcamera
  17986. /// 0A77 <type>.B <range>.F <rotation>.F <latitude>.F
  17987. void clif_parse_camerainfo( int fd, map_session_data* sd ){
  17988. char command[CHAT_SIZE_MAX];
  17989. // /viewpointvalue
  17990. if( RFIFOB( fd, 2 ) == 1 ){
  17991. safesnprintf( command, sizeof( command ), "%ccamerainfo", atcommand_symbol );
  17992. // /setcamera
  17993. }else{
  17994. safesnprintf( command, sizeof( command ), "%ccamerainfo %03.03f %03.03f %03.03f", atcommand_symbol, RFIFOF( fd, 3 ), RFIFOF( fd, 7 ), RFIFOF( fd, 11 ) );
  17995. }
  17996. is_atcommand( fd, sd, command, 1 );
  17997. }
  17998. /// Send the full list of items in the equip switch window
  17999. /// 0a9b <length>.W { <index>.W <position>.L }*
  18000. void clif_equipswitch_list( map_session_data* sd ){
  18001. #if PACKETVER >= 20170208
  18002. int fd = sd->fd;
  18003. int offset, i, position;
  18004. WFIFOW(fd, 0) = 0xa9b;
  18005. for( i = 0, offset = 4, position = 0; i < EQI_MAX; i++ ){
  18006. short index = sd->equip_switch_index[i];
  18007. if( index >= 0 && !( position & equip_bitmask[i] ) ){
  18008. WFIFOW(fd, offset) = index + 2;
  18009. WFIFOL(fd, offset + 2) = sd->inventory.u.items_inventory[index].equipSwitch;
  18010. position |= sd->inventory.u.items_inventory[index].equipSwitch;
  18011. offset += 6;
  18012. }
  18013. }
  18014. WFIFOW(fd, 2) = offset;
  18015. WFIFOSET(fd, offset);
  18016. #endif
  18017. }
  18018. /// Acknowledgement for removing an equip to the equip switch window
  18019. /// 0a9a <index>.W <position.>.L <failure>.W
  18020. void clif_equipswitch_remove( map_session_data* sd, uint16 index, uint32 pos, bool failed ){
  18021. #if PACKETVER >= 20170208
  18022. int fd = sd->fd;
  18023. WFIFOHEAD(fd, packet_len(0xa9a));
  18024. WFIFOW(fd, 0) = 0xa9a;
  18025. WFIFOW(fd, 2) = index + 2;
  18026. WFIFOL(fd, 4) = pos;
  18027. WFIFOW(fd, 8) = failed;
  18028. WFIFOSET(fd, packet_len(0xa9a));
  18029. #endif
  18030. }
  18031. /// Request to remove an equip from the equip switch window
  18032. /// 0a99 <index>.W <position>.L <= 20170502
  18033. /// 0a99 <index>.W
  18034. void clif_parse_equipswitch_remove( int fd, map_session_data* sd ){
  18035. #if PACKETVER >= 20170208
  18036. uint16 index = RFIFOW(fd, 2) - 2;
  18037. bool removed = false;
  18038. if( !battle_config.feature_equipswitch ){
  18039. return;
  18040. }
  18041. // Check if the index is valid
  18042. if( index >= MAX_INVENTORY ){
  18043. return;
  18044. }
  18045. pc_equipswitch_remove( sd, index );
  18046. #endif
  18047. }
  18048. /// Acknowledgement for adding an equip to the equip switch window
  18049. /// 0a98 <index>.W <position.>.L <flag>.L <= 20170502
  18050. /// 0a98 <index>.W <position.>.L <flag>.W
  18051. void clif_equipswitch_add( map_session_data* sd, uint16 index, uint32 pos, uint8 flag ){
  18052. #if PACKETVER >= 20170208
  18053. int fd = sd->fd;
  18054. WFIFOHEAD(fd, packet_len(0xa98));
  18055. WFIFOW(fd, 0) = 0xa98;
  18056. WFIFOW(fd, 2) = index + 2;
  18057. WFIFOL(fd, 4) = pos;
  18058. #if PACKETVER <= 20170502
  18059. WFIFOL(fd, 8) = flag;
  18060. #else
  18061. WFIFOW(fd, 8) = flag;
  18062. #endif
  18063. WFIFOSET(fd,packet_len(0xa98));
  18064. #endif
  18065. }
  18066. /// Request to add an equip to the equip switch window
  18067. /// 0a97 <index>.W <position>.L
  18068. void clif_parse_equipswitch_add( int fd, map_session_data* sd ){
  18069. #if PACKETVER >= 20170208
  18070. uint16 index = RFIFOW(fd, 2) - 2;
  18071. uint32 position = RFIFOL(fd, 4);
  18072. if( !battle_config.feature_equipswitch ){
  18073. return;
  18074. }
  18075. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  18076. return;
  18077. }
  18078. if( sd->state.trading || sd->npc_shopid ){
  18079. clif_equipswitch_add( sd, index, position, ITEM_EQUIP_ACK_OK );
  18080. return;
  18081. }
  18082. if( sd->inventory_data[index]->type == IT_AMMO ){
  18083. position = EQP_AMMO;
  18084. }
  18085. pc_equipitem( sd, index, position, true );
  18086. #endif
  18087. }
  18088. /// Acknowledgement packet for the full equip switch
  18089. /// 0a9d <failed>.W
  18090. void clif_equipswitch_reply( map_session_data* sd, bool failed ){
  18091. #if PACKETVER >= 20170208
  18092. int fd = sd->fd;
  18093. WFIFOHEAD(fd, packet_len(0xa9d));
  18094. WFIFOW(fd, 0) = 0xa9d;
  18095. WFIFOW(fd, 2) = failed;
  18096. WFIFOSET(fd, packet_len(0xa9d));
  18097. #endif
  18098. }
  18099. /// Request to do a full equip switch
  18100. /// 0a9c
  18101. void clif_parse_equipswitch_request( int fd, map_session_data* sd ){
  18102. #if PACKETVER >= 20170208
  18103. int i;
  18104. t_tick tick = gettick();
  18105. uint16 skill_id = ALL_EQSWITCH, skill_lv = 1;
  18106. if( DIFF_TICK(tick, sd->equipswitch_tick) < 0 ) {
  18107. // Client will not let you send a request
  18108. return;
  18109. }
  18110. sd->equipswitch_tick = tick + skill_get_cooldown( skill_id, skill_lv );
  18111. if( !battle_config.feature_equipswitch ){
  18112. return;
  18113. }
  18114. ARR_FIND( 0, EQI_MAX, i, sd->equip_switch_index[i] >= 0 );
  18115. if( i == EQI_MAX ){
  18116. // client will show: "There is no item to replace." and should not even come here
  18117. clif_equipswitch_reply( sd, false );
  18118. return;
  18119. }
  18120. if( pc_issit(sd) && !pc_setstand( sd, false ) ){
  18121. return;
  18122. }
  18123. clif_parse_skill_toid( sd, skill_id, skill_lv, sd->bl.id );
  18124. #endif
  18125. }
  18126. /// Request to do a single equip switch
  18127. /// 0ace <index>.W
  18128. void clif_parse_equipswitch_request_single( int fd, map_session_data* sd ){
  18129. #if PACKETVER >= 20170502
  18130. uint16 index = RFIFOW(fd, 2) - 2;
  18131. if( !battle_config.feature_equipswitch ){
  18132. return;
  18133. }
  18134. // Check if the index is valid
  18135. if( index >= MAX_INVENTORY ){
  18136. return;
  18137. }
  18138. // Check if the item exists
  18139. if (sd->inventory_data[index] == nullptr)
  18140. return;
  18141. // Check if the item was already added to equip switch
  18142. if( sd->inventory.u.items_inventory[index].equipSwitch ){
  18143. if( sd->npc_id ){
  18144. #ifdef RENEWAL
  18145. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) ){
  18146. clif_msg( sd, MSI_BUSY);
  18147. return;
  18148. }
  18149. #endif
  18150. if( !sd->npc_item_flag ){
  18151. return;
  18152. }
  18153. }
  18154. pc_equipswitch( sd, index );
  18155. return;
  18156. }
  18157. pc_equipitem( sd, index, pc_equippoint(sd, index), true );
  18158. #endif
  18159. }
  18160. void clif_parse_StartUseSkillToId( int fd, map_session_data* sd ){
  18161. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18162. const PACKET_CZ_USE_SKILL_START* p = reinterpret_cast<PACKET_CZ_USE_SKILL_START*>( RFIFOP( fd, 0 ) );
  18163. // Only rolling cutter is supported for now
  18164. if( p->skillId != GC_ROLLINGCUTTER ){
  18165. return;
  18166. }
  18167. // Already running - cant happen on officials, since only one skill is supported
  18168. if( sd->skill_keep_using.skill_id != 0 ){
  18169. return;
  18170. }
  18171. sd->skill_keep_using.tid = INVALID_TIMER;
  18172. sd->skill_keep_using.skill_id = p->skillId;
  18173. sd->skill_keep_using.level = p->skillLv;
  18174. sd->skill_keep_using.target = p->targetId;
  18175. clif_parse_skill_toid( sd, sd->skill_keep_using.skill_id, sd->skill_keep_using.level, sd->skill_keep_using.target );
  18176. #endif
  18177. }
  18178. void clif_parse_StopUseSkillToId( int fd, map_session_data* sd ){
  18179. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18180. const PACKET_CZ_USE_SKILL_END* p = reinterpret_cast<PACKET_CZ_USE_SKILL_END*>( RFIFOP( fd, 0 ) );
  18181. // Not running
  18182. if( sd->skill_keep_using.skill_id == 0 ){
  18183. return;
  18184. }
  18185. #if 0
  18186. // Hack
  18187. if( p->skillId != sd->skill_keep_using.skill_id ){
  18188. return;
  18189. }
  18190. #endif
  18191. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  18192. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  18193. sd->skill_keep_using.tid = INVALID_TIMER;
  18194. }
  18195. sd->skill_keep_using.skill_id = 0;
  18196. sd->skill_keep_using.level = 0;
  18197. sd->skill_keep_using.target = 0;
  18198. #endif
  18199. }
  18200. void clif_ping( map_session_data* sd ){
  18201. #if PACKETVER_MAIN_NUM >= 20190213 || PACKETVER_RE_NUM >= 20190213 || PACKETVER_ZERO_NUM >= 20190130
  18202. nullpo_retv( sd );
  18203. int fd = sd->fd;
  18204. if( !session_isActive( fd ) ){
  18205. return;
  18206. }
  18207. PACKET_ZC_PING_LIVE p = {};
  18208. p.packetType = HEADER_ZC_PING_LIVE;
  18209. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18210. #endif
  18211. }
  18212. int clif_ping_timer_sub( map_session_data *sd, va_list ap ){
  18213. nullpo_ret( sd );
  18214. int fd = sd->fd;
  18215. if( !session_isActive( fd ) ){
  18216. return 0;
  18217. }
  18218. t_tick tick = va_arg( ap, t_tick );
  18219. if( session[fd]->wdata_tick + battle_config.ping_time < tick ){
  18220. clif_ping( sd );
  18221. }
  18222. return 0;
  18223. }
  18224. TIMER_FUNC( clif_ping_timer ){
  18225. map_foreachpc( clif_ping_timer_sub, gettick() );
  18226. return 0;
  18227. }
  18228. /**
  18229. * Opens the refine UI on the designated client.
  18230. * 0aa0
  18231. */
  18232. void clif_refineui_open( map_session_data* sd ){
  18233. #if PACKETVER >= 20161012
  18234. nullpo_retv( sd );
  18235. PACKET_ZC_OPEN_REFINING_UI p = {};
  18236. p.packetType = HEADER_ZC_OPEN_REFINING_UI;
  18237. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18238. sd->state.refineui_open = true;
  18239. #endif
  18240. }
  18241. /**
  18242. * Parses cancel/close of the refine UI on client side.
  18243. * 0aa4
  18244. */
  18245. void clif_parse_refineui_close( int fd, map_session_data* sd ){
  18246. #if PACKETVER >= 20161012
  18247. sd->state.refineui_open = false;
  18248. #endif
  18249. }
  18250. /**
  18251. * Adds the selected item into the refine UI and sends the possible materials
  18252. * to the client.
  18253. * 0aa2 <length>.W <index>.W <catalyst count>.B { <material>.W <chance>.B <price>.L }*
  18254. */
  18255. void clif_refineui_info( map_session_data* sd, uint16 index ){
  18256. #if PACKETVER >= 20161012
  18257. int fd = sd->fd;
  18258. // Get the item db reference
  18259. struct item_data* id = sd->inventory_data[index];
  18260. // No item data was found
  18261. if( id == nullptr ){
  18262. return;
  18263. }
  18264. // Check the inventory
  18265. struct item* item = &sd->inventory.u.items_inventory[index];
  18266. // No item was found at the given index
  18267. if( item == nullptr ){
  18268. return;
  18269. }
  18270. // Check if the item is identified
  18271. if( !item->identify ){
  18272. return;
  18273. }
  18274. // Check if the item is broken
  18275. if( item->attribute ){
  18276. return;
  18277. }
  18278. PACKET_ZC_REFINING_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_REFINING_MATERIAL_LIST*>( packet_buffer );
  18279. p->packetType = HEADER_ZC_REFINING_MATERIAL_LIST;
  18280. p->packetLength = sizeof( *p );
  18281. p->itemIndex = client_index( index );
  18282. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18283. // No possibilities were found
  18284. if( info == nullptr ){
  18285. p->blacksmithBlessing = 0;
  18286. }else{
  18287. p->blacksmithBlessing = static_cast<decltype(p->blacksmithBlessing)>( info->blessing_amount );
  18288. for( uint16 i = REFINE_COST_NORMAL, count = 0; i < REFINE_COST_MAX; i++ ){
  18289. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, i );
  18290. if( cost == nullptr ){
  18291. continue;
  18292. }
  18293. PACKET_ZC_REFINING_MATERIAL_LIST_SUB& entry = p->req[count];
  18294. entry.itemId = client_nameid( cost->nameid );
  18295. entry.chance = static_cast<decltype(entry.chance)>( cost->chance / 100 );
  18296. entry.zeny = cost->zeny;
  18297. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  18298. count++;
  18299. }
  18300. }
  18301. clif_send( p, p->packetLength, &sd->bl, SELF );
  18302. #endif
  18303. }
  18304. /**
  18305. * Client request to add an item to the refine UI.
  18306. * 0aa1 <index>.W
  18307. */
  18308. void clif_parse_refineui_add( int fd, map_session_data* sd ){
  18309. #if PACKETVER >= 20161012
  18310. const PACKET_CZ_REFINING_SELECT_ITEM* p = reinterpret_cast<PACKET_CZ_REFINING_SELECT_ITEM*>( RFIFOP( fd, 0 ) );
  18311. uint16 index = server_index( p->index );
  18312. // Check if the refine UI is open
  18313. if( !sd->state.refineui_open ){
  18314. return;
  18315. }
  18316. // Check if the index is valid
  18317. if( index >= MAX_INVENTORY ){
  18318. return;
  18319. }
  18320. // Send out the requirements for the refine process
  18321. clif_refineui_info( sd, index );
  18322. #endif
  18323. }
  18324. /**
  18325. * Client requests to try to refine an item.
  18326. * 0aa3 <index>.W <material>.W <catalyst>.B
  18327. */
  18328. void clif_parse_refineui_refine( int fd, map_session_data* sd ){
  18329. #if PACKETVER >= 20161012
  18330. const PACKET_CZ_REQ_REFINING* p = reinterpret_cast<PACKET_CZ_REQ_REFINING*>( RFIFOP( fd, 0 ) );
  18331. uint16 index = server_index( p->index );
  18332. t_itemid material = p->itemId;
  18333. int16 j;
  18334. // Check if the refine UI is open
  18335. if( !sd->state.refineui_open ){
  18336. return;
  18337. }
  18338. // Check if the index is valid
  18339. if( index >= MAX_INVENTORY ){
  18340. return;
  18341. }
  18342. // Get the item db reference
  18343. struct item_data* id = sd->inventory_data[index];
  18344. // No item data was found
  18345. if( id == nullptr ){
  18346. return;
  18347. }
  18348. // Check the inventory
  18349. struct item* item = &sd->inventory.u.items_inventory[index];
  18350. // No item was found at the given index
  18351. if( item == nullptr ){
  18352. return;
  18353. }
  18354. // Check if the item is identified
  18355. if( !item->identify ){
  18356. return;
  18357. }
  18358. // Check if the item is broken
  18359. if( item->attribute ){
  18360. return;
  18361. }
  18362. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18363. // No refine possible
  18364. if( info == nullptr ){
  18365. return;
  18366. }
  18367. // No possibilities were found
  18368. if( info->costs.empty() ){
  18369. return;
  18370. }
  18371. // Check if the player has the selected material
  18372. if( ( j = pc_search_inventory( sd, material ) ) < 0 ){
  18373. return;
  18374. }
  18375. int16 blacksmith_index = -1;
  18376. uint16 blacksmith_amount = 0;
  18377. // Check if the player has enough blacksmith blessings
  18378. if( p->blacksmithBlessing != 0 ){
  18379. blacksmith_amount = info->blessing_amount;
  18380. // Player tried to use blacksmith blessing on a refine level, where it is not available
  18381. if( blacksmith_amount == 0 ){
  18382. return;
  18383. }
  18384. // Check if the player has blacksmith blessings
  18385. if( ( blacksmith_index = pc_search_inventory( sd, ITEMID_BLACKSMITH_BLESSING ) ) < 0 ){
  18386. return;
  18387. }
  18388. // Check if the player has enough blacksmith blessings
  18389. if( sd->inventory.u.items_inventory[blacksmith_index].amount < blacksmith_amount ){
  18390. return;
  18391. }
  18392. }
  18393. std::shared_ptr<s_refine_cost> cost = nullptr;
  18394. for( const auto& pair : info->costs ){
  18395. if( pair.second->nameid == material ){
  18396. cost = pair.second;
  18397. break;
  18398. }
  18399. }
  18400. // The material was not in the list of possible materials
  18401. if( cost == nullptr ){
  18402. return;
  18403. }
  18404. // Try to pay for the refine
  18405. if( pc_payzeny( sd, cost->zeny, LOG_TYPE_CONSUME ) ){
  18406. clif_npc_buy_result( sd, e_purchase_result::PURCHASE_FAIL_MONEY ); // "You do not have enough zeny."
  18407. return;
  18408. }
  18409. // Delete the required material
  18410. if( pc_delitem( sd, j, 1, 0, 0, LOG_TYPE_CONSUME ) ){
  18411. return;
  18412. }
  18413. // Delete the required blacksmith blessings
  18414. if( blacksmith_amount > 0 && pc_delitem( sd, blacksmith_index, blacksmith_amount, 0, 0, LOG_TYPE_CONSUME ) ){
  18415. return;
  18416. }
  18417. // Try to refine the item
  18418. if( cost->chance >= ( rnd() % 10000 ) ){
  18419. log_pick_pc( sd, LOG_TYPE_OTHER, -1, item );
  18420. // Success
  18421. item->refine = cap_value( item->refine + 1, 0, MAX_REFINE );
  18422. log_pick_pc( sd, LOG_TYPE_OTHER, 1, item );
  18423. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_SUCCESS );
  18424. clif_refine( *sd, index, ITEMREFINING_SUCCESS );
  18425. if (info->broadcast_success) {
  18426. clif_broadcast_refine_result(*sd, item->nameid, item->refine, true);
  18427. }
  18428. if( id->type == IT_WEAPON ){
  18429. achievement_update_objective( sd, AG_ENCHANT_SUCCESS, 2, id->weapon_level, item->refine );
  18430. }
  18431. clif_refineui_info( sd, index );
  18432. }else{
  18433. // Failure
  18434. if (info->broadcast_failure) {
  18435. clif_broadcast_refine_result(*sd, item->nameid, item->refine, false);
  18436. }
  18437. // Blacksmith blessings were used to prevent breaking and downgrading
  18438. if( blacksmith_amount > 0 ){
  18439. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18440. clif_refineui_info( sd, index );
  18441. // Delete the item if it is breakable
  18442. }else if( cost->breaking_rate > 0 && ( rnd() % 10000 ) < cost->breaking_rate ){
  18443. clif_refine( *sd, index, ITEMREFINING_FAILURE );
  18444. pc_delitem( sd, index, 1, 0, 2, LOG_TYPE_CONSUME );
  18445. // Downgrade the item if necessary
  18446. }else if( cost->downgrade_amount > 0 ){
  18447. item->refine = cap_value( item->refine - cost->downgrade_amount, 0, MAX_REFINE );
  18448. clif_refine( *sd, index, ITEMREFINING_DOWNGRADE );
  18449. clif_refineui_info(sd, index);
  18450. // Only show failure, but dont do anything
  18451. }else{
  18452. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18453. clif_refineui_info( sd, index );
  18454. }
  18455. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_FAILURE );
  18456. achievement_update_objective( sd, AG_ENCHANT_FAIL, 1, 1 );
  18457. }
  18458. #endif
  18459. }
  18460. void clif_unequipall_reply( map_session_data* sd, bool failed ){
  18461. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18462. PACKET_ZC_ACK_TAKEOFF_EQUIP_ALL p = {};
  18463. p.PacketType = HEADER_ZC_ACK_TAKEOFF_EQUIP_ALL;
  18464. p.result = failed;
  18465. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18466. #endif // PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18467. }
  18468. void clif_parse_unequipall( int fd, map_session_data* sd ){
  18469. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20210818
  18470. if( pc_cant_act( sd ) ){
  18471. clif_unequipall_reply( sd, true );
  18472. return;
  18473. }
  18474. const PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL* p = reinterpret_cast<PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL*>( RFIFOP( fd, 0 ) );
  18475. #if PACKETVER_MAIN_NUM >= 20230906
  18476. uint32 location = p->location;
  18477. int max = EQI_MAX;
  18478. #else
  18479. uint32 location = 0xFFFFFFFF;
  18480. int max = EQI_COSTUME_HEAD_TOP;
  18481. #endif
  18482. for( int i = 0; i < max; i++ ){
  18483. if( sd->equip_index[i] >= 0 && ( location & equip_bitmask[i] ) != 0 ){
  18484. pc_unequipitem( sd, sd->equip_index[i], 1 );
  18485. }
  18486. }
  18487. clif_unequipall_reply( sd, false );
  18488. #endif
  18489. }
  18490. void clif_stylist_response( map_session_data* sd, bool failed ){
  18491. #if PACKETVER >= 20151104
  18492. PACKET_ZC_STYLE_CHANGE_RES p = {};
  18493. p.PacketType = HEADER_ZC_STYLE_CHANGE_RES;
  18494. p.flag = failed;
  18495. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18496. if( !failed ){
  18497. sd->state.stylist_open = false;
  18498. }
  18499. #endif
  18500. }
  18501. bool clif_parse_stylist_buy_sub( map_session_data* sd, _look look, int16 index ){
  18502. std::shared_ptr<s_stylist_list> list = stylist_db.find( look );
  18503. if( list == nullptr ){
  18504. return false;
  18505. }
  18506. std::shared_ptr<s_stylist_entry> entry = util::umap_find( list->entries, index );
  18507. if( entry == nullptr ){
  18508. return false;
  18509. }
  18510. std::shared_ptr<s_stylist_costs> costs;
  18511. if( ( sd->class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  18512. costs = entry->doram;
  18513. }else{
  18514. costs = entry->human;
  18515. }
  18516. if( costs == nullptr ){
  18517. return false;
  18518. }
  18519. if( sd->status.zeny < costs->price ){
  18520. return false;
  18521. }
  18522. int16 inventoryIndex = -1;
  18523. if( costs->requiredItem != 0 ){
  18524. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18525. if( inventoryIndex < 0 ){
  18526. // No other option
  18527. if( costs->requiredItemBox == 0 ){
  18528. return false;
  18529. }
  18530. // Check if the box that contains the item is in the inventory
  18531. inventoryIndex = pc_search_inventory( sd, costs->requiredItemBox );
  18532. // The box containing the item also does not exist
  18533. if( inventoryIndex < 0 ){
  18534. return false;
  18535. }
  18536. }
  18537. }else if( costs->requiredItemBox != 0 ){
  18538. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18539. if( inventoryIndex < 0 ){
  18540. return false;
  18541. }
  18542. }
  18543. if( inventoryIndex >= 0 && pc_delitem( sd, inventoryIndex, 1, 0, 0, LOG_TYPE_OTHER ) != 0 ){
  18544. return false;
  18545. }
  18546. if( costs->price > 0 && pc_payzeny( sd, costs->price, LOG_TYPE_OTHER ) != 0 ){
  18547. return false;
  18548. }
  18549. switch( look ){
  18550. case LOOK_HAIR:
  18551. case LOOK_HAIR_COLOR:
  18552. case LOOK_CLOTHES_COLOR:
  18553. case LOOK_BODY2:
  18554. pc_changelook( sd, look, entry->value );
  18555. break;
  18556. case LOOK_HEAD_BOTTOM:
  18557. case LOOK_HEAD_MID:
  18558. case LOOK_HEAD_TOP: {
  18559. struct mail_message msg = {};
  18560. msg.dest_id = sd->status.char_id;
  18561. safestrncpy( msg.send_name, "Styling Shop", NAME_LENGTH );
  18562. safestrncpy( msg.title, "<MSG>2949</MSG>", MAIL_TITLE_LENGTH );
  18563. safestrncpy( msg.body, "<MSG>2950</MSG>", MAIL_BODY_LENGTH );
  18564. msg.item[0].nameid = entry->value;
  18565. msg.item[0].identify = 1;
  18566. msg.item[0].amount = 1;
  18567. msg.status = MAIL_NEW;
  18568. msg.type = MAIL_INBOX_NORMAL;
  18569. msg.timestamp = time( nullptr );
  18570. intif_Mail_send( 0, &msg );
  18571. } break;
  18572. }
  18573. return true;
  18574. }
  18575. void clif_parse_stylist_buy( int fd, map_session_data* sd ){
  18576. #if PACKETVER >= 20151104
  18577. #if PACKETVER >= 20180516
  18578. const PACKET_CZ_REQ_STYLE_CHANGE2* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE2*>( RFIFOP( fd, 0 ) );
  18579. #else
  18580. const PACKET_CZ_REQ_STYLE_CHANGE* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE*>( RFIFOP( fd, 0 ) );
  18581. #endif
  18582. if( p->HeadPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR_COLOR, p->HeadPalette ) ){
  18583. clif_stylist_response( sd, true );
  18584. return;
  18585. }
  18586. if( p->HeadStyle != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR, p->HeadStyle ) ){
  18587. clif_stylist_response( sd, true );
  18588. return;
  18589. }
  18590. if( p->BodyPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_CLOTHES_COLOR, p->BodyPalette ) ){
  18591. clif_stylist_response( sd, true );
  18592. return;
  18593. }
  18594. if( p->TopAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_TOP, p->TopAccessory ) ){
  18595. clif_stylist_response( sd, true );
  18596. return;
  18597. }
  18598. if( p->MidAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_MID, p->MidAccessory ) ){
  18599. clif_stylist_response( sd, true );
  18600. return;
  18601. }
  18602. if( p->BottomAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_BOTTOM, p->BottomAccessory ) ){
  18603. clif_stylist_response( sd, true );
  18604. return;
  18605. }
  18606. #if PACKETVER >= 20180516
  18607. if( p->BodyStyle != 0 && ( sd->class_ & JOBL_THIRD ) != 0 && ( sd->class_ & JOBL_FOURTH ) == 0 && !clif_parse_stylist_buy_sub( sd, LOOK_BODY2, p->BodyStyle ) ){
  18608. clif_stylist_response( sd, true );
  18609. return;
  18610. }
  18611. #endif
  18612. clif_stylist_response( sd, false );
  18613. #endif
  18614. }
  18615. void clif_parse_stylist_close( int fd, map_session_data* sd ){
  18616. #if PACKETVER >= 20151104
  18617. sd->state.stylist_open = false;
  18618. #endif
  18619. }
  18620. void clif_inventory_expansion_info( map_session_data* sd ){
  18621. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18622. nullpo_retv( sd );
  18623. PACKET_ZC_EXTEND_BODYITEM_SIZE p = {};
  18624. p.packetType = HEADER_ZC_EXTEND_BODYITEM_SIZE;
  18625. p.expansionSize = sd->status.inventory_slots - INVENTORY_BASE_SIZE;
  18626. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18627. #endif
  18628. }
  18629. enum class e_inventory_expansion_response : uint8{
  18630. ASK_CONFIRMATION = 0,
  18631. FAILED,
  18632. BUSY,
  18633. MISSING_ITEM,
  18634. MAXIMUM_REACHED
  18635. };
  18636. void clif_inventory_expansion_response( map_session_data* sd, e_inventory_expansion_response response ){
  18637. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18638. nullpo_retv( sd );
  18639. PACKET_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE p = {};
  18640. p.packetType = HEADER_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE;
  18641. p.result = (uint8)response;
  18642. p.itemId = sd->state.inventory_expansion_confirmation;
  18643. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18644. #endif
  18645. }
  18646. void clif_parse_inventory_expansion_request( int fd, map_session_data* sd ){
  18647. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18648. // Check if player is dead or busy with other stuff
  18649. if( pc_isdead( sd ) || pc_cant_act( sd ) ){
  18650. clif_inventory_expansion_response( sd, e_inventory_expansion_response::BUSY );
  18651. return;
  18652. }
  18653. // Check if the player already reached the maximum
  18654. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18655. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18656. return;
  18657. }
  18658. static std::map<t_itemid, uint16> items = {
  18659. // The order of entries in this list defines which will be used first
  18660. // This order and the usable items are hardcoded into the client
  18661. // The number of increased slots is "hardcoded" in the message of the client and cannot be sent per item
  18662. { ITEMID_INVENTORY_EX_EVT, 10 },
  18663. { ITEMID_INVENTORY_EX_DIS, 10 },
  18664. { ITEMID_INVENTORY_EX, 10 },
  18665. };
  18666. int16 index = -1;
  18667. bool found_over_limit = false;
  18668. for( const auto& entry : items ){
  18669. // Check if the player has the required item
  18670. index = pc_search_inventory( sd, entry.first );
  18671. // Found an item
  18672. if( index >= 0 ){
  18673. // Check if the player would exceed the maximum
  18674. if( sd->status.inventory_slots + entry.second > MAX_INVENTORY ){
  18675. found_over_limit = true;
  18676. }else{
  18677. found_over_limit = false;
  18678. sd->state.inventory_expansion_confirmation = entry.first;
  18679. sd->state.inventory_expansion_amount = entry.second;
  18680. break;
  18681. }
  18682. }
  18683. }
  18684. // Check if an item was found
  18685. if( sd->state.inventory_expansion_confirmation == 0 ){
  18686. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MISSING_ITEM );
  18687. return;
  18688. }
  18689. // Check if an item would have been found, but the player would exceed the maximum
  18690. if( found_over_limit ){
  18691. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18692. return;
  18693. }
  18694. // The player met all requirements => ask him for confirmation
  18695. clif_inventory_expansion_response( sd, e_inventory_expansion_response::ASK_CONFIRMATION );
  18696. #endif
  18697. }
  18698. enum class e_inventory_expansion_result : uint8{
  18699. SUCCESS = 0,
  18700. FAILED,
  18701. BUSY,
  18702. MISSING_ITEM,
  18703. MAXIMUM_REACHED
  18704. };
  18705. void clif_inventory_expansion_result( map_session_data* sd, e_inventory_expansion_result result ){
  18706. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18707. nullpo_retv( sd );
  18708. PACKET_ZC_ACK_EXTEND_BODYITEM_SIZE p = {};
  18709. p.packetType = HEADER_ZC_ACK_EXTEND_BODYITEM_SIZE;
  18710. p.result = (uint8)result;
  18711. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18712. // Reset the state tracking
  18713. sd->state.inventory_expansion_confirmation = 0;
  18714. sd->state.inventory_expansion_amount = 0;
  18715. #endif
  18716. }
  18717. void clif_parse_inventory_expansion_confirm( int fd, map_session_data* sd ){
  18718. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18719. if( sd->state.inventory_expansion_confirmation == 0 ){
  18720. return;
  18721. }
  18722. // Check if player is dead
  18723. if( pc_isdead( sd ) ){
  18724. clif_inventory_expansion_result( sd, e_inventory_expansion_result::BUSY );
  18725. return;
  18726. }
  18727. // Check if the player already reached the maximum
  18728. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18729. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18730. return;
  18731. }
  18732. // Check if the player has the required item
  18733. int index = pc_search_inventory( sd, sd->state.inventory_expansion_confirmation );
  18734. // The player did not have the item anymore
  18735. if( index < 0 ){
  18736. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MISSING_ITEM );
  18737. return;
  18738. }
  18739. // Check if the player would exceed the maximum
  18740. if( sd->status.inventory_slots + sd->state.inventory_expansion_amount > MAX_INVENTORY ){
  18741. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18742. return;
  18743. }
  18744. // Delete the required item
  18745. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_OTHER ) ){
  18746. clif_inventory_expansion_result( sd, e_inventory_expansion_result::FAILED );
  18747. return;
  18748. }
  18749. // Increase the slots
  18750. sd->status.inventory_slots += sd->state.inventory_expansion_amount;
  18751. // Save player data (slots) and inventory data (removed item)
  18752. chrif_save( sd, CSAVE_NORMAL | CSAVE_INVENTORY );
  18753. // Inform the player of success
  18754. clif_inventory_expansion_result( sd, e_inventory_expansion_result::SUCCESS );
  18755. clif_inventory_expansion_info( sd );
  18756. #endif
  18757. }
  18758. void clif_parse_inventory_expansion_reject( int fd, map_session_data* sd ){
  18759. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18760. sd->state.inventory_expansion_confirmation = 0;
  18761. sd->state.inventory_expansion_amount = 0;
  18762. #endif
  18763. }
  18764. void clif_barter_open( map_session_data& sd, struct npc_data& nd ){
  18765. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18766. if( nd.subtype != NPCTYPE_BARTER || nd.u.barter.extended || sd.state.barter_open ){
  18767. return;
  18768. }
  18769. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18770. if( barter == nullptr ){
  18771. return;
  18772. }
  18773. sd.state.barter_open = true;
  18774. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  18775. p->packetType = HEADER_ZC_NPC_BARTER_MARKET_ITEMINFO;
  18776. p->packetLength = sizeof( *p );
  18777. int16 count = 0;
  18778. for( const auto& itemPair : barter->items ){
  18779. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  18780. if( id == nullptr ){
  18781. continue;
  18782. }
  18783. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO_sub& item = p->list[count];
  18784. item.nameid = client_nameid( id->nameid );
  18785. item.type = itemtype( id->nameid );
  18786. if( itemPair.second->stockLimited ){
  18787. item.amount = itemPair.second->stock;
  18788. }else{
  18789. item.amount = -1;
  18790. }
  18791. item.weight = id->weight;
  18792. item.index = itemPair.second->index;
  18793. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  18794. item.viewSprite = id->look;
  18795. item.location = pc_equippoint_sub( &sd, id.get() );
  18796. #endif
  18797. // Use a loop if someone did not start with index 0
  18798. for( const auto& requirementPair : itemPair.second->requirements ){
  18799. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  18800. item.currencyNameid = client_nameid( requirement->nameid );
  18801. item.currencyAmount = requirement->amount;
  18802. // It is a normal barter, cancel after first entry
  18803. break;
  18804. }
  18805. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( item ) );
  18806. count++;
  18807. }
  18808. clif_send( p, p->packetLength, &sd.bl, SELF );
  18809. #endif
  18810. }
  18811. void clif_parse_barter_close( int fd, map_session_data* sd ){
  18812. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18813. if( sd->state.barter_open ){
  18814. sd->npc_shopid = 0;
  18815. sd->state.barter_open = false;
  18816. }
  18817. #endif
  18818. }
  18819. void clif_parse_barter_buy( int fd, map_session_data* sd ){
  18820. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18821. // No shop open
  18822. if( sd->npc_shopid == 0 || !sd->state.barter_open ){
  18823. return;
  18824. }
  18825. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  18826. // Unknown shop
  18827. if( nd == nullptr ){
  18828. return;
  18829. }
  18830. // Not a barter
  18831. if( nd->subtype != NPCTYPE_BARTER ){
  18832. return;
  18833. }
  18834. // It is an extended barter
  18835. if( nd->u.barter.extended ){
  18836. return;
  18837. }
  18838. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  18839. if( barter == nullptr ){
  18840. return;
  18841. }
  18842. const PACKET_CZ_NPC_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  18843. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  18844. // Empty purchase list
  18845. if( entries == 0 ){
  18846. return;
  18847. }
  18848. std::vector<s_barter_purchase> purchases;
  18849. purchases.reserve( entries );
  18850. // Make sure each shop index and target item id is only used once
  18851. for( int i = 0; i < entries; i++ ){
  18852. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  18853. // Invalid shop index
  18854. if( item == nullptr ){
  18855. return;
  18856. }
  18857. for( int j = i + 1; j < entries; j++ ){
  18858. // Same shop index
  18859. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  18860. return;
  18861. }
  18862. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  18863. // Invalid shop index
  18864. if( item2 == nullptr ){
  18865. return;
  18866. }
  18867. // Same target item id
  18868. if( item->nameid == item2->nameid ){
  18869. return;
  18870. }
  18871. }
  18872. s_barter_purchase purchase = {};
  18873. purchase.item = item;
  18874. purchase.amount = p->list[i].amount;
  18875. purchases.push_back( purchase );
  18876. }
  18877. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  18878. #endif
  18879. }
  18880. void clif_barter_extended_open( map_session_data& sd, struct npc_data& nd ){
  18881. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18882. if( nd.subtype != NPCTYPE_BARTER || !nd.u.barter.extended || sd.state.barter_extended_open ){
  18883. return;
  18884. }
  18885. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18886. if( barter == nullptr ){
  18887. return;
  18888. }
  18889. sd.state.barter_extended_open = true;
  18890. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  18891. p->packetType = HEADER_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO;
  18892. p->packetLength = sizeof( *p );
  18893. p->items_count = 0;
  18894. for( const auto& itemPair : barter->items ){
  18895. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  18896. if( id == nullptr ){
  18897. continue;
  18898. }
  18899. // Needs dynamic calculation, because of variable currencies
  18900. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub* item = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub*>( WBUFP( p, p->packetLength ) );
  18901. item->nameid = client_nameid( id->nameid );
  18902. item->type = itemtype( id->nameid );
  18903. if( itemPair.second->stockLimited ){
  18904. item->amount = itemPair.second->stock;
  18905. }else{
  18906. item->amount = -1;
  18907. }
  18908. item->weight = id->weight;
  18909. item->index = itemPair.second->index;
  18910. item->zeny = itemPair.second->price;
  18911. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  18912. item->viewSprite = id->look;
  18913. item->location = pc_equippoint_sub( &sd, id.get() );
  18914. #endif
  18915. // Because of a MSVS bug, the currencies have been defined with a fixed array entry, which has to be substracted
  18916. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *item ) - sizeof( item->currencies ) );
  18917. p->items_count++;
  18918. item->currency_count = 0;
  18919. for( const auto& requirementPair : itemPair.second->requirements ){
  18920. // Needs dynamic calculation, because of variable currencies
  18921. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2* req = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2*>( WBUFP( p, p->packetLength ) );
  18922. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  18923. req->nameid = requirement->nameid;
  18924. if( requirement->refine >= 0 ){
  18925. req->refine_level = requirement->refine;
  18926. }else{
  18927. req->refine_level = 0;
  18928. }
  18929. req->amount = requirement->amount;
  18930. req->type = itemtype( requirement->nameid );
  18931. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *req ) );
  18932. item->currency_count++;
  18933. }
  18934. }
  18935. clif_send( p, p->packetLength, &sd.bl, SELF );
  18936. #endif
  18937. }
  18938. void clif_parse_barter_extended_close( int fd, map_session_data* sd ){
  18939. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18940. if( sd->state.barter_extended_open ){
  18941. sd->npc_shopid = 0;
  18942. sd->state.barter_extended_open = false;
  18943. }
  18944. #endif
  18945. }
  18946. void clif_parse_barter_extended_buy( int fd, map_session_data* sd ){
  18947. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18948. // No shop open
  18949. if( sd->npc_shopid == 0 || !sd->state.barter_extended_open ){
  18950. return;
  18951. }
  18952. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  18953. // Unknown shop
  18954. if( nd == nullptr ){
  18955. return;
  18956. }
  18957. // Not a barter
  18958. if( nd->subtype != NPCTYPE_BARTER ){
  18959. return;
  18960. }
  18961. // Not an extended barter
  18962. if( !nd->u.barter.extended ){
  18963. return;
  18964. }
  18965. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  18966. if( barter == nullptr ){
  18967. return;
  18968. }
  18969. const PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  18970. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  18971. // Empty purchase list
  18972. if( entries == 0 ){
  18973. return;
  18974. }
  18975. std::vector<s_barter_purchase> purchases;
  18976. purchases.reserve( entries );
  18977. // Make sure each shop index and target item id is only used once
  18978. for( int i = 0; i < entries; i++ ){
  18979. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  18980. // Invalid shop index
  18981. if( item == nullptr ){
  18982. return;
  18983. }
  18984. for( int j = i + 1; j < entries; j++ ){
  18985. // Same shop index
  18986. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  18987. return;
  18988. }
  18989. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  18990. // Invalid shop index
  18991. if( item2 == nullptr ){
  18992. return;
  18993. }
  18994. // Same target item id
  18995. if( item->nameid == item2->nameid ){
  18996. return;
  18997. }
  18998. }
  18999. s_barter_purchase purchase = {};
  19000. purchase.item = item;
  19001. purchase.amount = p->list[i].amount;
  19002. purchases.push_back( purchase );
  19003. }
  19004. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  19005. #endif
  19006. }
  19007. void clif_summon_init(struct mob_data& md) {
  19008. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19009. struct block_list* master_bl = battle_get_master(&md.bl);
  19010. if( master_bl == nullptr ){
  19011. return;
  19012. }
  19013. PACKET_ZC_SUMMON_HP_INIT p = {};
  19014. p.PacketType = HEADER_ZC_SUMMON_HP_INIT;
  19015. p.summonAID = md.bl.id;
  19016. p.CurrentHP = md.status.hp;
  19017. p.MaxHP = md.status.max_hp;
  19018. clif_send( &p, sizeof( p ), master_bl, SELF );
  19019. #endif
  19020. }
  19021. void clif_summon_hp_bar(struct mob_data& md) {
  19022. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19023. struct block_list* master_bl = battle_get_master(&md.bl);
  19024. if( master_bl == nullptr ){
  19025. return;
  19026. }
  19027. PACKET_ZC_SUMMON_HP_UPDATE p = {};
  19028. p.PacketType = HEADER_ZC_SUMMON_HP_UPDATE;
  19029. p.summonAID = md.bl.id;
  19030. p.VarId = SP_HP; // HP parameter
  19031. p.Value = md.status.hp;
  19032. clif_send( &p, sizeof( p ), master_bl, SELF );
  19033. #endif
  19034. }
  19035. void clif_laphine_synthesis_open( map_session_data *sd, std::shared_ptr<s_laphine_synthesis> synthesis ){
  19036. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19037. nullpo_retv( sd );
  19038. sd->state.laphine_synthesis = synthesis->item_id;
  19039. PACKET_ZC_RANDOM_COMBINE_ITEM_UI_OPEN p = {};
  19040. p.packetType = HEADER_ZC_RANDOM_COMBINE_ITEM_UI_OPEN;
  19041. p.itemId = client_nameid( synthesis->item_id );
  19042. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19043. #endif
  19044. }
  19045. void clif_parse_laphine_synthesis_close( int fd, map_session_data* sd ){
  19046. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19047. sd->state.laphine_synthesis = 0;
  19048. #endif
  19049. }
  19050. enum e_laphine_synthesis_result : int16{
  19051. LAPHINE_SYNTHESIS_SUCCESS = 0,
  19052. LAPHINE_SYNTHESIS_AMOUNT = 5,
  19053. LAPHINE_SYNTHESIS_ITEM = 7
  19054. };
  19055. void clif_laphine_synthesis_result( map_session_data* sd, enum e_laphine_synthesis_result result ){
  19056. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19057. nullpo_retv( sd );
  19058. PACKET_ZC_ACK_RANDOM_COMBINE_ITEM p = {};
  19059. p.packetType = HEADER_ZC_ACK_RANDOM_COMBINE_ITEM;
  19060. p.result = result;
  19061. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19062. #endif
  19063. }
  19064. void clif_parse_laphine_synthesis( int fd, map_session_data* sd ){
  19065. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19066. if( sd->state.laphine_synthesis == 0 ){
  19067. return;
  19068. }
  19069. const PACKET_CZ_REQ_RANDOM_COMBINE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_COMBINE_ITEM*>( RFIFOP( fd, 0 ) );
  19070. if( sd->state.laphine_synthesis != p->itemId ){
  19071. return;
  19072. }
  19073. std::shared_ptr<s_laphine_synthesis> synthesis = laphine_synthesis_db.find( sd->state.laphine_synthesis );
  19074. if( synthesis == nullptr ){
  19075. return;
  19076. }
  19077. size_t count = ( p->packetLength - sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM ) ) / sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM_sub );
  19078. // Player sent more or less than actually required
  19079. if( count != synthesis->requiredRequirements ){
  19080. return;
  19081. }
  19082. // Check for duplicates
  19083. for( size_t i = 0; i < count; i++ ){
  19084. for( size_t j = i + 1; j < count; j++ ){
  19085. if( p->items[i].index == p->items[j].index ){
  19086. return;
  19087. }
  19088. }
  19089. }
  19090. for( size_t i = 0; i < count; i++ ){
  19091. int16 index = server_index( p->items[i].index );
  19092. if( index >= MAX_INVENTORY ){
  19093. return;
  19094. }
  19095. if( sd->inventory_data[index] == nullptr ){
  19096. return;
  19097. }
  19098. struct item* item = &sd->inventory.u.items_inventory[index];
  19099. std::shared_ptr<s_laphine_synthesis_requirement> requirement = util::umap_find( synthesis->requirements, item->nameid );
  19100. if( requirement == nullptr ){
  19101. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19102. return;
  19103. }
  19104. if( p->items[i].count != requirement->amount ){
  19105. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19106. return;
  19107. }
  19108. if( item->amount < requirement->amount ){
  19109. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19110. return;
  19111. }
  19112. if( item->refine < synthesis->minimumRefine ){
  19113. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19114. return;
  19115. }
  19116. if( item->refine > synthesis->maximumRefine ){
  19117. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19118. return;
  19119. }
  19120. if( item->equip != 0 ){
  19121. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19122. return;
  19123. }
  19124. if( item->equipSwitch != 0 ){
  19125. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19126. return;
  19127. }
  19128. }
  19129. // If triggered from item
  19130. if( sd->itemid == sd->state.laphine_synthesis ){
  19131. int16 index = pc_search_inventory( sd, sd->state.laphine_synthesis );
  19132. if( index < 0 ){
  19133. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19134. return;
  19135. }
  19136. if( ( sd->inventory_data[index]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19137. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19138. return;
  19139. }
  19140. }
  19141. }
  19142. for( size_t i = 0; i < count; i++ ){
  19143. int16 index = server_index( p->items[i].index );
  19144. if( pc_delitem( sd, index, p->items[i].count, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19145. return;
  19146. }
  19147. }
  19148. itemdb_group.pc_get_itemgroup( synthesis->rewardGroupId, true, *sd );
  19149. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_SUCCESS );
  19150. #endif
  19151. }
  19152. void clif_laphine_upgrade_open( map_session_data* sd, std::shared_ptr<s_laphine_upgrade> upgrade ){
  19153. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19154. nullpo_retv( sd );
  19155. sd->state.laphine_upgrade = upgrade->item_id;
  19156. PACKET_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN p = {};
  19157. p.packetType = HEADER_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN;
  19158. p.itemId = client_nameid( upgrade->item_id );
  19159. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19160. #endif
  19161. }
  19162. void clif_parse_laphine_upgrade_close( int fd, map_session_data* sd ){
  19163. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19164. sd->state.laphine_upgrade = 0;
  19165. #endif
  19166. }
  19167. void clif_laphine_upgrade_result( map_session_data *sd, bool failed ){
  19168. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19169. PACKET_ZC_ACK_RANDOM_UPGRADE_ITEM p = {};
  19170. p.packetType = HEADER_ZC_ACK_RANDOM_UPGRADE_ITEM;
  19171. p.result = failed;
  19172. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19173. #endif
  19174. }
  19175. static void clif_item_preview( map_session_data *sd, int16 index ){
  19176. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19177. nullpo_retv( sd );
  19178. struct item* item = &sd->inventory.u.items_inventory[index];
  19179. PACKET_ZC_CHANGE_ITEM_OPTION p = {};
  19180. p.packetType = HEADER_ZC_CHANGE_ITEM_OPTION;
  19181. p.index = client_index( index );
  19182. #if PACKETVER_MAIN_NUM >= 20181017 || PACKETVER_RE_NUM >= 20181017 || PACKETVER_ZERO_NUM >= 20181024
  19183. p.isDamaged = item->attribute != 0;
  19184. #endif
  19185. p.refiningLevel = item->refine;
  19186. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723
  19187. p.grade = item->enchantgrade;
  19188. #endif
  19189. clif_addcards( &p.slot, item );
  19190. clif_add_random_options( p.option_data, *item );
  19191. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19192. #endif
  19193. }
  19194. void clif_parse_laphine_upgrade( int fd, map_session_data* sd ){
  19195. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19196. if( sd->state.laphine_upgrade == 0 ){
  19197. return;
  19198. }
  19199. const PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM*>( RFIFOP( fd, 0 ) );
  19200. if( sd->state.laphine_upgrade != p->itemId ){
  19201. return;
  19202. }
  19203. std::shared_ptr<s_laphine_upgrade> upgrade = laphine_upgrade_db.find( sd->state.laphine_upgrade );
  19204. if( upgrade == nullptr ){
  19205. return;
  19206. }
  19207. uint16 index = server_index( p->index );
  19208. if( index >= MAX_INVENTORY ){
  19209. return;
  19210. }
  19211. if( sd->inventory_data[index] == nullptr ){
  19212. return;
  19213. }
  19214. struct item* item = &sd->inventory.u.items_inventory[index];
  19215. // Not a valid target item
  19216. if( !util::vector_exists( upgrade->target_item_ids, item->nameid ) ){
  19217. clif_laphine_upgrade_result( sd, true );
  19218. return;
  19219. }
  19220. // If target item is not identified
  19221. if( item->identify == 0 ){
  19222. clif_laphine_upgrade_result( sd, true );
  19223. return;
  19224. }
  19225. // If target item is equipped
  19226. if( item->equip != 0 ){
  19227. clif_laphine_upgrade_result( sd, true );
  19228. return;
  19229. }
  19230. // If target item is in equipswitch
  19231. if( item->equipSwitch != 0 ){
  19232. clif_laphine_upgrade_result( sd, true );
  19233. return;
  19234. }
  19235. // Check minimum refine requirement
  19236. if( item->refine < upgrade->minimumRefine ){
  19237. clif_laphine_upgrade_result( sd, true );
  19238. return;
  19239. }
  19240. // Check maximum refine requirement
  19241. if( item->refine > upgrade->maximumRefine ){
  19242. clif_laphine_upgrade_result( sd, true );
  19243. return;
  19244. }
  19245. // If no cards are allowed
  19246. if( !upgrade->cardsAllowed ){
  19247. for( int i = 0; i < MAX_SLOTS; i++ ){
  19248. if( item->card[i] != 0 ){
  19249. clif_laphine_upgrade_result( sd, true );
  19250. return;
  19251. }
  19252. }
  19253. }
  19254. // If random options are required
  19255. if( upgrade->requiredRandomOptions > 0 ){
  19256. int i;
  19257. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19258. if( item->option[i].id != 0 ){
  19259. break;
  19260. }
  19261. }
  19262. if( ( i + 1 ) < upgrade->requiredRandomOptions ){
  19263. clif_laphine_upgrade_result( sd, true );
  19264. return;
  19265. }
  19266. }
  19267. int16 index2 = pc_search_inventory( sd, sd->state.laphine_upgrade );
  19268. if( index2 < 0 ){
  19269. clif_laphine_upgrade_result( sd, true );
  19270. return;
  19271. }
  19272. if( ( sd->inventory_data[index2]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19273. if( pc_delitem( sd, index2, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19274. return;
  19275. }
  19276. }
  19277. // Log removal of item
  19278. log_pick_pc( sd, LOG_TYPE_LAPHINE, -1, item );
  19279. // Visually remove it from the client
  19280. clif_delitem( *sd, index, 1, 0 );
  19281. // Apply the random options
  19282. if( upgrade->randomOptionGroup != nullptr ){
  19283. upgrade->randomOptionGroup->apply( *item );
  19284. }
  19285. // Change the refine rate if needed
  19286. if( upgrade->resultRefine > 0 ){
  19287. // Absolute refine level change
  19288. item->refine = max( item->refine, upgrade->resultRefine );
  19289. }else if( upgrade->resultRefineMaximum > 0 ){
  19290. // If a minimum is specified it can also downgrade
  19291. if( upgrade->resultRefineMinimum ){
  19292. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, upgrade->resultRefineMaximum ) );
  19293. }else{
  19294. // Otherwise it can only be upgraded until the maximum, but not downgraded
  19295. item->refine = static_cast<uint8>( rnd_value<uint16>( item->refine, upgrade->resultRefineMaximum ) );
  19296. }
  19297. }else if( upgrade->resultRefineMinimum > 0 ){
  19298. // No maximum has been specified, so it can be anything between minimum and MAX_REFINE
  19299. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, MAX_REFINE ) );
  19300. }
  19301. // Log retrieving the item again -> with the new options
  19302. log_pick_pc( sd, LOG_TYPE_LAPHINE, 1, item );
  19303. // Make it visible for the client again
  19304. clif_additem( sd, index, 1, 0 );
  19305. // Open a preview of the item
  19306. clif_item_preview( sd, index );
  19307. // Tell the client we are done
  19308. clif_laphine_upgrade_result( sd, false );
  19309. #endif
  19310. }
  19311. void clif_enchantgrade_add( map_session_data& sd, uint16 index = UINT16_MAX, std::shared_ptr<s_enchantgradelevel> gradeLevel = nullptr ){
  19312. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19313. PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST*>( packet_buffer );
  19314. p->PacketType = HEADER_ZC_GRADE_ENCHANT_MATERIAL_LIST;
  19315. p->PacketLength = sizeof( *p );
  19316. if( index < UINT16_MAX ){
  19317. p->index = client_index( index );
  19318. p->success_chance = gradeLevel->chances[sd.inventory.u.items_inventory[index].refine] / 100;
  19319. p->blessing_info.id = client_nameid( gradeLevel->catalyst.item );
  19320. p->blessing_info.amount = gradeLevel->catalyst.amountPerStep;
  19321. p->blessing_info.max_blessing = gradeLevel->catalyst.maximumSteps;
  19322. p->blessing_info.bonus = gradeLevel->catalyst.chanceIncrease / 100;
  19323. // Not displayed by client
  19324. p->protect_itemid = 0;
  19325. p->protect_amount = 0;
  19326. int i = 0;
  19327. for( const auto& pair : gradeLevel->options ){
  19328. GRADE_ENCHANT_MATERIAL& mat = p->material_info[i];
  19329. std::shared_ptr<s_enchantgradeoption> option = pair.second;
  19330. mat.nameid = client_nameid( option->item );
  19331. mat.amount = option->amount;
  19332. mat.price = option->zeny;
  19333. mat.downgrade = option->downgrade_amount > 0;
  19334. mat.breakable = option->breaking_rate > 0;
  19335. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( mat ) );
  19336. i++;
  19337. }
  19338. }else{
  19339. p->index = -1;
  19340. p->success_chance = 0;
  19341. p->blessing_info.id = 0;
  19342. p->blessing_info.amount = 0;
  19343. p->blessing_info.max_blessing = 0;
  19344. p->blessing_info.bonus = 0;
  19345. p->protect_itemid = 0;
  19346. p->protect_amount = 0;
  19347. }
  19348. clif_send( p, p->PacketLength, &sd.bl, SELF );
  19349. #endif
  19350. }
  19351. void clif_parse_enchantgrade_add( int fd, map_session_data* sd ){
  19352. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19353. nullpo_retv( sd );
  19354. if( !sd->state.enchantgrade_open ){
  19355. return;
  19356. }
  19357. const PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT*>( RFIFOP( fd, 0 ) );
  19358. uint16 index = server_index( p->index );
  19359. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19360. return;
  19361. }
  19362. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19363. // Unsupported item type - no answer, because client should have actually prevented this request
  19364. if( enchantgrade == nullptr ){
  19365. return;
  19366. }
  19367. // Item can't be enhanced
  19368. if( !sd->inventory_data[index]->flag.gradable ){
  19369. return;
  19370. }
  19371. uint16 level = 0;
  19372. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19373. level = sd->inventory_data[index]->weapon_level;
  19374. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19375. level = sd->inventory_data[index]->armor_level;
  19376. }
  19377. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19378. // Cannot upgrade this weapon or armor level
  19379. if( enchantgradelevels == enchantgrade->levels.end() ){
  19380. clif_enchantgrade_add( *sd );
  19381. return;
  19382. }
  19383. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19384. // Cannot increase enchantgrade any further - no answer, because client should have actually prevented this request
  19385. if( enchantgradelevel == nullptr ){
  19386. return;
  19387. }
  19388. clif_enchantgrade_add( *sd, index, enchantgradelevel );
  19389. #endif
  19390. }
  19391. /// <summary>
  19392. /// Sends the result for trying to enchant an item
  19393. /// </summary>
  19394. /// <param name="sd">The player session</param>
  19395. /// <param name="index">The target item</param>
  19396. /// <param name="result">
  19397. /// 0= The grade has been successfully upgraded.
  19398. /// 1= Refinement failed.
  19399. /// 2= The refine level has decreased.
  19400. /// 3= Equipment destroyed.
  19401. /// 4= The equipment is protected.
  19402. /// </param>
  19403. void clif_enchantgrade_result( map_session_data& sd, uint16 index, e_enchantgrade_result result ){
  19404. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19405. PACKET_ZC_GRADE_ENCHANT_ACK p = {};
  19406. p.PacketType = HEADER_ZC_GRADE_ENCHANT_ACK;
  19407. p.index = client_index( index );
  19408. p.grade = sd.inventory.u.items_inventory[index].enchantgrade;
  19409. p.result = result;
  19410. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19411. #endif
  19412. }
  19413. void clif_enchantgrade_announce( map_session_data& sd, struct item& item, bool success ){
  19414. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19415. PACKET_ZC_GRADE_ENCHANT_BROADCAST_RESULT p = {};
  19416. p.packetType = HEADER_ZC_GRADE_ENCHANT_BROADCAST_RESULT;
  19417. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  19418. p.itemId = client_nameid( item.nameid );
  19419. p.grade = item.enchantgrade;
  19420. p.status = success;
  19421. clif_send( &p, sizeof( p ), nullptr, ALL_CLIENT );
  19422. #endif
  19423. }
  19424. void clif_parse_enchantgrade_start( int fd, map_session_data* sd ){
  19425. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19426. nullpo_retv( sd );
  19427. if( !sd->state.enchantgrade_open ){
  19428. return;
  19429. }
  19430. const PACKET_CZ_GRADE_ENCHANT_REQUEST* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_REQUEST*>( RFIFOP( fd, 0 ) );
  19431. uint16 index = server_index( p->index );
  19432. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19433. return;
  19434. }
  19435. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19436. // Unsupported item type - no answer
  19437. if( enchantgrade == nullptr ){
  19438. return;
  19439. }
  19440. uint16 level = 0;
  19441. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19442. level = sd->inventory_data[index]->weapon_level;
  19443. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19444. level = sd->inventory_data[index]->armor_level;
  19445. }
  19446. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19447. // Cannot upgrade this weapon or armor level - no answer
  19448. if( enchantgradelevels == enchantgrade->levels.end() ){
  19449. return;
  19450. }
  19451. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19452. // Cannot increase enchantgrade any further - no answer
  19453. if( enchantgradelevel == nullptr ){
  19454. return;
  19455. }
  19456. uint16 totalChance = enchantgradelevel->chances[sd->inventory.u.items_inventory[index].refine];
  19457. // No chance to increase the enchantgrade
  19458. if( totalChance == 0 ){
  19459. return;
  19460. }
  19461. std::shared_ptr<s_enchantgradeoption> option = util::map_find( enchantgradelevel->options, (uint16)p->material_index );
  19462. // Unknown option id - no answer
  19463. if( option == nullptr ){
  19464. return;
  19465. }
  19466. // Not enough zeny
  19467. if( sd->status.zeny < option->zeny ){
  19468. return;
  19469. }
  19470. uint16 steps = min( p->blessing_amount, enchantgradelevel->catalyst.maximumSteps );
  19471. std::unordered_map<uint16, uint16> requiredItems;
  19472. if( p->blessing_flag ){
  19473. // If the catalysator item is the same as the option item build the sum of amounts
  19474. if( enchantgradelevel->catalyst.item == option->item ){
  19475. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps + option->amount;
  19476. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19477. if( index < 0 ){
  19478. return;
  19479. }
  19480. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19481. return;
  19482. }
  19483. requiredItems[index] = amount;
  19484. }else{
  19485. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps;
  19486. // Check catalysator item
  19487. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19488. if( index < 0 ){
  19489. return;
  19490. }
  19491. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19492. return;
  19493. }
  19494. requiredItems[index] = amount;
  19495. // Check option item
  19496. index = pc_search_inventory( sd, option->item );
  19497. if( index < 0 ){
  19498. return;
  19499. }
  19500. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19501. return;
  19502. }
  19503. requiredItems[index] = option->amount;
  19504. }
  19505. totalChance += steps * enchantgradelevel->catalyst.chanceIncrease;
  19506. }else{
  19507. // Check option item
  19508. int16 index = pc_search_inventory( sd, option->item );
  19509. if( index < 0 ){
  19510. return;
  19511. }
  19512. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19513. return;
  19514. }
  19515. requiredItems[index] = option->amount;
  19516. }
  19517. // All items should be there, start deleting
  19518. for( const auto& pair : requiredItems ){
  19519. if( pc_delitem( sd, pair.first, pair.second, 0, 0, LOG_TYPE_ENCHANTGRADE ) != 0 ){
  19520. return;
  19521. }
  19522. }
  19523. if( pc_payzeny( sd, option->zeny, LOG_TYPE_ENCHANTGRADE ) > 0 ){
  19524. return;
  19525. }
  19526. if( rnd()%10000 < totalChance ){
  19527. // Log removal of item
  19528. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19529. // Increase enchantgrade
  19530. sd->inventory.u.items_inventory[index].enchantgrade = min( sd->inventory.u.items_inventory[index].enchantgrade + 1, MAX_ENCHANTGRADE );
  19531. // On successful enchantgrade increase the refine is reset
  19532. sd->inventory.u.items_inventory[index].refine = 0;
  19533. // Log retrieving the item again -> with the new refine and enchantgrade
  19534. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19535. // Show success
  19536. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_SUCCESS );
  19537. // Check if it has to be announced
  19538. if( enchantgradelevel->announceSuccess ){
  19539. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], true );
  19540. }
  19541. }else{
  19542. // Check if it has to be announced (has to be done before deleting the item from inventory)
  19543. if( enchantgradelevel->announceFail ){
  19544. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], false );
  19545. }
  19546. // Delete the item if it is breakable
  19547. if( option->breaking_rate > 0 && ( rnd() % 10000 ) < option->breaking_rate ){
  19548. // Delete the item
  19549. pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_ENCHANTGRADE );
  19550. // Show failure
  19551. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_BREAK );
  19552. // Downgrade the item if necessary
  19553. }else if( option->downgrade_amount > 0 ){
  19554. // Log removal of item
  19555. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19556. // Decrease refine level
  19557. sd->inventory.u.items_inventory[index].refine = cap_value( sd->inventory.u.items_inventory[index].refine - option->downgrade_amount, 0, MAX_REFINE );
  19558. // Log retrieving the item again -> with the new refine
  19559. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19560. // Show downgrade
  19561. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_DOWNGRADE );
  19562. // Only show failure, but dont do anything
  19563. }else{
  19564. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_FAILED );
  19565. }
  19566. }
  19567. #endif
  19568. }
  19569. void clif_parse_enchantgrade_close( int fd, map_session_data* sd ){
  19570. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19571. nullpo_retv( sd );
  19572. sd->state.enchantgrade_open = false;
  19573. #endif
  19574. }
  19575. void clif_reputation_type( map_session_data& sd, int64 type, int64 points ){
  19576. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19577. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19578. p->packetType = HEADER_ZC_REPUTE_INFO;
  19579. p->packetLength = sizeof( *p );
  19580. p->success = true;
  19581. PACKET_ZC_REPUTE_INFO_sub& entry = p->list[0];
  19582. entry.type = type;
  19583. entry.points = points;
  19584. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  19585. clif_send( p, p->packetLength, &sd.bl, SELF );
  19586. #endif
  19587. }
  19588. void clif_reputation_list( map_session_data& sd ){
  19589. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19590. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19591. p->packetType = HEADER_ZC_REPUTE_INFO;
  19592. p->packetLength = sizeof( *p );
  19593. p->success = true;
  19594. size_t index = 0;
  19595. for( const auto& entry : reputation_db ){
  19596. std::shared_ptr<s_reputation> reputation = entry.second;
  19597. PACKET_ZC_REPUTE_INFO_sub& list_entry = p->list[index];
  19598. list_entry.type = reputation->id;
  19599. list_entry.points = pc_readreg2( &sd, reputation->variable.c_str() );
  19600. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( list_entry ) );
  19601. index++;
  19602. }
  19603. clif_send( p, p->packetLength, &sd.bl, SELF );
  19604. #endif
  19605. }
  19606. void clif_item_reform_open( map_session_data& sd, t_itemid item ){
  19607. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19608. PACKET_ZC_OPEN_REFORM_UI p = {};
  19609. p.PacketType = HEADER_ZC_OPEN_REFORM_UI;
  19610. p.ITID = item;
  19611. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19612. sd.state.item_reform = item;
  19613. #endif
  19614. }
  19615. void clif_parse_item_reform_close( int fd, map_session_data* sd ){
  19616. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19617. sd->state.item_reform = 0;
  19618. #endif
  19619. }
  19620. void clif_item_reform_result( map_session_data& sd, uint16 index, uint8 result ){
  19621. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19622. PACKET_ZC_ITEM_REFORM_ACK p = {};
  19623. p.PacketType = HEADER_ZC_ITEM_REFORM_ACK;
  19624. p.index = client_index( index );
  19625. p.result = result;
  19626. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19627. if( result == 0 ){
  19628. // Client closes the window on success
  19629. sd.state.item_reform = 0;
  19630. }
  19631. #endif
  19632. }
  19633. void clif_parse_item_reform_start( int fd, map_session_data* sd ){
  19634. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19635. // Not opened
  19636. if( sd->state.item_reform == 0 ){
  19637. return;
  19638. }
  19639. const PACKET_CZ_ITEM_REFORM* p = reinterpret_cast<PACKET_CZ_ITEM_REFORM*>( RFIFOP( fd, 0 ) );
  19640. // Item mismatch
  19641. if( p->ITID != sd->state.item_reform ){
  19642. return;
  19643. }
  19644. uint16 index = server_index( p->index );
  19645. if( index >= MAX_INVENTORY ){
  19646. return;
  19647. }
  19648. if( sd->inventory_data[index] == nullptr ){
  19649. return;
  19650. }
  19651. std::shared_ptr<s_item_reform> reform = item_reform_db.find( sd->state.item_reform );
  19652. if( reform == nullptr ){
  19653. return;
  19654. }
  19655. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19656. std::shared_ptr<s_item_reform_base> base = util::umap_find( reform->base_items, selected_item.nameid );
  19657. if( base == nullptr ){
  19658. return;
  19659. }
  19660. // If target item is not identified
  19661. if( selected_item.identify == 0 ){
  19662. return;
  19663. }
  19664. // If target item is equipped
  19665. if( selected_item.equip != 0 ){
  19666. return;
  19667. }
  19668. // Check minimum refine requirement
  19669. if( selected_item.refine < base->minimumRefine ){
  19670. return;
  19671. }
  19672. // Check maximum refine requirement
  19673. if( selected_item.refine > base->maximumRefine ){
  19674. return;
  19675. }
  19676. // If no cards are allowed
  19677. if( !base->cardsAllowed ){
  19678. for( int i = 0; i < MAX_SLOTS; i++ ){
  19679. if( selected_item.card[i] != 0 ){
  19680. return;
  19681. }
  19682. }
  19683. }
  19684. // If random options are required
  19685. if( base->requiredRandomOptions > 0 ){
  19686. int i;
  19687. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19688. if( selected_item.option[i].id != 0 ){
  19689. break;
  19690. }
  19691. }
  19692. if( ( i + 1 ) < base->requiredRandomOptions ){
  19693. return;
  19694. }
  19695. }
  19696. std::unordered_map<uint16, uint16> materials;
  19697. // Check if all materials exist
  19698. for( const auto& material : base->materials ){
  19699. int16 material_index = pc_search_inventory( sd, material.first );
  19700. if( material_index < 0 ){
  19701. return;
  19702. }
  19703. if( sd->inventory.u.items_inventory[material_index].amount < material.second ){
  19704. return;
  19705. }
  19706. materials[material_index] = material.second;
  19707. }
  19708. // Remove the material
  19709. for( const auto& material : materials ){
  19710. if( pc_delitem( sd, material.first, material.second, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19711. return;
  19712. }
  19713. }
  19714. // If triggered from item
  19715. if( sd->itemid == sd->state.item_reform && pc_delitem( sd, sd->itemindex, 1, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19716. return;
  19717. }
  19718. // Log removal of item
  19719. log_pick_pc( sd, LOG_TYPE_REFORM, -1, &selected_item );
  19720. // Visually remove it from the client
  19721. clif_delitem( *sd, index, 1, 0 );
  19722. // Apply the random options
  19723. if( base->randomOptionGroup != nullptr ){
  19724. base->randomOptionGroup->apply( selected_item );
  19725. }
  19726. // Change the refine rate if needed
  19727. if( base->refineChange != 0 ){
  19728. selected_item.refine = cap_value( selected_item.refine + base->refineChange, 0, MAX_REFINE );
  19729. }
  19730. // Remove all cards and socket enchants
  19731. if( base->clearSlots ){
  19732. for( int i = 0; i < MAX_SLOTS; i++ ){
  19733. selected_item.card[i] = 0;
  19734. }
  19735. }
  19736. // Remove the current enchantgrade
  19737. if( base->removeEnchantgrade ){
  19738. selected_item.enchantgrade = 0;
  19739. }
  19740. // Finally change the item id
  19741. selected_item.nameid = base->resultItemId;
  19742. // Link inventory data cache to the new item
  19743. sd->inventory_data[index] = itemdb_search( base->resultItemId );
  19744. // Log retrieving the item again -> with the new options
  19745. log_pick_pc( sd, LOG_TYPE_REFORM, 1, &selected_item );
  19746. // Make it visible for the client again
  19747. clif_additem( sd, index, 1, 0 );
  19748. clif_item_reform_result( *sd, index, 0 );
  19749. #endif
  19750. }
  19751. void clif_enchantwindow_open( map_session_data& sd, uint64 clientLuaIndex ){
  19752. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19753. // Hardcoded clientside check
  19754. if( sd.weight > ( ( sd.max_weight * 70 ) / 100 ) ){
  19755. clif_msg_color( &sd, MSI_ENCHANT_FAILED_OVER_WEIGHT, color_table[COLOR_RED] );
  19756. sd.state.item_enchant_index = 0;
  19757. return;
  19758. }
  19759. PACKET_ZC_UI_OPEN_V3 p = {};
  19760. p.packetType = HEADER_ZC_UI_OPEN_V3;
  19761. p.type = OUT_UI_ENCHANT;
  19762. p.data = clientLuaIndex;
  19763. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19764. sd.state.item_enchant_index = clientLuaIndex;
  19765. #endif
  19766. }
  19767. void clif_enchantwindow_result( map_session_data& sd, bool success, t_itemid enchant = 0 ){
  19768. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19769. PACKET_ZC_RESPONSE_ENCHANT p = {};
  19770. p.PacketType = HEADER_ZC_RESPONSE_ENCHANT;
  19771. if( success ){
  19772. p.msgId = MSI_ENCHANT_SUCCESS;
  19773. }else{
  19774. p.msgId = MSI_ENCHANT_FAILED;
  19775. }
  19776. p.ITID = enchant;
  19777. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19778. sd.state.item_enchant_index = 0;
  19779. #endif
  19780. }
  19781. bool clif_parse_enchant_basecheck( struct item& selected_item, std::shared_ptr<s_item_enchant> enchant ){
  19782. if( selected_item.equip != 0 ){
  19783. return false;
  19784. }
  19785. if( selected_item.equipSwitch != 0 ){
  19786. return false;
  19787. }
  19788. if( selected_item.attribute != 0 ){
  19789. return false;
  19790. }
  19791. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  19792. return false;
  19793. }
  19794. if( selected_item.refine < enchant->minimumRefine ){
  19795. return false;
  19796. }
  19797. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  19798. return false;
  19799. }
  19800. if( !enchant->allowRandomOptions ){
  19801. for( const s_item_randomoption& option : selected_item.option ){
  19802. if( option.id != 0 ){
  19803. return false;
  19804. }
  19805. }
  19806. }
  19807. return true;
  19808. }
  19809. void clif_parse_enchantwindow_general( int fd, map_session_data* sd ){
  19810. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19811. const PACKET_CZ_REQUEST_RANDOM_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RANDOM_ENCHANT*>( RFIFOP( fd, 0 ) );
  19812. if( sd->state.item_enchant_index != p->enchant_group ){
  19813. return;
  19814. }
  19815. uint16 index = server_index( p->index );
  19816. if( index >= MAX_INVENTORY ){
  19817. return;
  19818. }
  19819. if( sd->inventory_data[index] == nullptr ){
  19820. return;
  19821. }
  19822. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19823. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19824. if( enchant == nullptr ){
  19825. return;
  19826. }
  19827. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19828. return;
  19829. }
  19830. uint16 slot = MAX_SLOTS;
  19831. for( uint16 nextSlot : enchant->order ){
  19832. if( selected_item.card[nextSlot] == 0 ){
  19833. slot = nextSlot;
  19834. break;
  19835. }
  19836. }
  19837. if( slot == MAX_SLOTS ){
  19838. return;
  19839. }
  19840. if( slot < sd->inventory_data[index]->slots ){
  19841. return;
  19842. }
  19843. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19844. if( enchant_slot == nullptr ){
  19845. return;
  19846. }
  19847. std::shared_ptr<s_item_enchant_normal> enchants_for_enchantgrade = util::umap_find( enchant_slot->normal.enchants, (uint16)selected_item.enchantgrade );
  19848. if( enchants_for_enchantgrade == nullptr ){
  19849. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 829 ), false, SELF); // Enchanting is not possible for your item's enchant grade.
  19850. clif_enchantwindow_result( *sd, false );
  19851. return;
  19852. }
  19853. if( sd->status.zeny < enchant_slot->normal.zeny ){
  19854. return;
  19855. }
  19856. std::unordered_map<uint16, uint16> materials;
  19857. for( const auto& entry : enchant_slot->normal.materials ){
  19858. int16 idx = pc_search_inventory( sd, entry.first );
  19859. if( idx < 0 ){
  19860. return;
  19861. }
  19862. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19863. return;
  19864. }
  19865. materials[idx] = entry.second;
  19866. }
  19867. if( pc_payzeny( sd, enchant_slot->normal.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19868. return;
  19869. }
  19870. for( const auto& entry : materials ){
  19871. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19872. return;
  19873. }
  19874. }
  19875. uint32 chance = enchant_slot->normal.chance;
  19876. for( int i = 0; i <= MAX_ENCHANTGRADE; i++ ){
  19877. chance += enchant_slot->normal.enchantgradeChanceIncrease[i];
  19878. }
  19879. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  19880. clif_enchantwindow_result( *sd, false );
  19881. return;
  19882. }
  19883. // Log removal of item
  19884. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19885. size_t maximum = 3 * enchant_slot->normal.enchants.size();
  19886. bool enchanted = false;
  19887. for( int i = 0; i < maximum; i++ ){
  19888. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  19889. if( rnd_value( 0, 10000 ) < normal_enchant->chance ){
  19890. selected_item.card[slot] = normal_enchant->item_id;
  19891. enchanted = true;
  19892. break;
  19893. }
  19894. }
  19895. if( !enchanted ){
  19896. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  19897. selected_item.card[slot] = normal_enchant->item_id;
  19898. }
  19899. // Log retrieving the item again -> with the new enchant
  19900. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  19901. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  19902. #endif
  19903. }
  19904. void clif_parse_enchantwindow_perfect( int fd, map_session_data* sd ){
  19905. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19906. const PACKET_CZ_REQUEST_PERFECT_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_PERFECT_ENCHANT*>( RFIFOP( fd, 0 ) );
  19907. if( sd->state.item_enchant_index != p->enchant_group ){
  19908. return;
  19909. }
  19910. uint16 index = server_index( p->index );
  19911. if( index >= MAX_INVENTORY ){
  19912. return;
  19913. }
  19914. if( sd->inventory_data[index] == nullptr ){
  19915. return;
  19916. }
  19917. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19918. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19919. if( enchant == nullptr ){
  19920. return;
  19921. }
  19922. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19923. return;
  19924. }
  19925. uint16 slot = MAX_SLOTS;
  19926. for( uint16 nextSlot : enchant->order ){
  19927. if( selected_item.card[nextSlot] == 0 ){
  19928. slot = nextSlot;
  19929. break;
  19930. }
  19931. }
  19932. if( slot == MAX_SLOTS ){
  19933. return;
  19934. }
  19935. if( slot < sd->inventory_data[index]->slots ){
  19936. return;
  19937. }
  19938. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19939. if( enchant_slot == nullptr ){
  19940. return;
  19941. }
  19942. std::shared_ptr<s_item_enchant_perfect> perfect_enchant = util::umap_find( enchant_slot->perfect.enchants, p->ITID );
  19943. if( perfect_enchant == nullptr ){
  19944. return;
  19945. }
  19946. if( sd->status.zeny < perfect_enchant->zeny ){
  19947. return;
  19948. }
  19949. std::unordered_map<uint16, uint16> materials;
  19950. for( const auto& entry : perfect_enchant->materials ){
  19951. int16 idx = pc_search_inventory( sd, entry.first );
  19952. if( idx < 0 ){
  19953. return;
  19954. }
  19955. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19956. return;
  19957. }
  19958. materials[idx] = entry.second;
  19959. }
  19960. if( pc_payzeny( sd, perfect_enchant->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19961. return;
  19962. }
  19963. for( const auto& entry : materials ){
  19964. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19965. return;
  19966. }
  19967. }
  19968. // Log removal of item
  19969. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19970. selected_item.card[slot] = perfect_enchant->item_id;
  19971. // Log retrieving the item again -> with the new enchant
  19972. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  19973. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  19974. #endif
  19975. }
  19976. void clif_parse_enchantwindow_upgrade( int fd, map_session_data* sd ){
  19977. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19978. const PACKET_CZ_REQUEST_UPGRADE_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_UPGRADE_ENCHANT*>( RFIFOP( fd, 0 ) );
  19979. if( sd->state.item_enchant_index != p->enchant_group ){
  19980. return;
  19981. }
  19982. uint16 index = server_index( p->index );
  19983. if( index >= MAX_INVENTORY ){
  19984. return;
  19985. }
  19986. if( sd->inventory_data[index] == nullptr ){
  19987. return;
  19988. }
  19989. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19990. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19991. if( enchant == nullptr ){
  19992. return;
  19993. }
  19994. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19995. return;
  19996. }
  19997. uint16 slot = p->slot;
  19998. if( slot >= MAX_SLOTS ){
  19999. return;
  20000. }
  20001. if( slot < sd->inventory_data[index]->slots ){
  20002. return;
  20003. }
  20004. if( selected_item.card[slot] == 0 ){
  20005. return;
  20006. }
  20007. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  20008. if( enchant_slot == nullptr ){
  20009. return;
  20010. }
  20011. std::shared_ptr<s_item_enchant_upgrade> upgrade = util::umap_find( enchant_slot->upgrade.enchants, selected_item.card[slot] );
  20012. if( upgrade == nullptr ){
  20013. return;
  20014. }
  20015. if( sd->status.zeny < upgrade->zeny ){
  20016. return;
  20017. }
  20018. std::unordered_map<uint16, uint16> materials;
  20019. for( const auto& entry : upgrade->materials ){
  20020. int16 idx = pc_search_inventory( sd, entry.first );
  20021. if( idx < 0 ){
  20022. return;
  20023. }
  20024. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20025. return;
  20026. }
  20027. materials[idx] = entry.second;
  20028. }
  20029. if( pc_payzeny( sd, upgrade->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20030. return;
  20031. }
  20032. for( const auto& entry : materials ){
  20033. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20034. return;
  20035. }
  20036. }
  20037. // Log removal of item
  20038. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20039. selected_item.card[slot] = upgrade->upgrade_item_id;
  20040. // Log retrieving the item again -> with the new enchant
  20041. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20042. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  20043. #endif
  20044. }
  20045. void clif_parse_enchantwindow_reset( int fd, map_session_data* sd ){
  20046. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20047. const PACKET_CZ_REQUEST_RESET_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RESET_ENCHANT*>( RFIFOP( fd, 0 ) );
  20048. if( sd->state.item_enchant_index != p->enchant_group ){
  20049. return;
  20050. }
  20051. uint16 index = server_index( p->index );
  20052. if( index >= MAX_INVENTORY ){
  20053. return;
  20054. }
  20055. if( sd->inventory_data[index] == nullptr ){
  20056. return;
  20057. }
  20058. struct item& selected_item = sd->inventory.u.items_inventory[index];
  20059. if( selected_item.equip != 0 ){
  20060. return;
  20061. }
  20062. if( selected_item.equipSwitch != 0 ){
  20063. return;
  20064. }
  20065. if( selected_item.attribute != 0 ){
  20066. return;
  20067. }
  20068. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  20069. if( enchant == nullptr ){
  20070. return;
  20071. }
  20072. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  20073. return;
  20074. }
  20075. if( selected_item.refine < enchant->minimumRefine ){
  20076. return;
  20077. }
  20078. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  20079. return;
  20080. }
  20081. bool is_enchanted = false;
  20082. for( int i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20083. if( selected_item.card[i] != 0 ){
  20084. is_enchanted = true;
  20085. break;
  20086. }
  20087. }
  20088. if( !is_enchanted ){
  20089. return;
  20090. }
  20091. if( sd->status.zeny < enchant->reset.zeny ){
  20092. return;
  20093. }
  20094. std::unordered_map<uint16, uint16> materials;
  20095. for( const auto& entry : enchant->reset.materials ){
  20096. int16 idx = pc_search_inventory( sd, entry.first );
  20097. if( idx < 0 ){
  20098. return;
  20099. }
  20100. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20101. return;
  20102. }
  20103. materials[idx] = entry.second;
  20104. }
  20105. if( pc_payzeny( sd, enchant->reset.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20106. return;
  20107. }
  20108. for( const auto& entry : materials ){
  20109. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20110. return;
  20111. }
  20112. }
  20113. uint32 chance = enchant->reset.chance;
  20114. if( chance == 0 ){
  20115. return;
  20116. }
  20117. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  20118. clif_enchantwindow_result( *sd, false );
  20119. return;
  20120. }
  20121. // Log removal of item
  20122. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20123. for( int i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20124. selected_item.card[i] = 0;
  20125. }
  20126. // Log retrieving the item again -> with the new enchant
  20127. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20128. clif_enchantwindow_result( *sd, true );
  20129. #endif
  20130. }
  20131. void clif_parse_enchantwindow_close( int fd, map_session_data* sd ){
  20132. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20133. sd->state.item_enchant_index = 0;
  20134. #endif
  20135. }
  20136. void clif_parse_itempackage_select( int fd, map_session_data* sd ){
  20137. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220316
  20138. const PACKET_CZ_USE_PACKAGEITEM* p = reinterpret_cast<PACKET_CZ_USE_PACKAGEITEM*>( RFIFOP( fd, 0 ) );
  20139. if( p->AID != sd->status.account_id ){
  20140. return;
  20141. }
  20142. uint16 index = server_index( p->index );
  20143. if( index >= MAX_INVENTORY ){
  20144. return;
  20145. }
  20146. if( sd->inventory_data[index] == nullptr ){
  20147. return;
  20148. }
  20149. if( sd->inventory_data[index]->nameid != p->itemID ){
  20150. return;
  20151. }
  20152. if( sd->inventory_data[index]->elv > sd->status.base_level ){
  20153. return;
  20154. }
  20155. std::shared_ptr<s_item_package> package = item_package_db.find( p->itemID );
  20156. if( package == nullptr ){
  20157. return;
  20158. }
  20159. std::shared_ptr<s_item_package_group> group = util::umap_find( package->groups, p->BoxIndex );
  20160. if( group == nullptr ){
  20161. return;
  20162. }
  20163. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_PACKAGE ) != 0 ){
  20164. return;
  20165. }
  20166. for( const auto& entry : group->items ){
  20167. struct item item = {};
  20168. item.nameid = entry.second->item_id;
  20169. item.identify = 1;
  20170. item.refine = (char)entry.second->refine;
  20171. if( entry.second->rentalhours ){
  20172. item.expire_time = (uint32)( time( nullptr ) + entry.second->rentalhours * 3600 );
  20173. }
  20174. // Check if it is a pet egg
  20175. std::shared_ptr<s_pet_db> pet = pet_db_search( item.nameid, PET_EGG );
  20176. if( pet != nullptr ){
  20177. for( int i = 0; i < entry.second->amount; i++ ){
  20178. pet_create_egg( sd, item.nameid );
  20179. }
  20180. }else if( entry.second->amount > 1 && ( !itemdb_isstackable( item.nameid ) || item.expire_time > 0 ) ){
  20181. for( int i = 0; i < entry.second->amount; i++ ){
  20182. // New random options on each iteration
  20183. if( entry.second->randomOptionGroup != nullptr ){
  20184. entry.second->randomOptionGroup->apply( item );
  20185. }
  20186. pc_additem( sd, &item, 1, LOG_TYPE_PACKAGE );
  20187. }
  20188. }else{
  20189. if( entry.second->randomOptionGroup != nullptr ){
  20190. entry.second->randomOptionGroup->apply( item );
  20191. }
  20192. pc_additem( sd, &item, entry.second->amount, LOG_TYPE_PACKAGE );
  20193. }
  20194. }
  20195. #endif
  20196. }
  20197. void clif_broadcast_refine_result(map_session_data& sd, t_itemid itemId, int8 level, bool success)
  20198. {
  20199. #if PACKETVER_MAIN_NUM >= 20170906 || PACKETVER_RE_NUM >= 20170830 || defined(PACKETVER_ZERO)
  20200. PACKET_ZC_BROADCAST_ITEMREFINING_RESULT p{};
  20201. p.packetType = HEADER_ZC_BROADCAST_ITEMREFINING_RESULT;
  20202. p.itemId = itemId;
  20203. p.refine_level = level;
  20204. p.status = (int8)success;
  20205. if (battle_config.broadcast_hide_name) {
  20206. std::string dispname = clif_hide_name(sd.status.name);
  20207. safestrncpy(p.name, dispname.c_str(), sizeof(p.name));
  20208. }
  20209. else {
  20210. safestrncpy(p.name, sd.status.name, sizeof(p.name));
  20211. }
  20212. clif_send(&p, sizeof(p), &sd.bl, ALL_CLIENT);
  20213. #endif
  20214. }
  20215. void clif_parse_captcha_register(int fd, map_session_data *sd) {
  20216. #if PACKETVER >= 20160316
  20217. nullpo_retv(sd);
  20218. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20219. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20220. return;
  20221. }
  20222. const PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20223. pc_macro_captcha_register(*sd, p->imageSize, p->answer);
  20224. #endif
  20225. }
  20226. void clif_captcha_upload_request(map_session_data &sd) {
  20227. #if PACKETVER >= 20160330
  20228. PACKET_ZC_ACK_UPLOAD_MACRO_DETECTOR p = {};
  20229. p.PacketType = HEADER_ZC_ACK_UPLOAD_MACRO_DETECTOR;
  20230. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20231. if (sd.captcha_upload.cd != nullptr) {
  20232. p.captchaFlag = 0;
  20233. } else {
  20234. p.captchaFlag = 1;
  20235. }
  20236. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20237. #endif
  20238. }
  20239. void clif_parse_captcha_upload(int fd, map_session_data *sd) {
  20240. #if PACKETVER >= 20160316
  20241. nullpo_retv(sd);
  20242. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20243. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20244. return;
  20245. }
  20246. const PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20247. int16 upload_size = p->PacketLength - sizeof( * p );
  20248. if (upload_size < 1 || upload_size > MAX_CAPTCHA_CHUNK_SIZE)
  20249. return;
  20250. if (sd->captcha_upload.upload_size + upload_size > sd->captcha_upload.cd->image_size)
  20251. return;
  20252. pc_macro_captcha_register_upload(*sd, upload_size, p->imageData);
  20253. #endif
  20254. }
  20255. void clif_captcha_upload_end(map_session_data &sd) {
  20256. #if PACKETVER >= 20160330
  20257. PACKET_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA p = {};
  20258. p.PacketType = HEADER_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA;
  20259. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20260. #endif
  20261. }
  20262. void clif_parse_captcha_preview_request(int fd, map_session_data *sd) {
  20263. #if PACKETVER >= 20160323
  20264. nullpo_retv(sd);
  20265. if (!(pc_has_permission(sd, PC_PERM_MACRO_REGISTER) && pc_has_permission(sd, PC_PERM_MACRO_DETECT))) {
  20266. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20267. return;
  20268. }
  20269. const PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20270. clif_captcha_preview_response(*sd, captcha_db.find(p->captchaID));
  20271. #endif
  20272. }
  20273. void clif_captcha_preview_response(map_session_data &sd, std::shared_ptr<s_captcha_data> cd) {
  20274. #if PACKETVER >= 20160330
  20275. PACKET_ZC_ACK_PREVIEW_MACRO_DETECTOR p = {};
  20276. p.PacketType = HEADER_ZC_ACK_PREVIEW_MACRO_DETECTOR;
  20277. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20278. if (cd == nullptr) {
  20279. p.captchaFlag = 1;
  20280. p.imageSize = 0;
  20281. } else {
  20282. p.captchaFlag = 0;
  20283. p.imageSize = cd->image_size;
  20284. }
  20285. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20286. if (cd != nullptr) {
  20287. for (uint16 offset = 0; offset < cd->image_size;) {
  20288. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20289. PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20290. p2->PacketType = HEADER_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA;
  20291. p2->PacketLength = sizeof( *p2 );
  20292. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20293. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20294. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20295. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20296. offset += chunk_size;
  20297. }
  20298. }
  20299. #endif
  20300. }
  20301. void clif_macro_detector_request(map_session_data &sd) {
  20302. #if PACKETVER >= 20160330
  20303. std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  20304. if (cd == nullptr) {
  20305. return;
  20306. }
  20307. // Send preview initialization request to the client.
  20308. PACKET_ZC_APPLY_MACRO_DETECTOR p = {};
  20309. p.PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR;
  20310. p.imageSize = cd->image_size;
  20311. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20312. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20313. for (uint16 offset = 0; offset < cd->image_size;) {
  20314. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20315. PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20316. p2->PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR_CAPTCHA;
  20317. p2->PacketLength = sizeof( *p2 );
  20318. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20319. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20320. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20321. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20322. offset += chunk_size;
  20323. }
  20324. #endif
  20325. }
  20326. void clif_macro_detector_request_show(map_session_data &sd) {
  20327. #if PACKETVER >= 20160330
  20328. PACKET_ZC_REQ_ANSWER_MACRO_DETECTOR p = {};
  20329. p.PacketType = HEADER_ZC_REQ_ANSWER_MACRO_DETECTOR;
  20330. p.retryCount = sd.macro_detect.retry;
  20331. p.timeout = battle_config.macro_detection_timeout;
  20332. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20333. #endif
  20334. }
  20335. void clif_parse_macro_detector_download_ack(int fd, map_session_data *sd) {
  20336. #if PACKETVER >= 20160316
  20337. nullpo_retv(sd);
  20338. if (sd->macro_detect.retry != 0) {
  20339. //const PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20340. clif_macro_detector_request_show(*sd);
  20341. }
  20342. #endif
  20343. }
  20344. void clif_parse_macro_detector_answer(int fd, map_session_data *sd) {
  20345. #if PACKETVER >= 20160316
  20346. nullpo_retv(sd);
  20347. const PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20348. pc_macro_detector_process_answer(*sd, p->answer);
  20349. #endif
  20350. }
  20351. void clif_macro_detector_status(map_session_data &sd, e_macro_detect_status stype) {
  20352. #if PACKETVER >= 20160330
  20353. PACKET_ZC_CLOSE_MACRO_DETECTOR p = {};
  20354. p.PacketType = HEADER_ZC_CLOSE_MACRO_DETECTOR;
  20355. p.status = stype;
  20356. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20357. #endif
  20358. }
  20359. void clif_parse_macro_reporter_select(int fd, map_session_data *sd) {
  20360. #if PACKETVER >= 20160330
  20361. nullpo_retv(sd);
  20362. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20363. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20364. return;
  20365. }
  20366. const PACKET_CZ_REQ_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_CZ_REQ_PLAYER_AID_IN_RANGE*>( RFIFOP( fd, 0 ) );
  20367. pc_macro_reporter_area_select(*sd, p->xPos, p->yPos, p->RadiusRange);
  20368. #endif
  20369. }
  20370. void clif_macro_reporter_select(map_session_data &sd, const std::vector<uint32> &aid_list) {
  20371. #if PACKETVER >= 20160330
  20372. PACKET_ZC_ACK_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_ZC_ACK_PLAYER_AID_IN_RANGE*>( packet_buffer );
  20373. p->PacketType = HEADER_ZC_ACK_PLAYER_AID_IN_RANGE;
  20374. p->PacketLength = sizeof( *p );
  20375. for (size_t i = 0; i < aid_list.size(); i++){
  20376. p->AID[i] = aid_list[static_cast<int32>(i)];
  20377. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->AID[0] ) );
  20378. }
  20379. clif_send(p, p->PacketLength, &sd.bl, SELF);
  20380. #endif
  20381. }
  20382. void clif_parse_macro_reporter_ack(int fd, map_session_data *sd) {
  20383. #if PACKETVER >= 20160316
  20384. nullpo_retv(sd);
  20385. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20386. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20387. return;
  20388. }
  20389. const PACKET_CZ_REQ_APPLY_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20390. map_session_data *tsd = map_id2sd(p->AID);
  20391. if (tsd == nullptr) {
  20392. clif_displaymessage(fd, msg_txt(sd, 3)); // Character not found.
  20393. return;
  20394. }
  20395. if (tsd->macro_detect.retry != 0) {
  20396. clif_macro_reporter_status(*sd, MCR_INPROGRESS);
  20397. return;
  20398. }
  20399. if (captcha_db.empty()) {
  20400. clif_macro_reporter_status(*sd, MCR_NO_DATA);
  20401. return;
  20402. }
  20403. pc_macro_reporter_process(*tsd, sd->status.account_id);
  20404. clif_macro_reporter_status(*sd, MCR_MONITORING);
  20405. #endif
  20406. }
  20407. void clif_macro_reporter_status(map_session_data &sd, e_macro_report_status stype) {
  20408. #if PACKETVER >= 20160330
  20409. PACKET_ZC_ACK_APPLY_MACRO_DETECTOR p = {};
  20410. p.PacketType = HEADER_ZC_ACK_APPLY_MACRO_DETECTOR;
  20411. p.status = stype;
  20412. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20413. #endif
  20414. }
  20415. void clif_dynamicnpc_result( map_session_data& sd, e_dynamicnpc_result result ){
  20416. #if PACKETVER_MAIN_NUM >= 20140430 || PACKETVER_RE_NUM >= 20140430 || defined(PACKETVER_ZERO)
  20417. PACKET_ZC_DYNAMICNPC_CREATE_RESULT p = {};
  20418. p.PacketType = HEADER_ZC_DYNAMICNPC_CREATE_RESULT;
  20419. p.result = result;
  20420. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20421. #endif
  20422. }
  20423. void clif_partybooking_ask( map_session_data* sd, map_session_data* joining_sd ){
  20424. #if PACKETVER >= 20191204
  20425. PACKET_ZC_PARTY_REQ_MASTER_TO_JOIN p = {};
  20426. p.packetType = HEADER_ZC_PARTY_REQ_MASTER_TO_JOIN;
  20427. p.CID = joining_sd->status.char_id;
  20428. p.AID = joining_sd->status.account_id;
  20429. safestrncpy( p.name, joining_sd->status.name, NAME_LENGTH );
  20430. p.x = joining_sd->status.base_level;
  20431. p.y = joining_sd->status.class_;
  20432. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20433. #endif
  20434. }
  20435. void clif_parse_partybooking_join( int fd, map_session_data* sd ){
  20436. #if PACKETVER >= 20191204
  20437. const PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20438. // Character is already in a party
  20439. if( sd->status.party_id != 0 ){
  20440. return;
  20441. }
  20442. map_session_data* tsd = map_charid2sd( p->CID );
  20443. // Target player is offline
  20444. if( tsd == nullptr ){
  20445. return;
  20446. }
  20447. if( tsd->status.account_id != p->AID ){
  20448. return;
  20449. }
  20450. struct s_party_booking_requirement requirement;
  20451. if( !party_booking_load( tsd->status.account_id, tsd->status.char_id, &requirement ) ){
  20452. return;
  20453. }
  20454. if( sd->status.base_level < requirement.minimum_level ){
  20455. return;
  20456. }
  20457. if( sd->status.base_level > requirement.maximum_level ){
  20458. return;
  20459. }
  20460. // Already requested to join this party
  20461. if( util::vector_exists( tsd->party_booking_requests, sd->status.char_id ) ){
  20462. return;
  20463. }
  20464. // Store information that the player tried to join the party
  20465. tsd->party_booking_requests.push_back( sd->status.char_id );
  20466. clif_partybooking_ask( tsd, sd );
  20467. #endif
  20468. }
  20469. void clif_partybooking_reply( map_session_data* sd, map_session_data* party_leader_sd, bool accepted ){
  20470. #if PACKETVER >= 20191204
  20471. PACKET_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER p = {};
  20472. if( party_leader_sd->status.party_id == 0 ){
  20473. return;
  20474. }
  20475. struct party_data* party = party_search( party_leader_sd->status.party_id );
  20476. if( party == nullptr ){
  20477. return;
  20478. }
  20479. p.packetType = HEADER_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER;
  20480. safestrncpy( p.player_name, party_leader_sd->status.name, NAME_LENGTH );
  20481. safestrncpy( p.party_name, party->party.name, NAME_LENGTH );
  20482. p.AID = party_leader_sd->status.account_id;
  20483. p.refused = !accepted;
  20484. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20485. #endif
  20486. }
  20487. void clif_parse_partybooking_reply( int fd, map_session_data* sd ){
  20488. #if PACKETVER >= 20191204
  20489. const PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20490. map_session_data* tsd = map_charid2sd( p->CID );
  20491. // Target player is offline
  20492. if( tsd == nullptr ){
  20493. return;
  20494. }
  20495. if( tsd->status.account_id != p->AID ){
  20496. return;
  20497. }
  20498. // Check if the player even requested to join the party
  20499. if( !util::vector_exists( sd->party_booking_requests, p->CID ) ){
  20500. return;
  20501. }
  20502. util::vector_erase_if_exists( sd->party_booking_requests, p->CID );
  20503. // Only party leaders can reply
  20504. if( !party_isleader( sd ) ){
  20505. return;
  20506. }
  20507. struct party_data* party = party_search( sd->status.party_id );
  20508. if( party == nullptr ){
  20509. return;
  20510. }
  20511. if( party->instance_id > 0 && battle_config.instance_block_invite ){
  20512. return;
  20513. }
  20514. if( p->accept ){
  20515. party_join( *tsd, sd->status.party_id );
  20516. }
  20517. clif_partybooking_reply( tsd, sd, p->accept );
  20518. #endif
  20519. }
  20520. void clif_parse_reset_skill( int fd, map_session_data* sd ){
  20521. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220203
  20522. const PACKET_CZ_RESET_SKILL* p = reinterpret_cast<PACKET_CZ_RESET_SKILL*>( RFIFOP( fd, 0 ) );
  20523. #endif
  20524. }
  20525. void clif_set_dialog_align(map_session_data& sd, int npcid, e_say_dialog_align align)
  20526. {
  20527. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20528. PACKET_ZC_DIALOG_TEXT_ALIGN p = {};
  20529. p.PacketType = HEADER_ZC_DIALOG_TEXT_ALIGN;
  20530. p.align = align;
  20531. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20532. #endif // PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20533. }
  20534. void clif_set_npc_window_size(map_session_data& sd, int width, int height)
  20535. {
  20536. #if PACKETVER_MAIN_NUM >= 20220504
  20537. PACKET_ZC_DIALOG_WINDOW_SIZE p = {};
  20538. p.PacketType = HEADER_ZC_DIALOG_WINDOW_SIZE;
  20539. p.width = width;
  20540. p.height = height;
  20541. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20542. #endif // PACKETVER_MAIN_NUM >= 20220504
  20543. }
  20544. void clif_set_npc_window_pos(map_session_data& sd, int x, int y)
  20545. {
  20546. #if PACKETVER_MAIN_NUM >= 20220504
  20547. PACKET_ZC_DIALOG_WINDOW_POS p = {};
  20548. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS;
  20549. p.x = x;
  20550. p.y = y;
  20551. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20552. #endif // PACKETVER_MAIN_NUM >= 20220504
  20553. }
  20554. void clif_set_npc_window_pos_percent(map_session_data& sd, int x, int y)
  20555. {
  20556. #if PACKETVER_MAIN_NUM >= 20220504
  20557. PACKET_ZC_DIALOG_WINDOW_POS2 p = {};
  20558. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS2;
  20559. p.x = x;
  20560. p.y = y;
  20561. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20562. #endif // PACKETVER_MAIN_NUM >= 20220504
  20563. }
  20564. /// Displays a special popup.
  20565. /// Works only if player moved from one map to another.
  20566. /// 0bbe <popup id>.L (ZC_SPECIALPOPUP)
  20567. void clif_specialpopup(map_session_data& sd, int32 id ){
  20568. #if PACKETVER >= 20221005
  20569. PACKET_ZC_SPECIALPOPUP p = {};
  20570. p.PacketType = HEADER_ZC_SPECIALPOPUP;
  20571. p.ppId = id;
  20572. clif_send( &p, sizeof( p ), &sd.bl, SELF);
  20573. #endif
  20574. }
  20575. /*==========================================
  20576. * Main client packet processing function
  20577. *------------------------------------------*/
  20578. static int clif_parse(int fd)
  20579. {
  20580. int cmd, packet_len;
  20581. TBL_PC* sd;
  20582. int pnum;
  20583. #ifdef PACKET_OBFUSCATION
  20584. int cmd2;
  20585. #endif
  20586. //TODO apply delays or disconnect based on packet throughput [FlavioJS]
  20587. // Note: "click masters" can do 80+ clicks in 10 seconds
  20588. 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]
  20589. { // begin main client packet processing loop
  20590. sd = (TBL_PC *)session[fd]->session_data;
  20591. if (session[fd]->flag.eof) {
  20592. if (sd) {
  20593. if (sd->state.autotrade) {
  20594. //Disassociate character from the socket connection.
  20595. session[fd]->session_data = nullptr;
  20596. sd->fd = 0;
  20597. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off (using @autotrade).\n", sd->status.name);
  20598. } else
  20599. if (sd->state.active) {
  20600. // Player logout display [Valaris]
  20601. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off.\n", sd->status.name);
  20602. clif_quitsave(fd, sd);
  20603. } else {
  20604. //Unusual logout (during log on/off/map-changer procedure)
  20605. ShowInfo("Player AID:%d/CID:%d logged off.\n", sd->status.account_id, sd->status.char_id);
  20606. map_quit(sd);
  20607. }
  20608. } else {
  20609. ShowInfo("Closed connection from '" CL_WHITE "%s" CL_RESET "'.\n", ip2str(session[fd]->client_addr, nullptr));
  20610. }
  20611. do_close(fd);
  20612. return 0;
  20613. }
  20614. if (RFIFOREST(fd) < 2)
  20615. return 0;
  20616. cmd = RFIFOW(fd, 0);
  20617. #ifdef PACKET_OBFUSCATION
  20618. // Check if it is a player that tries to connect to the map server.
  20619. if( sd ){
  20620. // Decrypt the current packet ID with the last key stored in the session.
  20621. cmd = (cmd ^ ((sd->cryptKey >> 16) & 0x7FFF));
  20622. }else{
  20623. // Store the original value for checking
  20624. cmd2 = cmd;
  20625. // A player tries to connect - use the initial keys for the decryption of the packet ID.
  20626. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20627. }
  20628. #endif
  20629. // filter out invalid / unsupported packets
  20630. if (cmd > MAX_PACKET_DB || cmd < MIN_PACKET_DB || packet_db[cmd].len == 0) {
  20631. ShowWarning("clif_parse: Received unsupported packet (packet 0x%04x, %d bytes received), disconnecting session #%d.\n", cmd, RFIFOREST(fd), fd);
  20632. #ifdef DUMP_INVALID_PACKET
  20633. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20634. #endif
  20635. #ifdef PACKET_OBFUSCATION_WARN
  20636. // If it is a connection attempt, check if the packet would have been valid without decrypting it
  20637. if( !sd ){
  20638. #ifdef PACKET_OBFUSCATION
  20639. if( cmd2 >= MIN_PACKET_DB && cmd2 < MAX_PACKET_DB && packet_db[cmd2].len != 0 && packet_db[cmd2].func == clif_parse_WantToConnection ){
  20640. ShowWarning( "clif_parse: It looks like you have enabled PACKET_OBFUSCATION on server side, but disabled it on client side.\n" );
  20641. }
  20642. #else
  20643. // Try to use the initial keys for the decryption of the packet ID.
  20644. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20645. if( cmd >= MIN_PACKET_DB && cmd < MAX_PACKET_DB && packet_db[cmd].len != 0 && packet_db[cmd].func == clif_parse_WantToConnection ){
  20646. ShowWarning( "clif_parse: It looks like you have disabled PACKET_OBFUSCATION on server side, but enabled it on client side.\n" );
  20647. }
  20648. #endif
  20649. }
  20650. #endif
  20651. set_eof(fd);
  20652. return 0;
  20653. }
  20654. // determine real packet length
  20655. packet_len = packet_db[cmd].len;
  20656. if (packet_len == -1) { // variable-length packet
  20657. if (RFIFOREST(fd) < 4)
  20658. return 0;
  20659. packet_len = RFIFOW(fd,2);
  20660. if (packet_len < 4 || packet_len > 32768) {
  20661. ShowWarning("clif_parse: Received packet 0x%04x specifies invalid packet_len (%d), disconnecting session #%d.\n", cmd, packet_len, fd);
  20662. #ifdef DUMP_INVALID_PACKET
  20663. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20664. #endif
  20665. set_eof(fd);
  20666. return 0;
  20667. }
  20668. }
  20669. if ((int)RFIFOREST(fd) < packet_len){
  20670. 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 );
  20671. #ifdef DUMP_INVALID_PACKET
  20672. ShowDump( RFIFOP( fd, 0 ), RFIFOREST( fd ) );
  20673. #endif
  20674. set_eof( fd );
  20675. return 0; // not enough data received to form the packet
  20676. }
  20677. #ifdef PACKET_OBFUSCATION
  20678. RFIFOW(fd, 0) = cmd;
  20679. if (sd)
  20680. sd->cryptKey = ((sd->cryptKey * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF; // Update key for the next packet
  20681. #endif
  20682. if( packet_db[cmd].func == clif_parse_debug )
  20683. packet_db[cmd].func(fd, sd);
  20684. else if( packet_db[cmd].func != nullptr ) {
  20685. if( !sd && packet_db[cmd].func != clif_parse_WantToConnection )
  20686. ; //Only valid packet when there is no session
  20687. else
  20688. if( sd && sd->bl.prev == nullptr && packet_db[cmd].func != clif_parse_LoadEndAck )
  20689. ; //Only valid packet when player is not on a map
  20690. else
  20691. packet_db[cmd].func(fd, sd);
  20692. }
  20693. #ifdef DUMP_UNKNOWN_PACKET
  20694. else DumpUnknown(fd,sd,cmd,packet_len);
  20695. #endif
  20696. RFIFOSKIP(fd, packet_len);
  20697. }; // main loop end
  20698. return 0;
  20699. }
  20700. void packetdb_addpacket( uint16 cmd, uint16 length, void (*func)(int, map_session_data *), ... ){
  20701. va_list argp;
  20702. int i;
  20703. if(cmd <= 0 || cmd > MAX_PACKET_DB)
  20704. return;
  20705. packet_db[cmd].len = length;
  20706. packet_db[cmd].func = func;
  20707. va_start(argp, func);
  20708. for( i = 0; i < MAX_PACKET_POS; i++ ){
  20709. int offset = va_arg(argp, int);
  20710. if( offset == 0 ){
  20711. break;
  20712. }
  20713. packet_db[cmd].pos[i] = offset;
  20714. }
  20715. if( i == MAX_PACKET_POS ){
  20716. ShowError( "Too many positions found for packet 0x%04x (max=%d).\n", cmd, MAX_PACKET_POS );
  20717. }
  20718. va_end(argp);
  20719. }
  20720. /*==========================================
  20721. * Reads packets and setups its array reference
  20722. *------------------------------------------*/
  20723. void packetdb_readdb(){
  20724. memset(packet_db,0,sizeof(packet_db));
  20725. #include "clif_packetdb.hpp"
  20726. #include "clif_shuffle.hpp"
  20727. ShowStatus("Using packet version: " CL_WHITE "%d" CL_RESET ".\n", PACKETVER);
  20728. #ifdef PACKET_OBFUSCATION
  20729. 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]);
  20730. #else
  20731. ShowStatus("Packet Obfuscation: " CL_RED "Disabled" CL_RESET ".\n");
  20732. #endif
  20733. }
  20734. /*==========================================
  20735. *
  20736. *------------------------------------------*/
  20737. void do_init_clif(void) {
  20738. const int colors[COLOR_MAX] = {
  20739. 0x00FF00, // COLOR_DEFAULT
  20740. 0xFF0000, // COLOR_RED
  20741. 0xFFFFFF, // COLOR_WHITE
  20742. 0xFFFF00, // COLOR_YELLOW
  20743. 0x00FFFF, // COLOR_CYAN
  20744. 0xB5FFB5, // COLOR_LIGHT_GREEN
  20745. 0xFFFF63, // COLOR_LIGHT_YELLOW
  20746. };
  20747. /**
  20748. * Setup Color Table (saves unnecessary load of strtoul on every call)
  20749. **/
  20750. for( int i = 0; i < COLOR_MAX; i++ ){
  20751. color_table[i] = ( colors[i] & 0x0000FF ) << 16 | ( colors[i] & 0x00FF00 ) | ( colors[i] & 0xFF0000 ) >> 16; //RGB to BGR
  20752. }
  20753. packetdb_readdb();
  20754. set_defaultparse(clif_parse);
  20755. if( make_listen_bind(bind_ip,map_port) == -1 ) {
  20756. ShowFatalError("Failed to bind to port '" CL_WHITE "%d" CL_RESET "'\n",map_port);
  20757. exit(EXIT_FAILURE);
  20758. }
  20759. add_timer_func_list(clif_clearunit_delayed_sub, "clif_clearunit_delayed_sub");
  20760. add_timer_func_list(clif_delayquit, "clif_delayquit");
  20761. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320
  20762. add_timer_func_list( clif_ping_timer, "clif_ping_timer" );
  20763. if( battle_config.ping_timer_interval > 0 ){
  20764. add_timer_interval( gettick() + battle_config.ping_timer_interval * 1000, clif_ping_timer, 0, 0, battle_config.ping_timer_interval * 1000 );
  20765. }
  20766. #endif
  20767. delay_clearunit_ers = ers_new(sizeof(struct block_list),"clif.cpp::delay_clearunit_ers",ERS_OPT_CLEAR);
  20768. }
  20769. void do_final_clif(void) {
  20770. ers_destroy(delay_clearunit_ers);
  20771. }