clif.cpp 749 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // Copyright (c) Hercules Dev Team - Licensed under GNU GPL
  3. // For more information, see LICENCE in the main folder
  4. #include "clif.hpp"
  5. #include <cstdarg>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <ctime>
  10. #include <unordered_set>
  11. #include <common/cbasetypes.hpp>
  12. #include <common/conf.hpp>
  13. #include <common/ers.hpp>
  14. #include <common/grfio.hpp>
  15. #include <common/malloc.hpp>
  16. #include <common/nullpo.hpp>
  17. #include <common/random.hpp>
  18. #include <common/showmsg.hpp>
  19. #include <common/socket.hpp>
  20. #include <common/strlib.hpp>
  21. #include <common/timer.hpp>
  22. #include <common/utilities.hpp>
  23. #include <common/utils.hpp>
  24. #include "achievement.hpp"
  25. #include "atcommand.hpp"
  26. #include "battle.hpp"
  27. #include "battleground.hpp"
  28. #include "cashshop.hpp"
  29. #include "channel.hpp"
  30. #include "chat.hpp"
  31. #include "chrif.hpp"
  32. #include "clan.hpp"
  33. #include "clif.hpp"
  34. #include "elemental.hpp"
  35. #include "guild.hpp"
  36. #include "homunculus.hpp"
  37. #include "instance.hpp"
  38. #include "intif.hpp"
  39. #include "itemdb.hpp"
  40. #include "log.hpp"
  41. #include "mail.hpp"
  42. #include "map.hpp"
  43. #include "mercenary.hpp"
  44. #include "mob.hpp"
  45. #include "npc.hpp"
  46. #include "party.hpp"
  47. #include "pc.hpp"
  48. #include "pc_groups.hpp"
  49. #include "pet.hpp"
  50. #include "quest.hpp"
  51. #include "script.hpp"
  52. #include "skill.hpp"
  53. #include "status.hpp"
  54. #include "storage.hpp"
  55. #include "unit.hpp"
  56. #include "vending.hpp"
  57. using namespace rathena;
  58. static inline uint32 client_tick( t_tick tick ){
  59. return (uint32)tick;
  60. }
  61. #if PACKETVER >= 20170830
  62. static inline int64 client_exp(t_exp exp) {
  63. return (int64)u64min(exp, MAX_EXP);
  64. }
  65. #else
  66. static inline int32 client_exp(t_exp exp) {
  67. return (int32)u64min(exp, MAX_EXP);
  68. }
  69. #endif
  70. /* for clif_clearunit_delayed */
  71. static struct eri *delay_clearunit_ers;
  72. struct s_packet_db packet_db[MAX_PACKET_DB + 1];
  73. unsigned long color_table[COLOR_MAX];
  74. #include "clif_obfuscation.hpp"
  75. static bool clif_session_isValid(map_session_data *sd);
  76. static void clif_loadConfirm( map_session_data *sd );
  77. static void clif_favorite_item( map_session_data& sd, uint16 index );
  78. #if PACKETVER >= 20150513
  79. enum mail_type {
  80. MAIL_TYPE_TEXT = 0x0,
  81. MAIL_TYPE_ZENY = 0x2,
  82. MAIL_TYPE_ITEM = 0x4,
  83. MAIL_TYPE_NPC = 0x8
  84. };
  85. #endif
  86. enum e_inventory_type{
  87. INVTYPE_INVENTORY = 0,
  88. INVTYPE_CART = 1,
  89. INVTYPE_STORAGE = 2,
  90. INVTYPE_GUILD_STORAGE = 3,
  91. };
  92. /** Converts item type to display it on client if necessary.
  93. * @param nameid: Item ID
  94. * @return item type. For IT_PETEGG will be displayed as IT_ARMOR. If Shadow Weapon of IT_SHADOWGEAR as IT_WEAPON and else as IT_ARMOR
  95. */
  96. static inline int32 itemtype(t_itemid nameid) {
  97. struct item_data* id = itemdb_search(nameid); //Use itemdb_search, so non-existance item will use dummy data and won't crash the server. bugreport:8468
  98. int32 type = id->type;
  99. if( type == IT_SHADOWGEAR ) {
  100. if( id->equip&EQP_SHADOW_WEAPON )
  101. return IT_WEAPON;
  102. else
  103. return IT_ARMOR;
  104. }
  105. return ( type == IT_PETEGG ) ? IT_ARMOR : type;
  106. }
  107. // TODO: doc
  108. static inline uint16 client_index( uint16 server_index ){
  109. return server_index + 2;
  110. }
  111. static inline uint16 server_index( uint16 client_index ){
  112. return client_index - 2;
  113. }
  114. static inline uint16 client_storage_index( uint16 server_index ){
  115. return server_index + 1;
  116. }
  117. static inline uint16 server_storage_index( uint16 client_index ){
  118. return client_index - 1;
  119. }
  120. static inline uint32 disguised_bl_id( uint32 bl_id ){
  121. // Casting to prevent a compiler warning
  122. return -((int32)bl_id);
  123. }
  124. #if PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  125. static inline uint32 client_nameid( t_itemid server_nameid ){
  126. t_itemid view = itemdb_viewid( server_nameid );
  127. if( view > 0 ){
  128. return view;
  129. }else{
  130. return server_nameid;
  131. }
  132. }
  133. #else
  134. static inline uint16 client_nameid( t_itemid server_nameid ){
  135. t_itemid view = itemdb_viewid( server_nameid );
  136. if( view > 0 ){
  137. if( view > UINT16_MAX ){
  138. return (uint16)UNKNOWN_ITEM_ID;
  139. }
  140. return (uint16)view;
  141. }else{
  142. if( server_nameid > UINT16_MAX ){
  143. return (uint16)UNKNOWN_ITEM_ID;
  144. }
  145. return (uint16)server_nameid;
  146. }
  147. }
  148. #endif
  149. static inline void WBUFPOS(uint8* p, 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(int32 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(int32 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(int32 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. int32 map_fd;
  224. static int32 clif_parse (int32 fd);
  225. /*==========================================
  226. * Ip setting of map-server
  227. *------------------------------------------*/
  228. int32 clif_setip(const char* ip) {
  229. char ip_str[16];
  230. map_ip = host2ip(ip);
  231. if (!map_ip) {
  232. ShowWarning("Failed to Resolve Map Server Address! (%s)\n", ip);
  233. return 0;
  234. }
  235. safestrncpy(map_ip_str, ip, sizeof(map_ip_str));
  236. ShowInfo("Map Server IP Address : '" CL_WHITE "%s" CL_RESET "' -> '" CL_WHITE "%s" CL_RESET "'.\n", ip, ip2str(map_ip, ip_str));
  237. return 1;
  238. }
  239. void clif_setbindip(const char* ip)
  240. {
  241. bind_ip = host2ip(ip);
  242. if (bind_ip) {
  243. char ip_str[16];
  244. ShowInfo("Map Server Bind IP Address : '" CL_WHITE "%s" CL_RESET "' -> '" CL_WHITE "%s" CL_RESET "'.\n", ip, ip2str(bind_ip, ip_str));
  245. } else {
  246. ShowWarning("Failed to Resolve Map Server Address! (%s)\n", ip);
  247. }
  248. }
  249. /*==========================================
  250. * Sets map port to 'port'
  251. * is run from map.cpp upon loading map server configuration
  252. *------------------------------------------*/
  253. void clif_setport(uint16 port)
  254. {
  255. map_port = port;
  256. }
  257. /*==========================================
  258. * Returns map server IP
  259. *------------------------------------------*/
  260. uint32 clif_getip(void)
  261. {
  262. return map_ip;
  263. }
  264. //Refreshes map_server ip, returns the new ip if the ip changed, otherwise it returns 0.
  265. uint32 clif_refresh_ip(void)
  266. {
  267. uint32 new_ip;
  268. new_ip = host2ip(map_ip_str);
  269. if (new_ip && new_ip != map_ip) {
  270. map_ip = new_ip;
  271. ShowInfo("Updating IP resolution of [%s].\n", map_ip_str);
  272. return map_ip;
  273. }
  274. return 0;
  275. }
  276. /*==========================================
  277. * Returns map port which is set by clif_setport()
  278. *------------------------------------------*/
  279. uint16 clif_getport(void)
  280. {
  281. return map_port;
  282. }
  283. #if PACKETVER >= 20071106
  284. static inline unsigned char clif_bl_type(struct block_list *bl, bool walking) {
  285. switch (bl->type) {
  286. case BL_PC: return (disguised(bl) && !pcdb_checkid(status_get_viewdata(bl)->class_))? 0x1:0x0; //PC_TYPE
  287. case BL_ITEM: return 0x2; //ITEM_TYPE
  288. case BL_SKILL: return 0x3; //SKILL_TYPE
  289. case BL_CHAT: return 0x4; //UNKNOWN_TYPE
  290. case BL_MOB:
  291. if( pcdb_checkid( status_get_viewdata( bl )->class_ ) ){
  292. return 0x0; //PC_TYPE
  293. }else{
  294. switch( ( (mob_data*)bl )->special_state.ai ){
  295. case AI_ABR:
  296. return 0xd; //NPC_ABR_TYPE
  297. case AI_BIONIC:
  298. return 0xe; //NPC_BIONIC_TYPE
  299. default:
  300. return 0x5; //NPC_MOB_TYPE
  301. }
  302. }
  303. case BL_NPC:
  304. // From 2017-07-26 on NPC type units can also use player sprites.
  305. // There is one exception and this is if they are walking.
  306. // Since walking NPCs are not supported on official servers, the client does not know how to handle it.
  307. #if PACKETVER >= 20170726
  308. if (pcdb_checkid( status_get_viewdata( bl )->class_ ) && walking)
  309. return 0x0;
  310. else if (mobdb_checkid( status_get_viewdata( bl )->class_ )) // FIXME: categorize NPCs able to walk
  311. return 0xC;
  312. else
  313. return 0x6;
  314. #else
  315. return pcdb_checkid(status_get_viewdata(bl)->class_) ? 0x0 : 0x6; //NPC_EVT_TYPE
  316. #endif
  317. case BL_PET: return pcdb_checkid(status_get_viewdata(bl)->class_)?0x0:0x7; //NPC_PET_TYPE
  318. case BL_HOM: return 0x8; //NPC_HOM_TYPE
  319. case BL_MER: return 0x9; //NPC_MERSOL_TYPE
  320. case BL_ELEM: return 0xa; //NPC_ELEMENTAL_TYPE
  321. default: return 0x1; //NPC_TYPE
  322. }
  323. }
  324. #endif
  325. static bool clif_session_isValid(map_session_data *sd) {
  326. return ( sd != nullptr && session_isActive(sd->fd) );
  327. }
  328. /*==========================================
  329. * sub process of clif_send
  330. * Called from a map_foreachinallarea (grabs all players in specific area and subjects them to this function)
  331. * In order to send area-wise packets, such as:
  332. * - AREA : everyone nearby your area
  333. * - AREA_WOSC (AREA WITHOUT SAME CHAT) : Not run for people in the same chat as yours
  334. * - AREA_WOC (AREA WITHOUT CHAT) : Not run for people inside a chat
  335. * - AREA_WOS (AREA WITHOUT SELF) : Not run for self
  336. * - AREA_CHAT_WOC : Everyone in the area of your chat without a chat
  337. *------------------------------------------*/
  338. static int32 clif_send_sub(struct block_list *bl, va_list ap)
  339. {
  340. struct block_list *src_bl;
  341. map_session_data *sd;
  342. unsigned char *buf;
  343. int32 len, type, fd;
  344. nullpo_ret(bl);
  345. nullpo_ret(sd = (map_session_data *)bl);
  346. // Don't send to disconnected clients.
  347. if( !session_isActive( fd = sd->fd ) ){
  348. return 0;
  349. }
  350. buf = va_arg(ap,unsigned char*);
  351. len = va_arg(ap,int32);
  352. nullpo_ret(src_bl = va_arg(ap,struct block_list*));
  353. type = va_arg(ap,int32);
  354. switch(type) {
  355. case AREA_WOS:
  356. if (bl == src_bl)
  357. return 0;
  358. break;
  359. case AREA_WOC:
  360. if (sd->chatID || bl == src_bl)
  361. return 0;
  362. break;
  363. case AREA_WOSC:
  364. {
  365. if(src_bl->type == BL_PC) {
  366. map_session_data *ssd = (map_session_data *)src_bl;
  367. if (ssd && sd->chatID && (sd->chatID == ssd->chatID))
  368. return 0;
  369. }
  370. else if(src_bl->type == BL_NPC) {
  371. struct npc_data *nd = (struct npc_data *)src_bl;
  372. if (nd && sd->chatID && (sd->chatID == nd->chat_id))
  373. return 0;
  374. }
  375. }
  376. break;
  377. }
  378. if( src_bl->type == BL_NPC && npc_is_hidden_dynamicnpc( *( (struct npc_data*)src_bl ), *sd ) ){
  379. // Do not send anything
  380. return 0;
  381. }
  382. /* unless visible, hold it here */
  383. if (!battle_config.update_enemy_position && clif_ally_only && !sd->special_state.intravision &&
  384. !sd->sc.getSCE(SC_INTRAVISION) && battle_check_target(src_bl,&sd->bl,BCT_ENEMY) > 0)
  385. return 0;
  386. WFIFOHEAD(fd, len);
  387. if (WFIFOP(fd,0) == buf) {
  388. ShowError("WARNING: Invalid use of clif_send function\n");
  389. ShowError(" Packet x%4x use a WFIFO of a player instead of to use a buffer.\n", WBUFW(buf,0));
  390. ShowError(" Please correct your code.\n");
  391. // don't send to not move the pointer of the packet for next sessions in the loop
  392. //WFIFOSET(fd,0);//## TODO is this ok?
  393. //NO. It is not ok. There is the chance WFIFOSET actually sends the buffer data, and shifts elements around, which will corrupt the buffer.
  394. return 0;
  395. }
  396. memcpy(WFIFOP(fd,0), buf, len);
  397. WFIFOSET(fd,len);
  398. return 0;
  399. }
  400. /*==========================================
  401. * Packet Delegation (called on all packets that require data to be sent to more than one client)
  402. * functions that are sent solely to one use whose ID it posses use WFIFOSET
  403. *------------------------------------------*/
  404. int32 clif_send(const void* buf, int32 len, struct block_list* bl, enum send_target type)
  405. {
  406. int32 i;
  407. map_session_data *sd, *tsd;
  408. struct party_data *p = nullptr;
  409. std::shared_ptr<s_battleground_data> bg;
  410. int32 x0 = 0, x1 = 0, y0 = 0, y1 = 0, fd;
  411. struct s_mapiterator* iter;
  412. if( type != ALL_CLIENT )
  413. nullpo_ret(bl);
  414. sd = BL_CAST(BL_PC, bl);
  415. switch(type) {
  416. case ALL_CLIENT: //All player clients.
  417. iter = mapit_getallusers();
  418. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  419. if( session_isActive( fd = tsd->fd ) ){
  420. WFIFOHEAD( fd, len );
  421. memcpy( WFIFOP( fd, 0 ), buf, len );
  422. WFIFOSET( fd, len );
  423. }
  424. }
  425. mapit_free(iter);
  426. break;
  427. case ALL_SAMEMAP: //All players on the same map
  428. iter = mapit_getallusers();
  429. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  430. if( bl->m == tsd->bl.m && session_isActive( fd = tsd->fd ) ){
  431. WFIFOHEAD( fd, len );
  432. memcpy( WFIFOP( fd, 0 ), buf, len );
  433. WFIFOSET( fd, len );
  434. }
  435. }
  436. mapit_free(iter);
  437. break;
  438. case AREA:
  439. case AREA_WOSC:
  440. if (sd && bl->prev == nullptr) //Otherwise source misses the packet.[Skotlex]
  441. clif_send (buf, len, bl, SELF);
  442. [[fallthrough]];
  443. case AREA_WOC:
  444. case AREA_WOS:
  445. map_foreachinallarea(clif_send_sub, bl->m, bl->x-AREA_SIZE, bl->y-AREA_SIZE, bl->x+AREA_SIZE, bl->y+AREA_SIZE,
  446. BL_PC, buf, len, bl, type);
  447. break;
  448. case AREA_CHAT_WOC:
  449. map_foreachinallarea(clif_send_sub, bl->m, bl->x-(AREA_SIZE-5), bl->y-(AREA_SIZE-5),
  450. bl->x+(AREA_SIZE-5), bl->y+(AREA_SIZE-5), BL_PC, buf, len, bl, AREA_WOC);
  451. break;
  452. case CHAT:
  453. case CHAT_WOS:
  454. {
  455. struct chat_data *cd;
  456. if (sd) {
  457. cd = (struct chat_data*)map_id2bl(sd->chatID);
  458. } else if (bl->type == BL_CHAT) {
  459. cd = (struct chat_data*)bl;
  460. } else break;
  461. if (cd == nullptr)
  462. break;
  463. for(i = 0; i < cd->users; i++) {
  464. if (type == CHAT_WOS && cd->usersd[i] == sd)
  465. continue;
  466. if( session_isActive( fd = cd->usersd[i]->fd ) ){
  467. WFIFOHEAD(fd,len);
  468. memcpy(WFIFOP(fd,0), buf, len);
  469. WFIFOSET(fd,len);
  470. }
  471. }
  472. }
  473. break;
  474. case PARTY_AREA:
  475. case PARTY_AREA_WOS:
  476. x0 = bl->x - AREA_SIZE;
  477. y0 = bl->y - AREA_SIZE;
  478. x1 = bl->x + AREA_SIZE;
  479. y1 = bl->y + AREA_SIZE;
  480. [[fallthrough]];
  481. case PARTY:
  482. case PARTY_WOS:
  483. case PARTY_SAMEMAP:
  484. case PARTY_SAMEMAP_WOS:
  485. if (sd && sd->status.party_id)
  486. p = party_search(sd->status.party_id);
  487. if (p) {
  488. for(i=0;i<MAX_PARTY;i++){
  489. if( (sd = p->data[i].sd) == nullptr )
  490. continue;
  491. if( !session_isActive( fd = sd->fd ) )
  492. continue;
  493. if( sd->bl.id == bl->id && (type == PARTY_WOS || type == PARTY_SAMEMAP_WOS || type == PARTY_AREA_WOS) )
  494. continue;
  495. if( type != PARTY && type != PARTY_WOS && bl->m != sd->bl.m )
  496. continue;
  497. if( (type == PARTY_AREA || type == PARTY_AREA_WOS) && (sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1) )
  498. continue;
  499. WFIFOHEAD(fd, len);
  500. memcpy(WFIFOP(fd, 0), buf, len);
  501. WFIFOSET(fd, len);
  502. }
  503. if (!enable_spy) //Skip unnecessary parsing. [Skotlex]
  504. break;
  505. iter = mapit_getallusers();
  506. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  507. if( tsd->partyspy == p->party.party_id && session_isActive( fd = tsd->fd ) ){
  508. WFIFOHEAD( fd, len );
  509. memcpy( WFIFOP( fd, 0 ), buf, len );
  510. WFIFOSET( tsd->fd, len );
  511. }
  512. }
  513. mapit_free(iter);
  514. }
  515. break;
  516. case DUEL:
  517. case DUEL_WOS:
  518. if (!sd || !sd->duel_group) break; //Invalid usage.
  519. iter = mapit_getallusers();
  520. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  521. if( type == DUEL_WOS && bl->id == tsd->bl.id )
  522. continue;
  523. if( sd->duel_group == tsd->duel_group && session_isActive( fd = tsd->fd ) ){
  524. WFIFOHEAD( fd, len );
  525. memcpy( WFIFOP( fd, 0 ), buf, len );
  526. WFIFOSET( fd, len );
  527. }
  528. }
  529. mapit_free(iter);
  530. break;
  531. case SELF:
  532. if( clif_session_isValid(sd) ){
  533. fd = sd->fd;
  534. WFIFOHEAD(fd,len);
  535. memcpy(WFIFOP(fd,0), buf, len);
  536. WFIFOSET(fd,len);
  537. }
  538. break;
  539. // New definitions for guilds [Valaris] - Cleaned up and reorganized by [Skotlex]
  540. case GUILD_AREA:
  541. case GUILD_AREA_WOS:
  542. x0 = bl->x - AREA_SIZE;
  543. y0 = bl->y - AREA_SIZE;
  544. x1 = bl->x + AREA_SIZE;
  545. y1 = bl->y + AREA_SIZE;
  546. [[fallthrough]];
  547. case GUILD_SAMEMAP:
  548. case GUILD_SAMEMAP_WOS:
  549. case GUILD:
  550. case GUILD_WOS:
  551. case GUILD_NOBG: {
  552. if (!sd || !sd->status.guild_id || !sd->guild)
  553. break;
  554. const auto &g = sd->guild->guild;
  555. for(i = 0; i < g.max_member; i++) {
  556. if( (sd = g.member[i].sd) != nullptr ){
  557. if( !session_isActive( fd = sd->fd ) )
  558. continue;
  559. if( type == GUILD_NOBG && sd->bg_id )
  560. continue;
  561. if( sd->bl.id == bl->id && (type == GUILD_WOS || type == GUILD_SAMEMAP_WOS || type == GUILD_AREA_WOS) )
  562. continue;
  563. if( type != GUILD && type != GUILD_NOBG && type != GUILD_WOS && sd->bl.m != bl->m )
  564. continue;
  565. if( (type == GUILD_AREA || type == GUILD_AREA_WOS) && (sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1) )
  566. continue;
  567. WFIFOHEAD(fd,len);
  568. memcpy(WFIFOP(fd,0), buf, len);
  569. WFIFOSET(fd,len);
  570. }
  571. }
  572. if (!enable_spy) //Skip unnecessary parsing. [Skotlex]
  573. break;
  574. iter = mapit_getallusers();
  575. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  576. if( tsd->guildspy == g.guild_id && session_isActive( fd = tsd->fd ) ){
  577. WFIFOHEAD( fd, len );
  578. memcpy( WFIFOP( fd, 0 ), buf, len );
  579. WFIFOSET( fd, len );
  580. }
  581. }
  582. mapit_free(iter);
  583. break;
  584. }
  585. case BG_AREA:
  586. case BG_AREA_WOS:
  587. x0 = bl->x - AREA_SIZE;
  588. y0 = bl->y - AREA_SIZE;
  589. x1 = bl->x + AREA_SIZE;
  590. y1 = bl->y + AREA_SIZE;
  591. [[fallthrough]];
  592. case BG_SAMEMAP:
  593. case BG_SAMEMAP_WOS:
  594. case BG:
  595. case BG_WOS:
  596. if( sd && sd->bg_id > 0 && (bg = util::umap_find(bg_team_db, sd->bg_id)))
  597. {
  598. for (const auto &member : bg->members) {
  599. if( ( sd = member.sd ) == nullptr || !session_isActive( fd = sd->fd ) )
  600. continue;
  601. if(sd->bl.id == bl->id && (type == BG_WOS || type == BG_SAMEMAP_WOS || type == BG_AREA_WOS) )
  602. continue;
  603. if( type != BG && type != BG_WOS && sd->bl.m != bl->m )
  604. continue;
  605. if( (type == BG_AREA || type == BG_AREA_WOS) && (sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1) )
  606. continue;
  607. WFIFOHEAD(fd,len);
  608. memcpy(WFIFOP(fd,0), buf, len);
  609. WFIFOSET(fd,len);
  610. }
  611. }
  612. break;
  613. case CLAN:
  614. if( sd && sd->clan ){
  615. struct clan* clan = sd->clan;
  616. for( i = 0; i < clan->max_member; i++ ){
  617. if( ( sd = clan->members[i] ) == nullptr || !session_isActive( fd = sd->fd ) ){
  618. continue;
  619. }
  620. WFIFOHEAD(fd,len);
  621. memcpy(WFIFOP(fd,0), buf, len);
  622. WFIFOSET(fd,len);
  623. }
  624. if (!enable_spy) //Skip unnecessary parsing. [Skotlex]
  625. break;
  626. iter = mapit_getallusers();
  627. while( ( tsd = (map_session_data*)mapit_next( iter ) ) != nullptr ){
  628. if( tsd->clanspy == clan->id && session_isActive( fd = tsd->fd ) ){
  629. WFIFOHEAD(fd, len);
  630. memcpy(WFIFOP(fd, 0), buf, len);
  631. WFIFOSET(fd, len);
  632. }
  633. }
  634. mapit_free(iter);
  635. }
  636. break;
  637. default:
  638. ShowError("clif_send: Unrecognized type %d\n",type);
  639. return -1;
  640. }
  641. return 0;
  642. }
  643. /// Notifies the client, that it's connection attempt was accepted.
  644. /// 0073 <start time>.L <position>.3B <x size>.B <y size>.B (ZC_ACCEPT_ENTER)
  645. /// 02eb <start time>.L <position>.3B <x size>.B <y size>.B <font>.W (ZC_ACCEPT_ENTER2)
  646. /// 0a18 <start time>.L <position>.3B <x size>.B <y size>.B <font>.W <sex>.B (ZC_ACCEPT_ENTER3)
  647. void clif_authok(map_session_data *sd)
  648. {
  649. PACKET_ZC_ACCEPT_ENTER packet{};
  650. packet.packetType = HEADER_ZC_ACCEPT_ENTER;
  651. packet.startTime = client_tick(gettick());
  652. WBUFPOS(packet.posDir, 0, sd->bl.x, sd->bl.y, sd->ud.dir);
  653. packet.xSize = 5; // ignored
  654. packet.ySize = 5; // ignored
  655. #if PACKETVER >= 20080102
  656. packet.font = sd->status.font;
  657. #endif
  658. #if PACKETVER >= 20141016 && PACKETVER < 20160330
  659. packet.sex = sd->status.sex;
  660. #endif
  661. clif_send(&packet, sizeof(packet), &sd->bl, SELF);
  662. }
  663. /// Notifies the client, that it's connection attempt was refused.
  664. /// 0074 <error code>.B (ZC_REFUSE_ENTER)
  665. /// error code:
  666. /// 0 = client type mismatch
  667. /// 1 = ID mismatch
  668. /// 2 = mobile - out of available time
  669. /// 3 = mobile - already logged in
  670. /// 4 = mobile - waiting state
  671. void clif_authrefuse(int32 fd, uint8 error_code)
  672. {
  673. PACKET_ZC_REFUSE_ENTER packet{};
  674. packet.packetType = HEADER_ZC_REFUSE_ENTER;
  675. packet.errorCode = error_code;
  676. socket_send<PACKET_ZC_REFUSE_ENTER>(fd, packet);
  677. }
  678. /// Notifies the client of a ban or forced disconnect.
  679. /// 0081 <error code>.B (SC_NOTIFY_BAN)
  680. /// error code:
  681. /// 0 = BAN_UNFAIR
  682. /// 1 = server closed -> MsgStringTable[4]
  683. /// 2 = ID already logged in -> MsgStringTable[5]
  684. /// 3 = timeout/too much lag -> MsgStringTable[241]
  685. /// 4 = server full -> MsgStringTable[264]
  686. /// 5 = underaged -> MsgStringTable[305]
  687. /// 8 = Server sill recognizes last connection -> MsgStringTable[441]
  688. /// 9 = too many connections from this ip -> MsgStringTable[529]
  689. /// 10 = out of available time paid for -> MsgStringTable[530]
  690. /// 11 = BAN_PAY_SUSPEND
  691. /// 12 = BAN_PAY_CHANGE
  692. /// 13 = BAN_PAY_WRONGIP
  693. /// 14 = BAN_PAY_PNGAMEROOM
  694. /// 15 = disconnected by a GM -> if( servicetype == taiwan ) MsgStringTable[579]
  695. /// 16 = BAN_JAPAN_REFUSE1
  696. /// 17 = BAN_JAPAN_REFUSE2
  697. /// 18 = BAN_INFORMATION_REMAINED_ANOTHER_ACCOUNT
  698. /// 100 = BAN_PC_IP_UNFAIR
  699. /// 101 = BAN_PC_IP_COUNT_ALL
  700. /// 102 = BAN_PC_IP_COUNT
  701. /// 103 = BAN_GRAVITY_MEM_AGREE
  702. /// 104 = BAN_GAME_MEM_AGREE
  703. /// 105 = BAN_HAN_VALID
  704. /// 106 = BAN_PC_IP_LIMIT_ACCESS
  705. /// 107 = BAN_OVER_CHARACTER_LIST
  706. /// 108 = BAN_IP_BLOCK
  707. /// 109 = BAN_INVALID_PWD_CNT
  708. /// 110 = BAN_NOT_ALLOWED_JOBCLASS
  709. /// ? = disconnected -> MsgStringTable[3]
  710. void clif_authfail_fd(int32 fd, int32 type)
  711. {
  712. if (!session_isValid(fd) || session[fd]->func_parse != clif_parse) //clif_authfail should only be invoked on players!
  713. return;
  714. PACKET_SC_NOTIFY_BAN packet{};
  715. packet.packetType = HEADER_SC_NOTIFY_BAN;
  716. packet.errorCode = static_cast<uint8>(type);
  717. socket_send<PACKET_SC_NOTIFY_BAN>(fd, packet);
  718. set_eof(fd);
  719. }
  720. /// Notifies the client, whether it can disconnect and change servers.
  721. /// 00b3 <type>.B (ZC_RESTART_ACK)
  722. /// type:
  723. /// 1 = disconnect, char-select
  724. /// ? = nothing
  725. void clif_charselectok(int32 id, uint8 ok)
  726. {
  727. map_session_data* sd = map_id2sd(id);
  728. if (sd == nullptr)
  729. return;
  730. PACKET_ZC_RESTART_ACK packet{};
  731. packet.packetType = HEADER_ZC_RESTART_ACK;
  732. packet.type = ok;
  733. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  734. }
  735. /// Makes an item appear on the ground.
  736. /// 009E <id>.L <name id>.W <identified>.B <x>.W <y>.W <subX>.B <subY>.B <amount>.W (ZC_ITEM_FALL_ENTRY)
  737. /// 084B <id>.L <name id>.W <type>.W <identified>.B <x>.W <y>.W <subX>.B <subY>.B <amount>.W (ZC_ITEM_FALL_ENTRY4)
  738. /// 0ADD <id>.L <name id>.W <type>.W <identified>.B <x>.W <y>.W <subX>.B <subY>.B <amount>.W <show drop effect>.B <drop effect mode>.W (ZC_ITEM_FALL_ENTRY5)
  739. void clif_dropflooritem( struct flooritem_data* fitem, bool canShowEffect ){
  740. nullpo_retv(fitem);
  741. if( fitem->item.nameid == 0 ){
  742. return;
  743. }
  744. struct packet_dropflooritem p;
  745. p.PacketType = dropflooritemType;
  746. p.ITAID = fitem->bl.id;
  747. p.ITID = client_nameid( fitem->item.nameid );
  748. #if PACKETVER >= 20130000 /* not sure date */
  749. p.type = itemtype( fitem->item.nameid );
  750. #endif
  751. p.IsIdentified = fitem->item.identify ? 1 : 0;
  752. p.xPos = fitem->bl.x;
  753. p.yPos = fitem->bl.y;
  754. p.subX = fitem->subx;
  755. p.subY = fitem->suby;
  756. p.count = fitem->item.amount;
  757. #if defined(PACKETVER_ZERO) || PACKETVER >= 20180418
  758. if( canShowEffect ){
  759. uint8 dropEffect = itemdb_dropeffect( fitem->item.nameid );
  760. if( dropEffect > 0 ){
  761. p.showdropeffect = 1;
  762. p.dropeffectmode = dropEffect - 1;
  763. }else if (battle_config.rndopt_drop_pillar != 0){
  764. uint8 optionCount = 0;
  765. for (uint8 i = 0; i < MAX_ITEM_RDM_OPT; i++) {
  766. if (fitem->item.option[i].id != 0) {
  767. optionCount++;
  768. }
  769. }
  770. if (optionCount > 0) {
  771. p.showdropeffect = 1;
  772. if (optionCount == 1)
  773. p.dropeffectmode = DROPEFFECT_BLUE_PILLAR - 1;
  774. else if (optionCount == 2)
  775. p.dropeffectmode = DROPEFFECT_YELLOW_PILLAR - 1;
  776. else
  777. p.dropeffectmode = DROPEFFECT_PURPLE_PILLAR - 1;
  778. } else {
  779. p.showdropeffect = 0;
  780. p.dropeffectmode = DROPEFFECT_NONE;
  781. }
  782. } else {
  783. p.showdropeffect = 0;
  784. p.dropeffectmode = DROPEFFECT_NONE;
  785. }
  786. }else{
  787. p.showdropeffect = 0;
  788. p.dropeffectmode = DROPEFFECT_NONE;
  789. }
  790. #endif
  791. clif_send( &p, sizeof(p), &fitem->bl, AREA );
  792. }
  793. /// Makes an item disappear from the ground.
  794. /// 00a1 <id>.L (ZC_ITEM_DISAPPEAR)
  795. void clif_clearflooritem( flooritem_data& fitem, map_session_data* tsd ){
  796. PACKET_ZC_ITEM_DISAPPEAR packet{};
  797. packet.packetType = HEADER_ZC_ITEM_DISAPPEAR;
  798. packet.itemAid = fitem.bl.id;
  799. if( tsd == nullptr ){
  800. clif_send(&packet, sizeof(PACKET_ZC_ITEM_DISAPPEAR), &fitem.bl, AREA);
  801. } else {
  802. clif_send(&packet, sizeof(PACKET_ZC_ITEM_DISAPPEAR), &tsd->bl, SELF );
  803. }
  804. }
  805. /// Makes a unit (char, npc, mob, homun) disappear to one client.
  806. /// 0080 <id>.L <type>.B (ZC_NOTIFY_VANISH)
  807. /// type:
  808. /// 0 = out of sight
  809. /// 1 = died
  810. /// 2 = logged out
  811. /// 3 = teleport
  812. /// 4 = trickdead
  813. void clif_clearunit_single( uint32 GID, clr_type type, map_session_data& tsd ){
  814. PACKET_ZC_NOTIFY_VANISH packet{};
  815. packet.packetType = HEADER_ZC_NOTIFY_VANISH;
  816. packet.gid = GID;
  817. packet.type = static_cast<decltype(packet.type)>(type);
  818. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  819. }
  820. /// Makes a unit (char, npc, mob, homun) disappear to all clients in area.
  821. /// 0080 <id>.L <type>.B (ZC_NOTIFY_VANISH)
  822. /// type:
  823. /// 0 = out of sight
  824. /// 1 = died
  825. /// 2 = logged out
  826. /// 3 = teleport
  827. /// 4 = trickdead
  828. void clif_clearunit_area( block_list& bl, clr_type type ){
  829. PACKET_ZC_NOTIFY_VANISH packet{};
  830. packet.packetType = HEADER_ZC_NOTIFY_VANISH;
  831. packet.gid = bl.id;
  832. packet.type = static_cast<uint8>(type);
  833. clif_send(&packet, sizeof(PACKET_ZC_NOTIFY_VANISH), &bl, type == CLR_DEAD ? AREA : AREA_WOS);
  834. if(disguised(&bl)) {
  835. packet.gid = disguised_bl_id( bl.id );
  836. clif_send(&packet, sizeof(PACKET_ZC_NOTIFY_VANISH), &bl, SELF);
  837. }
  838. }
  839. /// Used to make monsters with player-sprites disappear after dying
  840. /// like normal monsters, because the client does not remove those
  841. /// automatically.
  842. static TIMER_FUNC(clif_clearunit_delayed_sub){
  843. struct block_list *bl = (struct block_list *)data;
  844. if( bl != nullptr ){
  845. clif_clearunit_area( *bl, (clr_type)id );
  846. ers_free( delay_clearunit_ers, bl );
  847. }
  848. return 0;
  849. }
  850. void clif_clearunit_delayed(struct block_list* bl, clr_type type, t_tick tick)
  851. {
  852. struct block_list *tbl = ers_alloc(delay_clearunit_ers, struct block_list);
  853. tbl->next = nullptr;
  854. tbl->prev = nullptr;
  855. tbl->id = bl->id;
  856. tbl->m = bl->m;
  857. tbl->x = bl->x;
  858. tbl->y = bl->y;
  859. tbl->type = BL_NUL;
  860. add_timer(tick, clif_clearunit_delayed_sub, (int32)type, (intptr_t)tbl);
  861. }
  862. void clif_get_weapon_view(map_session_data* sd, t_itemid *rhand, t_itemid *lhand)
  863. {
  864. if(sd->sc.option&OPTION_COSTUME)
  865. {
  866. *rhand = *lhand = 0;
  867. return;
  868. }
  869. #if PACKETVER < 4
  870. *rhand = sd->status.weapon;
  871. *lhand = sd->status.shield;
  872. #else
  873. if (sd->equip_index[EQI_HAND_R] >= 0 &&
  874. sd->inventory_data[sd->equip_index[EQI_HAND_R]])
  875. {
  876. struct item_data* id = sd->inventory_data[sd->equip_index[EQI_HAND_R]];
  877. if (id->view_id > 0)
  878. *rhand = id->view_id;
  879. else
  880. *rhand = id->nameid;
  881. } else
  882. *rhand = 0;
  883. if (sd->equip_index[EQI_HAND_L] >= 0 &&
  884. sd->equip_index[EQI_HAND_L] != sd->equip_index[EQI_HAND_R] &&
  885. sd->inventory_data[sd->equip_index[EQI_HAND_L]])
  886. {
  887. struct item_data* id = sd->inventory_data[sd->equip_index[EQI_HAND_L]];
  888. if (id->view_id > 0)
  889. *lhand = id->view_id;
  890. else
  891. *lhand = id->nameid;
  892. } else
  893. *lhand = 0;
  894. #endif
  895. }
  896. //To make the assignation of the level based on limits clearer/easier. [Skotlex]
  897. static int32 clif_setlevel_sub(int32 lv) {
  898. if( lv < battle_config.max_lv ) {
  899. ;
  900. } else if( lv < battle_config.aura_lv ) {
  901. lv = battle_config.max_lv - 1;
  902. } else {
  903. lv = battle_config.max_lv;
  904. }
  905. return lv;
  906. }
  907. static int32 clif_setlevel(struct block_list* bl) {
  908. int32 lv = status_get_lv(bl);
  909. if( battle_config.client_limit_unit_lv&bl->type )
  910. return clif_setlevel_sub(lv);
  911. switch( bl->type ) {
  912. case BL_NPC:
  913. case BL_PET:
  914. // npcs and pets do not have level
  915. return 0;
  916. }
  917. return lv;
  918. }
  919. /*==========================================
  920. * Prepares 'unit standing/spawning' packet
  921. *------------------------------------------*/
  922. static void clif_set_unit_idle( struct block_list* bl, bool walking, send_target target, struct block_list* tbl ){
  923. nullpo_retv( bl );
  924. map_session_data* sd = BL_CAST( BL_PC, bl );
  925. status_change* sc = status_get_sc( bl );
  926. struct view_data* vd = status_get_viewdata( bl );
  927. int32 g_id = status_get_guild_id( bl );
  928. #if PACKETVER < 20091103
  929. if( !pcdb_checkid( vd->class_ ) ){
  930. struct packet_idle_unit2 p;
  931. p.PacketType = idle_unit2Type;
  932. #if PACKETVER >= 20071106
  933. p.objecttype = clif_bl_type( bl, walking );
  934. #endif
  935. p.GID = bl->id;
  936. p.speed = status_get_speed( bl );
  937. p.bodyState = ( sc ) ? sc->opt1 : 0;
  938. p.healthState = ( sc ) ? sc->opt2 : 0;
  939. p.effectState = ( sc ) ? sc->option : 0;
  940. p.job = vd->class_;
  941. p.head = vd->hair_style;
  942. p.weapon = vd->weapon;
  943. p.accessory = vd->head_bottom;
  944. if( bl->type == BL_NPC && vd->class_ == JT_GUILD_FLAG ){
  945. // The hell, why flags work like this?
  946. p.shield = status_get_emblem_id( bl );
  947. p.accessory2 = GetWord( g_id, 1 );
  948. p.accessory3 = GetWord( g_id, 0 );
  949. }else{
  950. p.shield = vd->shield;
  951. p.accessory2 = vd->head_top;
  952. p.accessory3 = vd->head_mid;
  953. }
  954. p.headpalette = vd->hair_color;
  955. p.bodypalette = vd->cloth_color;
  956. p.headDir = ( sd )? sd->head_dir : 0;
  957. p.GUID = g_id;
  958. p.GEmblemVer = status_get_emblem_id( bl );
  959. p.honor = ( sd ) ? sd->status.manner : 0;
  960. p.virtue = ( sc ) ? sc->opt3 : 0;
  961. p.isPKModeON = ( sd && sd->status.karma ) ? 1 : 0;
  962. p.sex = vd->sex;
  963. WBUFPOS( &p.PosDir[0], 0, bl->x, bl->y, unit_getdir( bl ) );
  964. p.xSize = p.ySize = ( sd ) ? 5 : 0;
  965. p.state = vd->dead_sit;
  966. p.clevel = clif_setlevel( bl );
  967. clif_send( &p, sizeof( p ), tbl, target );
  968. return;
  969. }
  970. #endif
  971. struct packet_idle_unit p;
  972. p.PacketType = idle_unitType;
  973. #if PACKETVER >= 20091103
  974. p.PacketLength = sizeof(p);
  975. p.objecttype = clif_bl_type( bl, walking );
  976. #endif
  977. #if PACKETVER >= 20131223
  978. p.AID = bl->id;
  979. p.GID = (sd) ? sd->status.char_id : 0; // CCODE
  980. #else
  981. p.GID = bl->id;
  982. #endif
  983. p.speed = status_get_speed(bl);
  984. p.bodyState = (sc) ? sc->opt1 : 0;
  985. p.healthState = (sc) ? sc->opt2 : 0;
  986. // npc option changed?
  987. if( tbl && tbl->type == BL_PC && bl->type == BL_NPC ){
  988. map_session_data* sd = (map_session_data*)tbl;
  989. struct npc_data* nd = (struct npc_data*)bl;
  990. int32 option = (sc) ? sc->option : 0;
  991. if( !nd->vd.dead_sit ){
  992. if( std::find( sd->cloaked_npc.begin(), sd->cloaked_npc.end(), nd->bl.id ) != sd->cloaked_npc.end() ){
  993. option ^= OPTION_CLOAK;
  994. }
  995. }
  996. p.effectState = option;
  997. }else{
  998. p.effectState = (sc) ? sc->option : 0;
  999. }
  1000. p.job = vd->class_;
  1001. p.head = vd->hair_style;
  1002. p.weapon = vd->weapon;
  1003. #if PACKETVER < 7 || PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  1004. p.shield = vd->shield;
  1005. #endif
  1006. if( bl->type == BL_NPC && vd->class_ == JT_GUILD_FLAG ){
  1007. // The hell, why flags work like this?
  1008. p.accessory = status_get_emblem_id( bl );
  1009. p.accessory2 = GetWord( g_id, 1 );
  1010. p.accessory3 = GetWord( g_id, 0 );
  1011. }else{
  1012. p.accessory = vd->head_bottom;
  1013. p.accessory2 = vd->head_top;
  1014. p.accessory3 = vd->head_mid;
  1015. }
  1016. p.headpalette = vd->hair_color;
  1017. p.bodypalette = vd->cloth_color;
  1018. p.headDir = (sd)? sd->head_dir : 0;
  1019. #if PACKETVER >= 20101124
  1020. p.robe = vd->robe;
  1021. #endif
  1022. p.GUID = g_id;
  1023. p.GEmblemVer = status_get_emblem_id( bl );
  1024. p.honor = (sd) ? sd->status.manner : 0;
  1025. p.virtue = (sc) ? sc->opt3 : 0;
  1026. p.isPKModeON = (sd && sd->status.karma) ? 1 : 0;
  1027. p.sex = vd->sex;
  1028. WBUFPOS( &p.PosDir[0], 0, bl->x, bl->y, unit_getdir( bl ) );
  1029. p.xSize = p.ySize = (sd) ? 5 : 0;
  1030. p.state = vd->dead_sit;
  1031. p.clevel = clif_setlevel( bl );
  1032. #if PACKETVER >= 20080102
  1033. p.font = (sd) ? sd->status.font : 0;
  1034. #endif
  1035. #if PACKETVER >= 20120221
  1036. if( battle_config.monster_hp_bars_info && !map_getmapflag( bl->m, MF_HIDEMOBHPBAR ) && bl->type == BL_MOB && ( status_get_hp( bl ) < status_get_max_hp( bl ) ) ){
  1037. p.maxHP = status_get_max_hp(bl);
  1038. p.HP = status_get_hp(bl);
  1039. }else{
  1040. p.maxHP = -1;
  1041. p.HP = -1;
  1042. }
  1043. if( bl->type == BL_MOB ){
  1044. p.isBoss = ( (mob_data*)bl )->get_bosstype();
  1045. }else if( bl->type == BL_PET ){
  1046. p.isBoss = ( (pet_data*)bl )->db->get_bosstype();
  1047. }else{
  1048. p.isBoss = BOSSTYPE_NONE;
  1049. }
  1050. #endif
  1051. #if PACKETVER >= 20150513
  1052. p.body = vd->body_style;
  1053. #endif
  1054. /* Might be earlier, this is when the named item bug began */
  1055. #if PACKETVER >= 20131223
  1056. safestrncpy(p.name, status_get_name( bl ), NAME_LENGTH);
  1057. #endif
  1058. clif_send( &p, sizeof( p ), tbl, target );
  1059. // if disguised, send to self
  1060. if( disguised( bl ) ){
  1061. #if PACKETVER >= 20091103
  1062. p.objecttype = pcdb_checkid( status_get_viewdata( bl )->class_ ) ? 0x0 : 0x5; //PC_TYPE : NPC_MOB_TYPE
  1063. #if PACKETVER >= 20131223
  1064. p.AID = disguised_bl_id( bl->id );
  1065. #else
  1066. p.GID = disguised_bl_id( bl->id );
  1067. #endif
  1068. #else
  1069. p.GID = disguised_bl_id( bl->id );
  1070. #endif
  1071. clif_send(&p, sizeof(p), bl, SELF);
  1072. }
  1073. }
  1074. static void clif_spawn_unit( struct block_list *bl, enum send_target target ){
  1075. nullpo_retv( bl );
  1076. map_session_data* sd = BL_CAST( BL_PC, bl );
  1077. status_change* sc = status_get_sc( bl );
  1078. struct view_data* vd = status_get_viewdata( bl );
  1079. int32 g_id = status_get_guild_id( bl );
  1080. #if PACKETVER < 20091103
  1081. if( !pcdb_checkid( vd->class_ ) ){
  1082. struct packet_spawn_unit2 p;
  1083. p.PacketType = spawn_unit2Type;
  1084. #if PACKETVER >= 20071106
  1085. p.objecttype = clif_bl_type( bl, false );
  1086. #endif
  1087. p.GID = bl->id;
  1088. p.speed = status_get_speed( bl );
  1089. p.bodyState = ( sc ) ? sc->opt1 : 0;
  1090. p.healthState = ( sc ) ? sc->opt2 : 0;
  1091. p.effectState = ( sc ) ? sc->option : 0;
  1092. p.head = vd->hair_style;
  1093. p.weapon = vd->weapon;
  1094. p.accessory = vd->head_bottom;
  1095. p.job = vd->class_;
  1096. if( bl->type == BL_NPC && vd->class_ == JT_GUILD_FLAG ){
  1097. // The hell, why flags work like this?
  1098. p.shield = status_get_emblem_id( bl );
  1099. p.accessory2 = GetWord( g_id, 1 );
  1100. p.accessory3 = GetWord( g_id, 0 );
  1101. }else{
  1102. p.shield = vd->shield;
  1103. p.accessory2 = vd->head_top;
  1104. p.accessory3 = vd->head_mid;
  1105. }
  1106. p.headpalette = vd->hair_color;
  1107. p.bodypalette = vd->cloth_color;
  1108. p.headDir = ( sd ) ? sd->head_dir : 0;
  1109. p.isPKModeON = ( sd && sd->status.karma ) ? 1 : 0;
  1110. p.sex = vd->sex;
  1111. WBUFPOS( &p.PosDir[0], 0, bl->x, bl->y, unit_getdir( bl ) );
  1112. p.xSize = p.ySize = ( sd ) ? 5 : 0;
  1113. clif_send( &p, sizeof( p ), bl, target );
  1114. return;
  1115. }
  1116. #endif
  1117. struct packet_spawn_unit p;
  1118. p.PacketType = spawn_unitType;
  1119. #if PACKETVER >= 20091103
  1120. p.PacketLength = sizeof(p);
  1121. p.objecttype = clif_bl_type( bl, false );
  1122. #endif
  1123. #if PACKETVER >= 20131223
  1124. p.AID = bl->id;
  1125. p.GID = (sd) ? sd->status.char_id : 0; // CCODE
  1126. #else
  1127. p.GID = bl->id;
  1128. #endif
  1129. p.speed = status_get_speed( bl );
  1130. p.bodyState = (sc) ? sc->opt1 : 0;
  1131. p.healthState = (sc) ? sc->opt2 : 0;
  1132. p.effectState = (sc) ? sc->option : 0;
  1133. p.job = vd->class_;
  1134. p.head = vd->hair_style;
  1135. p.weapon = vd->weapon;
  1136. #if PACKETVER < 7 || PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  1137. p.shield = vd->shield;
  1138. #endif
  1139. if( bl->type == BL_NPC && vd->class_ == JT_GUILD_FLAG ){
  1140. // The hell, why flags work like this?
  1141. p.accessory = status_get_emblem_id( bl );
  1142. p.accessory2 = GetWord( g_id, 1 );
  1143. p.accessory3 = GetWord( g_id, 0 );
  1144. }else{
  1145. p.accessory = vd->head_bottom;
  1146. p.accessory2 = vd->head_top;
  1147. p.accessory3 = vd->head_mid;
  1148. }
  1149. p.headpalette = vd->hair_color;
  1150. p.bodypalette = vd->cloth_color;
  1151. p.headDir = (sd)? sd->head_dir : 0;
  1152. #if PACKETVER >= 20101124
  1153. p.robe = vd->robe;
  1154. #endif
  1155. p.GUID = g_id;
  1156. p.GEmblemVer = status_get_emblem_id( bl );
  1157. p.honor = (sd) ? sd->status.manner : 0;
  1158. p.virtue = (sc) ? sc->opt3 : 0;
  1159. p.isPKModeON = (sd && sd->status.karma) ? 1 : 0;
  1160. p.sex = vd->sex;
  1161. WBUFPOS( &p.PosDir[0], 0, bl->x, bl->y, unit_getdir( bl ) );
  1162. p.xSize = p.ySize = (sd) ? 5 : 0;
  1163. p.clevel = clif_setlevel( bl );
  1164. #if PACKETVER >= 20080102
  1165. p.font = (sd) ? sd->status.font : 0;
  1166. #endif
  1167. #if PACKETVER >= 20120221
  1168. if( battle_config.monster_hp_bars_info && bl->type == BL_MOB && !map_getmapflag( bl->m, MF_HIDEMOBHPBAR ) && ( status_get_hp( bl ) < status_get_max_hp( bl ) ) ){
  1169. p.maxHP = status_get_max_hp( bl );
  1170. p.HP = status_get_hp( bl );
  1171. }else{
  1172. p.maxHP = -1;
  1173. p.HP = -1;
  1174. }
  1175. if( bl->type == BL_MOB ){
  1176. p.isBoss = ( (mob_data*)bl )->get_bosstype();
  1177. }else if( bl->type == BL_PET ){
  1178. p.isBoss = ( (pet_data*)bl )->db->get_bosstype();
  1179. }else{
  1180. p.isBoss = BOSSTYPE_NONE;
  1181. }
  1182. #endif
  1183. #if PACKETVER >= 20150513
  1184. p.body = vd->body_style;
  1185. #endif
  1186. /* Might be earlier, this is when the named item bug began */
  1187. #if PACKETVER >= 20131223
  1188. safestrncpy( p.name, status_get_name( bl ), NAME_LENGTH );
  1189. #endif
  1190. if( disguised( bl ) ){
  1191. nullpo_retv( sd );
  1192. if( sd->status.class_ != sd->disguise ){
  1193. clif_send( &p, sizeof( p ), bl, target );
  1194. }
  1195. #if PACKETVER >= 20091103
  1196. p.objecttype = pcdb_checkid( status_get_viewdata(bl)->class_ ) ? 0x0 : 0x5; //PC_TYPE : NPC_MOB_TYPE
  1197. #if PACKETVER >= 20131223
  1198. p.AID = disguised_bl_id( bl->id );
  1199. #else
  1200. p.GID = disguised_bl_id( bl->id );
  1201. #endif
  1202. #else
  1203. p.GID = disguised_bl_id( bl->id );
  1204. #endif
  1205. clif_send( &p, sizeof( p ), bl, SELF );
  1206. }else{
  1207. clif_send( &p, sizeof( p ), bl, target );
  1208. }
  1209. }
  1210. /*==========================================
  1211. * Prepares 'unit walking' packet
  1212. *------------------------------------------*/
  1213. static void clif_set_unit_walking( struct block_list& bl, map_session_data* tsd, struct unit_data& ud, enum send_target target ){
  1214. struct packet_unit_walking p;
  1215. p.PacketType = unit_walkingType;
  1216. #if PACKETVER >= 20091103
  1217. p.PacketLength = sizeof(p);
  1218. #endif
  1219. #if PACKETVER >= 20071106
  1220. p.objecttype = clif_bl_type( &bl, true );
  1221. #endif
  1222. map_session_data* sd = BL_CAST(BL_PC, &bl);
  1223. #if PACKETVER >= 20131223
  1224. p.AID = bl.id;
  1225. p.GID = (sd) ? sd->status.char_id : 0; // CCODE
  1226. #else
  1227. p.GID = bl.id;
  1228. #endif
  1229. p.speed = status_get_speed( &bl );
  1230. status_change* sc = status_get_sc( &bl );
  1231. p.bodyState = (sc) ? sc->opt1 : 0;
  1232. p.healthState = (sc) ? sc->opt2 : 0;
  1233. p.effectState = (sc) ? sc->option : 0;
  1234. struct view_data* vd = status_get_viewdata( &bl );
  1235. p.job = vd->class_;
  1236. p.head = vd->hair_style;
  1237. p.weapon = vd->weapon;
  1238. p.accessory = vd->head_bottom;
  1239. p.moveStartTime = client_tick(gettick());
  1240. #if PACKETVER < 7 || PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  1241. p.shield = vd->shield;
  1242. #endif
  1243. p.accessory2 = vd->head_top;
  1244. p.accessory3 = vd->head_mid;
  1245. p.headpalette = vd->hair_color;
  1246. p.bodypalette = vd->cloth_color;
  1247. p.headDir = (sd) ? sd->head_dir : 0;
  1248. #if PACKETVER >= 20101124
  1249. p.robe = vd->robe;
  1250. #endif
  1251. p.GUID = status_get_guild_id( &bl );
  1252. p.GEmblemVer = status_get_emblem_id( &bl );
  1253. p.honor = (sd) ? sd->status.manner : 0;
  1254. p.virtue = (sc) ? sc->opt3 : 0;
  1255. p.isPKModeON = (sd && sd->status.karma) ? 1 : 0;
  1256. p.sex = vd->sex;
  1257. WBUFPOS2(&p.MoveData[0], 0, bl.x, bl.y, ud.to_x, ud.to_y, ud.sx, ud.sy);
  1258. p.xSize = p.ySize = (sd) ? 5 : 0;
  1259. p.clevel = clif_setlevel( &bl );
  1260. #if PACKETVER >= 20080102
  1261. p.font = (sd) ? sd->status.font : 0;
  1262. #endif
  1263. #if PACKETVER >= 20120221
  1264. if( battle_config.monster_hp_bars_info && !map_getmapflag(bl.m, MF_HIDEMOBHPBAR) && bl.type == BL_MOB && (status_get_hp( &bl ) < status_get_max_hp( &bl ) ) ){
  1265. p.maxHP = status_get_max_hp( &bl );
  1266. p.HP = status_get_hp( &bl );
  1267. } else {
  1268. p.maxHP = -1;
  1269. p.HP = -1;
  1270. }
  1271. if( bl.type == BL_MOB ){
  1272. p.isBoss = reinterpret_cast<mob_data*>( &bl )->get_bosstype();
  1273. }else if( bl.type == BL_PET ){
  1274. p.isBoss = reinterpret_cast<pet_data*>( &bl )->db->get_bosstype();
  1275. }else{
  1276. p.isBoss = BOSSTYPE_NONE;
  1277. }
  1278. #endif
  1279. #if PACKETVER >= 20150513
  1280. p.body = vd->body_style;
  1281. #endif
  1282. /* Might be earlier, this is when the named item bug began */
  1283. #if PACKETVER >= 20131223
  1284. safestrncpy(p.name, status_get_name( &bl ), NAME_LENGTH);
  1285. #endif
  1286. clif_send( &p, sizeof(p), tsd ? &tsd->bl : &bl, target );
  1287. // if disguised, send the info to self
  1288. if( disguised( &bl ) ){
  1289. #if PACKETVER >= 20091103
  1290. p.objecttype = pcdb_checkid( status_get_viewdata( &bl )->class_ ) ? 0x0 : 0x5; //PC_TYPE : NPC_MOB_TYPE
  1291. #if PACKETVER >= 20131223
  1292. p.AID = disguised_bl_id( bl.id );
  1293. #else
  1294. p.GID = disguised_bl_id( bl.id );
  1295. #endif
  1296. #else
  1297. p.GID = disguised_bl_id( bl.id );
  1298. #endif
  1299. clif_send(&p, sizeof(p), &bl, SELF);
  1300. }
  1301. }
  1302. /// Changes sprite of a non player object.
  1303. /// 01b0 <id>.L <type>.B <value>.L (ZC_NPCSPRITE_CHANGE)
  1304. void clif_class_change( block_list& bl, int32 class_, enum send_target target, map_session_data* sd ){
  1305. if( pcdb_checkid( class_ ) ){
  1306. // player classes yield missing sprites
  1307. return;
  1308. }
  1309. PACKET_ZC_NPCSPRITE_CHANGE p = {};
  1310. p.packetType = HEADER_ZC_NPCSPRITE_CHANGE;
  1311. p.GID = bl.id;
  1312. // Unused
  1313. p.type = 0;
  1314. p.class_ = class_;
  1315. if( sd != nullptr ){
  1316. bl = sd->bl;
  1317. }
  1318. clif_send( &p, sizeof( p ), &bl, target );
  1319. }
  1320. void clif_servantball( map_session_data& sd, struct block_list* target, enum send_target send_target ){
  1321. PACKET_ZC_SPIRITS p = {};
  1322. p.PacketType = HEADER_ZC_SPIRITS;
  1323. p.AID = sd.bl.id;
  1324. p.num = sd.servantball;
  1325. if( target == nullptr ){
  1326. target = &sd.bl;
  1327. }
  1328. clif_send( &p, sizeof( p ), target, send_target );
  1329. }
  1330. void clif_abyssball( map_session_data& sd, struct block_list* target, enum send_target send_target ){
  1331. PACKET_ZC_SPIRITS p = {};
  1332. p.PacketType = HEADER_ZC_SPIRITS;
  1333. p.AID = sd.bl.id;
  1334. p.num = sd.abyssball;
  1335. if( target == nullptr ){
  1336. target = &sd.bl;
  1337. }
  1338. clif_send( &p, sizeof( p ), target, send_target );
  1339. }
  1340. /// Notifies the client of an object's Millenium Shields.
  1341. static void clif_millenniumshield_single( map_session_data& sd, map_session_data& tsd ){
  1342. #if PACKETVER >= 20081126
  1343. status_change_entry* sce = sd.sc.getSCE( SC_MILLENNIUMSHIELD );
  1344. if( sce == nullptr ){
  1345. return;
  1346. }
  1347. PACKET_ZC_MILLENNIUMSHIELD packet{};
  1348. packet.packetType = HEADER_ZC_MILLENNIUMSHIELD;
  1349. packet.aid = sd.bl.id;
  1350. packet.num = sce->val2;
  1351. packet.state = 0;
  1352. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  1353. #endif
  1354. }
  1355. /*==========================================
  1356. * Kagerou/Oboro amulet spirit
  1357. *------------------------------------------*/
  1358. static void clif_spiritcharm_single( map_session_data& sd, map_session_data& tsd ){
  1359. #if PACKETVER >= 20111102
  1360. if( sd.spiritcharm_type == CHARM_TYPE_NONE ){
  1361. return;
  1362. }
  1363. if( sd.spiritcharm <= 0 ){
  1364. return;
  1365. }
  1366. PACKET_ZC_SPIRITS_ATTRIBUTE packet{};
  1367. packet.packetType = HEADER_ZC_SPIRITS_ATTRIBUTE;
  1368. packet.aid = sd.bl.id;
  1369. packet.spiritsType = static_cast<decltype(packet.spiritsType)>(sd.spiritcharm_type);
  1370. packet.num = static_cast<decltype(packet.num)>(sd.spiritcharm);
  1371. clif_send( &packet, sizeof( packet ), &tsd.bl, SELF );
  1372. #endif
  1373. }
  1374. /*==========================================
  1375. * Enchanting Shadow / Shadow Scar Spirit
  1376. *------------------------------------------*/
  1377. void clif_enchantingshadow_spirit(unit_data &ud) {
  1378. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191120 || PACKETVER_ZERO_NUM >= 20191127
  1379. PACKET_ZC_TARGET_SPIRITS p = {};
  1380. p.packetType = HEADER_ZC_TARGET_SPIRITS;
  1381. p.GID = ud.bl->id;
  1382. p.unknown_val = 0;
  1383. p.amount = static_cast<uint16>(ud.shadow_scar_timer.size());
  1384. clif_send(&p, sizeof(p), ud.bl, AREA);
  1385. #endif
  1386. }
  1387. /*==========================================
  1388. * Run when player changes map / refreshes
  1389. * Tells its client to display all weather settings being used by this map
  1390. *------------------------------------------*/
  1391. static void clif_weather_check(map_session_data *sd)
  1392. {
  1393. int16 m = sd->bl.m;
  1394. int32 fd = sd->fd;
  1395. if (map_getmapflag(m, MF_SNOW)
  1396. || map_getmapflag(m, MF_CLOUDS)
  1397. || map_getmapflag(m, MF_FOG)
  1398. || map_getmapflag(m, MF_FIREWORKS)
  1399. || map_getmapflag(m, MF_SAKURA)
  1400. || map_getmapflag(m, MF_LEAVES)
  1401. || map_getmapflag(m, MF_CLOUDS2))
  1402. {
  1403. if (map_getmapflag(m, MF_SNOW))
  1404. clif_specialeffect_single(&sd->bl, EF_SNOW, fd);
  1405. if (map_getmapflag(m, MF_CLOUDS))
  1406. clif_specialeffect_single(&sd->bl, EF_CLOUD3, fd);
  1407. if (map_getmapflag(m, MF_CLOUDS2))
  1408. clif_specialeffect_single(&sd->bl, EF_CLOUD5, fd);
  1409. if (map_getmapflag(m, MF_FOG))
  1410. clif_specialeffect_single(&sd->bl, EF_CLOUD4, fd);
  1411. if (map_getmapflag(m, MF_FIREWORKS)) {
  1412. clif_specialeffect_single(&sd->bl, EF_POKJUK, fd);
  1413. clif_specialeffect_single(&sd->bl, EF_THROWITEM2, fd);
  1414. clif_specialeffect_single(&sd->bl, EF_POKJUK_SOUND, fd);
  1415. }
  1416. if (map_getmapflag(m, MF_SAKURA))
  1417. clif_specialeffect_single(&sd->bl, EF_SAKURA, fd);
  1418. if (map_getmapflag(m, MF_LEAVES))
  1419. clif_specialeffect_single(&sd->bl, EF_MAPLE, fd);
  1420. }
  1421. }
  1422. /**
  1423. * Run when the weather on a map changes, throws all players in map id 'm' to clif_weather_check function
  1424. **/
  1425. void clif_weather(int16 m)
  1426. {
  1427. struct s_mapiterator* iter;
  1428. map_session_data *sd=nullptr;
  1429. iter = mapit_getallusers();
  1430. for( sd = (map_session_data*)mapit_first(iter); mapit_exists(iter); sd = (map_session_data*)mapit_next(iter) )
  1431. {
  1432. if( sd->bl.m == m )
  1433. clif_weather_check(sd);
  1434. }
  1435. mapit_free(iter);
  1436. }
  1437. /**
  1438. * Hide a NPC from the effects of Maya Purple card.
  1439. * @param bl: Block data
  1440. * @return True if NPC is disabled or false otherwise
  1441. */
  1442. static inline bool clif_npc_mayapurple(block_list *bl) {
  1443. nullpo_retr(false, bl);
  1444. if (bl->type == BL_NPC) {
  1445. npc_data *nd = map_id2nd(bl->id);
  1446. // TODO: Confirm if waitingroom cause any special cases
  1447. if (/* nd->chat_id == 0 && */ nd->is_invisible)
  1448. return true;
  1449. }
  1450. return false;
  1451. }
  1452. /**
  1453. * Main function to spawn a unit on the client (player/mob/pet/etc)
  1454. **/
  1455. int32 clif_spawn( struct block_list *bl, bool walking ){
  1456. struct view_data *vd;
  1457. vd = status_get_viewdata(bl);
  1458. if( !vd || vd->class_ == JT_INVISIBLE )
  1459. return 0;
  1460. // Hide NPC from Maya Purple card
  1461. if (clif_npc_mayapurple(bl))
  1462. return 0;
  1463. if( bl->type == BL_NPC && !vd->dead_sit ){
  1464. clif_set_unit_idle( bl, walking, AREA_WOS, bl );
  1465. }else{
  1466. clif_spawn_unit( bl, AREA_WOS );
  1467. }
  1468. if (vd->cloth_color)
  1469. clif_refreshlook(bl,bl->id,LOOK_CLOTHES_COLOR,vd->cloth_color,AREA_WOS);
  1470. if (vd->body_style)
  1471. clif_refreshlook(bl,bl->id,LOOK_BODY2,vd->body_style,AREA_WOS);
  1472. switch (bl->type)
  1473. {
  1474. case BL_PC:
  1475. {
  1476. TBL_PC *sd = ((TBL_PC*)bl);
  1477. if (sd->spiritball > 0)
  1478. clif_spiritball(&sd->bl);
  1479. if (sd->sc.getSCE(SC_MILLENNIUMSHIELD))
  1480. clif_millenniumshield( sd->bl, sd->sc.getSCE( SC_MILLENNIUMSHIELD )->val2 );
  1481. if (sd->soulball > 0)
  1482. clif_soulball(sd);
  1483. if (sd->servantball > 0)
  1484. clif_servantball( *sd );
  1485. if (sd->abyssball > 0)
  1486. clif_abyssball( *sd );
  1487. if(sd->state.size==SZ_BIG) // tiny/big players [Valaris]
  1488. clif_specialeffect(bl,EF_GIANTBODY2,AREA);
  1489. else if(sd->state.size==SZ_MEDIUM)
  1490. clif_specialeffect(bl,EF_BABYBODY2,AREA);
  1491. if( sd->bg_id && map_getmapflag(sd->bl.m, MF_BATTLEGROUND) )
  1492. clif_sendbgemblem_area(sd);
  1493. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  1494. clif_spiritcharm( *sd );
  1495. if (sd->status.robe)
  1496. clif_refreshlook(bl,bl->id,LOOK_ROBE,sd->status.robe,AREA);
  1497. clif_efst_status_change_sub(bl, bl, AREA);
  1498. clif_hat_effects( *sd, sd->bl, AREA );
  1499. }
  1500. break;
  1501. case BL_MOB:
  1502. {
  1503. TBL_MOB *md = ((TBL_MOB*)bl);
  1504. if(md->special_state.size==SZ_BIG) // tiny/big mobs [Valaris]
  1505. clif_specialeffect(&md->bl,EF_GIANTBODY2,AREA);
  1506. else if(md->special_state.size==SZ_MEDIUM)
  1507. clif_specialeffect(&md->bl,EF_BABYBODY2,AREA);
  1508. if ( md->special_state.ai == AI_ABR || md->special_state.ai == AI_BIONIC )
  1509. clif_summon_init(*md);
  1510. }
  1511. break;
  1512. case BL_NPC:
  1513. {
  1514. TBL_NPC *nd = ((TBL_NPC*)bl);
  1515. if( nd->size == SZ_BIG )
  1516. clif_specialeffect(&nd->bl,EF_GIANTBODY2,AREA);
  1517. else if( nd->size == SZ_MEDIUM )
  1518. clif_specialeffect(&nd->bl,EF_BABYBODY2,AREA);
  1519. clif_efst_status_change_sub(bl, bl, AREA);
  1520. clif_progressbar_npc_area(nd);
  1521. }
  1522. break;
  1523. case BL_PET:
  1524. if (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, int32 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(int32 i = 0, count = 0; i < MAX_HOMUNSKILL; i++) {
  1723. int32 id = hd.homunculus.hskill[i].id;
  1724. if (id != 0) {
  1725. int32 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(int32 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( int32 i = 0, count = 0, discount = npc_shop_discount( &nd ); i < nd.u.shop.count; i++ ){
  1928. int32 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( int32 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. int32 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(int32 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( int32 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(int32 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( int32 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(int32 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. int32 count = ( p->PacketLength - sizeof( *p ) ) / sizeof( p->list[0] );
  2078. std::vector<s_npc_buy_list> items;
  2079. items.reserve( count );
  2080. for( int32 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, int32 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, int32 type, uint16 x, uint16 y, int32 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, int32 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. int32 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( int32 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( int32 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, int32 n, int32 amount, unsigned char fail ){
  2411. nullpo_retv( sd );
  2412. int32 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, int32 index, int32 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, int32 index, int32 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, int32 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. int32 strLen = (int32)safestrnlen(name, 24) + 1;
  2567. if (strLen > 24)
  2568. strLen = 24;
  2569. const int32 len = sizeof(PACKET_ZC_INVENTORY_START) + strLen;
  2570. p->packetLength = len;
  2571. safestrncpy(p->name, name, strLen);
  2572. #else
  2573. const int32 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. int32 equip = 0;
  2601. int32 normal = 0;
  2602. for( int32 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( int32 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, int32 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. int32 equip = 0;
  2676. int32 normal = 0;
  2677. for( int32 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. int32 normal = 0;
  2745. int32 equip = 0;
  2746. for( int32 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(int32 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. int32 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. int32 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(int32 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(int32 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 int32 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 int32 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, int32 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, int32 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. int32 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. int32 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, int32 id, int32 type, int32 val, int32 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, int32 type, int32 val) {
  3335. map_session_data* sd;
  3336. status_change* sc;
  3337. struct view_data* vd;
  3338. enum send_target target = AREA;
  3339. int32 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. int32 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,int32 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, int32 id, int32 type, int32 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. int32 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. int32 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, int32 index, int32 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, int32 index, int32 amount, bool ok ){
  3765. nullpo_retv( sd );
  3766. int32 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. /// Calculates the chat status
  3801. /// type:
  3802. /// 0 = private (password protected)
  3803. /// 1 = public
  3804. /// 2 = arena (npc waiting room)
  3805. /// 3 = PK zone (non-clickable)
  3806. static e_status_chatroom clif_chat_status( const chat_data &cd ){
  3807. if (cd.owner->type == BL_PC)
  3808. return (cd.pub) ? STATUSROOM_PUBLIC : STATUSROOM_PRIVATE;
  3809. else if (cd.owner->type == BL_NPC)
  3810. return (cd.limit) ? STATUSROOM_ARENA : STATUSROOM_PK_ZONE;
  3811. else
  3812. return STATUSROOM_PUBLIC;
  3813. }
  3814. /// Calculates the chat user count
  3815. static uint16 clif_chat_usercount( const chat_data& cd ){
  3816. if( cd.owner->type == BL_NPC ){
  3817. return cd.users + 1;
  3818. }else{
  3819. return cd.users;
  3820. }
  3821. }
  3822. /// Display a chat above the owner.
  3823. /// 00d7 <packet len>.W <owner id>.L <char id>.L <limit>.W <users>.W <type>.B <title>.?B (ZC_ROOM_NEWENTRY)
  3824. void clif_dispchat( const chat_data& cd ){
  3825. if( cd.owner == nullptr ){
  3826. return;
  3827. }
  3828. PACKET_ZC_ROOM_NEWENTRY* p = reinterpret_cast<PACKET_ZC_ROOM_NEWENTRY*>( packet_buffer );
  3829. p->packetType = HEADER_ZC_ROOM_NEWENTRY;
  3830. p->packetSize = sizeof( *p );
  3831. p->owner = cd.owner->id;
  3832. p->id = cd.bl.id;
  3833. p->limit = cd.limit;
  3834. p->users = clif_chat_usercount( cd );
  3835. p->type = clif_chat_status( cd );
  3836. // not zero-terminated
  3837. size_t max = safestrnlen( cd.title, CHATROOM_TITLE_SIZE );
  3838. strncpy( p->title, cd.title, max );
  3839. p->packetSize += static_cast<decltype(p->packetSize)>( max );
  3840. clif_send( p, p->packetSize, cd.owner, AREA_WOSC );
  3841. }
  3842. /// Chatroom properties adjustment.
  3843. /// 00df <packet len>.W <owner id>.L <chat id>.L <limit>.W <users>.W <type>.B <title>.?B (ZC_CHANGE_CHATROOM)
  3844. void clif_changechatstatus(chat_data& cd) {
  3845. if( cd.owner == nullptr ){
  3846. return;
  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 = clif_chat_usercount( cd );
  3855. p->flag = clif_chat_status( cd );
  3856. // not zero-terminated
  3857. size_t max = safestrnlen( cd.title, CHATROOM_TITLE_SIZE );
  3858. strncpy( p->title, cd.title, max );
  3859. p->packetSize += static_cast<decltype(p->packetSize)>( max );
  3860. clif_send(p,p->packetSize,cd.owner,CHAT);
  3861. }
  3862. /// Removes the chatroom
  3863. /// 00d8 <chat id>.L (ZC_DESTROY_ROOM)
  3864. void clif_clearchat(chat_data &cd){
  3865. PACKET_ZC_DESTROY_ROOM p{};
  3866. p.packetType = HEADER_ZC_DESTROY_ROOM;
  3867. p.chatId = cd.bl.id;
  3868. clif_send(&p,sizeof(p),cd.owner,AREA_WOSC);
  3869. }
  3870. /// Displays messages regarding join chat failures.
  3871. /// 00da <result>.B (ZC_REFUSE_ENTER_ROOM)
  3872. /// result:
  3873. /// 0 = room full
  3874. /// 1 = wrong password
  3875. /// 2 = kicked
  3876. /// 3 = success (no message)
  3877. /// 4 = no enough zeny
  3878. /// 5 = too low level
  3879. /// 6 = too high level
  3880. /// 7 = unsuitable job class
  3881. void clif_joinchatfail( map_session_data& sd, e_refuse_enter_room result ){
  3882. PACKET_ZC_REFUSE_ENTER_ROOM packet{};
  3883. packet.packetType = HEADER_ZC_REFUSE_ENTER_ROOM;
  3884. packet.result = static_cast<decltype(packet.result)>(result);
  3885. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  3886. }
  3887. /// Notifies the client about entering a chatroom (ZC_ENTER_ROOM).
  3888. /// 00db <packet len>.W <chat id>.L { <role>.L <name>.24B }*
  3889. /// role:
  3890. /// 0 = owner (menu)
  3891. /// 1 = normal
  3892. void clif_joinchatok(map_session_data& sd, chat_data& cd){
  3893. PACKET_ZC_ENTER_ROOM* p = reinterpret_cast<PACKET_ZC_ENTER_ROOM*>( packet_buffer );
  3894. p->packetType = HEADER_ZC_ENTER_ROOM;
  3895. p->packetSize = sizeof(*p);
  3896. p->chatId = cd.bl.id;
  3897. if(cd.owner->type == BL_NPC){
  3898. PACKET_ZC_ENTER_ROOM_sub& owner = p->members[0];
  3899. owner.flag = 0;
  3900. safestrncpy(owner.name, reinterpret_cast<npc_data*>(cd.owner)->name, sizeof(owner.name));
  3901. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( owner ) );
  3902. for (size_t i = 0; i < cd.users; i++) {
  3903. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i + 1];
  3904. member.flag = 1;
  3905. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3906. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3907. }
  3908. }else{
  3909. for (size_t i = 0; i < cd.users; i++) {
  3910. PACKET_ZC_ENTER_ROOM_sub& member = p->members[i];
  3911. member.flag = i > 0;
  3912. safestrncpy(member.name, cd.usersd[i]->status.name, sizeof(member.name));
  3913. p->packetSize += static_cast<decltype(p->packetSize)>( sizeof( member ) );
  3914. }
  3915. }
  3916. clif_send(p,p->packetSize,&sd.bl,SELF);
  3917. }
  3918. /// Notifies clients in a chat about a new member.
  3919. /// 00dc <users>.W <name>.24B (ZC_MEMBER_NEWENTRY)
  3920. void clif_addchat( chat_data& cd, map_session_data& sd ){
  3921. PACKET_ZC_MEMBER_NEWENTRY p = {};
  3922. p.packetType = HEADER_ZC_MEMBER_NEWENTRY;
  3923. p.count = cd.users;
  3924. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  3925. clif_send( &p, sizeof( p ), &sd.bl, CHAT_WOS );
  3926. }
  3927. /// Announce the new owner.
  3928. /// 00e1 <role>.L <nick>.24B (ZC_ROLE_CHANGE)
  3929. /// role:
  3930. /// 0 = owner (menu)
  3931. /// 1 = normal
  3932. void clif_chat_role( chat_data& cd, map_session_data& sd ){
  3933. PACKET_ZC_ROLE_CHANGE p = {};
  3934. p.packetType = HEADER_ZC_ROLE_CHANGE;
  3935. p.flag = cd.usersd[0] != &sd;
  3936. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  3937. clif_send( &p, sizeof( p ), &sd.bl, CHAT );
  3938. }
  3939. /// Notify about user leaving the chatroom.
  3940. /// 00dd <users>.W <nick>.24B <flag>.B (ZC_MEMBER_EXIT)
  3941. /// flag:
  3942. /// 0 = left
  3943. /// 1 = kicked
  3944. void clif_chat_leave( chat_data& cd, map_session_data& sd, bool kicked ){
  3945. PACKET_ZC_MEMBER_EXIT p = {};
  3946. p.packetType = HEADER_ZC_MEMBER_EXIT;
  3947. p.count = cd.users - 1;
  3948. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  3949. p.kicked = kicked;
  3950. clif_send( &p, sizeof( p ), &sd.bl, CHAT );
  3951. }
  3952. /// Opens a trade request window from char 'name'.
  3953. /// 00e5 <nick>.24B (ZC_REQ_EXCHANGE_ITEM)
  3954. /// 01f4 <nick>.24B <targetid>.L <baselvl>.W (ZC_REQ_EXCHANGE_ITEM2)
  3955. void clif_traderequest(map_session_data& sd, const char* name){
  3956. PACKET_ZC_REQ_EXCHANGE_ITEM p{};
  3957. p.packetType = HEADER_ZC_REQ_EXCHANGE_ITEM;
  3958. safestrncpy(p.requesterName, name, sizeof(p.requesterName));
  3959. #if PACKETVER > 6
  3960. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  3961. p.targetLv = sd.trade_partner.lv;
  3962. #endif
  3963. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3964. }
  3965. /// Reply to a trade-request.
  3966. /// 00e7 <result>.B (ZC_ACK_EXCHANGE_ITEM)
  3967. /// 01f5 <result>.B <targetid>.L <baselvl>.W (ZC_ACK_EXCHANGE_ITEM2)
  3968. /// result:
  3969. /// 0 = Char is too far
  3970. /// 1 = Character does not exist
  3971. /// 2 = Trade failed
  3972. /// 3 = Accept
  3973. /// 4 = Cancel
  3974. /// 5 = Busy
  3975. void clif_traderesponse( map_session_data& sd, e_ack_trade_response result ){
  3976. PACKET_ZC_ACK_EXCHANGE_ITEM p{};
  3977. p.packetType = HEADER_ZC_ACK_EXCHANGE_ITEM;
  3978. p.result = static_cast<decltype(p.result)>( result );
  3979. #if PACKETVER > 6
  3980. p.targetId = sd.trade_partner.id; // Client generates a random char[5] with this info
  3981. p.targetLv = sd.trade_partner.lv;
  3982. #endif
  3983. clif_send(&p,sizeof(p),&sd.bl,SELF);
  3984. }
  3985. /// Notifies the client about an item from other player in current trade.
  3986. /// 00e9 <amount>.L <nameid>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W (ZC_ADD_EXCHANGE_ITEM)
  3987. /// 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)
  3988. /// 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)
  3989. void clif_tradeadditem( map_session_data* sd, map_session_data* tsd, int32 index, int32 amount ){
  3990. nullpo_retv( sd );
  3991. nullpo_retv( tsd );
  3992. int32 fd = tsd->fd;
  3993. if( !session_isActive( fd ) ){
  3994. return;
  3995. }
  3996. PACKET_ZC_ADD_EXCHANGE_ITEM p = {};
  3997. if( index ){
  3998. index = server_index( index );
  3999. if( index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0 || sd->inventory_data[index] == nullptr ){
  4000. return;
  4001. }
  4002. p.itemId = client_nameid( sd->inventory.u.items_inventory[index].nameid );
  4003. #if PACKETVER >= 20100223
  4004. p.itemType = sd->inventory_data[index]->type;
  4005. #endif
  4006. p.identified = sd->inventory.u.items_inventory[index].identify;
  4007. p.damaged = sd->inventory.u.items_inventory[index].attribute;
  4008. p.refine = sd->inventory.u.items_inventory[index].refine;
  4009. clif_addcards( &p.slot, &sd->inventory.u.items_inventory[index] );
  4010. #if PACKETVER >= 20150226
  4011. clif_add_random_options( p.option_data, sd->inventory.u.items_inventory[index] );
  4012. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  4013. p.location = pc_equippoint_sub( sd, sd->inventory_data[index] );
  4014. p.look = sd->inventory_data[index]->look;
  4015. p.grade = sd->inventory.u.items_inventory[index].enchantgrade;
  4016. #endif
  4017. #endif
  4018. }else{
  4019. p = {};
  4020. }
  4021. p.packetType = HEADER_ZC_ADD_EXCHANGE_ITEM;
  4022. p.amount = amount;
  4023. clif_send( &p, sizeof( p ), &tsd->bl, SELF );
  4024. }
  4025. /// Notifies the client about the result of request to add an item to the current trade (ZC_ACK_ADD_EXCHANGE_ITEM).
  4026. /// 00ea <index>.W <result>.B
  4027. /// result:
  4028. /// 0 = success
  4029. /// 1 = overweight
  4030. /// 2 = trade canceled
  4031. void clif_tradeitemok(map_session_data& sd, int32 index, e_exitem_add_result result)
  4032. {
  4033. PACKET_ZC_ACK_ADD_EXCHANGE_ITEM p = {};
  4034. p.packetType = HEADER_ZC_ACK_ADD_EXCHANGE_ITEM;
  4035. p.index = client_index(index);
  4036. p.result = static_cast<uint8>(result);
  4037. clif_send(&p, sizeof(p), &sd.bl, SELF);
  4038. }
  4039. /// Notifies the client about finishing one side of the current trade.
  4040. /// 00ec <who>.B (ZC_CONCLUDE_EXCHANGE_ITEM)
  4041. /// who:
  4042. /// 0 = self
  4043. /// 1 = other player
  4044. void clif_tradedeal_lock( map_session_data& sd, bool who ){
  4045. PACKET_ZC_CONCLUDE_EXCHANGE_ITEM packet{};
  4046. packet.packetType = HEADER_ZC_CONCLUDE_EXCHANGE_ITEM;
  4047. packet.who = who;
  4048. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4049. }
  4050. /// Notifies the client about the trade being canceled.
  4051. /// 00ee (ZC_CANCEL_EXCHANGE_ITEM)
  4052. void clif_tradecancelled( map_session_data& sd ){
  4053. PACKET_ZC_CANCEL_EXCHANGE_ITEM packet{};
  4054. packet.packetType = HEADER_ZC_CANCEL_EXCHANGE_ITEM;
  4055. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4056. }
  4057. /// Result of a trade.
  4058. /// 00f0 <result>.B (ZC_EXEC_EXCHANGE_ITEM)
  4059. /// result:
  4060. /// 0 = success
  4061. /// 1 = failure
  4062. void clif_tradecompleted( map_session_data& sd ){
  4063. PACKET_ZC_EXEC_EXCHANGE_ITEM packet{};
  4064. packet.packetType = HEADER_ZC_EXEC_EXCHANGE_ITEM;
  4065. packet.result = 0;
  4066. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4067. }
  4068. /// Resets the trade window on the send side.
  4069. /// 00f1 (ZC_EXCHANGEITEM_UNDO)
  4070. /// NOTE: Unknown purpose. Items are not removed until the window is
  4071. /// refreshed (ex. by putting another item in there).
  4072. void clif_tradeundo( map_session_data& sd ){
  4073. PACKET_ZC_EXCHANGEITEM_UNDO packet{};
  4074. packet.packetType = HEADER_ZC_EXCHANGEITEM_UNDO;
  4075. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4076. }
  4077. /// Updates storage total amount.
  4078. /// 00f2 <current count>.W <max count>.W (ZC_NOTIFY_STOREITEM_COUNTINFO)
  4079. void clif_updatestorageamount( map_session_data& sd, uint16 amount, uint16 max_amount ){
  4080. PACKET_ZC_NOTIFY_STOREITEM_COUNTINFO packet{};
  4081. packet.packetType = HEADER_ZC_NOTIFY_STOREITEM_COUNTINFO;
  4082. packet.amount = amount;
  4083. packet.max_amount = max_amount;
  4084. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4085. }
  4086. /// Notifies the client of an item being added to the storage.
  4087. /// 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)
  4088. /// 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)
  4089. /// 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)
  4090. void clif_storageitemadded( map_session_data* sd, struct item* i, int32 index, int32 amount ){
  4091. nullpo_retv( sd );
  4092. nullpo_retv( i );
  4093. int32 fd = sd->fd;
  4094. if( !session_isActive( fd ) ){
  4095. return;
  4096. }
  4097. PACKET_ZC_ADD_ITEM_TO_STORE p = {};
  4098. p.packetType = HEADER_ZC_ADD_ITEM_TO_STORE; // Storage item added
  4099. p.index = client_storage_index( index ); // index
  4100. p.amount = amount; // amount
  4101. p.itemId = client_nameid( i->nameid ); // id
  4102. #if PACKETVER >= 5
  4103. p.itemType = itemtype( i->nameid ); //type
  4104. #endif
  4105. p.identified = i->identify; //identify flag
  4106. p.damaged = i->attribute; // attribute
  4107. p.refine = i->refine; //refine
  4108. clif_addcards( &p.slot, i );
  4109. #if PACKETVER_MAIN_NUM >= 20140813 || PACKETVER_RE_NUM >= 20140402 || defined(PACKETVER_ZERO)
  4110. clif_add_random_options( p.option_data, *i );
  4111. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20221024
  4112. p.grade = i->enchantgrade;
  4113. #endif
  4114. #endif
  4115. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  4116. }
  4117. /// Notifies the client of an item being deleted from the storage.
  4118. /// 00f6 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_STORE)
  4119. void clif_storageitemremoved( map_session_data& sd, uint16 index, uint32 amount ){
  4120. PACKET_ZC_DELETE_ITEM_FROM_STORE packet{};
  4121. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_STORE;
  4122. packet.index = client_storage_index(index);
  4123. packet.amount = amount;
  4124. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4125. }
  4126. /// Closes storage.
  4127. /// 00f8 (ZC_CLOSE_STORE)
  4128. void clif_storageclose( map_session_data& sd ){
  4129. PACKET_ZC_CLOSE_STORE packet{};
  4130. packet.packetType = HEADER_ZC_CLOSE_STORE;
  4131. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4132. }
  4133. /// Notifies clients in an area of a player's souls.
  4134. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  4135. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  4136. /// 0b73 <id>.L <amount>.W (ZC_SOULENERGY)
  4137. void clif_soulball( map_session_data *sd, struct block_list* target, enum send_target send_target ){
  4138. #if PACKETVER_MAIN_NUM >= 20200414 || PACKETVER_RE_NUM >= 20200723 || PACKETVER_ZERO_NUM >= 20200506
  4139. PACKET_ZC_SOULENERGY p = {};
  4140. p.PacketType = HEADER_ZC_SOULENERGY;
  4141. #else
  4142. PACKET_ZC_SPIRITS p = {};
  4143. p.PacketType = HEADER_ZC_SPIRITS;
  4144. #endif
  4145. p.AID = sd->bl.id;
  4146. p.num = sd->soulball;
  4147. clif_send( &p, sizeof( p ), target == nullptr ? &sd->bl : target, send_target );
  4148. }
  4149. /*==========================================
  4150. * Server tells 'sd' player client the abouts of 'dstsd' player
  4151. *------------------------------------------*/
  4152. static void clif_getareachar_pc(map_session_data* sd,map_session_data* dstsd)
  4153. {
  4154. struct block_list *d_bl;
  4155. int32 i;
  4156. if( dstsd->chatID ) {
  4157. chat_data* cd = map_id2cd( dstsd->chatID );
  4158. if( cd != nullptr && cd->usersd[0] == dstsd ){
  4159. clif_dispchat(*cd);
  4160. }
  4161. } else if( dstsd->state.vending )
  4162. clif_showvendingboard( *dstsd, SELF, &sd->bl );
  4163. else if( dstsd->state.buyingstore )
  4164. clif_buyingstore_entry( *dstsd, &sd->bl );
  4165. if(dstsd->spiritball > 0)
  4166. clif_spiritball( &dstsd->bl, &sd->bl, SELF );
  4167. clif_millenniumshield_single( *dstsd, *sd );
  4168. clif_spiritcharm_single( *dstsd, *sd );
  4169. if (dstsd->soulball > 0)
  4170. clif_soulball( dstsd, &sd->bl, SELF );
  4171. if (dstsd->servantball > 0)
  4172. clif_servantball( *dstsd, &sd->bl, SELF );
  4173. if (dstsd->abyssball > 0)
  4174. clif_abyssball( *dstsd, &sd->bl, SELF );
  4175. if( (sd->status.party_id && dstsd->status.party_id == sd->status.party_id) || //Party-mate, or hpdisp setting.
  4176. (sd->bg_id && sd->bg_id == dstsd->bg_id) || //BattleGround
  4177. pc_has_permission(sd, PC_PERM_VIEW_HPMETER)
  4178. )
  4179. clif_hpmeter_single( *sd, dstsd->bl.id, dstsd->battle_status.hp, dstsd->battle_status.max_hp );
  4180. // display link (sd - dstsd) to sd
  4181. ARR_FIND( 0, MAX_DEVOTION, i, sd->devotion[i] == dstsd->bl.id );
  4182. if( i < MAX_DEVOTION )
  4183. clif_devotion(&sd->bl, sd);
  4184. // display links (dstsd - devotees) to sd
  4185. ARR_FIND( 0, MAX_DEVOTION, i, dstsd->devotion[i] > 0 );
  4186. if( i < MAX_DEVOTION )
  4187. clif_devotion(&dstsd->bl, sd);
  4188. // display link (dstsd - crusader) to sd
  4189. if( dstsd->sc.getSCE(SC_DEVOTION) && (d_bl = map_id2bl(dstsd->sc.getSCE(SC_DEVOTION)->val1)) != nullptr )
  4190. clif_devotion(d_bl, sd);
  4191. }
  4192. void clif_getareachar_unit( map_session_data* sd,struct block_list *bl ){
  4193. struct unit_data *ud;
  4194. struct view_data *vd;
  4195. bool option = false;
  4196. uint32 option_val = 0;
  4197. vd = status_get_viewdata(bl);
  4198. if (!vd || vd->class_ == JT_INVISIBLE)
  4199. return;
  4200. // Hide NPC from Maya Purple card
  4201. if (clif_npc_mayapurple(bl))
  4202. return;
  4203. if( bl->type == BL_NPC && npc_is_hidden_dynamicnpc( *( (struct npc_data*)bl ), *sd ) ){
  4204. // Do not send anything
  4205. return;
  4206. }
  4207. ud = unit_bl2ud(bl);
  4208. if( ud && ud->walktimer != INVALID_TIMER ){
  4209. clif_set_unit_walking( *bl, sd, *ud, SELF );
  4210. }else{
  4211. clif_set_unit_idle( bl, false, SELF, &sd->bl );
  4212. }
  4213. if (vd->cloth_color)
  4214. clif_refreshlook(&sd->bl,bl->id,LOOK_CLOTHES_COLOR,vd->cloth_color,SELF);
  4215. if (vd->body_style)
  4216. clif_refreshlook(&sd->bl,bl->id,LOOK_BODY2,vd->body_style,SELF);
  4217. switch (bl->type)
  4218. {
  4219. case BL_PC:
  4220. {
  4221. TBL_PC* tsd = (TBL_PC*)bl;
  4222. clif_getareachar_pc(sd, tsd);
  4223. if(tsd->state.size==SZ_BIG) // tiny/big players [Valaris]
  4224. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4225. else if(tsd->state.size==SZ_MEDIUM)
  4226. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4227. if( tsd->bg_id && map_getmapflag(tsd->bl.m, MF_BATTLEGROUND) )
  4228. clif_sendbgemblem_single(sd->fd,tsd);
  4229. if ( tsd->status.robe )
  4230. clif_refreshlook(&sd->bl,bl->id,LOOK_ROBE,tsd->status.robe,SELF);
  4231. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4232. clif_hat_effects( *sd, tsd->bl, SELF );
  4233. }
  4234. break;
  4235. case BL_MER: // Devotion Effects
  4236. if( ((TBL_MER*)bl)->devotion_flag )
  4237. clif_devotion(bl, sd);
  4238. break;
  4239. case BL_NPC:
  4240. {
  4241. TBL_NPC* nd = (TBL_NPC*)bl;
  4242. if( nd->chat_id ){
  4243. chat_data* cd = map_id2cd( nd->chat_id );
  4244. if( cd != nullptr ){
  4245. clif_dispchat( *cd );
  4246. }
  4247. }
  4248. if( nd->size == SZ_BIG )
  4249. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4250. else if( nd->size == SZ_MEDIUM )
  4251. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4252. clif_efst_status_change_sub(&sd->bl, bl, SELF);
  4253. clif_progressbar_npc(nd, sd);
  4254. }
  4255. break;
  4256. case BL_MOB:
  4257. {
  4258. TBL_MOB* md = (TBL_MOB*)bl;
  4259. if(md->special_state.size==SZ_BIG) // tiny/big mobs [Valaris]
  4260. clif_specialeffect_single(bl,EF_GIANTBODY2,sd->fd);
  4261. else if(md->special_state.size==SZ_MEDIUM)
  4262. clif_specialeffect_single(bl,EF_BABYBODY2,sd->fd);
  4263. #if PACKETVER >= 20120404
  4264. if (battle_config.monster_hp_bars_info && !map_getmapflag(bl->m, MF_HIDEMOBHPBAR)) {
  4265. int32 i;
  4266. for(i = 0; i < DAMAGELOG_SIZE; i++)// must show hp bar to all char who already hit the mob.
  4267. if( md->dmglog[i].id == sd->status.char_id )
  4268. clif_monster_hp_bar(md, sd->fd);
  4269. }
  4270. #endif
  4271. }
  4272. break;
  4273. case BL_PET:
  4274. if (vd->head_bottom)
  4275. clif_pet_equip(sd, (TBL_PET*)bl); // needed to display pet equip properly
  4276. break;
  4277. }
  4278. }
  4279. //Modifies the type of damage according to status changes [Skotlex]
  4280. //Aegis data specifies that: 4 endure against single hit sources, 9 against multi-hit.
  4281. static enum e_damage_type clif_calc_delay(enum e_damage_type type, int32 div, int64 damage, int32 delay)
  4282. {
  4283. return ( delay == 0 && damage > 0 ) ? ( div > 1 ? DMG_MULTI_HIT_ENDURE : DMG_ENDURE ) : type;
  4284. }
  4285. /*==========================================
  4286. * Estimates walk delay based on the damage criteria. [Skotlex]
  4287. *------------------------------------------*/
  4288. static int32 clif_calc_walkdelay(struct block_list *bl,int32 delay, char type, int64 damage, int32 div_)
  4289. {
  4290. if (type == DMG_ENDURE || type == DMG_MULTI_HIT_ENDURE || damage <= 0)
  4291. return 0;
  4292. if (bl->type == BL_PC) {
  4293. if (battle_config.pc_walk_delay_rate != 100)
  4294. delay = delay*battle_config.pc_walk_delay_rate/100;
  4295. } else
  4296. if (battle_config.walk_delay_rate != 100)
  4297. delay = delay*battle_config.walk_delay_rate/100;
  4298. if (div_ > 1) //Multi-hit skills mean higher delays.
  4299. delay += battle_config.multihit_delay*(div_-1);
  4300. return (delay > 0) ? delay:1; //Return 1 to specify there should be no noticeable delay, but you should stop walking.
  4301. }
  4302. /*========================================== [Playtester]
  4303. * Returns hallucination damage the client displays
  4304. *------------------------------------------*/
  4305. static int32 clif_hallucination_damage()
  4306. {
  4307. int32 digit = rnd() % 5 + 1;
  4308. switch (digit)
  4309. {
  4310. case 1:
  4311. return (rnd() % 10);
  4312. case 2:
  4313. return (rnd() % 100);
  4314. case 3:
  4315. return (rnd() % 1000);
  4316. case 4:
  4317. return (rnd() % 10000);
  4318. }
  4319. return (rnd() % 32767);
  4320. }
  4321. #define DEFAULT_ANIMATION_SPEED 432
  4322. /// Sends a 'damage' packet (src performs action on dst)
  4323. /// 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)
  4324. /// 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)
  4325. /// 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)
  4326. /// type:
  4327. /// 0 = damage [ damage: total damage, div: amount of hits, damage2: assassin dual-wield damage ]
  4328. /// 1 = pick up item
  4329. /// 2 = sit down
  4330. /// 3 = stand up
  4331. /// 4 = damage (endure)
  4332. /// 5 = (splash?)
  4333. /// 6 = (skill?)
  4334. /// 7 = (repeat damage?)
  4335. /// 8 = multi-hit damage
  4336. /// 9 = multi-hit damage (endure)
  4337. /// 10 = critical hit
  4338. /// 11 = lucky dodge
  4339. /// 12 = (touch skill?)
  4340. /// 13 = multi-hit critical
  4341. int32 clif_damage(block_list& src, block_list& dst, t_tick tick, int32 sdelay, int32 ddelay, int64 sdamage, int32 div, enum e_damage_type type, int64 sdamage2, bool spdamage){
  4342. int32 damage = (int32)cap_value(sdamage,INT_MIN,INT_MAX);
  4343. int32 damage2 = (int32)cap_value(sdamage2,INT_MIN,INT_MAX);
  4344. if (type != DMG_MULTI_HIT_CRITICAL)
  4345. type = clif_calc_delay(type,div,damage+damage2,ddelay);
  4346. status_change *sc = status_get_sc(&dst);
  4347. if(sc != nullptr && !sc->empty()) {
  4348. if(sc->getSCE(SC_HALLUCINATION)) {
  4349. damage = clif_hallucination_damage();
  4350. if(damage2)
  4351. damage2 = clif_hallucination_damage();
  4352. }
  4353. }
  4354. // Calculate what sdelay to send to the client so it applies damage at the same time as the server
  4355. if (battle_config.synchronize_damage && src.type == BL_MOB) {
  4356. // When a clif_damage packet is sent to the client it will also send "sdelay" (amotion) as value.
  4357. // The client however does not interpret this value as AttackMotion but incorrectly as an inverted
  4358. // animation speed modifier, with 432 standing for 1x animation speed.
  4359. // The client will ignore all values above 432, but lower values will speed up the animation.
  4360. // 216 for example means play the animation at double the speed. 108 is quadruple speed.
  4361. // Each monster has an attack animation and may define the frame in the attack animation on which
  4362. // it displays the damage and makes the target flinch / stop. If the damage frame is undefined,
  4363. // it instead displays the damage / flinch / stop at the beginning of the second to last frame.
  4364. // We define the time after which the damage frame shows at 1x speed as clientamotion.
  4365. uint16 clientamotion = std::max((uint16)1, status_get_clientamotion(&src));
  4366. // Knowing when the damage frame happens in the animation allows us to synchronize the timing
  4367. // between client and server using the formula below.
  4368. sdelay = sdelay * DEFAULT_ANIMATION_SPEED / clientamotion;
  4369. // Hint: If amotion is larger than clientamotion this results in a value above 432 which makes the
  4370. // client display the attack at 1x speed. In this case we need to shorten the delay damage timer
  4371. // on the server to clientamotion ms instead (see battle_delay_damage).
  4372. sdelay = std::min(sdelay, DEFAULT_ANIMATION_SPEED);
  4373. }
  4374. PACKET_ZC_NOTIFY_ACT p{};
  4375. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4376. p.srcID = src.id;
  4377. p.targetID = dst.id;
  4378. p.serverTick = client_tick(tick);
  4379. p.srcSpeed = sdelay;
  4380. p.dmgSpeed = ddelay;
  4381. if (battle_config.hide_woe_damage && map_flag_gvg(src.m)) {
  4382. p.damage = damage ? div : 0;
  4383. p.damage2 = damage2 ? div : 0;
  4384. } else {
  4385. p.damage = static_cast<decltype(p.damage)>( std::min( damage, static_cast<decltype(damage)>( std::numeric_limits<decltype(p.damage)>::max() ) ) );
  4386. p.damage2 = static_cast<decltype(p.damage2)>( std::min( damage2, static_cast<decltype(damage2)>( std::numeric_limits<decltype(p.damage2)>::max() ) ) );
  4387. }
  4388. #if PACKETVER >= 20131223
  4389. p.isSPDamage = (spdamage) ? 1 : 0; // IsSPDamage - Displays blue digits.
  4390. #endif
  4391. p.div = div;
  4392. p.type = type;
  4393. if(disguised(&dst)) {
  4394. clif_send( &p, sizeof(p), &dst, AREA_WOS);
  4395. p.targetID = disguised_bl_id( dst.id );
  4396. clif_send( &p, sizeof(p), &dst, SELF);
  4397. } else
  4398. clif_send(&p, sizeof(p), &dst, AREA);
  4399. if(disguised(&src)) {
  4400. p.srcID = disguised_bl_id( src.id );
  4401. if(damage > 0)
  4402. p.damage = -1;
  4403. if(damage2 > 0)
  4404. p.damage2 = -1;
  4405. clif_send( &p, sizeof(p), &src, SELF);
  4406. }
  4407. if(&src == &dst)
  4408. unit_setdir(&src, unit_getdir(&src));
  4409. // In case this assignment is bypassed by DMG_MULTI_HIT_CRITICAL
  4410. type = clif_calc_delay(type, div, damage + damage2, ddelay);
  4411. //Return adjusted can't walk delay for further processing.
  4412. return clif_calc_walkdelay(&dst, ddelay, type, damage+damage2, div);
  4413. }
  4414. /*==========================================
  4415. * src picks up dst
  4416. *------------------------------------------*/
  4417. void clif_takeitem(block_list& src, block_list& dst){
  4418. PACKET_ZC_NOTIFY_ACT p{};
  4419. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4420. p.srcID = src.id;
  4421. p.targetID = dst.id;
  4422. p.type = DMG_PICKUP_ITEM;
  4423. clif_send(&p, sizeof(p), &src, AREA);
  4424. }
  4425. /*==========================================
  4426. * inform clients in area that `bl` is sitting
  4427. *------------------------------------------*/
  4428. void clif_sitting(block_list& bl){
  4429. PACKET_ZC_NOTIFY_ACT p{};
  4430. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4431. p.srcID = bl.id;
  4432. p.type = DMG_SIT_DOWN;
  4433. clif_send(&p, sizeof(p), &bl, AREA);
  4434. if(disguised(&bl)) {
  4435. p.srcID = disguised_bl_id( bl.id );
  4436. clif_send(&p, sizeof(p), &bl, SELF);
  4437. }
  4438. }
  4439. /*==========================================
  4440. * inform clients in area that `bl` is standing
  4441. *------------------------------------------*/
  4442. void clif_standing(block_list& bl){
  4443. PACKET_ZC_NOTIFY_ACT p{};
  4444. p.packetType = HEADER_ZC_NOTIFY_ACT;
  4445. p.srcID = bl.id;
  4446. p.type = DMG_STAND_UP;
  4447. clif_send(&p, sizeof(p), &bl, AREA);
  4448. if(disguised(&bl)) {
  4449. p.srcID = disguised_bl_id( bl.id );
  4450. clif_send(&p, sizeof(p), &bl, SELF);
  4451. }
  4452. }
  4453. /// Inform client(s) about a map-cell change (ZC_UPDATE_MAPINFO).
  4454. /// 0192 <x>.W <y>.W <type>.W <map name>.16B
  4455. void clif_changemapcell(int32 fd, int16 m, int32 x, int32 y, int32 type, enum send_target target)
  4456. {
  4457. unsigned char buf[32];
  4458. WBUFW(buf,0) = 0x192;
  4459. WBUFW(buf,2) = x;
  4460. WBUFW(buf,4) = y;
  4461. WBUFW(buf,6) = type;
  4462. mapindex_getmapname_ext(map_mapid2mapname(m),WBUFCP(buf,8));
  4463. if( session_isActive(fd) )
  4464. {
  4465. WFIFOHEAD(fd,packet_len(0x192));
  4466. memcpy(WFIFOP(fd,0), buf, packet_len(0x192));
  4467. WFIFOSET(fd,packet_len(0x192));
  4468. }
  4469. else
  4470. {
  4471. struct block_list dummy_bl;
  4472. dummy_bl.type = BL_NUL;
  4473. dummy_bl.x = x;
  4474. dummy_bl.y = y;
  4475. dummy_bl.m = m;
  4476. clif_send(buf,packet_len(0x192),&dummy_bl,target);
  4477. }
  4478. }
  4479. /// Notifies the client about an item on floor.
  4480. /// 009d <id>.L <name id>.W <identified>.B <x>.W <y>.W <amount>.W <subX>.B <subY>.B (ZC_ITEM_ENTRY)
  4481. void clif_getareachar_item( map_session_data& sd, flooritem_data& fitem ){
  4482. PACKET_ZC_ITEM_ENTRY p = {};
  4483. p.packetType = HEADER_ZC_ITEM_ENTRY;
  4484. p.AID = fitem.bl.id;
  4485. p.itemId = client_nameid( fitem.item.nameid );
  4486. p.identify = fitem.item.identify;
  4487. p.x = fitem.bl.x;
  4488. p.y = fitem.bl.y;
  4489. p.amount = fitem.item.amount;
  4490. p.subX = fitem.subx;
  4491. p.subY = fitem.suby;
  4492. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  4493. }
  4494. /// Notifes client about Graffiti
  4495. /// 01c9 <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B <has msg>.B <msg>.80B (ZC_SKILL_ENTRY2)
  4496. static void clif_graffiti(struct block_list *bl, struct skill_unit *unit, enum send_target target) {
  4497. unsigned char buf[128];
  4498. nullpo_retv(bl);
  4499. nullpo_retv(unit);
  4500. WBUFW(buf, 0) = 0x1c9;
  4501. WBUFL(buf, 2) = unit->bl.id;
  4502. WBUFL(buf, 6) = unit->group->src_id;
  4503. WBUFW(buf,10) = unit->bl.x;
  4504. WBUFW(buf,12) = unit->bl.y;
  4505. WBUFB(buf,14) = unit->group->unit_id;
  4506. WBUFB(buf,15) = 1;
  4507. WBUFB(buf,16) = 1;
  4508. safestrncpy(WBUFCP(buf,17),unit->group->valstr,MESSAGE_SIZE);
  4509. clif_send(buf,packet_len(0x1c9),bl,target);
  4510. }
  4511. /// Notifies the client of a skill unit.
  4512. /// 011f <id>.L <creator id>.L <x>.W <y>.W <unit id>.B <visible>.B (ZC_SKILL_ENTRY)
  4513. /// 08c7 <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.B <range>.W <visible>.B (ZC_SKILL_ENTRY3)
  4514. /// 099f <lenght>.W <id> L <creator id>.L <x>.W <y>.W <unit id>.L <range>.W <visible>.B (ZC_SKILL_ENTRY4)
  4515. /// 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)
  4516. void clif_getareachar_skillunit(struct block_list *bl, struct skill_unit *unit, enum send_target target, bool visible) {
  4517. int32 header = 0, unit_id = 0, pos = 0, fd = 0, len = -1;
  4518. unsigned char buf[128];
  4519. nullpo_retv(bl);
  4520. nullpo_retv(unit);
  4521. if (bl->type == BL_PC)
  4522. fd = ((TBL_PC*)bl)->fd;
  4523. if (unit->group->state.guildaura)
  4524. return;
  4525. if (unit->group->state.song_dance&0x1 && unit->val2&(1 << UF_ENSEMBLE))
  4526. unit_id = unit->val2&(1 << UF_SONG) ? UNT_DISSONANCE : UNT_UGLYDANCE;
  4527. else if (skill_get_unit_flag(unit->group->skill_id, UF_RANGEDSINGLEUNIT) && !(unit->val2 & (1 << UF_RANGEDSINGLEUNIT)))
  4528. unit_id = UNT_DUMMYSKILL; // Use invisible unit id for other case of rangedsingle unit
  4529. else
  4530. unit_id = unit->group->unit_id;
  4531. if (!visible)
  4532. unit_id = UNT_DUMMYSKILL; // Hack to makes hidden trap really hidden!
  4533. #if PACKETVER >= 3
  4534. if (unit_id == UNT_GRAFFITI) { // Graffiti [Valaris]
  4535. clif_graffiti(bl, unit, target);
  4536. return;
  4537. }
  4538. #endif
  4539. #if PACKETVER <= 20120702
  4540. header = 0x011f;
  4541. //#if PACKETVER < 20110718
  4542. // header = 0x011f;
  4543. //#elif PACKETVER < 20121212
  4544. // header = 0x08c7;
  4545. #elif PACKETVER < 20130731
  4546. header = 0x099f;
  4547. #else
  4548. header = 0x09ca;
  4549. #endif
  4550. len = packet_len(header);
  4551. WBUFW(buf,pos) = header;
  4552. if (header != 0x011f) {
  4553. WBUFW(buf, pos+2) = len;
  4554. pos += 2;
  4555. }
  4556. WBUFL(buf,pos+2) = unit->bl.id;
  4557. WBUFL(buf,pos+6) = unit->group->src_id;
  4558. WBUFW(buf,pos+10) = unit->bl.x;
  4559. WBUFW(buf,pos+12) = unit->bl.y;
  4560. switch (header) {
  4561. case 0x011f:
  4562. WBUFB(buf,pos+14) = unit_id;
  4563. WBUFB(buf,pos+15) = visible;
  4564. break;
  4565. case 0x08c7:
  4566. WBUFB(buf,pos+14) = unit_id;
  4567. WBUFW(buf,pos+15) = unit->range;
  4568. WBUFB(buf,pos+17) = visible;
  4569. break;
  4570. case 0x099f:
  4571. WBUFL(buf,pos+14) = unit_id;
  4572. WBUFW(buf,pos+18) = unit->range;
  4573. WBUFB(buf,pos+20) = visible;
  4574. break;
  4575. case 0x09ca:
  4576. WBUFL(buf,pos+14) = unit_id;
  4577. WBUFB(buf,pos+18) = (unsigned char)unit->range;
  4578. WBUFB(buf,pos+19) = visible;
  4579. WBUFB(buf,pos+20) = (unsigned char)unit->group->skill_lv;
  4580. break;
  4581. }
  4582. clif_send(buf, len, bl, target);
  4583. if (unit->group->skill_id == WZ_ICEWALL)
  4584. clif_changemapcell(fd, unit->bl.m, unit->bl.x, unit->bl.y, 5, SELF);
  4585. }
  4586. /// 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)
  4587. void clif_skill_unit_test(struct block_list *bl, short x, short y, int32 unit_id, short range, short skill_lv) {
  4588. unsigned char buf[128];
  4589. nullpo_retv(bl);
  4590. WBUFW(buf, 0) = 0x09ca;
  4591. WBUFW(buf, 2) = packet_len(0x09ca);
  4592. WBUFL(buf, 4) = 1000;
  4593. WBUFL(buf, 8) = 2000;
  4594. WBUFW(buf, 12) = x;
  4595. WBUFW(buf, 14) = y;
  4596. WBUFL(buf, 16) = unit_id;
  4597. WBUFB(buf, 20) = (unsigned char)range;
  4598. WBUFB(buf, 21) = 1;
  4599. WBUFB(buf, 22) = (unsigned char)skill_lv;
  4600. clif_send(buf, packet_len(0x09ca), bl, AREA);
  4601. }
  4602. /// Server tells client to remove unit of id 'unit->bl.id'
  4603. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4604. static void clif_clearchar_skillunit( skill_unit& unit, map_session_data& sd ){
  4605. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4606. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4607. packet.GID = unit.bl.id;
  4608. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  4609. if( unit.group && unit.group->skill_id == WZ_ICEWALL ){
  4610. clif_changemapcell( sd.fd, unit.bl.m, unit.bl.x, unit.bl.y, unit.val2, SELF );
  4611. }
  4612. }
  4613. /// Removes a skill unit.
  4614. /// 0120 <id>.L (ZC_SKILL_DISAPPEAR)
  4615. void clif_skill_delunit( skill_unit& unit ){
  4616. PACKET_ZC_SKILL_DISAPPEAR packet{};
  4617. packet.packetType = HEADER_ZC_SKILL_DISAPPEAR;
  4618. packet.GID = unit.bl.id;
  4619. clif_send( &packet, sizeof( packet ), &unit.bl, AREA );
  4620. }
  4621. /// Sent when an object gets ankle-snared.
  4622. /// 01ac <id>.L (ZC_SKILL_UPDATE)
  4623. /// Only affects units with class [139,153] client-side.
  4624. void clif_skillunit_update( block_list& bl ){
  4625. PACKET_ZC_SKILL_UPDATE packet{};
  4626. packet.packetType = HEADER_ZC_SKILL_UPDATE;
  4627. packet.GID = bl.id;
  4628. clif_send( &packet, sizeof( packet ), &bl, AREA );
  4629. }
  4630. /*==========================================
  4631. *
  4632. *------------------------------------------*/
  4633. static int32 clif_getareachar(struct block_list* bl,va_list ap)
  4634. {
  4635. map_session_data *sd;
  4636. nullpo_ret(bl);
  4637. sd=va_arg(ap,map_session_data*);
  4638. if (!clif_session_isValid(sd))
  4639. return 0;
  4640. switch(bl->type){
  4641. case BL_ITEM:
  4642. clif_getareachar_item( *sd, *reinterpret_cast<flooritem_data*>( bl ) );
  4643. break;
  4644. case BL_SKILL:
  4645. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &sd->bl);
  4646. break;
  4647. default:
  4648. if(&sd->bl == bl)
  4649. break;
  4650. clif_getareachar_unit(sd,bl);
  4651. break;
  4652. }
  4653. return 0;
  4654. }
  4655. /*==========================================
  4656. * tbl has gone out of view-size of bl
  4657. *------------------------------------------*/
  4658. int32 clif_outsight(struct block_list *bl,va_list ap)
  4659. {
  4660. struct block_list *tbl;
  4661. struct view_data *vd;
  4662. TBL_PC *sd, *tsd;
  4663. tbl=va_arg(ap,struct block_list*);
  4664. if(bl == tbl) return 0;
  4665. sd = BL_CAST(BL_PC, bl);
  4666. tsd = BL_CAST(BL_PC, tbl);
  4667. if (clif_session_isValid(tsd)) { //tsd has lost sight of the bl object.
  4668. nullpo_ret(bl);
  4669. switch(bl->type){
  4670. case BL_PC:
  4671. if(sd->vd.class_ != JT_INVISIBLE)
  4672. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4673. if(sd->chatID){
  4674. struct chat_data *cd;
  4675. cd=(struct chat_data*)map_id2bl(sd->chatID);
  4676. if(cd->usersd[0]==sd)
  4677. clif_dispchat(*cd);
  4678. }
  4679. if(sd->state.vending)
  4680. clif_closevendingboard(bl,tsd->fd);
  4681. if(sd->state.buyingstore)
  4682. clif_buyingstore_disappear_entry( *sd, &tsd->bl );
  4683. break;
  4684. case BL_ITEM:
  4685. clif_clearflooritem( *reinterpret_cast<flooritem_data*>( bl ), tsd );
  4686. break;
  4687. case BL_SKILL:
  4688. clif_clearchar_skillunit( *((skill_unit *)bl), *tsd );
  4689. break;
  4690. case BL_NPC:
  4691. if(!(((TBL_NPC*)bl)->is_invisible))
  4692. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4693. break;
  4694. default:
  4695. if((vd=status_get_viewdata(bl)) && vd->class_ != JT_INVISIBLE)
  4696. clif_clearunit_single( bl->id, CLR_OUTSIGHT, *tsd );
  4697. break;
  4698. }
  4699. }
  4700. if (clif_session_isValid(sd)) { //sd is watching tbl go out of view.
  4701. nullpo_ret(tbl);
  4702. if(tbl->type == BL_SKILL) //Trap knocked out of sight
  4703. clif_clearchar_skillunit( *((skill_unit *)tbl), *sd );
  4704. else if(((vd=status_get_viewdata(tbl)) && vd->class_ != JT_INVISIBLE) &&
  4705. !(tbl->type == BL_NPC && (((TBL_NPC*)tbl)->is_invisible)))
  4706. clif_clearunit_single( tbl->id, CLR_OUTSIGHT, *sd );
  4707. }
  4708. return 0;
  4709. }
  4710. /*==========================================
  4711. * tbl has come into view of bl
  4712. *------------------------------------------*/
  4713. int32 clif_insight(struct block_list *bl,va_list ap)
  4714. {
  4715. struct block_list *tbl;
  4716. TBL_PC *sd, *tsd;
  4717. tbl=va_arg(ap,struct block_list*);
  4718. if (bl == tbl) return 0;
  4719. sd = BL_CAST(BL_PC, bl);
  4720. tsd = BL_CAST(BL_PC, tbl);
  4721. if (clif_session_isValid(tsd)) { //Tell tsd that bl entered into his view
  4722. switch(bl->type){
  4723. case BL_ITEM:
  4724. clif_getareachar_item( *tsd, *reinterpret_cast<flooritem_data*>( bl ) );
  4725. break;
  4726. case BL_SKILL:
  4727. skill_getareachar_skillunit_visibilty_single((TBL_SKILL*)bl, &tsd->bl);
  4728. break;
  4729. default:
  4730. clif_getareachar_unit(tsd,bl);
  4731. break;
  4732. }
  4733. }
  4734. if (clif_session_isValid(sd)) { //Tell sd that tbl walked into his view
  4735. clif_getareachar_unit(sd,tbl);
  4736. }
  4737. return 0;
  4738. }
  4739. /// Updates whole skill tree (ZC_SKILLINFO_LIST).
  4740. /// 010f <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  4741. void clif_skillinfoblock(map_session_data *sd)
  4742. {
  4743. int32 fd;
  4744. int32 i,len,id;
  4745. nullpo_retv(sd);
  4746. fd = sd->fd;
  4747. if (!session_isActive(fd))
  4748. return;
  4749. WFIFOHEAD(fd, MAX_SKILL * 37 + 4);
  4750. WFIFOW(fd,0) = 0x10f;
  4751. bool haveCallPartnerSkill = false;
  4752. for ( i = 0, len = 4; i < MAX_SKILL; i++)
  4753. {
  4754. if( (id = sd->status.skill[i].id) != 0 )
  4755. {
  4756. // workaround for bugreport:5348
  4757. if (len + 37 > 8192)
  4758. break;
  4759. // skip WE_CALLPARTNER and send it in special way
  4760. if (id == WE_CALLPARTNER) {
  4761. haveCallPartnerSkill = true;
  4762. continue;
  4763. }
  4764. WFIFOW(fd,len) = id;
  4765. WFIFOL(fd,len+2) = skill_get_inf(id);
  4766. WFIFOW(fd,len+6) = sd->status.skill[i].lv;
  4767. WFIFOW(fd,len+8) = skill_get_sp(id,sd->status.skill[i].lv);
  4768. WFIFOW(fd,len+10)= skill_get_range2(&sd->bl,id,sd->status.skill[i].lv,false);
  4769. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  4770. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  4771. WFIFOB(fd,len+36) = (sd->status.skill[i].lv < skill_tree_get_max(id, sd->status.class_))? 1:0;
  4772. else
  4773. WFIFOB(fd,len+36) = 0;
  4774. len += 37;
  4775. }
  4776. }
  4777. WFIFOW(fd,2)=len;
  4778. WFIFOSET(fd,len);
  4779. // adoption fix
  4780. if (haveCallPartnerSkill) {
  4781. clif_addskill(*sd, WE_CALLPARTNER);
  4782. clif_skillinfo(sd, WE_CALLPARTNER, 0);
  4783. }
  4784. // workaround for bugreport:5348; send the remaining skills one by one to bypass packet size limit
  4785. for ( ; i < MAX_SKILL; i++)
  4786. {
  4787. if( (id = sd->status.skill[i].id) != 0 && ( id != WE_CALLPARTNER || !haveCallPartnerSkill ) )
  4788. {
  4789. clif_addskill(*sd, id);
  4790. clif_skillinfo(sd, id, 0);
  4791. }
  4792. }
  4793. }
  4794. /**
  4795. * Server tells client 'sd' to add skill of id 'id' to it's skill tree (e.g. with Ice Falcion item)
  4796. **/
  4797. /// Adds new skill to the skill tree.
  4798. /// 0111 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B (ZC_ADD_SKILL)
  4799. /// 0b31 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <upgradable>.B <isnew>.B (ZC_ADD_SKILL2)
  4800. void clif_addskill(map_session_data &sd, uint16 skill_id){
  4801. uint16 idx = skill_get_index(skill_id);
  4802. if (idx == 0)
  4803. return;
  4804. if( sd.status.skill[idx].id <= 0 )
  4805. return;
  4806. PACKET_ZC_ADD_SKILL p{};
  4807. p.packetType = HEADER_ZC_ADD_SKILL;
  4808. p.skill.id = skill_id;
  4809. p.skill.inf = skill_get_inf(skill_id);
  4810. p.skill.level = sd.status.skill[idx].lv;
  4811. p.skill.sp = static_cast<decltype(p.skill.sp)>( skill_get_sp(skill_id,sd.status.skill[idx].lv) );
  4812. p.skill.range2 = static_cast<decltype(p.skill.range2)>( skill_get_range2(&sd.bl,skill_id,sd.status.skill[idx].lv,false) );
  4813. #if PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190918
  4814. p.skill.level2 = pc_checkskill(&sd,skill_id);
  4815. #else
  4816. safestrncpy(p.skill.name, skill_get_name(skill_id), sizeof(p.skill.name));
  4817. #endif
  4818. if( sd.status.skill[idx].flag == SKILL_FLAG_PERMANENT && sd.status.skill[idx].lv < skill_tree_get_max(skill_id, sd.status.class_))
  4819. p.skill.upFlag = 1;
  4820. else
  4821. p.skill.upFlag = 0;
  4822. clif_send(&p,sizeof(p),&sd.bl,SELF);
  4823. }
  4824. /// Deletes a skill from the skill tree.
  4825. /// 0441 <skill id>.W (ZC_SKILLINFO_DELETE)
  4826. void clif_deleteskill(map_session_data& sd, uint16 skill_id, bool skip_infoblock){
  4827. #if PACKETVER >= 20081126
  4828. uint16 idx = skill_get_index(skill_id);
  4829. if (idx == 0)
  4830. return;
  4831. PACKET_ZC_SKILLINFO_DELETE p{};
  4832. p.packetType = HEADER_ZC_SKILLINFO_DELETE;
  4833. p.skillID = skill_id;
  4834. clif_send(&p,sizeof(p),&sd.bl,SELF);
  4835. #endif
  4836. #if PACKETVER_MAIN_NUM >= 20190807 || PACKETVER_RE_NUM >= 20190807 || PACKETVER_ZERO_NUM >= 20190918
  4837. if (!skip_infoblock)
  4838. #endif
  4839. clif_skillinfoblock(&sd);
  4840. }
  4841. /// Updates a skill in the skill tree (ZC_SKILLINFO_UPDATE).
  4842. /// 010e <skill id>.W <level>.W <sp cost>.W <attack range>.W <upgradable>.B
  4843. void clif_skillup(map_session_data *sd, uint16 skill_id, int32 lv, int32 range, int32 upgradable) {
  4844. nullpo_retv(sd);
  4845. int32 fd = sd->fd;
  4846. uint16 idx = skill_get_index(skill_id);
  4847. if (!session_isActive(fd) || !idx)
  4848. return;
  4849. WFIFOHEAD(fd, packet_len(0x10e));
  4850. WFIFOW(fd, 0) = 0x10e;
  4851. WFIFOW(fd, 2) = skill_id;
  4852. WFIFOW(fd, 4) = lv;
  4853. WFIFOW(fd, 6) = skill_get_sp(skill_id, lv);
  4854. WFIFOW(fd, 8) = range;
  4855. WFIFOB(fd, 10) = upgradable;
  4856. WFIFOSET(fd, packet_len(0x10e));
  4857. }
  4858. /// Updates a skill in the skill tree (ZC_SKILLINFO_UPDATE2).
  4859. /// 07e1 <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <upgradable>.B
  4860. void clif_skillinfo(map_session_data *sd,int32 skill_id, int32 inf)
  4861. {
  4862. nullpo_retv(sd);
  4863. const int32 fd = sd->fd;
  4864. uint16 idx = skill_get_index(skill_id);
  4865. if (!session_isActive(fd) || !idx)
  4866. return;
  4867. WFIFOHEAD(fd,packet_len(0x7e1));
  4868. WFIFOW(fd,0) = 0x7e1;
  4869. WFIFOW(fd,2) = skill_id;
  4870. WFIFOL(fd,4) = inf?inf:skill_get_inf(skill_id);
  4871. WFIFOW(fd,8) = sd->status.skill[idx].lv;
  4872. WFIFOW(fd,10) = skill_get_sp(skill_id,sd->status.skill[idx].lv);
  4873. WFIFOW(fd,12) = skill_get_range2(&sd->bl,skill_id,sd->status.skill[idx].lv,false);
  4874. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  4875. WFIFOB(fd,14) = (sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_))? 1:0;
  4876. else
  4877. WFIFOB(fd,14) = 0;
  4878. WFIFOSET(fd,packet_len(0x7e1));
  4879. }
  4880. void clif_skill_scale( struct block_list *bl, int32 src_id, int32 x, int32 y, uint16 skill_id, uint16 skill_lv, int32 casttime ){
  4881. #if PACKETVER >= 20151223
  4882. if( !battle_config.show_skill_scale ){
  4883. return;
  4884. }
  4885. PACKET_ZC_SKILL_SCALE p = {};
  4886. p.PacketType = skillscale;
  4887. p.AID = src_id;
  4888. p.skill_id = skill_id;
  4889. p.skill_lv = skill_lv;
  4890. p.x = x;
  4891. p.y = y;
  4892. p.casttime = casttime;
  4893. if( disguised( bl ) ){
  4894. clif_send( &p, sizeof( p ), bl, AREA_WOS );
  4895. p.AID = disguised_bl_id( bl->id );
  4896. clif_send( &p, sizeof( p ), bl, SELF );
  4897. }else{
  4898. clif_send( &p, sizeof( p ), bl, AREA );
  4899. }
  4900. #endif
  4901. }
  4902. /// Notifies clients in area, that an object is about to use a skill.
  4903. /// 013e <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L (ZC_USESKILL_ACK)
  4904. /// 07fb <src id>.L <dst id>.L <x>.W <y>.W <skill id>.W <property>.L <delaytime>.L <is disposable>.B (ZC_USESKILL_ACK2)
  4905. /// property:
  4906. /// 0 = Yellow cast aura
  4907. /// 1 = Water elemental cast aura
  4908. /// 2 = Earth elemental cast aura
  4909. /// 3 = Fire elemental cast aura
  4910. /// 4 = Wind elemental cast aura
  4911. /// 5 = Poison elemental cast aura
  4912. /// 6 = Holy elemental cast aura
  4913. /// ? = like 0
  4914. /// is disposable:
  4915. /// 0 = yellow chat text "[src name] will use skill [skill name]."
  4916. /// 1 = no text
  4917. void clif_skillcasting(struct block_list* bl, int32 src_id, int32 dst_id, int32 dst_x, int32 dst_y, uint16 skill_id, uint16 skill_lv, int32 property, int32 casttime)
  4918. {
  4919. #if PACKETVER < 20091124
  4920. const int32 cmd = 0x13e;
  4921. #else
  4922. const int32 cmd = 0x7fb;
  4923. #endif
  4924. unsigned char buf[32];
  4925. WBUFW(buf,0) = cmd;
  4926. WBUFL(buf,2) = src_id;
  4927. WBUFL(buf,6) = dst_id;
  4928. WBUFW(buf,10) = dst_x;
  4929. WBUFW(buf,12) = dst_y;
  4930. WBUFW(buf,14) = skill_id;
  4931. WBUFL(buf,16) = property<0?0:property; //Avoid sending negatives as element [Skotlex]
  4932. WBUFL(buf,20) = casttime;
  4933. #if PACKETVER >= 20091124
  4934. WBUFB(buf,24) = 0; // isDisposable
  4935. #endif
  4936. if (disguised(bl)) {
  4937. clif_send(buf,packet_len(cmd), bl, AREA_WOS);
  4938. WBUFL(buf,2) = disguised_bl_id( src_id );
  4939. clif_send(buf,packet_len(cmd), bl, SELF);
  4940. } else
  4941. clif_send(buf,packet_len(cmd), bl, AREA);
  4942. if( skill_get_inf2( skill_id, INF2_SHOWSCALE ) ){
  4943. clif_skill_scale( bl, src_id, bl->x, bl->y, skill_id, skill_lv, casttime );
  4944. }
  4945. }
  4946. /// Notifies clients in area, that an object canceled casting.
  4947. /// 01b9 <id>.L (ZC_DISPEL)
  4948. void clif_skillcastcancel( block_list& bl ){
  4949. PACKET_ZC_DISPEL packet{};
  4950. packet.packetType = HEADER_ZC_DISPEL;
  4951. packet.gid = bl.id;
  4952. clif_send( &packet, sizeof(PACKET_ZC_DISPEL), &bl, AREA );
  4953. }
  4954. /// Notifies the client about the result of a skill use request (ZC_ACK_TOUSESKILL).
  4955. /// 0110 <skill id>.W <num>.L <result>.B <cause>.B
  4956. /// num (only used when skill id = NV_BASIC and cause = 0):
  4957. /// 0 = "skill failed" MsgStringTable[159]
  4958. /// 1 = "no emotions" MsgStringTable[160]
  4959. /// 2 = "no sit" MsgStringTable[161]
  4960. /// 3 = "no chat" MsgStringTable[162]
  4961. /// 4 = "no party" MsgStringTable[163]
  4962. /// 5 = "no shout" MsgStringTable[164]
  4963. /// 6 = "no PKing" MsgStringTable[165]
  4964. /// 7 = "no alligning" MsgStringTable[383]
  4965. /// ? = ignored
  4966. /// cause:
  4967. /// 0 = "not enough skill level" MsgStringTable[214] (AL_WARP)
  4968. /// "steal failed" MsgStringTable[205] (TF_STEAL)
  4969. /// "envenom failed" MsgStringTable[207] (TF_POISON)
  4970. /// "skill failed" MsgStringTable[204] (otherwise)
  4971. /// ... = @see enum useskill_fail_cause
  4972. /// ? = ignored
  4973. ///
  4974. /// if(result!=0) doesn't display any of the previous messages
  4975. /// Note: when this packet is received an unknown flag is always set to 0,
  4976. /// suggesting this is an ACK packet for the UseSkill packets and should be sent on success too [FlavioJS]
  4977. void clif_skill_fail( map_session_data& sd, uint16 skill_id, enum useskill_fail_cause cause, int32 btype, t_itemid itemId ){
  4978. if(battle_config.display_skill_fail&1)
  4979. return; //Disable all skill failed messages
  4980. if(cause==USESKILL_FAIL_SKILLINTERVAL && !sd.state.showdelay)
  4981. return; //Disable delay failed messages
  4982. if(skill_id == RG_SNATCHER && battle_config.display_skill_fail&4)
  4983. return;
  4984. if(skill_id == TF_POISON && battle_config.display_skill_fail&8)
  4985. return;
  4986. PACKET_ZC_ACK_TOUSESKILL p = {};
  4987. p.packetType = HEADER_ZC_ACK_TOUSESKILL;
  4988. p.skillId = skill_id;
  4989. p.btype = btype;
  4990. p.itemId = itemId;
  4991. p.flag = 0; // 0 - failed
  4992. p.cause = cause;
  4993. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  4994. }
  4995. /// Skill cooldown display icon.
  4996. /// 043d <skill ID>.W <tick>.L (ZC_SKILL_POSTDELAY)
  4997. void clif_skill_cooldown( map_session_data &sd, uint16 skill_id, t_tick tick ){
  4998. #if PACKETVER_MAIN_NUM >= 20081112 || PACKETVER_RE_NUM >= 20081111 || defined(PACKETVER_ZERO)
  4999. PACKET_ZC_SKILL_POSTDELAY packet{};
  5000. packet.PacketType = HEADER_ZC_SKILL_POSTDELAY;
  5001. packet.SKID = skill_id;
  5002. packet.DelayTM = client_tick(tick);
  5003. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5004. #endif
  5005. }
  5006. /// Skill attack effect and damage.
  5007. /// 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)
  5008. /// 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)
  5009. int32 clif_skill_damage(struct block_list *src,struct block_list *dst,t_tick tick,int32 sdelay,int32 ddelay,int64 sdamage,int32 div,uint16 skill_id,uint16 skill_lv,enum e_damage_type type)
  5010. {
  5011. unsigned char buf[64];
  5012. int32 damage = (int32)cap_value(sdamage,INT_MIN,INT_MAX);
  5013. nullpo_ret(src);
  5014. nullpo_ret(dst);
  5015. type = clif_calc_delay(type,div,damage,ddelay);
  5016. status_change* sc = status_get_sc( dst );
  5017. if( sc != nullptr && !sc->empty() ) {
  5018. if(sc->getSCE(SC_HALLUCINATION) && damage)
  5019. damage = clif_hallucination_damage();
  5020. }
  5021. #if PACKETVER < 3
  5022. WBUFW(buf,0)=0x114;
  5023. WBUFW(buf,2)=skill_id;
  5024. WBUFL(buf,4)=src->id;
  5025. WBUFL(buf,8)=dst->id;
  5026. WBUFL(buf,12)=client_tick(tick);
  5027. WBUFL(buf,16)=sdelay;
  5028. WBUFL(buf,20)=ddelay;
  5029. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5030. WBUFW(buf,24)=damage?div:0;
  5031. } else {
  5032. WBUFW(buf,24)=damage;
  5033. }
  5034. WBUFW(buf,26)=skill_lv;
  5035. WBUFW(buf,28)=div;
  5036. WBUFB(buf,30)=type;
  5037. if (disguised(dst)) {
  5038. clif_send(buf,packet_len(0x114),dst,AREA_WOS);
  5039. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5040. clif_send(buf,packet_len(0x114),dst,SELF);
  5041. } else
  5042. clif_send(buf,packet_len(0x114),dst,AREA);
  5043. if(disguised(src)) {
  5044. WBUFL(buf,4)=disguised_bl_id(src->id);
  5045. if (disguised(dst))
  5046. WBUFL(buf,8)=dst->id;
  5047. if(damage > 0)
  5048. WBUFW(buf,24)=-1;
  5049. clif_send(buf,packet_len(0x114),src,SELF);
  5050. }
  5051. #else
  5052. WBUFW(buf,0)=0x1de;
  5053. WBUFW(buf,2)=skill_id;
  5054. WBUFL(buf,4)=src->id;
  5055. WBUFL(buf,8)=dst->id;
  5056. WBUFL(buf,12)=client_tick(tick);
  5057. WBUFL(buf,16)=sdelay;
  5058. WBUFL(buf,20)=ddelay;
  5059. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5060. WBUFL(buf,24)=damage?div:0;
  5061. } else {
  5062. WBUFL(buf,24)=damage;
  5063. }
  5064. WBUFW(buf,28)=skill_lv;
  5065. WBUFW(buf,30)=div;
  5066. // For some reason, late 2013 and newer clients have
  5067. // a issue that causes players and monsters to endure
  5068. // type 6 (ACTION_SKILL) skills. So we have to do a small
  5069. // hack to set all type 6 to be sent as type 8 ACTION_ATTACK_MULTIPLE
  5070. #if PACKETVER < 20131223
  5071. WBUFB(buf,32)=type;
  5072. #else
  5073. WBUFB(buf,32)=( type == DMG_SINGLE ) ? DMG_MULTI_HIT : type;
  5074. #endif
  5075. if (disguised(dst)) {
  5076. clif_send(buf,packet_len(0x1de),dst,AREA_WOS);
  5077. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5078. clif_send(buf,packet_len(0x1de),dst,SELF);
  5079. } else
  5080. clif_send(buf,packet_len(0x1de),dst,AREA);
  5081. if(disguised(src)) {
  5082. WBUFL(buf,4)=disguised_bl_id(src->id);
  5083. if (disguised(dst))
  5084. WBUFL(buf,8)=dst->id;
  5085. if(damage > 0)
  5086. WBUFL(buf,24)=-1;
  5087. clif_send(buf,packet_len(0x1de),src,SELF);
  5088. }
  5089. #endif
  5090. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5091. return clif_calc_walkdelay(dst,ddelay,type,damage,div);
  5092. }
  5093. /// Ground skill attack effect and damage (ZC_NOTIFY_SKILL_POSITION).
  5094. /// 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
  5095. /*
  5096. int32 clif_skill_damage2(struct block_list *src,struct block_list *dst,t_tick tick,int32 sdelay,int32 ddelay,int32 damage,int32 div,uint16 skill_id,uint16 skill_lv,enum e_damage_type type)
  5097. {
  5098. unsigned char buf[64];
  5099. status_change *sc;
  5100. nullpo_ret(src);
  5101. nullpo_ret(dst);
  5102. type = (type>DMG_NORMAL)?type:skill_get_hit(skill_id);
  5103. type = clif_calc_delay(type,div,damage,ddelay);
  5104. sc = status_get_sc(dst);
  5105. if(sc && sc->count) {
  5106. if(sc->getSCE(SC_HALLUCINATION) && damage)
  5107. damage = clif_hallucination_damage();
  5108. }
  5109. WBUFW(buf,0)=0x115;
  5110. WBUFW(buf,2)=skill_id;
  5111. WBUFL(buf,4)=src->id;
  5112. WBUFL(buf,8)=dst->id;
  5113. WBUFL(buf,12)=client_tick(tick);
  5114. WBUFL(buf,16)=sdelay;
  5115. WBUFL(buf,20)=ddelay;
  5116. WBUFW(buf,24)=dst->x;
  5117. WBUFW(buf,26)=dst->y;
  5118. if (battle_config.hide_woe_damage && map_flag_gvg(src->m)) {
  5119. WBUFW(buf,28)=damage?div:0;
  5120. } else {
  5121. WBUFW(buf,28)=damage;
  5122. }
  5123. WBUFW(buf,30)=skill_lv;
  5124. WBUFW(buf,32)=div;
  5125. WBUFB(buf,34)=type;
  5126. clif_send(buf,packet_len(0x115),src,AREA);
  5127. if(disguised(src)) {
  5128. WBUFL(buf,4)=disguised_bl_id(src->id);
  5129. if(damage > 0)
  5130. WBUFW(buf,28)=-1;
  5131. clif_send(buf,packet_len(0x115),src,SELF);
  5132. }
  5133. if (disguised(dst)) {
  5134. WBUFL(buf,8)=disguised_bl_id(dst->id);
  5135. if (disguised(src))
  5136. WBUFL(buf,4)=src->id;
  5137. else if(damage > 0)
  5138. WBUFW(buf,28)=-1;
  5139. clif_send(buf,packet_len(0x115),dst,SELF);
  5140. }
  5141. //Because the damage delay must be synced with the client, here is where the can-walk tick must be updated. [Skotlex]
  5142. return clif_calc_walkdelay(dst,ddelay,type,damage,div);
  5143. }
  5144. */
  5145. /// Non-damaging skill effect
  5146. /// 011a <skill id>.W <heal>.W <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL)
  5147. /// 09cb <skill id>.W <heal>.L <dst id>.L <src id>.L <result>.B (ZC_USE_SKILL2)
  5148. bool clif_skill_nodamage( block_list* src, block_list& dst, uint16 skill_id, int32 heal, bool success ){
  5149. PACKET_ZC_USE_SKILL p{};
  5150. p.PacketType = HEADER_ZC_USE_SKILL;
  5151. p.SKID = skill_id;
  5152. p.level = std::min( static_cast<decltype(p.level)>( heal ), std::numeric_limits<decltype(p.level)>::max() );
  5153. p.targetAID = dst.id;
  5154. p.result = success;
  5155. if(src != nullptr){
  5156. p.srcAID = src->id;
  5157. }else{
  5158. p.srcAID = 0;
  5159. }
  5160. if (disguised(&dst)) {
  5161. clif_send(&p, sizeof(p), &dst, AREA_WOS);
  5162. p.targetAID = disguised_bl_id(dst.id);
  5163. clif_send(&p, sizeof(p), &dst, SELF);
  5164. } else
  5165. clif_send(&p, sizeof(p), &dst, AREA);
  5166. if(src != nullptr && disguised(src)) {
  5167. p.srcAID = disguised_bl_id(src->id);
  5168. if (disguised(&dst)){
  5169. // It is necessary to revert the changes done above for the disguised target
  5170. p.targetAID = dst.id;
  5171. }
  5172. clif_send(&p, sizeof(p), src, SELF);
  5173. }
  5174. return success;
  5175. }
  5176. /// Non-damaging ground skill effect (ZC_NOTIFY_GROUNDSKILL).
  5177. /// 0117 <skill id>.W <src id>.L <level>.W <x>.W <y>.W <tick>.L
  5178. void clif_skill_poseffect(struct block_list *src,uint16 skill_id,int32 val,int32 x,int32 y,t_tick tick)
  5179. {
  5180. unsigned char buf[32];
  5181. nullpo_retv(src);
  5182. WBUFW(buf,0)=0x117;
  5183. WBUFW(buf,2)=skill_id;
  5184. WBUFL(buf,4)=src->id;
  5185. WBUFW(buf,8)=val;
  5186. WBUFW(buf,10)=x;
  5187. WBUFW(buf,12)=y;
  5188. WBUFL(buf,14)=client_tick(tick);
  5189. if(disguised(src)) {
  5190. clif_send(buf,packet_len(0x117),src,AREA_WOS);
  5191. WBUFL(buf,4)=disguised_bl_id(src->id);
  5192. clif_send(buf,packet_len(0x117),src,SELF);
  5193. } else
  5194. clif_send(buf,packet_len(0x117),src,AREA);
  5195. }
  5196. /// Presents a list of available warp destinations.
  5197. /// 011c <skill id>.W { <map name>.16B }*4 (ZC_WARPLIST)
  5198. /// 0abe <lenght>.W <skill id>.W { <map name>.16B }*? (ZC_WARPLIST2)
  5199. void clif_skill_warppoint( map_session_data& sd, uint16 skill_id, uint16 skill_lv, std::vector<std::string>& maps ){
  5200. if(maps.empty())
  5201. return;
  5202. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  5203. PACKET_ZC_WARPLIST* p = reinterpret_cast<PACKET_ZC_WARPLIST*>( packet_buffer );
  5204. p->packetType = HEADER_ZC_WARPLIST;
  5205. p->packetLength = sizeof( *p );
  5206. p->skillId = skill_id;
  5207. size_t memoCount = 0;
  5208. for( std::string& map : maps ){
  5209. if( map.empty() ){
  5210. continue;
  5211. }
  5212. PACKET_ZC_WARPLIST_sub& warp = p->maps[memoCount];
  5213. mapindex_getmapname_ext( map.c_str(), warp.map );
  5214. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( warp ) );
  5215. memoCount++;
  5216. }
  5217. clif_send( p, p->packetLength, &sd.bl, SELF );
  5218. #else
  5219. PACKET_ZC_WARPLIST p = {};
  5220. p.packetType = HEADER_ZC_WARPLIST;
  5221. p.skillId = skill_id;
  5222. size_t memoCount = 0, max = 4;
  5223. for( std::string& map : maps ){
  5224. if( map.empty() ){
  5225. continue;
  5226. }
  5227. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5228. mapindex_getmapname_ext( map.c_str(), warp.map );
  5229. if( memoCount++ == max ){
  5230. break;
  5231. }
  5232. }
  5233. for( ; memoCount < max; memoCount++ ){
  5234. PACKET_ZC_WARPLIST_sub& warp = p.maps[memoCount];
  5235. strcpy( warp.map, "" );
  5236. }
  5237. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  5238. #endif
  5239. sd.menuskill_id = skill_id;
  5240. if (skill_id == AL_WARP) {
  5241. sd.menuskill_val = (sd.ud.skillx<<16)|sd.ud.skilly; //Store warp position here.
  5242. sd.state.workinprogress = WIP_DISABLE_ALL;
  5243. } else
  5244. sd.menuskill_val = skill_lv;
  5245. }
  5246. /// Memo message.
  5247. /// 011e <type>.B (ZC_ACK_REMEMBER_WARPPOINT)
  5248. /// type:
  5249. /// 0 = "Saved location as a Memo Point for Warp skill." in color 0xFFFF00 (cyan)
  5250. /// 1 = "Skill Level is not high enough." in color 0x0000FF (red)
  5251. /// 2 = "You haven't learned Warp." in color 0x0000FF (red)
  5252. ///
  5253. /// @param sd Who receives the message
  5254. /// @param type What message
  5255. void clif_skill_memomessage( map_session_data& sd, e_ack_remember_warppoint_result result ){
  5256. PACKET_ZC_ACK_REMEMBER_WARPPOINT packet{};
  5257. packet.packetType = HEADER_ZC_ACK_REMEMBER_WARPPOINT;
  5258. packet.type = static_cast<decltype(packet.type)>(result);
  5259. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5260. }
  5261. /// Teleport message.
  5262. /// 0189 <type>.W (ZC_NOTIFY_MAPINFO)
  5263. /// type:
  5264. /// 0 = "Unable to Teleport in this area" in color 0xFFFF00 (cyan)
  5265. /// 1 = "Saved point cannot be memorized." in color 0x0000FF (red)
  5266. /// 2 = "This skill cannot be used in this area"
  5267. /// 3 = "This item cannot be used in this area"
  5268. ///
  5269. /// @param sd Who receives the message
  5270. /// @param type What message
  5271. void clif_skill_teleportmessage( map_session_data& sd, e_notify_mapinfo_result result ){
  5272. PACKET_ZC_NOTIFY_MAPINFO packet{};
  5273. packet.packetType = HEADER_ZC_NOTIFY_MAPINFO;
  5274. packet.type = static_cast<decltype(packet.type)>(result);
  5275. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5276. }
  5277. /// Displays Sense (WZ_ESTIMATION) information window (ZC_MONSTER_INFO).
  5278. /// 018c <class>.W <level>.W <size>.W <hp>.L <def>.W <race>.W <mdef>.W <element>.W
  5279. /// <water%>.B <earth%>.B <fire%>.B <wind%>.B <poison%>.B <holy%>.B <shadow%>.B <ghost%>.B <undead%>.B
  5280. void clif_skill_estimation(map_session_data *sd,struct block_list *dst)
  5281. {
  5282. unsigned char buf[64];
  5283. int32 i, fix;
  5284. nullpo_retv(sd);
  5285. nullpo_retv(dst);
  5286. if( dst->type != BL_MOB )
  5287. return;
  5288. status_data* status = status_get_status_data(*dst);
  5289. WBUFW(buf, 0)=0x18c;
  5290. WBUFW(buf, 2)=status_get_class(dst);
  5291. WBUFW(buf, 4)=status_get_lv(dst);
  5292. WBUFW(buf, 6)=status->size;
  5293. WBUFL(buf, 8)=status->hp;
  5294. WBUFW(buf,12)= (battle_config.estimation_type&1?status->def:0)
  5295. +(battle_config.estimation_type&2?status->def2:0);
  5296. WBUFW(buf,14)=status->race;
  5297. WBUFW(buf,16)= (battle_config.estimation_type&1?status->mdef:0)
  5298. +(battle_config.estimation_type&2?status->mdef2:0);
  5299. WBUFW(buf,18)= status->def_ele;
  5300. for(i=0;i<9;i++)
  5301. // The following caps negative attributes to 0 since the client displays them as 255-fix. [Skotlex]
  5302. WBUFB(buf,20+i)= (unsigned char)((fix=elemental_attribute_db.getAttribute(status->ele_lv, i+1, status->def_ele))<0?0:fix);
  5303. clif_send(buf,packet_len(0x18c),&sd->bl,sd->status.party_id>0?PARTY_SAMEMAP:SELF);
  5304. }
  5305. /// Presents a textual list of producable items.
  5306. /// 018d <packet len>.W { <name id>.W { <material id>.W }*3 }* (ZC_MAKABLEITEMLIST)
  5307. /// material id:
  5308. /// unused by the client
  5309. void clif_skill_produce_mix_list( map_session_data& sd, int32 skill_id, int32 trigger ){
  5310. // Avoid resending the menu
  5311. if( sd.menuskill_id == skill_id ){
  5312. return;
  5313. }
  5314. if (skill_id == GC_CREATENEWPOISON)
  5315. skill_id = GC_RESEARCHNEWPOISON;
  5316. PACKET_ZC_MAKABLEITEMLIST* p = reinterpret_cast<PACKET_ZC_MAKABLEITEMLIST*>( packet_buffer );
  5317. p->packetType = HEADER_ZC_MAKABLEITEMLIST;
  5318. p->packetLength = sizeof( *p );
  5319. int32 count = 0;
  5320. for( int32 i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5321. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, 1 ) ){
  5322. continue;
  5323. }
  5324. if( skill_id > 0 && skill_produce_db[i].req_skill != skill_id ){
  5325. continue;
  5326. }
  5327. PACKET_ZC_MAKABLEITEMLIST_sub& entry = p->items[count];
  5328. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5329. entry.material[0] = 0;
  5330. entry.material[1] = 0;
  5331. entry.material[2] = 0;
  5332. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5333. count++;
  5334. }
  5335. clif_send( p, p->packetLength, &sd.bl, SELF );
  5336. if( count > 0 ){
  5337. sd.menuskill_id = skill_id;
  5338. sd.menuskill_val = trigger;
  5339. }
  5340. }
  5341. /// Present a list of producable items.
  5342. /// 025a <packet len>.W <mk type>.W { <name id>.W }* (ZC_MAKINGITEM_LIST)
  5343. /// mk type:
  5344. /// 1 = cooking
  5345. /// 2 = arrow
  5346. /// 3 = elemental
  5347. /// 4 = GN_MIX_COOKING
  5348. /// 5 = GN_MAKEBOMB
  5349. /// 6 = GN_S_PHARMACY
  5350. /// 7 = MT_M_MACHINE
  5351. /// 8 = BO_BIONIC_PHARMACY
  5352. void clif_cooking_list( map_session_data& sd, int32 trigger, uint16 skill_id, int32 qty, int32 list_type ){
  5353. #if PACKETVER >= 20051010
  5354. // Avoid resending the menu
  5355. if( sd.menuskill_id == skill_id ){
  5356. return;
  5357. }
  5358. PACKET_ZC_MAKINGITEM_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGITEM_LIST*>( packet_buffer );
  5359. p->packetType = HEADER_ZC_MAKINGITEM_LIST;
  5360. p->packetLength = sizeof( *p );
  5361. p->makeItem = list_type;
  5362. int32 count = 0;
  5363. for( int32 i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  5364. if( !skill_can_produce_mix( &sd, skill_produce_db[i].nameid, trigger, qty ) ){
  5365. continue;
  5366. }
  5367. PACKET_ZC_MAKINGITEM_LIST_sub& entry = p->items[count];
  5368. entry.itemId = client_nameid( skill_produce_db[i].nameid );
  5369. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  5370. count++;
  5371. }
  5372. if( count > 0 || skill_id == AM_PHARMACY ){
  5373. clif_send( p, p->packetLength, &sd.bl, SELF );
  5374. sd.menuskill_id = skill_id;
  5375. sd.menuskill_val = trigger;
  5376. sd.menuskill_val2 = qty; // amount.
  5377. }else{
  5378. clif_menuskill_clear( &sd );
  5379. #if PACKETVER >= 20090922
  5380. clif_msg_skill( &sd, skill_id, MSI_SKILL_INVENTORY_KINDCNT_OVER );
  5381. #else
  5382. clif_send( p, p->packetLength, &sd.bl, SELF );
  5383. #endif
  5384. }
  5385. #endif
  5386. }
  5387. /// Notifies clients of a status change.
  5388. /// 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)]
  5389. /// 043f <index>.W <id>.L <state>.B <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE2) [used exclusively for starting statuses on pcs]
  5390. /// 0983 <index>.W <id>.L <state>.B <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_MSG_STATE_CHANGE3) (PACKETVER >= 20120618)
  5391. /// @param bl Sends packet to clients around this object
  5392. /// @param id ID of object that has this effect
  5393. /// @param type Status icon see enum efst_type
  5394. /// @param flag 1:Active, 0:Deactive
  5395. /// @param tick Duration in ms
  5396. /// @param val1
  5397. /// @param val2
  5398. /// @param val3
  5399. void clif_status_change_sub(struct block_list *bl, int32 id, int32 type, int32 flag, t_tick tick, int32 val1, int32 val2, int32 val3, enum send_target target_type)
  5400. {
  5401. unsigned char buf[32];
  5402. if (type == EFST_BLANK) //It shows nothing on the client...
  5403. return;
  5404. if (type == EFST_POSTDELAY && tick == 0)
  5405. return;
  5406. nullpo_retv(bl);
  5407. // Statuses with an infinite duration, but still needs a duration sent to display properly.
  5408. if (type == EFST_LUNARSTANCE || type == EFST_UNIVERSESTANCE || type == EFST_SUNSTANCE || type == EFST_STARSTANCE)
  5409. tick = 200;
  5410. #if PACKETVER >= 20120618
  5411. if (flag && battle_config.display_status_timers)
  5412. WBUFW(buf,0) = 0x983;
  5413. else
  5414. #elif PACKETVER >= 20090121
  5415. if (flag && battle_config.display_status_timers)
  5416. WBUFW(buf,0) = 0x43f;
  5417. else
  5418. #endif
  5419. WBUFW(buf,0) = 0x196;
  5420. WBUFW(buf,2) = type;
  5421. WBUFL(buf,4) = id;
  5422. WBUFB(buf,8) = flag;
  5423. #if PACKETVER >= 20120618
  5424. if (flag && battle_config.display_status_timers) {
  5425. if (tick <= 0)
  5426. tick = 9999; // this is indeed what official servers do
  5427. WBUFL(buf,9) = client_tick(tick);/* at this stage remain and total are the same value I believe */
  5428. WBUFL(buf,13) = client_tick(tick);
  5429. WBUFL(buf,17) = val1;
  5430. WBUFL(buf,21) = val2;
  5431. WBUFL(buf,25) = val3;
  5432. }
  5433. #elif PACKETVER >= 20090121
  5434. if (flag && battle_config.display_status_timers) {
  5435. if (tick <= 0)
  5436. tick = 9999; // this is indeed what official servers do
  5437. WBUFL(buf,9) = client_tick(tick);
  5438. WBUFL(buf,13) = val1;
  5439. WBUFL(buf,17) = val2;
  5440. WBUFL(buf,21) = val3;
  5441. }
  5442. #endif
  5443. clif_send(buf, packet_len(WBUFW(buf,0)), bl, target_type);
  5444. }
  5445. /* Sends status effect to clients around the bl
  5446. * @param bl Object that has the effect
  5447. * @param type Status icon see enum efst_type
  5448. * @param flag 1:Active, 0:Deactive
  5449. * @param tick Duration in ms
  5450. * @param val1
  5451. * @param val2
  5452. * @param val3
  5453. */
  5454. void clif_status_change(struct block_list *bl, int32 type, int32 flag, t_tick tick, int32 val1, int32 val2, int32 val3) {
  5455. map_session_data *sd = nullptr;
  5456. if (type == EFST_BLANK) //It shows nothing on the client...
  5457. return;
  5458. if (type == EFST_POSTDELAY && tick == 0)
  5459. return;
  5460. if (type == EFST_ILLUSION && !battle_config.display_hallucination) // Disable Hallucination.
  5461. return;
  5462. #if !( PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 )
  5463. // Older clients display normal riding icon.
  5464. if (type == EFST_MADOGEAR_TYPE)
  5465. type = EFST_RIDING;
  5466. #endif
  5467. nullpo_retv(bl);
  5468. sd = BL_CAST(BL_PC, bl);
  5469. // Check if current bl type is in the returned bitmask and only send status changes that actually matter to the client
  5470. if (!(status_efst_get_bl_type(static_cast<efst_type>(type)) & bl->type))
  5471. return;
  5472. clif_status_change_sub(bl, bl->id, type, flag, tick, val1, val2, val3, ((sd ? (pc_isinvisible(sd) ? SELF : AREA) : AREA_WOS)));
  5473. }
  5474. /**
  5475. * Send any active EFST to those around.
  5476. * @param tbl: Unit to send the packet to
  5477. * @param bl: Objects walking into view
  5478. * @param target: Client send type
  5479. */
  5480. void clif_efst_status_change_sub(struct block_list *tbl, struct block_list *bl, enum send_target target) {
  5481. unsigned char i;
  5482. struct sc_display_entry **sc_display;
  5483. unsigned char sc_display_count;
  5484. bool spheres_sent;
  5485. nullpo_retv(bl);
  5486. switch( bl->type ){
  5487. case BL_PC: {
  5488. map_session_data* sd = (map_session_data*)bl;
  5489. sc_display = sd->sc_display;
  5490. sc_display_count = sd->sc_display_count;
  5491. spheres_sent = !sd->state.connect_new;
  5492. }
  5493. break;
  5494. case BL_NPC: {
  5495. struct npc_data* nd = (struct npc_data*)bl;
  5496. sc_display = nd->sc_display;
  5497. sc_display_count = nd->sc_display_count;
  5498. spheres_sent = true;
  5499. }
  5500. break;
  5501. default:
  5502. return;
  5503. }
  5504. for (i = 0; i < sc_display_count; i++) {
  5505. enum sc_type type = sc_display[i]->type;
  5506. status_change *sc = status_get_sc(bl);
  5507. const struct TimerData *td = (sc && sc->getSCE(type) ? get_timer(sc->getSCE(type)->timer) : nullptr);
  5508. t_tick tick = 0;
  5509. if (td)
  5510. tick = DIFF_TICK(td->tick, gettick());
  5511. // Status changes that need special handling
  5512. switch( type ){
  5513. case SC_SPHERE_1:
  5514. case SC_SPHERE_2:
  5515. case SC_SPHERE_3:
  5516. case SC_SPHERE_4:
  5517. case SC_SPHERE_5:
  5518. if( spheres_sent ){
  5519. target = AREA_WOS;
  5520. }
  5521. break;
  5522. case SC_HELLS_PLANT:
  5523. if( sc && sc->getSCE(type) ){
  5524. tick = sc->getSCE(type)->val4;
  5525. }
  5526. break;
  5527. }
  5528. #if PACKETVER > 20120418
  5529. 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);
  5530. #else
  5531. 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);
  5532. #endif
  5533. }
  5534. }
  5535. /// Notifies the client when a player enters the screen with an active EFST.
  5536. /// 08ff <id>.L <index>.W <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER) (PACKETVER >= 20111108)
  5537. /// 0984 <id>.L <index>.W <total msec>.L <remain msec>.L { <val>.L }*3 (ZC_EFST_SET_ENTER2) (PACKETVER >= 20120618)
  5538. void clif_efst_status_change(struct block_list *bl, int32 tid, enum send_target target, int32 type, t_tick tick, int32 val1, int32 val2, int32 val3) {
  5539. #if PACKETVER >= 20111108
  5540. unsigned char buf[32];
  5541. #if PACKETVER >= 20120618
  5542. const int32 cmd = 0x984;
  5543. #elif PACKETVER >= 20111108
  5544. const int32 cmd = 0x8ff;
  5545. #endif
  5546. int32 offset = 0;
  5547. if (type == EFST_BLANK)
  5548. return;
  5549. nullpo_retv(bl);
  5550. if (tick <= 0)
  5551. tick = 9999;
  5552. WBUFW(buf,offset + 0) = cmd;
  5553. WBUFL(buf,offset + 2) = tid;
  5554. WBUFW(buf,offset + 6) = type;
  5555. #if PACKETVER >= 20111108
  5556. WBUFL(buf,offset + 8) = client_tick(tick); // Set remaining status duration [exneval]
  5557. #if PACKETVER >= 20120618
  5558. WBUFL(buf,offset + 12) = client_tick(tick);
  5559. offset += 4;
  5560. #endif
  5561. WBUFL(buf,offset + 12) = val1;
  5562. WBUFL(buf,offset + 16) = val2;
  5563. WBUFL(buf,offset + 20) = val3;
  5564. #endif
  5565. clif_send(buf,packet_len(cmd),bl,target);
  5566. #endif
  5567. }
  5568. /// Send message (modified by [Yor]) (ZC_NOTIFY_PLAYERCHAT).
  5569. /// 008e <packet len>.W <message>.?B
  5570. void clif_displaymessage(const int32 fd, const char* mes)
  5571. {
  5572. nullpo_retv(mes);
  5573. if( session_isActive( fd ) ){
  5574. char *message, *line;
  5575. message = aStrdup(mes);
  5576. line = strtok(message, "\n");
  5577. while(line != nullptr) {
  5578. #if PACKETVER == 20141022
  5579. /** for some reason game client crashes depending on message pattern (only for this packet) **/
  5580. /** so we redirect to ZC_NPC_CHAT **/
  5581. //clif_messagecolor(&sd->bl, color_table[COLOR_DEFAULT], mes, false, SELF);
  5582. unsigned long color = (color_table[COLOR_DEFAULT] & 0x0000FF) << 16 | (color_table[COLOR_DEFAULT] & 0x00FF00) | (color_table[COLOR_DEFAULT] & 0xFF0000) >> 16; // RGB to BGR
  5583. unsigned short len = strnlen(line, CHAT_SIZE_MAX);
  5584. if (len > 0) {
  5585. WFIFOHEAD(fd, 13 + len);
  5586. WFIFOW(fd, 0) = 0x2C1;
  5587. WFIFOW(fd, 2) = 13 + len;
  5588. WFIFOL(fd, 4) = 0;
  5589. WFIFOL(fd, 8) = color;
  5590. safestrncpy(WFIFOCP(fd, 12), line, len + 1);
  5591. WFIFOSET(fd, WFIFOW(fd, 2));
  5592. }
  5593. #else
  5594. // Limit message to 255+1 characters (otherwise it causes a buffer overflow in the client)
  5595. int16 len;
  5596. len = static_cast<decltype(len)>( strnlen(line, CHAT_SIZE_MAX) );
  5597. if (len > 0) { // don't send a void message (it's not displaying on the client chat). @help can send void line.
  5598. WFIFOHEAD(fd, 5 + len);
  5599. WFIFOW(fd,0) = 0x8e;
  5600. WFIFOW(fd,2) = 5 + len; // 4 + len + nullptr teminate
  5601. safestrncpy(WFIFOCP(fd,4), line, len + 1);
  5602. WFIFOSET(fd, 5 + len);
  5603. }
  5604. #endif
  5605. line = strtok(nullptr, "\n");
  5606. }
  5607. aFree(message);
  5608. }
  5609. }
  5610. /// Send broadcast message in yellow or blue without font formatting.
  5611. /// 009a <packet len>.W <message>.?B (ZC_BROADCAST)
  5612. void clif_broadcast(struct block_list* bl, const char* mes, size_t len, int32 type, enum send_target target)
  5613. {
  5614. nullpo_retv(mes);
  5615. if (len < 2)
  5616. return;
  5617. PACKET_ZC_BROADCAST* p = reinterpret_cast<PACKET_ZC_BROADCAST*>( packet_buffer );
  5618. p->packetType = HEADER_ZC_BROADCAST;
  5619. p->PacketLength = sizeof( *p );
  5620. if( ( type&BC_BLUE ) != 0 ){
  5621. // If there's "blue" at the beginning of the message, game client will display it in blue instead of yellow.
  5622. const char* color = "blue";
  5623. size_t length = strlen( color );
  5624. strcpy( p->message, color );
  5625. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  5626. strncpy( &p->message[length], mes, len );
  5627. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5628. }else if( ( type&BC_WOE ) != 0 ){
  5629. // If there's "ssss", game client will recognize message as 'WoE broadcast'.
  5630. const char* color = "ssss";
  5631. size_t length = strlen( color );
  5632. strcpy( p->message, color );
  5633. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  5634. strncpy( &p->message[length], mes, len );
  5635. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5636. }else{
  5637. strncpy( p->message, mes, len );
  5638. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5639. }
  5640. clif_send( p, p->PacketLength, bl, target );
  5641. }
  5642. /*==========================================
  5643. * Displays a message on a 'bl' to all it's nearby clients
  5644. * 008d <PacketLength>.W <GID>.L <message>.?B (ZC_NOTIFY_CHAT)
  5645. *------------------------------------------*/
  5646. void clif_GlobalMessage( block_list& bl, const char* message, enum send_target target ){
  5647. nullpo_retv(message);
  5648. int16 len = (int16)( strlen( message ) + 1 );
  5649. if( len > CHAT_SIZE_MAX ) {
  5650. ShowWarning("clif_GlobalMessage: Truncating too long message '%s' (len=%" PRId16 ").\n", message, len);
  5651. len = CHAT_SIZE_MAX;
  5652. }
  5653. PACKET_ZC_NOTIFY_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT*>( packet_buffer );
  5654. p->PacketType = HEADER_ZC_NOTIFY_CHAT;
  5655. p->PacketLength = sizeof( *p );
  5656. p->GID = bl.id;
  5657. safestrncpy( p->Message, message, len );
  5658. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5659. clif_send( p, p->PacketLength, &bl, target );
  5660. }
  5661. /// Send broadcast message with font formatting.
  5662. /// 01c3 <packet len>.W <fontColor>.L <fontType>.W <fontSize>.W <fontAlign>.W <fontY>.W <message>.?B (ZC_BROADCAST2)
  5663. 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)
  5664. {
  5665. nullpo_retv(mes);
  5666. if (len < 2)
  5667. return;
  5668. PACKET_ZC_BROADCAST2* p = reinterpret_cast<PACKET_ZC_BROADCAST2*>( packet_buffer );
  5669. p->packetType = HEADER_ZC_BROADCAST2;
  5670. p->PacketLength = sizeof( *p );
  5671. p->fontColor = fontColor;
  5672. p->fontType = fontType;
  5673. p->fontSize = fontSize;
  5674. p->fontAlign = fontAlign;
  5675. p->fontY = fontY;
  5676. strncpy( p->message, mes, len );
  5677. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  5678. clif_send( p, p->PacketLength, bl, target );
  5679. }
  5680. /*
  5681. * Display *msg to all *users in channel
  5682. */
  5683. void clif_channel_msg(struct Channel *channel, const char *msg, unsigned long color) {
  5684. DBIterator *iter;
  5685. map_session_data *user;
  5686. unsigned short msg_len = 0, len = 0;
  5687. unsigned char buf[CHAT_SIZE_MAX];
  5688. if (!channel || !msg)
  5689. return;
  5690. msg_len = (unsigned short)(strlen(msg) + 1);
  5691. if( msg_len > sizeof(buf)-12 ) {
  5692. ShowWarning("clif_channel_msg: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  5693. msg_len = sizeof(buf)-12;
  5694. }
  5695. WBUFW(buf,0) = 0x2C1;
  5696. WBUFW(buf,2) = (len = msg_len + 12);
  5697. WBUFL(buf,4) = 0;
  5698. WBUFL(buf,8) = color;
  5699. safestrncpy(WBUFCP(buf,12), msg, msg_len);
  5700. iter = db_iterator(channel->users);
  5701. for( user = (map_session_data *)dbi_first(iter); dbi_exists(iter); user = (map_session_data *)dbi_next(iter) ) {
  5702. clif_send(buf, len, &user->bl, SELF);
  5703. }
  5704. dbi_destroy(iter);
  5705. }
  5706. /// Displays heal effect.
  5707. /// 013d <var id>.W <amount>.W (ZC_RECOVERY)
  5708. /// 0a27 <var id>.W <amount>.L (ZC_RECOVERY2)
  5709. /// var id:
  5710. /// 5 = HP (SP_HP)
  5711. /// 7 = SP (SP_SP)
  5712. /// ? = ignored
  5713. void clif_heal( map_session_data& sd, int32 type, uint32 val ) {
  5714. PACKET_ZC_RECOVERY packet{};
  5715. packet.packetType = HEADER_ZC_RECOVERY;
  5716. packet.type = static_cast<decltype(packet.type)>(type);
  5717. packet.amount = std::min( static_cast<decltype(packet.amount)>( val ), std::numeric_limits<decltype(packet.amount)>::max() );
  5718. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5719. }
  5720. /// Displays resurrection effect.
  5721. /// 0148 <id>.L <type>.W (ZC_RESURRECTION)
  5722. /// type:
  5723. /// ignored
  5724. void clif_resurrection( block_list& bl ){
  5725. PACKET_ZC_RESURRECTION packet{};
  5726. packet.packetType = HEADER_ZC_RESURRECTION;
  5727. packet.gid = bl.id;
  5728. packet.type = 0;
  5729. clif_send( &packet, sizeof( packet ), &bl, AREA );
  5730. if (disguised(&bl)) {
  5731. clif_spawn(&bl);
  5732. }
  5733. }
  5734. /// Sets the map property
  5735. /// 0199 <type>.W (ZC_NOTIFY_MAPPROPERTY)
  5736. /// 099b <type>.W <flags>.L (ZC_MAPPROPERTY_R2)
  5737. void clif_map_property(struct block_list *bl, enum map_property property, enum send_target t)
  5738. {
  5739. #if PACKETVER >= 20121010
  5740. short cmd = 0x99b;
  5741. unsigned char buf[8];
  5742. #else
  5743. short cmd = 0x199;
  5744. unsigned char buf[4];
  5745. #endif
  5746. WBUFW(buf,0)=cmd;
  5747. WBUFW(buf,2)=property;
  5748. #if PACKETVER >= 20121010
  5749. struct map_data *mapdata = map_getmapdata(bl->m);
  5750. map_session_data *sd = BL_CAST(BL_PC, bl);
  5751. WBUFL(buf,4) = ((mapdata->getMapFlag(MF_PVP) || (sd && sd->duel_group > 0))<<0)| // PARTY - Show attack cursor on non-party members (PvP)
  5752. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<1)|// GUILD - Show attack cursor on non-guild members (GvG)
  5753. ((mapdata->getMapFlag(MF_BATTLEGROUND) || mapdata_flag_gvg2(mapdata))<<2)|// SIEGE - Show emblem over characters heads when in GvG (WoE castle)
  5754. ((mapdata->getMapFlag(MF_FORCEMINEFFECT) || mapdata_flag_gvg2(mapdata))<<3)| // USE_SIMPLE_EFFECT - Forces simpler skill effects, like /mineffect command
  5755. ((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
  5756. ((mapdata->getMapFlag(MF_PVP))<<5)| // COUNT_PK - Show the PvP counter
  5757. ((mapdata->getMapFlag(MF_PARTYLOCK))<<6)| // NO_PARTY_FORMATION - Prevents party creation/modification (Might be used for instance dungeons)
  5758. ((mapdata->getMapFlag(MF_BATTLEGROUND))<<7)| // BATTLEFIELD - Unknown (Does something for battlegrounds areas)
  5759. ((mapdata->getMapFlag(MF_NOCOSTUME))<<8)| // DISABLE_COSTUMEITEM - Disable costume sprites
  5760. ((!mapdata->getMapFlag(MF_NOUSECART))<<9)| // USECART - Allow opening cart inventory (Well force it to always allow it)
  5761. ((!mapdata->getMapFlag(MF_NOSUNMOONSTARMIRACLE))<<10); // SUNMOONSTAR_MIRACLE - Allows Star Gladiator's Miracle to activate
  5762. //(1<<11); // Unused bits. 1 - 10 is 0x1 length and 11 is 0x15 length. May be used for future settings.
  5763. #endif
  5764. clif_send(buf,packet_len(cmd),bl,t);
  5765. }
  5766. /// Set the map type.
  5767. /// 01d6 <type>.W (ZC_NOTIFY_MAPPROPERTY2)
  5768. void clif_map_type( map_session_data& sd, e_map_type type ){
  5769. PACKET_ZC_NOTIFY_MAPPROPERTY2 packet{};
  5770. packet.packetType = HEADER_ZC_NOTIFY_MAPPROPERTY2;
  5771. packet.type = static_cast<decltype(packet.type)>(type);
  5772. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5773. }
  5774. /// Updates PvP ranking (ZC_NOTIFY_RANKING).
  5775. /// 019a <id>.L <ranking>.L <total>.L
  5776. void clif_pvpset(map_session_data *sd,int32 pvprank,int32 pvpnum,int32 type)
  5777. {
  5778. if(type == 2) {
  5779. int32 fd = sd->fd;
  5780. WFIFOHEAD(fd,packet_len(0x19a));
  5781. WFIFOW(fd,0) = 0x19a;
  5782. WFIFOL(fd,2) = sd->bl.id;
  5783. WFIFOL(fd,6) = pvprank;
  5784. WFIFOL(fd,10) = pvpnum;
  5785. WFIFOSET(fd,packet_len(0x19a));
  5786. } else {
  5787. unsigned char buf[32];
  5788. WBUFW(buf,0) = 0x19a;
  5789. WBUFL(buf,2) = sd->bl.id;
  5790. if(sd->sc.option&(OPTION_HIDE|OPTION_CLOAK))
  5791. WBUFL(buf,6) = UINT32_MAX; //On client displays as --
  5792. else
  5793. WBUFL(buf,6) = pvprank;
  5794. WBUFL(buf,10) = pvpnum;
  5795. if(pc_isinvisible(sd) || sd->disguise) //Causes crashes when a 'mob' with pvp info dies.
  5796. clif_send(buf,packet_len(0x19a),&sd->bl,SELF);
  5797. else if(!type)
  5798. clif_send(buf,packet_len(0x19a),&sd->bl,AREA);
  5799. else
  5800. clif_send(buf,packet_len(0x19a),&sd->bl,ALL_SAMEMAP);
  5801. }
  5802. }
  5803. /*==========================================
  5804. *
  5805. *------------------------------------------*/
  5806. void clif_map_property_mapall(int32 map_idx, enum map_property property)
  5807. {
  5808. struct block_list bl;
  5809. bl.id = 0;
  5810. bl.type = BL_NUL;
  5811. bl.m = map_idx;
  5812. clif_map_property( &bl, property, ALL_SAMEMAP );
  5813. }
  5814. /// Notifies the client about the result of a refine attempt.
  5815. /// 0188 <result>.W <index>.W <refine>.W (ZC_ACK_ITEMREFINING)
  5816. /// result:
  5817. /// 0 = success
  5818. /// 1 = failure
  5819. /// 2 = downgrade
  5820. /// 3 = failure (?)
  5821. void clif_refine( map_session_data& sd, uint16 index, e_ack_itemrefining result ){
  5822. PACKET_ZC_ACK_ITEMREFINING packet{};
  5823. packet.packetType = HEADER_ZC_ACK_ITEMREFINING;
  5824. packet.result = static_cast<decltype(packet.result)>(result);
  5825. packet.index = client_index( index );
  5826. packet.value = static_cast<decltype(packet.value)>( sd.inventory.u.items_inventory[index].refine );
  5827. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5828. }
  5829. /// Notifies the client about the result of a weapon refine attempt (ZC_ACK_WEAPONREFINE).
  5830. /// 0223 <result>.L <nameid>.W
  5831. /// result:
  5832. /// 0 = "weapon upgraded: %s" MsgStringTable[911] in rgb(0,255,255)
  5833. /// 1 = "weapon upgraded: %s" MsgStringTable[912] in rgb(0,205,205)
  5834. /// 2 = "cannot upgrade %s until you level up the upgrade weapon skill" MsgStringTable[913] in rgb(255,200,200)
  5835. /// 3 = "you lack the item %s to upgrade the weapon" MsgStringTable[914] in rgb(255,200,200)
  5836. void clif_upgrademessage( map_session_data* sd, int32 result, t_itemid item_id ){
  5837. PACKET_ZC_ACK_WEAPONREFINE p = {};
  5838. p.packetType = HEADER_ZC_ACK_WEAPONREFINE;
  5839. p.result = result;
  5840. p.itemId = client_nameid( item_id );
  5841. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  5842. }
  5843. /// Whisper is transmitted to the destination player (ZC_WHISPER).
  5844. /// 0097 <packet len>.W <nick>.24B <message>.?B
  5845. /// 0097 <packet len>.W <nick>.24B <isAdmin>.L <message>.?B (PACKETVER >= 20091104)
  5846. void clif_wis_message(map_session_data* sd, const char* nick, const char* mes, size_t mes_len, int32 gmlvl){
  5847. PACKET_ZC_WHISPER* p = reinterpret_cast<PACKET_ZC_WHISPER*>( packet_buffer );
  5848. p->PacketType = HEADER_ZC_WHISPER;
  5849. p->PacketLength = sizeof( *p );
  5850. safestrncpy( p->sender, nick, NAME_LENGTH );
  5851. safestrncpy( p->message, mes, mes_len );
  5852. p->PacketLength += static_cast<decltype( p->PacketLength )>( mes_len );
  5853. #if PACKETVER >= 20091104
  5854. map_session_data* ssd = map_nick2sd(nick, false);
  5855. // If it is not a message from the server or a player from another map-server
  5856. if( ssd != nullptr ){
  5857. gmlvl = pc_get_group_level(ssd);
  5858. }
  5859. p->isAdmin = (gmlvl == 99) ? 1 : 0;
  5860. #if PACKETVER_MAIN_NUM >= 20131204 || PACKETVER_RE_NUM >= 20131120 || defined(PACKETVER_ZERO)
  5861. if( ssd != nullptr ){
  5862. p->senderGID = ssd->bl.id;
  5863. }else{
  5864. p->senderGID = 0;
  5865. }
  5866. #endif
  5867. #endif
  5868. clif_send( p, p->PacketLength, &sd->bl, SELF );
  5869. }
  5870. /// Inform the player about the result of his whisper action
  5871. /// 0098 <result>.B (ZC_ACK_WHISPER).
  5872. /// 09df <result>.B <CID>.L (ZC_ACK_WHISPER02).
  5873. /// result:
  5874. /// 0 = success to send whisper
  5875. /// 1 = target character is not loged in
  5876. /// 2 = ignored by target
  5877. /// 3 = everyone ignored by target
  5878. void clif_wis_end( map_session_data& sd, e_ack_whisper result ){
  5879. PACKET_ZC_ACK_WHISPER packet{};
  5880. packet.packetType = HEADER_ZC_ACK_WHISPER;
  5881. packet.result = static_cast<decltype(packet.result)>(result);
  5882. #if PACKETVER >= 20131223
  5883. packet.CID = sd.status.char_id;
  5884. #endif
  5885. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5886. }
  5887. /// Returns character name requested by char_id (ZC_ACK_REQNAME_BYGID).
  5888. /// 0194 <char id>.L <name>.24B
  5889. /// 0af7 <flag>.W <char id>.L <name>.24B
  5890. void clif_solved_charname(int32 fd, int32 charid, const char* name)
  5891. {
  5892. #if PACKETVER >= 20180221
  5893. WFIFOHEAD(fd,packet_len(0xaf7));
  5894. WFIFOW(fd,0) = 0xaf7;
  5895. WFIFOW(fd,2) = name[0] ? 3 : 2;
  5896. WFIFOL(fd,4) = charid;
  5897. safestrncpy(WFIFOCP(fd, 8), name, NAME_LENGTH);
  5898. WFIFOSET(fd,packet_len(0x0af7));
  5899. #else
  5900. WFIFOHEAD(fd,packet_len(0x194));
  5901. WFIFOW(fd,0)=0x194;
  5902. WFIFOL(fd,2)=charid;
  5903. safestrncpy(WFIFOCP(fd,6), name, NAME_LENGTH);
  5904. WFIFOSET(fd,packet_len(0x194));
  5905. #endif
  5906. }
  5907. /// Presents a list of items that can be carded/composed (ZC_ITEMCOMPOSITION_LIST).
  5908. /// 017b <packet len>.W { <name id>.W }*
  5909. void clif_use_card(map_session_data *sd,int32 idx)
  5910. {
  5911. int32 i,c,ep;
  5912. int32 fd=sd->fd;
  5913. nullpo_retv(sd);
  5914. if (idx < 0 || idx >= MAX_INVENTORY) //Crash-fix from bad packets.
  5915. return;
  5916. if (!sd->inventory_data[idx] || sd->inventory_data[idx]->type != IT_CARD)
  5917. return; //Avoid parsing invalid item indexes (no card/no item)
  5918. ep=sd->inventory_data[idx]->equip;
  5919. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5920. WFIFOW(fd,0)=0x17b;
  5921. for(i=c=0;i<MAX_INVENTORY;i++){
  5922. int32 j;
  5923. if(sd->inventory_data[i] == nullptr)
  5924. continue;
  5925. if(sd->inventory_data[i]->type!=IT_WEAPON && sd->inventory_data[i]->type!=IT_ARMOR)
  5926. continue;
  5927. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0])) //Can't slot it
  5928. continue;
  5929. if(sd->inventory.u.items_inventory[i].identify==0 ) //Not identified
  5930. continue;
  5931. if((sd->inventory_data[i]->equip&ep)==0) //Not equippable on this part.
  5932. continue;
  5933. if(sd->inventory_data[i]->type==IT_WEAPON && ep==EQP_SHIELD) //Shield card won't go on left weapon.
  5934. continue;
  5935. 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.
  5936. continue;
  5937. ARR_FIND( 0, sd->inventory_data[i]->slots, j, sd->inventory.u.items_inventory[i].card[j] == 0 );
  5938. if( j == sd->inventory_data[i]->slots ) // No room
  5939. continue;
  5940. if( sd->inventory.u.items_inventory[i].equip > 0 ) // Do not check items that are already equipped
  5941. continue;
  5942. WFIFOW(fd,4+c*2)=i+2;
  5943. c++;
  5944. }
  5945. if( !c ) return; // no item is available for card insertion
  5946. WFIFOW(fd,2)=4+c*2;
  5947. WFIFOSET(fd,WFIFOW(fd,2));
  5948. }
  5949. /// Notifies the client about the result of item carding/composition.
  5950. /// 017d <equip index>.W <card index>.W <result>.B (ZC_ACK_ITEMCOMPOSITION)
  5951. /// result:
  5952. /// 0 = success
  5953. /// 1 = failure
  5954. void clif_insert_card( map_session_data& sd, int32 idx_equip, int32 idx_card, bool failure ){
  5955. PACKET_ZC_ACK_ITEMCOMPOSITION packet{};
  5956. packet.packetType = HEADER_ZC_ACK_ITEMCOMPOSITION;
  5957. packet.equipIndex = client_index( idx_equip );
  5958. packet.cardIndex = client_index( idx_card );
  5959. packet.result = failure;
  5960. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5961. }
  5962. /// Presents a list of items that can be identified (ZC_ITEMIDENTIFY_LIST).
  5963. /// 0177 <packet len>.W { <index>.W }*
  5964. void clif_item_identify_list(map_session_data *sd)
  5965. {
  5966. int32 i,c;
  5967. int32 fd;
  5968. nullpo_retv(sd);
  5969. fd=sd->fd;
  5970. WFIFOHEAD(fd,MAX_INVENTORY * 2 + 4);
  5971. WFIFOW(fd,0)=0x177;
  5972. for(i=c=0;i<MAX_INVENTORY;i++){
  5973. if(sd->inventory.u.items_inventory[i].nameid > 0 && !sd->inventory.u.items_inventory[i].identify){
  5974. WFIFOW(fd,c*2+4)=client_index(i);
  5975. c++;
  5976. }
  5977. }
  5978. if(c > 0) {
  5979. WFIFOW(fd,2)=c*2+4;
  5980. WFIFOSET(fd,WFIFOW(fd,2));
  5981. sd->menuskill_id = MC_IDENTIFY;
  5982. sd->menuskill_val = c;
  5983. sd->state.workinprogress = WIP_DISABLE_ALL;
  5984. }
  5985. }
  5986. /// Notifies the client about the result of a item identify request.
  5987. /// 0179 <index>.W <result>.B (ZC_ACK_ITEMIDENTIFY)
  5988. /// result:
  5989. /// 0 = success
  5990. /// 1 = failure
  5991. void clif_item_identified( map_session_data& sd, int32 idx, bool failure ){
  5992. PACKET_ZC_ACK_ITEMIDENTIFY packet{};
  5993. packet.packetType = HEADER_ZC_ACK_ITEMIDENTIFY;
  5994. packet.index = client_index( idx );
  5995. packet.result = failure;
  5996. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  5997. }
  5998. /// Presents a list of items that can be repaired.
  5999. /// 01fc <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }* (ZC_REPAIRITEMLIST)
  6000. void clif_item_repair_list( map_session_data& sd, map_session_data& dstsd, uint16 lv ){
  6001. PACKET_ZC_REPAIRITEMLIST* p = reinterpret_cast<PACKET_ZC_REPAIRITEMLIST*>( packet_buffer );
  6002. p->packetType = HEADER_ZC_REPAIRITEMLIST;
  6003. p->packetLength = sizeof( *p );
  6004. size_t c = 0;
  6005. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  6006. 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)){
  6007. p->items[c].index = static_cast<decltype( p->items[0].index )>( i );
  6008. p->items[c].itemId = client_nameid( dstsd.inventory.u.items_inventory[i].nameid );
  6009. p->items[c].refine = dstsd.inventory.u.items_inventory[i].refine;
  6010. clif_addcards( &p->items[c].slot, &dstsd.inventory.u.items_inventory[i] );
  6011. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  6012. c++;
  6013. }
  6014. }
  6015. if( c > 0 ){
  6016. clif_send( p, p->packetLength, &sd.bl, SELF );
  6017. sd.menuskill_id = BS_REPAIRWEAPON;
  6018. sd.menuskill_val = dstsd.bl.id;
  6019. sd.menuskill_val2 = lv;
  6020. }else{
  6021. clif_skill_fail( sd, sd.ud.skill_id );
  6022. }
  6023. }
  6024. /// Notifies the client about the result of a item repair request.
  6025. /// 01fe <index>.W <result>.B (ZC_ACK_ITEMREPAIR)
  6026. /// index:
  6027. /// ignored (inventory index)
  6028. /// result:
  6029. /// 0 = Item repair success.
  6030. /// 1 = Item repair failure.
  6031. void clif_item_repaireffect( map_session_data& sd, int32 idx, bool failure ){
  6032. PACKET_ZC_ACK_ITEMREPAIR packet{};
  6033. packet.packetType = HEADER_ZC_ACK_ITEMREPAIR;
  6034. packet.index = client_index( idx );
  6035. packet.result = failure;
  6036. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6037. }
  6038. /// Displays a message, that an equipment got damaged.
  6039. /// 02bb <equip location>.W <account id>.L (ZC_EQUIPITEM_DAMAGED)
  6040. void clif_item_damaged( map_session_data& sd, uint16 position ){
  6041. PACKET_ZC_EQUIPITEM_DAMAGED packet{};
  6042. packet.packetType = HEADER_ZC_EQUIPITEM_DAMAGED;
  6043. packet.equipLocation = position;
  6044. packet.GID = sd.bl.id;
  6045. // TODO: the packet seems to be sent to other people as well, probably party and/or guild.
  6046. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6047. }
  6048. /// Presents a list of weapon items that can be refined [Taken from jAthena] (ZC_NOTIFY_WEAPONITEMLIST).
  6049. /// 0221 <packet len>.W { <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W }*
  6050. void clif_item_refine_list( map_session_data& sd ){
  6051. PACKET_ZC_NOTIFY_WEAPONITEMLIST* p = reinterpret_cast<PACKET_ZC_NOTIFY_WEAPONITEMLIST*>( packet_buffer );
  6052. p->packetType = HEADER_ZC_NOTIFY_WEAPONITEMLIST;
  6053. p->packetLength = sizeof( *p );
  6054. uint16 skill_lv = pc_checkskill( &sd, WS_WEAPONREFINE );
  6055. int32 refine_item[MAX_WEAPON_LEVEL];
  6056. refine_item[0] = pc_search_inventory( &sd, ITEMID_PHRACON );
  6057. refine_item[1] = pc_search_inventory( &sd, ITEMID_EMVERETARCON );
  6058. refine_item[2] = refine_item[3] = pc_search_inventory( &sd, ITEMID_ORIDECON );
  6059. #ifdef RENEWAL
  6060. refine_item[4] = -1;
  6061. #endif
  6062. int32 count = 0;
  6063. for( int32 i = 0; i < MAX_INVENTORY; i++ ){
  6064. if( sd.inventory.u.items_inventory[i].nameid > 0 && sd.inventory.u.items_inventory[i].refine < skill_lv &&
  6065. sd.inventory_data[i] != nullptr && sd.inventory_data[i]->type == IT_WEAPON &&
  6066. sd.inventory.u.items_inventory[i].identify && sd.inventory_data[i]->weapon_level >= 1 &&
  6067. refine_item[sd.inventory_data[i]->weapon_level - 1] != -1 && !( sd.inventory.u.items_inventory[i].equip & EQP_ARMS ) ){
  6068. p->items[count].index = client_index( i );
  6069. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  6070. p->items[count].refine = sd.inventory.u.items_inventory[i].refine;
  6071. clif_addcards( &p->items[count].slot, &sd.inventory.u.items_inventory[i] );
  6072. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  6073. count++;
  6074. }
  6075. }
  6076. clif_send( p, p->packetLength, &sd.bl, SELF );
  6077. if( count > 0 ){
  6078. sd.menuskill_id = WS_WEAPONREFINE;
  6079. sd.menuskill_val = skill_lv;
  6080. }
  6081. }
  6082. /// Notification of an auto-casted skill (ZC_AUTORUN_SKILL).
  6083. /// 0147 <skill id>.W <type>.L <level>.W <sp cost>.W <atk range>.W <skill name>.24B <upgradable>.B
  6084. void clif_item_skill( map_session_data *sd, uint16 skill_id, uint16 skill_lv ){
  6085. nullpo_retv( sd );
  6086. PACKET_ZC_AUTORUN_SKILL p = {};
  6087. p.packetType = HEADER_ZC_AUTORUN_SKILL;
  6088. p.skill_id = skill_id;
  6089. p.skill_type = skill_get_inf( skill_id );
  6090. p.skill_lv = skill_lv;
  6091. p.skill_sp = skill_get_sp( skill_id, skill_lv );
  6092. p.skill_range = skill_get_range2( &sd->bl, skill_id, skill_lv, false );
  6093. safestrncpy( p.skill_name, skill_get_name( skill_id ), NAME_LENGTH );
  6094. p.up_flag = false;
  6095. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6096. }
  6097. /// Adds an item to character's cart.
  6098. /// 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)
  6099. /// 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)
  6100. /// 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)
  6101. void clif_cart_additem( map_session_data *sd, int32 n, int32 amount ){
  6102. nullpo_retv( sd );
  6103. int32 fd = sd->fd;
  6104. if( !session_isActive( fd ) ){
  6105. return;
  6106. }
  6107. if( n < 0 || n >= MAX_CART || sd->cart.u.items_cart[n].nameid == 0 ){
  6108. return;
  6109. }
  6110. PACKET_ZC_ADD_ITEM_TO_CART p = {};
  6111. p.packetType = HEADER_ZC_ADD_ITEM_TO_CART;
  6112. p.index = client_index( n );
  6113. p.amount = amount;
  6114. p.itemId = client_nameid( sd->cart.u.items_cart[n].nameid );
  6115. #if PACKETVER >= 5
  6116. p.itemType = itemdb_type( sd->cart.u.items_cart[n].nameid );
  6117. #endif
  6118. p.identified = sd->cart.u.items_cart[n].identify;
  6119. p.damaged = sd->cart.u.items_cart[n].attribute;
  6120. p.refine = sd->cart.u.items_cart[n].refine;
  6121. clif_addcards( &p.slot, &sd->cart.u.items_cart[n] );
  6122. #if PACKETVER >= 20150226
  6123. clif_add_random_options( p.option_data, sd->cart.u.items_cart[n] );
  6124. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6125. p.grade = sd->cart.u.items_cart[n].enchantgrade;
  6126. #endif
  6127. #endif
  6128. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6129. }
  6130. /// Acknowledge an item have been added to cart
  6131. /// 012c <result>.B (ZC_ACK_ADDITEM_TO_CART)
  6132. /// result:
  6133. /// 0 = ADDITEM_TO_CART_FAIL_WEIGHT
  6134. /// 1 = ADDITEM_TO_CART_FAIL_COUNT
  6135. void clif_cart_additem_ack( map_session_data& sd, e_ack_additem_to_cart flag ){
  6136. PACKET_ZC_ACK_ADDITEM_TO_CART packet{};
  6137. packet.packetType = HEADER_ZC_ACK_ADDITEM_TO_CART;
  6138. packet.result = static_cast<decltype(packet.result)>(flag);
  6139. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6140. }
  6141. // 09B7 <unknow data> (ZC_ACK_OPEN_BANKING)
  6142. void clif_bank_open( map_session_data& sd ){
  6143. #if PACKETVER >= 20130717
  6144. PACKET_ZC_ACK_OPEN_BANKING p = {};
  6145. p.packetType = HEADER_ZC_ACK_OPEN_BANKING;
  6146. p.unknown = 0;
  6147. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6148. #endif
  6149. }
  6150. /*
  6151. * Request to Open the banking system
  6152. * 09B6 <aid>L (CZ_REQ_OPEN_BANKING)
  6153. */
  6154. void clif_parse_BankOpen(int32 fd, map_session_data* sd) {
  6155. #if PACKETVER >= 20130717
  6156. const PACKET_CZ_REQ_OPEN_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BANKING*>( RFIFOP( fd, 0 ) );
  6157. //TODO check if preventing trade or stuff like that
  6158. //also mark something in case char ain't available for saving, should we check now ?
  6159. nullpo_retv(sd);
  6160. if( !battle_config.feature_banking ) {
  6161. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6162. return;
  6163. }
  6164. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6165. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6166. return;
  6167. }
  6168. else {
  6169. if(sd->status.account_id == p->AID){
  6170. sd->state.banking = 1;
  6171. //request save ?
  6172. //chrif_bankdata_request(sd->status.account_id, sd->status.char_id);
  6173. //on succes open bank ?
  6174. clif_bank_open( *sd );
  6175. }
  6176. }
  6177. #endif
  6178. }
  6179. // 09B9 <unknow data> (ZC_ACK_CLOSE_BANKING)
  6180. void clif_bank_close( map_session_data& sd ){
  6181. #if PACKETVER >= 20130717
  6182. PACKET_ZC_ACK_CLOSE_BANKING p = {};
  6183. p.packetType = HEADER_ZC_ACK_CLOSE_BANKING;
  6184. p.unknown = 0;
  6185. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6186. #endif
  6187. }
  6188. /*
  6189. * Request to close the banking system
  6190. * 09B8 <aid>L (CZ_REQ_CLOSE_BANKING)
  6191. */
  6192. void clif_parse_BankClose(int32 fd, map_session_data* sd) {
  6193. #if PACKETVER >= 20130717
  6194. const PACKET_CZ_REQ_CLOSE_BANKING* p = reinterpret_cast<PACKET_CZ_REQ_CLOSE_BANKING*>( RFIFOP( fd, 0 ) );
  6195. nullpo_retv(sd);
  6196. if( !battle_config.feature_banking ) {
  6197. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6198. //still allow to go trough to not stuck player if we have disable it while they was in
  6199. }
  6200. if(sd->status.account_id == p->AID){
  6201. sd->state.banking = 0;
  6202. clif_bank_close( *sd );
  6203. }
  6204. #endif
  6205. }
  6206. /*
  6207. * Display how much we got in bank (I suppose)
  6208. * 09A6 <Bank_Vault>Q <Reason>W (ZC_BANKING_CHECK)
  6209. */
  6210. void clif_Bank_Check( map_session_data& sd ){
  6211. #if PACKETVER >= 20130717
  6212. PACKET_ZC_BANKING_CHECK p = {};
  6213. p.packetType = HEADER_ZC_BANKING_CHECK;
  6214. p.money = sd.bank_vault;
  6215. p.reason = 0;
  6216. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6217. #endif
  6218. }
  6219. /*
  6220. * Requesting the data in bank
  6221. * 09AB <aid>L (CZ_REQ_BANKING_CHECK)
  6222. */
  6223. void clif_parse_BankCheck(int32 fd, map_session_data* sd) {
  6224. #if PACKETVER >= 20130717
  6225. const PACKET_CZ_REQ_BANKING_CHECK* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_CHECK*>( RFIFOP( fd, 0 ) );
  6226. nullpo_retv(sd);
  6227. if( !battle_config.feature_banking ) {
  6228. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6229. return;
  6230. }
  6231. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6232. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6233. return;
  6234. }
  6235. else {
  6236. if(sd->status.account_id == p->AID) //since we have it let check it for extra security
  6237. clif_Bank_Check( *sd );
  6238. }
  6239. #endif
  6240. }
  6241. /*
  6242. * Acknowledge of deposit some money in bank
  6243. 09A8 <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_DEPOSIT)
  6244. */
  6245. void clif_bank_deposit( map_session_data& sd, enum e_BANKING_DEPOSIT_ACK reason ){
  6246. #if PACKETVER >= 20130717
  6247. PACKET_ZC_ACK_BANKING_DEPOSIT p = {};
  6248. p.packetType = HEADER_ZC_ACK_BANKING_DEPOSIT;
  6249. p.money = sd.bank_vault;
  6250. p.zeny = sd.status.zeny;
  6251. p.reason = reason;
  6252. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6253. #endif
  6254. }
  6255. /*
  6256. * Request saving some money in bank
  6257. * @author : original [Yommy/Hercules]
  6258. * 09A7 <AID>L <Money>L (CZ_REQ_BANKING_DEPOSIT)
  6259. */
  6260. void clif_parse_BankDeposit(int32 fd, map_session_data* sd) {
  6261. #if PACKETVER >= 20130717
  6262. const PACKET_CZ_REQ_BANKING_DEPOSIT* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_DEPOSIT*>( RFIFOP( fd, 0 ) );
  6263. nullpo_retv(sd);
  6264. if( !battle_config.feature_banking ) {
  6265. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6266. return;
  6267. }
  6268. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6269. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6270. return;
  6271. }
  6272. else {
  6273. if(sd->status.account_id == p->AID){
  6274. enum e_BANKING_DEPOSIT_ACK reason = pc_bank_deposit(sd,max(0,p->zeny));
  6275. clif_bank_deposit( *sd, reason );
  6276. }
  6277. }
  6278. #endif
  6279. }
  6280. /*
  6281. * Acknowledge of withdrawing some money from bank
  6282. * 09AA <Reason>W <Money>Q <balance>L (ZC_ACK_BANKING_WITHDRAW)
  6283. */
  6284. void clif_bank_withdraw( map_session_data& sd, enum e_BANKING_WITHDRAW_ACK reason ){
  6285. #if PACKETVER >= 20130717
  6286. PACKET_ZC_ACK_BANKING_WITHDRAW p = {};
  6287. p.packetType = HEADER_ZC_ACK_BANKING_WITHDRAW;
  6288. p.reason = reason;
  6289. p.money = sd.bank_vault;
  6290. p.zeny = sd.status.zeny;
  6291. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6292. #endif
  6293. }
  6294. /*
  6295. * Request Withdrawing some money from bank
  6296. * 09A9 <AID>L <Money>L (CZ_REQ_BANKING_WITHDRAW)
  6297. */
  6298. void clif_parse_BankWithdraw(int32 fd, map_session_data* sd) {
  6299. #if PACKETVER >= 20130717
  6300. const PACKET_CZ_REQ_BANKING_WITHDRAW* p = reinterpret_cast<PACKET_CZ_REQ_BANKING_WITHDRAW*>( RFIFOP( fd, 0 ) );
  6301. nullpo_retv(sd);
  6302. if( !battle_config.feature_banking ) {
  6303. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1496),false,SELF); //Banking is disabled
  6304. return;
  6305. }
  6306. if( map_getmapflag( sd->bl.m, MF_NOBANK ) ){
  6307. clif_displaymessage( sd->fd, msg_txt( sd, 831 ) ); // You cannot use the Bank on this map.
  6308. return;
  6309. }
  6310. else {
  6311. if(sd->status.account_id == p->AID){
  6312. enum e_BANKING_WITHDRAW_ACK reason = pc_bank_withdraw(sd,max(0,p->zeny));
  6313. clif_bank_withdraw( *sd, reason );
  6314. }
  6315. }
  6316. #endif
  6317. }
  6318. /// Deletes an item from character's cart.
  6319. /// 0125 <index>.W <amount>.L (ZC_DELETE_ITEM_FROM_CART)
  6320. void clif_cart_delitem( map_session_data& sd, int32 index, int32 amount ){
  6321. PACKET_ZC_DELETE_ITEM_FROM_CART packet{};
  6322. packet.packetType = HEADER_ZC_DELETE_ITEM_FROM_CART;
  6323. packet.index = client_index( index );
  6324. packet.amount = amount;
  6325. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6326. }
  6327. /// Opens the shop creation menu.
  6328. /// 012d <num>.W (ZC_OPENSTORE)
  6329. /// num:
  6330. /// number of allowed item slots
  6331. void clif_openvendingreq( map_session_data& sd, uint16 num ){
  6332. PACKET_ZC_OPENSTORE packet{};
  6333. packet.packetType = HEADER_ZC_OPENSTORE;
  6334. packet.num = num;
  6335. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6336. }
  6337. /// Displays a vending board to target/area (ZC_STORE_ENTRY).
  6338. /// 0131 <owner id>.L <message>.80B
  6339. void clif_showvendingboard( map_session_data& sd, enum send_target target, struct block_list* tbl ){
  6340. if( tbl == nullptr ){
  6341. tbl = &sd.bl;
  6342. target = AREA_WOS;
  6343. }
  6344. PACKET_ZC_STORE_ENTRY p = {};
  6345. p.packetType = HEADER_ZC_STORE_ENTRY;
  6346. p.makerAID = sd.status.account_id;
  6347. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  6348. clif_send( &p, sizeof( p ), tbl, target );
  6349. }
  6350. /// Removes a vending board from screen (ZC_DISAPPEAR_ENTRY).
  6351. /// 0132 <owner id>.L
  6352. void clif_closevendingboard(struct block_list* bl, int32 fd)
  6353. {
  6354. unsigned char buf[16];
  6355. nullpo_retv(bl);
  6356. WBUFW(buf,0) = 0x132;
  6357. WBUFL(buf,2) = bl->id;
  6358. if( session_isActive(fd) ) {
  6359. WFIFOHEAD(fd,packet_len(0x132));
  6360. memcpy(WFIFOP(fd,0),buf,packet_len(0x132));
  6361. WFIFOSET(fd,packet_len(0x132));
  6362. } else {
  6363. clif_send(buf,packet_len(0x132),bl,AREA_WOS);
  6364. }
  6365. }
  6366. /// Sends a list of items in a shop.
  6367. /// 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)
  6368. /// 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)
  6369. void clif_vendinglist( map_session_data& sd, map_session_data& vsd ){
  6370. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC*>( packet_buffer );
  6371. p->packetType = HEADER_ZC_PC_PURCHASE_ITEMLIST_FROMMC;
  6372. p->packetLength = sizeof( *p );
  6373. p->AID = vsd.status.account_id;
  6374. #if PACKETVER >= 20100105
  6375. p->venderId = vsd.vender_id;
  6376. #endif
  6377. for( int32 i = 0, count = 0; i < vsd.vend_num; i++ ){
  6378. PACKET_ZC_PC_PURCHASE_ITEMLIST_FROMMC_sub& entry = p->items[count];
  6379. int32 index = vsd.vending[i].index;
  6380. struct item_data* data = itemdb_search( vsd.cart.u.items_cart[index].nameid );
  6381. if( data == nullptr ){
  6382. continue;
  6383. }
  6384. entry.price = vsd.vending[i].value;
  6385. entry.amount = vsd.vending[i].amount;
  6386. entry.index = client_index( index );
  6387. entry.itemType = itemtype( vsd.cart.u.items_cart[index].nameid );
  6388. entry.itemId = client_nameid( vsd.cart.u.items_cart[index].nameid );
  6389. entry.identified = vsd.cart.u.items_cart[index].identify;
  6390. entry.damaged = vsd.cart.u.items_cart[index].attribute;
  6391. entry.refine = vsd.cart.u.items_cart[index].refine;
  6392. clif_addcards( &entry.slot, &vsd.cart.u.items_cart[index] );
  6393. #if PACKETVER >= 20150226
  6394. clif_add_random_options( entry.option_data, vsd.cart.u.items_cart[index] );
  6395. #if PACKETVER >= 20160921
  6396. entry.location = pc_equippoint_sub( &sd, data );
  6397. entry.viewSprite = data->look;
  6398. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6399. entry.grade = vsd.cart.u.items_cart[index].enchantgrade;
  6400. #endif
  6401. #endif
  6402. #endif
  6403. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6404. count++;
  6405. }
  6406. clif_send( p, p->packetLength, &sd.bl, SELF );
  6407. }
  6408. /// Shop purchase failure.
  6409. /// 0135 <index>.W <amount>.W <result>.B (ZC_PC_PURCHASE_RESULT_FROMMC)
  6410. /// result:
  6411. /// 0 = success
  6412. /// 1 = not enough zeny
  6413. /// 2 = overweight
  6414. /// 4 = out of stock
  6415. /// 5 = "cannot use an npc shop while in a trade"
  6416. /// 6 = Because the store information was incorrect the item was not purchased.
  6417. /// 7 = No sales information.
  6418. void clif_buyvending( map_session_data& sd, uint16 index, uint16 amount, e_pc_purchase_result_frommc result ){
  6419. PACKET_ZC_PC_PURCHASE_RESULT_FROMMC packet{};
  6420. packet.packetType = HEADER_ZC_PC_PURCHASE_RESULT_FROMMC;
  6421. packet.index = client_index( index );
  6422. packet.amount = amount;
  6423. packet.result = static_cast<decltype(packet.result)>(result);
  6424. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6425. }
  6426. /// Show's vending player its list of items for sale.
  6427. /// 0a28 <result>.B (ZC_ACK_OPENSTORE2)
  6428. void clif_openvending_ack( map_session_data& sd, e_ack_openstore2 result ){
  6429. #if PACKETVER >= 20141022
  6430. PACKET_ZC_ACK_OPENSTORE2 packet{};
  6431. packet.packetType = HEADER_ZC_ACK_OPENSTORE2;
  6432. packet.result = static_cast<decltype(packet.result)>(result);
  6433. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6434. #endif
  6435. }
  6436. /// Shop creation success.
  6437. /// 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)
  6438. void clif_openvending( map_session_data& sd ){
  6439. PACKET_ZC_PC_PURCHASE_MYITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_PURCHASE_MYITEMLIST*>( packet_buffer );
  6440. p->packetType = HEADER_ZC_PC_PURCHASE_MYITEMLIST;
  6441. p->packetLength = sizeof( *p );
  6442. p->AID = sd.bl.id;
  6443. for( int32 i = 0; i < sd.vend_num; i++ ) {
  6444. PACKET_ZC_PC_PURCHASE_MYITEMLIST_sub& entry = p->items[i];
  6445. int16 index = sd.vending[i].index;
  6446. entry.price = sd.vending[i].value;
  6447. entry.index = client_index( index );
  6448. entry.amount = sd.vending[i].amount;
  6449. entry.itemType = itemtype( sd.cart.u.items_cart[index].nameid );
  6450. entry.itemId = client_nameid( sd.cart.u.items_cart[index].nameid );
  6451. entry.identified = sd.cart.u.items_cart[index].identify;
  6452. entry.damaged = sd.cart.u.items_cart[index].attribute;
  6453. entry.refine = sd.cart.u.items_cart[index].refine;
  6454. clif_addcards( &entry.slot, &sd.cart.u.items_cart[index] );
  6455. #if PACKETVER >= 20150226
  6456. clif_add_random_options( entry.option_data, sd.cart.u.items_cart[index] );
  6457. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  6458. entry.grade = sd.cart.u.items_cart[index].enchantgrade;
  6459. #endif
  6460. #endif
  6461. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  6462. }
  6463. clif_send( p, p->packetLength, &sd.bl, SELF );
  6464. clif_openvending_ack( sd, OPENSTORE2_SUCCESS );
  6465. }
  6466. /// Inform merchant that someone has bought an item.
  6467. /// 0137 <index>.W <amount>.W (ZC_DELETEITEM_FROM_MCSTORE)
  6468. /// 09e5 <index>.W <amount>.W <GID>.L <Date>.L <zeny>.L (ZC_DELETEITEM_FROM_MCSTORE2)
  6469. void clif_vendingreport( map_session_data& sd, uint16 index, uint16 amount, uint32 char_id, int32 zeny ){
  6470. PACKET_ZC_DELETEITEM_FROM_MCSTORE packet{};
  6471. packet.packetType = HEADER_ZC_DELETEITEM_FROM_MCSTORE;
  6472. packet.index = client_index( index );
  6473. packet.amount = amount;
  6474. // TODO : not sure for client date [Napster]
  6475. #if PACKETVER >= 20141016
  6476. packet.buyerCID = char_id;
  6477. packet.date = client_tick( time(nullptr) );
  6478. packet.zeny = zeny;
  6479. #endif
  6480. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6481. }
  6482. /// Result of organizing a party (ZC_ACK_MAKE_GROUP).
  6483. /// 00fa <result>.B
  6484. /// result:
  6485. /// 0 = opens party window and shows MsgStringTable[77]="party successfully organized"
  6486. /// 1 = MsgStringTable[78]="party name already exists"
  6487. /// 2 = MsgStringTable[79]="already in a party"
  6488. /// 3 = cannot organize parties on this map
  6489. /// ? = nothing
  6490. void clif_party_created( map_session_data& sd, int32 result ){
  6491. PACKET_ZC_ACK_MAKE_GROUP p = {};
  6492. p.PacketType = HEADER_ZC_ACK_MAKE_GROUP;
  6493. p.result = result;
  6494. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6495. }
  6496. /// Adds new member to a party.
  6497. /// 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)
  6498. /// 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)
  6499. /// 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)
  6500. /// role:
  6501. /// 0 = leader
  6502. /// 1 = normal
  6503. /// state:
  6504. /// 0 = connected
  6505. /// 1 = disconnected
  6506. void clif_party_member_info( struct party_data& party, map_session_data& sd ){
  6507. int32 id = party_getmemberid( &party, &sd );
  6508. if( id < 0 ){
  6509. return;
  6510. }
  6511. PACKET_ZC_ADD_MEMBER_TO_GROUP p = {};
  6512. p.packetType = partymemberinfo;
  6513. p.AID = sd.status.account_id;
  6514. #if PACKETVER >= 20171207
  6515. p.GID = sd.status.char_id;
  6516. #endif
  6517. p.leader = ( party.party.member[id].leader != 0 ) ? 0 : 1;
  6518. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6519. p.class_ = sd.status.class_;
  6520. p.baseLevel = sd.status.base_level;
  6521. #endif
  6522. p.x = sd.bl.x;
  6523. p.y = sd.bl.y;
  6524. p.offline = ( party.party.member[id].online != 0 ) ? 0 : 1;
  6525. safestrncpy( p.partyName, party.party.name, sizeof( p.partyName ) );
  6526. safestrncpy( p.playerName, sd.status.name, sizeof( p.playerName ) );
  6527. mapindex_getmapname_ext( map_mapid2mapname( sd.bl.m ), p.mapName );
  6528. p.sharePickup = ( party.party.item&1 ) ? 1 : 0;
  6529. p.shareLoot = ( party.party.item&2 )? 1 : 0;
  6530. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6531. }
  6532. /// Sends party information (ZC_GROUP_LIST).
  6533. /// 00fb <packet len>.W <party name>.24B { <account id>.L <nick>.24B <map name>.16B <role>.B <state>.B }*
  6534. /// role:
  6535. /// 0 = leader
  6536. /// 1 = normal
  6537. /// state:
  6538. /// 0 = connected
  6539. /// 1 = disconnected
  6540. /// 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
  6541. void clif_party_info( struct party_data& party, map_session_data* sd ){
  6542. send_target target;
  6543. if( sd == nullptr ){
  6544. sd = party_getavailablesd( &party );
  6545. if( sd == nullptr ){
  6546. // Nobody is online to notify
  6547. return;
  6548. }
  6549. // Send to whole party
  6550. target = PARTY;
  6551. }else{
  6552. // Send only to self
  6553. target = SELF;
  6554. }
  6555. PACKET_ZC_GROUP_LIST* p = reinterpret_cast<PACKET_ZC_GROUP_LIST*>( packet_buffer );
  6556. p->packetType = partyinfo;
  6557. p->packetLen = sizeof( *p );
  6558. safestrncpy( p->partyName, party.party.name, sizeof( p->partyName ) );
  6559. for( int32 i = 0, c = 0; i < MAX_PARTY; i++ ){
  6560. struct party_member& m = party.party.member[i];
  6561. if( m.account_id == 0 ){
  6562. continue;
  6563. }
  6564. PACKET_ZC_GROUP_LIST_SUB& member = p->members[c];
  6565. member.AID = m.account_id;
  6566. #if PACKETVER >= 20171207
  6567. member.GID = m.char_id;
  6568. #endif
  6569. safestrncpy( member.playerName, m.name, sizeof( member.playerName ) );
  6570. mapindex_getmapname_ext( m.map, member.mapName );
  6571. member.leader = ( m.leader ) ? 0 : 1;
  6572. member.offline = ( m.online ) ? 0 : 1;
  6573. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6574. member.class_ = m.class_;
  6575. member.baseLevel = m.lv;
  6576. #endif
  6577. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( member ) );
  6578. c++;
  6579. }
  6580. clif_send( p, p->packetLen, &sd->bl, target );
  6581. }
  6582. /// The player's 'party invite' state, sent during login (ZC_PARTY_CONFIG).
  6583. /// 02c9 <flag>.B
  6584. /// flag:
  6585. /// 0 = allow party invites
  6586. /// 1 = auto-deny party invites
  6587. void clif_partyinvitationstate( map_session_data& sd ){
  6588. #if PACKETVER_MAIN_NUM >= 20070911 || defined(PACKETVER_RE) || PACKETVER_AD_NUM >= 20070911 || PACKETVER_SAK_NUM >= 20070904 || defined(PACKETVER_ZERO)
  6589. PACKET_ZC_PARTY_CONFIG p = {};
  6590. p.packetType = HEADER_ZC_PARTY_CONFIG;
  6591. p.denyPartyInvites = sd.status.disable_partyinvite;
  6592. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6593. #endif
  6594. }
  6595. /// Party invitation request.
  6596. /// 00fe <party id>.L <party name>.24B (ZC_REQ_JOIN_GROUP)
  6597. /// 02c6 <party id>.L <party name>.24B (ZC_PARTY_JOIN_REQ)
  6598. void clif_party_invite( map_session_data& sd, map_session_data& tsd ){
  6599. struct party_data* party = party_search( sd.status.party_id );
  6600. if( party == nullptr ){
  6601. return;
  6602. }
  6603. PACKET_ZC_PARTY_JOIN_REQ p = {};
  6604. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ;
  6605. p.GRID = party->party.party_id;
  6606. safestrncpy( p.groupName, party->party.name, sizeof( p.groupName ) );
  6607. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6608. }
  6609. /// Party invite result.
  6610. /// 00fd <nick>.24S <result>.B (ZC_ACK_REQ_JOIN_GROUP)
  6611. /// 02c5 <nick>.24S <result>.L (ZC_PARTY_JOIN_REQ_ACK)
  6612. /// result=0 : char is already in a party -> MsgStringTable[80]
  6613. /// result=1 : party invite was rejected -> MsgStringTable[81]
  6614. /// result=2 : party invite was accepted -> MsgStringTable[82]
  6615. /// result=3 : party is full -> MsgStringTable[83]
  6616. /// result=4 : char of the same account already joined the party -> MsgStringTable[608]
  6617. /// result=5 : char blocked party invite -> MsgStringTable[1324] (since 20070904)
  6618. /// result=7 : char is not online or doesn't exist -> MsgStringTable[71] (since 20070904)
  6619. /// result=8 : (%s) are currently in restricted map to join a party. -> MsgStringTable[1388] (since 20080527)
  6620. /// result=9 : Cannot join a party in this map. -> MsgStringTable[1871] (since 20110215)
  6621. /// result=10 : You cannot invite or withdraw while in memorial dungeon -> message: MSG_ID_BD3 (since 20161130)
  6622. /// result=11 : The character is a level that can not join the party -> message: MSG_ID_C9A (since 20170412)
  6623. void clif_party_invite_reply( map_session_data& sd, const char* nick, enum e_party_invite_reply reply ){
  6624. #if PACKETVER < 20070904
  6625. if( reply == PARTY_REPLY_OFFLINE ){
  6626. clif_displaymessage( sd.fd, msg_txt( &sd, 3 ) ); // Character not found.
  6627. return;
  6628. }
  6629. #endif
  6630. PACKET_ZC_PARTY_JOIN_REQ_ACK p = {};
  6631. p.PacketType = HEADER_ZC_PARTY_JOIN_REQ_ACK;
  6632. safestrncpy( p.characterName, nick, sizeof( p.characterName ) );
  6633. p.result = reply;
  6634. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6635. }
  6636. /// Updates party settings.
  6637. /// 0101 <exp option>.L (ZC_GROUPINFO_CHANGE)
  6638. /// 07d8 <exp option>.L <item pick rule>.B <item share rule>.B (ZC_REQ_GROUPINFO_CHANGE_V2)
  6639. /// exp option:
  6640. /// 0 = exp sharing disabled
  6641. /// 1 = exp sharing enabled
  6642. /// 2 = cannot change exp sharing
  6643. ///
  6644. /// flag:
  6645. /// 0 = send to party
  6646. /// 1 = send to sd
  6647. void clif_party_option(struct party_data *p,map_session_data *sd,int32 flag)
  6648. {
  6649. unsigned char buf[16];
  6650. #if PACKETVER < 20090603
  6651. const int32 cmd = 0x101;
  6652. #else
  6653. const int32 cmd = 0x7d8;
  6654. #endif
  6655. nullpo_retv(p);
  6656. if(!sd && flag==0){
  6657. int32 i;
  6658. ARR_FIND(0,MAX_PARTY,i,p->data[i].sd);
  6659. if (i < MAX_PARTY)
  6660. sd = p->data[i].sd;
  6661. }
  6662. if(!sd) return;
  6663. WBUFW(buf,0)=cmd;
  6664. WBUFL(buf,2)=((flag&0x01)?2:p->party.exp);
  6665. #if PACKETVER >= 20090603
  6666. WBUFB(buf,6)=(p->party.item&1)?1:0;
  6667. WBUFB(buf,7)=(p->party.item&2)?1:0;
  6668. #endif
  6669. if(flag==0)
  6670. clif_send(buf,packet_len(cmd),&sd->bl,PARTY);
  6671. else
  6672. clif_send(buf,packet_len(cmd),&sd->bl,SELF);
  6673. }
  6674. /**
  6675. * 0105 <account id>.L <char name>.24B <result>.B (ZC_DELETE_MEMBER_FROM_GROUP).
  6676. * @param sd Send the notification for this player
  6677. * @param account_id Account ID of kicked member
  6678. * @param name Name of kicked member
  6679. * @param result Party leave result @see PARTY_MEMBER_WITHDRAW
  6680. * @param target Send target
  6681. **/
  6682. void clif_party_withdraw( map_session_data& sd, uint32 account_id, const char* name, enum e_party_member_withdraw result, enum send_target target ){
  6683. PACKET_ZC_DELETE_MEMBER_FROM_GROUP p = {};
  6684. p.PacketType = HEADER_ZC_DELETE_MEMBER_FROM_GROUP;
  6685. p.AID = account_id;
  6686. safestrncpy( p.characterName, name, sizeof( p.characterName ) );
  6687. p.result = result;
  6688. clif_send( &p, sizeof( p ), &sd.bl, target );
  6689. }
  6690. /// Party chat message (ZC_NOTIFY_CHAT_PARTY).
  6691. /// 0109 <packet len>.W <account id>.L <message>.?B
  6692. void clif_party_message( struct party_data& party, uint32 account_id, const char* mes, size_t len ){
  6693. map_session_data* sd = party_getavailablesd( &party );
  6694. if( sd == nullptr ){
  6695. return;
  6696. }
  6697. if (len > CHAT_SIZE_MAX) {
  6698. ShowWarning( "clif_party_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", party_id=%d).\n", mes, len, CHAT_SIZE_MAX, party.party.party_id );
  6699. len = CHAT_SIZE_MAX;
  6700. }
  6701. PACKET_ZC_NOTIFY_CHAT_PARTY* p = reinterpret_cast<PACKET_ZC_NOTIFY_CHAT_PARTY*>( packet_buffer );
  6702. p->PacketType = HEADER_ZC_NOTIFY_CHAT_PARTY;
  6703. p->PacketLength = sizeof( *p );
  6704. p->AID = account_id;
  6705. safestrncpy( p->chatMsg, mes, len );
  6706. p->PacketLength += static_cast<decltype(p->PacketLength)>( len );
  6707. clif_send( p, p->PacketLength, &sd->bl, PARTY );
  6708. }
  6709. /// Updates the position of a party member on the minimap (ZC_NOTIFY_POSITION_TO_GROUPM).
  6710. /// 0107 <account id>.L <x>.W <y>.W
  6711. void clif_party_xy( map_session_data& sd ){
  6712. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6713. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6714. p.AID = sd.status.account_id;
  6715. p.xPos = sd.bl.x;
  6716. p.yPos = sd.bl.y;
  6717. clif_send( &p, sizeof( p ), &sd.bl, PARTY_SAMEMAP_WOS );
  6718. }
  6719. /*==========================================
  6720. * Sends x/y dot to a single fd. [Skotlex]
  6721. *------------------------------------------*/
  6722. void clif_party_xy_single( map_session_data& sd, map_session_data& tsd ){
  6723. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p = {};
  6724. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  6725. p.AID = sd.status.account_id;
  6726. p.xPos = sd.bl.x;
  6727. p.yPos = sd.bl.y;
  6728. clif_send( &p, sizeof( p ), &tsd.bl, SELF );
  6729. }
  6730. /// Updates HP bar of a party member.
  6731. /// 0106 <account id>.L <hp>.W <max hp>.W (ZC_NOTIFY_HP_TO_GROUPM)
  6732. /// 080e <account id>.L <hp>.L <max hp>.L (ZC_NOTIFY_HP_TO_GROUPM_R2)
  6733. void clif_party_hp( map_session_data& sd ){
  6734. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6735. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6736. p.AID = sd.status.account_id;
  6737. #if PACKETVER < 20100119
  6738. // To correctly display the %hp bar. [Skotlex]
  6739. if( sd.battle_status.max_hp > INT16_MAX ){
  6740. p.hp = sd.battle_status.hp / ( sd.battle_status.max_hp / 100 );
  6741. p.maxhp = 100;
  6742. }else{
  6743. p.hp = sd.battle_status.hp;
  6744. p.maxhp = sd.battle_status.max_hp;
  6745. }
  6746. #else
  6747. p.hp = sd.battle_status.hp;
  6748. p.maxhp = sd.battle_status.max_hp;
  6749. #endif
  6750. clif_send( &p, sizeof( p ), &sd.bl, PARTY_AREA_WOS );
  6751. }
  6752. /// Notifies the party members of a character's death or revival.
  6753. /// 0AB2 <GID>.L <dead>.B
  6754. void clif_party_dead( map_session_data& sd ){
  6755. #if PACKETVER_MAIN_NUM >= 20170524 || PACKETVER_RE_NUM >= 20170502 || defined(PACKETVER_ZERO)
  6756. PACKET_ZC_GROUP_ISALIVE p = {};
  6757. p.packetType = HEADER_ZC_GROUP_ISALIVE;
  6758. p.AID = sd.status.account_id;
  6759. p.isDead = pc_isdead( &sd );
  6760. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6761. #endif
  6762. }
  6763. /// Updates the job and level of a party member
  6764. /// 0abd <account id>.L <job>.W <level>.W
  6765. void clif_party_job_and_level( map_session_data& sd ){
  6766. #if PACKETVER_MAIN_NUM >= 20170502 || PACKETVER_RE_NUM >= 20170419 || defined(PACKETVER_ZERO)
  6767. PACKET_ZC_NOTIFY_MEMBERINFO_TO_GROUPM p = {};
  6768. p.PacketType = HEADER_ZC_NOTIFY_MEMBERINFO_TO_GROUPM;
  6769. p.AID = sd.status.account_id;
  6770. p.job = sd.status.class_;
  6771. p.level = sd.status.base_level;
  6772. clif_send( &p, sizeof( p ), &sd.bl, PARTY );
  6773. #endif
  6774. }
  6775. /*==========================================
  6776. * Sends HP bar to a single fd. [Skotlex]
  6777. *------------------------------------------*/
  6778. void clif_hpmeter_single( map_session_data& sd, uint32 id, uint32 hp, uint32 maxhp ){
  6779. PACKET_ZC_NOTIFY_HP_TO_GROUPM p = {};
  6780. p.PacketType = HEADER_ZC_NOTIFY_HP_TO_GROUPM;
  6781. p.AID = id;
  6782. #if PACKETVER < 20100119
  6783. // To correctly display the %hp bar. [Skotlex]
  6784. if( maxhp > INT16_MAX ){
  6785. p.hp = hp / ( maxhp / 100 );
  6786. p.maxhp = 100;
  6787. }else{
  6788. p.hp = hp;
  6789. p.maxhp = maxhp;
  6790. }
  6791. #else
  6792. p.hp = hp;
  6793. p.maxhp = maxhp;
  6794. #endif
  6795. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  6796. }
  6797. /// Notifies the client, that it's attack target is too far.
  6798. /// 0139 <target id>.L <target x>.W <target y>.W <x>.W <y>.W <atk range>.W (ZC_ATTACK_FAILURE_FOR_DISTANCE)
  6799. void clif_movetoattack( map_session_data& sd, block_list& bl ){
  6800. PACKET_ZC_ATTACK_FAILURE_FOR_DISTANCE packet{};
  6801. packet.PacketType = HEADER_ZC_ATTACK_FAILURE_FOR_DISTANCE;
  6802. packet.targetAID = bl.id;
  6803. packet.targetXPos = bl.x;
  6804. packet.targetYPos = bl.y;
  6805. packet.xPos = sd.bl.x;
  6806. packet.yPos = sd.bl.y;
  6807. packet.currentAttRange = sd.battle_status.rhw.range;
  6808. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6809. }
  6810. /// Notifies the client about the result of an item produce request (ZC_ACK_REQMAKINGITEM).
  6811. /// 018f <result>.W <name id>.W
  6812. /// result:
  6813. /// 0 = success
  6814. /// 1 = failure
  6815. /// 2 = success (alchemist)
  6816. /// 3 = failure (alchemist)
  6817. void clif_produceeffect(map_session_data* sd,int32 flag, t_itemid nameid){
  6818. nullpo_retv( sd );
  6819. clif_solved_charname( sd->fd, sd->status.char_id, sd->status.name );
  6820. PACKET_ZC_ACK_REQMAKINGITEM p = {};
  6821. p.packetType = HEADER_ZC_ACK_REQMAKINGITEM;
  6822. p.result = flag;
  6823. p.itemId = client_nameid( nameid );
  6824. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  6825. }
  6826. /// Initiates the pet taming process.
  6827. /// 019e (ZC_START_CAPTURE)
  6828. void clif_catch_process( map_session_data& sd ){
  6829. PACKET_ZC_START_CAPTURE packet{};
  6830. packet.PacketType = HEADER_ZC_START_CAPTURE;
  6831. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6832. }
  6833. /// Displays the result of a pet taming attempt.
  6834. /// 01a0 <result>.B (ZC_TRYCAPTURE_MONSTER)
  6835. /// 0 = failure
  6836. /// 1 = success
  6837. void clif_pet_roulette( map_session_data& sd, bool success ){
  6838. PACKET_ZC_TRYCAPTURE_MONSTER packet{};
  6839. packet.PacketType = HEADER_ZC_TRYCAPTURE_MONSTER;
  6840. packet.result = success;
  6841. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6842. }
  6843. /// Presents a list of pet eggs that can be hatched (ZC_PETEGG_LIST).
  6844. /// 01a6 <packet len>.W { <index>.W }*
  6845. void clif_sendegg(map_session_data *sd)
  6846. {
  6847. int32 i,n=0,fd;
  6848. nullpo_retv(sd);
  6849. fd=sd->fd;
  6850. if (battle_config.pet_no_gvg && map_flag_gvg2(sd->bl.m)) { //Disable pet hatching in GvG grounds during Guild Wars [Skotlex]
  6851. clif_displaymessage(fd, msg_txt(sd,666));
  6852. return;
  6853. }
  6854. WFIFOHEAD(fd, MAX_INVENTORY * 2 + 4);
  6855. WFIFOW(fd,0)=0x1a6;
  6856. for(i=0,n=0;i<MAX_INVENTORY;i++){
  6857. if(sd->inventory.u.items_inventory[i].nameid == 0 || sd->inventory_data[i] == nullptr ||
  6858. sd->inventory_data[i]->type != IT_PETEGG ||
  6859. sd->inventory.u.items_inventory[i].amount <= 0)
  6860. continue;
  6861. WFIFOW(fd,n*2+4)=client_index(i);
  6862. n++;
  6863. }
  6864. WFIFOW(fd,2)=4+n*2;
  6865. WFIFOSET(fd,WFIFOW(fd,2));
  6866. sd->menuskill_id = SA_TAMINGMONSTER;
  6867. sd->menuskill_val = -1;
  6868. }
  6869. /// Sends a specific pet data update (ZC_CHANGESTATE_PET).
  6870. /// 01a4 <type>.B <id>.L <data>.L
  6871. /// type:
  6872. /// 0 = pre-init (data = 0)
  6873. /// 1 = intimacy (data = 0~4)
  6874. /// 2 = hunger (data = 0~4)
  6875. /// 3 = accessory
  6876. /// 4 = performance (data = 1~3: normal, 4: special)
  6877. /// 5 = hairstyle
  6878. /// 6 = close egg selection ui and update egg in inventory (PACKETVER >= 20180704)
  6879. ///
  6880. /// If sd is null, the update is sent to nearby objects, otherwise it is sent only to that player.
  6881. void clif_send_petdata(map_session_data* sd, struct pet_data* pd, int32 type, int32 param)
  6882. {
  6883. uint8 buf[16];
  6884. nullpo_retv(pd);
  6885. WBUFW(buf,0) = 0x1a4;
  6886. WBUFB(buf,2) = type;
  6887. WBUFL(buf,3) = pd->bl.id;
  6888. WBUFL(buf,7) = param;
  6889. if (sd)
  6890. clif_send(buf, packet_len(0x1a4), &sd->bl, SELF);
  6891. else
  6892. clif_send(buf, packet_len(0x1a4), &pd->bl, AREA);
  6893. }
  6894. /// Pet's base data (ZC_PROPERTY_PET).
  6895. /// 01a2 <name>.24B <renamed>.B <level>.W <hunger>.W <intimacy>.W <accessory id>.W <class>.W
  6896. void clif_send_petstatus(map_session_data *sd)
  6897. {
  6898. int32 fd;
  6899. struct s_pet *pet;
  6900. nullpo_retv(sd);
  6901. nullpo_retv(sd->pd);
  6902. fd=sd->fd;
  6903. pet = &sd->pd->pet;
  6904. WFIFOHEAD(fd,packet_len(0x1a2));
  6905. WFIFOW(fd,0)=0x1a2;
  6906. safestrncpy(WFIFOCP(fd,2),pet->name,NAME_LENGTH);
  6907. WFIFOB(fd,26)=battle_config.pet_rename?0:pet->rename_flag;
  6908. WFIFOW(fd,27)=pet->level;
  6909. WFIFOW(fd,29)=pet->hungry;
  6910. WFIFOW(fd,31)=pet->intimate;
  6911. WFIFOW(fd,33)=pet->equip;
  6912. #if PACKETVER >= 20081126
  6913. WFIFOW(fd,35)=pet->class_;
  6914. #endif
  6915. WFIFOSET(fd,packet_len(0x1a2));
  6916. }
  6917. /// Notification about a pet's emotion/talk (ZC_PET_ACT).
  6918. /// 01aa <id>.L <data>.L
  6919. /// data:
  6920. /// @see CZ_PET_ACT.
  6921. void clif_pet_emotion(struct pet_data *pd,int32 param)
  6922. {
  6923. unsigned char buf[16];
  6924. nullpo_retv(pd);
  6925. memset(buf,0,packet_len(0x1aa));
  6926. WBUFW(buf,0)=0x1aa;
  6927. WBUFL(buf,2)=pd->bl.id;
  6928. WBUFL(buf,6)=param;
  6929. clif_send(buf,packet_len(0x1aa),&pd->bl,AREA);
  6930. }
  6931. /// Result of request to feed a pet.
  6932. /// 01a3 <result>.B <name id>.W (ZC_FEED_PET)
  6933. /// result:
  6934. /// 0 = failure
  6935. /// 1 = success
  6936. void clif_pet_food( map_session_data& sd, int32 foodid, bool success ){
  6937. PACKET_ZC_FEED_PET packet{};
  6938. packet.packetType = HEADER_ZC_FEED_PET;
  6939. packet.result = success;
  6940. packet.itemId = client_nameid( foodid );
  6941. clif_send( &packet, sizeof( packet ), &sd.bl, SELF );
  6942. }
  6943. /// Send pet auto feed info.
  6944. void clif_pet_autofeed_status(map_session_data* sd, bool force) {
  6945. #if PACKETVER >= 20141008
  6946. nullpo_retv(sd);
  6947. if (force || battle_config.pet_autofeed_always) {
  6948. // Always send ON or OFF
  6949. if (sd->pd && battle_config.feature_pet_autofeed) {
  6950. clif_configuration(sd, CONFIG_PET_AUTOFEED, sd->pd->pet.autofeed);
  6951. }
  6952. else {
  6953. clif_configuration(sd, CONFIG_PET_AUTOFEED, false);
  6954. }
  6955. }
  6956. else {
  6957. // Only send when enabled
  6958. if (sd->pd && battle_config.feature_pet_autofeed && sd->pd->pet.autofeed) {
  6959. clif_configuration(sd, CONFIG_PET_AUTOFEED, true);
  6960. }
  6961. }
  6962. #endif
  6963. }
  6964. /// Presents a list of skills that can be auto-spelled.
  6965. /// 01cd { <skill id>.L }*7 (ZC_AUTOSPELLLIST)
  6966. void clif_autospell( map_session_data& sd, uint16 skill_lv ){
  6967. struct s_autospell_requirement{
  6968. uint16 skill_id;
  6969. uint16 required_autospell_skill_lv;
  6970. };
  6971. #ifdef RENEWAL
  6972. const std::vector<s_autospell_requirement> autospell_skills = {
  6973. { MG_FIREBOLT, 0 },
  6974. { MG_COLDBOLT, 0 },
  6975. { MG_LIGHTNINGBOLT, 0 },
  6976. { MG_SOULSTRIKE, 3 },
  6977. { MG_FIREBALL, 3 },
  6978. { WZ_EARTHSPIKE, 6 },
  6979. { MG_FROSTDIVER, 6 },
  6980. { MG_THUNDERSTORM, 9 },
  6981. { WZ_HEAVENDRIVE, 9 }
  6982. };
  6983. #else
  6984. const std::vector<s_autospell_requirement> autospell_skills = {
  6985. { MG_NAPALMBEAT, 0 },
  6986. { MG_COLDBOLT, 1 },
  6987. { MG_FIREBOLT, 1 },
  6988. { MG_LIGHTNINGBOLT, 1 },
  6989. { MG_SOULSTRIKE, 4 },
  6990. { MG_FIREBALL, 7 },
  6991. { MG_FROSTDIVER, 9 },
  6992. };
  6993. #endif
  6994. #if PACKETVER_MAIN_NUM >= 20181128 || PACKETVER_RE_NUM >= 20181031
  6995. PACKET_ZC_AUTOSPELLLIST* p = reinterpret_cast<PACKET_ZC_AUTOSPELLLIST*>( packet_buffer );
  6996. p->packetType = HEADER_ZC_AUTOSPELLLIST;
  6997. p->packetLength = sizeof( *p );
  6998. size_t count = 0;
  6999. for( const s_autospell_requirement& requirement : autospell_skills ){
  7000. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  7001. p->skills[count++] = requirement.skill_id;
  7002. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skills[0] ) );
  7003. }
  7004. }
  7005. clif_send( p, p->packetLength, &sd.bl, SELF );
  7006. #elif PACKETVER_MAIN_NUM >= 20090406 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_SAK_NUM >= 20080618
  7007. PACKET_ZC_AUTOSPELLLIST p = {};
  7008. p.packetType = HEADER_ZC_AUTOSPELLLIST;
  7009. size_t count = 0;
  7010. for( const s_autospell_requirement& requirement : autospell_skills ){
  7011. if( count == ARRAYLENGTH( p.skills ) ){
  7012. break;
  7013. }
  7014. if( skill_lv > requirement.required_autospell_skill_lv && pc_checkskill( &sd, requirement.skill_id ) ){
  7015. p.skills[count++] = requirement.skill_id;
  7016. }else{
  7017. p.skills[count++] = 0;
  7018. }
  7019. }
  7020. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7021. #endif
  7022. sd.menuskill_id = SA_AUTOSPELL;
  7023. sd.menuskill_val = skill_lv;
  7024. }
  7025. /// Devotion's visual effect (ZC_DEVOTIONLIST).
  7026. /// 01cf <devoter id>.L { <devotee id>.L }*5 <max distance>.W
  7027. void clif_devotion(struct block_list *src, map_session_data *tsd)
  7028. {
  7029. unsigned char buf[56];
  7030. nullpo_retv(src);
  7031. memset(buf,0,packet_len(0x1cf));
  7032. WBUFW(buf,0) = 0x1cf;
  7033. WBUFL(buf,2) = src->id;
  7034. if( src->type == BL_MER )
  7035. {
  7036. s_mercenary_data *md = BL_CAST(BL_MER,src);
  7037. if( md && md->master && md->devotion_flag )
  7038. WBUFL(buf,6) = md->master->bl.id;
  7039. WBUFW(buf,26) = skill_get_range2(src, ML_DEVOTION, mercenary_checkskill(md, ML_DEVOTION), false);
  7040. }
  7041. else
  7042. {
  7043. int32 i;
  7044. map_session_data *sd = BL_CAST(BL_PC,src);
  7045. if( sd == nullptr )
  7046. return;
  7047. for( i = 0; i < 5 /*MAX_DEVOTION*/; i++ ) // Client only able show to 5 links
  7048. WBUFL(buf,6+4*i) = sd->devotion[i];
  7049. WBUFW(buf,26) = skill_get_range2(src, CR_DEVOTION, pc_checkskill(sd, CR_DEVOTION), false);
  7050. }
  7051. if( tsd )
  7052. clif_send(buf, packet_len(0x1cf), &tsd->bl, SELF);
  7053. else
  7054. clif_send(buf, packet_len(0x1cf), src, AREA);
  7055. }
  7056. /// Notifies the client of an object's spirits.
  7057. /// 01d0 <id>.L <amount>.W (ZC_SPIRITS)
  7058. /// 01e1 <id>.L <amount>.W (ZC_SPIRITS2)
  7059. void clif_spiritball( struct block_list *bl, struct block_list* target, enum send_target send_target ){
  7060. nullpo_retv( bl );
  7061. PACKET_ZC_SPIRITS p = {};
  7062. p.PacketType = HEADER_ZC_SPIRITS;
  7063. p.AID = bl->id;
  7064. switch( bl->type ){
  7065. case BL_PC:
  7066. p.num = ( (map_session_data*)bl )->spiritball;
  7067. break;
  7068. case BL_HOM:
  7069. p.num = ( (struct homun_data*)bl )->homunculus.spiritball;
  7070. break;
  7071. }
  7072. clif_send( &p, sizeof( p ), target == nullptr ? bl : target, send_target );
  7073. }
  7074. /// Notifies clients in area of a character's combo delay (ZC_COMBODELAY).
  7075. /// 01d2 <account id>.L <delay>.L
  7076. void clif_combo_delay(struct block_list *bl,t_tick wait)
  7077. {
  7078. unsigned char buf[32];
  7079. nullpo_retv(bl);
  7080. WBUFW(buf,0)=0x1d2;
  7081. WBUFL(buf,2)=bl->id;
  7082. WBUFL(buf,6)=client_tick(wait);
  7083. clif_send(buf,packet_len(0x1d2),bl,AREA);
  7084. }
  7085. /// Notifies clients in area that a character has blade-stopped another (ZC_BLADESTOP).
  7086. /// 01d1 <src id>.L <dst id>.L <flag>.L
  7087. /// flag:
  7088. /// 0 = inactive
  7089. /// 1 = active
  7090. void clif_bladestop(struct block_list *src, int32 dst_id, int32 active)
  7091. {
  7092. unsigned char buf[32];
  7093. nullpo_retv(src);
  7094. WBUFW(buf,0)=0x1d1;
  7095. WBUFL(buf,2)=src->id;
  7096. WBUFL(buf,6)=dst_id;
  7097. WBUFL(buf,10)=active;
  7098. clif_send(buf,packet_len(0x1d1),src,AREA);
  7099. }
  7100. /// MVP effect (ZC_MVP).
  7101. /// 010c <account id>.L
  7102. void clif_mvp_effect(map_session_data *sd)
  7103. {
  7104. unsigned char buf[16];
  7105. nullpo_retv(sd);
  7106. WBUFW(buf,0)=0x10c;
  7107. WBUFL(buf,2)=sd->bl.id;
  7108. clif_send(buf,packet_len(0x10c),&sd->bl,AREA);
  7109. }
  7110. /// MVP item reward message (ZC_MVP_GETTING_ITEM).
  7111. /// 010a <name id>.W
  7112. void clif_mvp_item( map_session_data *sd, t_itemid nameid ){
  7113. PACKET_ZC_MVP_GETTING_ITEM p = {};
  7114. p.packetType = HEADER_ZC_MVP_GETTING_ITEM;
  7115. p.itemId = client_nameid( nameid );
  7116. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  7117. }
  7118. /// MVP EXP reward message (ZC_MVP_GETTING_SPECIAL_EXP).
  7119. /// 010b <exp>.L
  7120. void clif_mvp_exp(map_session_data *sd, t_exp exp) {
  7121. #if PACKETVER >= 20131223 // Kro remove this packet [Napster]
  7122. if (battle_config.mvp_exp_reward_message) {
  7123. char e_msg[CHAT_SIZE_MAX];
  7124. sprintf(e_msg, msg_txt(sd, 717), exp);
  7125. clif_messagecolor(&sd->bl, color_table[COLOR_CYAN], e_msg, false, SELF); // Congratulations! You are the MVP! Your reward EXP Points are %u !!
  7126. }
  7127. #else
  7128. int32 fd;
  7129. nullpo_retv(sd);
  7130. fd = sd->fd;
  7131. WFIFOHEAD(fd, packet_len(0x10b));
  7132. WFIFOW(fd,0) = 0x10b;
  7133. WFIFOL(fd,2) = (uint32)u64min( exp, MAX_EXP );
  7134. WFIFOSET(fd, packet_len(0x10b));
  7135. #endif
  7136. }
  7137. /// Dropped MVP item reward message (ZC_THROW_MVPITEM).
  7138. /// 010d
  7139. ///
  7140. /// "You are the MVP, but cannot obtain the reward because
  7141. /// you are overweight."
  7142. void clif_mvp_noitem(map_session_data* sd)
  7143. {
  7144. int32 fd = sd->fd;
  7145. WFIFOHEAD(fd,packet_len(0x10d));
  7146. WFIFOW(fd,0) = 0x10d;
  7147. WFIFOSET(fd,packet_len(0x10d));
  7148. }
  7149. /// Guild creation result.
  7150. /// 0167 <result>.B (ZC_RESULT_MAKE_GUILD)
  7151. /// result:
  7152. /// 0 = "Guild has been created."
  7153. /// 1 = "You are already in a Guild."
  7154. /// 2 = "That Guild Name already exists."
  7155. /// 3 = "You need the neccessary item to create a Guild."
  7156. void clif_guild_created( map_session_data& sd, int32 flag ){
  7157. PACKET_ZC_RESULT_MAKE_GUILD p = {};
  7158. p.packetType = HEADER_ZC_RESULT_MAKE_GUILD;
  7159. p.result = static_cast<decltype(p.result)>( flag );
  7160. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7161. }
  7162. /// Notifies the client that it is belonging to a guild (ZC_UPDATE_GDID).
  7163. /// 016c <guild id>.L <emblem id>.L <mode>.L <ismaster>.B <inter sid>.L <guild name>.24B
  7164. /// mode:
  7165. /// &0x01 = allow invite
  7166. /// &0x10 = allow expel
  7167. void clif_guild_belonginfo( map_session_data& sd ){
  7168. if( sd.guild == nullptr ){
  7169. return;
  7170. }
  7171. const auto &guild = sd.guild->guild;
  7172. int32 position = guild_getposition(sd);
  7173. if( position < 0 ){
  7174. return;
  7175. }
  7176. PACKET_ZC_UPDATE_GDID p = {};
  7177. p.PacketType = HEADER_ZC_UPDATE_GDID;
  7178. p.guildId = guild.guild_id;
  7179. p.emblemVersion = guild.emblem_id;
  7180. p.mode = guild.position[position].mode;
  7181. p.isMaster = ( sd.state.gmaster_flag == 1 );
  7182. p.interSid = 0; // InterSID (unknown purpose)
  7183. safestrncpy( p.guildName, guild.name, sizeof( p.guildName ) );
  7184. #if PACKETVER_MAIN_NUM >= 20220216
  7185. p.masterGID = guild.member[0].char_id;
  7186. #endif
  7187. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7188. }
  7189. /// Guild member login notice.
  7190. /// 016d <account id>.L <char id>.L <status>.L (ZC_UPDATE_CHARSTAT)
  7191. /// 01f2 <account id>.L <char id>.L <status>.L <gender>.W <hair style>.W <hair color>.W (ZC_UPDATE_CHARSTAT2)
  7192. /// status:
  7193. /// 0 = offline
  7194. /// 1 = online
  7195. void clif_guild_memberlogin_notice(const struct mmo_guild &g,int32 idx,int32 flag)
  7196. {
  7197. unsigned char buf[64];
  7198. map_session_data* sd;
  7199. WBUFW(buf, 0)=0x1f2;
  7200. WBUFL(buf, 2)=g.member[idx].account_id;
  7201. WBUFL(buf, 6)=g.member[idx].char_id;
  7202. WBUFL(buf,10)=flag;
  7203. if( ( sd = g.member[idx].sd ) != nullptr )
  7204. {
  7205. WBUFW(buf,14) = sd->status.sex;
  7206. WBUFW(buf,16) = sd->status.hair;
  7207. WBUFW(buf,18) = sd->status.hair_color;
  7208. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD_WOS);
  7209. }
  7210. else if( ( sd = guild_getavailablesd(g) ) != nullptr )
  7211. {
  7212. WBUFW(buf,14) = 0;
  7213. WBUFW(buf,16) = 0;
  7214. WBUFW(buf,18) = 0;
  7215. clif_send(buf,packet_len(0x1f2),&sd->bl,GUILD);
  7216. }
  7217. }
  7218. // Function `clif_guild_memberlogin_notice` sends info about
  7219. // logins and logouts of a guild member to the rest members.
  7220. // But at the 1st time (after a player login or map changing)
  7221. // the client won't show the message.
  7222. // So I suggest use this function for sending "first-time-info"
  7223. // to some player on entering the game or changing location.
  7224. // At next time the client would always show the message.
  7225. // The function sends all the statuses in the single packet
  7226. // to economize traffic. [LuzZza]
  7227. void clif_guild_send_onlineinfo(map_session_data *sd)
  7228. {
  7229. unsigned char buf[14*128];
  7230. int32 i, count=0, p_len;
  7231. nullpo_retv(sd);
  7232. p_len = packet_len(0x16d);
  7233. auto &g = sd->guild;
  7234. if (!g)
  7235. return;
  7236. for(i=0; i<g->guild.max_member; i++) {
  7237. if(g->guild.member[i].account_id > 0 &&
  7238. g->guild.member[i].account_id != sd->status.account_id) {
  7239. WBUFW(buf,count*p_len) = 0x16d;
  7240. WBUFL(buf,count*p_len+2) = g->guild.member[i].account_id;
  7241. WBUFL(buf,count*p_len+6) = g->guild.member[i].char_id;
  7242. WBUFL(buf,count*p_len+10) = g->guild.member[i].online;
  7243. count++;
  7244. }
  7245. }
  7246. clif_send(buf, p_len*count, &sd->bl, SELF);
  7247. }
  7248. /// Bitmask of enabled guild window tabs
  7249. /// 014e <menu flag>.L (ZC_ACK_GUILD_MENUINTERFACE)
  7250. /// menu flag:
  7251. /// 0x00 = Basic Info (always on)
  7252. /// &0x01 = Member manager
  7253. /// &0x02 = Positions
  7254. /// &0x04 = Skills
  7255. /// &0x10 = Expulsion list
  7256. /// &0x40 = Unknown (GMENUFLAG_ALLGUILDLIST)
  7257. /// &0x80 = Notice
  7258. void clif_guild_masterormember(map_session_data& sd){
  7259. PACKET_ZC_ACK_GUILD_MENUINTERFACE p{};
  7260. p.packetType = HEADER_ZC_ACK_GUILD_MENUINTERFACE;
  7261. if(sd.state.gmaster_flag){
  7262. p.menuFlag = 0xd7;
  7263. }else{
  7264. p.menuFlag = 0x57;
  7265. }
  7266. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7267. }
  7268. /// Guild basic information (Territories [Valaris])
  7269. /// 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)
  7270. /// 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)
  7271. /// 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)
  7272. void clif_guild_basicinfo( map_session_data& sd ){
  7273. if( sd.guild == nullptr ){
  7274. return;
  7275. }
  7276. const auto &guild = sd.guild->guild;
  7277. PACKET_ZC_GUILD_INFO p = {};
  7278. p.PacketType = HEADER_ZC_GUILD_INFO;
  7279. p.GDID = guild.guild_id;
  7280. p.level = guild.guild_lv;
  7281. p.userNum = guild.connect_member;
  7282. p.maxUserNum = guild.max_member;
  7283. p.userAverageLevel = guild.average_lv;
  7284. p.exp = (uint32)cap_value( guild.exp, 0, MAX_GUILD_EXP );
  7285. p.maxExp = (uint32)cap_value( guild.next_exp, 0, MAX_GUILD_EXP );
  7286. p.point = 0; // Tax Points
  7287. p.honor = 0; // Honor: (left) Vulgar [-100,100] Famed (right)
  7288. p.virtue = 0; // Virtue: (down) Wicked [-100,100] Righteous (up)
  7289. p.emblemVersion = guild.emblem_id;
  7290. safestrncpy( p.guildname, guild.name, sizeof( p.guildname ) );
  7291. safestrncpy( p.manageLand, msg_txt( &sd, 300 + guild_checkcastles( guild ) ), sizeof( p.manageLand ) );
  7292. p.zeny = 0;
  7293. #if PACKETVER >= 20200902
  7294. p.masterGID = guild.member[0].char_id; // leader
  7295. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7296. #elif PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7297. p.masterGID = guild.member[0].char_id; // leader
  7298. #else
  7299. safestrncpy( p.masterName, guild.master, sizeof( p.masterName ) );
  7300. #endif
  7301. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7302. }
  7303. /// Guild alliance and opposition list
  7304. /// 014c <packet len>.W { <relation>.L <guild id>.L <guild name>.24B }* (ZC_MYGUILD_BASIC_INFO)
  7305. void clif_guild_allianceinfo(map_session_data& sd){
  7306. auto &g = sd.guild;
  7307. if (g == nullptr){
  7308. return;
  7309. }
  7310. PACKET_ZC_MYGUILD_BASIC_INFO* p = reinterpret_cast<PACKET_ZC_MYGUILD_BASIC_INFO*>( packet_buffer );
  7311. p->PacketType = HEADER_ZC_MYGUILD_BASIC_INFO;
  7312. p->PacketLength = sizeof(*p);
  7313. for(size_t i=0, c = 0;i<MAX_GUILDALLIANCE;i++){
  7314. guild_alliance &a = g->guild.alliance[i];
  7315. if(a.guild_id<=0){
  7316. continue;
  7317. }
  7318. RELATED_GUILD_INFO& info = p->rgInfo[c];
  7319. info.relation = a.opposition;
  7320. info.GDID = a.guild_id;
  7321. safestrncpy(info.guildname,a.name,sizeof(info.guildname));
  7322. p->PacketLength += static_cast<decltype(p->PacketLength)>(sizeof(info));
  7323. c++;
  7324. }
  7325. clif_send(p,p->PacketLength,&sd.bl,SELF);
  7326. }
  7327. /// Guild member manager information
  7328. /// 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)
  7329. /// state:
  7330. /// 0 = offline
  7331. /// 1 = online
  7332. /// memo:
  7333. /// probably member's self-introduction (unused, no client UI/packets for editing it)
  7334. /// 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)
  7335. void clif_guild_memberlist( map_session_data& sd ){
  7336. if( sd.guild == nullptr ){
  7337. return;
  7338. }
  7339. const auto &guild = sd.guild->guild;
  7340. PACKET_ZC_MEMBERMGR_INFO* p = reinterpret_cast<PACKET_ZC_MEMBERMGR_INFO*>( packet_buffer );
  7341. p->PacketType = HEADER_ZC_MEMBERMGR_INFO;
  7342. p->packetLength = sizeof( *p );
  7343. for( int32 i = 0, c = 0; i < guild.max_member; i++ ){
  7344. const auto &member = guild.member[i];
  7345. if( member.account_id == 0 ){
  7346. continue;
  7347. }
  7348. GUILD_MEMBER_INFO& member_info = p->guildMemberInfo[c];
  7349. member_info.AID = member.account_id;
  7350. member_info.GID = member.char_id;
  7351. member_info.head = member.hair;
  7352. member_info.headPalette = member.hair_color;
  7353. member_info.sex = member.gender;
  7354. member_info.job = member.class_;
  7355. member_info.level = member.lv;
  7356. member_info.contributionExp = (uint32)cap_value( member.exp, 0, MAX_GUILD_EXP );
  7357. member_info.currentState = member.online;
  7358. member_info.positionID = member.position;
  7359. #if PACKETVER >= 20200902
  7360. member_info.lastLoginTime = member.last_login;
  7361. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7362. #elif PACKETVER_MAIN_NUM >= 20161214 || PACKETVER_RE_NUM >= 20161130 || defined(PACKETVER_ZERO)
  7363. member_info.lastLoginTime = member.last_login;
  7364. #else
  7365. 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.
  7366. safestrncpy( member_info.char_name, member.name, sizeof( member_info.char_name ) );
  7367. #endif
  7368. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( member_info ) );
  7369. c++;
  7370. }
  7371. clif_send( p, p->packetLength, &sd.bl, SELF );
  7372. }
  7373. /// Guild position name information (ZC_POSITION_ID_NAME_INFO).
  7374. /// 0166 <packet len>.W { <position id>.L <position name>.24B }*
  7375. void clif_guild_positionnamelist(map_session_data *sd)
  7376. {
  7377. int32 i,fd;
  7378. nullpo_retv(sd);
  7379. auto &g = sd->guild;
  7380. if (!g)
  7381. return;
  7382. fd = sd->fd;
  7383. WFIFOHEAD(fd, MAX_GUILDPOSITION * 28 + 4);
  7384. WFIFOW(fd, 0)=0x166;
  7385. for(i=0;i<MAX_GUILDPOSITION;i++){
  7386. WFIFOL(fd,i*28+4)=i;
  7387. safestrncpy(WFIFOCP(fd,i*28+8),g->guild.position[i].name,NAME_LENGTH);
  7388. }
  7389. WFIFOW(fd,2)=i*28+4;
  7390. WFIFOSET(fd,WFIFOW(fd,2));
  7391. }
  7392. /// Guild position information
  7393. /// 0160 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L }* (ZC_POSITION_INFO)
  7394. /// mode:
  7395. /// See enum e_guild_permission
  7396. /// ranking:
  7397. /// TODO
  7398. static void clif_guild_positioninfolist(map_session_data& sd){
  7399. auto &g = sd.guild;
  7400. if (g == nullptr){
  7401. return;
  7402. }
  7403. PACKET_ZC_POSITION_INFO* p = reinterpret_cast<PACKET_ZC_POSITION_INFO*>( packet_buffer );
  7404. p->PacketType = HEADER_ZC_POSITION_INFO;
  7405. p->PacketLength = sizeof(*p);
  7406. for(size_t i=0;i<MAX_GUILDPOSITION;i++){
  7407. guild_position& gp = g->guild.position[i];
  7408. PACKET_ZC_POSITION_INFO_sub& position = p->posInfo[i];
  7409. position.positionID = static_cast<decltype(position.positionID)>( i );
  7410. position.right = gp.mode;
  7411. position.ranking = static_cast<decltype(position.ranking)>( i );
  7412. position.payRate = gp.exp_mode;
  7413. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( position ) );
  7414. }
  7415. clif_send(p,p->PacketLength,&sd.bl,SELF);
  7416. }
  7417. /// Notifies clients in a guild about updated position information.
  7418. /// 0174 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <position name>.24B }* (ZC_ACK_CHANGE_GUILD_POSITIONINFO)
  7419. /// mode:
  7420. /// &0x01 = allow invite
  7421. /// &0x10 = allow expel
  7422. /// ranking:
  7423. /// TODO
  7424. void clif_guild_positionchanged(const struct mmo_guild &g,int32 idx)
  7425. {
  7426. // FIXME: This packet is intended to update the clients after a
  7427. // commit of position info changes, not sending one packet per
  7428. // position.
  7429. map_session_data *sd;
  7430. unsigned char buf[128];
  7431. WBUFW(buf, 0)=0x174;
  7432. WBUFW(buf, 2)=44; // packet len
  7433. // GUILD_REG_POSITION_INFO{
  7434. WBUFL(buf, 4)=idx;
  7435. WBUFL(buf, 8)=g.position[idx].mode;
  7436. WBUFL(buf,12)=idx;
  7437. WBUFL(buf,16)=g.position[idx].exp_mode;
  7438. safestrncpy(WBUFCP(buf,20),g.position[idx].name,NAME_LENGTH);
  7439. // }*
  7440. if( (sd=guild_getavailablesd(g))!=nullptr )
  7441. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7442. }
  7443. /// Notifies clients in a guild about updated member position assignments.
  7444. /// 0156 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (ZC_ACK_REQ_CHANGE_MEMBERS)
  7445. void clif_guild_memberpositionchanged(const struct mmo_guild &g, int32 idx)
  7446. {
  7447. // FIXME: This packet is intended to update the clients after a
  7448. // commit of member position assignment changes, not sending one
  7449. // packet per position.
  7450. map_session_data *sd;
  7451. unsigned char buf[64];
  7452. WBUFW(buf, 0)=0x156;
  7453. WBUFW(buf, 2)=16; // packet len
  7454. // MEMBER_POSITION_INFO{
  7455. WBUFL(buf, 4)=g.member[idx].account_id;
  7456. WBUFL(buf, 8)=g.member[idx].char_id;
  7457. WBUFL(buf,12)=g.member[idx].position;
  7458. // }*
  7459. if( (sd=guild_getavailablesd(g))!=nullptr )
  7460. clif_send(buf,WBUFW(buf,2),&sd->bl,GUILD);
  7461. }
  7462. /// Sends emblems bitmap data to the client that requested it.
  7463. /// 0152 <packet len>.W <guild id>.L <emblem id>.L <emblem data>.?B (ZC_GUILD_EMBLEM_IMG)
  7464. void clif_guild_emblem(const map_session_data &sd, const struct mmo_guild &g)
  7465. {
  7466. int32 fd = sd.fd;
  7467. if( g.emblem_len <= 0 )
  7468. return;
  7469. WFIFOHEAD(fd,g.emblem_len+12);
  7470. WFIFOW(fd,0)=0x152;
  7471. WFIFOW(fd,2)=g.emblem_len+12;
  7472. WFIFOL(fd,4)=g.guild_id;
  7473. WFIFOL(fd,8)=g.emblem_id;
  7474. memcpy(WFIFOP(fd,12),g.emblem_data,g.emblem_len);
  7475. WFIFOSET(fd,WFIFOW(fd,2));
  7476. }
  7477. /// Sends update of the guild id/emblem id to everyone in the area.
  7478. /// 01b4 <id>.L <guild id>.L <emblem id>.W (ZC_CHANGE_GUILD)
  7479. /// 0b1f <guild id>.L <version>.L <AID>.L (ZC_NEW_EMBLEM_DOWNLOAD)
  7480. /// 0b47 <guild id>.L <version>.L <AID>.L (ZC_ACK_ADD_NEW_EMBLEM)
  7481. void clif_guild_emblem_area(struct block_list* bl)
  7482. {
  7483. // TODO this packet doesn't force the update of ui components that have the emblem visible
  7484. // (emblem in the flag npcs and emblem over the head in agit maps) [FlavioJS]
  7485. PACKET_ZC_CHANGE_GUILD p{};
  7486. p.packetType = HEADER_ZC_CHANGE_GUILD;
  7487. p.guild_id = status_get_guild_id(bl);
  7488. p.emblem_id = status_get_emblem_id(bl);
  7489. p.AID = bl->id;
  7490. clif_send(&p, sizeof(p), bl, AREA);
  7491. }
  7492. /// Sends guild skills.
  7493. /// 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)
  7494. void clif_guild_skillinfo( map_session_data& sd ){
  7495. auto& g = sd.guild;
  7496. if( g == nullptr ){
  7497. return;
  7498. }
  7499. PACKET_ZC_GUILD_SKILLINFO* p = reinterpret_cast<PACKET_ZC_GUILD_SKILLINFO*>( packet_buffer );
  7500. p->PacketType = HEADER_ZC_GUILD_SKILLINFO;
  7501. p->PacketLength = sizeof( *p );
  7502. p->skillPoint = g->guild.skill_point;
  7503. for( size_t i = 0, c = 0; i < MAX_GUILDSKILL; i++ ){
  7504. if( g->guild.skill[i].id <= 0 ){
  7505. continue;
  7506. }
  7507. if( !guild_check_skill_require( g->guild, g->guild.skill[i].id ) ){
  7508. continue;
  7509. }
  7510. GUILD_SKILLDATA& gs = p->skillInfo[c];
  7511. int32 skill_id = g->guild.skill[i].id;
  7512. gs.id = skill_id;
  7513. gs.inf = skill_get_inf(skill_id);
  7514. gs.level = g->guild.skill[i].lv;
  7515. if( g->guild.skill[i].lv > 0 ){
  7516. gs.sp = skill_get_sp( skill_id, g->guild.skill[i].lv );
  7517. gs.range2 = skill_get_range( skill_id, g->guild.skill[i].lv );
  7518. }else{
  7519. gs.sp = 0;
  7520. gs.range2 = 0;
  7521. }
  7522. safestrncpy( gs.name, skill_get_name( skill_id ), sizeof( gs.name ) );
  7523. gs.upFlag = ( g->guild.skill[i].lv < guild_skill_get_max( skill_id ) && &sd == g->guild.member[0].sd ) ? 1 : 0;
  7524. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( gs ) );
  7525. c++;
  7526. }
  7527. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7528. }
  7529. /// Sends guild notice to client.
  7530. /// 016f <subject>.60B <notice>.120B (ZC_GUILD_NOTICE)
  7531. void clif_guild_notice( map_session_data& sd ){
  7532. auto& g = sd.guild;
  7533. if( g == nullptr ){
  7534. return;
  7535. }
  7536. if( g->guild.mes1[0] == '\0' && g->guild.mes2[0] == '\0' ){
  7537. return;
  7538. }
  7539. PACKET_ZC_GUILD_NOTICE p = {};
  7540. p.packetType = HEADER_ZC_GUILD_NOTICE;
  7541. safestrncpy( p.subject, g->guild.mes1, sizeof( p.subject ) );
  7542. safestrncpy( p.notice, g->guild.mes2, sizeof( p.notice ) );
  7543. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7544. }
  7545. /// Guild invite.
  7546. /// 016a <guild id>.L <guild name>.24B (ZC_REQ_JOIN_GUILD)
  7547. void clif_guild_invite( map_session_data& sd, const struct mmo_guild& g ){
  7548. PACKET_ZC_REQ_JOIN_GUILD p = {};
  7549. p.packetType = HEADER_ZC_REQ_JOIN_GUILD;
  7550. p.guild_id = g.guild_id;
  7551. safestrncpy( p.guild_name, g.name, sizeof( p.guild_name ) );
  7552. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7553. }
  7554. /// Reply to invite request.
  7555. /// 0169 <answer>.B (ZC_ACK_REQ_JOIN_GUILD)
  7556. /// answer:
  7557. /// 0 = Already in guild.
  7558. /// 1 = Offer rejected.
  7559. /// 2 = Offer accepted.
  7560. /// 3 = Guild full.
  7561. void clif_guild_inviteack( map_session_data& sd, int32 flag ){
  7562. PACKET_ZC_ACK_REQ_JOIN_GUILD p = {};
  7563. p.packetType = HEADER_ZC_ACK_REQ_JOIN_GUILD;
  7564. p.result = static_cast<decltype(p.result)>( flag );
  7565. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7566. }
  7567. /// Notifies clients of a guild of a leaving member.
  7568. /// 015a <char name>.24B <reason>.40B (ZC_ACK_LEAVE_GUILD)
  7569. /// 0a83 <CID>.L <reason>.40B (ZC_ACK_LEAVE_GUILD_DELNAME)
  7570. void clif_guild_leave( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7571. PACKET_ZC_ACK_LEAVE_GUILD p = {};
  7572. p.packetType = guildLeave;
  7573. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7574. p.GID = char_id;
  7575. #else
  7576. safestrncpy( p.name, name, sizeof( p.name ) );
  7577. #endif
  7578. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7579. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7580. }
  7581. /// Notifies clients of a guild of an expelled member.
  7582. /// 015c <char name>.24B <reason>.40B <account name>.24B (ZC_ACK_BAN_GUILD)
  7583. /// 0839 <char name>.24B <reason>.40B (ZC_ACK_BAN_GUILD_SSO)
  7584. /// 0a82 <CID>.L <reason>.40B (ZC_ACK_BAN_GUILD_DELNAME)
  7585. void clif_guild_expulsion( map_session_data& sd, const char* name, uint32 char_id, const char* mes ){
  7586. PACKET_ZC_ACK_BAN_GUILD p = {};
  7587. p.packetType = guildExpulsion;
  7588. #if PACKETVER_MAIN_NUM >= 20161019 || PACKETVER_RE_NUM >= 20160921 || defined(PACKETVER_ZERO)
  7589. p.GID = char_id;
  7590. #else
  7591. safestrncpy( p.name, name, sizeof( p.name ) );
  7592. #endif
  7593. safestrncpy( p.reason, mes, sizeof( p.reason ) );
  7594. #if PACKETVER < 20100803
  7595. // account name is not sent for security reasons
  7596. safestrncpy( p.account_name, "", sizeof( p.account_name ) );
  7597. #endif
  7598. clif_send( &p, sizeof( p ), &sd.bl, GUILD_NOBG );
  7599. }
  7600. /// Guild expulsion list
  7601. /// 0163 <packet len>.W { <char name>.24B <account name>.24B <reason>.40B }* (ZC_BAN_LIST)
  7602. /// 0163 <packet len>.W { <char name>.24B <reason>.40B }* (PACKETVER >= 20100803) (ZC_BAN_LIST)
  7603. /// 0a87 <packet len>.W { <charid>.L <reason>.40B }* (ZC_BAN_LIST2)
  7604. /// 0b7c <packet len>.W { <charid>.L <reason>.40B <char name>.24B }* (ZC_BAN_LIST3)
  7605. static void clif_guild_expulsionlist(map_session_data& sd){
  7606. auto &g = sd.guild;
  7607. if (!g)
  7608. return;
  7609. PACKET_ZC_BAN_LIST* p = reinterpret_cast<PACKET_ZC_BAN_LIST*>( packet_buffer );
  7610. p->packetType = HEADER_ZC_BAN_LIST;
  7611. p->packetLen = sizeof(*p);
  7612. for( size_t i = 0, c = 0; i < MAX_GUILDEXPULSION; i++ ){
  7613. struct guild_expulsion& e = g->guild.expulsion[i];
  7614. if( e.account_id == 0 ){
  7615. continue;
  7616. }
  7617. PACKET_ZC_BAN_LIST_sub& banned = p->chars[c];
  7618. #if PACKETVER >= 20161019
  7619. banned.char_id = e.char_id;
  7620. #elif PACKETVER < 20100803
  7621. // account name (not used for security reasons)
  7622. safestrncpy(banned.account_name, "", sizeof(banned.account_name));
  7623. #endif
  7624. #if PACKETVER >= 20200902 || PACKETVER < 20161019
  7625. safestrncpy(banned.char_name, e.name, sizeof(banned.char_name));
  7626. #endif
  7627. safestrncpy(banned.message, e.mes, sizeof(banned.message));
  7628. p->packetLen += static_cast<decltype(p->packetLen)>(sizeof(banned));
  7629. c++;
  7630. }
  7631. clif_send(p,p->packetLen,&sd.bl,SELF);
  7632. }
  7633. /// Guild chat message
  7634. /// 017f <packet len>.W <message>.?B (ZC_GUILD_CHAT)
  7635. void clif_guild_message( const struct mmo_guild& g, const char* mes, size_t len ){
  7636. PACKET_ZC_GUILD_CHAT *p = reinterpret_cast<PACKET_ZC_GUILD_CHAT*>( packet_buffer );
  7637. // -1 for null terminator
  7638. static const size_t max_len = CHAT_SIZE_MAX - sizeof( *p ) - 1;
  7639. map_session_data* sd = guild_getavailablesd(g);
  7640. // Ignore this message, if no guildmember is available
  7641. if (sd == nullptr)
  7642. return;
  7643. if( len == 0 ){
  7644. return;
  7645. } else if( len > max_len ){
  7646. ShowWarning("clif_guild_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", guild_id=%u).\n", mes, len, max_len, g.guild_id);
  7647. len = max_len;
  7648. }
  7649. p->packetType = HEADER_ZC_GUILD_CHAT;
  7650. p->packetLength = sizeof(*p);
  7651. safestrncpy(p->message, mes, len+1);
  7652. p->packetLength += static_cast<decltype(p->packetLength)>( len + 1 );
  7653. clif_send(p, p->packetLength, &sd->bl, GUILD_NOBG);
  7654. }
  7655. /// Request for guild alliance
  7656. /// 0171 <inviter account id>.L <guild name>.24B (ZC_REQ_ALLY_GUILD).
  7657. void clif_guild_reqalliance(map_session_data& sd,uint32 account_id,const char *name)
  7658. {
  7659. PACKET_ZC_REQ_ALLY_GUILD p{};
  7660. p.packetType = HEADER_ZC_REQ_ALLY_GUILD;
  7661. p.inviterId = account_id;
  7662. safestrncpy(p.inviterGuildName,name,sizeof(p.inviterGuildName));
  7663. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7664. }
  7665. /// Notifies the client about the result of a alliance request.
  7666. /// 0173 <answer>.B (ZC_ACK_REQ_ALLY_GUILD)
  7667. /// answer:
  7668. /// 0 = Already allied.
  7669. /// 1 = You rejected the offer.
  7670. /// 2 = You accepted the offer.
  7671. /// 3 = They have too any alliances.
  7672. /// 4 = You have too many alliances.
  7673. /// 5 = Alliances are disabled.
  7674. void clif_guild_allianceack(map_session_data& sd,uint8 flag){
  7675. PACKET_ZC_ACK_REQ_ALLY_GUILD p{};
  7676. p.packetType = HEADER_ZC_ACK_REQ_ALLY_GUILD;
  7677. p.flag = flag;
  7678. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7679. }
  7680. /// Notifies the client that a alliance or opposition has been removed.
  7681. /// 0184 <other guild id>.L <relation>.L (ZC_DELETE_RELATED_GUILD)
  7682. /// relation:
  7683. /// 0 = Ally
  7684. /// 1 = Enemy
  7685. void clif_guild_delalliance(map_session_data& sd,uint32 guild_id,uint32 flag)
  7686. {
  7687. PACKET_ZC_DELETE_RELATED_GUILD p{};
  7688. p.packetType = HEADER_ZC_DELETE_RELATED_GUILD;
  7689. p.allyID = guild_id;
  7690. p.flag = flag;
  7691. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7692. }
  7693. /// Notifies the client about the result of a opposition request.
  7694. /// 0181 <result>.B (ZC_ACK_REQ_HOSTILE_GUILD)
  7695. /// result:
  7696. /// 0 = Antagonist has been set.
  7697. /// 1 = Guild has too many Antagonists.
  7698. /// 2 = Already set as an Antagonist.
  7699. /// 3 = Antagonists are disabled.
  7700. void clif_guild_oppositionack(map_session_data& sd,uint8 flag){
  7701. PACKET_ZC_ACK_REQ_HOSTILE_GUILD p{};
  7702. p.packetType = HEADER_ZC_ACK_REQ_HOSTILE_GUILD;
  7703. p.flag = flag;
  7704. clif_send(&p,sizeof(p),&sd.bl,SELF);
  7705. }
  7706. /// Adds alliance or opposition (ZC_ADD_RELATED_GUILD).
  7707. /// 0185 <relation>.L <guild id>.L <guild name>.24B
  7708. /*
  7709. void clif_guild_allianceadded(struct guild *g,int32 idx)
  7710. {
  7711. unsigned char buf[64];
  7712. WBUFW(buf,0)=0x185;
  7713. WBUFL(buf,2)=g->alliance[idx].opposition;
  7714. WBUFL(buf,6)=g->alliance[idx].guild_id;
  7715. safestrncpy(WBUFP(buf,10),g->alliance[idx].name,NAME_LENGTH);
  7716. clif_send(buf,packet_len(0x185),guild_getavailablesd(g),GUILD);
  7717. }
  7718. */
  7719. /// Notifies the client about the result of a guild break.
  7720. /// 015e <reason>.L (ZC_ACK_DISORGANIZE_GUILD_RESULT)
  7721. /// 0 = success
  7722. /// 1 = invalid key (guild name, @see clif_parse_GuildBreak)
  7723. /// 2 = there are still members in the guild
  7724. void clif_guild_broken( map_session_data& sd, int32 flag ){
  7725. PACKET_ZC_ACK_DISORGANIZE_GUILD_RESULT p = {};
  7726. p.packetType = HEADER_ZC_ACK_DISORGANIZE_GUILD_RESULT;
  7727. p.result = flag;
  7728. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7729. }
  7730. void clif_guild_position_selected(map_session_data& sd)
  7731. {
  7732. #if PACKETVER_MAIN_NUM >= 20180605 || PACKETVER_RE_NUM >= 20180605 || PACKETVER_ZERO_NUM >= 20180605
  7733. PACKET_ZC_GUILD_POSITION* p = reinterpret_cast<PACKET_ZC_GUILD_POSITION*>(packet_buffer);
  7734. p->packetType = HEADER_ZC_GUILD_POSITION;
  7735. p->packetLength = sizeof( PACKET_ZC_GUILD_POSITION );
  7736. p->AID = sd.bl.id;
  7737. if( sd.guild != nullptr ){
  7738. const auto& g = sd.guild->guild;
  7739. if( int ps = guild_getposition( sd ); ps != -1 ){
  7740. safestrncpy( p->position, g.position[ps].name, NAME_LENGTH );
  7741. p->packetLength += static_cast<decltype(p->packetLength)>( NAME_LENGTH );
  7742. }
  7743. }
  7744. clif_send( p, p->packetLength, &sd.bl, AREA );
  7745. #else
  7746. clif_name_area(&sd.bl);
  7747. #endif
  7748. }
  7749. /// Displays emotion on an object (ZC_EMOTION).
  7750. /// 00c0 <id>.L <type>.B
  7751. /// type:
  7752. /// enum emotion_type
  7753. void clif_emotion(struct block_list *bl,int32 type)
  7754. {
  7755. unsigned char buf[8];
  7756. nullpo_retv(bl);
  7757. WBUFW(buf,0)=0xc0;
  7758. WBUFL(buf,2)=bl->id;
  7759. WBUFB(buf,6)=type;
  7760. clif_send(buf,packet_len(0xc0),bl,AREA);
  7761. }
  7762. /// Displays the contents of a talkiebox trap.
  7763. /// 0191 <id>.L <contents>.80B (ZC_TALKBOX_CHATCONTENTS)
  7764. void clif_talkiebox( struct block_list* bl, const char* talkie ){
  7765. nullpo_retv( bl );
  7766. nullpo_retv( talkie );
  7767. PACKET_ZC_TALKBOX_CHATCONTENTS p = {};
  7768. p.PacketType = HEADER_ZC_TALKBOX_CHATCONTENTS;
  7769. p.aid = bl->id;
  7770. safestrncpy( p.message, talkie, TALKBOX_MESSAGE_SIZE );
  7771. clif_send( &p, sizeof( PACKET_ZC_TALKBOX_CHATCONTENTS ), bl, AREA );
  7772. }
  7773. /// Displays wedding effect centered on an object (ZC_CONGRATULATION).
  7774. /// 01ea <id>.L
  7775. void clif_wedding_effect(struct block_list *bl)
  7776. {
  7777. unsigned char buf[6];
  7778. nullpo_retv(bl);
  7779. WBUFW(buf,0) = 0x1ea;
  7780. WBUFL(buf,2) = bl->id;
  7781. clif_send(buf, packet_len(0x1ea), bl, AREA);
  7782. }
  7783. /// Notifies the client of the name of the partner character (ZC_COUPLENAME).
  7784. /// 01e6 <partner name>.24B
  7785. void clif_callpartner(map_session_data& sd)
  7786. {
  7787. PACKET_ZC_COUPLENAME p = { 0 };
  7788. p.packetType = HEADER_ZC_COUPLENAME;
  7789. if( sd.status.partner_id ) {
  7790. map_session_data *p_sd = pc_get_partner(&sd);
  7791. if (p_sd != nullptr && !p_sd->state.autotrade)
  7792. safestrncpy(p.name, p_sd->status.name, NAME_LENGTH);
  7793. else
  7794. p.name[0] = 0;
  7795. } else {// Send zero-length name if no partner, to initialize the client buffer.
  7796. p.name[0] = 0;
  7797. }
  7798. clif_send(&p, sizeof(p), &sd.bl, AREA);
  7799. }
  7800. /// Initiates the partner "taming" process [DracoRPG] (ZC_START_COUPLE).
  7801. /// 01e4
  7802. /// This packet while still implemented by the client is no longer being officially used.
  7803. /*
  7804. void clif_marriage_process(map_session_data *sd)
  7805. {
  7806. int32 fd;
  7807. nullpo_retv(sd);
  7808. fd=sd->fd;
  7809. WFIFOHEAD(fd,packet_len(0x1e4));
  7810. WFIFOW(fd,0)=0x1e4;
  7811. WFIFOSET(fd,packet_len(0x1e4));
  7812. }
  7813. */
  7814. /// Notice of divorce (ZC_DIVORCE).
  7815. /// 0205 <partner name>.24B
  7816. void clif_divorced(map_session_data* sd, const char* name)
  7817. {
  7818. int32 fd;
  7819. nullpo_retv(sd);
  7820. fd=sd->fd;
  7821. WFIFOHEAD(fd,packet_len(0x205));
  7822. WFIFOW(fd,0)=0x205;
  7823. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  7824. WFIFOSET(fd, packet_len(0x205));
  7825. }
  7826. /// Marriage proposal (ZC_REQ_COUPLE).
  7827. /// 01e2 <account id>.L <char id>.L <char name>.24B
  7828. /// This packet while still implemented by the client is no longer being officially used.
  7829. /*
  7830. void clif_marriage_proposal(int32 fd, map_session_data *sd, map_session_data* ssd)
  7831. {
  7832. nullpo_retv(sd);
  7833. WFIFOHEAD(fd,packet_len(0x1e2));
  7834. WFIFOW(fd,0) = 0x1e2;
  7835. WFIFOL(fd,2) = ssd->status.account_id;
  7836. WFIFOL(fd,6) = ssd->status.char_id;
  7837. safestrncpy(WFIFOCP(fd,10), ssd->status.name, NAME_LENGTH);
  7838. WFIFOSET(fd, packet_len(0x1e2));
  7839. }
  7840. */
  7841. /*==========================================
  7842. * Displays a message using the guild-chat colors to the specified targets. [Skotlex]
  7843. *------------------------------------------*/
  7844. void clif_disp_message(struct block_list* src, const char* mes, size_t len, enum send_target target)
  7845. {
  7846. unsigned char buf[256];
  7847. if( len == 0 ) {
  7848. return;
  7849. } else if( len > sizeof(buf)-5 ) {
  7850. ShowWarning("clif_disp_message: Truncated message '%s' (len=%d, max=%" PRIuPTR ", aid=%d).\n", mes, len, sizeof(buf)-5, src->id);
  7851. len = sizeof(buf)-5;
  7852. }
  7853. WBUFW(buf, 0) = 0x17f;
  7854. WBUFW(buf, 2) = static_cast<int16>( len + 5 );
  7855. safestrncpy(WBUFCP(buf,4), mes, len+1);
  7856. clif_send(buf, WBUFW(buf,2), src, target);
  7857. }
  7858. /// Notifies the client about the result of a request to disconnect another player (ZC_ACK_DISCONNECT_CHARACTER).
  7859. /// 00cd <result>.L (unknown packet version or invalid information at packet_len_table)
  7860. /// 00cd <result>.B
  7861. /// result:
  7862. /// 0 = failure
  7863. /// 1 = success
  7864. void clif_GM_kickack(map_session_data *sd, int32 id)
  7865. {
  7866. int32 fd;
  7867. nullpo_retv(sd);
  7868. fd = sd->fd;
  7869. WFIFOHEAD(fd,packet_len(0xcd));
  7870. WFIFOW(fd,0) = 0xcd;
  7871. WFIFOB(fd,2) = id; // FIXME: this is not account id
  7872. WFIFOSET(fd, packet_len(0xcd));
  7873. }
  7874. void clif_GM_kick(map_session_data *sd, map_session_data *tsd)
  7875. {
  7876. nullpo_retv(tsd);
  7877. if (sd == nullptr)
  7878. tsd->state.keepshop = true;
  7879. if (session_isActive(tsd->fd))
  7880. clif_authfail_fd(tsd->fd, 15);
  7881. else
  7882. map_quit(tsd);
  7883. if (sd)
  7884. clif_GM_kickack(sd, tsd->status.account_id);
  7885. }
  7886. /// Displays various manner-related status messages (ZC_ACK_GIVE_MANNER_POINT).
  7887. /// 014a <result>.L
  7888. /// result:
  7889. /// 0 = "A manner point has been successfully aligned."
  7890. /// 1 = MP_FAILURE_EXHAUST
  7891. /// 2 = MP_FAILURE_ALREADY_GIVING
  7892. /// 3 = "Chat Block has been applied by GM due to your ill-mannerous action."
  7893. /// 4 = "Automated Chat Block has been applied due to Anti-Spam System."
  7894. /// 5 = "You got a good point from %s."
  7895. void clif_manner_message(map_session_data* sd, uint32 type)
  7896. {
  7897. int32 fd;
  7898. nullpo_retv(sd);
  7899. fd = sd->fd;
  7900. WFIFOHEAD(fd,packet_len(0x14a));
  7901. WFIFOW(fd,0) = 0x14a;
  7902. WFIFOL(fd,2) = type;
  7903. WFIFOSET(fd, packet_len(0x14a));
  7904. }
  7905. /// Followup to 0x14a type 3/5, informs who did the manner adjustment action (ZC_NOTIFY_MANNER_POINT_GIVEN).
  7906. /// 014b <type>.B <GM name>.24B
  7907. /// type:
  7908. /// 0 = positive (unmute)
  7909. /// 1 = negative (mute)
  7910. void clif_GM_silence(map_session_data* sd, map_session_data* tsd, uint8 type)
  7911. {
  7912. int32 fd;
  7913. nullpo_retv(sd);
  7914. nullpo_retv(tsd);
  7915. fd = tsd->fd;
  7916. WFIFOHEAD(fd,packet_len(0x14b));
  7917. WFIFOW(fd,0) = 0x14b;
  7918. WFIFOB(fd,2) = type;
  7919. safestrncpy(WFIFOCP(fd,3), sd->status.name, NAME_LENGTH);
  7920. WFIFOSET(fd, packet_len(0x14b));
  7921. }
  7922. /// Notifies the client about the result of a request to allow/deny whispers from a player (ZC_SETTING_WHISPER_PC).
  7923. /// 00d1 <type>.B <result>.B
  7924. /// type:
  7925. /// 0 = /ex (deny)
  7926. /// 1 = /in (allow)
  7927. /// result:
  7928. /// 0 = success
  7929. /// 1 = failure
  7930. /// 2 = too many blocks
  7931. void clif_wisexin(map_session_data *sd,int32 type,int32 flag)
  7932. {
  7933. int32 fd;
  7934. nullpo_retv(sd);
  7935. fd=sd->fd;
  7936. WFIFOHEAD(fd,packet_len(0xd1));
  7937. WFIFOW(fd,0)=0xd1;
  7938. WFIFOB(fd,2)=type;
  7939. WFIFOB(fd,3)=flag;
  7940. WFIFOSET(fd,packet_len(0xd1));
  7941. }
  7942. /// Notifies the client about the result of a request to allow/deny whispers from anyone (ZC_SETTING_WHISPER_STATE).
  7943. /// 00d2 <type>.B <result>.B
  7944. /// type:
  7945. /// 0 = /exall (deny)
  7946. /// 1 = /inall (allow)
  7947. /// result:
  7948. /// 0 = success
  7949. /// 1 = failure
  7950. void clif_wisall(map_session_data *sd,int32 type,int32 flag)
  7951. {
  7952. int32 fd;
  7953. nullpo_retv(sd);
  7954. fd=sd->fd;
  7955. WFIFOHEAD(fd,packet_len(0xd2));
  7956. WFIFOW(fd,0)=0xd2;
  7957. WFIFOB(fd,2)=type;
  7958. WFIFOB(fd,3)=flag;
  7959. WFIFOSET(fd,packet_len(0xd2));
  7960. }
  7961. /// Play a BGM! [Rikter/Yommy] (ZC_PLAY_NPC_BGM).
  7962. /// 07fe <bgm>.24B
  7963. void clif_playBGM( map_session_data& sd, const char* name ){
  7964. #if PACKETVER_MAIN_NUM >= 20220504
  7965. PACKET_ZC_PLAY_NPC_BGM* p = reinterpret_cast<PACKET_ZC_PLAY_NPC_BGM*>( packet_buffer );
  7966. size_t length = strlen( name ) + 1;
  7967. p->PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7968. p->PacketLength = sizeof( *p );
  7969. p->playType = 0; // TODO: implement, send loop for the time being
  7970. safestrncpy( p->bgm, name, length );
  7971. p->PacketLength += static_cast<decltype(p->PacketLength)>( length );
  7972. clif_send( p, p->PacketLength, &sd.bl, SELF );
  7973. #elif PACKETVER >= 20091201
  7974. PACKET_ZC_PLAY_NPC_BGM p = {};
  7975. p.PacketType = HEADER_ZC_PLAY_NPC_BGM;
  7976. safestrncpy( p.bgm, name, sizeof( p.bgm ) );
  7977. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  7978. #endif
  7979. }
  7980. /// Plays/stops a wave sound (ZC_SOUND).
  7981. /// 01d3 <file name>.24B <act>.B <term>.L <npc id>.L
  7982. /// file name:
  7983. /// relative to data\wav
  7984. /// act:
  7985. /// 0 = play (once)
  7986. /// 1 = play (repeat, does not work)
  7987. /// 2 = stops all sound instances of file name (does not work)
  7988. /// term:
  7989. /// unknown purpose, only relevant to act = 1
  7990. /// npc id:
  7991. /// The accustic direction of the sound is determined by the
  7992. /// relative position of the NPC to the player (3D sound).
  7993. void clif_soundeffect( struct block_list& bl, const char* name, int32 type, enum send_target target ){
  7994. PACKET_ZC_SOUND p = {};
  7995. p.PacketType = HEADER_ZC_SOUND;
  7996. safestrncpy( p.name, name, sizeof( p.name ) );
  7997. p.act = type;
  7998. p.term = 0; // TODO: implement
  7999. p.AID = bl.id;
  8000. clif_send( &p, sizeof( p ), &bl, target );
  8001. }
  8002. /// Displays special effects (npcs, weather, etc) [Valaris] (ZC_NOTIFY_EFFECT2).
  8003. /// 01f3 <id>.L <effect id>.L
  8004. /// effect id:
  8005. /// @see doc/effect_list.txt
  8006. void clif_specialeffect(struct block_list* bl, int32 type, enum send_target target)
  8007. {
  8008. unsigned char buf[24];
  8009. nullpo_retv(bl);
  8010. memset(buf, 0, packet_len(0x1f3));
  8011. WBUFW(buf,0) = 0x1f3;
  8012. WBUFL(buf,2) = bl->id;
  8013. WBUFL(buf,6) = type;
  8014. clif_send(buf, packet_len(0x1f3), bl, target);
  8015. if (disguised(bl)) {
  8016. WBUFL(buf,2) = disguised_bl_id(bl->id);
  8017. clif_send(buf, packet_len(0x1f3), bl, SELF);
  8018. }
  8019. }
  8020. void clif_specialeffect_single(struct block_list* bl, int32 type, int32 fd)
  8021. {
  8022. WFIFOHEAD(fd,10);
  8023. WFIFOW(fd,0) = 0x1f3;
  8024. WFIFOL(fd,2) = bl->id;
  8025. WFIFOL(fd,6) = type;
  8026. WFIFOSET(fd,10);
  8027. }
  8028. /// Notifies clients of an special/visual effect that accepts an value (ZC_NOTIFY_EFFECT3).
  8029. /// 0284 <id>.L <effect id>.L <num data>.L
  8030. /// effect id:
  8031. /// @see doc/effect_list.txt
  8032. /// num data:
  8033. /// effect-dependent value
  8034. void clif_specialeffect_value(struct block_list* bl, int32 effect_id, int32 num, send_target target)
  8035. {
  8036. uint8 buf[14];
  8037. WBUFW(buf,0) = 0x284;
  8038. WBUFL(buf,2) = bl->id;
  8039. WBUFL(buf,6) = effect_id;
  8040. WBUFL(buf,10) = num;
  8041. clif_send(buf, packet_len(0x284), bl, target);
  8042. if( disguised(bl) )
  8043. {
  8044. WBUFL(buf,2) = disguised_bl_id(bl->id);
  8045. clif_send(buf, packet_len(0x284), bl, SELF);
  8046. }
  8047. }
  8048. void clif_specialeffect_remove(struct block_list* bl_src, int32 effect, enum send_target e_target, struct block_list* bl_target)
  8049. {
  8050. #if PACKETVER >= 20181002
  8051. nullpo_retv( bl_src );
  8052. nullpo_retv( bl_target );
  8053. PACKET_ZC_REMOVE_EFFECT p = {};
  8054. p.packetType = HEADER_ZC_REMOVE_EFFECT;
  8055. p.aid = bl_src->id;
  8056. p.effectId = effect;
  8057. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_target, e_target );
  8058. if( disguised(bl_src) )
  8059. {
  8060. p.aid = disguised_bl_id( bl_src->id );
  8061. clif_send( &p, sizeof( PACKET_ZC_REMOVE_EFFECT ), bl_src, SELF );
  8062. }
  8063. #endif
  8064. }
  8065. /// Monster/NPC color chat [SnakeDrak] (ZC_NPC_CHAT).
  8066. /// 02c1 <packet len>.W <id>.L <color>.L <message>.?B
  8067. void clif_messagecolor_target(struct block_list *bl, unsigned long color, const char *msg, bool rgb2bgr, enum send_target type, map_session_data *sd) {
  8068. unsigned short msg_len = (unsigned short)(strlen(msg) + 1);
  8069. uint8 buf[CHAT_SIZE_MAX];
  8070. nullpo_retv(bl);
  8071. if (msg_len > sizeof(buf) - 12) {
  8072. ShowWarning("clif_messagecolor: Truncating too long message '%s' (len=%u).\n", msg, msg_len);
  8073. msg_len = sizeof(buf) - 12;
  8074. }
  8075. if (rgb2bgr)
  8076. color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16; // RGB to BGR
  8077. WBUFW(buf,0) = 0x2C1;
  8078. WBUFW(buf,2) = msg_len + 12;
  8079. WBUFL(buf,4) = bl->id;
  8080. WBUFL(buf,8) = color;
  8081. memcpy(WBUFCP(buf,12), msg, msg_len);
  8082. clif_send(buf, WBUFW(buf,2), (sd == nullptr ? bl : &(sd->bl)), type);
  8083. }
  8084. /**
  8085. * Notifies the client that the storage window is still open
  8086. *
  8087. * Should only be used in cases where the client closed the
  8088. * storage window without server's consent
  8089. */
  8090. void clif_refresh_storagewindow(map_session_data *sd) {
  8091. // Notify the client that the storage is open
  8092. if( sd->state.storage_flag == 1 ) {
  8093. storage_sortitem(sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage));
  8094. clif_storagelist(sd, sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage), storage_getName(0));
  8095. clif_updatestorageamount(*sd, sd->storage.amount, sd->storage.max_amount);
  8096. }
  8097. // Notify the client that the gstorage is open otherwise it will
  8098. // remain locked forever and nobody will be able to access it
  8099. if( sd->state.storage_flag == 2 ) {
  8100. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  8101. if( !gstor ) // Shouldn't happen. The information should already be at the map-server
  8102. intif_request_guild_storage(sd->status.account_id, sd->status.guild_id);
  8103. else {
  8104. storage_sortitem(gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild));
  8105. clif_storagelist(sd, gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild), "Guild Storage");
  8106. clif_updatestorageamount(*sd, gstor->amount, gstor->max_amount);
  8107. }
  8108. }
  8109. // Notify the client that the premium storage is open
  8110. if (sd->state.storage_flag == 3) {
  8111. storage_sortitem(sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage));
  8112. clif_storagelist(sd, sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage), storage_getName(sd->premiumStorage.stor_id));
  8113. clif_updatestorageamount(*sd, sd->premiumStorage.amount, sd->premiumStorage.max_amount);
  8114. }
  8115. }
  8116. // refresh the client's screen, getting rid of any effects
  8117. void clif_refresh(map_session_data *sd)
  8118. {
  8119. nullpo_retv(sd);
  8120. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8121. clif_inventorylist(sd);
  8122. clif_equipswitch_list(sd);
  8123. if(pc_iscarton(sd)) {
  8124. clif_cartlist(sd);
  8125. clif_updatestatus(*sd,SP_CARTINFO);
  8126. }
  8127. clif_updatestatus(*sd,SP_WEIGHT);
  8128. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8129. clif_updatestatus(*sd,SP_STR);
  8130. clif_updatestatus(*sd,SP_AGI);
  8131. clif_updatestatus(*sd,SP_VIT);
  8132. clif_updatestatus(*sd,SP_INT);
  8133. clif_updatestatus(*sd,SP_DEX);
  8134. clif_updatestatus(*sd,SP_LUK);
  8135. #ifdef RENEWAL
  8136. clif_updatestatus(*sd,SP_POW);
  8137. clif_updatestatus(*sd,SP_STA);
  8138. clif_updatestatus(*sd,SP_WIS);
  8139. clif_updatestatus(*sd,SP_SPL);
  8140. clif_updatestatus(*sd,SP_CON);
  8141. clif_updatestatus(*sd,SP_CRT);
  8142. #endif
  8143. if (sd->spiritball)
  8144. clif_spiritball( &sd->bl, &sd->bl, SELF );
  8145. clif_millenniumshield_single( *sd, *sd );
  8146. clif_spiritcharm_single( *sd, *sd );
  8147. if (sd->soulball)
  8148. clif_soulball( sd, &sd->bl, SELF );
  8149. if (sd->servantball)
  8150. clif_servantball( *sd, &sd->bl, SELF );
  8151. if (sd->abyssball)
  8152. clif_abyssball( *sd, &sd->bl, SELF );
  8153. if (sd->vd.cloth_color)
  8154. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8155. if (sd->vd.body_style)
  8156. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8157. if(hom_is_active(sd->hd))
  8158. clif_send_homdata( *sd->hd, SP_ACK );
  8159. if( sd->md ) {
  8160. clif_mercenary_info(sd);
  8161. clif_mercenary_skillblock(sd);
  8162. }
  8163. if( sd->ed )
  8164. clif_elemental_info(sd);
  8165. map_foreachinallrange(clif_getareachar,&sd->bl,AREA_SIZE,BL_ALL,sd);
  8166. clif_weather_check(sd);
  8167. if( sd->chatID )
  8168. chat_leavechat(sd,0);
  8169. if( sd->state.vending )
  8170. clif_openvending( *sd );
  8171. if( pc_issit(sd) )
  8172. clif_sitting(sd->bl); // FIXME: just send to self, not area
  8173. if( pc_isdead(sd) ) // When you refresh, resend the death packet.
  8174. clif_clearunit_single( sd->bl.id, CLR_DEAD, *sd );
  8175. else
  8176. clif_changed_dir(sd->bl, SELF);
  8177. clif_efst_status_change_sub(&sd->bl,&sd->bl,SELF);
  8178. //Issue #2143
  8179. //Cancel Trading State
  8180. if (sd->state.trading)
  8181. trade_tradecancel(sd);
  8182. //Cancel Buying/Selling State
  8183. if (sd->state.buyingstore)
  8184. buyingstore_close(sd);
  8185. mail_clear(sd);
  8186. clif_loadConfirm( sd );
  8187. if( disguised(&sd->bl) ) {/* refresh-da */
  8188. short disguise = sd->disguise;
  8189. pc_disguise(sd, 0);
  8190. pc_disguise(sd, disguise);
  8191. }
  8192. clif_refresh_storagewindow(sd);
  8193. }
  8194. /// Updates the object's (bl) name on client.
  8195. /// Used to update when a char leaves a party/guild. [Skotlex]
  8196. /// Needed because when you send a 0x95 packet, the client will not remove the cached party/guild info that is not sent.
  8197. /// 0095 <id>.L <char name>.24B (ZC_ACK_REQNAME)
  8198. /// 0195 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B (ZC_ACK_REQNAMEALL)
  8199. /// 0a30 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B <title ID>.L (ZC_ACK_REQNAMEALL2)
  8200. void clif_name( struct block_list* src, struct block_list *bl, send_target target ){
  8201. nullpo_retv( src );
  8202. nullpo_retv( bl );
  8203. switch( bl->type ){
  8204. case BL_PC: {
  8205. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8206. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8207. packet.gid = bl->id;
  8208. map_session_data *sd = (map_session_data *)bl;
  8209. //Requesting your own "shadow" name. [Skotlex]
  8210. if( src == bl && target == SELF && sd->disguise ){
  8211. packet.gid = -bl->id;
  8212. }
  8213. if( sd->fakename[0] ) {
  8214. safestrncpy( packet.name, sd->fakename, NAME_LENGTH );
  8215. clif_send( &packet, sizeof(packet), src, target );
  8216. return;
  8217. }
  8218. safestrncpy( packet.name, sd->status.name, NAME_LENGTH );
  8219. party_data *p = nullptr;
  8220. if( sd->status.party_id ){
  8221. p = party_search( sd->status.party_id );
  8222. }
  8223. // do not display party unless the player is also in a guild
  8224. if( p && ( sd->guild || battle_config.display_party_name ) ){
  8225. safestrncpy( packet.party_name, p->party.name, NAME_LENGTH );
  8226. }
  8227. if( sd->guild ){
  8228. int32 position;
  8229. // Will get the position of the guild the player is in
  8230. position = guild_getposition(*sd);
  8231. safestrncpy( packet.guild_name, sd->guild->guild.name, NAME_LENGTH );
  8232. safestrncpy( packet.position_name, sd->guild->guild.position[position].name, NAME_LENGTH );
  8233. }else if( sd->clan ){
  8234. safestrncpy( packet.position_name, sd->clan->name, NAME_LENGTH );
  8235. }
  8236. #if PACKETVER_MAIN_NUM >= 20150225 || PACKETVER_RE_NUM >= 20141126 || defined( PACKETVER_ZERO )
  8237. packet.title_id = sd->status.title_id; // Title ID
  8238. #endif
  8239. clif_send(&packet, sizeof(packet), src, target);
  8240. }
  8241. break;
  8242. //[blackhole89]
  8243. case BL_HOM:
  8244. case BL_MER:
  8245. case BL_PET:
  8246. case BL_NPC:
  8247. case BL_ELEM: {
  8248. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8249. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8250. packet.gid = bl->id;
  8251. switch (bl->type) {
  8252. case BL_HOM:
  8253. memcpy(packet.name, ((TBL_HOM *)bl)->homunculus.name, NAME_LENGTH);
  8254. break;
  8255. case BL_MER:
  8256. memcpy(packet.name, ((TBL_MER *)bl)->db->name.c_str(), NAME_LENGTH);
  8257. break;
  8258. case BL_PET:
  8259. safestrncpy(packet.name, ((TBL_PET *)bl)->pet.name, NAME_LENGTH);
  8260. break;
  8261. case BL_NPC:
  8262. safestrncpy(packet.name, ((TBL_NPC *)bl)->name, NAME_LENGTH);
  8263. break;
  8264. case BL_ELEM:
  8265. safestrncpy(packet.name, ((TBL_ELEM *)bl)->db->name.c_str(), NAME_LENGTH);
  8266. break;
  8267. }
  8268. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8269. unit_data *ud = unit_bl2ud(bl);
  8270. if (ud != nullptr) {
  8271. memcpy(packet.title, ud->title, NAME_LENGTH);
  8272. packet.groupId = ud->group_id;
  8273. }
  8274. #endif
  8275. clif_send(&packet, sizeof(packet), src, target);
  8276. }
  8277. break;
  8278. case BL_MOB: {
  8279. mob_data *md = (mob_data *)bl;
  8280. if( md->guardian_data && md->guardian_data->guild_id ){
  8281. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8282. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8283. packet.gid = bl->id;
  8284. safestrncpy( packet.name, md->name, NAME_LENGTH );
  8285. safestrncpy( packet.guild_name, md->guardian_data->guild_name, NAME_LENGTH );
  8286. safestrncpy( packet.position_name, md->guardian_data->castle->castle_name, NAME_LENGTH );
  8287. clif_send(&packet, sizeof(packet), src, target);
  8288. }else if( battle_config.show_mob_info ){
  8289. PACKET_ZC_ACK_REQNAMEALL packet = { 0 };
  8290. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL;
  8291. packet.gid = bl->id;
  8292. safestrncpy( packet.name, md->name, NAME_LENGTH );
  8293. char mobhp[50], *str_p = mobhp;
  8294. if( battle_config.show_mob_info&4 ){
  8295. str_p += sprintf( str_p, "Lv. %d | ", md->level );
  8296. }
  8297. if( battle_config.show_mob_info&1 ){
  8298. str_p += sprintf( str_p, "HP: %u/%u | ", md->status.hp, md->status.max_hp );
  8299. }
  8300. if( battle_config.show_mob_info&2 ){
  8301. str_p += sprintf( str_p, "HP: %u%% | ", get_percentage( md->status.hp, md->status.max_hp ) );
  8302. }
  8303. // Even thought mobhp ain't a name, we send it as one so the client can parse it. [Skotlex]
  8304. if( str_p != mobhp ){
  8305. *(str_p-3) = '\0'; //Remove trailing space + pipe.
  8306. safestrncpy( packet.party_name, mobhp, NAME_LENGTH );
  8307. }
  8308. clif_send(&packet, sizeof(packet), src, target);
  8309. } else {
  8310. PACKET_ZC_ACK_REQNAMEALL_NPC packet = { 0 };
  8311. packet.packet_id = HEADER_ZC_ACK_REQNAMEALL_NPC;
  8312. packet.gid = bl->id;
  8313. safestrncpy(packet.name, md->name, NAME_LENGTH);
  8314. #if PACKETVER_MAIN_NUM >= 20180207 || PACKETVER_RE_NUM >= 20171129 || PACKETVER_ZERO_NUM >= 20171130
  8315. unit_data *ud = unit_bl2ud(bl);
  8316. if (ud != nullptr) {
  8317. memcpy(packet.title, ud->title, NAME_LENGTH);
  8318. packet.groupId = ud->group_id;
  8319. }
  8320. #endif
  8321. clif_send(&packet, sizeof(packet), src, target);
  8322. }
  8323. }
  8324. break;
  8325. case BL_CHAT:
  8326. case BL_SKILL:
  8327. // Newer clients request this, but do not need an answer
  8328. return;
  8329. default:
  8330. ShowError("clif_name: bad type %d(%d)\n", bl->type, bl->id);
  8331. return;
  8332. }
  8333. }
  8334. /// Taekwon Jump (TK_HIGHJUMP) effect (ZC_HIGHJUMP).
  8335. /// 01ff <id>.L <x>.W <y>.W
  8336. ///
  8337. /// Visually moves(instant) a character to x,y. The char moves even
  8338. /// when the target cell isn't walkable. If the char is sitting it
  8339. /// stays that way.
  8340. void clif_slide(block_list& bl, int32 x, int32 y){
  8341. PACKET_ZC_HIGHJUMP p{};
  8342. p.packetType = HEADER_ZC_HIGHJUMP;
  8343. p.srcId = bl.id;
  8344. p.x = x;
  8345. p.y = y;
  8346. clif_send(&p, sizeof(p), &bl, AREA);
  8347. if( disguised(&bl) )
  8348. {
  8349. p.srcId = disguised_bl_id(bl.id);
  8350. clif_send(&p, sizeof(p), &bl, SELF);
  8351. }
  8352. }
  8353. /// Public chat message (ZC_NOTIFY_CHAT). lordalfa/Skotlex - used by @me as well
  8354. /// 008d <packet len>.W <id>.L <message>.?B
  8355. void clif_disp_overhead_(struct block_list *bl, const char* mes, enum send_target flag)
  8356. {
  8357. unsigned char buf[256]; //This should be more than sufficient, the theorical max is CHAT_SIZE + 8 (pads and extra inserted crap)
  8358. int16 len_mes;
  8359. len_mes = static_cast<decltype(len_mes)>( strlen(mes) + 1 ); //Account for \0
  8360. if (len_mes > sizeof(buf)-8) {
  8361. ShowError("clif_disp_overhead: Message too long (length %d)\n", len_mes);
  8362. len_mes = sizeof(buf)-8; //Trunk it to avoid problems.
  8363. }
  8364. // send message to others
  8365. if (flag == AREA) {
  8366. WBUFW(buf,0) = 0x8d;
  8367. WBUFW(buf,2) = len_mes + 8; // len of message + 8 (command+len+id)
  8368. WBUFL(buf,4) = bl->id;
  8369. safestrncpy(WBUFCP(buf,8), mes, len_mes);
  8370. clif_send(buf, WBUFW(buf,2), bl, AREA_CHAT_WOC);
  8371. }
  8372. // send back message to the speaker
  8373. if( bl->type == BL_PC ) {
  8374. WBUFW(buf,0) = 0x8e;
  8375. WBUFW(buf, 2) = len_mes + 4;
  8376. safestrncpy(WBUFCP(buf,4), mes, len_mes);
  8377. clif_send(buf, WBUFW(buf,2), bl, SELF);
  8378. }
  8379. }
  8380. /*==========================
  8381. * Minimap fix [Kevin]
  8382. * Remove dot from minimap
  8383. *--------------------------*/
  8384. void clif_party_xy_remove(map_session_data* sd)
  8385. {
  8386. nullpo_retv(sd);
  8387. PACKET_ZC_NOTIFY_POSITION_TO_GROUPM p{};
  8388. p.PacketType = HEADER_ZC_NOTIFY_POSITION_TO_GROUPM;
  8389. p.AID = sd->status.account_id;
  8390. p.xPos = -1;
  8391. p.yPos = -1;
  8392. clif_send(&p, sizeof(p), &sd->bl, PARTY_SAMEMAP_WOS);
  8393. }
  8394. /// Displays a skill message (thanks to Rayce) (ZC_SKILLMSG).
  8395. /// 0215 <msg id>.L
  8396. /// msg id:
  8397. /// 0x15 = End all negative status (PA_GOSPEL)
  8398. /// 0x16 = Immunity to all status (PA_GOSPEL)
  8399. /// 0x17 = MaxHP +100% (PA_GOSPEL)
  8400. /// 0x18 = MaxSP +100% (PA_GOSPEL)
  8401. /// 0x19 = All stats +20 (PA_GOSPEL)
  8402. /// 0x1c = Enchant weapon with Holy element (PA_GOSPEL)
  8403. /// 0x1d = Enchant armor with Holy element (PA_GOSPEL)
  8404. /// 0x1e = DEF +25% (PA_GOSPEL)
  8405. /// 0x1f = ATK +100% (PA_GOSPEL)
  8406. /// 0x20 = HIT/Flee +50 (PA_GOSPEL)
  8407. /// 0x28 = Full strip failed because of coating (ST_FULLSTRIP)
  8408. /// ? = nothing
  8409. void clif_gospel_info(map_session_data *sd, int32 type)
  8410. {
  8411. int32 fd=sd->fd;
  8412. WFIFOHEAD(fd,packet_len(0x215));
  8413. WFIFOW(fd,0)=0x215;
  8414. WFIFOL(fd,2)=type;
  8415. WFIFOSET(fd, packet_len(0x215));
  8416. }
  8417. /// Multi-purpose mission information packet (ZC_STARSKILL).
  8418. /// 020e <mapname>.24B <monster_id>.L <star>.B <result>.B
  8419. /// result:
  8420. /// 0 = Star Gladiator %s has designed <mapname>'s as the %s.
  8421. /// star:
  8422. /// 0 = Place of the Sun
  8423. /// 1 = Place of the Moon
  8424. /// 2 = Place of the Stars
  8425. /// 1 = Star Gladiator %s's %s: <mapname>
  8426. /// star:
  8427. /// 0 = Place of the Sun
  8428. /// 1 = Place of the Moon
  8429. /// 2 = Place of the Stars
  8430. /// 10 = Star Gladiator %s has designed <mapname>'s as the %s.
  8431. /// star:
  8432. /// 0 = Target of the Sun
  8433. /// 1 = Target of the Moon
  8434. /// 2 = Target of the Stars
  8435. /// 11 = Star Gladiator %s's %s: <mapname used as monster name>
  8436. /// star:
  8437. /// 0 = Monster of the Sun
  8438. /// 1 = Monster of the Moon
  8439. /// 2 = Monster of the Stars
  8440. /// 20 = [TaeKwon Mission] Target Monster : <mapname used as monster name> (<star>%)
  8441. /// 21 = [Taming Mission] Target Monster : <mapname used as monster name>
  8442. /// 22 = [Collector Rank] Target Item : <monster_id used as item id>
  8443. /// 30 = [Sun, Moon and Stars Angel] Designed places and monsters have been reset.
  8444. /// 40 = Target HP : <monster_id used as HP>
  8445. void clif_starskill(map_session_data* sd, const char* mapname, int32 monster_id, unsigned char star, unsigned char result)
  8446. {
  8447. int32 fd = sd->fd;
  8448. WFIFOHEAD(fd,packet_len(0x20e));
  8449. WFIFOW(fd,0) = 0x20e;
  8450. safestrncpy(WFIFOCP(fd,2), mapname, NAME_LENGTH);
  8451. WFIFOL(fd,26) = monster_id;
  8452. WFIFOB(fd,30) = star;
  8453. WFIFOB(fd,31) = result;
  8454. WFIFOSET(fd,packet_len(0x20e));
  8455. }
  8456. /*==========================================
  8457. * Info about Star Glaldiator save map [Komurka]
  8458. * type: 1: Information, 0: Map registered
  8459. *------------------------------------------*/
  8460. void clif_feel_info(map_session_data* sd, unsigned char feel_level, unsigned char type)
  8461. {
  8462. char mapname[MAP_NAME_LENGTH_EXT];
  8463. mapindex_getmapname_ext(mapindex_id2name(sd->feel_map[feel_level].index), mapname);
  8464. clif_starskill(sd, mapname, 0, feel_level, type ? 1 : 0);
  8465. }
  8466. /*==========================================
  8467. * Info about Star Glaldiator hate mob [Komurka]
  8468. * type: 1: Register mob, 0: Information.
  8469. *------------------------------------------*/
  8470. void clif_hate_info(map_session_data *sd, unsigned char hate_level,int32 class_, unsigned char type)
  8471. {
  8472. if( pcdb_checkid(class_) ) {
  8473. clif_starskill(sd, job_name(class_), class_, hate_level, type ? 10 : 11);
  8474. } else if( mobdb_checkid(class_) ) {
  8475. clif_starskill(sd, mob_db.find(class_)->jname.c_str(), class_, hate_level, type ? 10 : 11);
  8476. } else {
  8477. 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);
  8478. }
  8479. }
  8480. /*==========================================
  8481. * Info about TaeKwon Do TK_MISSION mob [Skotlex]
  8482. *------------------------------------------*/
  8483. void clif_mission_info(map_session_data *sd, int32 mob_id, unsigned char progress)
  8484. {
  8485. clif_starskill(sd, mob_db.find(mob_id)->jname.c_str(), mob_id, progress, 20);
  8486. }
  8487. /*==========================================
  8488. * Feel/Hate reset (thanks to Rayce) [Skotlex]
  8489. *------------------------------------------*/
  8490. void clif_feel_hate_reset(map_session_data *sd)
  8491. {
  8492. clif_starskill(sd, "", 0, 0, 30);
  8493. }
  8494. /// Send out reply to configuration change
  8495. /// 02d9 <type>.L <value>.L (ZC_CONFIG)
  8496. /// type: see enum e_config_type
  8497. /// value:
  8498. /// false = disabled
  8499. /// true = enabled
  8500. void clif_configuration( map_session_data* sd, enum e_config_type type, bool enabled ){
  8501. int32 fd;
  8502. nullpo_retv(sd);
  8503. fd = sd->fd;
  8504. WFIFOHEAD(fd, packet_len(0x2d9));
  8505. WFIFOW(fd, 0) = 0x2d9;
  8506. WFIFOL(fd, 2) = type;
  8507. WFIFOL(fd, 6) = enabled;
  8508. WFIFOSET(fd, packet_len(0x2d9));
  8509. }
  8510. /// The player's 'view equip' state, sent during login (ZC_CONFIG_NOTIFY).
  8511. /// 02da <open equip window>.B
  8512. /// open equip window:
  8513. /// 0 = disabled
  8514. /// 1 = enabled
  8515. void clif_equipcheckbox(map_session_data* sd)
  8516. {
  8517. int32 fd;
  8518. nullpo_retv(sd);
  8519. fd = sd->fd;
  8520. WFIFOHEAD(fd, packet_len(0x2da));
  8521. WFIFOW(fd, 0) = 0x2da;
  8522. WFIFOB(fd, 2) = (sd->status.show_equip ? 1 : 0);
  8523. WFIFOSET(fd, packet_len(0x2da));
  8524. }
  8525. /// Sends info about a player's equipped items.
  8526. /// 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)
  8527. /// 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)
  8528. /// 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)
  8529. /// 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)
  8530. /// 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)
  8531. /// 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)
  8532. void clif_viewequip_ack( map_session_data& sd, map_session_data& tsd ){
  8533. #if PACKETVER_AD_NUM >= 20071211 || PACKETVER_SAK_NUM >= 20071127 || PACKETVER_MAIN_NUM >= 20071211 || defined(PACKETVER_RE)
  8534. PACKET_ZC_EQUIPWIN_MICROSCOPE* p = reinterpret_cast<PACKET_ZC_EQUIPWIN_MICROSCOPE*>( packet_buffer );
  8535. p->PacketType = HEADER_ZC_EQUIPWIN_MICROSCOPE;
  8536. p->PacketLength = sizeof( *p );
  8537. safestrncpy( p->characterName, tsd.status.name, NAME_LENGTH );
  8538. p->job = tsd.status.class_;
  8539. p->head = tsd.vd.hair_style;
  8540. p->accessory = tsd.vd.head_bottom;
  8541. p->accessory2 = tsd.vd.head_mid;
  8542. p->accessory3 = tsd.vd.head_top;
  8543. #if PACKETVER >= 20110111
  8544. p->robe = tsd.vd.robe;
  8545. #endif
  8546. p->headpalette = tsd.vd.hair_color;
  8547. p->bodypalette = tsd.vd.cloth_color;
  8548. #if PACKETVER_MAIN_NUM >= 20180801 || PACKETVER_RE_NUM >= 20180801 || PACKETVER_ZERO_NUM >= 20180808
  8549. p->body2 = tsd.vd.body_style;
  8550. #endif
  8551. p->sex = tsd.vd.sex;
  8552. for( int32 i = 0, equip = 0; i < EQI_MAX; i++ ){
  8553. int32 k = tsd.equip_index[i];
  8554. if( k >= 0 ){
  8555. if( tsd.inventory.u.items_inventory[k].nameid == 0 || tsd.inventory_data[k] == nullptr ){ // Item doesn't exist
  8556. continue;
  8557. }
  8558. if( !tsd.inventory.u.items_inventory[k].equip ){
  8559. continue;
  8560. }
  8561. if( !itemdb_isequip2( tsd.inventory_data[k] ) ){ // Is not equippable
  8562. continue;
  8563. }
  8564. clif_item_equip( client_index( k ), &p->list[equip], &tsd.inventory.u.items_inventory[k], tsd.inventory_data[k], pc_equippoint( &tsd, k ) );
  8565. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->list[0] ) );
  8566. equip++;
  8567. }
  8568. }
  8569. clif_send( p, p->PacketLength, &sd.bl, SELF );
  8570. #endif
  8571. }
  8572. /// Display msgstringtable.txt string (ZC_MSG).
  8573. /// 0291 <message>.W
  8574. void clif_msg(map_session_data* sd, unsigned short id)
  8575. {
  8576. int32 fd;
  8577. nullpo_retv(sd);
  8578. fd = sd->fd;
  8579. WFIFOHEAD(fd, packet_len(0x291));
  8580. WFIFOW(fd, 0) = 0x291;
  8581. WFIFOW(fd, 2) = id; // zero-based msgstringtable.txt index
  8582. WFIFOSET(fd, packet_len(0x291));
  8583. }
  8584. /// Display msgstringtable.txt string and fill in a valid for %d format (ZC_MSG_VALUE).
  8585. /// 0x7e2 <message>.W <value>.L
  8586. void clif_msg_value(map_session_data* sd, unsigned short id, int32 value)
  8587. {
  8588. int32 fd = sd->fd;
  8589. WFIFOHEAD(fd, packet_len(0x7e2));
  8590. WFIFOW(fd,0) = 0x7e2;
  8591. WFIFOW(fd,2) = id;
  8592. WFIFOL(fd,4) = value;
  8593. WFIFOSET(fd, packet_len(0x7e2));
  8594. }
  8595. /// Displays msgstringtable.txt string, prefixed with a skill name. (ZC_MSG_SKILL).
  8596. /// 07e6 <skill id>.W <msg id>.L
  8597. ///
  8598. /// NOTE: Message has following format and is printed in color 0xCDCDFF (purple):
  8599. /// "[SkillName] Message"
  8600. void clif_msg_skill(map_session_data* sd, uint16 skill_id, int32 msg_id)
  8601. {
  8602. int32 fd = sd->fd;
  8603. WFIFOHEAD(fd, packet_len(0x7e6));
  8604. WFIFOW(fd,0) = 0x7e6;
  8605. WFIFOW(fd,2) = skill_id;
  8606. WFIFOL(fd,4) = msg_id;
  8607. WFIFOSET(fd, packet_len(0x7e6));
  8608. }
  8609. /// Displays msgstringtable.txt string in a color. (ZC_MSG_COLOR).
  8610. /// 09cd <msg id>.W <color>.L
  8611. void clif_msg_color( map_session_data *sd, uint16 msg_id, uint32 color ){
  8612. nullpo_retv(sd);
  8613. int32 fd = sd->fd;
  8614. WFIFOHEAD(fd, packet_len(0x9cd));
  8615. WFIFOW(fd, 0) = 0x9cd;
  8616. WFIFOW(fd, 2) = msg_id;
  8617. WFIFOL(fd, 4) = color;
  8618. WFIFOSET(fd, packet_len(0x9cd));
  8619. }
  8620. /// Validates one global/guild/party/whisper message packet and tries to recognize its components.
  8621. /// Returns true if the packet was parsed successfully.
  8622. /// Formats: false - <packet id>.w <packet len>.w (<name> : <message>).?B 00
  8623. /// true - <packet id>.w <packet len>.w <name>.24B <message>.?B 00
  8624. static bool clif_process_message(map_session_data* sd, bool whisperFormat, char* out_name, char* out_message, char* out_full_message ){
  8625. const char* seperator = " : ";
  8626. int32 fd;
  8627. struct s_packet_db* info;
  8628. uint16 packetLength, inputLength;
  8629. const char *input, *name, *message;
  8630. size_t nameLength, messageLength;
  8631. fd = sd->fd;
  8632. info = &packet_db[RFIFOW(fd,0)];
  8633. packetLength = RFIFOW(fd,info->pos[0]);
  8634. input = RFIFOCP(fd,info->pos[1]);
  8635. // basic structure check for the 4-byte header
  8636. if( packetLength < 4 ){
  8637. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no message data)!\n", sd->status.name);
  8638. return false;
  8639. }else{
  8640. inputLength = packetLength - 4;
  8641. }
  8642. // process <name> part of the packet
  8643. if( whisperFormat ) {
  8644. // name has fixed width
  8645. if( inputLength < NAME_LENGTH + 1 ) {
  8646. ShowWarning("clif_process_message: Received malformed packet from player '%s' (packet length is incorrect)!\n", sd->status.name);
  8647. return false;
  8648. }
  8649. name = input;
  8650. // validate name
  8651. nameLength = strnlen( name, NAME_LENGTH - 1 );
  8652. // only restriction is that the name must be zero-terminated
  8653. if( name[nameLength] != '\0' ) {
  8654. ShowWarning("clif_process_message: Player '%s' sent an unterminated name!\n", sd->status.name);
  8655. return false;
  8656. }
  8657. message = input + NAME_LENGTH;
  8658. messageLength = inputLength - NAME_LENGTH;
  8659. }else{
  8660. // name and message are separated by ' : '
  8661. size_t seperatorLength = strnlen( seperator, NAME_LENGTH );
  8662. nameLength = strnlen( sd->status.name, NAME_LENGTH - 1 ); // name length (w/o zero byte)
  8663. // check if there's enough data provided
  8664. if( inputLength < nameLength + seperatorLength + 1 ){
  8665. ShowWarning("clif_process_message: Received malformed packet from player '%s' (no username data)!\n", sd->status.name);
  8666. return false;
  8667. }
  8668. name = input;
  8669. // validate name
  8670. if( strncmp( name, sd->status.name, nameLength ) || // the text must start with the speaker's name
  8671. strncmp( name + nameLength, seperator, seperatorLength ) ) // followed by the seperator
  8672. {
  8673. //Hacked message, or infamous "client desynch" issue where they pick one char while loading another.
  8674. ShowWarning("clif_process_message: Player '%s' sent a message using an incorrect name! Forcing a relog...\n", sd->status.name);
  8675. set_eof(sd->fd); // Just kick them out to correct it.
  8676. return false;
  8677. }
  8678. message = input + nameLength + seperatorLength;
  8679. messageLength = inputLength - nameLength - seperatorLength;
  8680. }
  8681. #if PACKETVER < 20151001
  8682. // the declared length must match real length
  8683. if( messageLength != strnlen(message, messageLength)+1 ) {
  8684. ShowWarning("clif_process_message: Received malformed packet from player '%s' (length is incorrect)!\n", sd->status.name);
  8685. return false;
  8686. }
  8687. // verify <message> part of the packet
  8688. if( message[messageLength-1] != '\0' ) { // message must be zero-terminated
  8689. ShowWarning("clif_process_message: Player '%s' sent an unterminated message string!\n", sd->status.name);
  8690. return false;
  8691. }
  8692. #else
  8693. // No zero termination anymore
  8694. messageLength += 1;
  8695. #endif
  8696. // messages mustn't be too long
  8697. if( messageLength > CHAT_SIZE_MAX-1 ) {
  8698. // Normally you can only enter CHATBOX_SIZE-1 letters into the chat box, but Frost Joke / Dazzler's text can be longer.
  8699. // Also, the physical size of strings that use multibyte encoding can go multiple times over the chatbox capacity.
  8700. // Neither the official client nor server place any restriction on the length of the data in the packet,
  8701. // but we'll only allow reasonably long strings here. This also makes sure that they fit into the `chatlog` table.
  8702. ShowWarning("clif_process_message: Player '%s' sent a message too long ('%.*s')!\n", sd->status.name, CHAT_SIZE_MAX-1, message);
  8703. return false;
  8704. }
  8705. // If it is not a whisper message, set the name to the fakename of the player
  8706. if( whisperFormat == false && sd->fakename[0] != '\0' ){
  8707. strcpy( out_name, sd->fakename );
  8708. }else{
  8709. safestrncpy( out_name, name, nameLength + 1 );
  8710. }
  8711. safestrncpy( out_message, message, messageLength );
  8712. if( whisperFormat ){
  8713. sprintf( out_full_message, "%-24s%s", out_name, out_message );
  8714. out_full_message[nameLength] = '\0';
  8715. }else{
  8716. sprintf( out_full_message, "%s%s%s", out_name, seperator, out_message );
  8717. }
  8718. if( is_atcommand( fd, sd, out_message, 1 ) )
  8719. return false;
  8720. if (sd->sc.cant.chat || (sd->state.block_action & PCBLOCK_CHAT))
  8721. return false; //no "chatting" while muted.
  8722. if( battle_config.min_chat_delay ) { //[Skotlex]
  8723. if (DIFF_TICK(sd->cantalk_tick, gettick()) > 0)
  8724. return false;
  8725. sd->cantalk_tick = gettick() + battle_config.min_chat_delay;
  8726. }
  8727. if (battle_config.idletime_option&IDLE_CHAT)
  8728. sd->idletime = last_tick;
  8729. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_CHAT)
  8730. sd->idletime_hom = last_tick;
  8731. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_CHAT)
  8732. sd->idletime_mer = last_tick;
  8733. //achievement_update_objective(sd, AG_CHATTING, 1, 1); // !TODO: Confirm how this achievement is triggered
  8734. return true;
  8735. }
  8736. /**
  8737. * Displays a message if the player enters a PK Zone (during pk_mode)
  8738. * @param sd: Player data
  8739. */
  8740. inline void clif_pk_mode_message(map_session_data * sd)
  8741. {
  8742. if (battle_config.pk_mode && battle_config.pk_mode_mes &&
  8743. sd && map_getmapflag(sd->bl.m, MF_PVP)) {
  8744. if( (int32)sd->status.base_level < battle_config.pk_min_level ) {
  8745. char output[CHAT_SIZE_MAX];
  8746. // 1504: You've entered a PK Zone (safe until level %d).
  8747. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(sd,1504),
  8748. battle_config.pk_min_level);
  8749. clif_showscript(&sd->bl, output, SELF);
  8750. } else {
  8751. // 1503: You've entered a PK Zone.
  8752. clif_showscript(&sd->bl, msg_txt(sd,1503), SELF);
  8753. }
  8754. }
  8755. return;
  8756. }
  8757. // ---------------------
  8758. // clif_parse_wanttoconnect
  8759. // ---------------------
  8760. // Parses a WantToConnection packet to try to identify which is the packet version used. [Skotlex]
  8761. // error codes:
  8762. // 0 - Success
  8763. // 1 - Unknown packet_ver
  8764. // 2 - Invalid account_id
  8765. // 3 - Invalid char_id
  8766. // 4 - Invalid login_id1 (reserved)
  8767. // 5 - Invalid client_tick (reserved)
  8768. // 6 - Invalid sex
  8769. // Only the first 'invalid' error that appears is used.
  8770. static int32 clif_parse_WantToConnection_sub(int32 fd)
  8771. {
  8772. int32 value; //Value is used to temporarily store account/char_id/sex
  8773. //By default, start searching on the default one.
  8774. uint16 cmd = RFIFOW(fd, 0);
  8775. int16 packet_len;
  8776. packet_len = static_cast<decltype(packet_len)>( RFIFOREST( fd ) );
  8777. // FIXME: If the packet is not received at once, this will FAIL.
  8778. // Figure out, when it happens, that only part of the packet is
  8779. // received, or fix the function to be able to deal with that
  8780. // case.
  8781. if( packet_len != packet_db[cmd].len )
  8782. return 1; /* wrong length */
  8783. else if( (value=(int32)RFIFOL(fd, packet_db[cmd].pos[0])) < START_ACCOUNT_NUM || value > END_ACCOUNT_NUM )
  8784. return 2; /* invalid account_id */
  8785. else if( (value=(int32)RFIFOL(fd, packet_db[cmd].pos[1])) <= 0 )
  8786. return 3; /* invalid char_id */
  8787. /* RFIFOL(fd, packet_db[cmd].pos[2]) - don't care about login_id1 */
  8788. /* RFIFOL(fd, packet_db[cmd].pos[3]) - don't care about client_tick */
  8789. else if( (value=(int32)RFIFOB(fd, packet_db[cmd].pos[4])) != 0 && value != 1 )
  8790. return 6; /* invalid sex */
  8791. else
  8792. return 0;
  8793. }
  8794. /// Request to connect to map-server.
  8795. /// 0072 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER)
  8796. /// 0436 <account id>.L <char id>.L <auth code>.L <client time>.L <gender>.B (CZ_ENTER2)
  8797. /// There are various variants of this packet, some of them have padding between fields.
  8798. void clif_parse_WantToConnection(int32 fd, map_session_data* sd)
  8799. {
  8800. struct block_list* bl;
  8801. struct auth_node* node;
  8802. int32 cmd, account_id, char_id, login_id1, sex, err;
  8803. t_tick client_tick; //The client tick is a tick, therefore it needs be unsigned. [Skotlex]
  8804. if (sd) {
  8805. ShowError("clif_parse_WantToConnection : invalid request (character already logged in)\n");
  8806. return;
  8807. }
  8808. cmd = RFIFOW(fd, 0);
  8809. account_id = RFIFOL(fd, packet_db[cmd].pos[0]);
  8810. char_id = RFIFOL(fd, packet_db[cmd].pos[1]);
  8811. login_id1 = RFIFOL(fd, packet_db[cmd].pos[2]);
  8812. client_tick = RFIFOL(fd, packet_db[cmd].pos[3]);
  8813. sex = RFIFOB(fd, packet_db[cmd].pos[4]);
  8814. err = clif_parse_WantToConnection_sub(fd);
  8815. if( err ){ // connection rejected
  8816. ShowInfo("clif_parse: Disconnecting session #%d with unknown connect packet 0x%04x(length:%d)%s\n", fd, cmd, RFIFOREST(fd), (
  8817. err == 1 ? "." :
  8818. err == 2 ? ", possibly for having an invalid account_id." :
  8819. err == 3 ? ", possibly for having an invalid char_id." :
  8820. /* Uncomment when checks are added in clif_parse_WantToConnection_sub. [FlavioJS]
  8821. err == 4 ? ", possibly for having an invalid login_id1." :
  8822. err == 5 ? ", possibly for having an invalid client_tick." :
  8823. */
  8824. err == 6 ? ", possibly for having an invalid sex." :
  8825. ". ERROR invalid error code"));
  8826. WFIFOHEAD(fd,packet_len(0x6a));
  8827. WFIFOW(fd,0) = 0x6a;
  8828. WFIFOB(fd,2) = err;
  8829. WFIFOSET(fd,packet_len(0x6a));
  8830. #ifdef DUMP_INVALID_PACKET
  8831. ShowDump(RFIFOP(fd, 0), RFIFOREST(fd));
  8832. #endif
  8833. RFIFOSKIP(fd, RFIFOREST(fd));
  8834. set_eof(fd);
  8835. return;
  8836. }
  8837. if( !global_core->is_running() ){ // not allowed
  8838. clif_authfail_fd(fd,1);// server closed
  8839. return;
  8840. }
  8841. //Check for double login.
  8842. bl = map_id2bl(account_id);
  8843. if(bl && bl->type != BL_PC) {
  8844. ShowError("clif_parse_WantToConnection: a non-player object already has id %d, please increase the starting account number\n", account_id);
  8845. WFIFOHEAD(fd,packet_len(0x6a));
  8846. WFIFOW(fd,0) = 0x6a;
  8847. WFIFOB(fd,2) = 3; // Rejected by server
  8848. WFIFOSET(fd,packet_len(0x6a));
  8849. set_eof(fd);
  8850. return;
  8851. }
  8852. if (bl ||
  8853. ((node=chrif_search(account_id)) && //An already existing node is valid only if it is for this login.
  8854. !(node->account_id == account_id && node->char_id == char_id && node->state == ST_LOGIN)))
  8855. {
  8856. clif_authfail_fd(fd, 8); //Still recognizes last connection
  8857. return;
  8858. }
  8859. CREATE(sd, TBL_PC, 1);
  8860. new(sd) map_session_data();
  8861. sd->fd = fd;
  8862. #ifdef PACKET_OBFUSCATION
  8863. sd->cryptKey = (((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF) * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF;
  8864. #endif
  8865. session[fd]->session_data = sd;
  8866. pc_setnewpc(sd, account_id, char_id, login_id1, client_tick, sex, fd);
  8867. #if PACKETVER < 20070521
  8868. WFIFOHEAD(fd,4);
  8869. WFIFOL(fd,0) = sd->bl.id;
  8870. WFIFOSET(fd,4);
  8871. #else
  8872. WFIFOHEAD(fd,packet_len(0x283));
  8873. WFIFOW(fd,0) = 0x283;
  8874. WFIFOL(fd,2) = sd->bl.id;
  8875. WFIFOSET(fd,packet_len(0x283));
  8876. #endif
  8877. chrif_authreq(sd,false);
  8878. }
  8879. /// Notification from the client, that it has finished map loading and is about to display player's character (CZ_NOTIFY_ACTORINIT).
  8880. /// 007d
  8881. void clif_parse_LoadEndAck(int32 fd,map_session_data *sd)
  8882. {
  8883. bool guild_notice = false;
  8884. if(sd->bl.prev != nullptr)
  8885. return;
  8886. // Autotraders should ignore this entirely, clif_parse_LoadEndAck is always invoked manually for them
  8887. if (!sd->state.active || (!sd->state.autotrade && !sd->state.pc_loaded)) { //Character loading is not complete yet!
  8888. //Let pc_reg_received or pc_scdata_received reinvoke this when ready.
  8889. sd->state.connect_new = 0;
  8890. return;
  8891. }
  8892. if (sd->state.rewarp) { //Rewarp player.
  8893. sd->state.rewarp = 0;
  8894. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  8895. return;
  8896. }
  8897. sd->state.warping = 0;
  8898. // look
  8899. #if PACKETVER < 4
  8900. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8901. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8902. #else
  8903. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8904. #endif
  8905. pc_set_costume_view(sd);
  8906. if(sd->vd.cloth_color)
  8907. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_CLOTHES_COLOR,sd->vd.cloth_color,SELF);
  8908. if(sd->vd.body_style)
  8909. clif_refreshlook(&sd->bl,sd->bl.id,LOOK_BODY2,sd->vd.body_style,SELF);
  8910. // item
  8911. clif_inventorylist(sd); // inventory list first, otherwise deleted items in pc_checkitem show up as 'unknown item'
  8912. pc_checkitem(sd);
  8913. clif_equipswitch_list(sd);
  8914. // cart
  8915. if(pc_iscarton(sd)) {
  8916. clif_cartlist(sd);
  8917. clif_updatestatus(*sd,SP_CARTINFO);
  8918. }
  8919. // weight
  8920. clif_updatestatus(*sd,SP_WEIGHT);
  8921. clif_updatestatus(*sd,SP_MAXWEIGHT);
  8922. // guild
  8923. // (needs to go before clif_spawn() to show guild emblems correctly)
  8924. if(sd->status.guild_id)
  8925. guild_send_memberinfoshort(sd,1);
  8926. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8927. if(battle_config.pc_invincible_time > 0) {
  8928. if(mapdata_flag_gvg(mapdata))
  8929. pc_setinvincibletimer(sd,battle_config.pc_invincible_time<<1);
  8930. else
  8931. pc_setinvincibletimer(sd,battle_config.pc_invincible_time);
  8932. }
  8933. if( mapdata->users++ == 0 && battle_config.dynamic_mobs )
  8934. map_spawnmobs(sd->bl.m);
  8935. if( !pc_isinvisible(sd) ) { // increment the number of pvp players on the map
  8936. mapdata->users_pvp++;
  8937. }
  8938. sd->state.debug_remove_map = 0; // temporary state to track double remove_map's [FlavioJS]
  8939. // reset the callshop flag if the player changes map
  8940. sd->state.callshop = 0;
  8941. if(map_addblock(&sd->bl))
  8942. return;
  8943. clif_spawn(&sd->bl);
  8944. // Party
  8945. // (needs to go after clif_spawn() to show hp bars correctly)
  8946. if(sd->status.party_id) {
  8947. party_send_movemap(sd);
  8948. clif_party_hp( *sd ); // Show hp after displacement [LuzZza]
  8949. }
  8950. if( sd->bg_id ) clif_bg_hp(sd); // BattleGround System
  8951. if(!pc_isinvisible(sd) && mapdata->getMapFlag(MF_PVP)) {
  8952. if(!battle_config.pk_mode) { // remove pvp stuff for pk_mode [Valaris]
  8953. if (!mapdata->getMapFlag(MF_PVP_NOCALCRANK))
  8954. sd->pvp_timer = add_timer(gettick()+200, pc_calc_pvprank_timer, sd->bl.id, 0);
  8955. sd->pvp_rank = 0;
  8956. sd->pvp_lastusers = 0;
  8957. sd->pvp_point = 5;
  8958. sd->pvp_won = 0;
  8959. sd->pvp_lost = 0;
  8960. }
  8961. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8962. } else if(sd->duel_group) // set flag, if it's a duel [LuzZza]
  8963. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF);
  8964. else if (mapdata->getMapFlag(MF_GVG_DUNGEON))
  8965. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE, SELF); //TODO: Figure out the real packet to send here.
  8966. else if( mapdata_flag_gvg(mapdata) )
  8967. clif_map_property(&sd->bl, MAPPROPERTY_AGITZONE, SELF);
  8968. else
  8969. clif_map_property(&sd->bl, MAPPROPERTY_NOTHING, SELF);
  8970. // info about nearby objects
  8971. // must use foreachinarea (CIRCULAR_AREA interferes with foreachinrange)
  8972. 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);
  8973. // pet
  8974. if( sd->pd ) {
  8975. if( battle_config.pet_no_gvg && mapdata_flag_gvg(mapdata) ) { //Return the pet to egg. [Skotlex]
  8976. clif_displaymessage(sd->fd, msg_txt(sd,666));
  8977. pet_return_egg( sd, sd->pd );
  8978. } else {
  8979. if(map_addblock(&sd->pd->bl))
  8980. return;
  8981. clif_spawn(&sd->pd->bl);
  8982. clif_send_petdata(sd,sd->pd,0,0);
  8983. clif_send_petstatus(sd);
  8984. // skill_unit_move(&sd->pd->bl,gettick(),1);
  8985. }
  8986. }
  8987. //homunculus [blackhole89]
  8988. if( hom_is_active(sd->hd) ) {
  8989. if(map_addblock(&sd->hd->bl))
  8990. return;
  8991. clif_spawn(&sd->hd->bl);
  8992. clif_send_homdata( *sd->hd, SP_ACK );
  8993. // For some reason, official servers send the homunculus info twice, then update the HP/SP again.
  8994. clif_hominfo(sd, sd->hd, 1);
  8995. clif_hominfo(sd, sd->hd, 0);
  8996. clif_homunculus_updatestatus(*sd, SP_HP);
  8997. clif_homunculus_updatestatus(*sd, SP_SP);
  8998. clif_homskillinfoblock( *sd->hd );
  8999. status_calc_bl(&sd->hd->bl, { SCB_SPEED });
  9000. if( !(battle_config.hom_setting&HOMSET_NO_INSTANT_LAND_SKILL) )
  9001. skill_unit_move(&sd->hd->bl,gettick(),1); // apply land skills immediately
  9002. }
  9003. if( sd->md ) {
  9004. if(map_addblock(&sd->md->bl))
  9005. return;
  9006. clif_spawn(&sd->md->bl);
  9007. clif_mercenary_info(sd);
  9008. clif_mercenary_skillblock(sd);
  9009. status_calc_bl(&sd->md->bl, { SCB_SPEED }); // Mercenary mimic their master's speed on each map change
  9010. }
  9011. if( sd->ed ) {
  9012. if (map_addblock(&sd->ed->bl))
  9013. return;
  9014. clif_spawn(&sd->ed->bl);
  9015. clif_elemental_info(sd);
  9016. clif_elemental_updatestatus(*sd, SP_HP);
  9017. clif_hpmeter_single( *sd, sd->ed->bl.id, sd->ed->battle_status.hp, sd->ed->battle_status.max_hp );
  9018. clif_elemental_updatestatus(*sd, SP_SP);
  9019. status_calc_bl(&sd->ed->bl, { SCB_SPEED }); //Elemental mimic their master's speed on each map change
  9020. }
  9021. if(sd->state.connect_new) {
  9022. int32 lv;
  9023. guild_notice = true;
  9024. clif_skillinfoblock(sd);
  9025. clif_hotkeys_send(sd,0);
  9026. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9027. clif_hotkeys_send(sd,1);
  9028. #endif
  9029. clif_updatestatus(*sd,SP_BASEEXP);
  9030. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  9031. clif_updatestatus(*sd,SP_JOBEXP);
  9032. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  9033. clif_updatestatus(*sd,SP_SKILLPOINT);
  9034. clif_initialstatus( *sd );
  9035. if (sd->sc.option&OPTION_FALCON)
  9036. clif_status_load(&sd->bl, EFST_FALCON, 1);
  9037. else if (sd->sc.option&(OPTION_RIDING|OPTION_DRAGON))
  9038. clif_status_load(&sd->bl, EFST_RIDING, 1);
  9039. else if (sd->sc.option&OPTION_WUGRIDER)
  9040. clif_status_load(&sd->bl, EFST_WUGRIDER, 1);
  9041. else if (sd->sc.getSCE(SC_ALL_RIDING))
  9042. clif_status_load(&sd->bl, EFST_ALL_RIDING, 1);
  9043. if(sd->status.manner < 0)
  9044. sc_start(&sd->bl,&sd->bl,SC_NOCHAT,100,0,0);
  9045. //Auron reported that This skill only triggers when you logon on the map o.O [Skotlex]
  9046. if ((lv = pc_checkskill(sd,SG_KNOWLEDGE)) > 0) {
  9047. if(sd->bl.m == sd->feel_map[0].m
  9048. || sd->bl.m == sd->feel_map[1].m
  9049. || sd->bl.m == sd->feel_map[2].m)
  9050. sc_start(&sd->bl,&sd->bl, SC_KNOWLEDGE, 100, lv, skill_get_time(SG_KNOWLEDGE, lv));
  9051. }
  9052. if(sd->pd && sd->pd->pet.intimate > 900)
  9053. clif_pet_emotion(sd->pd,(sd->pd->pet.class_ - 100)*100 + 50 + pet_hungry_val(sd->pd));
  9054. if(hom_is_active(sd->hd))
  9055. hom_init_timers(sd->hd);
  9056. if (night_flag && mapdata->getMapFlag(MF_NIGHTENABLED)) {
  9057. sd->state.night = 1;
  9058. clif_status_load(&sd->bl, EFST_SKE, 1);
  9059. }
  9060. // Notify everyone that this char logged in [Skotlex].
  9061. map_foreachpc(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 1);
  9062. if (!sd->state.autotrade) { // Don't trigger NPC event or opening vending/buyingstore will be failed
  9063. npc_script_event( *sd, NPCE_LOGIN );
  9064. }
  9065. // Set facing direction before check below to update client
  9066. if (battle_config.spawn_direction)
  9067. unit_setdir(&sd->bl, sd->status.body_direction, false);
  9068. } else {
  9069. //For some reason the client "loses" these on warp/map-change.
  9070. clif_updatestatus(*sd,SP_STR);
  9071. clif_updatestatus(*sd,SP_AGI);
  9072. clif_updatestatus(*sd,SP_VIT);
  9073. clif_updatestatus(*sd,SP_INT);
  9074. clif_updatestatus(*sd,SP_DEX);
  9075. clif_updatestatus(*sd,SP_LUK);
  9076. #ifdef RENEWAL
  9077. clif_updatestatus(*sd,SP_POW);
  9078. clif_updatestatus(*sd,SP_STA);
  9079. clif_updatestatus(*sd,SP_WIS);
  9080. clif_updatestatus(*sd,SP_SPL);
  9081. clif_updatestatus(*sd,SP_CON);
  9082. clif_updatestatus(*sd,SP_CRT);
  9083. #endif
  9084. // abort currently running script
  9085. sd->state.using_fake_npc = 0;
  9086. sd->state.menu_or_input = 0;
  9087. sd->npc_menu = 0;
  9088. if(sd->npc_id)
  9089. npc_event_dequeue(sd);
  9090. }
  9091. if( sd->state.changemap ) {// restore information that gets lost on map-change
  9092. clif_partyinvitationstate( *sd );
  9093. #if PACKETVER >= 20070918
  9094. clif_equipcheckbox(sd);
  9095. #endif
  9096. clif_pet_autofeed_status(sd,false);
  9097. clif_configuration( sd, CONFIG_CALL, sd->status.disable_call );
  9098. #if PACKETVER >= 20170920
  9099. if( battle_config.homunculus_autofeed_always ){
  9100. // Always send ON or OFF
  9101. if( sd->hd && battle_config.feature_homunculus_autofeed ){
  9102. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, sd->hd->homunculus.autofeed );
  9103. }else{
  9104. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, false );
  9105. }
  9106. }else{
  9107. // Only send when enabled
  9108. if( sd->hd && battle_config.feature_homunculus_autofeed && sd->hd->homunculus.autofeed ){
  9109. clif_configuration( sd, CONFIG_HOMUNCULUS_AUTOFEED, true );
  9110. }
  9111. }
  9112. #endif
  9113. clif_reputation_list( *sd );
  9114. if (sd->guild && battle_config.guild_notice_changemap == 1){
  9115. // Displays after VIP
  9116. clif_guild_notice( *sd );
  9117. guild_notice = false; // Do not display it twice
  9118. }
  9119. if (battle_config.bg_flee_penalty != 100 || battle_config.gvg_flee_penalty != 100) {
  9120. struct map_data *pmap = map_getmapdata(sd->state.pmap);
  9121. if ((pmap != nullptr && (mapdata_flag_gvg(pmap) || pmap->getMapFlag(MF_BATTLEGROUND))) || (mapdata != nullptr && (mapdata_flag_gvg(mapdata) || mapdata->getMapFlag(MF_BATTLEGROUND))))
  9122. status_calc_bl(&sd->bl, { SCB_FLEE }); //Refresh flee penalty
  9123. }
  9124. if( night_flag && mapdata->getMapFlag(MF_NIGHTENABLED) )
  9125. { //Display night.
  9126. if( !sd->state.night )
  9127. {
  9128. sd->state.night = 1;
  9129. clif_status_load(&sd->bl, EFST_SKE, 1);
  9130. }
  9131. }
  9132. else if( sd->state.night )
  9133. { //Clear night display.
  9134. sd->state.night = 0;
  9135. clif_status_load(&sd->bl, EFST_SKE, 0);
  9136. }
  9137. if( mapdata->getMapFlag(MF_BATTLEGROUND) )
  9138. {
  9139. clif_map_type( *sd, MAPTYPE_BATTLEFIELD ); // Battleground Mode
  9140. if( map_getmapflag(sd->bl.m, MF_BATTLEGROUND) == 2 )
  9141. clif_bg_updatescore_single(sd);
  9142. }
  9143. if( mapdata->getMapFlag(MF_ALLOWKS) && !mapdata_flag_ks(mapdata) )
  9144. {
  9145. char output[128];
  9146. sprintf(output, "[ Kill Steal Protection Disable. KS is allowed in this map ]");
  9147. clif_broadcast(&sd->bl, output, strlen(output) + 1, BC_BLUE, SELF);
  9148. }
  9149. if( pc_has_permission(sd,PC_PERM_VIEW_HPMETER) ) {
  9150. mapdata->hpmeter_visible++;
  9151. sd->state.hpmeter_visible = 1;
  9152. }
  9153. status_change_clear_onChangeMap(&sd->bl, &sd->sc);
  9154. map_iwall_get(sd); // Updates Walls Info on this Map to Client
  9155. status_calc_pc(sd, sd->state.autotrade ? SCO_FIRST : SCO_NONE); // Some conditions are map-dependent so we must recalculate
  9156. #ifdef VIP_ENABLE
  9157. if (!sd->state.connect_new &&
  9158. !sd->vip.disableshowrate &&
  9159. sd->state.pmap != sd->bl.m &&
  9160. map_getmapflag(sd->state.pmap, MF_BEXP) != mapdata->getMapFlag(MF_BEXP)
  9161. )
  9162. {
  9163. clif_display_pinfo( *sd );
  9164. }
  9165. #endif
  9166. // Instances do not need their own channels
  9167. if( channel_config.map_tmpl.name[0] && (channel_config.map_tmpl.opt&CHAN_OPT_AUTOJOIN) && !mapdata->instance_id && !mapdata->getMapFlag(MF_NOMAPCHANNELAUTOJOIN) )
  9168. channel_mjoin(sd); //join new map
  9169. clif_pk_mode_message(sd);
  9170. }
  9171. if( sd->guild && ( battle_config.guild_notice_changemap == 2 || guild_notice ) ){
  9172. // Displays at end
  9173. clif_guild_notice( *sd );
  9174. }
  9175. mail_clear(sd);
  9176. /* Guild Aura Init */
  9177. if( sd->guild && sd->state.gmaster_flag ) {
  9178. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  9179. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  9180. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  9181. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  9182. }
  9183. if( sd->state.vending ) { /* show we have a vending */
  9184. clif_openvending( *sd );
  9185. clif_showvendingboard( *sd );
  9186. }
  9187. // Don't trigger NPC event or opening vending/buyingstore will be failed
  9188. if(!sd->state.autotrade && mapdata->getMapFlag(MF_LOADEVENT)) // Lance
  9189. npc_script_event( *sd, NPCE_LOADMAP );
  9190. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  9191. clif_status_load(&sd->bl, EFST_DEVIL1, 1); //blindness [Komurka]
  9192. if (sd->sc.opt2) //Client loses these on warp.
  9193. clif_changeoption(&sd->bl);
  9194. 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)) {
  9195. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM);
  9196. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM);
  9197. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  9198. }
  9199. clif_weather_check(sd);
  9200. // For automatic triggering of NPCs after map loading (so you don't need to walk 1 step first)
  9201. if (map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNPC))
  9202. npc_touch_area_allnpc(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  9203. else
  9204. sd->areanpc.clear();
  9205. /* it broke at some point (e.g. during a crash), so we make it visibly dead again. */
  9206. if( !sd->status.hp && !pc_isdead(sd) && status_isdead(sd->bl) )
  9207. pc_setdead(sd);
  9208. // If player is dead, and is spawned (such as @refresh) send death packet. [Valaris]
  9209. if(pc_isdead(sd))
  9210. clif_clearunit_area( sd->bl, CLR_DEAD );
  9211. else {
  9212. skill_usave_trigger(sd);
  9213. if (battle_config.spawn_direction)
  9214. clif_changed_dir(sd->bl, SELF);
  9215. }
  9216. // Trigger skill effects if you appear standing on them
  9217. if(!battle_config.pc_invincible_time)
  9218. skill_unit_move(&sd->bl,gettick(),1);
  9219. pc_show_questinfo_reinit(sd);
  9220. pc_show_questinfo(sd);
  9221. #if PACKETVER >= 20150513
  9222. if( sd->mail.inbox.unread ){
  9223. clif_Mail_new(sd, 0, nullptr, nullptr);
  9224. }
  9225. #endif
  9226. sd->state.connect_new = 0;
  9227. sd->state.changemap = false;
  9228. }
  9229. /// Server's tick (ZC_NOTIFY_TIME).
  9230. /// 007f <time>.L
  9231. void clif_notify_time(map_session_data* sd, t_tick time)
  9232. {
  9233. int32 fd = sd->fd;
  9234. WFIFOHEAD(fd,packet_len(0x7f));
  9235. WFIFOW(fd,0) = 0x7f;
  9236. WFIFOL(fd,2) = client_tick(time);
  9237. WFIFOSET(fd,packet_len(0x7f));
  9238. }
  9239. /// Request for server's tick.
  9240. /// 007e <client tick>.L (CZ_REQUEST_TIME)
  9241. /// 0360 <client tick>.L (CZ_REQUEST_TIME2)
  9242. /// There are various variants of this packet, some of them have padding between fields.
  9243. void clif_parse_TickSend(int32 fd, map_session_data *sd)
  9244. {
  9245. sd->client_tick = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9246. clif_notify_time(sd, gettick());
  9247. }
  9248. /// Sends hotkey bar.
  9249. /// 02b9 { <is skill>.B <id>.L <count>.W }*27 (ZC_SHORTCUT_KEY_LIST)
  9250. /// 07d9 { <is skill>.B <id>.L <count>.W }*36 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090603)
  9251. /// 07d9 { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V2, PACKETVER >= 20090617)
  9252. /// 0a00 <rotate>.B { <is skill>.B <id>.L <count>.W }*38 (ZC_SHORTCUT_KEY_LIST_V3, PACKETVER >= 20141022)
  9253. void clif_hotkeys_send( map_session_data *sd, int32 tab ){
  9254. #if defined(HOTKEY_SAVING) && ( PACKETVER_MAIN_NUM >= 20070711 || PACKETVER_RE_NUM >= 20080827 || PACKETVER_AD_NUM >= 20070711 || PACKETVER_SAK_NUM >= 20070628 )
  9255. nullpo_retv( sd );
  9256. PACKET_ZC_SHORTCUT_KEY_LIST p = {};
  9257. p.packetType = HEADER_ZC_SHORTCUT_KEY_LIST;
  9258. #if PACKETVER_MAIN_NUM >= 20141022 || PACKETVER_RE_NUM >= 20141015 || defined(PACKETVER_ZERO)
  9259. if( tab == 0 ){
  9260. p.rotate = sd->status.hotkey_rowshift;
  9261. }else{
  9262. p.rotate = sd->status.hotkey_rowshift2;
  9263. }
  9264. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9265. p.tab = tab;
  9266. #endif
  9267. #endif
  9268. for( int32 i = 0, offset = tab * MAX_HOTKEYS; i < MAX_HOTKEYS_PACKET; i++ ){
  9269. p.hotkey[i].isSkill = sd->status.hotkeys[i + offset].type;
  9270. p.hotkey[i].id = sd->status.hotkeys[i + offset].id;
  9271. p.hotkey[i].count = sd->status.hotkeys[i + offset].lv;
  9272. }
  9273. clif_send( &p, sizeof(PACKET_ZC_SHORTCUT_KEY_LIST), &sd->bl, SELF );
  9274. #endif
  9275. }
  9276. /// Request to update a position on the hotkey row bar
  9277. void clif_parse_HotkeyRowShift( int32 fd, map_session_data *sd ){
  9278. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9279. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE2* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE2*>( RFIFOP( fd, 0 ) );
  9280. if( p->tab == 0 ){
  9281. sd->status.hotkey_rowshift = p->rowshift;
  9282. }else{
  9283. sd->status.hotkey_rowshift2 = p->rowshift;
  9284. }
  9285. #elif PACKETVER_MAIN_NUM >= 20140129 || PACKETVER_RE_NUM >= 20140129 || defined(PACKETVER_ZERO)
  9286. const PACKET_CZ_SHORTCUTKEYBAR_ROTATE1* p = reinterpret_cast<PACKET_CZ_SHORTCUTKEYBAR_ROTATE1*>( RFIFOP( fd, 0 ) );
  9287. sd->status.hotkey_rowshift = p->rowshift;
  9288. #endif
  9289. }
  9290. /// Request to update a position on the hotkey bar.
  9291. /// 02ba <index>.W <is skill>.B <id>.L <count>.W (CZ_SHORTCUT_KEY_CHANGE)
  9292. void clif_parse_Hotkey(int32 fd, map_session_data *sd) {
  9293. #ifdef HOTKEY_SAVING
  9294. #if PACKETVER_MAIN_NUM >= 20190522 || PACKETVER_RE_NUM >= 20190508 || PACKETVER_ZERO_NUM >= 20190605
  9295. const PACKET_CZ_SHORTCUT_KEY_CHANGE2* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE2*>( RFIFOP( fd, 0 ) );
  9296. const unsigned short idx = p->index + p->tab * MAX_HOTKEYS;
  9297. if( idx >= MAX_HOTKEYS_DB ){
  9298. return;
  9299. }
  9300. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9301. sd->status.hotkeys[idx].id = p->hotkey.id;
  9302. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9303. #elif PACKETVER_MAIN_NUM >= 20070618 || defined(PACKETVER_RE) || defined(PACKETVER_ZERO) || PACKETVER_AD_NUM >= 20070618 || PACKETVER_SAK_NUM >= 20070618
  9304. const PACKET_CZ_SHORTCUT_KEY_CHANGE1* p = reinterpret_cast<PACKET_CZ_SHORTCUT_KEY_CHANGE1*>( RFIFOP( fd, 0 ) );
  9305. const unsigned short idx = p->index;
  9306. if( idx >= MAX_HOTKEYS_DB ){
  9307. return;
  9308. }
  9309. sd->status.hotkeys[idx].type = p->hotkey.isSkill;
  9310. sd->status.hotkeys[idx].id = p->hotkey.id;
  9311. sd->status.hotkeys[idx].lv = p->hotkey.count;
  9312. #endif
  9313. #endif
  9314. }
  9315. /// Displays cast-like progress bar (ZC_PROGRESS).
  9316. /// 02f0 <color>.L <time>.L
  9317. void clif_progressbar(map_session_data * sd, unsigned long color, uint32 second)
  9318. {
  9319. int32 fd = sd->fd;
  9320. WFIFOHEAD(fd,packet_len(0x2f0));
  9321. WFIFOW(fd,0) = 0x2f0;
  9322. WFIFOL(fd,2) = color;
  9323. WFIFOL(fd,6) = second;
  9324. WFIFOSET(fd,packet_len(0x2f0));
  9325. }
  9326. /// Removes an ongoing progress bar (ZC_PROGRESS_CANCEL).
  9327. /// 02f2
  9328. void clif_progressbar_abort(map_session_data * sd)
  9329. {
  9330. int32 fd = sd->fd;
  9331. WFIFOHEAD(fd,packet_len(0x2f2));
  9332. WFIFOW(fd,0) = 0x2f2;
  9333. WFIFOSET(fd,packet_len(0x2f2));
  9334. }
  9335. /// Notification from the client, that the progress bar has reached 100%.
  9336. /// 02f1 (CZ_PROGRESS)
  9337. void clif_parse_progressbar(int32 fd, map_session_data * sd){
  9338. // No progressbar active, ignore it
  9339. if( !sd->progressbar.npc_id ){
  9340. return;
  9341. }
  9342. int32 npc_id = sd->progressbar.npc_id;
  9343. bool closing = false;
  9344. // Check if the progress was canceled
  9345. if( gettick() < sd->progressbar.timeout && sd->st ){
  9346. closing = true;
  9347. sd->st->state = CLOSE; // will result in END in npc_scriptcont
  9348. // If a message window was open, offer a close button to the user
  9349. if( sd->st->mes_active ){
  9350. clif_scriptclose( *sd, npc_id );
  9351. }
  9352. }
  9353. sd->progressbar.npc_id = 0;
  9354. sd->progressbar.timeout = 0;
  9355. sd->state.workinprogress = WIP_DISABLE_NONE;
  9356. if( battle_config.idletime_option&IDLE_NPC_PROGRESS ){
  9357. sd->idletime = last_tick;
  9358. }
  9359. npc_scriptcont(sd, npc_id, closing);
  9360. }
  9361. /// Displays cast-like progress bar on a NPC
  9362. /// 09d1 <id>.L <color>.L <time>.L (ZC_PROGRESS_ACTOR)
  9363. void clif_progressbar_npc( struct npc_data *nd, map_session_data* sd ){
  9364. #if PACKETVER >= 20130821
  9365. unsigned char buf[14];
  9366. if( nd->progressbar.timeout > 0 ){
  9367. WBUFW(buf,0) = 0x9d1;
  9368. WBUFL(buf,2) = nd->bl.id;
  9369. WBUFL(buf,6) = nd->progressbar.color;
  9370. WBUFL(buf,10) = client_tick( ( nd->progressbar.timeout - gettick() ) / 1000 );
  9371. if( sd ){
  9372. clif_send(buf, packet_len(0x9d1), &sd->bl, SELF);
  9373. }else{
  9374. clif_send(buf, packet_len(0x9d1), &nd->bl, AREA);
  9375. }
  9376. }
  9377. #endif
  9378. }
  9379. /// Request to walk to a certain position on the current map.
  9380. /// 0085 <dest>.3B (CZ_REQUEST_MOVE)
  9381. /// 035f <dest>.3B (CZ_REQUEST_MOVE2)
  9382. /// There are various variants of this packet, some of them have padding between fields.
  9383. void clif_parse_WalkToXY(int32 fd, map_session_data *sd)
  9384. {
  9385. short x, y;
  9386. if (pc_isdead(sd)) {
  9387. clif_clearunit_area( sd->bl, CLR_DEAD );
  9388. return;
  9389. }
  9390. if (sd->sc.opt1 && ( sd->sc.opt1 == OPT1_STONEWAIT || sd->sc.opt1 == OPT1_BURNING ))
  9391. ; //You CAN walk on this OPT1 value.
  9392. else if (sd->progressbar.npc_id) {
  9393. clif_progressbar_abort(sd);
  9394. return; // First walk attempt cancels the progress bar
  9395. } else if (pc_cant_act(sd))
  9396. return;
  9397. if(sd->sc.getSCE(SC_RUN) || sd->sc.getSCE(SC_WUGDASH))
  9398. return;
  9399. RFIFOPOS(fd, packet_db[RFIFOW(fd,0)].pos[0], &x, &y, nullptr);
  9400. //A move command one cell west is only valid if the target cell is free
  9401. if(battle_config.official_cell_stack_limit > 0
  9402. && sd->bl.x == x+1 && sd->bl.y == y && map_count_oncell(sd->bl.m, x, y, BL_CHAR|BL_NPC, 1))
  9403. return;
  9404. // Cloaking wall check is actually updated when you click to process next movement
  9405. // not when you move each cell. This is official behaviour.
  9406. if (sd->sc.getSCE(SC_CLOAKING))
  9407. skill_check_cloaking(&sd->bl, sd->sc.getSCE(SC_CLOAKING));
  9408. status_change_end(&sd->bl, SC_ROLLINGCUTTER); // If you move, you lose your counters. [malufett]
  9409. status_change_end(&sd->bl, SC_CRESCIVEBOLT);
  9410. pc_delinvincibletimer(sd);
  9411. //Set last idle time... [Skotlex]
  9412. if (battle_config.idletime_option&IDLE_WALK)
  9413. sd->idletime = last_tick;
  9414. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_WALK)
  9415. sd->idletime_hom = last_tick;
  9416. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_WALK)
  9417. sd->idletime_mer = last_tick;
  9418. unit_walktoxy(&sd->bl, x, y, 4);
  9419. }
  9420. /// Notification about the result of a disconnect request (ZC_ACK_REQ_DISCONNECT).
  9421. /// 018b <result>.W
  9422. /// result:
  9423. /// 0 = disconnect (quit)
  9424. /// 1 = cannot disconnect (wait 10 seconds)
  9425. /// ? = ignored
  9426. void clif_disconnect_ack(map_session_data* sd, short result)
  9427. {
  9428. int32 fd = sd->fd;
  9429. WFIFOHEAD(fd,packet_len(0x18b));
  9430. WFIFOW(fd,0) = 0x18b;
  9431. WFIFOW(fd,2) = result;
  9432. WFIFOSET(fd,packet_len(0x18b));
  9433. }
  9434. /// Request to disconnect from server (CZ_REQ_DISCONNECT).
  9435. /// 018a <type>.W
  9436. /// type:
  9437. /// 0 = quit
  9438. void clif_parse_QuitGame(int32 fd, map_session_data *sd)
  9439. {
  9440. /* Rovert's prevent logout option fixed [Valaris] */
  9441. 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) &&
  9442. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9443. {
  9444. pc_damage_log_clear(sd,0);
  9445. clif_disconnect_ack(sd, 0);
  9446. flush_fifo( fd );
  9447. if( battle_config.drop_connection_on_quit ){
  9448. set_eof( fd );
  9449. }
  9450. } else {
  9451. clif_disconnect_ack(sd, 1);
  9452. }
  9453. }
  9454. /// Requesting unit's name.
  9455. /// 0094 <id>.L (CZ_REQNAME)
  9456. /// 0368 <id>.L (CZ_REQNAME2)
  9457. /// There are various variants of this packet, some of them have padding between fields.
  9458. void clif_parse_GetCharNameRequest(int32 fd, map_session_data *sd)
  9459. {
  9460. int32 id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9461. struct block_list* bl;
  9462. //status_change *sc;
  9463. if( id < 0 && -id == sd->bl.id ) // for disguises [Valaris]
  9464. id = sd->bl.id;
  9465. bl = map_id2bl(id);
  9466. if( bl == nullptr )
  9467. return; // Lagged clients could request names of already gone mobs/players. [Skotlex]
  9468. if( sd->bl.m != bl->m || !check_distance_bl(&sd->bl, bl, AREA_SIZE) )
  9469. return; // Block namerequests past view range
  9470. // 'see people in GM hide' cheat detection
  9471. /* disabled due to false positives (network lag + request name of char that's about to hide = race condition)
  9472. sc = status_get_sc(bl);
  9473. if (sc && sc->option&OPTION_INVISIBLE && !disguised(bl) &&
  9474. bl->type != BL_NPC && //Skip hidden NPCs which can be seen using Maya Purple
  9475. pc_get_group_level(sd) < battle_config.hack_info_GM_level
  9476. ) {
  9477. char gm_msg[256];
  9478. 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);
  9479. ShowWarning(gm_msg);
  9480. // information is sent to all online GMs
  9481. intif_wis_message_to_gm(wisp_server_name, battle_config.hack_info_GM_level, gm_msg);
  9482. return;
  9483. }
  9484. */
  9485. clif_name(&sd->bl,bl,SELF);
  9486. }
  9487. /// Validates and processes global messages
  9488. /// 008c <packet len>.W <text>.?B (<name> : <message>) 00 (CZ_REQUEST_CHAT)
  9489. /// There are various variants of this packet.
  9490. void clif_parse_GlobalMessage(int32 fd, map_session_data* sd)
  9491. {
  9492. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9493. size_t length;
  9494. // validate packet and retrieve name and message
  9495. if( !clif_process_message(sd, false, name, message, output ) )
  9496. return;
  9497. if( sd->gcbind && ((sd->gcbind->opt&CHAN_OPT_CAN_CHAT) || pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN)) ) {
  9498. channel_send(sd->gcbind,sd,message);
  9499. return;
  9500. }
  9501. // send message to others (using the send buffer for temp. storage)
  9502. clif_GlobalMessage( sd->bl, output, sd->chatID ? CHAT_WOS : AREA_CHAT_WOC );
  9503. length = strlen(output) + 1;
  9504. // send back message to the speaker
  9505. WFIFOHEAD(fd,4+length);
  9506. WFIFOW(fd,0) = 0x8e;
  9507. WFIFOW(fd,2) = (uint16)(4+length);
  9508. safestrncpy(WFIFOCP(fd,4), output, length );
  9509. WFIFOSET(fd, WFIFOW(fd,2));
  9510. #ifdef PCRE_SUPPORT
  9511. // trigger listening npcs
  9512. map_foreachinallrange(npc_chat_sub, &sd->bl, AREA_SIZE, BL_NPC, output, strlen(output), &sd->bl);
  9513. #endif
  9514. // Chat logging type 'O' / Global Chat
  9515. 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);
  9516. //achievement_update_objective(sd, AG_CHAT, 1, sd->bl.m); //! TODO: What's the official use of this achievement type?
  9517. }
  9518. /// /mm /mapmove (as @rura GM command).
  9519. /// Request to warp to a map on given coordinates.
  9520. /// 0140 <map name>.16B <x>.W <y>.W (CZ_MOVETO_MAP)
  9521. void clif_parse_MapMove( int32 fd, map_session_data* sd){
  9522. const PACKET_CZ_MOVETO_MAP* p = reinterpret_cast<PACKET_CZ_MOVETO_MAP*>( RFIFOP( fd, 0 ) );
  9523. char map_name[MAP_NAME_LENGTH_EXT];
  9524. safestrncpy( map_name, p->map, sizeof( map_name ) );
  9525. char command[CHAT_SIZE_MAX];
  9526. safesnprintf( command, sizeof( command ),"%cmapmove %s %hu %hu", atcommand_symbol, map_name, p->x, p->y );
  9527. is_atcommand(fd, sd, command, 1);
  9528. }
  9529. /// Updates body and head direction of an object (ZC_CHANGE_DIRECTION).
  9530. /// 009c <id>.L <head dir>.W <dir>.B
  9531. /// head dir:
  9532. /// 0 = straight
  9533. /// 1 = turned CW
  9534. /// 2 = turned CCW
  9535. /// dir:
  9536. /// 0 = north
  9537. /// 1 = northwest
  9538. /// 2 = west
  9539. /// 3 = southwest
  9540. /// 4 = south
  9541. /// 5 = southeast
  9542. /// 6 = east
  9543. /// 7 = northeast
  9544. void clif_changed_dir(block_list& bl, enum send_target target){
  9545. PACKET_ZC_CHANGE_DIRECTION p{};
  9546. p.packetType = HEADER_ZC_CHANGE_DIRECTION;
  9547. p.srcId = bl.id;
  9548. if( bl.type == BL_PC ){
  9549. p.headDir = reinterpret_cast<map_session_data*>(&bl)->head_dir;
  9550. }else{
  9551. p.headDir = 0;
  9552. }
  9553. p.dir = unit_getdir(&bl);
  9554. clif_send(&p, sizeof(p), &bl, target);
  9555. if (disguised(&bl)) {
  9556. p.srcId = disguised_bl_id(bl.id);
  9557. p.headDir = 0;
  9558. clif_send(&p, sizeof(p), &bl, SELF);
  9559. }
  9560. }
  9561. /// Request to change own body and head direction.
  9562. /// 009b <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION)
  9563. /// 0361 <head dir>.W <dir>.B (CZ_CHANGE_DIRECTION2)
  9564. /// There are various variants of this packet, some of them have padding between fields.
  9565. void clif_parse_ChangeDir(int32 fd, map_session_data *sd)
  9566. {
  9567. unsigned char headdir, dir;
  9568. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9569. headdir = RFIFOB(fd,info->pos[0]);
  9570. dir = RFIFOB(fd,info->pos[1]);
  9571. pc_setdir(sd, dir, headdir);
  9572. clif_changed_dir(sd->bl, AREA_WOS);
  9573. }
  9574. /// Request to show an emotion (CZ_REQ_EMOTION).
  9575. /// 00bf <type>.B
  9576. /// type:
  9577. /// @see enum emotion_type
  9578. void clif_parse_Emotion(int32 fd, map_session_data *sd){
  9579. if( sd == nullptr ){
  9580. return;
  9581. }
  9582. int32 emoticon = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9583. if (battle_config.basic_skill_check == 0 || pc_checkskill(sd, NV_BASIC) >= 2 || pc_checkskill(sd, SU_BASIC_SKILL) >= 1) {
  9584. if (emoticon == ET_CHAT_PROHIBIT) {// prevent use of the mute emote [Valaris]
  9585. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9586. return;
  9587. }
  9588. // fix flood of emotion icon (ro-proxy): flood only the hacker player
  9589. if (sd->emotionlasttime + 1 >= time(nullptr)) { // not more than 1 per second
  9590. sd->emotionlasttime = time(nullptr);
  9591. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9592. return;
  9593. }
  9594. sd->emotionlasttime = time(nullptr);
  9595. if (battle_config.idletime_option&IDLE_EMOTION)
  9596. sd->idletime = last_tick;
  9597. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_EMOTION)
  9598. sd->idletime_hom = last_tick;
  9599. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_EMOTION)
  9600. sd->idletime_mer = last_tick;
  9601. if (sd->state.block_action & PCBLOCK_EMOTION) {
  9602. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9603. return;
  9604. }
  9605. if(battle_config.client_reshuffle_dice && emoticon>=ET_DICE1 && emoticon<=ET_DICE6) {// re-roll dice
  9606. emoticon = rnd()%6+ET_DICE1;
  9607. }
  9608. clif_emotion(&sd->bl, emoticon);
  9609. } else
  9610. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 1 );
  9611. }
  9612. /// Amount of currently online players, reply to /w /who
  9613. /// 00c2 <count>.L (ZC_USER_COUNT)
  9614. static void clif_user_count(map_session_data& sd)
  9615. {
  9616. PACKET_ZC_USER_COUNT p{};
  9617. p.packetType = HEADER_ZC_USER_COUNT;
  9618. p.playersCount = map_getusers();
  9619. clif_send(&p,sizeof(p),&sd.bl,SELF);
  9620. }
  9621. /// /w /who (CZ_REQ_USER_COUNT).
  9622. /// Request to display amount of currently connected players.
  9623. /// 00c1
  9624. void clif_parse_HowManyConnections(int32 fd, map_session_data *sd) {
  9625. if( sd == nullptr ){
  9626. return;
  9627. }
  9628. clif_user_count(*sd);
  9629. }
  9630. void clif_parse_ActionRequest_sub( map_session_data& sd, uint8 action_type, int32 target_id, t_tick tick ){
  9631. if (pc_isdead(&sd)) {
  9632. clif_clearunit_area(sd.bl, CLR_DEAD);
  9633. return;
  9634. }
  9635. // Statuses that don't let the player sit / stand / talk with NPCs (targeted)
  9636. if (action_type != DMG_NORMAL && action_type != DMG_REPEAT) {
  9637. if (sd.sc.cant.interact)
  9638. return;
  9639. pc_stop_walking(&sd, 1);
  9640. }
  9641. pc_stop_attack(&sd);
  9642. if(target_id<0 && -target_id == sd.bl.id) // for disguises [Valaris]
  9643. target_id = sd.bl.id;
  9644. switch(action_type)
  9645. {
  9646. case DMG_NORMAL: // once attack
  9647. case DMG_REPEAT: // continuous attack
  9648. if( pc_cant_act(&sd) )
  9649. return;
  9650. if (!battle_config.sdelay_attack_enable && pc_checkskill(&sd, SA_FREECAST) <= 0) {
  9651. if (DIFF_TICK(tick, sd.ud.canact_tick) < 0) {
  9652. clif_skill_fail( sd, 1, USESKILL_FAIL_SKILLINTERVAL );
  9653. return;
  9654. }
  9655. }
  9656. pc_delinvincibletimer(&sd);
  9657. if (battle_config.idletime_option&IDLE_ATTACK)
  9658. sd.idletime = last_tick;
  9659. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_ATTACK)
  9660. sd.idletime_hom = last_tick;
  9661. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_ATTACK)
  9662. sd.idletime_mer = last_tick;
  9663. unit_attack(&sd.bl, target_id, action_type != 0);
  9664. break;
  9665. case DMG_SIT_DOWN: // sitdown
  9666. if (battle_config.basic_skill_check && pc_checkskill(&sd, NV_BASIC) < 3 && pc_checkskill(&sd, SU_BASIC_SKILL) < 1) {
  9667. clif_skill_fail( sd, 1, USESKILL_FAIL_LEVEL, 2 );
  9668. break;
  9669. }
  9670. if(pc_issit(&sd)) {
  9671. //Bugged client? Just refresh them.
  9672. clif_sitting(sd.bl);
  9673. return;
  9674. }
  9675. if (sd.ud.skilltimer != INVALID_TIMER || (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING))
  9676. break;
  9677. if (!sd.sc.empty() && (
  9678. sd.sc.getSCE(SC_DANCING) ||
  9679. (sd.sc.getSCE(SC_GRAVITATION) && sd.sc.getSCE(SC_GRAVITATION)->val3 == BCT_SELF)
  9680. )) //No sitting during these states either.
  9681. break;
  9682. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9683. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9684. break;
  9685. }
  9686. if (battle_config.idletime_option&IDLE_SIT)
  9687. sd.idletime = last_tick;
  9688. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9689. sd.idletime_hom = last_tick;
  9690. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9691. sd.idletime_mer = last_tick;
  9692. pc_setsit(&sd);
  9693. skill_sit(&sd, true);
  9694. clif_sitting(sd.bl);
  9695. break;
  9696. case DMG_STAND_UP: // standup
  9697. if (!pc_issit(&sd)) {
  9698. //Bugged client? Just refresh them.
  9699. clif_standing(sd.bl);
  9700. return;
  9701. }
  9702. if (sd.sc.opt1 && sd.sc.opt1 != OPT1_STONEWAIT && sd.sc.opt1 != OPT1_BURNING)
  9703. break;
  9704. if (sd.state.block_action & PCBLOCK_SITSTAND) {
  9705. clif_displaymessage(sd.fd, msg_txt(&sd,794)); // This action is currently blocked.
  9706. break;
  9707. }
  9708. if (pc_setstand(&sd, false)) {
  9709. if (battle_config.idletime_option&IDLE_SIT)
  9710. sd.idletime = last_tick;
  9711. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_SIT)
  9712. sd.idletime_hom = last_tick;
  9713. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_SIT)
  9714. sd.idletime_mer = last_tick;
  9715. skill_sit(&sd, false);
  9716. clif_standing(sd.bl);
  9717. }
  9718. break;
  9719. }
  9720. }
  9721. /// Request for an action.
  9722. /// 0089 <target id>.L <action>.B (CZ_REQUEST_ACT)
  9723. /// 0437 <target id>.L <action>.B (CZ_REQUEST_ACT2)
  9724. /// action:
  9725. /// 0 = attack
  9726. /// 1 = pick up item
  9727. /// 2 = sit down
  9728. /// 3 = stand up
  9729. /// 7 = continous attack
  9730. /// 12 = (touch skill?)
  9731. /// There are various variants of this packet, some of them have padding between fields.
  9732. void clif_parse_ActionRequest(int32 fd, map_session_data *sd)
  9733. {
  9734. if( sd == nullptr ){
  9735. return;
  9736. }
  9737. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9738. clif_parse_ActionRequest_sub( *sd,
  9739. RFIFOB(fd,info->pos[1]),
  9740. RFIFOL(fd,info->pos[0]),
  9741. gettick()
  9742. );
  9743. }
  9744. /// Response to the death/system menu (CZ_RESTART).
  9745. /// 00b2 <type>.B
  9746. /// type:
  9747. /// 0 = restart (respawn)
  9748. /// 1 = char-select (disconnect)
  9749. void clif_parse_Restart(int32 fd, map_session_data *sd)
  9750. {
  9751. switch(RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0])) {
  9752. case 0x00:
  9753. pc_respawn(sd,CLR_OUTSIGHT);
  9754. break;
  9755. case 0x01:
  9756. /* Rovert's Prevent logout option - Fixed [Valaris] */
  9757. 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) &&
  9758. (!battle_config.prevent_logout || sd->canlog_tick == 0 || DIFF_TICK(gettick(), sd->canlog_tick) > battle_config.prevent_logout) )
  9759. { //Send to char-server for character selection.
  9760. pc_damage_log_clear(sd,0);
  9761. chrif_charselectreq(sd, session[fd]->client_addr);
  9762. } else {
  9763. clif_disconnect_ack(sd, 1);
  9764. }
  9765. break;
  9766. }
  9767. }
  9768. /// Validates and processes whispered messages (CZ_WHISPER).
  9769. /// 0096 <packet len>.W <nick>.24B <message>.?B
  9770. void clif_parse_WisMessage(int32 fd, map_session_data* sd)
  9771. {
  9772. map_session_data* dstsd;
  9773. int32 i;
  9774. char target[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  9775. // validate packet and retrieve name and message
  9776. if( !clif_process_message( sd, true, target, message, output ) )
  9777. return;
  9778. // Chat logging type 'W' / Whisper
  9779. 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);
  9780. //-------------------------------------------------------//
  9781. // Lordalfa - Paperboy - To whisper NPC commands //
  9782. //-------------------------------------------------------//
  9783. if (target[0] && (strncasecmp(target,"NPC:",4) == 0) && (strlen(target) > 4))
  9784. {
  9785. char* str = target+4; //Skip the NPC: string part.
  9786. struct npc_data* npc;
  9787. if ((npc = npc_name2id(str))) {
  9788. char split_data[NUM_WHISPER_VAR][CHAT_SIZE_MAX];
  9789. char *split;
  9790. char event[EVENT_NAME_LENGTH];
  9791. str = message;
  9792. // skip codepage indicator, if detected
  9793. if( str[0] == '|' && strlen(str) >= 4 )
  9794. str += 3;
  9795. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {// Splits the message using '#' as separators
  9796. split = strchr(str,'#');
  9797. if( split == nullptr ) { // use the remaining string
  9798. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9799. for( ++i; i < NUM_WHISPER_VAR; ++i )
  9800. split_data[i][0] = '\0';
  9801. break;
  9802. }
  9803. *split = '\0';
  9804. safestrncpy(split_data[i], str, ARRAYLENGTH(split_data[i]));
  9805. str = split+1;
  9806. }
  9807. for( i = 0; i < NUM_WHISPER_VAR; ++i ) {
  9808. char variablename[CHAT_SIZE_MAX];
  9809. safesnprintf(variablename,sizeof(variablename),"@whispervar%d$", i);
  9810. set_var_str( sd, variablename, split_data[i] );
  9811. }
  9812. safesnprintf(event,sizeof(event),"%s::%s", npc->exname,script_config.onwhisper_event_name);
  9813. npc_event(sd,event,0); // Calls the NPC label
  9814. return;
  9815. }
  9816. } else if( target[0] == '#' ) {
  9817. struct Channel *channel = nullptr;
  9818. char* chname = target;
  9819. channel = channel_name2channel(chname,sd,3);
  9820. if(channel && (pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) || ((channel->opt&CHAN_OPT_CAN_CHAT) && channel_pccheckgroup(channel,sd->group_id)))){
  9821. if(channel_pc_haschan(sd,channel)>=0){ //we are in the chan
  9822. channel_send(channel,sd,message);
  9823. }
  9824. else if( channel->pass[0] == '\0') { //no pass needed
  9825. if (channel_join(channel,sd)==0)
  9826. channel_send(channel,sd,message); //join success
  9827. }
  9828. else {
  9829. clif_displaymessage(fd, msg_txt(sd,1402)); //You're not in that channel, type '@join <#channel_name>'
  9830. }
  9831. return;
  9832. }
  9833. }
  9834. // searching destination character
  9835. dstsd = map_nick2sd(target,false);
  9836. if (dstsd == nullptr || strcmp(dstsd->status.name, target) != 0) {
  9837. // player is not on this map-server
  9838. // At this point, don't send wisp/page if it's not exactly the same name, because (example)
  9839. // if there are 'Test' player on an other map-server and 'test' player on this map-server,
  9840. // and if we ask for 'Test', we must not contact 'test' player
  9841. // so, we send information to inter-server, which is the only one which decide (and copy correct name).
  9842. intif_wis_message(sd, target, message, strlen(message) + 1);
  9843. return;
  9844. }
  9845. // if player ignores everyone
  9846. if (dstsd->state.ignoreAll && pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9847. if (pc_isinvisible(dstsd) && pc_get_group_level(sd) < pc_get_group_level(dstsd))
  9848. clif_wis_end( *sd, ACKWHISPER_TARGET_OFFLINE );
  9849. else
  9850. clif_wis_end( *sd, ACKWHISPER_ALL_IGNORED );
  9851. return;
  9852. }
  9853. // if player is autotrading
  9854. if (dstsd->state.autotrade == 1){
  9855. safesnprintf(output,sizeof(output),"%s is in autotrade mode and cannot receive whispered messages.", dstsd->status.name);
  9856. clif_wis_message(sd, wisp_server_name, output, strlen(output) + 1, 0);
  9857. return;
  9858. }
  9859. if (pc_get_group_level(sd) <= pc_get_group_level(dstsd)) {
  9860. // if player ignores the source character
  9861. ARR_FIND(0, MAX_IGNORE_LIST, i, dstsd->ignore[i].name[0] == '\0' || strcmp(dstsd->ignore[i].name, sd->status.name) == 0);
  9862. if(i < MAX_IGNORE_LIST && dstsd->ignore[i].name[0] != '\0') { // source char present in ignore list
  9863. clif_wis_end( *sd, ACKWHISPER_IGNORED );
  9864. return;
  9865. }
  9866. }
  9867. // notify sender of success
  9868. clif_wis_end( *sd, ACKWHISPER_SUCCESS );
  9869. // Normal message
  9870. clif_wis_message(dstsd, sd->status.name, message, strlen(message)+1, 0);
  9871. }
  9872. /// /b /nb.
  9873. /// Request to broadcast a message on whole server.
  9874. /// 0099 <packet len>.W <text>.?B 00 (CZ_BROADCAST)
  9875. void clif_parse_Broadcast(int32 fd, map_session_data* sd) {
  9876. const PACKET_CZ_BROADCAST* p = reinterpret_cast<PACKET_CZ_BROADCAST*>( RFIFOP( fd, 0 ) );
  9877. if( p->packetSize < sizeof( *p ) ){
  9878. return;
  9879. }
  9880. char message[CHAT_SIZE_MAX];
  9881. safestrncpy( message, p->message, std::min<size_t>( sizeof( message ), p->packetSize - sizeof( *p ) + 1 ) );
  9882. char command[CHAT_SIZE_MAX];
  9883. safesnprintf( command, sizeof( command ),"%ckami %s", atcommand_symbol, message );
  9884. is_atcommand(fd, sd, command, 1);
  9885. }
  9886. /// Request to pick up an item.
  9887. /// 009f <id>.L (CZ_ITEM_PICKUP)
  9888. /// 0362 <id>.L (CZ_ITEM_PICKUP2)
  9889. /// There are various variants of this packet, some of them have padding between fields.
  9890. void clif_parse_TakeItem(int32 fd, map_session_data *sd)
  9891. {
  9892. struct flooritem_data *fitem;
  9893. int32 map_object_id;
  9894. map_object_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  9895. fitem = (struct flooritem_data*)map_id2bl(map_object_id);
  9896. do {
  9897. if (pc_isdead(sd)) {
  9898. clif_clearunit_area( sd->bl, CLR_DEAD );
  9899. break;
  9900. }
  9901. if (fitem == nullptr || fitem->bl.type != BL_ITEM || fitem->bl.m != sd->bl.m)
  9902. break;
  9903. if (pc_cant_act(sd))
  9904. break;
  9905. if (!pc_takeitem(sd, fitem))
  9906. break;
  9907. return;
  9908. } while (0);
  9909. // Client REQUIRES a fail packet or you can no longer pick items.
  9910. clif_additem(sd,0,0,6);
  9911. }
  9912. /// Request to drop an item.
  9913. /// 00a2 <index>.W <amount>.W (CZ_ITEM_THROW)
  9914. /// 0363 <index>.W <amount>.W (CZ_ITEM_THROW2)
  9915. /// There are various variants of this packet, some of them have padding between fields.
  9916. void clif_parse_DropItem(int32 fd, map_session_data *sd){
  9917. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  9918. int32 item_index = RFIFOW(fd,info->pos[0]) -2;
  9919. int32 item_amount = RFIFOW(fd,info->pos[1]) ;
  9920. for(;;) {
  9921. if (pc_isdead(sd))
  9922. break;
  9923. if (pc_cant_act2(sd) || sd->npc_id)
  9924. break;
  9925. if (sd->sc.cant.drop)
  9926. break;
  9927. if (!pc_dropitem(sd, item_index, item_amount))
  9928. break;
  9929. if (battle_config.idletime_option&IDLE_DROPITEM)
  9930. sd->idletime = last_tick;
  9931. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_DROPITEM)
  9932. sd->idletime_hom = last_tick;
  9933. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_DROPITEM)
  9934. sd->idletime_mer = last_tick;
  9935. return;
  9936. }
  9937. //Because the client does not like being ignored.
  9938. clif_dropitem( *sd, item_index, 0 );
  9939. }
  9940. /// Request to use an item.
  9941. /// 00a7 <index>.W <account id>.L (CZ_USE_ITEM)
  9942. /// 0439 <index>.W <account id>.L (CZ_USE_ITEM2)
  9943. /// There are various variants of this packet, some of them have padding between fields.
  9944. void clif_parse_UseItem(int32 fd, map_session_data *sd)
  9945. {
  9946. int32 n;
  9947. if (pc_isdead(sd)) {
  9948. clif_clearunit_area( sd->bl, CLR_DEAD );
  9949. return;
  9950. }
  9951. if ( (!sd->npc_id && pc_istrading(sd)) || sd->chatID || (sd->state.block_action & PCBLOCK_USEITEM) ) {
  9952. clif_msg(sd, MSI_BUSY);
  9953. return;
  9954. }
  9955. //Whether the item is used or not is irrelevant, the char ain't idle. [Skotlex]
  9956. if (battle_config.idletime_option&IDLE_USEITEM)
  9957. sd->idletime = last_tick;
  9958. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  9959. sd->idletime_hom = last_tick;
  9960. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  9961. sd->idletime_mer = last_tick;
  9962. n = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  9963. if(n <0 || n >= MAX_INVENTORY)
  9964. return;
  9965. if (!pc_useitem(sd,n))
  9966. clif_useitemack(sd,n,0,false); //Send an empty ack packet or the client gets stuck.
  9967. }
  9968. /// Request to equip an item
  9969. /// 00a9 <index>.W <position>.W (CZ_REQ_WEAR_EQUIP).
  9970. /// 0998 <index>.W <position>.L (CZ_REQ_WEAR_EQUIP_V5)
  9971. void clif_parse_EquipItem( int32 fd, map_session_data* sd ){
  9972. const PACKET_CZ_REQ_WEAR_EQUIP* p = reinterpret_cast<PACKET_CZ_REQ_WEAR_EQUIP*>( RFIFOP( fd, 0 ) );
  9973. if(pc_isdead(sd)) {
  9974. clif_clearunit_area( sd->bl, CLR_DEAD );
  9975. return;
  9976. }
  9977. uint16 index = server_index( p->index );
  9978. if( index >= MAX_INVENTORY ){
  9979. return;
  9980. }
  9981. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  9982. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  9983. return;
  9984. } else if (sd->state.storage_flag || sd->sc.opt1)
  9985. ; //You can equip/unequip stuff while storage is open/under status changes
  9986. else if (pc_cant_act2(sd))
  9987. return;
  9988. if(!sd->inventory.u.items_inventory[index].identify) {
  9989. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, index ); // fail
  9990. return;
  9991. }
  9992. if(!sd->inventory_data[index])
  9993. return;
  9994. if(sd->inventory_data[index]->type == IT_PETARMOR){
  9995. pet_equipitem(sd,index);
  9996. return;
  9997. }
  9998. if (battle_config.idletime_option&IDLE_USEITEM)
  9999. sd->idletime = last_tick;
  10000. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  10001. sd->idletime_hom = last_tick;
  10002. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  10003. sd->idletime_mer = last_tick;
  10004. //Client doesn't send the position for ammo.
  10005. if(sd->inventory_data[index]->type == IT_AMMO)
  10006. pc_equipitem(sd,index,EQP_AMMO);
  10007. else{
  10008. pc_equipitem( sd, index, p->position );
  10009. }
  10010. }
  10011. /// Request to take off an equip (CZ_REQ_TAKEOFF_EQUIP).
  10012. /// 00ab <index>.W
  10013. void clif_parse_UnequipItem(int32 fd,map_session_data *sd)
  10014. {
  10015. int32 index;
  10016. if(pc_isdead(sd)) {
  10017. clif_clearunit_area( sd->bl, CLR_DEAD );
  10018. return;
  10019. }
  10020. if((sd->npc_id && !sd->npc_item_flag) || (sd->state.block_action & PCBLOCK_EQUIP)) {
  10021. clif_msg_color( sd, MSI_CAN_NOT_EQUIP_ITEM, color_table[COLOR_RED] );
  10022. return;
  10023. } else if (sd->state.storage_flag || sd->sc.opt1)
  10024. ; //You can equip/unequip stuff while storage is open/under status changes
  10025. else if (pc_cant_act2(sd))
  10026. return;
  10027. index = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2;
  10028. if (battle_config.idletime_option&IDLE_USEITEM)
  10029. sd->idletime = last_tick;
  10030. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USEITEM)
  10031. sd->idletime_hom = last_tick;
  10032. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USEITEM)
  10033. sd->idletime_mer = last_tick;
  10034. pc_unequipitem(sd,index,1);
  10035. }
  10036. /// Request to start a conversation with an NPC.
  10037. /// 0090 <id>.L <type>.B (CZ_CONTACTNPC)
  10038. /// type:
  10039. /// 1 = click
  10040. void clif_parse_NpcClicked( int32 fd, map_session_data* sd ){
  10041. const PACKET_CZ_CONTACTNPC* p = reinterpret_cast<PACKET_CZ_CONTACTNPC*>( RFIFOP( fd, 0 ) );
  10042. if( sd == nullptr ){
  10043. return;
  10044. }
  10045. if(pc_isdead(sd)) {
  10046. clif_clearunit_area( sd->bl, CLR_DEAD );
  10047. return;
  10048. }
  10049. if( pc_cant_act2(sd) || sd->npc_id || pc_hasprogress( sd, WIP_DISABLE_NPC ) ){
  10050. #ifdef RENEWAL
  10051. clif_msg( sd, MSI_BUSY);
  10052. #endif
  10053. return;
  10054. }
  10055. if( sd->state.mail_writing )
  10056. return;
  10057. block_list* bl = map_id2bl( p->AID );
  10058. if( bl == nullptr ){
  10059. return;
  10060. }
  10061. switch (bl->type) {
  10062. case BL_MOB:
  10063. case BL_PC:
  10064. clif_parse_ActionRequest_sub( *sd, DMG_REPEAT, bl->id, gettick() );
  10065. break;
  10066. case BL_NPC:
  10067. #ifdef RENEWAL
  10068. 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
  10069. clif_msg(sd, MSI_BUSY);
  10070. break;
  10071. }
  10072. #endif
  10073. if( bl->m != -1 ){ // the user can't click floating npcs directly (hack attempt)
  10074. struct npc_data* nd = (struct npc_data*)bl;
  10075. // Progressbar is running
  10076. if( nd->progressbar.timeout > 0 ){
  10077. return;
  10078. }
  10079. npc_click(sd,nd);
  10080. }
  10081. break;
  10082. }
  10083. }
  10084. /// Selection between buy/sell was made.
  10085. /// 00c5 <id>.L <type>.B (CZ_ACK_SELECT_DEALTYPE)
  10086. /// type:
  10087. /// 0 = buy
  10088. /// 1 = sell
  10089. void clif_parse_NpcBuySellSelected( int32 fd, map_session_data* sd ){
  10090. const PACKET_CZ_ACK_SELECT_DEALTYPE* p = reinterpret_cast<PACKET_CZ_ACK_SELECT_DEALTYPE*>( RFIFOP( fd, 0 ) );
  10091. if( sd == nullptr ){
  10092. return;
  10093. }
  10094. if (sd->state.trading)
  10095. return;
  10096. npc_buysellsel( sd, p->GID, p->type );
  10097. }
  10098. /// Notification about the result of a purchase attempt from an NPC shop (ZC_PC_PURCHASE_RESULT).
  10099. /// 00ca <result>.B
  10100. /// result:
  10101. /// 0 = "The deal has successfully completed."
  10102. /// 1 = "You do not have enough zeny."
  10103. /// 2 = "You are over your Weight Limit."
  10104. /// 3 = "Out of the maximum capacity, you have too many items."
  10105. /// 9 = "Amounts are exceeded the possession of the item is not available for purchase."
  10106. /// 10 = "Props open-air store sales will be traded in RODEX"
  10107. /// 11 = "The exchange failed."
  10108. /// 12 = "The exchange was well done."
  10109. /// 13 = "The item is already sold and out of stock."
  10110. /// 14 = "There is not enough goods to exchange."
  10111. void clif_npc_buy_result( map_session_data* sd, e_purchase_result result ){
  10112. PACKET_ZC_PC_PURCHASE_RESULT p = {};
  10113. p.packetType = HEADER_ZC_PC_PURCHASE_RESULT;
  10114. p.result = (uint8)result;
  10115. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  10116. }
  10117. /// Request to buy chosen items from npc shop.
  10118. /// 00c8 <packet len>.W { <amount>.W <name id>.W }* (CZ_PC_PURCHASE_ITEMLIST)
  10119. void clif_parse_NpcBuyListSend( int32 fd, map_session_data* sd ){
  10120. const PACKET_CZ_PC_PURCHASE_ITEMLIST *p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10121. uint16 n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->items[0] );
  10122. e_purchase_result result;
  10123. if( sd->state.trading || !sd->npc_shopid )
  10124. result = e_purchase_result::PURCHASE_FAIL_MONEY;
  10125. else{
  10126. std::vector<s_npc_buy_list> items = {};
  10127. items.reserve( n );
  10128. for( uint16 i = 0; i < n; i++ ){
  10129. s_npc_buy_list item = {};
  10130. item.nameid = p->items[i].itemId;
  10131. item.qty = p->items[i].amount;
  10132. items.push_back( item );
  10133. }
  10134. result = npc_buylist( sd, items );
  10135. }
  10136. sd->npc_shopid = 0; //Clear shop data.
  10137. clif_npc_buy_result(sd, result);
  10138. }
  10139. /// Notification about the result of a sell attempt to an NPC shop (ZC_PC_SELL_RESULT).
  10140. /// 00cb <result>.B
  10141. /// result:
  10142. /// 0 = "The deal has successfully completed."
  10143. /// 1 = "The deal has failed."
  10144. void clif_npc_sell_result(map_session_data* sd, unsigned char result)
  10145. {
  10146. int32 fd = sd->fd;
  10147. WFIFOHEAD(fd,packet_len(0xcb));
  10148. WFIFOW(fd,0) = 0xcb;
  10149. WFIFOB(fd,2) = result;
  10150. WFIFOSET(fd,packet_len(0xcb));
  10151. }
  10152. /// Request to sell chosen items to npc shop (CZ_PC_SELL_ITEMLIST).
  10153. /// 00c9 <packet len>.W { <index>.W <amount>.W }*
  10154. void clif_parse_NpcSellListSend(int32 fd,map_session_data *sd)
  10155. {
  10156. int32 fail=0;
  10157. const PACKET_CZ_PC_SELL_ITEMLIST* p = reinterpret_cast<PACKET_CZ_PC_SELL_ITEMLIST*>( RFIFOP( fd, 0 ) );
  10158. int32 n = ( p->packetLength - sizeof( *p ) ) / sizeof( p->sellList[0] );
  10159. if (sd->state.trading || !sd->npc_shopid)
  10160. fail = 1;
  10161. else
  10162. fail = npc_selllist(sd, n, p->sellList);
  10163. sd->npc_shopid = 0; //Clear shop data.
  10164. clif_npc_sell_result(sd, fail);
  10165. }
  10166. /// Chatroom creation request.
  10167. /// 00d5 <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B (CZ_CREATE_CHATROOM)
  10168. /// type:
  10169. /// 0 = private
  10170. /// 1 = public
  10171. void clif_parse_CreateChatRoom( int32 fd, map_session_data* sd){
  10172. const PACKET_CZ_CREATE_CHATROOM* p = reinterpret_cast<PACKET_CZ_CREATE_CHATROOM*>( RFIFOP( fd, 0 ) );
  10173. if( sd == nullptr ){
  10174. return;
  10175. }
  10176. if( sd->sc.getSCE( SC_NOCHAT ) && sd->sc.getSCE( SC_NOCHAT )->val1&MANNER_NOROOM ){
  10177. return;
  10178. }
  10179. if(battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 4 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10180. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 3 );
  10181. return;
  10182. }
  10183. if( npc_isnear(&sd->bl) ) {
  10184. // uncomment to send msg_txt.
  10185. //char output[150];
  10186. //sprintf(output, msg_txt(662), battle_config.min_npc_vendchat_distance);
  10187. //clif_displaymessage(sd->fd, output);
  10188. clif_skill_fail( *sd, 1, USESKILL_FAIL_THERE_ARE_NPC_AROUND );
  10189. return;
  10190. }
  10191. if( p->packetSize < sizeof( *p ) ){
  10192. // invalid input
  10193. return;
  10194. }
  10195. char password[CHATROOM_PASS_SIZE];
  10196. safestrncpy( password, p->password, sizeof( password ) );
  10197. char title[CHATROOM_TITLE_SIZE];
  10198. safestrncpy( title, p->title, std::min<size_t>( sizeof( title ), p->packetSize - sizeof( *p ) + 1 ) );
  10199. chat_createpcchat( sd, title, password, p->limit, p->type != 0 );
  10200. }
  10201. /// Chatroom join request (CZ_REQ_ENTER_ROOM).
  10202. /// 00d9 <chat ID>.L <passwd>.8B
  10203. void clif_parse_ChatAddMember(int32 fd, map_session_data* sd){
  10204. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10205. int32 chatid = RFIFOL(fd,info->pos[0]);
  10206. const char* password = RFIFOCP(fd,info->pos[1]); // not zero-terminated
  10207. chat_joinchat(sd,chatid,password);
  10208. }
  10209. /// Chatroom properties adjustment request (CZ_CHANGE_CHATROOM).
  10210. /// 00de <packet len>.W <limit>.W <type>.B <passwd>.8B <title>.?B
  10211. /// type:
  10212. /// 0 = private
  10213. /// 1 = public
  10214. void clif_parse_ChatRoomStatusChange(int32 fd, map_session_data* sd){
  10215. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10216. int32 len = RFIFOW(fd,info->pos[0])-15;
  10217. int32 limit = RFIFOW(fd,info->pos[1]);
  10218. bool pub = (RFIFOB(fd,info->pos[2]) != 0);
  10219. const char* password = RFIFOCP(fd,info->pos[3]); // not zero-terminated
  10220. const char* title = RFIFOCP(fd,info->pos[4]); // not zero-terminated
  10221. char s_password[CHATROOM_PASS_SIZE];
  10222. char s_title[CHATROOM_TITLE_SIZE];
  10223. if( len <= 0 )
  10224. return; // invalid input
  10225. safestrncpy(s_password, password, CHATROOM_PASS_SIZE);
  10226. safestrncpy(s_title, title, min(len+1,CHATROOM_TITLE_SIZE)); //NOTE: assumes that safestrncpy will not access the len+1'th byte
  10227. chat_changechatstatus(sd, s_title, s_password, limit, pub);
  10228. }
  10229. /// Request to change the chat room ownership (CZ_REQ_ROLE_CHANGE).
  10230. /// 00e0 <role>.L <nick>.24B
  10231. /// role:
  10232. /// 0 = owner
  10233. /// 1 = normal
  10234. void clif_parse_ChangeChatOwner(int32 fd, map_session_data* sd)
  10235. {
  10236. //int32 role = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10237. chat_changechatowner(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]));
  10238. }
  10239. /// Request to expel a player from chat room (CZ_REQ_EXPEL_MEMBER).
  10240. /// 00e2 <name>.24B
  10241. void clif_parse_KickFromChat(int32 fd,map_session_data *sd)
  10242. {
  10243. chat_kickchat(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10244. }
  10245. /// Request to leave the current chatroom (CZ_EXIT_ROOM).
  10246. /// 00e3
  10247. void clif_parse_ChatLeave(int32 fd, map_session_data* sd)
  10248. {
  10249. chat_leavechat(sd,0);
  10250. }
  10251. // Handles notifying asker and rejecter of what has just ocurred.
  10252. // Type is used to determine the correct msg_txt to use
  10253. void clif_noask_sub( map_session_data& sd, map_session_data& tsd, int32 type ){
  10254. char output[CHAT_SIZE_MAX];
  10255. // Your request has been rejected by autoreject option.
  10256. clif_messagecolor( &sd.bl, color_table[COLOR_LIGHT_GREEN], msg_txt( &sd, 392 ), false, SELF );
  10257. // Notice that a request was rejected.
  10258. safesnprintf( output, CHAT_SIZE_MAX, msg_txt( &tsd, type ), sd.status.name );
  10259. clif_messagecolor( &tsd.bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  10260. }
  10261. /// Request to begin a trade (CZ_REQ_EXCHANGE_ITEM).
  10262. /// 00e4 <account id>.L
  10263. void clif_parse_TradeRequest(int32 fd,map_session_data *sd)
  10264. {
  10265. map_session_data *t_sd;
  10266. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10267. if(!sd->chatID && pc_cant_act(sd))
  10268. return; //You can trade while in a chatroom.
  10269. if(t_sd){
  10270. // @noask [LuzZza]
  10271. if(t_sd->state.noask) {
  10272. clif_noask_sub( *sd, *t_sd, 393 ); // Autorejected trade request from %s.
  10273. return;
  10274. }
  10275. if (t_sd->state.mail_writing) {
  10276. // Fake trading
  10277. clif_traderesponse(*sd,TRADE_ACK_BUSY);
  10278. return;
  10279. }
  10280. }
  10281. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 1 && pc_checkskill(sd, SU_BASIC_SKILL) < 1) {
  10282. clif_skill_fail( *sd, 1 );
  10283. return;
  10284. }
  10285. trade_traderequest(sd,t_sd);
  10286. }
  10287. /// Answer to a trade request (CZ_ACK_EXCHANGE_ITEM).
  10288. /// 00e6 <result>.B
  10289. /// result:
  10290. /// 3 = accepted
  10291. /// 4 = rejected
  10292. void clif_parse_TradeAck(int32 fd,map_session_data *sd)
  10293. {
  10294. trade_tradeack(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10295. }
  10296. /// Request to add an item to current trade (CZ_ADD_EXCHANGE_ITEM).
  10297. /// 00e8 <index>.W <amount>.L
  10298. void clif_parse_TradeAddItem(int32 fd,map_session_data *sd)
  10299. {
  10300. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10301. short index = RFIFOW(fd,info->pos[0]);
  10302. int32 amount = RFIFOL(fd,info->pos[1]);
  10303. if( index == 0 )
  10304. trade_tradeaddzeny(sd, amount);
  10305. else
  10306. trade_tradeadditem(sd, server_index(index), (short)amount);
  10307. }
  10308. /// Request to lock items in current trade (CZ_CONCLUDE_EXCHANGE_ITEM).
  10309. /// 00eb
  10310. void clif_parse_TradeOk(int32 fd,map_session_data *sd)
  10311. {
  10312. trade_tradeok(sd);
  10313. }
  10314. /// Request to cancel current trade (CZ_CANCEL_EXCHANGE_ITEM).
  10315. /// 00ed
  10316. void clif_parse_TradeCancel(int32 fd,map_session_data *sd)
  10317. {
  10318. trade_tradecancel(sd);
  10319. }
  10320. /// Request to commit current trade (CZ_EXEC_EXCHANGE_ITEM).
  10321. /// 00ef
  10322. void clif_parse_TradeCommit(int32 fd,map_session_data *sd)
  10323. {
  10324. trade_tradecommit(sd);
  10325. }
  10326. /// Request to stop chasing/attacking an unit (CZ_CANCEL_LOCKON).
  10327. /// 0118
  10328. void clif_parse_StopAttack(int32 fd,map_session_data *sd)
  10329. {
  10330. pc_stop_attack(sd);
  10331. if (sd) sd->ud.state.attack_continue = 0;
  10332. }
  10333. /// Request to move an item from inventory to cart.
  10334. /// 0126 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_CART)
  10335. void clif_parse_PutItemToCart( int32 fd, map_session_data *sd ){
  10336. if (pc_istrading(sd) || !pc_iscarton(sd) || pc_cant_act2(sd))
  10337. return;
  10338. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10339. return;
  10340. const PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART* p = reinterpret_cast<PACKET_CZ_MOVE_ITEM_FROM_BODY_TO_CART*>( RFIFOP( fd, 0 ) );
  10341. pc_putitemtocart( sd, server_index( p->index ), p->count);
  10342. }
  10343. /// Request to move an item from cart to inventory (CZ_MOVE_ITEM_FROM_CART_TO_BODY).
  10344. /// 0127 <index>.W <amount>.L
  10345. void clif_parse_GetItemFromCart(int32 fd,map_session_data *sd)
  10346. {
  10347. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10348. if (!pc_iscarton(sd) || pc_cant_act2(sd))
  10349. return;
  10350. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  10351. return;
  10352. pc_getitemfromcart(sd,RFIFOW(fd,info->pos[0])-2,RFIFOL(fd,info->pos[1]));
  10353. }
  10354. /// Request to remove cart/falcon/peco/dragon/mado (CZ_REQ_CARTOFF).
  10355. /// 012a
  10356. void clif_parse_RemoveOption(int32 fd,map_session_data *sd)
  10357. {
  10358. if( !(sd->sc.option&(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR))
  10359. #ifdef NEW_CARTS
  10360. && sd->sc.getSCE(SC_PUSH_CART) )
  10361. pc_setcart(sd,0);
  10362. #else
  10363. )
  10364. pc_setoption(sd,sd->sc.option&~OPTION_CART);
  10365. #endif
  10366. else // priority to remove this option before we can clear cart
  10367. pc_setoption(sd,sd->sc.option&~(OPTION_RIDING|OPTION_FALCON|OPTION_DRAGON|OPTION_MADOGEAR));
  10368. }
  10369. /// Request to select cart's visual look for new cart design (ZC_SELECTCART).
  10370. /// 097f <Length>.W <identity>.L <type>.B
  10371. void clif_SelectCart(map_session_data *sd) {
  10372. #if PACKETVER >= 20150826
  10373. int32 i = 0, carts = 3;
  10374. int32 fd = sd->fd;
  10375. WFIFOHEAD(fd,8 + carts);
  10376. WFIFOW(fd,0) = 0x97f;
  10377. WFIFOW(fd,2) = 8 + carts;
  10378. WFIFOL(fd,4) = sd->status.account_id;
  10379. // Right now we have 10-12, tested it you can also enable selection for all cart styles here(1-12)
  10380. for( i = 0; i < carts; i++ ) {
  10381. WFIFOB(fd,8 + i) = 10 + i;
  10382. }
  10383. WFIFOSET(fd,8 + carts);
  10384. #endif
  10385. }
  10386. /// Request to select cart's visual look for new cart design (CZ_SELECTCART).
  10387. /// 0980 <identity>.L <type>.B
  10388. void clif_parse_SelectCart(int32 fd,map_session_data *sd) {
  10389. #if PACKETVER >= 20150826
  10390. int32 type;
  10391. // Check identity
  10392. if( !sd || pc_checkskill(sd,MC_CARTDECORATE) < 1 || RFIFOL(fd,2) != sd->status.account_id )
  10393. return;
  10394. type = (int32)RFIFOB(fd,6);
  10395. // Check type
  10396. if( type < 10 || type > 12 )
  10397. return;
  10398. pc_setcart(sd, type);
  10399. #endif
  10400. }
  10401. /// Request to change cart's visual look (CZ_REQ_CHANGECART).
  10402. /// 01af <num>.W
  10403. void clif_parse_ChangeCart(int32 fd,map_session_data *sd)
  10404. {// TODO: State tracking?
  10405. int32 type;
  10406. if( !sd || pc_checkskill(sd, MC_CHANGECART) < 1 )
  10407. return;
  10408. #ifdef RENEWAL
  10409. if (sd->npc_id || pc_hasprogress(sd, WIP_DISABLE_SKILLITEM)) {
  10410. clif_msg(sd, MSI_BUSY);
  10411. return;
  10412. }
  10413. #endif
  10414. type = (int32)RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  10415. if(
  10416. #ifdef NEW_CARTS
  10417. #if PACKETVER >= 20191106
  10418. (type == 13 && sd->status.base_level > 100) ||
  10419. #endif
  10420. (type == 9 && sd->status.base_level > 130) ||
  10421. (type == 8 && sd->status.base_level > 120) ||
  10422. (type == 7 && sd->status.base_level > 110) ||
  10423. (type == 6 && sd->status.base_level > 100) ||
  10424. #endif
  10425. (type == 5 && sd->status.base_level > 90) ||
  10426. (type == 4 && sd->status.base_level > 80) ||
  10427. (type == 3 && sd->status.base_level > 65) ||
  10428. (type == 2 && sd->status.base_level > 40) ||
  10429. (type == 1))
  10430. pc_setcart(sd, type);
  10431. }
  10432. /// Request to increase status (CZ_STATUS_CHANGE).
  10433. /// 00bb <status id>.W <amount>.B
  10434. /// status id:
  10435. /// SP_STR ~ SP_LUK
  10436. /// amount:
  10437. /// Old clients always send 1 for this, even when using /str+ and the like.
  10438. /// Newer clients (2013-12-23 and newer) send the correct amount.
  10439. void clif_parse_StatusUp(int32 fd,map_session_data *sd)
  10440. {
  10441. int32 increase_amount = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  10442. if( increase_amount < 0 ) {
  10443. ShowDebug("clif_parse_StatusUp: Negative 'increase' value sent by client! (fd: %d, value: %d)\n",
  10444. fd, increase_amount);
  10445. }
  10446. pc_statusup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]),increase_amount);
  10447. }
  10448. /// Request to increase trait status.
  10449. /// 0b24 <status id>.W <amount>.W (CZ_ADVANCED_STATUS_CHANGE)
  10450. /// status id:
  10451. /// SP_POW ~ SP_CON
  10452. /// amount:
  10453. /// The amount to increase the trait status
  10454. void clif_parse_traitstatus_up( int32 fd, map_session_data *sd ){
  10455. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10456. const PACKET_CZ_ADVANCED_STATUS_CHANGE* p = reinterpret_cast<PACKET_CZ_ADVANCED_STATUS_CHANGE*>( RFIFOP( fd, 0 ) );
  10457. if( p->amount < 0 ){
  10458. 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 );
  10459. return;
  10460. }
  10461. pc_traitstatusup( sd, p->type, p->amount );
  10462. #endif
  10463. }
  10464. /// Request to increase level of a skill (CZ_UPGRADE_SKILLLEVEL).
  10465. /// 0112 <skill id>.W
  10466. void clif_parse_SkillUp(int32 fd,map_session_data *sd)
  10467. {
  10468. pc_skillup(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  10469. }
  10470. static void clif_parse_UseSkillToId_homun(struct homun_data *hd, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, int32 target_id)
  10471. {
  10472. int32 lv;
  10473. if( !hd )
  10474. return;
  10475. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10476. clif_emotion(&hd->bl, ET_THINK);
  10477. return;
  10478. }
  10479. if( hd->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10480. target_id = hd->bl.id;
  10481. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10482. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10483. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10484. clif_emotion(&hd->bl, ET_THINK);
  10485. if (hd->master)
  10486. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10487. return;
  10488. }
  10489. lv = hom_checkskill(hd, skill_id);
  10490. if( skill_lv > lv )
  10491. skill_lv = lv;
  10492. if( skill_lv )
  10493. unit_skilluse_id(&hd->bl, target_id, skill_id, skill_lv);
  10494. }
  10495. 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, int32 skillmoreinfo)
  10496. {
  10497. int32 lv;
  10498. if( !hd )
  10499. return;
  10500. if( skill_isNotOk_hom(hd, skill_id, skill_lv) ) {
  10501. clif_emotion(&hd->bl, ET_THINK);
  10502. return;
  10503. }
  10504. if( hd->ud.skilltimer != INVALID_TIMER ) {
  10505. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10506. } else if( DIFF_TICK(tick, hd->ud.canact_tick) < 0 ) {
  10507. clif_emotion(&hd->bl, ET_THINK);
  10508. if (hd->master)
  10509. clif_skill_fail( *hd->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10510. return;
  10511. }
  10512. #ifdef RENEWAL
  10513. if (hd->sc.getSCE(SC_BASILICA_CELL))
  10514. #else
  10515. if (hd->sc.getSCE(SC_BASILICA))
  10516. #endif
  10517. return;
  10518. lv = hom_checkskill(hd, skill_id);
  10519. if( skill_lv > lv )
  10520. skill_lv = lv;
  10521. if( skill_lv )
  10522. unit_skilluse_pos(&hd->bl, x, y, skill_id, skill_lv);
  10523. }
  10524. static void clif_parse_UseSkillToId_mercenary(s_mercenary_data *md, map_session_data *sd, t_tick tick, uint16 skill_id, uint16 skill_lv, int32 target_id)
  10525. {
  10526. int32 lv;
  10527. if( !md )
  10528. return;
  10529. if( skill_isNotOk_mercenary(skill_id, *md) )
  10530. return;
  10531. if( md->bl.id != target_id && skill_get_inf(skill_id)&INF_SELF_SKILL )
  10532. target_id = md->bl.id;
  10533. if( md->ud.skilltimer != INVALID_TIMER )
  10534. {
  10535. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST ) return;
  10536. }
  10537. else if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10538. return;
  10539. lv = mercenary_checkskill(md, skill_id);
  10540. if( skill_lv > lv )
  10541. skill_lv = lv;
  10542. if( skill_lv )
  10543. unit_skilluse_id(&md->bl, target_id, skill_id, skill_lv);
  10544. }
  10545. 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, int32 skillmoreinfo)
  10546. {
  10547. int32 lv;
  10548. if( !md )
  10549. return;
  10550. if( skill_isNotOk_mercenary(skill_id, *md) )
  10551. return;
  10552. if( md->ud.skilltimer != INVALID_TIMER )
  10553. return;
  10554. if( DIFF_TICK(tick, md->ud.canact_tick) < 0 )
  10555. {
  10556. if (md->master)
  10557. clif_skill_fail( *md->master, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10558. return;
  10559. }
  10560. #ifdef RENEWAL
  10561. if (md->sc.getSCE(SC_BASILICA_CELL))
  10562. #else
  10563. if (md->sc.getSCE(SC_BASILICA))
  10564. #endif
  10565. return;
  10566. lv = mercenary_checkskill(md, skill_id);
  10567. if( skill_lv > lv )
  10568. skill_lv = lv;
  10569. if( skill_lv )
  10570. unit_skilluse_pos(&md->bl, x, y, skill_id, skill_lv);
  10571. }
  10572. void clif_parse_skill_toid( map_session_data* sd, uint16 skill_id, uint16 skill_lv, int32 target_id ){
  10573. if( sd == nullptr ){
  10574. return;
  10575. }
  10576. t_tick tick = gettick();
  10577. if( skill_lv < 1 ) skill_lv = 1; //No clue, I have seen the client do this with guild skills :/ [Skotlex]
  10578. int32 inf = skill_get_inf(skill_id);
  10579. if (inf&INF_GROUND_SKILL || !inf)
  10580. return; //Using a ground/passive skill on a target? WRONG.
  10581. if (sd->state.block_action & PCBLOCK_SKILL) {
  10582. clif_msg(sd, MSI_BUSY);
  10583. return;
  10584. }
  10585. if( SKILL_CHK_HOMUN(skill_id) ) {
  10586. clif_parse_UseSkillToId_homun(sd->hd, sd, tick, skill_id, skill_lv, target_id);
  10587. return;
  10588. }
  10589. if( SKILL_CHK_MERC(skill_id) ) {
  10590. clif_parse_UseSkillToId_mercenary(sd->md, sd, tick, skill_id, skill_lv, target_id);
  10591. return;
  10592. }
  10593. // Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10594. // This is done here, because homunculi and mercenaries can be triggered by AI and not by the player itself
  10595. if (battle_config.idletime_option&IDLE_USESKILLTOID)
  10596. sd->idletime = last_tick;
  10597. if (battle_config.hom_idle_no_share && sd->hd && battle_config.idletime_hom_option&IDLE_USESKILLTOID)
  10598. sd->idletime_hom = last_tick;
  10599. if (battle_config.mer_idle_no_share && sd->md && battle_config.idletime_mer_option&IDLE_USESKILLTOID)
  10600. sd->idletime_mer = last_tick;
  10601. if( sd->npc_id ){
  10602. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag || !( inf & INF_SELF_SKILL ) ){
  10603. #ifdef RENEWAL
  10604. clif_msg( sd, MSI_BUSY);
  10605. #endif
  10606. return;
  10607. }
  10608. }
  10609. if ((pc_cant_act2(sd) || sd->chatID) &&
  10610. skill_id != RK_REFRESH &&
  10611. !( ( skill_id == SR_GENTLETOUCH_CURE || skill_id == SU_GROOMING ) && (sd->sc.opt1 == OPT1_STONE || sd->sc.opt1 == OPT1_FREEZE || sd->sc.opt1 == OPT1_STUN)) &&
  10612. !(sd->state.storage_flag && (inf&INF_SELF_SKILL))) //SELF skills can be used with the storage open, issue: 8027
  10613. return;
  10614. if( pc_issit(sd) )
  10615. return;
  10616. if( skill_isNotOk(skill_id, *sd) )
  10617. return;
  10618. if( sd->bl.id != target_id && inf&INF_SELF_SKILL )
  10619. target_id = sd->bl.id; // never trust the client
  10620. if( target_id < 0 && -target_id == sd->bl.id ) // for disguises [Valaris]
  10621. target_id = sd->bl.id;
  10622. if( sd->ud.skilltimer != INVALID_TIMER ) {
  10623. if( skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST )
  10624. return;
  10625. } else if( DIFF_TICK(tick, sd->ud.canact_tick) < 0 ) {
  10626. if( sd->skillitem != skill_id ) {
  10627. clif_skill_fail( *sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10628. return;
  10629. }
  10630. }
  10631. if( sd->sc.option&OPTION_COSTUME )
  10632. return;
  10633. #ifndef RENEWAL
  10634. if( sd->sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd->sc.getSCE(SC_BASILICA)->val4 != sd->bl.id) )
  10635. return; // On basilica only caster can use Basilica again to stop it.
  10636. #endif
  10637. if( sd->menuskill_id ) {
  10638. if( sd->menuskill_id == SA_TAMINGMONSTER ) {
  10639. clif_menuskill_clear(sd); //Cancel pet capture.
  10640. }else if( sd->menuskill_id == SG_FEEL ){
  10641. clif_menuskill_clear( sd ); // Cancel selection
  10642. }else if( sd->menuskill_id != SA_AUTOSPELL )
  10643. return; //Can't use skills while a menu is open.
  10644. }
  10645. if( sd->skillitem == skill_id ) {
  10646. if( skill_lv != sd->skillitemlv )
  10647. skill_lv = sd->skillitemlv;
  10648. if( !(inf&INF_SELF_SKILL) )
  10649. pc_delinvincibletimer(sd); // Target skills thru items cancel invincibility. [Inkfish]
  10650. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10651. return;
  10652. }
  10653. sd->skillitem = sd->skillitemlv = 0;
  10654. if( SKILL_CHK_GUILD(skill_id) ) {
  10655. if (sd->guild != nullptr && (sd->state.gmaster_flag || skill_id == GD_CHARGESHOUT_BEATING))
  10656. skill_lv = guild_checkskill(sd->guild->guild, skill_id);
  10657. else
  10658. skill_lv = 0;
  10659. } else {
  10660. if( skill_id != ALL_EQSWITCH ){
  10661. skill_lv = min(pc_checkskill(sd, skill_id),skill_lv); //never trust client
  10662. }
  10663. }
  10664. pc_delinvincibletimer(sd);
  10665. if( skill_lv )
  10666. unit_skilluse_id(&sd->bl, target_id, skill_id, skill_lv);
  10667. }
  10668. /// Request to use a targeted skill.
  10669. /// 0113 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL)
  10670. /// 0438 <skill lv>.W <skill id>.W <target id>.L (CZ_USE_SKILL2)
  10671. /// There are various variants of this packet, some of them have padding between fields.
  10672. void clif_parse_UseSkillToId( int32 fd, map_session_data *sd ){
  10673. struct s_packet_db* info = &packet_db[RFIFOW(fd, 0)];
  10674. clif_parse_skill_toid( sd, RFIFOW(fd, info->pos[1]), RFIFOW(fd, info->pos[0]), RFIFOL(fd, info->pos[2]) );
  10675. }
  10676. /*==========================================
  10677. * Client tells server he'd like to use AoE skill id 'skill_id' of level 'skill_lv' on 'x','y' location
  10678. *------------------------------------------*/
  10679. static void clif_parse_UseSkillToPosSub( int32 fd, map_session_data& sd, uint16 skill_lv, uint16 skill_id, short x, short y, int32 skillmoreinfo ){
  10680. t_tick tick = gettick();
  10681. if( !(skill_get_inf(skill_id)&INF_GROUND_SKILL) )
  10682. return; //Using a target skill on the ground? WRONG.
  10683. if (sd.state.block_action & PCBLOCK_SKILL) {
  10684. clif_msg(&sd, MSI_BUSY);
  10685. return;
  10686. }
  10687. if( SKILL_CHK_HOMUN(skill_id) ) {
  10688. clif_parse_UseSkillToPos_homun(sd.hd, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10689. return;
  10690. }
  10691. if( SKILL_CHK_MERC(skill_id) ) {
  10692. clif_parse_UseSkillToPos_mercenary(sd.md, &sd, tick, skill_id, skill_lv, x, y, skillmoreinfo);
  10693. return;
  10694. }
  10695. if( pc_hasprogress( &sd, WIP_DISABLE_SKILLITEM ) ){
  10696. #ifdef RENEWAL
  10697. clif_msg( &sd, MSI_BUSY);
  10698. #endif
  10699. return;
  10700. }
  10701. //Whether skill fails or not is irrelevant, the char ain't idle. [Skotlex]
  10702. if (battle_config.idletime_option&IDLE_USESKILLTOPOS)
  10703. sd.idletime = last_tick;
  10704. if (battle_config.hom_idle_no_share && sd.hd && battle_config.idletime_hom_option&IDLE_USESKILLTOPOS)
  10705. sd.idletime_hom = last_tick;
  10706. if (battle_config.mer_idle_no_share && sd.md && battle_config.idletime_mer_option&IDLE_USESKILLTOPOS)
  10707. sd.idletime_mer = last_tick;
  10708. if( skill_isNotOk(skill_id, sd) )
  10709. return;
  10710. if( skillmoreinfo != -1 ) {
  10711. if( pc_issit(&sd) ) {
  10712. clif_skill_fail( sd, skill_id );
  10713. return;
  10714. }
  10715. //You can't use Graffiti/TalkieBox AND have a vending open, so this is safe.
  10716. safestrncpy(sd.message, RFIFOCP(fd,skillmoreinfo), MESSAGE_SIZE);
  10717. }
  10718. if( sd.ud.skilltimer != INVALID_TIMER )
  10719. return;
  10720. if( DIFF_TICK(tick, sd.ud.canact_tick) < 0 ) {
  10721. if( sd.skillitem != skill_id ) {
  10722. clif_skill_fail( sd, skill_id, USESKILL_FAIL_SKILLINTERVAL );
  10723. return;
  10724. }
  10725. }
  10726. if( sd.sc.option&OPTION_COSTUME )
  10727. return;
  10728. #ifndef RENEWAL
  10729. if( sd.sc.getSCE(SC_BASILICA) && (skill_id != HP_BASILICA || sd.sc.getSCE(SC_BASILICA)->val4 != sd.bl.id) )
  10730. return; // On basilica only caster can use Basilica again to stop it.
  10731. #endif
  10732. if( sd.menuskill_id ) {
  10733. if( sd.menuskill_id == SA_TAMINGMONSTER ) {
  10734. clif_menuskill_clear(&sd); //Cancel pet capture.
  10735. }else if( sd.menuskill_id == SG_FEEL ){
  10736. clif_menuskill_clear( &sd ); // Cancel selection
  10737. } else if( sd.menuskill_id != SA_AUTOSPELL )
  10738. return; //Can't use skills while a menu is open.
  10739. }
  10740. pc_delinvincibletimer(&sd);
  10741. if( sd.skillitem == skill_id ) {
  10742. if( skill_lv != sd.skillitemlv )
  10743. skill_lv = sd.skillitemlv;
  10744. unit_skilluse_pos(&sd.bl, x, y, skill_id, skill_lv);
  10745. } else {
  10746. int32 lv;
  10747. sd.skillitem = sd.skillitemlv = 0;
  10748. if( (lv = pc_checkskill(&sd, skill_id)) > 0 ) {
  10749. if( skill_lv > lv )
  10750. skill_lv = lv;
  10751. unit_skilluse_pos(&sd.bl, x, y, skill_id,skill_lv);
  10752. }
  10753. }
  10754. }
  10755. /// Request to use a ground skill.
  10756. /// 0116 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND)
  10757. /// 0366 <skill lv>.W <skill id>.W <x>.W <y>.W (CZ_USE_SKILL_TOGROUND2)
  10758. /// 0AF4 <skill lv>.W <skill id>.W <x>.W <y>.W <unknown>.B (CZ_USE_SKILL_TOGROUND3)
  10759. /// There are various variants of this packet, some of them have padding between fields.
  10760. void clif_parse_UseSkillToPos(int32 fd, map_session_data *sd)
  10761. {
  10762. if( sd == nullptr ){
  10763. return;
  10764. }
  10765. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10766. if (pc_cant_act(sd))
  10767. return;
  10768. if (pc_issit(sd))
  10769. return;
  10770. clif_parse_UseSkillToPosSub(fd, *sd,
  10771. RFIFOW(fd,info->pos[0]), //skill lv
  10772. RFIFOW(fd,info->pos[1]), //skill num
  10773. RFIFOW(fd,info->pos[2]), //pos x
  10774. RFIFOW(fd,info->pos[3]), //pos y
  10775. // TODO: find out what this is intended to do
  10776. //RFIFOB(fd, info->pos[4])
  10777. -1 //Skill more info.
  10778. );
  10779. }
  10780. /// Request to use a ground skill with text.
  10781. /// 0190 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX)
  10782. /// 0367 <skill lv>.W <skill id>.W <x>.W <y>.W <contents>.80B (CZ_USE_SKILL_TOGROUND_WITHTALKBOX2)
  10783. /// There are various variants of this packet, some of them have padding between fields.
  10784. void clif_parse_UseSkillToPosMoreInfo(int32 fd, map_session_data *sd)
  10785. {
  10786. if( sd == nullptr ){
  10787. return;
  10788. }
  10789. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10790. if (pc_cant_act(sd))
  10791. return;
  10792. if (pc_issit(sd))
  10793. return;
  10794. clif_parse_UseSkillToPosSub(fd, *sd,
  10795. RFIFOW(fd,info->pos[0]), //Skill lv
  10796. RFIFOW(fd,info->pos[1]), //Skill num
  10797. RFIFOW(fd,info->pos[2]), //pos x
  10798. RFIFOW(fd,info->pos[3]), //pos y
  10799. info->pos[4] //skill more info
  10800. );
  10801. }
  10802. /// Answer to map selection dialog (CZ_SELECT_WARPPOINT).
  10803. /// 011b <skill id>.W <map name>.16B
  10804. void clif_parse_UseSkillMap(int32 fd, map_session_data* sd)
  10805. {
  10806. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10807. uint16 skill_id = RFIFOW(fd,info->pos[0]);
  10808. char map_name[MAP_NAME_LENGTH];
  10809. mapindex_getmapname(RFIFOCP(fd,info->pos[1]), map_name);
  10810. sd->state.workinprogress = WIP_DISABLE_NONE;
  10811. if(skill_id != sd->menuskill_id)
  10812. return;
  10813. //It is possible to use teleport with the storage window open bugreport:8027
  10814. if (pc_cant_act(sd) && !sd->state.storage_flag && skill_id != AL_TELEPORT) {
  10815. clif_menuskill_clear(sd);
  10816. return;
  10817. }
  10818. pc_delinvincibletimer(sd);
  10819. skill_castend_map(sd,skill_id,map_name);
  10820. }
  10821. /// Request to set a memo on current map (CZ_REMEMBER_WARPPOINT).
  10822. /// 011d
  10823. void clif_parse_RequestMemo(int32 fd,map_session_data *sd)
  10824. {
  10825. if (!pc_isdead(sd))
  10826. pc_memo(sd,-1);
  10827. }
  10828. /// Answer to pharmacy item selection dialog.
  10829. /// 018e <name id>.W { <material id>.W }*3 (CZ_REQMAKINGITEM)
  10830. void clif_parse_ProduceMix(int32 fd,map_session_data *sd){
  10831. if( sd == nullptr ){
  10832. return;
  10833. }
  10834. const PACKET_CZ_REQMAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQMAKINGITEM*>( RFIFOP( fd, 0 ) );
  10835. switch( sd->menuskill_id ) {
  10836. case -1:
  10837. case AM_PHARMACY:
  10838. case RK_RUNEMASTERY:
  10839. case GC_RESEARCHNEWPOISON:
  10840. break;
  10841. default:
  10842. return;
  10843. }
  10844. if (pc_istrading(sd)) {
  10845. //Make it fail to avoid shop exploits where you sell something different than you see.
  10846. clif_skill_fail( *sd, sd->ud.skill_id );
  10847. clif_menuskill_clear(sd);
  10848. return;
  10849. }
  10850. int32 produce_idx;
  10851. if( (produce_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, 1)) )
  10852. skill_produce_mix(sd,0,p->itemId,p->material[0],p->material[1],p->material[2],1,produce_idx-1);
  10853. clif_menuskill_clear(sd);
  10854. }
  10855. /// Answer to mixing item selection dialog.
  10856. /// 025b <mk type>.W <name id>.W (CZ_REQ_MAKINGITEM)
  10857. /// mk type:
  10858. /// 1 = cooking
  10859. /// 2 = arrow
  10860. /// 3 = elemental
  10861. /// 4 = GN_MIX_COOKING
  10862. /// 5 = GN_MAKEBOMB
  10863. /// 6 = GN_S_PHARMACY
  10864. /// 7 = MT_M_MACHINE - Unconfirmed
  10865. /// 8 = BO_BIONIC_PHARMACY - Unconfirmed
  10866. void clif_parse_Cooking(int32 fd,map_session_data *sd) {
  10867. #if PACKETVER >= 20051010
  10868. if( sd == nullptr ){
  10869. return;
  10870. }
  10871. const PACKET_CZ_REQ_MAKINGITEM* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGITEM*>( RFIFOP( fd, 0 ) );
  10872. int32 amount = sd->menuskill_val2 ? sd->menuskill_val2 : 1;
  10873. short food_idx = -1;
  10874. if( p->type == 6 && sd->menuskill_id != GN_MIX_COOKING && sd->menuskill_id != GN_S_PHARMACY )
  10875. return;
  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. if( (food_idx = skill_can_produce_mix(sd,p->itemId,sd->menuskill_val, amount)) )
  10883. skill_produce_mix(sd,(p->type>1?sd->menuskill_id:0),p->itemId,0,0,0,amount,food_idx-1);
  10884. clif_menuskill_clear(sd);
  10885. #endif
  10886. }
  10887. /// Answer to repair weapon item selection dialog
  10888. /// 01fd <index> W (CZ_REQ_ITEMREPAIR)
  10889. /// 01fd <index>.W <name id>.W <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W ???
  10890. void clif_parse_RepairItem( int32 fd, map_session_data *sd ){
  10891. if( sd == nullptr ){
  10892. return;
  10893. }
  10894. // Hercules has wrong date -> use correct one here
  10895. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  10896. const PACKET_CZ_REQ_ITEMREPAIR2* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR2*>( RFIFOP( fd, 0 ) );
  10897. #else
  10898. const PACKET_CZ_REQ_ITEMREPAIR1* p = reinterpret_cast<PACKET_CZ_REQ_ITEMREPAIR1*>( RFIFOP( fd, 0 ) );
  10899. #endif
  10900. if (sd->menuskill_id != BS_REPAIRWEAPON)
  10901. return;
  10902. if (pc_istrading(sd)) {
  10903. //Make it fail to avoid shop exploits where you sell something different than you see.
  10904. clif_skill_fail( *sd, sd->ud.skill_id );
  10905. clif_menuskill_clear(sd);
  10906. return;
  10907. }
  10908. skill_repairweapon( *sd, p->item.index );
  10909. clif_menuskill_clear(sd);
  10910. }
  10911. /// Answer to refine weapon item selection dialog (CZ_REQ_WEAPONREFINE).
  10912. /// 0222 <index>.L
  10913. void clif_parse_WeaponRefine( int32 fd, map_session_data *sd ){
  10914. if( sd == nullptr ){
  10915. return;
  10916. }
  10917. if (sd->menuskill_id != WS_WEAPONREFINE) //Packet exploit?
  10918. return;
  10919. if (pc_istrading(sd)) {
  10920. //Make it fail to avoid shop exploits where you sell something different than you see.
  10921. clif_skill_fail( *sd, sd->ud.skill_id );
  10922. clif_menuskill_clear(sd);
  10923. return;
  10924. }
  10925. skill_weaponrefine( *sd, server_index( RFIFOL( fd, 2 ) ) );
  10926. clif_menuskill_clear(sd);
  10927. }
  10928. /// Answer to script menu dialog (CZ_CHOOSE_MENU).
  10929. /// 00b8 <npc id>.L <choice>.B
  10930. /// choice:
  10931. /// 1~254 = menu item
  10932. /// 255 = cancel
  10933. /// NOTE: If there were more than 254 items in the list, choice
  10934. /// overflows to choice%256.
  10935. void clif_parse_NpcSelectMenu(int32 fd,map_session_data *sd){
  10936. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10937. int32 npc_id = RFIFOL(fd,info->pos[0]);
  10938. uint8 select = RFIFOB(fd,info->pos[1]);
  10939. #ifdef SECURE_NPCTIMEOUT
  10940. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  10941. return;
  10942. #endif
  10943. if( (select > sd->npc_menu && select != 0xff) || select == 0 ) {
  10944. TBL_NPC* nd = map_id2nd(npc_id);
  10945. 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);
  10946. clif_GM_kick(nullptr,sd);
  10947. return;
  10948. }
  10949. sd->npc_menu = select;
  10950. if( battle_config.idletime_option&IDLE_NPC_MENU ){
  10951. sd->idletime = last_tick;
  10952. }
  10953. npc_scriptcont(sd,npc_id, false);
  10954. }
  10955. /// NPC dialog 'next' click (CZ_REQ_NEXT_SCRIPT).
  10956. /// 00b9 <npc id>.L
  10957. void clif_parse_NpcNextClicked(int32 fd,map_session_data *sd)
  10958. {
  10959. if( battle_config.idletime_option&IDLE_NPC_NEXT ){
  10960. sd->idletime = last_tick;
  10961. }
  10962. npc_scriptcont(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), false);
  10963. }
  10964. /// NPC numeric input dialog value (CZ_INPUT_EDITDLG).
  10965. /// 0143 <npc id>.L <value>.L
  10966. void clif_parse_NpcAmountInput(int32 fd,map_session_data *sd){
  10967. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10968. int32 npcid = RFIFOL(fd,info->pos[0]);
  10969. int32 amount = (int32)RFIFOL(fd,info->pos[1]);
  10970. sd->npc_amount = amount;
  10971. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  10972. sd->idletime = last_tick;
  10973. }
  10974. npc_scriptcont(sd, npcid, false);
  10975. }
  10976. /// NPC text input dialog value (CZ_INPUT_EDITDLGSTR).
  10977. /// 01d5 <packet len>.W <npc id>.L <string>.?B
  10978. void clif_parse_NpcStringInput(int32 fd, map_session_data* sd){
  10979. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  10980. int32 message_len = RFIFOW(fd,info->pos[0])-8;
  10981. int32 npcid = RFIFOL(fd,info->pos[1]);
  10982. const char* message = RFIFOCP(fd,info->pos[2]);
  10983. if( message_len <= 0 )
  10984. return; // invalid input
  10985. #if PACKETVER >= 20151001
  10986. message_len++;
  10987. #endif
  10988. safestrncpy(sd->npc_str, message, min(message_len,CHATBOX_SIZE));
  10989. if( battle_config.idletime_option&IDLE_NPC_INPUT ){
  10990. sd->idletime = last_tick;
  10991. }
  10992. npc_scriptcont(sd, npcid, false);
  10993. }
  10994. /// NPC dialog 'close' click (CZ_CLOSE_DIALOG).
  10995. /// 0146 <npc id>.L
  10996. void clif_parse_NpcCloseClicked(int32 fd,map_session_data *sd)
  10997. {
  10998. if (!sd->npc_id) //Avoid parsing anything when the script was done with. [Skotlex]
  10999. return;
  11000. if( battle_config.idletime_option&IDLE_NPC_CLOSE ){
  11001. sd->idletime = last_tick;
  11002. }
  11003. npc_scriptcont(sd, RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]), true);
  11004. }
  11005. /// Answer to identify item selection dialog (CZ_REQ_ITEMIDENTIFY).
  11006. /// 0178 <index>.W
  11007. /// index:
  11008. /// -1 = cancel
  11009. void clif_parse_ItemIdentify(int32 fd,map_session_data *sd) {
  11010. short idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2;
  11011. if (sd->menuskill_id != MC_IDENTIFY)
  11012. return;
  11013. // Ignore the request
  11014. // - Invalid item index
  11015. // - Invalid item ID or item doesn't exist
  11016. // - Item is already identified
  11017. if (idx < 0 || idx >= MAX_INVENTORY ||
  11018. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  11019. sd->inventory.u.items_inventory[idx].identify) {// cancel pressed
  11020. sd->state.workinprogress = WIP_DISABLE_NONE;
  11021. clif_menuskill_clear(sd);
  11022. return;
  11023. }
  11024. skill_identify(sd, idx);
  11025. clif_menuskill_clear(sd);
  11026. }
  11027. /// Answer to arrow crafting item selection dialog.
  11028. /// 01ae <name id>.W (CZ_REQ_MAKINGARROW)
  11029. void clif_parse_SelectArrow(int32 fd,map_session_data *sd) {
  11030. if( sd == nullptr ){
  11031. return;
  11032. }
  11033. if (pc_istrading(sd)) {
  11034. //Make it fail to avoid shop exploits where you sell something different than you see.
  11035. clif_skill_fail( *sd, sd->ud.skill_id );
  11036. clif_menuskill_clear(sd);
  11037. return;
  11038. }
  11039. const PACKET_CZ_REQ_MAKINGARROW* p = reinterpret_cast<PACKET_CZ_REQ_MAKINGARROW*>( RFIFOP( fd, 0 ) );
  11040. switch (sd->menuskill_id) {
  11041. case AC_MAKINGARROW:
  11042. skill_arrow_create(sd,p->itemId);
  11043. break;
  11044. case SA_CREATECON:
  11045. skill_produce_mix(sd,SA_CREATECON,p->itemId,0,0,0,1,-1);
  11046. break;
  11047. case GC_POISONINGWEAPON:
  11048. skill_poisoningweapon(*sd,p->itemId);
  11049. break;
  11050. case NC_MAGICDECOY:
  11051. skill_magicdecoy(*sd,p->itemId);
  11052. break;
  11053. }
  11054. clif_menuskill_clear(sd);
  11055. }
  11056. /// Answer to SA_AUTOSPELL skill selection dialog (CZ_SELECTAUTOSPELL).
  11057. /// 01ce <skill id>.L
  11058. void clif_parse_AutoSpell(int32 fd,map_session_data *sd)
  11059. {
  11060. if (sd->menuskill_id != SA_AUTOSPELL)
  11061. return;
  11062. sd->state.workinprogress = WIP_DISABLE_NONE;
  11063. skill_autospell(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11064. clif_menuskill_clear(sd);
  11065. }
  11066. /// Request to display item carding/composition list (CZ_REQ_ITEMCOMPOSITION_LIST).
  11067. /// 017a <card index>.W
  11068. void clif_parse_UseCard(int32 fd,map_session_data *sd)
  11069. {
  11070. if (sd->state.trading != 0)
  11071. return;
  11072. clif_use_card(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  11073. }
  11074. /// Answer to carding/composing item selection dialog (CZ_REQ_ITEMCOMPOSITION).
  11075. /// 017c <card index>.W <equip index>.W
  11076. void clif_parse_InsertCard(int32 fd,map_session_data *sd)
  11077. {
  11078. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11079. if (sd->state.trading != 0)
  11080. return;
  11081. pc_insert_card(sd,RFIFOW(fd,info->pos[0])-2,RFIFOW(fd,info->pos[1])-2);
  11082. }
  11083. /// Request of character's name by char ID.
  11084. /// 0193 <char id>.L (CZ_REQNAME_BYGID)
  11085. /// 0369 <char id>.L (CZ_REQNAME_BYGID2)
  11086. /// There are various variants of this packet, some of them have padding between fields.
  11087. void clif_parse_SolveCharName(int32 fd, map_session_data *sd)
  11088. {
  11089. int32 charid;
  11090. charid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11091. map_reqnickdb(sd, charid);
  11092. }
  11093. /// /resetskill /resetstate (CZ_RESET).
  11094. /// Request to reset stats or skills.
  11095. /// 0197 <type>.W
  11096. /// type:
  11097. /// 0 = state
  11098. /// 1 = skill
  11099. void clif_parse_ResetChar(int32 fd, map_session_data *sd) {
  11100. char cmd[15];
  11101. if( RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) )
  11102. safesnprintf(cmd,sizeof(cmd),"%cresetskill",atcommand_symbol);
  11103. else
  11104. safesnprintf(cmd,sizeof(cmd),"%cresetstat",atcommand_symbol);
  11105. is_atcommand(fd, sd, cmd, 1);
  11106. }
  11107. /// /lb /nlb (CZ_LOCALBROADCAST).
  11108. /// Request to broadcast a message on current map.
  11109. /// 019c <packet len>.W <text>.?B
  11110. void clif_parse_LocalBroadcast(int32 fd, map_session_data* sd)
  11111. {
  11112. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11113. char command[CHAT_SIZE_MAX+16];
  11114. uint32 len = RFIFOW(fd,info->pos[0])-4;
  11115. char* msg = RFIFOCP(fd,info->pos[1]);
  11116. // as the length varies depending on the command used, just block unreasonably long strings
  11117. mes_len_check(msg, len, CHAT_SIZE_MAX);
  11118. safesnprintf(command,sizeof(command),"%clkami %s", atcommand_symbol, msg);
  11119. is_atcommand(fd, sd, command, 1);
  11120. }
  11121. /// Request to move an item from inventory to storage.
  11122. /// 00f3 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE)
  11123. /// 0364 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_BODY_TO_STORE2)
  11124. /// There are various variants of this packet, some of them have padding between fields.
  11125. void clif_parse_MoveToKafra(int32 fd, map_session_data *sd)
  11126. {
  11127. int32 item_index, item_amount;
  11128. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11129. if (pc_istrading(sd))
  11130. return;
  11131. item_index = RFIFOW(fd,info->pos[0])-2;
  11132. item_amount = RFIFOL(fd,info->pos[1]);
  11133. if (item_index < 0 || item_index >= MAX_INVENTORY || item_amount < 1)
  11134. return;
  11135. if( sd->inventory.u.items_inventory[item_index].equipSwitch ){
  11136. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11137. return;
  11138. }
  11139. if (sd->state.storage_flag == 1)
  11140. storage_storageadd(sd, &sd->storage, item_index, item_amount);
  11141. else
  11142. if (sd->state.storage_flag == 2)
  11143. storage_guild_storageadd(sd, item_index, item_amount);
  11144. else if (sd->state.storage_flag == 3)
  11145. storage_storageadd(sd, &sd->premiumStorage, item_index, item_amount);
  11146. }
  11147. /// Request to move an item from storage to inventory.
  11148. /// 00f5 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY)
  11149. /// 0365 <index>.W <amount>.L (CZ_MOVE_ITEM_FROM_STORE_TO_BODY2)
  11150. /// There are various variants of this packet, some of them have padding between fields.
  11151. void clif_parse_MoveFromKafra(int32 fd,map_session_data *sd)
  11152. {
  11153. int32 item_index, item_amount;
  11154. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11155. item_index = RFIFOW(fd,info->pos[0])-1;
  11156. item_amount = RFIFOL(fd,info->pos[1]);
  11157. if (sd->state.storage_flag == 1)
  11158. storage_storageget(sd, &sd->storage, item_index, item_amount);
  11159. else if(sd->state.storage_flag == 2)
  11160. storage_guild_storageget(sd, item_index, item_amount);
  11161. else if(sd->state.storage_flag == 3)
  11162. storage_storageget(sd, &sd->premiumStorage, item_index, item_amount);
  11163. }
  11164. /// Request to move an item from cart to storage (CZ_MOVE_ITEM_FROM_CART_TO_STORE).
  11165. /// 0129 <index>.W <amount>.L
  11166. void clif_parse_MoveToKafraFromCart(int32 fd, map_session_data *sd){
  11167. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11168. int32 idx = RFIFOW(fd,info->pos[0]) - 2;
  11169. int32 amount = RFIFOL(fd,info->pos[1]);
  11170. if( sd->state.vending )
  11171. return;
  11172. if (!pc_iscarton(sd))
  11173. return;
  11174. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11175. return;
  11176. if (idx < 0 || idx >= MAX_INVENTORY || amount < 1)
  11177. return;
  11178. if( sd->inventory.u.items_inventory[idx].equipSwitch ){
  11179. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  11180. return;
  11181. }
  11182. if (sd->state.storage_flag == 1)
  11183. storage_storageaddfromcart(sd, &sd->storage, idx, amount);
  11184. else if (sd->state.storage_flag == 2)
  11185. storage_guild_storageaddfromcart(sd, idx, amount);
  11186. else if (sd->state.storage_flag == 3)
  11187. storage_storageaddfromcart(sd, &sd->premiumStorage, idx, amount);
  11188. }
  11189. /// Request to move an item from storage to cart (CZ_MOVE_ITEM_FROM_STORE_TO_CART).
  11190. /// 0128 <index>.W <amount>.L
  11191. void clif_parse_MoveFromKafraToCart(int32 fd, map_session_data *sd){
  11192. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11193. int32 idx = RFIFOW(fd,info->pos[0]) - 1;
  11194. int32 amount = RFIFOL(fd,info->pos[1]);
  11195. if( sd->state.vending )
  11196. return;
  11197. if (!pc_iscarton(sd))
  11198. return;
  11199. if (map_getmapflag(sd->bl.m, MF_NOUSECART))
  11200. return;
  11201. if (sd->state.storage_flag == 1)
  11202. storage_storagegettocart(sd, &sd->storage, idx, amount);
  11203. else
  11204. if (sd->state.storage_flag == 2)
  11205. storage_guild_storagegettocart(sd, idx, amount);
  11206. else if (sd->state.storage_flag == 3)
  11207. storage_storagegettocart(sd, &sd->premiumStorage, idx, amount);
  11208. }
  11209. /// Request to close storage (CZ_CLOSE_STORE).
  11210. /// 00f7
  11211. void clif_parse_CloseKafra(int32 fd, map_session_data *sd)
  11212. {
  11213. if( sd->state.storage_flag == 1 )
  11214. storage_storageclose(sd);
  11215. else
  11216. if( sd->state.storage_flag == 2 )
  11217. storage_guild_storageclose(sd);
  11218. else if( sd->state.storage_flag == 3 )
  11219. storage_premiumStorage_close(sd);
  11220. }
  11221. /// Displays kafra storage password dialog (ZC_REQ_STORE_PASSWORD).
  11222. /// 023a <info>.W
  11223. /// info:
  11224. /// 0 = password has not been set yet
  11225. /// 1 = storage is password-protected
  11226. /// 8 = too many wrong passwords
  11227. /// ? = ignored
  11228. /// NOTE: This packet is only available on certain non-kRO clients.
  11229. void clif_storagepassword(map_session_data* sd, short info)
  11230. {
  11231. int32 fd = sd->fd;
  11232. WFIFOHEAD(fd,packet_len(0x23a));
  11233. WFIFOW(fd,0) = 0x23a;
  11234. WFIFOW(fd,2) = info;
  11235. WFIFOSET(fd,packet_len(0x23a));
  11236. }
  11237. /// Answer to the kafra storage password dialog (CZ_ACK_STORE_PASSWORD).
  11238. /// 023b <type>.W <password>.16B <new password>.16B
  11239. /// type:
  11240. /// 2 = change password
  11241. /// 3 = check password
  11242. /// NOTE: This packet is only available on certain non-kRO clients.
  11243. void clif_parse_StoragePassword(int32 fd, map_session_data *sd){ //@TODO
  11244. // struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11245. // int32 type = RFIFOW(fd,info->pos[0]);
  11246. // char* password = RFIFOP(fd,info->pos[1]);
  11247. // char* new_password = RFIFOP(fd,info->pos[2]);
  11248. // storage_changepass(sd,password,new_password);
  11249. }
  11250. /// Result of kafra storage password validation (ZC_RESULT_STORE_PASSWORD).
  11251. /// 023c <result>.W <error count>.W
  11252. /// result:
  11253. /// 4 = password change success
  11254. /// 5 = password change failure
  11255. /// 6 = password check success
  11256. /// 7 = password check failure
  11257. /// 8 = too many wrong passwords
  11258. /// ? = ignored
  11259. /// NOTE: This packet is only available on certain non-kRO clients.
  11260. void clif_storagepassword_result(map_session_data* sd, short result, short error_count)
  11261. {
  11262. int32 fd = sd->fd;
  11263. WFIFOHEAD(fd,packet_len(0x23c));
  11264. WFIFOW(fd,0) = 0x23c;
  11265. WFIFOW(fd,2) = result;
  11266. WFIFOW(fd,4) = error_count;
  11267. WFIFOSET(fd,packet_len(0x23c));
  11268. }
  11269. /// Party creation request
  11270. /// 00f9 <party name>.24B (CZ_MAKE_GROUP)
  11271. void clif_parse_CreateParty(int32 fd, map_session_data *sd){
  11272. if( sd == nullptr ){
  11273. return;
  11274. }
  11275. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11276. name[NAME_LENGTH-1] = '\0';
  11277. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11278. clif_displaymessage(fd, msg_txt(sd,227));
  11279. return;
  11280. }
  11281. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11282. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11283. return;
  11284. }
  11285. party_create( *sd, name, 0, 0 );
  11286. }
  11287. /// 01e8 <party name>.24B <item pickup rule>.B <item share rule>.B (CZ_MAKE_GROUP2)
  11288. void clif_parse_CreateParty2(int32 fd, map_session_data *sd){
  11289. if( sd == nullptr ){
  11290. return;
  11291. }
  11292. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11293. char* name = RFIFOCP(fd,info->pos[0]);
  11294. int32 item1 = RFIFOB(fd,info->pos[1]);
  11295. int32 item2 = RFIFOB(fd,info->pos[2]);
  11296. name[NAME_LENGTH-1] = '\0';
  11297. if( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {// Party locked.
  11298. clif_displaymessage(fd, msg_txt(sd,227));
  11299. return;
  11300. }
  11301. if( battle_config.basic_skill_check && pc_checkskill(sd,NV_BASIC) < 7 && pc_checkskill(sd, SU_BASIC_SKILL) < 1 ) {
  11302. clif_skill_fail( *sd, 1, USESKILL_FAIL_LEVEL, 4 );
  11303. return;
  11304. }
  11305. party_create( *sd, name, item1, item2 );
  11306. }
  11307. /// Party invitation request by account id
  11308. /// 00fc <account id>.L (CZ_REQ_JOIN_GROUP)
  11309. void clif_parse_PartyInvite( int32 fd, map_session_data *sd ){
  11310. if( sd == nullptr ){
  11311. return;
  11312. }
  11313. const PACKET_CZ_REQ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11314. party_invite( *sd, map_id2sd( p->AID ) );
  11315. }
  11316. /// Party invitation request by name
  11317. /// 02c4 <char name>.24B (CZ_PARTY_JOIN_REQ)
  11318. void clif_parse_PartyInvite2( int32 fd, map_session_data *sd ){
  11319. #if PACKETVER >= 20070227
  11320. if( sd == nullptr ){
  11321. return;
  11322. }
  11323. const PACKET_CZ_PARTY_JOIN_REQ* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ*>( RFIFOP( fd, 0 ) );
  11324. char name[NAME_LENGTH];
  11325. safestrncpy( name, p->name, sizeof( name ) );
  11326. party_invite( *sd, map_nick2sd( name, false ) );
  11327. #endif
  11328. }
  11329. /// Party invitation reply
  11330. /// 00ff <party id>.L <flag>.L (CZ_JOIN_GROUP)
  11331. /// flag:
  11332. /// 0 = reject
  11333. /// 1 = accept
  11334. void clif_parse_ReplyPartyInvite( int32 fd, map_session_data *sd ){
  11335. if( sd == nullptr ){
  11336. return;
  11337. }
  11338. const PACKET_CZ_JOIN_GROUP* p = reinterpret_cast<PACKET_CZ_JOIN_GROUP*>( RFIFOP( fd, 0 ) );
  11339. party_reply_invite( *sd, p->party_id, p->flag );
  11340. }
  11341. /// Party invitation reply
  11342. /// 02c7 <party id>.L <flag>.B (CZ_PARTY_JOIN_REQ_ACK)
  11343. /// flag:
  11344. /// 0 = reject
  11345. /// 1 = accept
  11346. void clif_parse_ReplyPartyInvite2( int32 fd, map_session_data *sd ){
  11347. #if PACKETVER >= 20070227
  11348. if( sd == nullptr ){
  11349. return;
  11350. }
  11351. const PACKET_CZ_PARTY_JOIN_REQ_ACK* p = reinterpret_cast<PACKET_CZ_PARTY_JOIN_REQ_ACK*>( RFIFOP( fd, 0 ) );
  11352. party_reply_invite( *sd, p->party_id, p->flag );
  11353. #endif
  11354. }
  11355. /// Request to leave party.
  11356. /// 0100 (CZ_REQ_LEAVE_GROUP)
  11357. void clif_parse_LeaveParty( int32 fd, map_session_data *sd ){
  11358. if( sd == nullptr ){
  11359. return;
  11360. }
  11361. //const PACKET_CZ_REQ_LEAVE_GROUP* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GROUP*>( RFIFOP( fd, 0 ) );
  11362. party_leave( *sd, true );
  11363. }
  11364. /// Request to expel a party member.
  11365. /// 0103 <account id>.L <char name>.24B (CZ_REQ_EXPEL_GROUP_MEMBER)
  11366. void clif_parse_RemovePartyMember( int32 fd, map_session_data* sd ){
  11367. if( sd == nullptr ){
  11368. return;
  11369. }
  11370. const PACKET_CZ_REQ_EXPEL_GROUP_MEMBER* p = reinterpret_cast<PACKET_CZ_REQ_EXPEL_GROUP_MEMBER*>( RFIFOP( fd, 0 ) );
  11371. party_removemember( *sd, p->AID, p->name );
  11372. }
  11373. /// Request to change party options.
  11374. /// 0102 <exp share rule>.L (CZ_CHANGE_GROUPEXPOPTION)
  11375. /// 07d7 <exp share rule>.L <item pickup rule>.B <item share rule>.B (CZ_GROUPINFO_CHANGE_V2)
  11376. void clif_parse_PartyChangeOption(int32 fd, map_session_data *sd)
  11377. {
  11378. struct party_data *p;
  11379. int32 i,expflag;
  11380. int32 cmd = RFIFOW(fd,0);
  11381. struct s_packet_db* info = &packet_db[cmd];
  11382. if( !sd->status.party_id )
  11383. return;
  11384. p = party_search(sd->status.party_id);
  11385. if( p == nullptr )
  11386. return;
  11387. ARR_FIND( 0, MAX_PARTY, i, p->data[i].sd == sd );
  11388. if( i == MAX_PARTY )
  11389. return; //Shouldn't happen
  11390. if( !p->party.member[i].leader )
  11391. return;
  11392. expflag = RFIFOL(fd,info->pos[0]);
  11393. if(cmd == 0x0102){ //Client can't change the item-field
  11394. party_changeoption(sd, expflag, p->party.item);
  11395. }
  11396. else {
  11397. int32 itemflag = (RFIFOB(fd,info->pos[1])?1:0)|(RFIFOB(fd,info->pos[2])?2:0);
  11398. party_changeoption(sd, expflag, itemflag);
  11399. }
  11400. }
  11401. /// Validates and processes party messages (CZ_REQUEST_CHAT_PARTY).
  11402. /// 0108 <packet len>.W <text>.?B (<name> : <message>) 00
  11403. void clif_parse_PartyMessage(int32 fd, map_session_data* sd){
  11404. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11405. // validate packet and retrieve name and message
  11406. if( !clif_process_message( sd, false, name, message, output ) )
  11407. return;
  11408. party_send_message(sd, output, strlen(output) + 1 );
  11409. }
  11410. /// Changes Party Leader (CZ_CHANGE_GROUP_MASTER).
  11411. /// 07da <account id>.L
  11412. void clif_parse_PartyChangeLeader(int32 fd, map_session_data* sd){
  11413. party_changeleader(sd, map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0])),nullptr);
  11414. }
  11415. /// Party Booking in KRO [Spiria]
  11416. ///
  11417. /// Request to register a party booking advertisment (CZ_PARTY_BOOKING_REQ_REGISTER).
  11418. /// 0802 <level>.W <map id>.W { <job>.W }*6
  11419. void clif_parse_PartyBookingRegisterReq(int32 fd, map_session_data* sd){
  11420. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11421. short level = RFIFOW(fd,info->pos[0]);
  11422. short mapid = RFIFOW(fd,info->pos[1]);
  11423. int32 idxpbj = info->pos[2];
  11424. short job[MAX_PARTY_BOOKING_JOBS];
  11425. int32 i;
  11426. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11427. job[i] = RFIFOB(fd,idxpbj+i*2);
  11428. party_booking_register(sd, level, mapid, job);
  11429. }
  11430. /// Result of request to register a party booking advertisment (ZC_PARTY_BOOKING_ACK_REGISTER).
  11431. /// 0803 <result>.W
  11432. /// result:
  11433. /// 0 = success
  11434. /// 1 = failure
  11435. /// 2 = already registered
  11436. void clif_PartyBookingRegisterAck(map_session_data *sd, int32 flag)
  11437. {
  11438. int32 fd = sd->fd;
  11439. WFIFOHEAD(fd,packet_len(0x803));
  11440. WFIFOW(fd,0) = 0x803;
  11441. WFIFOW(fd,2) = flag;
  11442. WFIFOSET(fd,packet_len(0x803));
  11443. }
  11444. /// Request to search for party booking advertisments (CZ_PARTY_BOOKING_REQ_SEARCH).
  11445. /// 0804 <level>.W <map id>.W <job>.W <last index>.L <result count>.W
  11446. void clif_parse_PartyBookingSearchReq(int32 fd, map_session_data* sd){
  11447. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11448. short level = RFIFOW(fd,info->pos[0]);
  11449. short mapid = RFIFOW(fd,info->pos[1]);
  11450. short job = RFIFOW(fd,info->pos[2]);
  11451. unsigned long lastindex = RFIFOL(fd,info->pos[3]);
  11452. short resultcount = RFIFOW(fd,info->pos[4]);
  11453. party_booking_search(sd, level, mapid, job, lastindex, resultcount);
  11454. }
  11455. /// Party booking search results (ZC_PARTY_BOOKING_ACK_SEARCH).
  11456. /// 0805 <packet len>.W <more results>.B { <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6 }*
  11457. /// more results:
  11458. /// 0 = no
  11459. /// 1 = yes
  11460. void clif_PartyBookingSearchAck(int32 fd, struct party_booking_ad_info** results, int32 count, bool more_result)
  11461. {
  11462. int32 i, j;
  11463. int32 size = sizeof(struct party_booking_ad_info); // structure size (48)
  11464. struct party_booking_ad_info *pb_ad;
  11465. WFIFOHEAD(fd,size*count + 5);
  11466. WFIFOW(fd,0) = 0x805;
  11467. WFIFOW(fd,2) = size*count + 5;
  11468. WFIFOB(fd,4) = more_result;
  11469. for(i=0; i<count; i++)
  11470. {
  11471. pb_ad = results[i];
  11472. WFIFOL(fd,i*size+5) = pb_ad->index;
  11473. safestrncpy(WFIFOCP(fd,i*size+9),pb_ad->charname,NAME_LENGTH);
  11474. WFIFOL(fd,i*size+33) = pb_ad->starttime; // FIXME: This is expire time
  11475. WFIFOW(fd,i*size+37) = pb_ad->p_detail.level;
  11476. WFIFOW(fd,i*size+39) = pb_ad->p_detail.mapid;
  11477. for(j=0; j<MAX_PARTY_BOOKING_JOBS; j++)
  11478. WFIFOW(fd,i*size+41+j*2) = pb_ad->p_detail.job[j];
  11479. }
  11480. WFIFOSET(fd,WFIFOW(fd,2));
  11481. }
  11482. /// Request to delete own party booking advertisment (CZ_PARTY_BOOKING_REQ_DELETE).
  11483. /// 0806
  11484. void clif_parse_PartyBookingDeleteReq(int32 fd, map_session_data* sd)
  11485. {
  11486. if(party_booking_delete(sd))
  11487. clif_PartyBookingDeleteAck(sd, 0);
  11488. }
  11489. /// Result of request to delete own party booking advertisment (ZC_PARTY_BOOKING_ACK_DELETE).
  11490. /// 0807 <result>.W
  11491. /// result:
  11492. /// 0 = success
  11493. /// 1 = success (auto-removed expired ad)
  11494. /// 2 = failure
  11495. /// 3 = nothing registered
  11496. void clif_PartyBookingDeleteAck(map_session_data* sd, int32 flag)
  11497. {
  11498. int32 fd = sd->fd;
  11499. WFIFOHEAD(fd,packet_len(0x807));
  11500. WFIFOW(fd,0) = 0x807;
  11501. WFIFOW(fd,2) = flag;
  11502. WFIFOSET(fd,packet_len(0x807));
  11503. }
  11504. /// Request to update party booking advertisment (CZ_PARTY_BOOKING_REQ_UPDATE).
  11505. /// 0808 { <job>.W }*6
  11506. void clif_parse_PartyBookingUpdateReq(int32 fd, map_session_data* sd)
  11507. {
  11508. short job[MAX_PARTY_BOOKING_JOBS];
  11509. int32 i;
  11510. int32 idxpbu = packet_db[RFIFOW(fd,0)].pos[0];
  11511. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11512. job[i] = RFIFOW(fd,idxpbu+i*2);
  11513. party_booking_update(sd, job);
  11514. }
  11515. /// Notification about new party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_INSERT).
  11516. /// 0809 <index>.L <char name>.24B <expire time>.L <level>.W <map id>.W { <job>.W }*6
  11517. void clif_PartyBookingInsertNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11518. {
  11519. int32 i;
  11520. uint8 buf[38+MAX_PARTY_BOOKING_JOBS*2];
  11521. if(pb_ad == nullptr) return;
  11522. WBUFW(buf,0) = 0x809;
  11523. WBUFL(buf,2) = pb_ad->index;
  11524. safestrncpy(WBUFCP(buf,6),pb_ad->charname,NAME_LENGTH);
  11525. WBUFL(buf,30) = pb_ad->starttime; // FIXME: This is expire time
  11526. WBUFW(buf,34) = pb_ad->p_detail.level;
  11527. WBUFW(buf,36) = pb_ad->p_detail.mapid;
  11528. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11529. WBUFW(buf,38+i*2) = pb_ad->p_detail.job[i];
  11530. clif_send(buf, packet_len(0x809), &sd->bl, ALL_CLIENT);
  11531. }
  11532. /// Notification about updated party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_UPDATE).
  11533. /// 080a <index>.L { <job>.W }*6
  11534. void clif_PartyBookingUpdateNotify(map_session_data* sd, struct party_booking_ad_info* pb_ad)
  11535. {
  11536. int32 i;
  11537. uint8 buf[6+MAX_PARTY_BOOKING_JOBS*2];
  11538. if(pb_ad == nullptr) return;
  11539. WBUFW(buf,0) = 0x80a;
  11540. WBUFL(buf,2) = pb_ad->index;
  11541. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  11542. WBUFW(buf,6+i*2) = pb_ad->p_detail.job[i];
  11543. clif_send(buf,packet_len(0x80a),&sd->bl,ALL_CLIENT); // Now UPDATE all client.
  11544. }
  11545. /// Notification about deleted party booking advertisment (ZC_PARTY_BOOKING_NOTIFY_DELETE).
  11546. /// 080b <index>.L
  11547. void clif_PartyBookingDeleteNotify(map_session_data* sd, int32 index)
  11548. {
  11549. uint8 buf[6];
  11550. WBUFW(buf,0) = 0x80b;
  11551. WBUFL(buf,2) = index;
  11552. clif_send(buf, packet_len(0x80b), &sd->bl, ALL_CLIENT); // Now UPDATE all client.
  11553. }
  11554. /// Request to close own vending (CZ_REQ_CLOSESTORE).
  11555. /// 012e
  11556. void clif_parse_CloseVending(int32 fd, map_session_data* sd)
  11557. {
  11558. vending_closevending(sd);
  11559. }
  11560. /// Request to open a vending shop (CZ_REQ_BUY_FROMMC).
  11561. /// 0130 <account id>.L
  11562. void clif_parse_VendingListReq(int32 fd, map_session_data* sd)
  11563. {
  11564. if( sd->npc_id ) {// using an NPC
  11565. return;
  11566. }
  11567. vending_vendinglistreq(sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11568. }
  11569. /// Shop item(s) purchase request.
  11570. /// 0134 <packet len>.W <account id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC)
  11571. void clif_parse_PurchaseReq(int32 fd, map_session_data* sd){
  11572. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC*>( RFIFOP( fd, 0 ) );
  11573. vending_purchasereq( sd, p->AID, sd->vended_id, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11574. // whether it fails or not, the buy window is closed
  11575. sd->vended_id = 0;
  11576. }
  11577. /// Shop item(s) purchase request.
  11578. /// 0801 <packet len>.W <account id>.L <unique id>.L { <amount>.W <index>.W }* (CZ_PC_PURCHASE_ITEMLIST_FROMMC2)
  11579. void clif_parse_PurchaseReq2(int32 fd, map_session_data* sd){
  11580. #if PACKETVER >= 20100105
  11581. const PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2* p = reinterpret_cast<PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2*>( RFIFOP( fd, 0 ) );
  11582. vending_purchasereq( sd, p->AID, p->UniqueID, (uint8*)p->list, ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] ) );
  11583. // whether it fails or not, the buy window is closed
  11584. sd->vended_id = 0;
  11585. #endif
  11586. }
  11587. /// Confirm or cancel the shop preparation window.
  11588. /// 012f <packet len>.W <shop name>.80B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE)
  11589. /// 01b2 <packet len>.W <shop name>.80B <result>.B { <index>.W <amount>.W <price>.L }* (CZ_REQ_OPENSTORE2)
  11590. /// result:
  11591. /// 0 = canceled
  11592. /// 1 = open
  11593. void clif_parse_OpenVending(int32 fd, map_session_data* sd){
  11594. if( sd == nullptr ){
  11595. return;
  11596. }
  11597. int32 cmd = RFIFOW(fd,0);
  11598. struct s_packet_db* info = &packet_db[cmd];
  11599. short len = (short)RFIFOW(fd,info->pos[0]);
  11600. const char* message = RFIFOCP(fd,info->pos[1]);
  11601. const uint8* data = (uint8*)RFIFOP(fd,info->pos[3]);
  11602. if(cmd == 0x12f){ // (CZ_REQ_OPENSTORE)
  11603. len -= 84;
  11604. }
  11605. else { //(CZ_REQ_OPENSTORE2)
  11606. bool flag;
  11607. len -= 85;
  11608. flag = RFIFOB(fd,info->pos[2]) != 0;
  11609. if (!flag) {
  11610. sd->state.prevend = 0;
  11611. sd->state.workinprogress = WIP_DISABLE_NONE;
  11612. }
  11613. }
  11614. if( sd->sc.getSCE(SC_NOCHAT) && sd->sc.getSCE(SC_NOCHAT)->val1&MANNER_NOROOM )
  11615. return;
  11616. if( map_getmapflag(sd->bl.m, MF_NOVENDING) ) {
  11617. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  11618. return;
  11619. }
  11620. if( map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNOVENDING) ) {
  11621. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  11622. return;
  11623. }
  11624. if( message[0] == '\0' ) // invalid input
  11625. return;
  11626. vending_openvending(*sd, message, data, len/8, nullptr);
  11627. }
  11628. /// Guild creation request (CZ_REQ_MAKE_GUILD).
  11629. /// 0165 <char id>.L <guild name>.24B
  11630. void clif_parse_CreateGuild(int32 fd,map_session_data *sd){
  11631. //int32 charid = RFIFOL(fd,packet_db[cmd].pos[0]);
  11632. char* name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11633. name[NAME_LENGTH-1] = '\0';
  11634. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11635. clif_displaymessage(fd, msg_txt(sd,228));
  11636. return;
  11637. }
  11638. if(sd->clan){
  11639. // Should display a clientside message "You are currently joined in Clan !!" so we ignore it
  11640. return;
  11641. }
  11642. guild_create( *sd, name );
  11643. }
  11644. /// Request for guild window interface permissions
  11645. /// 014d (CZ_REQ_GUILD_MENUINTERFACE)
  11646. static void clif_parse_GuildCheckMaster(int32 fd, map_session_data *sd){
  11647. if(sd == nullptr)
  11648. return;
  11649. clif_guild_masterormember(*sd);
  11650. }
  11651. /// Request for guild window information (CZ_REQ_GUILD_MENU).
  11652. /// 014f <type>.L
  11653. /// type:
  11654. /// 0 = basic info
  11655. /// 1 = member manager
  11656. /// 2 = positions
  11657. /// 3 = skills
  11658. /// 4 = expulsion list
  11659. /// 5 = unknown (GM_ALLGUILDLIST)
  11660. /// 6 = notice
  11661. void clif_parse_GuildRequestInfo(int32 fd, map_session_data *sd)
  11662. {
  11663. int32 type = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11664. if( !sd->status.guild_id && !sd->bg_id )
  11665. return;
  11666. switch( type )
  11667. {
  11668. case 0: // Basic Information Guild, hostile alliance information
  11669. clif_guild_basicinfo( *sd );
  11670. clif_guild_allianceinfo(*sd);
  11671. clif_guild_castle_list(*sd);
  11672. break;
  11673. case 1: // Members list, list job title
  11674. clif_guild_positionnamelist(sd);
  11675. clif_guild_memberlist( *sd );
  11676. break;
  11677. case 2: // List job title, title information list
  11678. clif_guild_positionnamelist(sd);
  11679. clif_guild_positioninfolist(*sd);
  11680. break;
  11681. case 3: // Skill list
  11682. clif_guild_skillinfo( *sd );
  11683. break;
  11684. case 4: // Expulsion list
  11685. clif_guild_expulsionlist(*sd);
  11686. break;
  11687. default:
  11688. ShowError("clif: guild request info: unknown type %d\n", type);
  11689. break;
  11690. }
  11691. }
  11692. /// Request to update guild positions (CZ_REG_CHANGE_GUILD_POSITIONINFO).
  11693. /// 0161 <packet len>.W { <position id>.L <mode>.L <ranking>.L <pay rate>.L <name>.24B }*
  11694. void clif_parse_GuildChangePositionInfo(int32 fd, map_session_data *sd)
  11695. {
  11696. int32 i;
  11697. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11698. int32 len = RFIFOW(fd,info->pos[0]);
  11699. int32 idxgpos = info->pos[1];
  11700. if(!sd->state.gmaster_flag)
  11701. return;
  11702. for(i = idxgpos; i < len; i += 40 ){
  11703. guild_change_position(sd->status.guild_id, RFIFOL(fd,i), RFIFOL(fd,i+4), RFIFOL(fd,i+12), RFIFOCP(fd,i+16));
  11704. }
  11705. }
  11706. /// Request to update the position of guild members.
  11707. /// 0155 <packet len>.W { <account id>.L <char id>.L <position id>.L }* (CZ_REQ_CHANGE_MEMBERPOS)
  11708. void clif_parse_GuildChangeMemberPosition( int32 fd, map_session_data *sd ){
  11709. if(!sd->state.gmaster_flag)
  11710. return;
  11711. const PACKET_CZ_REQ_CHANGE_MEMBERPOS* p = reinterpret_cast<PACKET_CZ_REQ_CHANGE_MEMBERPOS*>( RFIFOP( fd, 0 ) );
  11712. int16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  11713. for( int16 i = 0; i < entries; i++ ){
  11714. const PACKET_CZ_REQ_CHANGE_MEMBERPOS_sub& entry = p->list[i];
  11715. // Guild leadership change
  11716. if( entry.position == 0 ){
  11717. if( !battle_config.guild_leaderchange_woe && is_agit_start() ){
  11718. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_IN_SIEGE_TIME );
  11719. return;
  11720. }
  11721. if( battle_config.guild_leaderchange_delay && DIFF_TICK( time( nullptr ),sd->guild->guild.last_leader_change ) < battle_config.guild_leaderchange_delay ){
  11722. clif_msg( sd, MSI_IMPOSSIBLE_CHANGE_GUILD_MASTER_NOT_TIME );
  11723. return;
  11724. }
  11725. guild_gm_change( sd->status.guild_id, entry.CID );
  11726. // No further entries will be processed - the requesting player lost his guild master status
  11727. return;
  11728. }else if( entry.position > 0 ){
  11729. guild_change_memberposition( sd->status.guild_id, entry.AID, entry.CID, entry.position );
  11730. }
  11731. }
  11732. }
  11733. /// Request for guild emblem data (CZ_REQ_GUILD_EMBLEM_IMG).
  11734. /// 0151 <guild id>.L
  11735. void clif_parse_GuildRequestEmblem(int32 fd,map_session_data *sd)
  11736. {
  11737. int32 guild_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  11738. auto g = guild_search(guild_id);
  11739. if (g)
  11740. clif_guild_emblem(*sd, g->guild);
  11741. }
  11742. /// Validates data of a guild emblem (compressed bitmap)
  11743. enum e_result_validate_emblem { // Used as Result for clif_validate_emblem
  11744. EMBVALIDATE_SUCCESS = 0,
  11745. EMBVALIDATE_ERR_RAW_FILEFORMAT, // Invalid File Format (Error in zlib/decompression or malformed BMP header)
  11746. EMBVALIDATE_ERR_TRANSPARENCY // uploaded emblem does not met the requirements of inter_config.emblem_transparency_limit
  11747. };
  11748. static enum e_result_validate_emblem clif_validate_emblem(const uint8* emblem, unsigned long emblem_len)
  11749. {
  11750. uint8 buf[1800]; // no well-formed emblem bitmap is larger than 1782 (24 bit) / 1654 (8 bit) bytes
  11751. unsigned long buf_len = sizeof(buf);
  11752. int32 offset = 0;
  11753. if(!(( decode_zip(buf, &buf_len, emblem, emblem_len) == 0 && buf_len >= 18 ) // sizeof(BITMAPFILEHEADER) + sizeof(biSize) of the following info header struct
  11754. && RBUFW(buf,0) == 0x4d42 // BITMAPFILEHEADER.bfType (signature)
  11755. && RBUFL(buf,2) == buf_len // BITMAPFILEHEADER.bfSize (file size)
  11756. && (offset = RBUFL(buf,10)) < buf_len // BITMAPFILEHEADER.bfOffBits (offset to bitmap bits)
  11757. ))
  11758. return EMBVALIDATE_ERR_RAW_FILEFORMAT;
  11759. if(inter_config.emblem_transparency_limit != 100) {
  11760. int32 i, transcount = 1, tmp[3];
  11761. for(i = offset; i < buf_len-1; i++) {
  11762. int32 j = i%3;
  11763. tmp[j] = RBUFL(buf,i);
  11764. if(j == 2 && (tmp[0] == 0xFFFF00FF) && (tmp[1] == 0xFFFF00) && (tmp[2] == 0xFF00FFFF)) //if pixel is transparent
  11765. transcount++;
  11766. }
  11767. if(((transcount*300)/(buf_len-offset)) > inter_config.emblem_transparency_limit) //convert in % to chk
  11768. return EMBVALIDATE_ERR_TRANSPARENCY;
  11769. }
  11770. return EMBVALIDATE_SUCCESS;
  11771. }
  11772. /// Request to update the guild emblem (CZ_REGISTER_GUILD_EMBLEM_IMG).
  11773. /// 0153 <packet len>.W <emblem data>.?B
  11774. void clif_parse_GuildChangeEmblem(int32 fd,map_session_data *sd){
  11775. if( sd == nullptr ){
  11776. return;
  11777. }
  11778. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11779. unsigned long emblem_len = RFIFOW(fd,info->pos[0])-4;
  11780. const uint8* emblem = RFIFOP(fd,info->pos[1]);
  11781. int32 emb_val=0;
  11782. if( !emblem_len || !sd->state.gmaster_flag )
  11783. return;
  11784. if(!(inter_config.emblem_woe_change) && is_agit_start() ){
  11785. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,385),false,SELF); //"You not allowed to change emblem during woe"
  11786. return;
  11787. }
  11788. emb_val = clif_validate_emblem(emblem, emblem_len);
  11789. if(emb_val == EMBVALIDATE_ERR_RAW_FILEFORMAT){
  11790. 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);
  11791. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,386),false,SELF); //"The chosen emblem was detected invalid\n"
  11792. return;
  11793. } else if(emb_val == EMBVALIDATE_ERR_TRANSPARENCY){
  11794. char output[128];
  11795. safesnprintf(output,sizeof(output),msg_txt(sd,387),inter_config.emblem_transparency_limit);
  11796. 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"
  11797. return;
  11798. }
  11799. guild_change_emblem( *sd, emblem_len, (const char*)emblem );
  11800. }
  11801. /// Request to update the guild emblem id
  11802. /// 0b46 <guild id>.L <version>.L
  11803. void clif_parse_GuildChangeEmblem2(int32 fd, map_session_data* sd) {
  11804. if( sd == nullptr ){
  11805. return;
  11806. }
  11807. #if PACKETVER >= 20190724
  11808. const PACKET_CZ_REQ_ADD_NEW_EMBLEM* p = reinterpret_cast<PACKET_CZ_REQ_ADD_NEW_EMBLEM*>( RFIFOP( fd, 0 ) );
  11809. auto &g = sd->guild;
  11810. if (!g || g->guild.guild_id != p->guild_id)
  11811. return;
  11812. if (!sd->state.gmaster_flag)
  11813. return;
  11814. if (!inter_config.emblem_woe_change && is_agit_start()) {
  11815. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 385), false, SELF); //"You not allowed to change emblem during woe"
  11816. return;
  11817. }
  11818. guild_change_emblem_version( *sd, p->version );
  11819. #endif
  11820. }
  11821. /// Guild notice update request (CZ_GUILD_NOTICE).
  11822. /// 016e <guild id>.L <msg1>.60B <msg2>.120B
  11823. void clif_parse_GuildChangeNotice(int32 fd, map_session_data* sd){
  11824. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11825. int32 guild_id = RFIFOL(fd,info->pos[0]);
  11826. char* msg1 = RFIFOCP(fd,info->pos[1]);
  11827. char* msg2 = RFIFOCP(fd,info->pos[2]);
  11828. if(!sd->state.gmaster_flag)
  11829. return;
  11830. // compensate for some client defects when using multilanguage mode
  11831. if (msg1[0] == '|' && msg1[3] == '|') msg1+= 3; // skip duplicate marker
  11832. if (msg2[0] == '|' && msg2[3] == '|') msg2+= 3; // skip duplicate marker
  11833. if (msg2[0] == '|') msg2[strnlen(msg2, MAX_GUILDMES2)-1] = '\0'; // delete extra space at the end of string
  11834. guild_change_notice(sd, guild_id, msg1, msg2);
  11835. }
  11836. /// Guild invite request.
  11837. /// 0168 <account id>.L <inviter account id>.L <inviter char id>.L (CZ_REQ_JOIN_GUILD)
  11838. void clif_parse_GuildInvite( int32 fd,map_session_data *sd ){
  11839. if( sd == nullptr ){
  11840. return;
  11841. }
  11842. const PACKET_CZ_REQ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11843. guild_invite( *sd, map_id2sd( p->AID ) );
  11844. }
  11845. /// Guild invite request (/guildinvite)
  11846. /// 0916 <char name>.24B (CZ_REQ_JOIN_GUILD2)
  11847. void clif_parse_GuildInvite2( int32 fd, map_session_data *sd ){
  11848. #if PACKETVER >= 20120410
  11849. if( sd == nullptr ){
  11850. return;
  11851. }
  11852. const PACKET_CZ_REQ_JOIN_GUILD2* p = reinterpret_cast<PACKET_CZ_REQ_JOIN_GUILD2*>( RFIFOP( fd, 0 ) );
  11853. char nick[NAME_LENGTH] = {0};
  11854. safestrncpy( nick, p->name, NAME_LENGTH );
  11855. guild_invite( *sd, map_nick2sd( nick, false ) );
  11856. #endif
  11857. }
  11858. /// Answer to guild invitation.
  11859. /// 016b <guild id>.L <answer>.L (CZ_JOIN_GUILD)
  11860. /// answer:
  11861. /// 0 = refuse
  11862. /// 1 = accept
  11863. void clif_parse_GuildReplyInvite( int32 fd, map_session_data *sd ){
  11864. if( sd == nullptr ){
  11865. return;
  11866. }
  11867. const PACKET_CZ_JOIN_GUILD* p = reinterpret_cast<PACKET_CZ_JOIN_GUILD*>( RFIFOP( fd, 0 ) );
  11868. guild_reply_invite( *sd, p->guild_id, p->answer );
  11869. }
  11870. /// Request to leave guild.
  11871. /// 0159 <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_LEAVE_GUILD)
  11872. void clif_parse_GuildLeave(int32 fd,map_session_data *sd){
  11873. if( sd == nullptr ){
  11874. return;
  11875. }
  11876. const PACKET_CZ_REQ_LEAVE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_LEAVE_GUILD*>( RFIFOP( fd, 0 ) );
  11877. guild_leave( *sd, p->guild_id, p->AID, p->CID, p->message );
  11878. }
  11879. /// Request to expel a member of a guild.
  11880. /// 015b <guild id>.L <account id>.L <char id>.L <reason>.40B (CZ_REQ_BAN_GUILD)
  11881. void clif_parse_GuildExpulsion(int32 fd,map_session_data *sd){
  11882. if( sd == nullptr ){
  11883. return;
  11884. }
  11885. const PACKET_CZ_REQ_BAN_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_BAN_GUILD*>( RFIFOP( fd, 0 ) );
  11886. guild_expulsion( *sd, p->guild_id, p->AID, p->CID, p->message );
  11887. }
  11888. /// Validates and processes guild messages (CZ_GUILD_CHAT).
  11889. /// 017e <packet len>.W <text>.?B (<name> : <message>) 00
  11890. void clif_parse_GuildMessage(int32 fd, map_session_data* sd){
  11891. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  11892. // validate packet and retrieve name and message
  11893. if( !clif_process_message( sd, false, name, message, output ) )
  11894. return;
  11895. if( sd->bg_id )
  11896. bg_send_message(sd, output, strlen(output) );
  11897. else
  11898. guild_send_message(sd, output, strlen(output) );
  11899. }
  11900. /// Guild alliance request (CZ_REQ_ALLY_GUILD).
  11901. /// 0170 <account id>.L <inviter account id>.L <inviter char id>.L
  11902. void clif_parse_GuildRequestAlliance(int32 fd, map_session_data *sd)
  11903. {
  11904. map_session_data *t_sd;
  11905. if(!sd->state.gmaster_flag)
  11906. return;
  11907. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11908. clif_displaymessage(fd, msg_txt(sd,228));
  11909. return;
  11910. }
  11911. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11912. //inv_aid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  11913. //inv_cid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[2]);
  11914. // @noask [LuzZza]
  11915. if(t_sd && t_sd->state.noask) {
  11916. clif_noask_sub( *sd, *t_sd, 396 ); // Autorejected alliance request from %s.
  11917. return;
  11918. }
  11919. guild_reqalliance(sd,t_sd);
  11920. }
  11921. /// Answer to a guild alliance request (CZ_ALLY_GUILD).
  11922. /// 0172 <inviter account id>.L <answer>.L
  11923. /// answer:
  11924. /// 0 = refuse
  11925. /// 1 = accept
  11926. void clif_parse_GuildReplyAlliance(int32 fd, map_session_data *sd){
  11927. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11928. guild_reply_reqalliance(sd,
  11929. RFIFOL(fd,info->pos[0]),
  11930. RFIFOL(fd,info->pos[1]));
  11931. }
  11932. /// Request to delete a guild alliance or opposition (CZ_REQ_DELETE_RELATED_GUILD).
  11933. /// 0183 <opponent guild id>.L <relation>.L
  11934. /// relation:
  11935. /// 0 = Ally
  11936. /// 1 = Enemy
  11937. void clif_parse_GuildDelAlliance(int32 fd, map_session_data *sd){
  11938. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  11939. if(!sd->state.gmaster_flag)
  11940. return;
  11941. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11942. clif_displaymessage(fd, msg_txt(sd,228));
  11943. return;
  11944. }
  11945. guild_delalliance(sd,
  11946. RFIFOL(fd,info->pos[0]),
  11947. RFIFOL(fd,info->pos[1]));
  11948. }
  11949. /// Request to set a guild as opposition (CZ_REQ_HOSTILE_GUILD).
  11950. /// 0180 <account id>.L
  11951. void clif_parse_GuildOpposition(int32 fd, map_session_data *sd)
  11952. {
  11953. map_session_data *t_sd;
  11954. if(!sd->state.gmaster_flag)
  11955. return;
  11956. if(map_getmapflag(sd->bl.m, MF_GUILDLOCK)) { //Guild locked.
  11957. clif_displaymessage(fd, msg_txt(sd,228));
  11958. return;
  11959. }
  11960. t_sd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11961. // @noask [LuzZza]
  11962. if(t_sd && t_sd->state.noask) {
  11963. clif_noask_sub( *sd, *t_sd, 397 ); // Autorejected opposition request from %s.
  11964. return;
  11965. }
  11966. guild_opposition(sd,t_sd);
  11967. }
  11968. /// Request to delete own guild.
  11969. /// 015d <key>.40B (CZ_REQ_DISORGANIZE_GUILD)
  11970. /// key:
  11971. /// now guild name; might have been (intended) email, since the
  11972. /// field name and size is same as the one in CH_DELETE_CHAR.
  11973. void clif_parse_GuildBreak( int32 fd, map_session_data *sd ){
  11974. if( sd == nullptr ){
  11975. return;
  11976. }
  11977. const PACKET_CZ_REQ_DISORGANIZE_GUILD* p = reinterpret_cast<PACKET_CZ_REQ_DISORGANIZE_GUILD*>( RFIFOP( fd, 0 ) );
  11978. guild_break( *sd, p->key );
  11979. }
  11980. /// Pet
  11981. ///
  11982. /// Request to invoke a pet menu action (CZ_COMMAND_PET).
  11983. /// 01a1 <type>.B
  11984. /// type:
  11985. /// 0 = pet information
  11986. /// 1 = feed
  11987. /// 2 = performance
  11988. /// 3 = return to egg
  11989. /// 4 = unequip accessory
  11990. void clif_parse_PetMenu(int32 fd, map_session_data *sd){
  11991. pet_menu(sd,RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11992. }
  11993. /// Attempt to tame a monster (CZ_TRYCAPTURE_MONSTER).
  11994. /// 019f <id>.L
  11995. void clif_parse_CatchPet(int32 fd, map_session_data *sd){
  11996. pet_catch_process_end(*sd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  11997. }
  11998. /// Answer to pet incubator egg selection dialog (CZ_SELECT_PETEGG).
  11999. /// 01a7 <index>.W
  12000. void clif_parse_SelectEgg(int32 fd, map_session_data *sd){
  12001. if (sd->menuskill_id != SA_TAMINGMONSTER || sd->menuskill_val != -1)
  12002. return;
  12003. pet_select_egg(sd,RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0])-2);
  12004. clif_menuskill_clear(sd);
  12005. }
  12006. /// Request to display pet's emotion/talk (CZ_PET_ACT).
  12007. /// 01a9 <data>.L
  12008. /// data:
  12009. /// is either emotion (@see enum emotion_type) or a compound value
  12010. /// (((mob id)-100)*100+(act id)*10+(hunger)) that describes an
  12011. /// entry (given in parentheses) in data\pettalktable.xml
  12012. /// act id:
  12013. /// 0 = feeding
  12014. /// 1 = hunting
  12015. /// 2 = danger
  12016. /// 3 = dead
  12017. /// 4 = normal (stand)
  12018. /// 5 = special performance (perfor_s)
  12019. /// 6 = level up (levelup)
  12020. /// 7 = performance 1 (perfor_1)
  12021. /// 8 = performance 2 (perfor_2)
  12022. /// 9 = performance 3 (perfor_3)
  12023. /// 10 = log-in greeting (connect)
  12024. /// hungry value:
  12025. /// 0 = very hungry (hungry)
  12026. /// 1 = hungry (bit_hungry)
  12027. /// 2 = satisfied (noting)
  12028. /// 3 = stuffed (full)
  12029. /// 4 = full (so_full)
  12030. void clif_parse_SendEmotion(int32 fd, map_session_data *sd)
  12031. {
  12032. if(sd->pd)
  12033. clif_pet_emotion(sd->pd,RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12034. }
  12035. /// Request to change pet's name (CZ_RENAME_PET).
  12036. /// 01a5 <name>.24B
  12037. void clif_parse_ChangePetName(int32 fd, map_session_data *sd)
  12038. {
  12039. pet_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12040. }
  12041. /// /kill (CZ_DISCONNECT_CHARACTER).
  12042. /// Request to disconnect a character.
  12043. /// 00cc <account id>.L
  12044. /// NOTE: Also sent when using GM right click menu "(name) force to quit"
  12045. void clif_parse_GMKick(int32 fd, map_session_data *sd)
  12046. {
  12047. struct block_list *target;
  12048. int32 tid;
  12049. tid = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12050. target = map_id2bl(tid);
  12051. if (!target) {
  12052. clif_GM_kickack(sd, 0);
  12053. return;
  12054. }
  12055. switch (target->type) {
  12056. case BL_PC:
  12057. {
  12058. char command[NAME_LENGTH+6];
  12059. safesnprintf(command,sizeof(command),"%ckick %s", atcommand_symbol, status_get_name(target));
  12060. is_atcommand(fd, sd, command, 1);
  12061. }
  12062. break;
  12063. /**
  12064. * This one does not invoke any atcommand, so we need to check for permissions.
  12065. */
  12066. case BL_MOB:
  12067. {
  12068. char command[100];
  12069. if( !pc_can_use_command(sd, "killmonster", COMMAND_ATCOMMAND)) {
  12070. clif_GM_kickack(sd, 0);
  12071. return;
  12072. }
  12073. safesnprintf(command,sizeof(command),"/kick %s (%d)", status_get_name(target), status_get_class(target));
  12074. log_atcommand(sd, command);
  12075. status_percent_damage(&sd->bl, target, 100, 0, true); // can invalidate 'target'
  12076. }
  12077. break;
  12078. case BL_NPC:
  12079. {
  12080. struct npc_data* nd = (struct npc_data *)target;
  12081. if( pc_can_use_command(sd, "unloadnpc", COMMAND_ATCOMMAND)) {
  12082. npc_unload_duplicates(nd);
  12083. npc_unload(nd,true);
  12084. npc_read_event_script();
  12085. }
  12086. }
  12087. break;
  12088. default:
  12089. clif_GM_kickack(sd, 0);
  12090. }
  12091. }
  12092. /// /killall (CZ_DISCONNECT_ALL_CHARACTER).
  12093. /// Request to disconnect all characters.
  12094. /// 00ce
  12095. void clif_parse_GMKickAll(int32 fd, map_session_data* sd) {
  12096. char cmd[15];
  12097. safesnprintf(cmd,sizeof(cmd),"%ckickall",atcommand_symbol);
  12098. is_atcommand(fd, sd, cmd, 1);
  12099. }
  12100. /// /remove (CZ_REMOVE_AID).
  12101. /// Request to warp to a character with given login ID.
  12102. /// 01ba <account name>.24B
  12103. /// /shift (CZ_SHIFT).
  12104. /// Request to warp to a character with given name.
  12105. /// 01bb <char name>.24B
  12106. void clif_parse_GMShift(int32 fd, map_session_data *sd)
  12107. {// FIXME: remove is supposed to receive account name for clients prior 20100803RE
  12108. char *player_name;
  12109. char command[NAME_LENGTH+8];
  12110. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12111. player_name[NAME_LENGTH-1] = '\0';
  12112. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, player_name);
  12113. is_atcommand(fd, sd, command, 1);
  12114. }
  12115. /// /remove (CZ_REMOVE_AID_SSO).
  12116. /// Request to warp to a character with given account ID.
  12117. /// 0843 <account id>.L
  12118. void clif_parse_GMRemove2(int32 fd, map_session_data* sd)
  12119. {
  12120. uint32 account_id;
  12121. map_session_data* pl_sd;
  12122. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12123. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12124. char command[NAME_LENGTH+8];
  12125. safesnprintf(command,sizeof(command),"%cjumpto %s", atcommand_symbol, pl_sd->status.name);
  12126. is_atcommand(fd, sd, command, 1);
  12127. }
  12128. }
  12129. /// /recall (CZ_RECALL).
  12130. /// Request to summon a player with given login ID to own position.
  12131. /// 01bc <account name>.24B
  12132. /// /summon (CZ_RECALL_GID).
  12133. /// Request to summon a player with given name to own position.
  12134. /// 01bd <char name>.24B
  12135. void clif_parse_GMRecall(int32 fd, map_session_data *sd)
  12136. {// FIXME: recall is supposed to receive account name for clients prior 20100803RE
  12137. char *player_name;
  12138. char command [NAME_LENGTH+8];
  12139. player_name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12140. player_name[NAME_LENGTH-1] = '\0';
  12141. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, player_name);
  12142. is_atcommand(fd, sd, command, 1);
  12143. }
  12144. /// /recall (CZ_RECALL_SSO).
  12145. /// Request to summon a player with given account ID to own position.
  12146. /// 0842 <account id>.L
  12147. void clif_parse_GMRecall2(int32 fd, map_session_data* sd)
  12148. {
  12149. uint32 account_id;
  12150. map_session_data* pl_sd;
  12151. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12152. if( (pl_sd = map_id2sd(account_id)) != nullptr ) {
  12153. char command[NAME_LENGTH+8];
  12154. safesnprintf(command,sizeof(command),"%crecall %s", atcommand_symbol, pl_sd->status.name);
  12155. is_atcommand(fd, sd, command, 1);
  12156. }
  12157. }
  12158. /// /item /monster (CZ_ITEM_CREATE).
  12159. /// Request to execute GM commands.
  12160. /// Usage:
  12161. /// /item n - summon n monster or acquire n item/s
  12162. /// /item money - grants 2147483647 zenies
  12163. /// /item whereisboss - locate boss mob in current map.(not yet implemented)
  12164. /// /item regenboss_n t - regenerate n boss monster by t millisecond.(not yet implemented)
  12165. /// /item onekillmonster - toggle an ability to kill mobs in one hit.(not yet implemented)
  12166. /// /item bossinfo - display the information of a boss monster in current map.(not yet implemented)
  12167. /// /item cap_n - capture n monster as pet.(not yet implemented)
  12168. /// /item agitinvest - reset current global agit investments.(not yet implemented)
  12169. /// 013f <item/mob name>.24B
  12170. /// 09ce <item/mob name>.100B [Ind/Yommy]
  12171. void clif_parse_GM_Item_Monster(int32 fd, map_session_data *sd)
  12172. {
  12173. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12174. StringBuf command;
  12175. char *str;
  12176. //#if PACKETVER >= 20131218
  12177. // char str[100];
  12178. //#else
  12179. // char str[24];
  12180. //#endif
  12181. str = RFIFOCP(fd,info->pos[0]);
  12182. if (!str || str[0] == '\0')
  12183. return;
  12184. if (strcmpi(str,"SPPOINT") == 0 || strcmpi(str,"JOBLEVEL") == 0) //! TODO /sp sends these values
  12185. return;
  12186. trim(str);
  12187. // Zeny
  12188. if( strcmpi(str, "money") == 0 ) {
  12189. StringBuf_Init(&command);
  12190. StringBuf_Printf(&command, "%czeny %d", atcommand_symbol, INT_MAX);
  12191. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12192. StringBuf_Destroy(&command);
  12193. return;
  12194. }
  12195. std::shared_ptr<item_data> id = item_db.searchname( str );
  12196. // Item
  12197. if( id ){
  12198. StringBuf_Init(&command);
  12199. if( !itemdb_isstackable2( id.get() ) ){ //Nonstackable
  12200. StringBuf_Printf(&command, "%citem2 %u 1 0 0 0 0 0 0 0", atcommand_symbol, id->nameid);
  12201. }else{
  12202. if (id->flag.guid)
  12203. StringBuf_Printf(&command, "%citem %u 1", atcommand_symbol, id->nameid);
  12204. else
  12205. StringBuf_Printf(&command, "%citem %u 20", atcommand_symbol, id->nameid);
  12206. }
  12207. is_atcommand(fd, sd, StringBuf_Value(&command), 1);
  12208. StringBuf_Destroy(&command);
  12209. return;
  12210. }
  12211. // Monster
  12212. if (pc_can_use_command(sd, "monster", COMMAND_ATCOMMAND)) {
  12213. // If AegisName matches exactly, summon that monster (official behavior)
  12214. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname(str);
  12215. // Otherwise, search for monster with that ID or name (rAthena added behavior)
  12216. if (mob == nullptr) {
  12217. // Check for ID first as this is faster; if search string is not a number it will return 0
  12218. int32 mob_id = util::strtoint32def(str);
  12219. if (mob_id == 0 || mobdb_checkid(mob_id) == 0) {
  12220. mob_id = mobdb_searchname(str);
  12221. if (mob_id != 0) {
  12222. mob = mob_db.find(mob_id);
  12223. }
  12224. }
  12225. else
  12226. mob = mob_db.find(mob_id);
  12227. }
  12228. // Call corresponding atcommand when a valid monster was found
  12229. if (mob != nullptr) {
  12230. char command[CHAT_SIZE_MAX];
  12231. safesnprintf(command, sizeof(command), "%cmonster %s", atcommand_symbol, mob->sprite.c_str());
  12232. is_atcommand(fd, sd, command, 1);
  12233. return;
  12234. }
  12235. }
  12236. }
  12237. /// /hide (CZ_CHANGE_EFFECTSTATE).
  12238. /// 019d <effect state>.L
  12239. /// effect state:
  12240. /// TODO: Any OPTION_* ?
  12241. void clif_parse_GMHide(int32 fd, map_session_data *sd) {
  12242. char cmd[6];
  12243. //int32 eff_st = RFIFOL(packet_db[RFIFOW(fd,0)].pos[0]);
  12244. safesnprintf(cmd,sizeof(cmd),"%chide",atcommand_symbol);
  12245. is_atcommand(fd, sd, cmd, 1);
  12246. }
  12247. /// /resetcooltime
  12248. /// 0a88 (CZ_CMD_RESETCOOLTIME).
  12249. void clif_parse_gm_resetcooltime( int32 fd, map_session_data* sd ){
  12250. #if PACKETVER_MAIN_NUM >= 20160622 || PACKETVER_RE_NUM >= 20160622 || defined(PACKETVER_ZERO)
  12251. const PACKET_CZ_CMD_RESETCOOLTIME* p = reinterpret_cast<const PACKET_CZ_CMD_RESETCOOLTIME*>( RFIFOP( fd, 0 ) );
  12252. char cmd[CHAT_SIZE_MAX];
  12253. safesnprintf(cmd,sizeof(cmd),"%cresetcooltime",atcommand_symbol);
  12254. is_atcommand(fd, sd, cmd, 1);
  12255. #endif
  12256. }
  12257. /// Request to adjust player's manner points (CZ_REQ_GIVE_MANNER_POINT).
  12258. /// 0149 <account id>.L <type>.B <value>.W
  12259. /// type:
  12260. /// 0 = positive points
  12261. /// 1 = negative points
  12262. /// 2 = self mute (+10 minutes)
  12263. void clif_parse_GMReqNoChat(int32 fd,map_session_data *sd)
  12264. {
  12265. int32 id, type, value;
  12266. map_session_data *dstsd;
  12267. char command[NAME_LENGTH+15];
  12268. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12269. id = RFIFOL(fd,info->pos[0]);
  12270. type = RFIFOB(fd,info->pos[1]);
  12271. value = RFIFOW(fd,info->pos[2]);
  12272. if( type == 0 )
  12273. value = -value;
  12274. //If type is 2 and the ids don't match, this is a crafted hacked packet!
  12275. //Disabled because clients keep self-muting when you give players public @ commands... [Skotlex]
  12276. if (type == 2 /* && (pc_get_group_level(sd) > 0 || sd->bl.id != id)*/)
  12277. return;
  12278. dstsd = map_id2sd(id);
  12279. if( dstsd == nullptr )
  12280. return;
  12281. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, value, dstsd->status.name);
  12282. is_atcommand(fd, sd, command, 1);
  12283. }
  12284. /// /rc (CZ_REQ_GIVE_MANNER_BYNAME).
  12285. /// GM adjustment of a player's manner value by -60.
  12286. /// 0212 <char name>.24B
  12287. void clif_parse_GMRc(int32 fd, map_session_data* sd)
  12288. {
  12289. char command[NAME_LENGTH+15];
  12290. char *name = RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12291. name[NAME_LENGTH-1] = '\0';
  12292. safesnprintf(command,sizeof(command),"%cmute %d %s", atcommand_symbol, 60, name);
  12293. is_atcommand(fd, sd, command, 1);
  12294. }
  12295. /// Result of request to resolve account name (ZC_ACK_ACCOUNTNAME).
  12296. /// 01e0 <account id>.L <account name>.24B
  12297. void clif_account_name(int32 fd, uint32 account_id, const char* accname)
  12298. {
  12299. WFIFOHEAD(fd,packet_len(0x1e0));
  12300. WFIFOW(fd,0) = 0x1e0;
  12301. WFIFOL(fd,2) = account_id;
  12302. safestrncpy(WFIFOCP(fd,6), accname, NAME_LENGTH);
  12303. WFIFOSET(fd,packet_len(0x1e0));
  12304. }
  12305. /// GM requesting account name (for right-click gm menu) (CZ_REQ_ACCOUNTNAME).
  12306. /// 01df <account id>.L
  12307. //! TODO: Figure out how does this actually work
  12308. void clif_parse_GMReqAccountName(int32 fd, map_session_data *sd)
  12309. {
  12310. uint32 account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12311. /*
  12312. char query[30];
  12313. safesnprintf(query,sizeof(query),"%d", account_id);
  12314. intif_request_accinfo(sd->fd, sd->bl.id, pc_get_group_level(sd), query, 1); //will call clif_account_name at return
  12315. */
  12316. clif_account_name(fd, account_id, "");
  12317. }
  12318. /// /changemaptype <x> <y> <type> (CZ_CHANGE_MAPTYPE).
  12319. /// GM single cell type change request.
  12320. /// 0198 <x>.W <y>.W <type>.W
  12321. /// type:
  12322. /// 0 = not walkable
  12323. /// 1 = walkable
  12324. void clif_parse_GMChangeMapType(int32 fd, map_session_data *sd)
  12325. {
  12326. int32 x,y,type;
  12327. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12328. if(! pc_has_permission(sd, PC_PERM_USE_CHANGEMAPTYPE) )
  12329. return;
  12330. x = RFIFOW(fd,info->pos[0]);
  12331. y = RFIFOW(fd,info->pos[1]);
  12332. type = RFIFOW(fd,info->pos[2]);
  12333. map_setgatcell(sd->bl.m,x,y,type);
  12334. clif_changemapcell(0,sd->bl.m,x,y,type,ALL_SAMEMAP);
  12335. //FIXME: once players leave the map, the client 'forgets' this information.
  12336. }
  12337. /// /in /ex (CZ_SETTING_WHISPER_PC).
  12338. /// Request to allow/deny whispers from a nick.
  12339. /// 00cf <nick>.24B <type>.B
  12340. /// type:
  12341. /// 0 = (/ex nick) deny speech from nick
  12342. /// 1 = (/in nick) allow speech from nick
  12343. void clif_parse_PMIgnore(int32 fd, map_session_data* sd)
  12344. {
  12345. char* nick;
  12346. uint8 type;
  12347. int32 i;
  12348. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12349. nick = RFIFOCP(fd,info->pos[0]);
  12350. nick[NAME_LENGTH-1] = '\0'; // to be sure that the player name has at most 23 characters
  12351. type = RFIFOB(fd,info->pos[1]);
  12352. if( type == 0 ) { // Add name to ignore list (block)
  12353. if (strcmp(wisp_server_name, nick) == 0) {
  12354. clif_wisexin(sd, type, 1); // fail
  12355. return;
  12356. }
  12357. // try to find a free spot, while checking for duplicates at the same time
  12358. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12359. if( i == MAX_IGNORE_LIST ) {// no space for new entry
  12360. clif_wisexin(sd, type, 2); // too many blocks
  12361. return;
  12362. }
  12363. if( sd->ignore[i].name[0] != '\0' ) {// name already exists
  12364. clif_wisexin(sd, type, 0); // Aegis reports success.
  12365. return;
  12366. }
  12367. //Insert in position i
  12368. safestrncpy(sd->ignore[i].name, nick, NAME_LENGTH);
  12369. } else { // Remove name from ignore list (unblock)
  12370. // find entry
  12371. ARR_FIND( 0, MAX_IGNORE_LIST, i, sd->ignore[i].name[0] == '\0' || strcmp(sd->ignore[i].name, nick) == 0 );
  12372. if( i == MAX_IGNORE_LIST || sd->ignore[i].name[0] == '\0' ) { //Not found
  12373. clif_wisexin(sd, type, 1); // fail
  12374. return;
  12375. }
  12376. // move everything one place down to overwrite removed entry
  12377. memmove(sd->ignore[i].name, sd->ignore[i+1].name, (MAX_IGNORE_LIST-i-1)*sizeof(sd->ignore[0].name));
  12378. // wipe last entry
  12379. memset(sd->ignore[MAX_IGNORE_LIST-1].name, 0, sizeof(sd->ignore[0].name));
  12380. }
  12381. clif_wisexin(sd, type, 0); // success
  12382. }
  12383. /// /inall /exall (CZ_SETTING_WHISPER_STATE).
  12384. /// Request to allow/deny all whispers.
  12385. /// 00d0 <type>.B
  12386. /// type:
  12387. /// 0 = (/exall) deny all speech
  12388. /// 1 = (/inall) allow all speech
  12389. void clif_parse_PMIgnoreAll(int32 fd, map_session_data *sd)
  12390. {
  12391. int32 type = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]), flag;
  12392. if( type == 0 ) {// Deny all
  12393. if( sd->state.ignoreAll ) {
  12394. flag = 1; // fail
  12395. } else {
  12396. sd->state.ignoreAll = 1;
  12397. flag = 0; // success
  12398. }
  12399. } else {//Unblock everyone
  12400. if( sd->state.ignoreAll ) {
  12401. sd->state.ignoreAll = 0;
  12402. flag = 0; // success
  12403. } else {
  12404. if (sd->ignore[0].name[0] != '\0')
  12405. { //Wipe the ignore list.
  12406. memset(sd->ignore, 0, sizeof(sd->ignore));
  12407. flag = 0; // success
  12408. } else {
  12409. flag = 1; // fail
  12410. }
  12411. }
  12412. }
  12413. clif_wisall(sd, type, flag);
  12414. }
  12415. /// Whisper ignore list (ZC_WHISPER_LIST).
  12416. /// 00d4 <packet len>.W { <char name>.24B }*
  12417. void clif_PMIgnoreList(map_session_data* sd)
  12418. {
  12419. int32 i, fd = sd->fd;
  12420. WFIFOHEAD(fd,4+ARRAYLENGTH(sd->ignore)*NAME_LENGTH);
  12421. WFIFOW(fd,0) = 0xd4;
  12422. for( i = 0; i < ARRAYLENGTH(sd->ignore) && sd->ignore[i].name[0]; i++ ) {
  12423. safestrncpy(WFIFOCP(fd,4+i*NAME_LENGTH), sd->ignore[i].name, NAME_LENGTH);
  12424. }
  12425. WFIFOW(fd,2) = 4+i*NAME_LENGTH;
  12426. WFIFOSET(fd,WFIFOW(fd,2));
  12427. }
  12428. /// Whisper ignore list request (CZ_REQ_WHISPER_LIST).
  12429. /// 00d3
  12430. void clif_parse_PMIgnoreList(int32 fd,map_session_data *sd)
  12431. {
  12432. clif_PMIgnoreList(sd);
  12433. }
  12434. /// Request to invoke the /doridori recovery bonus (CZ_DORIDORI).
  12435. /// 01e7
  12436. void clif_parse_NoviceDoriDori(int32 fd, map_session_data *sd)
  12437. {
  12438. if (sd->state.doridori) return;
  12439. switch (sd->class_&MAPID_UPPERMASK) {
  12440. case MAPID_SOUL_LINKER:
  12441. case MAPID_STAR_GLADIATOR:
  12442. case MAPID_TAEKWON:
  12443. if (!sd->state.rest)
  12444. break;
  12445. [[fallthrough]];
  12446. case MAPID_SUPER_NOVICE:
  12447. case MAPID_SUPER_BABY:
  12448. case MAPID_SUPER_NOVICE_E:
  12449. case MAPID_SUPER_BABY_E:
  12450. sd->state.doridori=1;
  12451. break;
  12452. }
  12453. }
  12454. /// Request to invoke the effect of super novice's guardian angel prayer (CZ_CHOPOKGI).
  12455. /// 01ed
  12456. /// Note: This packet is caused by 7 lines of any text, followed by
  12457. /// the prayer and an another line of any text. The prayer is
  12458. /// defined by lines 790~793 in data\msgstringtable.txt
  12459. /// "Dear angel, can you hear my voice?"
  12460. /// "I am" (space separated player name) "Super Novice~"
  12461. /// "Help me out~ Please~ T_T"
  12462. void clif_parse_NoviceExplosionSpirits(int32 fd, map_session_data *sd)
  12463. {
  12464. if( (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE ) {
  12465. t_exp next = pc_nextbaseexp(sd);
  12466. if( next ) {
  12467. int32 percent = (int32)( ( (double)sd->status.base_exp/(double)next )*1000. );
  12468. if( percent && ( percent%100 ) == 0 ) {// 10.0%, 20.0%, ..., 90.0%
  12469. sc_start(&sd->bl,&sd->bl, SC_EXPLOSIONSPIRITS, 100, 17, skill_get_time(MO_EXPLOSIONSPIRITS, 5)); //Lv17-> +50 critical (noted by Poki) [Skotlex]
  12470. clif_skill_nodamage(&sd->bl, sd->bl, MO_EXPLOSIONSPIRITS, 5); // prayer always shows successful Lv5 cast and disregards noskill restrictions
  12471. }
  12472. }
  12473. }
  12474. }
  12475. /// Friends List
  12476. ///
  12477. /// Toggles a single friend online/offline [Skotlex] (ZC_FRIENDS_STATE).
  12478. /// 0206 <account id>.L <char id>.L <state>.B
  12479. /// 0206 <account id>.L <char id>.L <state>.B <name>.24B >= 20180221
  12480. /// state:
  12481. /// 0 = online
  12482. /// 1 = offline
  12483. void clif_friendslist_toggle(map_session_data *sd,uint32 account_id, uint32 char_id, int32 online)
  12484. {
  12485. int32 i, fd = sd->fd;
  12486. //Seek friend.
  12487. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id &&
  12488. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12489. if(i == MAX_FRIENDS || sd->status.friends[i].char_id == 0)
  12490. return; //Not found
  12491. WFIFOHEAD(fd,packet_len(0x206));
  12492. WFIFOW(fd, 0) = 0x206;
  12493. WFIFOL(fd, 2) = sd->status.friends[i].account_id;
  12494. WFIFOL(fd, 6) = sd->status.friends[i].char_id;
  12495. WFIFOB(fd,10) = !online; //Yeah, a 1 here means "logged off", go figure...
  12496. #if PACKETVER >= 20180221
  12497. safestrncpy(WFIFOCP(fd, 11), sd->status.friends[i].name, NAME_LENGTH);
  12498. #endif
  12499. WFIFOSET(fd, packet_len(0x206));
  12500. }
  12501. //Subfunction called from clif_foreachclient to toggle friends on/off [Skotlex]
  12502. int32 clif_friendslist_toggle_sub(map_session_data *sd,va_list ap)
  12503. {
  12504. uint32 account_id, char_id, online;
  12505. account_id = va_arg(ap, int32);
  12506. char_id = va_arg(ap, int32);
  12507. online = va_arg(ap, int32);
  12508. clif_friendslist_toggle(sd, account_id, char_id, online);
  12509. return 0;
  12510. }
  12511. /// Sends the whole friends list (ZC_FRIENDS_LIST).
  12512. /// 0201 <packet len>.W { <account id>.L <char id>.L <name>.24B }*
  12513. /// 0201 <packet len>.W { <account id>.L <char id>.L }* >= 20180221
  12514. void clif_friendslist_send( map_session_data& sd ){
  12515. PACKET_ZC_FRIENDS_LIST* p = reinterpret_cast<PACKET_ZC_FRIENDS_LIST*>( packet_buffer );
  12516. p->packetType = HEADER_ZC_FRIENDS_LIST;
  12517. p->PacketLength = sizeof( *p );
  12518. for( int32 i = 0, c = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12519. PACKET_ZC_FRIENDS_LIST_sub& f = p->friends[c];
  12520. f.AID = sd.status.friends[i].account_id;
  12521. f.CID = sd.status.friends[i].char_id;
  12522. #if !( PACKETVER_MAIN_NUM >= 20180307 || PACKETVER_RE_NUM >= 20180221 || PACKETVER_ZERO_NUM >= 20180328 ) || PACKETVER >= 20200902
  12523. safestrncpy( f.name, sd.status.friends[i].name, sizeof( f.name ) );
  12524. #endif
  12525. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( f ) );
  12526. c++;
  12527. }
  12528. clif_send( p, p->PacketLength, &sd.bl, SELF );
  12529. // Sending the online players
  12530. for( int32 i = 0; i < MAX_FRIENDS && sd.status.friends[i].char_id; i++ ){
  12531. if( map_charid2sd( sd.status.friends[i].char_id ) ){
  12532. clif_friendslist_toggle( &sd, sd.status.friends[i].account_id, sd.status.friends[i].char_id, 1 );
  12533. }
  12534. }
  12535. }
  12536. /// Notification about the result of a friend add request (ZC_ADD_FRIENDS_LIST).
  12537. /// 0209 <result>.W <account id>.L <char id>.L <name>.24B
  12538. /// result:
  12539. /// 0 = MsgStringTable[821]="You have become friends with (%s)."
  12540. /// 1 = MsgStringTable[822]="(%s) does not want to be friends with you."
  12541. /// 2 = MsgStringTable[819]="Your Friend List is full."
  12542. /// 3 = MsgStringTable[820]="(%s)'s Friend List is full."
  12543. void clif_friendslist_reqack(map_session_data *sd, map_session_data *f_sd, int32 type)
  12544. {
  12545. int32 fd;
  12546. nullpo_retv(sd);
  12547. fd = sd->fd;
  12548. WFIFOHEAD(fd,packet_len(0x209));
  12549. WFIFOW(fd,0) = 0x209;
  12550. WFIFOW(fd,2) = type;
  12551. if (f_sd) {
  12552. WFIFOL(fd,4) = f_sd->status.account_id;
  12553. WFIFOL(fd,8) = f_sd->status.char_id;
  12554. safestrncpy(WFIFOCP(fd, 12), f_sd->status.name,NAME_LENGTH);
  12555. }
  12556. WFIFOSET(fd, packet_len(0x209));
  12557. }
  12558. /// Asks a player for permission to be added as friend (ZC_REQ_ADD_FRIENDS).
  12559. /// 0207 <req account id>.L <req char id>.L <req char name>.24B
  12560. void clif_friendlist_req(map_session_data* sd, uint32 account_id, uint32 char_id, const char* name)
  12561. {
  12562. int32 fd = sd->fd;
  12563. WFIFOHEAD(fd,packet_len(0x207));
  12564. WFIFOW(fd,0) = 0x207;
  12565. WFIFOL(fd,2) = account_id;
  12566. WFIFOL(fd,6) = char_id;
  12567. safestrncpy(WFIFOCP(fd,10), name, NAME_LENGTH);
  12568. WFIFOSET(fd,packet_len(0x207));
  12569. }
  12570. /// Request to add a player as friend (CZ_ADD_FRIENDS).
  12571. /// 0202 <name>.24B
  12572. void clif_parse_FriendsListAdd(int32 fd, map_session_data *sd)
  12573. {
  12574. map_session_data *f_sd;
  12575. int32 i;
  12576. f_sd = map_nick2sd(RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]),false);
  12577. // Friend doesn't exist (no player with this name)
  12578. if (f_sd == nullptr) {
  12579. clif_displaymessage(fd, msg_txt(sd,3));
  12580. return;
  12581. }
  12582. if( sd->bl.id == f_sd->bl.id ) {// adding oneself as friend
  12583. return;
  12584. }
  12585. // ensure that the request player's friend list is not full
  12586. ARR_FIND(0, MAX_FRIENDS, i, sd->status.friends[i].char_id == 0);
  12587. if( i == MAX_FRIENDS ){
  12588. clif_friendslist_reqack(sd, f_sd, 2);
  12589. return;
  12590. }
  12591. // @noask [LuzZza]
  12592. if(f_sd->state.noask) {
  12593. clif_noask_sub( *sd, *f_sd, 398 ); // Autorejected friend request from %s.
  12594. return;
  12595. }
  12596. // Friend already exists
  12597. for (i = 0; i < MAX_FRIENDS && sd->status.friends[i].char_id != 0; i++) {
  12598. if (sd->status.friends[i].char_id == f_sd->status.char_id) {
  12599. clif_displaymessage(fd, msg_txt(sd,671)); //"Friend already exists."
  12600. return;
  12601. }
  12602. }
  12603. f_sd->friend_req = sd->status.char_id;
  12604. sd->friend_req = f_sd->status.char_id;
  12605. clif_friendlist_req(f_sd, sd->status.account_id, sd->status.char_id, sd->status.name);
  12606. }
  12607. /// Answer to a friend add request (CZ_ACK_REQ_ADD_FRIENDS).
  12608. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.B
  12609. /// 0208 <inviter account id>.L <inviter char id>.L <reply>.L (PACKETVER >= 6)
  12610. /// reply:
  12611. /// 0 = rejected
  12612. /// 1 = accepted
  12613. void clif_parse_FriendsListReply(int32 fd, map_session_data *sd)
  12614. {
  12615. map_session_data *f_sd;
  12616. uint32 account_id;
  12617. char reply;
  12618. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12619. account_id = RFIFOL(fd,info->pos[0]);
  12620. //char_id = RFIFOL(fd,info->pos[1]);
  12621. #if PACKETVER < 6
  12622. reply = RFIFOB(fd,info->pos[2]);
  12623. #else
  12624. reply = RFIFOL(fd,info->pos[2]);
  12625. #endif
  12626. if( sd->bl.id == account_id ) {// adding oneself as friend
  12627. return;
  12628. }
  12629. f_sd = map_id2sd(account_id); //The account id is the same as the bl.id of players.
  12630. if (f_sd == nullptr)
  12631. return;
  12632. if (reply == 0 || !( sd->friend_req == f_sd->status.char_id && f_sd->friend_req == sd->status.char_id ) )
  12633. clif_friendslist_reqack(f_sd, sd, 1);
  12634. else {
  12635. int32 i;
  12636. // Find an empty slot
  12637. for (i = 0; i < MAX_FRIENDS; i++)
  12638. if (f_sd->status.friends[i].char_id == 0)
  12639. break;
  12640. if (i == MAX_FRIENDS) {
  12641. clif_friendslist_reqack(f_sd, sd, 2);
  12642. return;
  12643. }
  12644. f_sd->status.friends[i].account_id = sd->status.account_id;
  12645. f_sd->status.friends[i].char_id = sd->status.char_id;
  12646. safestrncpy(f_sd->status.friends[i].name, sd->status.name, NAME_LENGTH);
  12647. clif_friendslist_reqack(f_sd, sd, 0);
  12648. achievement_update_objective(f_sd, AG_ADD_FRIEND, 1, i + 1);
  12649. if (battle_config.friend_auto_add) {
  12650. // Also add f_sd to sd's friendlist.
  12651. for (i = 0; i < MAX_FRIENDS; i++) {
  12652. if (sd->status.friends[i].char_id == f_sd->status.char_id)
  12653. return; //No need to add anything.
  12654. if (sd->status.friends[i].char_id == 0)
  12655. break;
  12656. }
  12657. if (i == MAX_FRIENDS) {
  12658. clif_friendslist_reqack(sd, f_sd, 2);
  12659. return;
  12660. }
  12661. sd->status.friends[i].account_id = f_sd->status.account_id;
  12662. sd->status.friends[i].char_id = f_sd->status.char_id;
  12663. safestrncpy(sd->status.friends[i].name, f_sd->status.name, NAME_LENGTH);
  12664. clif_friendslist_reqack(sd, f_sd, 0);
  12665. achievement_update_objective(sd, AG_ADD_FRIEND, 1, i + 1);
  12666. }
  12667. }
  12668. }
  12669. /// Request to delete a friend (CZ_DELETE_FRIENDS).
  12670. /// 0203 <account id>.L <char id>.L
  12671. void clif_parse_FriendsListRemove(int32 fd, map_session_data *sd)
  12672. {
  12673. map_session_data *f_sd = nullptr;
  12674. uint32 account_id, char_id;
  12675. int32 i, j;
  12676. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12677. account_id = RFIFOL(fd,info->pos[0]);
  12678. char_id = RFIFOL(fd,info->pos[1]);
  12679. // Search friend
  12680. for (i = 0; i < MAX_FRIENDS &&
  12681. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12682. if (i == MAX_FRIENDS) {
  12683. clif_displaymessage(fd, msg_txt(sd,672)); //"Name not found in list."
  12684. return;
  12685. }
  12686. //remove from friend's list first
  12687. if( (f_sd = map_id2sd(account_id)) && f_sd->status.char_id == char_id) {
  12688. for (i = 0; i < MAX_FRIENDS &&
  12689. (f_sd->status.friends[i].char_id != sd->status.char_id || f_sd->status.friends[i].account_id != sd->status.account_id); i++);
  12690. if (i != MAX_FRIENDS) {
  12691. // move all chars up
  12692. for(j = i + 1; j < MAX_FRIENDS; j++)
  12693. memcpy(&f_sd->status.friends[j-1], &f_sd->status.friends[j], sizeof(f_sd->status.friends[0]));
  12694. memset(&f_sd->status.friends[MAX_FRIENDS-1], 0, sizeof(f_sd->status.friends[MAX_FRIENDS-1]));
  12695. //should the guy be notified of some message? we should add it here if so
  12696. WFIFOHEAD(f_sd->fd,packet_len(0x20a));
  12697. WFIFOW(f_sd->fd,0) = 0x20a;
  12698. WFIFOL(f_sd->fd,2) = sd->status.account_id;
  12699. WFIFOL(f_sd->fd,6) = sd->status.char_id;
  12700. WFIFOSET(f_sd->fd, packet_len(0x20a));
  12701. }
  12702. } else { //friend not online -- ask char server to delete from his friendlist
  12703. if(chrif_removefriend(char_id,sd->status.char_id)) { // char-server offline, abort
  12704. clif_displaymessage(fd, msg_txt(sd,673)); //"This action can't be performed at the moment. Please try again later."
  12705. return;
  12706. }
  12707. }
  12708. // We can now delete from original requester
  12709. for (i = 0; i < MAX_FRIENDS &&
  12710. (sd->status.friends[i].char_id != char_id || sd->status.friends[i].account_id != account_id); i++);
  12711. // move all chars up
  12712. for(j = i + 1; j < MAX_FRIENDS; j++)
  12713. memcpy(&sd->status.friends[j-1], &sd->status.friends[j], sizeof(sd->status.friends[0]));
  12714. memset(&sd->status.friends[MAX_FRIENDS-1], 0, sizeof(sd->status.friends[MAX_FRIENDS-1]));
  12715. clif_displaymessage(fd, msg_txt(sd,674)); //"Friend removed"
  12716. WFIFOHEAD(fd,packet_len(0x20a));
  12717. WFIFOW(fd,0) = 0x20a;
  12718. WFIFOL(fd,2) = account_id;
  12719. WFIFOL(fd,6) = char_id;
  12720. WFIFOSET(fd, packet_len(0x20a));
  12721. }
  12722. /// /pvpinfo list (ZC_ACK_PVPPOINT).
  12723. /// 0210 <char id>.L <account id>.L <win point>.L <lose point>.L <point>.L
  12724. void clif_PVPInfo(map_session_data* sd)
  12725. {
  12726. int32 fd = sd->fd;
  12727. WFIFOHEAD(fd,packet_len(0x210));
  12728. WFIFOW(fd,0) = 0x210;
  12729. WFIFOL(fd,2) = sd->status.char_id;
  12730. WFIFOL(fd,6) = sd->status.account_id;
  12731. WFIFOL(fd,10) = sd->pvp_won; // times won
  12732. WFIFOL(fd,14) = sd->pvp_lost; // times lost
  12733. WFIFOL(fd,18) = sd->pvp_point;
  12734. WFIFOSET(fd, packet_len(0x210));
  12735. }
  12736. /// /pvpinfo (CZ_REQ_PVPPOINT).
  12737. /// 020f <char id>.L <account id>.L
  12738. void clif_parse_PVPInfo(int32 fd,map_session_data *sd)
  12739. {
  12740. // TODO: Is there a way to use this on an another player (char/acc id)?
  12741. //int32 cid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12742. //int32 aid = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  12743. clif_PVPInfo(sd);
  12744. }
  12745. /// SG Feel save OK [Komurka] (CZ_AGREE_STARPLACE).
  12746. /// 0254 <which>.B
  12747. /// which: (unused atm for security purpose)
  12748. /// 0 = sun
  12749. /// 1 = moon
  12750. /// 2 = star
  12751. void clif_parse_FeelSaveOk(int32 fd,map_session_data *sd)
  12752. {
  12753. int32 i;
  12754. //int32 wich = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  12755. if (sd->menuskill_id != SG_FEEL)
  12756. return;
  12757. i = sd->menuskill_val-1;
  12758. if (i<0 || i >= MAX_PC_FEELHATE) return; //Bug?
  12759. sd->feel_map[i].index = map_id2index(sd->bl.m);
  12760. sd->feel_map[i].m = sd->bl.m;
  12761. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), sd->feel_map[i].index);
  12762. //Are these really needed? Shouldn't they show up automatically from the feel save packet?
  12763. // clif_misceffect2(&sd->bl, 0x1b0);
  12764. // clif_misceffect2(&sd->bl, 0x21f);
  12765. clif_feel_info(sd, i, 0);
  12766. clif_menuskill_clear(sd);
  12767. }
  12768. /// Star Gladiator's Feeling map confirmation prompt (ZC_STARPLACE).
  12769. /// 0253 <which>.B
  12770. /// which:
  12771. /// 0 = sun
  12772. /// 1 = moon
  12773. /// 2 = star
  12774. void clif_feel_req(int32 fd, map_session_data *sd, uint16 skill_lv)
  12775. {
  12776. WFIFOHEAD(fd,packet_len(0x253));
  12777. WFIFOW(fd,0)=0x253;
  12778. WFIFOB(fd,2)=TOB(skill_lv-1);
  12779. WFIFOSET(fd, packet_len(0x253));
  12780. sd->menuskill_id = SG_FEEL;
  12781. sd->menuskill_val = skill_lv;
  12782. }
  12783. /// Request to change homunculus' name (CZ_RENAME_MER).
  12784. /// 0231 <name>.24B
  12785. void clif_parse_ChangeHomunculusName(int32 fd, map_session_data *sd){
  12786. hom_change_name(sd,RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  12787. }
  12788. /// Request to warp/move homunculus/mercenary to it's owner (CZ_REQUEST_MOVETOOWNER).
  12789. /// 0234 <id>.L
  12790. void clif_parse_HomMoveToMaster(int32 fd, map_session_data *sd){
  12791. int32 id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]); // Mercenary or Homunculus
  12792. struct block_list *bl = nullptr;
  12793. struct unit_data *ud = nullptr;
  12794. if( sd->md && sd->md->bl.id == id )
  12795. bl = &sd->md->bl;
  12796. else if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12797. bl = &sd->hd->bl; // Moving Homunculus
  12798. else
  12799. return;
  12800. unit_calc_pos(bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  12801. ud = unit_bl2ud(bl);
  12802. unit_walktoxy(bl, ud->to_x, ud->to_y, 4);
  12803. }
  12804. /// Request to move homunculus/mercenary.
  12805. /// 0232 <id>.L <position data>.3B (CZ_REQUEST_MOVENPC)
  12806. void clif_parse_HomMoveTo(int32 fd, map_session_data *sd){
  12807. #if PACKETVER >= 20050425
  12808. const PACKET_CZ_REQUEST_MOVENPC* p = reinterpret_cast<const PACKET_CZ_REQUEST_MOVENPC*>( RFIFOP( fd, 0 ) );
  12809. struct block_list *bl = nullptr;
  12810. short x, y;
  12811. RBUFPOS( p->PosDir, 0, &x, &y, nullptr );
  12812. if( sd->md && sd->md->bl.id == p->GID )
  12813. bl = &sd->md->bl; // Moving Mercenary
  12814. else if( hom_is_active(sd->hd) && sd->hd->bl.id == p->GID )
  12815. bl = &sd->hd->bl; // Moving Homunculus
  12816. else
  12817. return;
  12818. if (x < 0 || y < 0 || (x == bl->x && y == bl->y))
  12819. return;
  12820. unit_walktoxy(bl, x, y, 4);
  12821. #endif
  12822. }
  12823. /// Request to do an action with homunculus/mercenary (CZ_REQUEST_ACTNPC).
  12824. /// 0233 <id>.L <target id>.L <action>.B
  12825. /// action:
  12826. /// always 0
  12827. void clif_parse_HomAttack(int32 fd,map_session_data *sd)
  12828. {
  12829. struct block_list *bl = nullptr;
  12830. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  12831. int32 id = RFIFOL(fd,info->pos[0]);
  12832. int32 target_id = RFIFOL(fd,info->pos[1]);
  12833. int32 action_type = RFIFOB(fd,info->pos[2]);
  12834. if( hom_is_active(sd->hd) && sd->hd->bl.id == id )
  12835. bl = &sd->hd->bl;
  12836. else if( sd->md && sd->md->bl.id == id )
  12837. bl = &sd->md->bl;
  12838. else return;
  12839. unit_attack(bl, target_id, action_type != 0);
  12840. }
  12841. /// Request to invoke a homunculus menu action (CZ_COMMAND_MER).
  12842. /// 022d <type>.W <command>.B
  12843. /// type:
  12844. /// always 0
  12845. /// command:
  12846. /// 0 = homunculus information
  12847. /// 1 = feed
  12848. /// 2 = delete
  12849. void clif_parse_HomMenu(int32 fd, map_session_data *sd)
  12850. { //[orn]
  12851. int32 cmd = RFIFOW(fd,0);
  12852. //int32 type = RFIFOW(fd,packet_db[cmd].pos[0]);
  12853. if(!hom_is_active(sd->hd))
  12854. return;
  12855. hom_menu(sd,RFIFOB(fd,packet_db[cmd].pos[1]));
  12856. }
  12857. /// Request to resurrect oneself using Token of Siegfried (CZ_STANDING_RESURRECTION).
  12858. /// 0292
  12859. void clif_parse_AutoRevive(int32 fd, map_session_data *sd)
  12860. {
  12861. pc_revive_item(sd);
  12862. }
  12863. /// Information about character's status values (ZC_ACK_STATUS_GM).
  12864. /// 0214 <str>.B <standardStr>.B <agi>.B <standardAgi>.B <vit>.B <standardVit>.B
  12865. /// <int>.B <standardInt>.B <dex>.B <standardDex>.B <luk>.B <standardLuk>.B
  12866. /// <attPower>.W <refiningPower>.W <max_mattPower>.W <min_mattPower>.W
  12867. /// <itemdefPower>.W <plusdefPower>.W <mdefPower>.W <plusmdefPower>.W
  12868. /// <hitSuccessValue>.W <avoidSuccessValue>.W <plusAvoidSuccessValue>.W
  12869. /// <criticalSuccessValue>.W <ASPD>.W <plusASPD>.W
  12870. void clif_check(int32 fd, map_session_data* pl_sd)
  12871. {
  12872. WFIFOHEAD(fd,packet_len(0x214));
  12873. WFIFOW(fd, 0) = 0x214;
  12874. WFIFOB(fd, 2) = min(pl_sd->status.str, UINT8_MAX);
  12875. WFIFOB(fd, 3) = pc_need_status_point(pl_sd, SP_STR, 1);
  12876. WFIFOB(fd, 4) = min(pl_sd->status.agi, UINT8_MAX);
  12877. WFIFOB(fd, 5) = pc_need_status_point(pl_sd, SP_AGI, 1);
  12878. WFIFOB(fd, 6) = min(pl_sd->status.vit, UINT8_MAX);
  12879. WFIFOB(fd, 7) = pc_need_status_point(pl_sd, SP_VIT, 1);
  12880. WFIFOB(fd, 8) = min(pl_sd->status.int_, UINT8_MAX);
  12881. WFIFOB(fd, 9) = pc_need_status_point(pl_sd, SP_INT, 1);
  12882. WFIFOB(fd,10) = min(pl_sd->status.dex, UINT8_MAX);
  12883. WFIFOB(fd,11) = pc_need_status_point(pl_sd, SP_DEX, 1);
  12884. WFIFOB(fd,12) = min(pl_sd->status.luk, UINT8_MAX);
  12885. WFIFOB(fd,13) = pc_need_status_point(pl_sd, SP_LUK, 1);
  12886. WFIFOW(fd,14) = pl_sd->battle_status.batk+pl_sd->battle_status.rhw.atk+pl_sd->battle_status.lhw.atk;
  12887. WFIFOW(fd,16) = pl_sd->battle_status.rhw.atk2+pl_sd->battle_status.lhw.atk2;
  12888. WFIFOW(fd,18) = pl_sd->battle_status.matk_max;
  12889. WFIFOW(fd,20) = pl_sd->battle_status.matk_min;
  12890. WFIFOW(fd,22) = pl_sd->battle_status.def;
  12891. WFIFOW(fd,24) = pl_sd->battle_status.def2;
  12892. WFIFOW(fd,26) = pl_sd->battle_status.mdef;
  12893. WFIFOW(fd,28) = pl_sd->battle_status.mdef2;
  12894. WFIFOW(fd,30) = pl_sd->battle_status.hit;
  12895. WFIFOW(fd,32) = pl_sd->battle_status.flee;
  12896. WFIFOW(fd,34) = pl_sd->battle_status.flee2/10;
  12897. WFIFOW(fd,36) = pl_sd->battle_status.cri/10;
  12898. WFIFOW(fd,38) = (2000-pl_sd->battle_status.amotion)/10; // aspd
  12899. WFIFOW(fd,40) = 0; // FIXME: What is 'plusASPD' supposed to be? Maybe adelay?
  12900. WFIFOSET(fd,packet_len(0x214));
  12901. }
  12902. /// /check (CZ_REQ_STATUS_GM).
  12903. /// Request character's status values.
  12904. /// 0213 <char name>.24B
  12905. void clif_parse_Check(int32 fd, map_session_data *sd)
  12906. {
  12907. char charname[NAME_LENGTH];
  12908. map_session_data* pl_sd;
  12909. if(!pc_has_permission(sd, PC_PERM_USE_CHECK))
  12910. return;
  12911. safestrncpy(charname, RFIFOCP(fd,packet_db[RFIFOW(fd,0)].pos[0]), sizeof(charname));
  12912. if( ( pl_sd = map_nick2sd(charname,false) ) == nullptr || pc_get_group_level(sd) < pc_get_group_level(pl_sd) )
  12913. {
  12914. return;
  12915. }
  12916. clif_check(fd, pl_sd);
  12917. }
  12918. /// MAIL SYSTEM
  12919. /// By Zephyrus
  12920. ///
  12921. /// Notification about the result of adding an item to mail
  12922. /// 0255 <index>.W <result>.B (ZC_ACK_MAIL_ADD_ITEM)
  12923. /// result:
  12924. /// 0 = success
  12925. /// 1 = failure
  12926. /// 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)
  12927. /// { <card id>.W }*4 { <option index>.W <option value>.W <option parameter>.B }*5
  12928. /// result:
  12929. /// 0 = success
  12930. /// 1 = over weight
  12931. /// 2 = fatal error
  12932. void clif_Mail_setattachment( map_session_data* sd, int32 index, int32 amount, uint8 flag ){
  12933. int32 fd = sd->fd;
  12934. #if PACKETVER < 20150513
  12935. WFIFOHEAD(fd,packet_len(0x255));
  12936. WFIFOW(fd,0) = 0x255;
  12937. WFIFOW(fd,2) = index;
  12938. WFIFOB(fd,4) = flag;
  12939. WFIFOSET(fd,packet_len(0x255));
  12940. #else
  12941. PACKET_ZC_ACK_ADD_ITEM_RODEX p = {};
  12942. if( flag ){
  12943. memset( &p, 0, sizeof( p ) );
  12944. }else{
  12945. struct item* item = &sd->inventory.u.items_inventory[server_index(index)];
  12946. p.index = index;
  12947. p.count = amount;
  12948. p.itemId = client_nameid( item->nameid );
  12949. p.type = itemtype( item->nameid );
  12950. p.IsIdentified = item->identify ? 1 : 0;
  12951. p.IsDamaged = item->attribute ? 1 : 0;
  12952. p.refiningLevel = item->refine;
  12953. clif_addcards( &p.slot, item );
  12954. clif_add_random_options( p.optionData, *item );
  12955. p.weight = 0;
  12956. for( int32 i = 0; i < MAIL_MAX_ITEM; i++ ){
  12957. if( sd->mail.item[i].nameid == 0 ){
  12958. continue;
  12959. }
  12960. p.weight += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  12961. }
  12962. p.favorite = ( item->favorite != 0 ) ? 1 : 0;
  12963. p.location = pc_equippoint( sd, server_index( index ) );
  12964. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  12965. p.grade = item->enchantgrade;
  12966. #endif
  12967. }
  12968. p.PacketType = HEADER_ZC_ACK_ADD_ITEM_RODEX;
  12969. p.result = flag;
  12970. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  12971. #endif
  12972. }
  12973. /// Notification about the result of retrieving a mail attachment
  12974. /// 0245 <result>.B (ZC_MAIL_REQ_GET_ITEM)
  12975. /// result:
  12976. /// 0 = success
  12977. /// 1 = failure
  12978. /// 2 = too many items
  12979. /// 09f2 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ZENY_FROM_MAIL)
  12980. /// 09f4 <mail id>.Q <mail tab>.B <result>.B (ZC_ACK_ITEM_FROM_MAIL)
  12981. void clif_mail_getattachment(map_session_data* sd, struct mail_message *msg, uint8 result, enum mail_attachment_type type) {
  12982. #if PACKETVER < 20150513
  12983. int32 fd = sd->fd;
  12984. WFIFOHEAD(fd,packet_len(0x245));
  12985. WFIFOW(fd,0) = 0x245;
  12986. WFIFOB(fd,2) = result;
  12987. WFIFOSET(fd,packet_len(0x245));
  12988. #else
  12989. switch( type ){
  12990. case MAIL_ATT_ITEM:
  12991. case MAIL_ATT_ZENY:
  12992. break;
  12993. default:
  12994. return;
  12995. }
  12996. PACKET_ZC_ACK_ITEM_FROM_MAIL p = { 0 };
  12997. p.PacketType = type == MAIL_ATT_ZENY ? rodexgetzeny : rodexgetitem;
  12998. p.MailID = msg->id;
  12999. p.opentype = msg->type;
  13000. p.result = result;
  13001. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13002. clif_Mail_refreshinbox( sd, msg->type, 0 );
  13003. #endif
  13004. }
  13005. /// Notification about the result of sending a mail
  13006. /// 0249 <result>.B (ZC_MAIL_REQ_SEND)
  13007. /// result:
  13008. /// 0 = success
  13009. /// 1 = recipinent does not exist
  13010. /// 09ed <result>.B (ZC_ACK_WRITE_MAIL)
  13011. void clif_Mail_send(map_session_data* sd, enum mail_send_result result){
  13012. #if PACKETVER < 20150513
  13013. int32 fd = sd->fd;
  13014. WFIFOHEAD(fd,packet_len(0x249));
  13015. WFIFOW(fd,0) = 0x249;
  13016. WFIFOB(fd,2) = result != WRITE_MAIL_SUCCESS;
  13017. WFIFOSET(fd,packet_len(0x249));
  13018. #else
  13019. PACKET_ZC_WRITE_MAIL_RESULT p = { 0 };
  13020. p.PacketType = rodexwriteresult;
  13021. p.result = result;
  13022. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13023. #endif
  13024. }
  13025. /// Notification about the result of deleting a mail.
  13026. /// 0257 <mail id>.L <result>.W (ZC_ACK_MAIL_DELETE)
  13027. /// result:
  13028. /// 0 = success
  13029. /// 1 = failure
  13030. // 09f6 <mail tab>.B <mail id>.Q (ZC_ACK_DELETE_MAIL)
  13031. void clif_mail_delete( map_session_data* sd, struct mail_message *msg, bool success ){
  13032. #if PACKETVER < 20150513
  13033. int32 fd = sd->fd;
  13034. WFIFOHEAD(fd, packet_len(0x257));
  13035. WFIFOW(fd,0) = 0x257;
  13036. WFIFOL(fd,2) = msg->id;
  13037. WFIFOW(fd,6) = !success;
  13038. WFIFOSET(fd, packet_len(0x257));
  13039. #else
  13040. // This is only a success notification
  13041. if( success ){
  13042. PACKET_ZC_ACK_DELETE_MAIL p = { 0 };
  13043. p.PacketType = rodexdelete;
  13044. p.opentype = msg->type;
  13045. p.MailID = msg->id;
  13046. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13047. }
  13048. #endif
  13049. }
  13050. /// Notification about the result of returning a mail (ZC_ACK_MAIL_RETURN).
  13051. /// 0274 <mail id>.L <result>.W
  13052. /// result:
  13053. /// 0 = success
  13054. /// 1 = failure
  13055. void clif_Mail_return(int32 fd, int32 mail_id, short fail)
  13056. {
  13057. WFIFOHEAD(fd,packet_len(0x274));
  13058. WFIFOW(fd,0) = 0x274;
  13059. WFIFOL(fd,2) = mail_id;
  13060. WFIFOW(fd,6) = fail;
  13061. WFIFOSET(fd,packet_len(0x274));
  13062. }
  13063. /// Notification about new mail.
  13064. /// 024a <mail id>.L <title>.40B <sender>.24B (ZC_MAIL_RECEIVE)
  13065. /// 09e7 <result>.B (ZC_NOTIFY_UNREADMAIL)
  13066. void clif_Mail_new(map_session_data* sd, int32 mail_id, const char *sender, const char *title){
  13067. #if PACKETVER < 20150513
  13068. int32 fd = sd->fd;
  13069. WFIFOHEAD(fd,packet_len(0x24a));
  13070. WFIFOW(fd,0) = 0x24a;
  13071. WFIFOL(fd,2) = mail_id;
  13072. safestrncpy(WFIFOCP(fd,6), title, MAIL_TITLE_LENGTH);
  13073. safestrncpy(WFIFOCP(fd,46), sender, NAME_LENGTH);
  13074. WFIFOSET(fd,packet_len(0x24a));
  13075. #else
  13076. PACKET_ZC_NOTIFY_UNREADMAIL p = { 0 };
  13077. p.PacketType = rodexicon;
  13078. p.result = sd->mail.inbox.unread > 0 || sd->mail.inbox.unchecked > 0; // unread
  13079. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13080. #endif
  13081. }
  13082. /// Opens/closes the mail window (ZC_MAIL_WINDOWS).
  13083. /// 0260 <type>.L
  13084. /// type:
  13085. /// 0 = open
  13086. /// 1 = close
  13087. void clif_Mail_window(int32 fd, int32 flag)
  13088. {
  13089. WFIFOHEAD(fd,packet_len(0x260));
  13090. WFIFOW(fd,0) = 0x260;
  13091. WFIFOL(fd,2) = flag;
  13092. WFIFOSET(fd,packet_len(0x260));
  13093. }
  13094. /// Lists mails stored in inbox.
  13095. /// 0240 <packet len>.W <amount>.L { <mail id>.L <title>.40B <read>.B <sender>.24B <time>.L }*amount (ZC_MAIL_REQ_GET_LIST)
  13096. /// read:
  13097. /// 0 = unread
  13098. /// 1 = read
  13099. /// 09f0 <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST)
  13100. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13101. /// 0a7d <packet len>.W <type>.B <amount>.B <last page>.B (ZC_ACK_MAIL_LIST2)
  13102. /// { <mail id>.Q <read>.B <type>.B <sender>.24B <received>.L <expires>.L <title length>.W <title>.?B }*
  13103. /// 0ac2 <packet len>.W <unknown>.B (ZC_ACK_MAIL_LIST3)
  13104. /// { <type>.B <mail id>.Q <read>.B <type>.B <sender>.24B <expires>.L <title length>.W <title>.?B }*
  13105. void clif_Mail_refreshinbox(map_session_data *sd,enum mail_inbox_type type,int64 mailID){
  13106. #if PACKETVER < 20150513
  13107. int32 fd = sd->fd;
  13108. struct mail_data *md = &sd->mail.inbox;
  13109. struct mail_message *msg;
  13110. int32 len, i, j;
  13111. len = 8 + (73 * md->amount);
  13112. WFIFOHEAD(fd,len);
  13113. WFIFOW(fd,0) = 0x240;
  13114. WFIFOW(fd,2) = len;
  13115. WFIFOL(fd,4) = md->amount;
  13116. for( i = j = 0; i < MAIL_MAX_INBOX && j < md->amount; i++ )
  13117. {
  13118. msg = &md->msg[i];
  13119. if (msg->id < 1)
  13120. continue;
  13121. WFIFOL(fd,8+73*j) = msg->id;
  13122. memcpy(WFIFOP(fd,12+73*j), msg->title, MAIL_TITLE_LENGTH);
  13123. WFIFOB(fd,52+73*j) = (msg->status != MAIL_UNREAD);
  13124. safestrncpy(WFIFOCP(fd,53+73*j), msg->send_name, NAME_LENGTH);
  13125. WFIFOL(fd,77+73*j) = (uint32)msg->timestamp;
  13126. j++;
  13127. }
  13128. WFIFOSET(fd,len);
  13129. if( md->full ) {// TODO: is this official?
  13130. char output[100];
  13131. safesnprintf(output,sizeof(output),"Inbox is full (Max %d). Delete some mails.", MAIL_MAX_INBOX);
  13132. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  13133. }
  13134. #else
  13135. int32 fd = sd->fd;
  13136. struct mail_data *md = &sd->mail.inbox;
  13137. struct mail_message *msg;
  13138. int32 i, j, k, offset, titleLength;
  13139. uint8 mailType, amount, remaining;
  13140. uint32 now = (uint32)time(nullptr);
  13141. #if PACKETVER >= 20170419
  13142. int32 cmd = 0xac2;
  13143. #elif PACKETVER >= 20160601
  13144. int32 cmd = 0xa7d;
  13145. #else
  13146. int32 cmd = 0x9f0;
  13147. #endif
  13148. if( battle_config.mail_daily_count ){
  13149. mail_refresh_remaining_amount(sd);
  13150. }
  13151. #if PACKETVER >= 20170419
  13152. // Always send all
  13153. i = md->amount;
  13154. #else
  13155. // If a starting mail id was sent
  13156. if( mailID != 0 ){
  13157. ARR_FIND( 0, md->amount, i, md->msg[i].id == mailID );
  13158. // Unknown mail
  13159. if( i == md->amount ){
  13160. // Ignore the request for now
  13161. return; // TODO: Should we just show the first page instead?
  13162. }
  13163. // It was actually the oldest/first mail, there is no further page
  13164. if( i == 0 ){
  13165. return;
  13166. }
  13167. // We actually want the next/older mail
  13168. i -= 1;
  13169. }else{
  13170. i = md->amount;
  13171. }
  13172. #endif
  13173. // Count the remaining mails from the starting mail or the beginning
  13174. // Only count mails of the target type(before 2017-04-19) and those that should not have been deleted already
  13175. for( j = i, remaining = 0; j >= 0; j-- ){
  13176. msg = &md->msg[j];
  13177. if (msg->id < 1)
  13178. continue;
  13179. #if PACKETVER < 20170419
  13180. if (msg->type != type)
  13181. continue;
  13182. #endif
  13183. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13184. continue;
  13185. remaining++;
  13186. }
  13187. #if PACKETVER >= 20170419
  13188. // Always send all
  13189. amount = remaining;
  13190. #else
  13191. if( remaining > MAIL_PAGE_SIZE ){
  13192. amount = MAIL_PAGE_SIZE;
  13193. }else{
  13194. amount = remaining;
  13195. }
  13196. #endif
  13197. WFIFOHEAD(fd, 7 + ((44 + MAIL_TITLE_LENGTH) * amount));
  13198. WFIFOW(fd, 0) = cmd;
  13199. #if PACKETVER >= 20170419
  13200. WFIFOB(fd, 4) = 1; // Unknown
  13201. offset = 5;
  13202. #else
  13203. WFIFOB(fd, 4) = type;
  13204. WFIFOB(fd, 5) = amount;
  13205. WFIFOB(fd, 6) = ( remaining < MAIL_PAGE_SIZE ); // last page
  13206. offset = 7;
  13207. #endif
  13208. for( amount = 0; i >= 0; i-- ){
  13209. msg = &md->msg[i];
  13210. if (msg->id < 1)
  13211. continue;
  13212. #if PACKETVER < 20170419
  13213. if (msg->type != type)
  13214. continue;
  13215. #endif
  13216. if (msg->scheduled_deletion > 0 && msg->scheduled_deletion <= now)
  13217. continue;
  13218. #if PACKETVER >= 20170419
  13219. WFIFOB(fd, offset) = msg->type;
  13220. offset += 1;
  13221. #endif
  13222. WFIFOQ(fd, offset + 0) = (uint64)msg->id;
  13223. WFIFOB(fd, offset + 8) = (msg->status != MAIL_UNREAD);
  13224. mailType = MAIL_TYPE_TEXT; // Normal letter
  13225. if( msg->zeny > 0 ){
  13226. mailType |= MAIL_TYPE_ZENY; // Mail contains zeny
  13227. }
  13228. for( k = 0; k < MAIL_MAX_ITEM; k++ ){
  13229. if( msg->item[k].nameid > 0 ){
  13230. mailType |= MAIL_TYPE_ITEM; // Mail contains items
  13231. break;
  13232. }
  13233. }
  13234. #if PACKETVER >= 20170419
  13235. // If it came from an npc?
  13236. if( !msg->send_id ){
  13237. mailType |= MAIL_TYPE_NPC;
  13238. }
  13239. #endif
  13240. WFIFOB(fd, offset + 9) = mailType;
  13241. safestrncpy(WFIFOCP(fd, offset + 10), msg->send_name, NAME_LENGTH);
  13242. #if PACKETVER < 20170419
  13243. // How much time has passed since you received the mail
  13244. WFIFOL(fd, offset + 34 ) = now - (uint32)msg->timestamp;
  13245. offset += 4;
  13246. #endif
  13247. // If automatic return/deletion of mails is enabled, notify the client when it will kick in
  13248. if( msg->scheduled_deletion > 0 ){
  13249. WFIFOL(fd, offset + 34) = (uint32)msg->scheduled_deletion - now;
  13250. }else{
  13251. // Fake the scheduled deletion to one year in the future
  13252. // Sadly the client always displays the scheduled deletion after 24 hours no matter how high this value gets [Lemongrass]
  13253. WFIFOL(fd, offset + 34) = 365 * 24 * 60 * 60;
  13254. }
  13255. WFIFOW(fd, offset + 38) = titleLength = (int16)(strlen(msg->title) + 1);
  13256. safestrncpy(WFIFOCP(fd, offset + 40), msg->title, titleLength);
  13257. offset += 40 + titleLength;
  13258. }
  13259. WFIFOW(fd, 2) = (int16)offset;
  13260. WFIFOSET(fd, offset);
  13261. #endif
  13262. }
  13263. /// Mail inbox list request.
  13264. /// 023f (CZ_MAIL_GET_LIST)
  13265. /// 09e8 <mail tab>.B <mail id>.Q (CZ_OPEN_MAILBOX)
  13266. /// 09ee <mail tab>.B <mail id>.Q (CZ_REQ_NEXT_MAIL_LIST)
  13267. /// 09ef <mail tab>.B <mail id>.Q (CZ_REQ_REFRESH_MAIL_LIST)
  13268. /// 0ac0 <mail id>.Q <unknown>.16B (CZ_OPEN_MAILBOX2)
  13269. /// 0ac1 <mail id>.Q <unknown>.16B (CZ_REQ_REFRESH_MAIL_LIST2)
  13270. void clif_parse_Mail_refreshinbox(int32 fd, map_session_data *sd){
  13271. if( mail_invalid_operation( sd ) ){
  13272. return;
  13273. }
  13274. #if PACKETVER < 20150513
  13275. struct mail_data* md = &sd->mail.inbox;
  13276. if( md->amount < MAIL_MAX_INBOX && (md->full || sd->mail.changed) )
  13277. intif_Mail_requestinbox(sd->status.char_id, 1, MAIL_INBOX_NORMAL);
  13278. else
  13279. clif_Mail_refreshinbox(sd, MAIL_INBOX_NORMAL,0);
  13280. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13281. mail_removezeny(sd, false);
  13282. #else
  13283. int32 cmd = RFIFOW(fd, 0);
  13284. #if PACKETVER < 20170419
  13285. uint8 openType = RFIFOB(fd, 2);
  13286. uint64 mailId = RFIFOQ(fd, 3);
  13287. #else
  13288. uint8 openType;
  13289. uint64 mailId = RFIFOQ(fd, 2);
  13290. int32 i;
  13291. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mailId);
  13292. if( i == MAIL_MAX_INBOX ){
  13293. openType = MAIL_INBOX_NORMAL;
  13294. mailId = 0;
  13295. }else{
  13296. openType = sd->mail.inbox.msg[i].type;
  13297. mailId = 0;
  13298. }
  13299. #endif
  13300. switch( openType ){
  13301. case MAIL_INBOX_NORMAL:
  13302. case MAIL_INBOX_ACCOUNT:
  13303. case MAIL_INBOX_RETURNED:
  13304. break;
  13305. default:
  13306. // Unknown type: ignore request
  13307. return;
  13308. }
  13309. if( sd->mail.changed || ( cmd == 0x9ef || cmd == 0xac1 ) ){
  13310. intif_Mail_requestinbox(sd->status.char_id, 1, (enum mail_inbox_type)openType);
  13311. return;
  13312. }
  13313. // If it is not a next page request
  13314. if( cmd != 0x9ee ){
  13315. mailId = 0;
  13316. }
  13317. clif_Mail_refreshinbox(sd,(enum mail_inbox_type)openType,mailId);
  13318. #endif
  13319. }
  13320. /// Opens a mail
  13321. /// 0242 <packet len>.W <mail id>.L <title>.40B <sender>.24B <time>.L <zeny>.L (ZC_MAIL_REQ_OPEN)
  13322. /// <amount>.L <name id>.W <item type>.W <identified>.B <damaged>.B <refine>.B
  13323. /// <card1>.W <card2>.W <card3>.W <card4>.W <message>.?B
  13324. /// 09eb <packet len>.W <type>.B <mail id>.Q <message length>.W <zeny>.Q <item count>.B (ZC_ACK_READ_MAIL)
  13325. /// { }*n
  13326. // TODO: Packet description => for repeated block
  13327. void clif_Mail_read( map_session_data *sd, int32 mail_id ){
  13328. int32 i, fd = sd->fd;
  13329. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13330. if( i == MAIL_MAX_INBOX ) {
  13331. clif_Mail_return(sd->fd, mail_id, 1); // Mail doesn't exist
  13332. ShowWarning("clif_parse_Mail_read: char '%s' trying to read a message not the inbox.\n", sd->status.name);
  13333. return;
  13334. } else {
  13335. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13336. struct item *item;
  13337. int16 msg_len;
  13338. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13339. if( msg_len == 0 ) {
  13340. strcpy(msg->body, "(no message)"); // TODO: confirm for RODEX
  13341. msg_len = static_cast<decltype(msg_len)>( strlen(msg->body) );
  13342. }
  13343. #if PACKETVER < 20150513
  13344. item = &msg->item[0];
  13345. int16 len = 101 + msg_len;
  13346. WFIFOHEAD(fd,len);
  13347. WFIFOW(fd,0) = 0x242;
  13348. WFIFOW(fd,2) = len;
  13349. WFIFOL(fd,4) = msg->id;
  13350. safestrncpy(WFIFOCP(fd,8), msg->title, MAIL_TITLE_LENGTH + 1);
  13351. safestrncpy(WFIFOCP(fd,48), msg->send_name, NAME_LENGTH + 1);
  13352. WFIFOL(fd,72) = 0;
  13353. WFIFOL(fd,76) = msg->zeny;
  13354. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13355. if( item->nameid && data != nullptr ) {
  13356. WFIFOL(fd,80) = item->amount;
  13357. WFIFOW(fd,84) = client_nameid( item->nameid );
  13358. WFIFOW(fd,86) = itemtype( item->nameid );
  13359. WFIFOB(fd,88) = item->identify;
  13360. WFIFOB(fd,89) = item->attribute;
  13361. WFIFOB(fd,90) = item->refine;
  13362. WFIFOW(fd,91) = item->card[0];
  13363. WFIFOW(fd,93) = item->card[1];
  13364. WFIFOW(fd,95) = item->card[2];
  13365. WFIFOW(fd,97) = item->card[3];
  13366. } else // no item, set all to zero
  13367. memset(WFIFOP(fd,80), 0x00, 19);
  13368. WFIFOB(fd,99) = (unsigned char)msg_len;
  13369. safestrncpy(WFIFOCP(fd,100), msg->body, msg_len + 1);
  13370. WFIFOSET(fd,len);
  13371. #else
  13372. msg_len += 1; // Zero Termination
  13373. PACKET_ZC_ACK_READ_RODEX* p = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX*>( packet_buffer );
  13374. p->PacketType = HEADER_ZC_ACK_READ_RODEX;
  13375. p->PacketLength = sizeof( *p );
  13376. p->opentype = msg->type;
  13377. p->MailID = msg->id;
  13378. p->TextcontentsLength = static_cast<decltype(p->TextcontentsLength)>( msg_len );
  13379. p->zeny = msg->zeny;
  13380. safestrncpy( WBUFCP( p, p->PacketLength ), msg->body, msg_len );
  13381. p->PacketLength += p->TextcontentsLength;
  13382. p->ItemCnt = 0;
  13383. for( int32 j = 0; j < MAIL_MAX_ITEM; j++ ){
  13384. item = &msg->item[j];
  13385. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13386. if( item->nameid > 0 && item->amount > 0 && data != nullptr ){
  13387. PACKET_ZC_ACK_READ_RODEX_SUB* mailitem = reinterpret_cast<PACKET_ZC_ACK_READ_RODEX_SUB*>( WBUFP( p, p->PacketLength ) );
  13388. mailitem->ITID = client_nameid( item->nameid );
  13389. mailitem->count = item->amount;
  13390. mailitem->type = itemtype( item->nameid );
  13391. mailitem->IsIdentified = item->identify ? 1 : 0;
  13392. mailitem->IsDamaged = item->attribute ? 1 : 0;
  13393. mailitem->refiningLevel = item->refine;
  13394. mailitem->location = pc_equippoint_sub( sd, data.get() );
  13395. mailitem->viewSprite = data->look;
  13396. mailitem->bindOnEquip = item->bound ? 2 : data->flag.bindOnEquip ? 1 : 0;
  13397. clif_addcards( &mailitem->slot, item );
  13398. clif_add_random_options( mailitem->option_data, *item );
  13399. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  13400. mailitem->grade = item->enchantgrade;
  13401. #endif
  13402. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( *mailitem ) );
  13403. p->ItemCnt++;
  13404. }
  13405. }
  13406. clif_send( p, p->PacketLength, &sd->bl, SELF );
  13407. #endif
  13408. if (msg->status == MAIL_UNREAD) {
  13409. msg->status = MAIL_READ;
  13410. intif_Mail_read(mail_id);
  13411. clif_parse_Mail_refreshinbox(fd, sd);
  13412. sd->mail.inbox.unread--;
  13413. clif_Mail_new(sd, 0, msg->send_name, msg->title);
  13414. }
  13415. }
  13416. }
  13417. /// Request to open a mail.
  13418. /// 0241 <mail id>.L (CZ_MAIL_OPEN)
  13419. /// 09ea <mail tab>.B <mail id>.Q (CZ_REQ_READ_MAIL)
  13420. void clif_parse_Mail_read(int32 fd, map_session_data *sd){
  13421. #if PACKETVER < 20150513
  13422. int32 mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13423. #else
  13424. //uint8 openType = RFIFOB(fd, 2);
  13425. int32 mail_id = (int32)RFIFOQ(fd, 3);
  13426. #endif
  13427. if( mail_id <= 0 )
  13428. return;
  13429. if( mail_invalid_operation(sd) )
  13430. return;
  13431. clif_Mail_read(sd, mail_id);
  13432. }
  13433. /// Allow a player to begin writing a mail
  13434. /// 0a12 <receiver>.24B <success>.B (ZC_ACK_OPEN_WRITE_MAIL)
  13435. void clif_send_Mail_beginwrite_ack( map_session_data *sd, char* name, bool success ){
  13436. PACKET_ZC_ACK_OPEN_WRITE_MAIL p = { 0 };
  13437. p.PacketType = rodexopenwrite;
  13438. safestrncpy(p.receiveName, name, NAME_LENGTH);
  13439. p.result = success;
  13440. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13441. }
  13442. /// Request to start writing a mail
  13443. /// 0a08 <receiver>.24B (CZ_REQ_OPEN_WRITE_MAIL)
  13444. void clif_parse_Mail_beginwrite( int32 fd, map_session_data *sd ){
  13445. char name[NAME_LENGTH];
  13446. safestrncpy(name, RFIFOCP(fd, 2), NAME_LENGTH);
  13447. if( mail_invalid_operation( sd ) ){
  13448. return;
  13449. }
  13450. if( sd->state.storage_flag || sd->state.mail_writing || sd->state.trading ){
  13451. clif_send_Mail_beginwrite_ack(sd, name, false);
  13452. return;
  13453. }
  13454. mail_clear(sd);
  13455. sd->state.mail_writing = true;
  13456. clif_send_Mail_beginwrite_ack(sd, name,true);
  13457. }
  13458. /// Notification that the client cancelled writing a mail
  13459. /// 0a03 (CZ_REQ_CANCEL_WRITE_MAIL)
  13460. void clif_parse_Mail_cancelwrite( int32 fd, map_session_data *sd ){
  13461. sd->state.mail_writing = false;
  13462. }
  13463. /// Give the client information about the recipient, if available
  13464. /// 0a14 <char id>.L <class>.W <base level>.W (ZC_CHECK_RECEIVE_CHARACTER_NAME)
  13465. /// 0a51 <char id>.L <class>.W <base level>.W <name>.24B (ZC_CHECK_RECEIVE_CHARACTER_NAME2)
  13466. void clif_Mail_Receiver_Ack( map_session_data* sd, uint32 char_id, short class_, uint32 level, const char* name ){
  13467. #if PACKETVER >= 20141119
  13468. PACKET_ZC_CHECKNAME p = { 0 };
  13469. p.PacketType = HEADER_ZC_CHECKNAME;
  13470. p.CharId = char_id;
  13471. p.Class = class_;
  13472. p.BaseLevel = level;
  13473. #if PACKETVER >= 20160302
  13474. strncpy(p.Name, name, NAME_LENGTH);
  13475. #endif
  13476. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13477. #endif
  13478. }
  13479. /// Request information about the recipient
  13480. /// 0a13 <name>.24B (CZ_CHECK_RECEIVE_CHARACTER_NAME)
  13481. void clif_parse_Mail_Receiver_Check(int32 fd, map_session_data *sd) {
  13482. #if PACKETVER >= 20140423
  13483. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13484. const PACKET_CZ_CHECKNAME2* p = reinterpret_cast<PACKET_CZ_CHECKNAME2*>( RFIFOP( fd, 0 ) );
  13485. #else
  13486. const PACKET_CZ_CHECKNAME1* p = reinterpret_cast<PACKET_CZ_CHECKNAME1*>( RFIFOP( fd, 0 ) );
  13487. #endif
  13488. static char name[NAME_LENGTH];
  13489. if( mail_invalid_operation( sd ) ){
  13490. return;
  13491. }
  13492. safestrncpy(name, p->Name, NAME_LENGTH);
  13493. intif_mail_checkreceiver(sd, name);
  13494. #endif
  13495. }
  13496. /// Request to receive mail's attachment.
  13497. /// 0244 <mail id>.L (CZ_MAIL_GET_ITEM)
  13498. /// 09f1 <mail id>.Q <mail tab>.B (CZ_REQ_ZENY_FROM_MAIL)
  13499. /// 09f3 <mail id>.Q <mail tab>.B (CZ_REQ_ITEM_FROM_MAIL)
  13500. void clif_parse_Mail_getattach( int32 fd, map_session_data *sd ){
  13501. int32 i;
  13502. struct mail_message* msg;
  13503. #if PACKETVER < 20150513
  13504. int32 mail_id = RFIFOL(fd, packet_db[RFIFOW(fd, 0)].pos[0]);
  13505. int32 attachment = MAIL_ATT_ALL;
  13506. #else
  13507. uint16 packet_id = RFIFOW(fd, 0);
  13508. int32 mail_id = (int32)RFIFOQ(fd, 2);
  13509. //int32 openType = RFIFOB(fd, 10);
  13510. int32 attachment = packet_id == 0x9f1 ? MAIL_ATT_ZENY : packet_id == 0x9f3 ? MAIL_ATT_ITEM : MAIL_ATT_NONE;
  13511. #endif
  13512. if( !chrif_isconnected() )
  13513. return;
  13514. if( mail_id <= 0 )
  13515. return;
  13516. if( mail_invalid_operation(sd) )
  13517. return;
  13518. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13519. if( i == MAIL_MAX_INBOX )
  13520. return;
  13521. msg = &sd->mail.inbox.msg[i];
  13522. if( attachment&MAIL_ATT_ZENY && msg->zeny < 1 ){
  13523. attachment &= ~MAIL_ATT_ZENY;
  13524. }
  13525. if( attachment&MAIL_ATT_ITEM ){
  13526. ARR_FIND(0, MAIL_MAX_ITEM, i, msg->item[i].nameid > 0 || msg->item[i].amount > 0);
  13527. // No items were found
  13528. if( i == MAIL_MAX_ITEM ){
  13529. attachment &= ~MAIL_ATT_ITEM;
  13530. }
  13531. }
  13532. // Either no attachment requested at all or there are no zeny or items in the mail
  13533. if( attachment == MAIL_ATT_NONE ){
  13534. return;
  13535. }
  13536. if( attachment&MAIL_ATT_ZENY ){
  13537. if( ( msg->zeny + sd->status.zeny + sd->mail.pending_zeny ) > MAX_ZENY ){
  13538. clif_mail_getattachment(sd, msg, 1, MAIL_ATT_ZENY); //too many zeny
  13539. return;
  13540. }else{
  13541. // Store the pending zeny (required for the "retrieve all" feature)
  13542. sd->mail.pending_zeny += msg->zeny;
  13543. // To make sure another request fails
  13544. msg->zeny = 0;
  13545. }
  13546. }
  13547. if( attachment&MAIL_ATT_ITEM ){
  13548. int32 slots = 0, totalweight = 0;
  13549. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  13550. struct item* item = &msg->item[i];
  13551. if( item->nameid > 0 && item->amount > 0 ){
  13552. std::shared_ptr<item_data> data = item_db.find(item->nameid);
  13553. if(data == nullptr)
  13554. continue;
  13555. switch( pc_checkadditem(sd, item->nameid, item->amount) ){
  13556. case CHKADDITEM_NEW:
  13557. slots += data->inventorySlotNeeded( item->amount );
  13558. break;
  13559. case CHKADDITEM_OVERAMOUNT:
  13560. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13561. return;
  13562. }
  13563. totalweight += data->weight * item->amount;
  13564. }
  13565. }
  13566. if( ( totalweight + sd->weight + sd->mail.pending_weight ) > sd->max_weight ){
  13567. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13568. return;
  13569. }else if( pc_inventoryblank(sd) < ( slots + sd->mail.pending_slots ) ){
  13570. clif_mail_getattachment(sd, msg, 2, MAIL_ATT_ITEM);
  13571. return;
  13572. }
  13573. // To make sure another request fails
  13574. memset(msg->item, 0, MAIL_MAX_ITEM*sizeof(struct item));
  13575. // Store the pending weight (required for the "retrieve all" feature)
  13576. sd->mail.pending_weight += totalweight;
  13577. // Store the pending slots (required for the "retrieve all" feature)
  13578. sd->mail.pending_slots += slots;
  13579. }
  13580. intif_mail_getattach(sd,msg, (enum mail_attachment_type)attachment);
  13581. }
  13582. /// Request to delete a mail.
  13583. /// 0243 <mail id>.L (CZ_MAIL_DELETE)
  13584. /// 09f5 <mail tab>.B <mail id>.Q (CZ_REQ_DELETE_MAIL)
  13585. void clif_parse_Mail_delete(int32 fd, map_session_data *sd){
  13586. #if PACKETVER < 20150513
  13587. int32 mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13588. #else
  13589. //int32 openType = RFIFOB(fd, 2);
  13590. int32 mail_id = (int32)RFIFOQ(fd, 3);
  13591. #endif
  13592. int32 i, j;
  13593. if( !chrif_isconnected() )
  13594. return;
  13595. if( mail_id <= 0 )
  13596. return;
  13597. if( mail_invalid_operation(sd) )
  13598. return;
  13599. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13600. if (i < MAIL_MAX_INBOX) {
  13601. struct mail_message *msg = &sd->mail.inbox.msg[i];
  13602. // can't delete mail without removing zeny first
  13603. if( msg->zeny > 0 ){
  13604. clif_mail_delete(sd, msg, false);
  13605. return;
  13606. }
  13607. // can't delete mail without removing attachment(s) first
  13608. for( j = 0; j < MAIL_MAX_ITEM; j++ ){
  13609. if( msg->item[j].nameid > 0 && msg->item[j].amount > 0 ){
  13610. clif_mail_delete(sd, msg, false);
  13611. return;
  13612. }
  13613. }
  13614. if( intif_Mail_delete(sd->status.char_id, mail_id) && msg->status == MAIL_UNREAD ){
  13615. sd->mail.inbox.unread--;
  13616. clif_Mail_new(sd,0,nullptr,nullptr);
  13617. }
  13618. }
  13619. }
  13620. /// Request to return a mail.
  13621. /// 0273 <mail id>.L <receive name>.24B (CZ_REQ_MAIL_RETURN)
  13622. void clif_parse_Mail_return(int32 fd, map_session_data *sd){
  13623. #if PACKETVER_MAIN_NUM >= 20201104 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20201118
  13624. const PACKET_CZ_RODEX_RETURN* p = reinterpret_cast<PACKET_CZ_RODEX_RETURN*>( RFIFOP( fd, 0 ) );
  13625. //ShowDump( p, sizeof( p ) );
  13626. int32 mail_id = p->msgId;
  13627. // not supported for now
  13628. return;
  13629. #else
  13630. int32 mail_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13631. #endif
  13632. //char *rec_name = RFIFOP(fd,packet_db[RFIFOW(fd,0)].pos[1]);
  13633. int32 i;
  13634. if( mail_id <= 0 )
  13635. return;
  13636. if( mail_invalid_operation(sd) )
  13637. return;
  13638. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  13639. if( i < MAIL_MAX_INBOX && sd->mail.inbox.msg[i].send_id != 0 )
  13640. intif_Mail_return(sd->status.char_id, mail_id);
  13641. else
  13642. clif_Mail_return(sd->fd, mail_id, 1);
  13643. }
  13644. /// Request to add an item or Zeny to mail.
  13645. /// 0247 <index>.W <amount>.L (CZ_MAIL_ADD_ITEM)
  13646. /// 0a04 <index>.W <amount>.W (CZ_REQ_ADD_ITEM_TO_MAIL)
  13647. void clif_parse_Mail_setattach(int32 fd, map_session_data *sd){
  13648. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13649. uint16 idx = RFIFOW(fd,info->pos[0]);
  13650. #if PACKETVER < 20150513
  13651. int32 amount = RFIFOL(fd,info->pos[1]);
  13652. #else
  13653. int32 amount = RFIFOW(fd,info->pos[1]);
  13654. #endif
  13655. unsigned char flag;
  13656. if( !chrif_isconnected() )
  13657. return;
  13658. if (amount < 0 || server_index(idx) >= MAX_INVENTORY)
  13659. return;
  13660. if (sd->inventory_data[server_index(idx)] == nullptr)
  13661. return;
  13662. if( mail_invalid_operation( sd ) ){
  13663. return;
  13664. }
  13665. flag = mail_setitem(sd, idx, amount);
  13666. if( flag == MAIL_ATTACH_EQUIPSWITCH ){
  13667. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  13668. }else{
  13669. clif_Mail_setattachment(sd,idx,amount,flag);
  13670. }
  13671. }
  13672. /// Remove an item from a mail
  13673. /// 0a07 <result>.B <index>.W <amount>.W <weight>.W
  13674. void clif_mail_removeitem( map_session_data* sd, bool success, int32 index, int32 amount ){
  13675. PACKET_ZC_ACK_REMOVE_ITEM_MAIL p = { 0 };
  13676. p.PacketType = rodexremoveitem;
  13677. p.result = success;
  13678. p.index = index;
  13679. p.cnt = amount;
  13680. int32 total = 0;
  13681. for( int32 i = 0; i < MAIL_MAX_ITEM; i++ ){
  13682. if( sd->mail.item[i].nameid == 0 ){
  13683. break;
  13684. }
  13685. total += sd->mail.item[i].amount * ( sd->inventory_data[sd->mail.item[i].index]->weight / 10 );
  13686. }
  13687. p.weight = total;
  13688. clif_send(&p, sizeof(p), &sd->bl, SELF);
  13689. }
  13690. /// Request to reset mail item and/or Zeny
  13691. /// 0246 <type>.W (CZ_MAIL_RESET_ITEM)
  13692. /// type:
  13693. /// 0 = reset all
  13694. /// 1 = remove item
  13695. /// 2 = remove zeny
  13696. /// 0a06 <index>.W <amount>.W (CZ_REQ_REMOVE_ITEM_MAIL)
  13697. void clif_parse_Mail_winopen(int32 fd, map_session_data *sd)
  13698. {
  13699. #if PACKETVER < 20150513
  13700. int32 type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13701. if (type == 0 || type == 1)
  13702. mail_removeitem(sd, 0, sd->mail.item[0].index, sd->mail.item[0].amount);
  13703. if (type == 0 || type == 2)
  13704. mail_removezeny(sd, false);
  13705. #else
  13706. uint16 index = RFIFOW(fd, 2);
  13707. uint16 count = RFIFOW(fd, 4);
  13708. mail_removeitem(sd,0,index,count);
  13709. #endif
  13710. }
  13711. /// Request to send mail
  13712. /// 0248 <packet len>.W <recipient>.24B <title>.40B <body len>.B <body>.?B (CZ_MAIL_SEND)
  13713. /// 09ec <packet len>.W <recipient>.24B <sender>.24B <zeny>.Q <title length>.W <body length>.W <title>.?B <body>.?B (CZ_REQ_WRITE_MAIL)
  13714. /// 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)
  13715. void clif_parse_Mail_send(int32 fd, map_session_data *sd){
  13716. #if PACKETVER < 20150513
  13717. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13718. if( !chrif_isconnected() )
  13719. return;
  13720. if( RFIFOW(fd,info->pos[0]) < 69 ) {
  13721. ShowWarning("Invalid Msg Len from account %d.\n", sd->status.account_id);
  13722. return;
  13723. }
  13724. mail_send(sd, RFIFOCP(fd,info->pos[1]), RFIFOCP(fd,info->pos[2]), RFIFOCP(fd,info->pos[4]), RFIFOB(fd,info->pos[3]));
  13725. #else
  13726. uint16 length = RFIFOW(fd, 2);
  13727. if( length < 0x3e ){
  13728. ShowWarning("Too short...\n");
  13729. clif_Mail_send(sd, WRITE_MAIL_FAILED);
  13730. return;
  13731. }
  13732. if( mail_invalid_operation( sd ) ){
  13733. return;
  13734. }
  13735. // Forged request without a begin writing packet?
  13736. if( !sd->state.mail_writing ){
  13737. return; // Ignore it
  13738. }
  13739. char receiver[NAME_LENGTH];
  13740. safestrncpy(receiver, RFIFOCP(fd, 4), NAME_LENGTH);
  13741. // char sender[NAME_LENGTH];
  13742. // safestrncpy(sender, RFIFOCP(fd, 28), NAME_LENGTH);
  13743. uint64 zeny = RFIFOQ(fd, 52);
  13744. uint16 titleLength = RFIFOW(fd, 60);
  13745. uint16 textLength = RFIFOW(fd, 62);
  13746. uint16 realTitleLength = min(titleLength, MAIL_TITLE_LENGTH);
  13747. uint16 realTextLength = min(textLength, MAIL_BODY_LENGTH);
  13748. char title[MAIL_TITLE_LENGTH];
  13749. char text[MAIL_BODY_LENGTH];
  13750. #if PACKETVER <= 20160330
  13751. safestrncpy(title, RFIFOCP(fd, 64), realTitleLength);
  13752. safestrncpy(text, RFIFOCP(fd, 64 + titleLength), realTextLength);
  13753. #else
  13754. // 64 = <char id>.L
  13755. safestrncpy(title, RFIFOCP(fd, 68), realTitleLength);
  13756. safestrncpy(text, RFIFOCP(fd, 68 + titleLength), realTextLength);
  13757. #endif
  13758. if( zeny > 0 ){
  13759. if( mail_setitem(sd,0,(uint32)zeny) != MAIL_ATTACH_SUCCESS ){
  13760. clif_Mail_send(sd,WRITE_MAIL_FAILED);
  13761. return;
  13762. }
  13763. }
  13764. mail_send(sd, receiver, title, text, realTextLength);
  13765. #endif
  13766. }
  13767. /// AUCTION SYSTEM
  13768. /// By Zephyrus
  13769. ///
  13770. /// Opens/closes the auction window (ZC_AUCTION_WINDOWS).
  13771. /// 025f <type>.L
  13772. /// type:
  13773. /// 0 = open
  13774. /// 1 = close
  13775. void clif_Auction_openwindow(map_session_data *sd)
  13776. {
  13777. int32 fd = sd->fd;
  13778. if( sd->state.storage_flag || sd->state.vending || sd->state.buyingstore || sd->state.trading )
  13779. return;
  13780. if( !battle_config.feature_auction )
  13781. return;
  13782. WFIFOHEAD(fd,packet_len(0x25f));
  13783. WFIFOW(fd,0) = 0x25f;
  13784. WFIFOL(fd,2) = 0;
  13785. WFIFOSET(fd,packet_len(0x25f));
  13786. }
  13787. /// Returns auction item search results (ZC_AUCTION_ITEM_REQ_SEARCH).
  13788. /// 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 }*
  13789. void clif_Auction_results(map_session_data *sd, short count, short pages, uint8 *buf)
  13790. {
  13791. int32 i, fd = sd->fd, len = sizeof(struct auction_data);
  13792. struct auction_data auction;
  13793. WFIFOHEAD(fd,12 + (count * 83));
  13794. WFIFOW(fd,0) = 0x252;
  13795. WFIFOW(fd,2) = 12 + (count * 83);
  13796. WFIFOL(fd,4) = pages;
  13797. WFIFOL(fd,8) = count;
  13798. for( i = 0; i < count; i++ ) {
  13799. int32 k = 12 + (i * 83);
  13800. memcpy(&auction, RBUFP(buf,i * len), len);
  13801. WFIFOL(fd,k) = auction.auction_id;
  13802. safestrncpy(WFIFOCP(fd,4+k), auction.seller_name, NAME_LENGTH);
  13803. WFIFOW(fd,28+k) = client_nameid( auction.item.nameid );
  13804. WFIFOL(fd,30+k) = auction.type;
  13805. WFIFOW(fd,34+k) = auction.item.amount; // Always 1
  13806. WFIFOB(fd,36+k) = auction.item.identify;
  13807. WFIFOB(fd,37+k) = auction.item.attribute;
  13808. WFIFOB(fd,38+k) = auction.item.refine;
  13809. WFIFOW(fd,39+k) = auction.item.card[0];
  13810. WFIFOW(fd,41+k) = auction.item.card[1];
  13811. WFIFOW(fd,43+k) = auction.item.card[2];
  13812. WFIFOW(fd,45+k) = auction.item.card[3];
  13813. WFIFOL(fd,47+k) = auction.price;
  13814. WFIFOL(fd,51+k) = auction.buynow;
  13815. safestrncpy(WFIFOCP(fd,55+k), auction.buyer_name, NAME_LENGTH);
  13816. WFIFOL(fd,79+k) = (uint32)auction.timestamp;
  13817. }
  13818. WFIFOSET(fd,WFIFOW(fd,2));
  13819. }
  13820. /// Result from request to add an item (ZC_ACK_AUCTION_ADD_ITEM).
  13821. /// 0256 <index>.W <result>.B
  13822. /// result:
  13823. /// 0 = success
  13824. /// 1 = failure
  13825. static void clif_Auction_setitem(int32 fd, int32 index, bool fail)
  13826. {
  13827. WFIFOHEAD(fd,packet_len(0x256));
  13828. WFIFOW(fd,0) = 0x256;
  13829. WFIFOW(fd,2) = index;
  13830. WFIFOB(fd,4) = fail;
  13831. WFIFOSET(fd,packet_len(0x256));
  13832. }
  13833. /// Request to initialize 'new auction' data (CZ_AUCTION_CREATE).
  13834. /// 024b <type>.W
  13835. /// type:
  13836. /// 0 = create (any other action in auction window)
  13837. /// 1 = cancel (cancel pressed on register tab)
  13838. /// ? = junk, uninitialized value (ex. when switching between list filters)
  13839. void clif_parse_Auction_cancelreg(int32 fd, map_session_data *sd){
  13840. //int32 type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13841. if( sd->auction.amount > 0 )
  13842. clif_additem(sd, sd->auction.index, sd->auction.amount, 0);
  13843. sd->auction.amount = 0;
  13844. }
  13845. /// Request to add an item to the action (CZ_AUCTION_ADD_ITEM).
  13846. /// 024c <index>.W <count>.L
  13847. void clif_parse_Auction_setitem(int32 fd, map_session_data *sd){
  13848. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  13849. int32 idx = RFIFOW(fd,info->pos[0]) - 2;
  13850. int32 amount = RFIFOL(fd,info->pos[1]); // Always 1
  13851. if( !battle_config.feature_auction )
  13852. return;
  13853. if( sd->auction.amount > 0 )
  13854. sd->auction.amount = 0;
  13855. if( idx < 0 || idx >= MAX_INVENTORY ) {
  13856. ShowWarning("Character %s trying to set invalid item index in auctions.\n", sd->status.name);
  13857. return;
  13858. }
  13859. 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.
  13860. ShowWarning("Character %s trying to set invalid amount in auctions.\n", sd->status.name);
  13861. return;
  13862. }
  13863. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[idx].nameid);
  13864. 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) )
  13865. { // Consumable or pets are not allowed
  13866. clif_Auction_setitem(sd->fd, idx, true);
  13867. return;
  13868. }
  13869. if( !pc_can_give_items(sd) || sd->inventory.u.items_inventory[idx].expire_time ||
  13870. !sd->inventory.u.items_inventory[idx].identify ||
  13871. (sd->inventory.u.items_inventory[idx].bound && !pc_can_give_bounded_items(sd)) ||
  13872. !itemdb_available(sd->inventory.u.items_inventory[idx].nameid) ||
  13873. !itemdb_canauction(&sd->inventory.u.items_inventory[idx],pc_get_group_level(sd)) ) { // Quest Item or something else
  13874. clif_Auction_setitem(sd->fd, idx, true);
  13875. return;
  13876. }
  13877. sd->auction.index = idx;
  13878. sd->auction.amount = amount;
  13879. clif_Auction_setitem(fd, idx + 2, false);
  13880. }
  13881. /// Result from an auction action (ZC_AUCTION_RESULT).
  13882. /// 0250 <result>.B
  13883. /// result:
  13884. /// 0 = You have failed to bid into the auction
  13885. /// 1 = You have successfully bid in the auction
  13886. /// 2 = The auction has been canceled
  13887. /// 3 = An auction with at least one bidder cannot be canceled
  13888. /// 4 = You cannot register more than 5 items in an auction at a time
  13889. /// 5 = You do not have enough Zeny to pay the Auction Fee
  13890. /// 6 = You have won the auction
  13891. /// 7 = You have failed to win the auction
  13892. /// 8 = You do not have enough Zeny
  13893. /// 9 = You cannot place more than 5 bids at a time
  13894. void clif_Auction_message(int32 fd, unsigned char flag)
  13895. {
  13896. WFIFOHEAD(fd,packet_len(0x250));
  13897. WFIFOW(fd,0) = 0x250;
  13898. WFIFOB(fd,2) = flag;
  13899. WFIFOSET(fd,packet_len(0x250));
  13900. }
  13901. /// Result of the auction close request (ZC_AUCTION_ACK_MY_SELL_STOP).
  13902. /// 025e <result>.W
  13903. /// result:
  13904. /// 0 = You have ended the auction
  13905. /// 1 = You cannot end the auction
  13906. /// 2 = Auction ID is incorrect
  13907. void clif_Auction_close(int32 fd, unsigned char flag)
  13908. {
  13909. WFIFOHEAD(fd,packet_len(0x25e));
  13910. WFIFOW(fd,0) = 0x25d; // BUG: The client identifies this packet as 0x25d (CZ_AUCTION_REQ_MY_SELL_STOP)
  13911. WFIFOW(fd,2) = flag;
  13912. WFIFOSET(fd,packet_len(0x25e));
  13913. }
  13914. /// Request to add an auction.
  13915. /// 024d <now money>.L <max money>.L <delete hour>.W (CZ_AUCTION_ADD)
  13916. void clif_parse_Auction_register( int32 fd, map_session_data* sd ){
  13917. #if PACKETVER >= 20050808
  13918. const PACKET_CZ_AUCTION_ADD* p = reinterpret_cast<PACKET_CZ_AUCTION_ADD*>( RFIFOP( fd, 0 ) );
  13919. struct auction_data auction;
  13920. if( !battle_config.feature_auction )
  13921. return;
  13922. auction.price = p->now_money;
  13923. auction.buynow = p->max_money;
  13924. auction.hours = p->hours;
  13925. // Invalid Situations...
  13926. if( sd->auction.amount < 1 ) {
  13927. ShowWarning("Character %s trying to register auction without item.\n", sd->status.name);
  13928. return;
  13929. }
  13930. if( auction.price >= auction.buynow ) {
  13931. ShowWarning("Character %s trying to alter auction prices.\n", sd->status.name);
  13932. return;
  13933. }
  13934. if( auction.hours < 1 || auction.hours > 48 ) {
  13935. ShowWarning("Character %s trying to enter an invalid time for auction.\n", sd->status.name);
  13936. return;
  13937. }
  13938. // Auction checks...
  13939. if( sd->inventory.u.items_inventory[sd->auction.index].bound && !pc_can_give_bounded_items(sd) ) {
  13940. clif_displaymessage(sd->fd, msg_txt(sd,293));
  13941. clif_Auction_message(fd, 2); // The auction has been canceled
  13942. return;
  13943. }
  13944. if( sd->status.zeny < (auction.hours * battle_config.auction_feeperhour) ) {
  13945. clif_Auction_message(fd, 5); // You do not have enough zeny to pay the Auction Fee.
  13946. return;
  13947. }
  13948. if( auction.buynow > battle_config.auction_maximumprice ) { // Zeny Limits
  13949. auction.buynow = battle_config.auction_maximumprice;
  13950. if( auction.price >= auction.buynow )
  13951. auction.price = auction.buynow - 1;
  13952. }
  13953. auction.auction_id = 0;
  13954. auction.seller_id = sd->status.char_id;
  13955. safestrncpy(auction.seller_name, sd->status.name, sizeof(auction.seller_name));
  13956. auction.buyer_id = 0;
  13957. memset(auction.buyer_name, '\0', sizeof(auction.buyer_name));
  13958. if( sd->inventory.u.items_inventory[sd->auction.index].nameid == 0 || sd->inventory.u.items_inventory[sd->auction.index].amount < sd->auction.amount )
  13959. {
  13960. clif_Auction_message(fd, 2); // The auction has been canceled
  13961. return;
  13962. }
  13963. std::shared_ptr<item_data> id = item_db.find(sd->inventory.u.items_inventory[sd->auction.index].nameid);
  13964. if( id == nullptr )
  13965. { // Just in case
  13966. clif_Auction_message(fd, 2); // The auction has been canceled
  13967. return;
  13968. }
  13969. safestrncpy(auction.item_name, id->ename.c_str(), sizeof(auction.item_name));
  13970. auction.type = id->type;
  13971. memcpy(&auction.item, &sd->inventory.u.items_inventory[sd->auction.index], sizeof(struct item));
  13972. auction.item.amount = 1;
  13973. auction.timestamp = 0;
  13974. if( !intif_Auction_register(&auction) )
  13975. clif_Auction_message(fd, 4); // No Char Server? lets say something to the client
  13976. else
  13977. {
  13978. int32 zeny = auction.hours*battle_config.auction_feeperhour;
  13979. pc_delitem(sd, sd->auction.index, sd->auction.amount, 1, 6, LOG_TYPE_AUCTION);
  13980. sd->auction.amount = 0;
  13981. pc_payzeny(sd, zeny, LOG_TYPE_AUCTION);
  13982. }
  13983. #endif
  13984. }
  13985. /// Cancels an auction (CZ_AUCTION_ADD_CANCEL).
  13986. /// 024e <auction id>.L
  13987. void clif_parse_Auction_cancel(int32 fd, map_session_data *sd){
  13988. uint32 auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13989. intif_Auction_cancel(sd->status.char_id, auction_id);
  13990. }
  13991. /// Closes an auction (CZ_AUCTION_REQ_MY_SELL_STOP).
  13992. /// 025d <auction id>.L
  13993. void clif_parse_Auction_close(int32 fd, map_session_data *sd){
  13994. uint32 auction_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  13995. intif_Auction_close(sd->status.char_id, auction_id);
  13996. }
  13997. /// Places a bid on an auction.
  13998. /// 024f <auction id>.L <money>.L (CZ_AUCTION_BUY)
  13999. void clif_parse_Auction_bid( int32 fd, map_session_data* sd ){
  14000. #if PACKETVER >= 20050718
  14001. const PACKET_CZ_AUCTION_BUY* p = reinterpret_cast<PACKET_CZ_AUCTION_BUY*>( RFIFOP( fd, 0 ) );
  14002. if( !pc_can_give_items(sd) ){
  14003. clif_displaymessage( sd->fd, msg_txt( sd, 246 ) ); // Your GM level doesn't authorize you to perform this action.
  14004. return;
  14005. }
  14006. // Check if char server is down (bugreport:1138)
  14007. if( CheckForCharServer() ){
  14008. clif_Auction_message(fd, 0); // You have failed to bid into the auction
  14009. return;
  14010. }
  14011. if( pc_payzeny( sd, p->money, LOG_TYPE_AUCTION ) != 0 ){
  14012. clif_Auction_message( fd, 8 ); // You do not have enough zeny
  14013. return;
  14014. }
  14015. intif_Auction_bid( sd->status.char_id, sd->status.name, p->auction_id, p->money );
  14016. #endif
  14017. }
  14018. /// Auction Search.
  14019. /// 0251 <search type>.W <auction id>.L <search text>.24B <page number>.W (CZ_AUCTION_ITEM_SEARCH)
  14020. /// search type:
  14021. /// 0 = armor
  14022. /// 1 = weapon
  14023. /// 2 = card
  14024. /// 3 = misc
  14025. /// 4 = name search
  14026. /// 5 = auction id search
  14027. void clif_parse_Auction_search(int32 fd, map_session_data* sd){
  14028. #if PACKETVER >= 20051107
  14029. const PACKET_CZ_AUCTION_ITEM_SEARCH* p = reinterpret_cast<PACKET_CZ_AUCTION_ITEM_SEARCH*>( RFIFOP( fd, 0 ) );
  14030. if( !battle_config.feature_auction )
  14031. return;
  14032. clif_parse_Auction_cancelreg(fd, sd);
  14033. char search_text[NAME_LENGTH];
  14034. safestrncpy( search_text, p->text, sizeof( search_text ) );
  14035. intif_Auction_requestlist( sd->status.char_id, p->type, p->auction_id, search_text, p->page );
  14036. #endif
  14037. }
  14038. /// Requests list of own currently active bids or auctions (CZ_AUCTION_REQ_MY_INFO).
  14039. /// 025c <type>.W
  14040. /// type:
  14041. /// 0 = sell (own auctions)
  14042. /// 1 = buy (own bids)
  14043. void clif_parse_Auction_buysell(int32 fd, map_session_data* sd)
  14044. {
  14045. short type = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) + 6;
  14046. if( !battle_config.feature_auction )
  14047. return;
  14048. clif_parse_Auction_cancelreg(fd, sd);
  14049. intif_Auction_requestlist(sd->status.char_id, type, 0, "", 1);
  14050. }
  14051. /// CASH/POINT SHOP
  14052. ///
  14053. void clif_cashshop_open( map_session_data* sd, int32 tab ){
  14054. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14055. nullpo_retv( sd );
  14056. PACKET_ZC_SE_CASHSHOP_OPEN p = {};
  14057. p.packetType = HEADER_ZC_SE_CASHSHOP_OPEN;
  14058. p.cashPoints = sd->cashPoints;
  14059. p.kafraPoints = sd->kafraPoints;
  14060. #if PACKETVER_MAIN_NUM >= 20200129 || PACKETVER_RE_NUM >= 20200205 || PACKETVER_ZERO_NUM >= 20191224
  14061. p.tab = tab;
  14062. #endif
  14063. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14064. #endif
  14065. }
  14066. void clif_parse_cashshop_open_request( int32 fd, map_session_data* sd ){
  14067. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14068. nullpo_retv( sd );
  14069. int32 tab = 0;
  14070. #if PACKETVER >= 20191224
  14071. const PACKET_CZ_SE_CASHSHOP_OPEN2* p = reinterpret_cast<PACKET_CZ_SE_CASHSHOP_OPEN2*>( RFIFOP( fd, 0 ) );
  14072. tab = p->tab;
  14073. #endif
  14074. if (map_getmapflag(sd->bl.m, MF_NOCASHSHOP)) {
  14075. clif_displaymessage(fd, msg_txt(sd, 451)); // Cash Shop is disabled on this map.
  14076. return;
  14077. }
  14078. sd->state.cashshop_open = true;
  14079. sd->npc_shopid = -1; // Set npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14080. clif_cashshop_open( sd, tab );
  14081. #endif
  14082. }
  14083. void clif_parse_cashshop_close( int32 fd, map_session_data* sd ){
  14084. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined(PACKETVER_ZERO)
  14085. sd->state.cashshop_open = false;
  14086. sd->npc_shopid = 0; // Reset npc_shopid when using cash shop from "cash shop" button [Aelys|Susu] bugreport:96
  14087. // No need to do anything here
  14088. #endif
  14089. }
  14090. //0846 <tabid>.W (CZ_REQ_SE_CASH_TAB_CODE))
  14091. //08c0 <len>.W <openIdentity>.L <itemcount>.W (ZC_ACK_SE_CASH_ITEM_LIST2)
  14092. void clif_parse_CashShopReqTab(int32 fd, map_session_data *sd) {
  14093. #if PACKETVER_MAIN_NUM >= 20100824 || PACKETVER_RE_NUM >= 20100824 || defined(PACKETVER_ZERO)
  14094. const PACKET_CZ_REQ_SE_CASH_TAB_CODE* p_in = reinterpret_cast<PACKET_CZ_REQ_SE_CASH_TAB_CODE*>( RFIFOP( fd, 0 ) );
  14095. // TODO: most likely wrong answer packet. Most likely HEADER_ZC_ACK_SE_CASH_ITEM_LIST (0x847) would be correct [Lemongrass]
  14096. // [4144] packet exists only in 2011 and was dropped after
  14097. #if PACKETVER >= 20110222 && PACKETVER < 20120000
  14098. std::shared_ptr<s_cash_item_tab> tab = cash_shop_db.find( static_cast<e_cash_shop_tab>( p_in->tab ) );
  14099. if( tab == nullptr ){
  14100. return;
  14101. }
  14102. // Skip empty tabs, the client only expects filled ones
  14103. if( tab->items.empty() ){
  14104. return;
  14105. }
  14106. #if !(PACKETVER_SUPPORTS_SALES)
  14107. if( tab->tab == CASHSHOP_TAB_SALE ){
  14108. return;
  14109. }
  14110. #endif
  14111. PACKET_ZC_ACK_SE_CASH_ITEM_LIST2* p = reinterpret_cast<PACKET_ZC_ACK_SE_CASH_ITEM_LIST2*>( packet_buffer );
  14112. p->packetType = HEADER_ZC_ACK_SE_CASH_ITEM_LIST2;
  14113. p->packetLength = sizeof( *p );
  14114. p->tab = tab->tab;
  14115. p->count = 0;
  14116. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14117. p->items[p->count].itemId = client_nameid( item->nameid );
  14118. p->items[p->count].price = item->price;
  14119. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14120. p->count++;
  14121. }
  14122. clif_send( p, p->packetLength, &sd->bl, SELF );
  14123. #endif
  14124. #endif
  14125. }
  14126. //08ca <len>.W <itemcount> W <tabcode>.W (ZC_ACK_SCHEDULER_CASHITEM)
  14127. void clif_cashshop_list( map_session_data& sd ){
  14128. for( const auto& pair : cash_shop_db ){
  14129. std::shared_ptr<s_cash_item_tab> tab = pair.second;
  14130. // Skip empty tabs, the client only expects filled ones
  14131. if( tab->items.empty() ){
  14132. continue;
  14133. }
  14134. #if !(PACKETVER_SUPPORTS_SALES)
  14135. if( tab->tab == CASHSHOP_TAB_SALE ){
  14136. continue;
  14137. }
  14138. #endif
  14139. PACKET_ZC_ACK_SCHEDULER_CASHITEM* p = reinterpret_cast<PACKET_ZC_ACK_SCHEDULER_CASHITEM*>( packet_buffer );
  14140. p->packetType = HEADER_ZC_ACK_SCHEDULER_CASHITEM;
  14141. p->packetLength = sizeof( *p );
  14142. p->count = 0;
  14143. p->tabNum = tab->tab;
  14144. for( std::shared_ptr<s_cash_item> item : tab->items ){
  14145. p->items[p->count].itemId = client_nameid( item->nameid );
  14146. p->items[p->count].price = item->price;
  14147. #ifdef ENABLE_CASHSHOP_PREVIEW_PATCH
  14148. struct item_data* id = itemdb_search( item->nameid );
  14149. if( id == nullptr ){
  14150. p->items[p->count].location = 0;
  14151. p->items[p->count].viewSprite = 0;
  14152. }else{
  14153. p->items[p->count].location = pc_equippoint_sub( &sd, id );
  14154. p->items[p->count].viewSprite = id->look;
  14155. }
  14156. #endif
  14157. p->count++;
  14158. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14159. if( ( static_cast<size_t>( p->packetLength ) + sizeof( p->items[0] ) ) >= INT16_MAX ){
  14160. // Send current data
  14161. clif_send( p, p->packetLength, &sd.bl, SELF );
  14162. // Start a new packet
  14163. p->count = 0;
  14164. p->packetLength = sizeof( *p );
  14165. }
  14166. }
  14167. clif_send( p, p->packetLength, &sd.bl, SELF );
  14168. }
  14169. }
  14170. void clif_parse_cashshop_list_request( int32 fd, map_session_data* sd ){
  14171. if( !sd->status.cashshop_sent ) {
  14172. clif_cashshop_list( *sd );
  14173. #if PACKETVER_SUPPORTS_SALES
  14174. sale_notify_login(sd);
  14175. #endif
  14176. sd->status.cashshop_sent = true;
  14177. }
  14178. }
  14179. /// List of items offered in a cash shop (ZC_PC_CASH_POINT_ITEMLIST).
  14180. /// 0287 <packet len>.W <cash point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }*
  14181. /// 0287 <packet len>.W <cash point>.L <kafra point>.L { <sell price>.L <discount price>.L <item type>.B <name id>.W }* (PACKETVER >= 20070711)
  14182. void clif_cashshop_show( map_session_data& sd, npc_data& nd ){
  14183. sd.npc_shopid = nd.bl.id;
  14184. int32 cost[2] = { 0, 0 };
  14185. npc_shop_currency_type( &sd, &nd, cost, true );
  14186. PACKET_ZC_PC_CASH_POINT_ITEMLIST* p = reinterpret_cast<PACKET_ZC_PC_CASH_POINT_ITEMLIST*>( packet_buffer );
  14187. p->packetType = HEADER_ZC_PC_CASH_POINT_ITEMLIST;
  14188. p->packetLength = sizeof( *p );
  14189. p->cashPoints = cost[0];
  14190. #if PACKETVER >= 20070711
  14191. p->kafraPoints = cost[1];
  14192. #endif
  14193. for( int32 i = 0; i < nd.u.shop.count; i++ ) {
  14194. struct item_data* id = itemdb_search( nd.u.shop.shop_item[i].nameid );
  14195. p->items[i].price = nd.u.shop.shop_item[i].value;
  14196. p->items[i].discountPrice = nd.u.shop.shop_item[i].value; // Discount Price
  14197. p->items[i].itemType = itemtype( id->nameid );
  14198. p->items[i].itemId = client_nameid( id->nameid );
  14199. #ifdef ENABLE_OLD_CASHSHOP_PREVIEW_PATCH
  14200. p->items[i].location = pc_equippoint_sub( &sd, id );
  14201. p->items[i].viewSprite = id->look;
  14202. memset( p->items[i].unused, 0, sizeof( p->items[i].unused ) );
  14203. #endif
  14204. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  14205. }
  14206. clif_send( p, p->packetLength, &sd.bl, SELF );
  14207. }
  14208. /// Cashshop Buy Ack (ZC_PC_CASH_POINT_UPDATE).
  14209. /// 0289 <cash point>.L <error>.W
  14210. /// 0289 <cash point>.L <kafra point>.L <error>.W (PACKETVER >= 20070711)
  14211. /// error:
  14212. /// 0 = The deal has successfully completed. (ERROR_TYPE_NONE)
  14213. /// 1 = The Purchase has failed because the NPC does not exist. (ERROR_TYPE_NPC)
  14214. /// 2 = The Purchase has failed because the Kafra Shop System is not working correctly. (ERROR_TYPE_SYSTEM)
  14215. /// 3 = You are over your Weight Limit. (ERROR_TYPE_INVENTORY_WEIGHT)
  14216. /// 4 = You cannot purchase items while you are in a trade. (ERROR_TYPE_EXCHANGE)
  14217. /// 5 = The Purchase has failed because the Item Information was incorrect. (ERROR_TYPE_ITEM_ID)
  14218. /// 6 = You do not have enough Kafra Credit Points. (ERROR_TYPE_MONEY)
  14219. /// 7 = You can purchase up to 10 items. (ERROR_TYPE_AMOUNT)
  14220. /// 8 = Some items could not be purchased. (ERROR_TYPE_PURCHASE_FAIL)
  14221. void clif_cashshop_ack(map_session_data* sd, int32 error)
  14222. {
  14223. int32 fd, cost[2] = { 0, 0 };
  14224. struct npc_data *nd;
  14225. nullpo_retv(sd);
  14226. fd = sd->fd;
  14227. nd = map_id2nd(sd->npc_shopid);
  14228. npc_shop_currency_type(sd, nd, cost, false);
  14229. WFIFOHEAD(fd, packet_len(0x289));
  14230. WFIFOW(fd,0) = 0x289;
  14231. WFIFOL(fd,2) = cost[0];
  14232. #if PACKETVER < 20070711
  14233. WFIFOW(fd,6) = TOW(error);
  14234. #else
  14235. WFIFOL(fd,6) = cost[1];
  14236. WFIFOW(fd,10) = TOW(error);
  14237. #endif
  14238. WFIFOSET(fd, packet_len(0x289));
  14239. }
  14240. void clif_cashshop_result( map_session_data *sd, t_itemid item_id, uint16 result ){
  14241. #if PACKETVER_MAIN_NUM >= 20101123 || PACKETVER_RE_NUM >= 20120328 || defined( PACKETVER_ZERO )
  14242. nullpo_retv( sd );
  14243. PACKET_ZC_SE_PC_BUY_CASHITEM_RESULT packet = {};
  14244. packet.packetType = HEADER_ZC_SE_PC_BUY_CASHITEM_RESULT;
  14245. if( item_id != 0 ){
  14246. packet.itemId = client_nameid( item_id );
  14247. }else{
  14248. packet.itemId = 0;
  14249. }
  14250. packet.result = result;
  14251. packet.cashPoints = sd->cashPoints;
  14252. packet.kafraPoints = sd->kafraPoints;
  14253. clif_send( &packet, sizeof( packet ), &sd->bl, SELF );
  14254. #endif
  14255. }
  14256. /// Request to buy item(s) from cash shop.
  14257. /// 0288 <name id>.W <amount>.W (CZ_PC_BUY_CASH_POINT_ITEM)
  14258. /// 0288 <name id>.W <amount>.W <kafra points>.L (PACKETVER >= 20070711)
  14259. /// 0288 <packet len>.W <kafra points>.L <count>.W { <amount>.W <name id>.W }.4B*count (PACKETVER >= 20100803)
  14260. void clif_parse_npccashshop_buy( int32 fd, map_session_data *sd ){
  14261. nullpo_retv( sd );
  14262. if( sd->state.trading || !sd->npc_shopid ){
  14263. clif_cashshop_ack( sd, 1 );
  14264. return;
  14265. }
  14266. const PACKET_CZ_PC_BUY_CASH_POINT_ITEM* p = reinterpret_cast<PACKET_CZ_PC_BUY_CASH_POINT_ITEM*>( RFIFOP( fd, 0 ) );
  14267. #if PACKETVER < 20070711
  14268. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, 0 ) );
  14269. #elif PACKETVER < 20101116
  14270. clif_cashshop_ack( sd, npc_cashshop_buy( sd, p->itemId, p->amount, p->kafraPoints ) );
  14271. #else
  14272. int32 s_itl = sizeof( p->items[0] );
  14273. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14274. 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 );
  14275. return;
  14276. }
  14277. std::vector<s_npc_buy_list> item_list = {};
  14278. item_list.reserve( p->count );
  14279. for( int32 i = 0; i < p->count; i++ ){
  14280. s_npc_buy_list item = {};
  14281. item.nameid = p->items[i].itemId;
  14282. item.qty = p->items[i].amount;
  14283. item_list.push_back( item );
  14284. }
  14285. clif_cashshop_ack( sd, npc_cashshop_buylist( sd, p->kafraPoints, item_list ) );
  14286. #endif
  14287. }
  14288. /// Request to buy item(s) from cash shop.
  14289. /// 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)
  14290. void clif_parse_cashshop_buy( int32 fd, map_session_data *sd ){
  14291. const PACKET_CZ_SE_PC_BUY_CASHITEM_LIST* p = reinterpret_cast<PACKET_CZ_SE_PC_BUY_CASHITEM_LIST*>( RFIFOP( fd, 0 ) );
  14292. int32 s_itl = sizeof( p->items[0] );
  14293. if( p->packetLength < sizeof( *p ) || p->packetLength != sizeof( *p ) + p->count * s_itl ){
  14294. 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 );
  14295. return;
  14296. }
  14297. cashshop_buylist( sd, p->kafraPoints, p->count, p->items );
  14298. }
  14299. /// Adoption System
  14300. ///
  14301. /// Adoption message (ZC_BABYMSG).
  14302. /// 0216 <msg>.L
  14303. /// msg:
  14304. /// ADOPT_REPLY_MORE_CHILDREN = "You cannot adopt more than 1 child."
  14305. /// ADOPT_REPLY_LEVEL_70 = "You must be at least character level 70 in order to adopt someone."
  14306. /// ADOPT_REPLY_MARRIED = "You cannot adopt a married person."
  14307. void clif_Adopt_reply(map_session_data *sd, int32 type)
  14308. {
  14309. int32 fd = sd->fd;
  14310. WFIFOHEAD(fd,6);
  14311. WFIFOW(fd,0) = 0x216;
  14312. WFIFOL(fd,2) = type;
  14313. WFIFOSET(fd,6);
  14314. }
  14315. /// Adoption confirmation (ZC_REQ_BABY).
  14316. /// 01f6 <account id>.L <char id>.L <name>.B
  14317. void clif_Adopt_request(map_session_data *sd, map_session_data *src, int32 p_id)
  14318. {
  14319. int32 fd = sd->fd;
  14320. WFIFOHEAD(fd,34);
  14321. WFIFOW(fd,0) = 0x1f6;
  14322. WFIFOL(fd,2) = src->status.account_id;
  14323. WFIFOL(fd,6) = p_id;
  14324. safestrncpy(WFIFOCP(fd,10), src->status.name, NAME_LENGTH);
  14325. WFIFOSET(fd,34);
  14326. }
  14327. /// Request to adopt a player (CZ_REQ_JOIN_BABY).
  14328. /// 01f9 <account id>.L
  14329. void clif_parse_Adopt_request(int32 fd, map_session_data *sd)
  14330. {
  14331. TBL_PC *tsd = map_id2sd(RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]));
  14332. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  14333. if( pc_try_adopt(sd, p_sd, tsd) == ADOPT_ALLOWED )
  14334. {
  14335. tsd->adopt_invite = sd->status.account_id;
  14336. clif_Adopt_request(tsd, sd, p_sd->status.account_id);
  14337. }
  14338. }
  14339. /// Answer to adopt confirmation.
  14340. /// 01f7 <account id>.L <char id>.L <answer>.L (CZ_JOIN_BABY)
  14341. /// answer:
  14342. /// 0 = rejected
  14343. /// 1 = accepted
  14344. void clif_parse_Adopt_reply(int32 fd, map_session_data *sd){
  14345. const PACKET_CZ_JOIN_BABY* p = reinterpret_cast<PACKET_CZ_JOIN_BABY*>( RFIFOP( fd, 0 ) );
  14346. // Check if the adoption was accepted
  14347. if( p->accepted == 0 ){
  14348. sd->adopt_invite = 0;
  14349. return;
  14350. }
  14351. map_session_data* father_sd = map_id2sd( p->father_AID );
  14352. // The father has to be online
  14353. if( father_sd == nullptr ){
  14354. sd->adopt_invite = 0;
  14355. return;
  14356. }
  14357. map_session_data* mother_sd = map_id2sd( p->mother_AID );
  14358. // The mother has to be online
  14359. if( mother_sd == nullptr ){
  14360. sd->adopt_invite = 0;
  14361. return;
  14362. }
  14363. if( sd->adopt_invite != father_sd->status.account_id ){
  14364. sd->adopt_invite = 0;
  14365. return;
  14366. }
  14367. sd->adopt_invite = 0;
  14368. pc_adoption( father_sd, mother_sd, sd );
  14369. }
  14370. /// Convex Mirror
  14371. /// 0293 <infoType>.B <x>.L <y>.L <minHours>.W <minMinutes>.W <maxHours>.W <maxMinutes>.W <monster name>.51B (ZC_BOSS_INFO)
  14372. /// infoType:
  14373. /// BOSS_INFO_NOT = No boss on this map.
  14374. /// BOSS_INFO_ALIVE = Boss is alive (position update).
  14375. /// BOSS_INFO_ALIVE_WITHMSG = Boss is alive (initial announce).
  14376. /// BOSS_INFO_DEAD = Boss is dead.
  14377. void clif_bossmapinfo( map_session_data& sd, mob_data* md, e_bossmap_info flag ){
  14378. PACKET_ZC_BOSS_INFO p = {};
  14379. p.packetType = HEADER_ZC_BOSS_INFO;
  14380. p.type = flag;
  14381. switch (flag) {
  14382. case BOSS_INFO_NOT:
  14383. // No data required
  14384. break;
  14385. case BOSS_INFO_ALIVE_WITHMSG:
  14386. [[fallthrough]];
  14387. case BOSS_INFO_ALIVE:
  14388. p.x = md->bl.x;
  14389. p.y = md->bl.y;
  14390. break;
  14391. case BOSS_INFO_DEAD: {
  14392. const struct TimerData * timer_data = get_timer(md->spawn_timer);
  14393. uint32 seconds;
  14394. int32 hours, minutes;
  14395. seconds = (uint32)(DIFF_TICK(timer_data->tick, gettick()) / 1000 + 60);
  14396. hours = seconds / (60 * 60);
  14397. seconds = seconds - (60 * 60 * hours);
  14398. minutes = seconds / 60;
  14399. p.minHours = hours;
  14400. p.minMinutes = minutes;
  14401. break;
  14402. }
  14403. }
  14404. if( md != nullptr ){
  14405. safestrncpy( p.name, md->db->jname.c_str(), sizeof( p.name ) );
  14406. }
  14407. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  14408. }
  14409. /// Requesting equip of a player (CZ_EQUIPWIN_MICROSCOPE).
  14410. /// 02d6 <account id>.L
  14411. void clif_parse_ViewPlayerEquip(int32 fd, map_session_data* sd)
  14412. {
  14413. int32 aid = RFIFOL(fd, packet_db[RFIFOW(fd,0)].pos[0]);
  14414. map_session_data* tsd = map_id2sd(aid);
  14415. if (!tsd)
  14416. return;
  14417. if (sd->bl.m != tsd->bl.m)
  14418. return;
  14419. else if( tsd->status.show_equip || pc_has_permission(sd, PC_PERM_VIEW_EQUIPMENT) )
  14420. clif_viewequip_ack( *sd, *tsd );
  14421. else
  14422. clif_msg(sd, MSI_OPEN_EQUIPEDITEM_REFUSED);
  14423. }
  14424. /// Request to a configuration.
  14425. /// 02d8 <type>.L <flag>.L (CZ_CONFIG)
  14426. /// type: see enum e_config_type
  14427. /// flag:
  14428. /// false = disabled
  14429. /// true = enabled
  14430. void clif_parse_configuration( int32 fd, map_session_data* sd ){
  14431. int32 cmd = RFIFOW(fd,0);
  14432. int32 type = RFIFOL(fd,packet_db[cmd].pos[0]);
  14433. bool flag = RFIFOL(fd,packet_db[cmd].pos[1]) != 0;
  14434. switch( type ){
  14435. case CONFIG_OPEN_EQUIPMENT_WINDOW:
  14436. sd->status.show_equip = flag;
  14437. break;
  14438. case CONFIG_CALL:
  14439. sd->status.disable_call = flag;
  14440. break;
  14441. case CONFIG_PET_AUTOFEED:
  14442. // Player can not click this if he does not have a pet
  14443. if( sd->pd == nullptr || !battle_config.feature_pet_autofeed || !sd->pd->get_pet_db()->allow_autofeed ){
  14444. return;
  14445. }
  14446. sd->pd->pet.autofeed = flag;
  14447. break;
  14448. case CONFIG_HOMUNCULUS_AUTOFEED:
  14449. // Player can not click this if he does not have a homunculus or it is vaporized
  14450. if( sd->hd == nullptr || sd->hd->homunculus.vaporize || !battle_config.feature_homunculus_autofeed ){
  14451. return;
  14452. }
  14453. sd->hd->homunculus.autofeed = flag;
  14454. break;
  14455. default:
  14456. ShowWarning( "clif_parse_configuration: received unknown configuration type '%d'...\n", type );
  14457. return;
  14458. }
  14459. clif_configuration( sd, static_cast<e_config_type>(type), flag );
  14460. }
  14461. /// Request to change party invitation tick.
  14462. /// 02C8 <enabled>.B (CZ_PARTY_CONFIG)
  14463. /// value:
  14464. /// 0 = disabled (triggered by /accept)
  14465. /// 1 = enabled (triggered by /refuse)
  14466. void clif_parse_PartyTick( int32 fd, map_session_data* sd ){
  14467. const PACKET_CZ_PARTY_CONFIG* p = reinterpret_cast<PACKET_CZ_PARTY_CONFIG*>( RFIFOP( fd, 0 ) );
  14468. sd->status.disable_partyinvite = p->refuseInvite;
  14469. clif_partyinvitationstate( *sd );
  14470. }
  14471. /// Questlog System [Kevin] [Inkfish]
  14472. ///
  14473. /**
  14474. * Safe check to init packet len & quest num for player.
  14475. * @param def_len
  14476. * @param info_len Length of each quest info.
  14477. * @param avail_quests Avail quests that player has.
  14478. * @param limit_out Limit for quest num, to make sure the quest num is still in allowed value.
  14479. * @param len_out Packet length as initial set => def_len + limit_out * info_len.
  14480. **/
  14481. static void clif_quest_len(int32 def_len, int32 info_len, int32 avail_quests, int32 *limit_out, int32 *len_out) {
  14482. (*limit_out) = (0xFFFF - def_len) / info_len;
  14483. (*limit_out) = (avail_quests > (*limit_out)) ? (*limit_out) : avail_quests;
  14484. (*len_out) = ((*limit_out) * info_len) + def_len;
  14485. }
  14486. std::string clif_quest_string( std::shared_ptr<s_quest_objective> objective ){
  14487. std::string race_name;
  14488. switch( objective->race ){
  14489. case RC_FORMLESS: race_name = "Formless"; break;
  14490. case RC_UNDEAD: race_name = "Undead"; break;
  14491. case RC_BRUTE: race_name = "Brute"; break;
  14492. case RC_PLANT: race_name = "Plant"; break;
  14493. case RC_INSECT: race_name = "Insect"; break;
  14494. case RC_FISH: race_name = "Fish"; break;
  14495. case RC_DEMON: race_name = "Demon"; break;
  14496. case RC_DEMIHUMAN: race_name = "Demihuman"; break;
  14497. case RC_ANGEL: race_name = "Angel"; break;
  14498. case RC_DRAGON: race_name = "Dragon"; break;
  14499. default:
  14500. ShowWarning( "clif_quest_string: Unsupported race %d - using empty string...\n", objective->race );
  14501. [[fallthrough]];
  14502. case RC_ALL: race_name = ""; break;
  14503. }
  14504. std::string size_name;
  14505. switch( objective->size ){
  14506. case SZ_SMALL: size_name = "Small"; break;
  14507. case SZ_MEDIUM: size_name = "Medium"; break;
  14508. case SZ_BIG: size_name = "Large"; break;
  14509. default:
  14510. ShowWarning( "clif_quest_string: Unsupported size %d - using empty string...\n", objective->size );
  14511. [[fallthrough]];
  14512. case SZ_ALL: size_name = ""; break;
  14513. }
  14514. std::string ele_name;
  14515. switch( objective->element ){
  14516. case ELE_NEUTRAL: ele_name = "Neutral Element"; break;
  14517. case ELE_WATER: ele_name = "Water Element"; break;
  14518. case ELE_EARTH: ele_name = "Earth Element"; break;
  14519. case ELE_FIRE: ele_name = "Fire Element"; break;
  14520. case ELE_WIND: ele_name = "Wind Element"; break;
  14521. case ELE_POISON: ele_name = "Poison Element"; break;
  14522. case ELE_HOLY: ele_name = "Holy Element"; break;
  14523. case ELE_DARK: ele_name = "Shadow Element"; break;
  14524. case ELE_GHOST: ele_name = "Ghost Element"; break;
  14525. case ELE_UNDEAD: ele_name = "Undead Element"; break;
  14526. default:
  14527. ShowWarning( "clif_quest_string: Unsupported element %d - using empty string...\n", objective->element );
  14528. [[fallthrough]];
  14529. case ELE_ALL: ele_name = ""; break;
  14530. }
  14531. std::string str;
  14532. if( !objective->map_name.empty() ){
  14533. str += objective->map_name;
  14534. }
  14535. if( !race_name.empty() ){
  14536. if( !str.empty() ){
  14537. str += ", ";
  14538. }
  14539. str += race_name;
  14540. }
  14541. if( !size_name.empty() ){
  14542. if( !str.empty() ){
  14543. str += ", ";
  14544. }
  14545. str += size_name;
  14546. }
  14547. if( !ele_name.empty() ){
  14548. if( !str.empty() ){
  14549. str += ", ";
  14550. }
  14551. str += ele_name;
  14552. }
  14553. return str;
  14554. }
  14555. /// Sends list of all quest states
  14556. /// 02b1 <packet len>.W <num>.L { <quest id>.L <active>.B }*num (ZC_ALL_QUEST_LIST)
  14557. /// 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)
  14558. /// 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)
  14559. void clif_quest_send_list(map_session_data *sd)
  14560. {
  14561. nullpo_retv(sd);
  14562. int32 fd = sd->fd;
  14563. int32 i;
  14564. int32 offset = 8;
  14565. int32 limit = 0;
  14566. #if PACKETVER >= 20141022
  14567. #if PACKETVER >= 20150513
  14568. int32 size = 22 + NAME_LENGTH;
  14569. #else
  14570. int32 size = 10 + NAME_LENGTH;
  14571. #endif
  14572. clif_quest_len(offset, 15 + ( size * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &i);
  14573. WFIFOHEAD(fd,i);
  14574. #if PACKETVER >= 20150513
  14575. WFIFOW(fd, 0) = 0x9f8;
  14576. #else
  14577. WFIFOW(fd, 0) = 0x97a;
  14578. #endif
  14579. WFIFOL(fd, 4) = limit;
  14580. for (i = 0; i < limit; i++) {
  14581. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14582. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14583. offset += 4;
  14584. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14585. offset++;
  14586. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14587. offset += 4;
  14588. WFIFOL(fd, offset) = static_cast<uint32>(sd->quest_log[i].time);
  14589. offset += 4;
  14590. WFIFOW(fd, offset) = static_cast<uint16>(qi->objectives.size());
  14591. offset += 2;
  14592. if (!qi->objectives.empty()) {
  14593. std::shared_ptr<s_mob_db> mob;
  14594. for (int32 j = 0; j < qi->objectives.size(); j++) {
  14595. mob = mob_db.find(qi->objectives[j]->mob_id);
  14596. e_race race = qi->objectives[j]->race;
  14597. e_size size = qi->objectives[j]->size;
  14598. e_element element = qi->objectives[j]->element;
  14599. #if PACKETVER >= 20150513
  14600. WFIFOL(fd, offset) = sd->quest_log[i].quest_id * 1000 + j;
  14601. offset += 4;
  14602. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0)));
  14603. offset += 4;
  14604. #endif
  14605. WFIFOL(fd, offset) = ((mob && qi->objectives[j]->mob_id > 0) ? qi->objectives[j]->mob_id : MOBID_PORING);
  14606. offset += 4;
  14607. #if PACKETVER >= 20150513
  14608. WFIFOW(fd, offset) = qi->objectives[j]->min_level;
  14609. offset += 2;
  14610. WFIFOW(fd, offset) = qi->objectives[j]->max_level;
  14611. offset += 2;
  14612. #endif
  14613. WFIFOW(fd, offset) = sd->quest_log[i].count[j];
  14614. offset += 2;
  14615. WFIFOW(fd, offset) = qi->objectives[j]->count;
  14616. offset += 2;
  14617. if (mob && qi->objectives[j]->mob_id > 0)
  14618. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14619. else
  14620. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14621. offset += NAME_LENGTH;
  14622. }
  14623. }
  14624. }
  14625. WFIFOW(fd, 2) = offset;
  14626. WFIFOSET(fd, offset);
  14627. #else
  14628. clif_quest_len(offset, 5, sd->avail_quests, &limit, &i);
  14629. WFIFOHEAD(fd,i);
  14630. WFIFOW(fd, 0) = 0x2b1;
  14631. WFIFOL(fd, 4) = limit;
  14632. for (i = 0; i < limit; i++) {
  14633. WFIFOL(fd, offset) = sd->quest_log[i].quest_id;
  14634. offset += 4;
  14635. WFIFOB(fd, offset) = sd->quest_log[i].state;
  14636. offset += 1;
  14637. }
  14638. WFIFOW(fd, 2) = offset;
  14639. WFIFOSET(fd, offset);
  14640. #endif
  14641. }
  14642. /// Sends list of all quest missions (ZC_ALL_QUEST_MISSION).
  14643. /// 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
  14644. void clif_quest_send_mission(map_session_data *sd)
  14645. {
  14646. nullpo_retv(sd);
  14647. int32 fd = sd->fd;
  14648. int32 limit = 0;
  14649. int32 len = sd->avail_quests*104+8;
  14650. clif_quest_len(8, 14 + ((6 + NAME_LENGTH) * MAX_QUEST_OBJECTIVES), sd->avail_quests, &limit, &len);
  14651. WFIFOHEAD(fd, len);
  14652. WFIFOW(fd, 0) = 0x2b2;
  14653. WFIFOW(fd, 2) = len;
  14654. WFIFOL(fd, 4) = limit;
  14655. for (int32 i = 0; i < limit; i++) {
  14656. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  14657. WFIFOL(fd, i*104+8) = sd->quest_log[i].quest_id;
  14658. WFIFOL(fd, i*104+12) = static_cast<uint32>(sd->quest_log[i].time - qi->time);
  14659. WFIFOL(fd, i*104+16) = static_cast<uint32>(sd->quest_log[i].time);
  14660. WFIFOW(fd, i*104+20) = static_cast<uint16>(qi->objectives.size());
  14661. for (int32 j = 0 ; j < qi->objectives.size(); j++) {
  14662. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[j]->mob_id);
  14663. WFIFOL(fd, i*104+22+j*30) = (mob ? qi->objectives[j]->mob_id : MOBID_PORING);
  14664. WFIFOW(fd, i*104+26+j*30) = sd->quest_log[i].count[j];
  14665. if (mob && qi->objectives[j]->mob_id > 0)
  14666. safestrncpy(WFIFOCP(fd, i*104+28+j*30), mob->jname.c_str(), NAME_LENGTH);
  14667. else
  14668. safestrncpy(WFIFOCP(fd, i*104+28+j*30), clif_quest_string(qi->objectives[j]).c_str(), NAME_LENGTH);
  14669. }
  14670. }
  14671. WFIFOSET(fd, len);
  14672. }
  14673. /// Notification about a new quest
  14674. /// 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)
  14675. /// 08fe <packet len>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_HUNTING_QUEST_INFO)
  14676. /// 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)
  14677. void clif_quest_add(map_session_data *sd, struct quest *qd)
  14678. {
  14679. nullpo_retv(sd);
  14680. nullpo_retv(qd);
  14681. int32 fd = sd->fd;
  14682. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14683. if (!qi)
  14684. return;
  14685. #if PACKETVER >= 20150513
  14686. int32 cmd = 0x9f9;
  14687. #else
  14688. int32 cmd = 0x2b3;
  14689. #endif
  14690. WFIFOHEAD(fd, packet_len(cmd));
  14691. WFIFOW(fd, 0) = cmd;
  14692. WFIFOL(fd, 2) = qd->quest_id;
  14693. WFIFOB(fd, 6) = qd->state;
  14694. WFIFOB(fd, 7) = static_cast<uint8>(qd->time - qi->time);
  14695. WFIFOL(fd, 11) = static_cast<uint32>(qd->time);
  14696. WFIFOW(fd, 15) = static_cast<uint16>(qi->objectives.size());
  14697. for (int32 i = 0, offset = 17; i < qi->objectives.size(); i++) {
  14698. std::shared_ptr<s_mob_db> mob = mob_db.find(qi->objectives[i]->mob_id);
  14699. e_race race = qi->objectives[i]->race;
  14700. e_size size = qi->objectives[i]->size;
  14701. e_element element = qi->objectives[i]->element;
  14702. #if PACKETVER >= 20150513
  14703. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14704. offset += 4;
  14705. WFIFOL(fd, offset) = (race ? race : (size ? size : (element ? element : 0))); // effect ?
  14706. offset += 4;
  14707. #endif
  14708. 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
  14709. offset += 4;
  14710. #if PACKETVER >= 20150513
  14711. WFIFOW(fd, offset) = qi->objectives[i]->min_level;
  14712. offset += 2;
  14713. WFIFOW(fd, offset) = qi->objectives[i]->max_level;
  14714. offset += 2;
  14715. #endif
  14716. WFIFOW(fd, offset) = qd->count[i];
  14717. offset += 2;
  14718. if (mob && qi->objectives[i]->mob_id > 0)
  14719. safestrncpy(WFIFOCP(fd,offset), mob->jname.c_str(), NAME_LENGTH);
  14720. else
  14721. safestrncpy(WFIFOCP(fd,offset), clif_quest_string(qi->objectives[i]).c_str(), NAME_LENGTH);
  14722. offset += NAME_LENGTH;
  14723. }
  14724. WFIFOSET(fd, packet_len(cmd));
  14725. #if PACKETVER >= 20150513
  14726. int16 len;
  14727. len = static_cast<decltype(len)>( 4 + qi->objectives.size() * 12 );
  14728. WFIFOHEAD(fd, len);
  14729. WFIFOW(fd, 0) = 0x8fe;
  14730. WFIFOW(fd, 2) = len;
  14731. for (int32 i = 0, offset = 4; i < qi->objectives.size(); i++, offset += 12) {
  14732. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14733. WFIFOL(fd, offset + 4) = qi->objectives[i]->mob_id;
  14734. WFIFOW(fd, offset + 8) = qi->objectives[i]->count;
  14735. WFIFOW(fd, offset + 10) = qd->count[i];
  14736. }
  14737. WFIFOSET(fd, len);
  14738. #endif
  14739. }
  14740. /// Notification about a quest being removed (ZC_DEL_QUEST).
  14741. /// 02b4 <quest id>.L
  14742. void clif_quest_delete(map_session_data *sd, int32 quest_id)
  14743. {
  14744. int32 fd = sd->fd;
  14745. WFIFOHEAD(fd, packet_len(0x2b4));
  14746. WFIFOW(fd, 0) = 0x2b4;
  14747. WFIFOL(fd, 2) = quest_id;
  14748. WFIFOSET(fd, packet_len(0x2b4));
  14749. }
  14750. /// Notification of an update to the hunting mission counter
  14751. /// 02b5 <packet len>.W <mobs>.W { <quest id>.L <mob id>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT)
  14752. /// 09fa <packet len>.W <mobs>.W { <quest id>.L <hunt identification>.L <total count>.W <current count>.W }*3 (ZC_UPDATE_MISSION_HUNT_EX)
  14753. void clif_quest_update_objective(map_session_data *sd, struct quest *qd)
  14754. {
  14755. int32 fd = sd->fd;
  14756. int32 offset = 6;
  14757. std::shared_ptr<s_quest_db> qi = quest_search(qd->quest_id);
  14758. int16 len;
  14759. len = static_cast<decltype(len)>( qi->objectives.size() * 12 + 6 );
  14760. #if PACKETVER >= 20150513
  14761. int32 cmd = 0x9fa;
  14762. #else
  14763. int32 cmd = 0x2b5;
  14764. #endif
  14765. WFIFOHEAD(fd, len);
  14766. WFIFOW(fd, 0) = cmd;
  14767. WFIFOW(fd, 4) = static_cast<uint16>(qi->objectives.size());
  14768. for (int32 i = 0; i < qi->objectives.size(); i++) {
  14769. WFIFOL(fd, offset) = qd->quest_id;
  14770. offset += 4;
  14771. #if PACKETVER >= 20150513
  14772. WFIFOL(fd, offset) = qd->quest_id * 1000 + i;
  14773. offset += 4;
  14774. #else
  14775. WFIFOL(fd, offset) = qi->objectives[i]->mob_id;
  14776. offset += 4;
  14777. #endif
  14778. WFIFOW(fd, offset) = qi->objectives[i]->count;
  14779. offset += 2;
  14780. WFIFOW(fd, offset) = qd->count[i];
  14781. offset += 2;
  14782. }
  14783. WFIFOW(fd, 2) = offset;
  14784. WFIFOSET(fd, offset);
  14785. }
  14786. /// Request to change the state of a quest.
  14787. /// 02b6 <quest id>.L <active>.B (CZ_ACTIVE_QUEST)
  14788. void clif_parse_questStateAck( int32 fd, map_session_data* sd ){
  14789. #if PACKETVER >= 20070622
  14790. const PACKET_CZ_ACTIVE_QUEST* p = reinterpret_cast<PACKET_CZ_ACTIVE_QUEST*>( RFIFOP( fd, 0 ) );
  14791. quest_update_status( sd, p->quest_id, ( p->active != 0 ) ? Q_ACTIVE : Q_INACTIVE );
  14792. #endif
  14793. }
  14794. /// Notification about the change of a quest state (ZC_ACTIVE_QUEST).
  14795. /// 02b7 <quest id>.L <active>.B
  14796. void clif_quest_update_status(map_session_data *sd, int32 quest_id, bool active)
  14797. {
  14798. int32 fd = sd->fd;
  14799. WFIFOHEAD(fd, packet_len(0x2b7));
  14800. WFIFOW(fd, 0) = 0x2b7;
  14801. WFIFOL(fd, 2) = quest_id;
  14802. WFIFOB(fd, 6) = active;
  14803. WFIFOSET(fd, packet_len(0x2b7));
  14804. }
  14805. /// Notification about an NPC's quest state (ZC_QUEST_NOTIFY_EFFECT).
  14806. /// 0446 <npc id>.L <x>.W <y>.W <effect>.W <color>.W
  14807. /// effect:
  14808. /// 0 = none
  14809. /// 1 = exclamation mark icon
  14810. /// 2 = question mark icon
  14811. /// color:
  14812. /// 0 = yellow
  14813. /// 1 = orange
  14814. /// 2 = green
  14815. /// 3 = purple
  14816. void clif_quest_show_event(map_session_data *sd, struct block_list *bl, e_questinfo_types effect, e_questinfo_markcolor color)
  14817. {
  14818. #if PACKETVER >= 20090218
  14819. nullpo_retv(sd);
  14820. nullpo_retv(bl);
  14821. int32 fd = sd->fd;
  14822. WFIFOHEAD(fd, packet_len(0x446));
  14823. WFIFOW(fd, 0) = 0x446;
  14824. WFIFOL(fd, 2) = bl->id;
  14825. WFIFOW(fd, 6) = bl->x;
  14826. WFIFOW(fd, 8) = bl->y;
  14827. WFIFOW(fd, 10) = effect;
  14828. WFIFOW(fd, 12) = color;
  14829. WFIFOSET(fd, packet_len(0x446));
  14830. #endif
  14831. }
  14832. /// Mercenary System
  14833. ///
  14834. /// Notification about a mercenary status parameter change (ZC_MER_PAR_CHANGE).
  14835. /// 02a2 <var id>.W <value>.L
  14836. void clif_mercenary_updatestatus(map_session_data *sd, int32 type)
  14837. {
  14838. s_mercenary_data *md;
  14839. struct status_data *status;
  14840. int32 fd;
  14841. if( !clif_session_isValid(sd) || (md = sd->md) == nullptr )
  14842. return;
  14843. fd = sd->fd;
  14844. status = &md->battle_status;
  14845. WFIFOHEAD(fd,packet_len(0x2a2));
  14846. WFIFOW(fd,0) = 0x2a2;
  14847. WFIFOW(fd,2) = type;
  14848. switch( type ) {
  14849. case SP_ATK1:
  14850. {
  14851. int32 atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14852. WFIFOL(fd,4) = cap_value(atk, 0, INT16_MAX);
  14853. }
  14854. break;
  14855. case SP_MATK1:
  14856. WFIFOL(fd,4) = min(status->matk_max, UINT16_MAX);
  14857. break;
  14858. case SP_HIT:
  14859. WFIFOL(fd,4) = status->hit;
  14860. break;
  14861. case SP_CRITICAL:
  14862. WFIFOL(fd,4) = status->cri/10;
  14863. break;
  14864. case SP_DEF1:
  14865. WFIFOL(fd,4) = status->def;
  14866. break;
  14867. case SP_MDEF1:
  14868. WFIFOL(fd,4) = status->mdef;
  14869. break;
  14870. case SP_MERCFLEE:
  14871. WFIFOL(fd,4) = status->flee;
  14872. break;
  14873. case SP_ASPD:
  14874. WFIFOL(fd,4) = status->amotion;
  14875. break;
  14876. case SP_HP:
  14877. WFIFOL(fd,4) = status->hp;
  14878. break;
  14879. case SP_MAXHP:
  14880. WFIFOL(fd,4) = status->max_hp;
  14881. break;
  14882. case SP_SP:
  14883. WFIFOL(fd,4) = status->sp;
  14884. break;
  14885. case SP_MAXSP:
  14886. WFIFOL(fd,4) = status->max_sp;
  14887. break;
  14888. case SP_MERCKILLS:
  14889. WFIFOL(fd,4) = md->mercenary.kill_count;
  14890. break;
  14891. case SP_MERCFAITH:
  14892. WFIFOL(fd,4) = mercenary_get_faith(md);
  14893. break;
  14894. }
  14895. WFIFOSET(fd,packet_len(0x2a2));
  14896. }
  14897. /// Mercenary base status data (ZC_MER_INIT).
  14898. /// 029b <id>.L <atk>.W <matk>.W <hit>.W <crit>.W <def>.W <mdef>.W <flee>.W <aspd>.W
  14899. /// <name>.24B <level>.W <hp>.L <maxhp>.L <sp>.L <maxsp>.L <expire time>.L <faith>.W
  14900. /// <calls>.L <kills>.L <atk range>.W
  14901. void clif_mercenary_info(map_session_data *sd)
  14902. {
  14903. int32 fd;
  14904. s_mercenary_data *md;
  14905. struct status_data *status;
  14906. int32 atk;
  14907. if( !clif_session_isValid(sd) || (md = sd->md) == nullptr )
  14908. return;
  14909. fd = sd->fd;
  14910. status = &md->battle_status;
  14911. WFIFOHEAD(fd,packet_len(0x29b));
  14912. WFIFOW(fd,0) = 0x29b;
  14913. WFIFOL(fd,2) = md->bl.id;
  14914. // Mercenary shows ATK as a random value between ATK ~ ATK2
  14915. atk = rnd()%(status->rhw.atk2 - status->rhw.atk + 1) + status->batk + status->rhw.atk;
  14916. WFIFOW(fd,6) = cap_value(atk, 0, INT16_MAX);
  14917. WFIFOW(fd,8) = min(status->matk_max, UINT16_MAX);
  14918. WFIFOW(fd,10) = status->hit;
  14919. WFIFOW(fd,12) = status->cri/10;
  14920. WFIFOW(fd,14) = status->def;
  14921. WFIFOW(fd,16) = status->mdef;
  14922. WFIFOW(fd,18) = status->flee;
  14923. WFIFOW(fd,20) = status->amotion;
  14924. safestrncpy(WFIFOCP(fd,22), md->db->name.c_str(), NAME_LENGTH);
  14925. WFIFOW(fd,46) = md->db->lv;
  14926. WFIFOL(fd,48) = status->hp;
  14927. WFIFOL(fd,52) = status->max_hp;
  14928. WFIFOL(fd,56) = status->sp;
  14929. WFIFOL(fd,60) = status->max_sp;
  14930. WFIFOL(fd,64) = client_tick(time(nullptr) + (mercenary_get_lifetime(md) / 1000));
  14931. WFIFOW(fd,68) = mercenary_get_faith(md);
  14932. WFIFOL(fd,70) = mercenary_get_calls(md);
  14933. WFIFOL(fd,74) = md->mercenary.kill_count;
  14934. WFIFOW(fd,78) = md->battle_status.rhw.range;
  14935. WFIFOSET(fd,packet_len(0x29b));
  14936. }
  14937. /// Mercenary skill tree (ZC_MER_SKILLINFO_LIST).
  14938. /// 029d <packet len>.W { <skill id>.W <type>.L <level>.W <sp cost>.W <attack range>.W <skill name>.24B <upgradable>.B }*
  14939. void clif_mercenary_skillblock(map_session_data *sd)
  14940. {
  14941. s_mercenary_data *md;
  14942. int32 fd, len = 4;
  14943. if( sd == nullptr || (md = sd->md) == nullptr )
  14944. return;
  14945. fd = sd->fd;
  14946. WFIFOHEAD(fd,4+37*MAX_MERCSKILL);
  14947. WFIFOW(fd,0) = 0x29d;
  14948. for (const auto &it : md->db->skill) {
  14949. uint16 id = it.first;
  14950. if (!SKILL_CHK_MERC(id))
  14951. continue;
  14952. uint16 lv = it.second;
  14953. WFIFOW(fd,len) = id;
  14954. WFIFOL(fd,len+2) = skill_get_inf(id);
  14955. WFIFOW(fd,len+6) = lv;
  14956. WFIFOW(fd,len+8) = skill_get_sp(id, lv);
  14957. WFIFOW(fd,len+10) = skill_get_range2(&md->bl, id, lv, false);
  14958. safestrncpy(WFIFOCP(fd,len+12), skill_get_name(id), NAME_LENGTH);
  14959. WFIFOB(fd,len+36) = 0; // Skillable for Mercenary?
  14960. len += 37;
  14961. }
  14962. WFIFOW(fd,2) = len;
  14963. WFIFOSET(fd,len);
  14964. }
  14965. /// Request to invoke a mercenary menu action (CZ_MER_COMMAND).
  14966. /// 029f <command>.B
  14967. /// 1 = mercenary information
  14968. /// 2 = delete
  14969. void clif_parse_mercenary_action(int32 fd, map_session_data* sd)
  14970. {
  14971. int32 option = RFIFOB(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  14972. if( sd->md == nullptr )
  14973. return;
  14974. if( option == 2 ) mercenary_delete(sd->md, 2);
  14975. }
  14976. /// Notification about the remaining time of a rental item.
  14977. /// 0298 <name id>.W <seconds>.L (ZC_CASH_TIME_COUNTER)
  14978. void clif_rental_time( map_session_data* sd, t_itemid nameid, int32 seconds ){
  14979. // '<ItemName>' item will disappear in <seconds/60> minutes.
  14980. PACKET_ZC_CASH_TIME_COUNTER p = {};
  14981. p.packetType = HEADER_ZC_CASH_TIME_COUNTER;
  14982. p.itemId = client_nameid( nameid );
  14983. p.seconds = seconds;
  14984. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14985. }
  14986. /// Deletes a rental item from client's inventory.
  14987. /// 0299 <index>.W <name id>.W (ZC_CASH_ITEM_DELETE)
  14988. void clif_rental_expired( map_session_data* sd, int32 index, t_itemid nameid ){
  14989. // '<ItemName>' item has been deleted from the Inventory
  14990. PACKET_ZC_CASH_ITEM_DELETE p = {};
  14991. p.packetType = HEADER_ZC_CASH_ITEM_DELETE;
  14992. p.index = client_index( index );
  14993. p.itemId = client_nameid( nameid );
  14994. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  14995. }
  14996. /// Book Reading (ZC_READ_BOOK).
  14997. /// 0294 <book id>.L <page>.L
  14998. void clif_readbook(int32 fd, int32 book_id, int32 page)
  14999. {
  15000. WFIFOHEAD(fd,packet_len(0x294));
  15001. WFIFOW(fd,0) = 0x294;
  15002. WFIFOL(fd,2) = book_id;
  15003. WFIFOL(fd,6) = page;
  15004. WFIFOSET(fd,packet_len(0x294));
  15005. }
  15006. /// Battlegrounds
  15007. ///
  15008. /// Updates HP bar of a camp member.
  15009. /// 02e0 <account id>.L <name>.24B <hp>.W <max hp>.W (ZC_BATTLEFIELD_NOTIFY_HP)
  15010. /// 0a0e <account id>.L <hp>.L <max hp>.L (ZC_BATTLEFIELD_NOTIFY_HP2)
  15011. void clif_bg_hp(map_session_data *sd) {
  15012. #if PACKETVER < 20140613
  15013. unsigned char buf[34];
  15014. const int32 cmd = 0x2e0;
  15015. #else
  15016. unsigned char buf[14];
  15017. const int32 cmd = 0xa0e;
  15018. #endif
  15019. nullpo_retv(sd);
  15020. WBUFW(buf,0) = cmd;
  15021. WBUFL(buf,2) = sd->status.account_id;
  15022. #if PACKETVER < 20140613
  15023. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  15024. if( sd->battle_status.max_hp > INT16_MAX ) { // To correctly display the %hp bar. [Skotlex]
  15025. WBUFW(buf,30) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  15026. WBUFW(buf,32) = 100;
  15027. } else {
  15028. WBUFW(buf,30) = sd->battle_status.hp;
  15029. WBUFW(buf,32) = sd->battle_status.max_hp;
  15030. }
  15031. #else
  15032. if( sd->battle_status.max_hp > INT32_MAX ) {
  15033. WBUFL(buf,6) = sd->battle_status.hp/(sd->battle_status.max_hp/100);
  15034. WBUFL(buf,10) = 100;
  15035. } else {
  15036. WBUFL(buf,6) = sd->battle_status.hp;
  15037. WBUFL(buf,10) = sd->battle_status.max_hp;
  15038. }
  15039. #endif
  15040. clif_send(buf, packet_len(cmd), &sd->bl, BG_AREA_WOS);
  15041. }
  15042. /// Updates the position of a camp member on the minimap (ZC_BATTLEFIELD_NOTIFY_POSITION).
  15043. /// 02df <account id>.L <name>.24B <class>.W <x>.W <y>.W
  15044. void clif_bg_xy(map_session_data *sd)
  15045. {
  15046. unsigned char buf[36];
  15047. nullpo_retv(sd);
  15048. WBUFW(buf,0)=0x2df;
  15049. WBUFL(buf,2)=sd->status.account_id;
  15050. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH);
  15051. WBUFW(buf,30)=sd->status.class_;
  15052. WBUFW(buf,32)=sd->bl.x;
  15053. WBUFW(buf,34)=sd->bl.y;
  15054. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  15055. }
  15056. void clif_bg_xy_remove(map_session_data *sd)
  15057. {
  15058. unsigned char buf[36];
  15059. nullpo_retv(sd);
  15060. WBUFW(buf,0)=0x2df;
  15061. WBUFL(buf,2)=sd->status.account_id;
  15062. memset(WBUFP(buf,6), 0, NAME_LENGTH);
  15063. WBUFW(buf,30)=0;
  15064. WBUFW(buf,32)=-1;
  15065. WBUFW(buf,34)=-1;
  15066. clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS);
  15067. }
  15068. /// Notifies clients of a battleground message.
  15069. /// 02DC <packet len>.W <account id>.L <name>.24B <message>.?B (ZC_BATTLEFIELD_CHAT)
  15070. void clif_bg_message( struct s_battleground_data *bg, int32 src_id, const char *name, const char *mes, size_t len ){
  15071. map_session_data *sd = bg_getavailablesd( bg );
  15072. if( sd == nullptr ){
  15073. return;
  15074. }
  15075. // limit length
  15076. len = zmin( len + 1, CHAT_SIZE_MAX );
  15077. unsigned char buf[8 + NAME_LENGTH + CHAT_SIZE_MAX];
  15078. WBUFW(buf,0) = 0x2dc;
  15079. WBUFW(buf,2) = static_cast<int16>( len + NAME_LENGTH + 8 );
  15080. WBUFL(buf,4) = src_id;
  15081. safestrncpy(WBUFCP(buf,8), name, NAME_LENGTH);
  15082. safestrncpy(WBUFCP(buf,8+NAME_LENGTH), mes, len );
  15083. clif_send(buf,WBUFW(buf,2), &sd->bl, BG);
  15084. }
  15085. /// Validates and processes battlechat messages.
  15086. /// All messages that are sent after enabling battleground chat with /battlechat.
  15087. /// 02DB <packet len>.W <text>.?B (CZ_BATTLEFIELD_CHAT)
  15088. void clif_parse_BattleChat(int32 fd, map_session_data* sd){
  15089. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  15090. if( !clif_process_message( sd, false, name, message, output ) )
  15091. return;
  15092. bg_send_message(sd, output, strlen(output) );
  15093. }
  15094. /// Notifies client of a battleground score change (ZC_BATTLEFIELD_NOTIFY_POINT).
  15095. /// 02de <camp A points>.W <camp B points>.W
  15096. void clif_bg_updatescore(int16 m)
  15097. {
  15098. struct block_list bl;
  15099. unsigned char buf[6];
  15100. struct map_data *mapdata = map_getmapdata(m);
  15101. bl.id = 0;
  15102. bl.type = BL_NUL;
  15103. bl.m = m;
  15104. WBUFW(buf,0) = 0x2de;
  15105. WBUFW(buf,2) = mapdata->bgscore_lion;
  15106. WBUFW(buf,4) = mapdata->bgscore_eagle;
  15107. clif_send(buf,packet_len(0x2de),&bl,ALL_SAMEMAP);
  15108. }
  15109. void clif_bg_updatescore_single(map_session_data *sd)
  15110. {
  15111. int32 fd;
  15112. nullpo_retv(sd);
  15113. fd = sd->fd;
  15114. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  15115. WFIFOHEAD(fd,packet_len(0x2de));
  15116. WFIFOW(fd,0) = 0x2de;
  15117. WFIFOW(fd,2) = mapdata->bgscore_lion;
  15118. WFIFOW(fd,4) = mapdata->bgscore_eagle;
  15119. WFIFOSET(fd,packet_len(0x2de));
  15120. }
  15121. /// Battleground camp belong-information (ZC_BATTLEFIELD_NOTIFY_CAMPINFO).
  15122. /// 02dd <account id>.L <name>.24B <camp>.W
  15123. void clif_sendbgemblem_area(map_session_data *sd)
  15124. {
  15125. unsigned char buf[33];
  15126. nullpo_retv(sd);
  15127. WBUFW(buf, 0) = 0x2dd;
  15128. WBUFL(buf,2) = sd->bl.id;
  15129. safestrncpy(WBUFCP(buf,6), sd->status.name, NAME_LENGTH); // name don't show in screen.
  15130. WBUFW(buf,30) = sd->bg_id;
  15131. clif_send(buf,packet_len(0x2dd), &sd->bl, AREA);
  15132. }
  15133. void clif_sendbgemblem_single(int32 fd, map_session_data *sd)
  15134. {
  15135. nullpo_retv(sd);
  15136. WFIFOHEAD(fd,32);
  15137. WFIFOW(fd,0) = 0x2dd;
  15138. WFIFOL(fd,2) = sd->bl.id;
  15139. safestrncpy(WFIFOCP(fd,6), sd->status.name, NAME_LENGTH);
  15140. WFIFOW(fd,30) = sd->bg_id;
  15141. WFIFOSET(fd,packet_len(0x2dd));
  15142. }
  15143. /// Battlegrounds queue incoming apply request from client.
  15144. /// Queue types: 1 solo queue, 2 party queue, 4 guild queue.
  15145. /// 0x8d7 <queue type>.W <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_APPLY)
  15146. void clif_parse_bg_queue_apply_request(int32 fd, map_session_data *sd)
  15147. {
  15148. if (!battle_config.feature_bgqueue)
  15149. return;
  15150. nullpo_retv(sd);
  15151. short type = RFIFOW(fd,2);
  15152. char name[NAME_LENGTH];
  15153. safestrncpy(name, RFIFOCP(fd, 4), NAME_LENGTH);
  15154. if (sd->bg_queue_id > 0) {
  15155. //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);
  15156. clif_bg_queue_apply_result(BG_APPLY_DUPLICATE, name, sd); // Duplicate application warning
  15157. return;
  15158. } else if (type == 1) // Solo
  15159. bg_queue_join_solo(name, sd);
  15160. else if (type == 2) // Party
  15161. bg_queue_join_party(name, sd);
  15162. else if (type == 4) // Guild
  15163. bg_queue_join_guild(name, sd);
  15164. else {
  15165. 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);
  15166. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd); // Someone sent an invalid queue type packet
  15167. return;
  15168. }
  15169. }
  15170. /// Outgoing battlegrounds queue apply result.
  15171. /// Result types: @see e_bg_queue_apply_ack
  15172. /// 0x8d8 <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_APPLY)
  15173. void clif_bg_queue_apply_result(e_bg_queue_apply_ack result, const char *name, map_session_data *sd)
  15174. {
  15175. nullpo_retv(sd);
  15176. int32 fd = sd->fd;
  15177. WFIFOHEAD(fd, packet_len(0x8d8));
  15178. WFIFOW(fd,0) = 0x8d8;
  15179. WFIFOB(fd,2) = result;
  15180. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15181. WFIFOSET(fd, packet_len(0x8d8));
  15182. }
  15183. /// Outgoing battlegrounds queue apply notification.
  15184. /// Sends a notification at the start of the battlegrounds queue and is also used to update the queue number.
  15185. /// 0x8d9 <battleground name>.24B <queue number>.L (ZC_NOTIFY_ENTRY_QUEUE_APPLY)
  15186. void clif_bg_queue_apply_notify(const char *name, map_session_data *sd)
  15187. {
  15188. nullpo_retv(sd);
  15189. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15190. if (queue == nullptr) {
  15191. ShowError("clif_bg_queue_apply_notify: Player is not in a battleground queue.\n");
  15192. return;
  15193. }
  15194. int32 fd = sd->fd;
  15195. WFIFOHEAD(fd, packet_len(0x8d9));
  15196. WFIFOW(fd,0) = 0x8d9;
  15197. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15198. WFIFOL(fd,2+NAME_LENGTH) = static_cast<int32>( queue->teama_members.size() + queue->teamb_members.size() );
  15199. WFIFOSET(fd, packet_len(0x8d9));
  15200. }
  15201. /// Battlegrounds queue outgoing cancel result.
  15202. /// 0x8db <result>.B <battleground name>.24B (ZC_ACK_ENTRY_QUEUE_CANCEL)
  15203. void clif_bg_queue_cancel_result(bool success, const char *name, map_session_data *sd)
  15204. {
  15205. nullpo_retv(sd);
  15206. int32 fd = sd->fd;
  15207. WFIFOHEAD(fd, packet_len(0x8d8));
  15208. WFIFOW(fd,0) = 0x8db;
  15209. WFIFOB(fd,2) = success;
  15210. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15211. WFIFOSET(fd, packet_len(0x8d8));
  15212. }
  15213. /// Battlegrounds queue incoming cancel request from client.
  15214. /// 0x8da <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_CANCEL)
  15215. void clif_parse_bg_queue_cancel_request(int32 fd, map_session_data *sd)
  15216. {
  15217. if (!battle_config.feature_bgqueue)
  15218. return;
  15219. nullpo_retv(sd);
  15220. bool success;
  15221. if (sd->bg_queue_id > 0) {
  15222. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  15223. if (queue && queue->state == QUEUE_STATE_SETUP_DELAY)
  15224. 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.
  15225. else
  15226. success = bg_queue_leave(sd);
  15227. } else {
  15228. 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);
  15229. success = false;
  15230. }
  15231. char name[NAME_LENGTH];
  15232. safestrncpy( name, RFIFOCP( fd, 2 ), NAME_LENGTH );
  15233. clif_bg_queue_cancel_result(success, name, sd);
  15234. }
  15235. /// Battleground is ready to be joined, send a window asking for players to accept or decline.
  15236. /// 0x8df <battleground name>.24B <lobby name>.24B (ZC_NOTIFY_LOBBY_ADMISSION)
  15237. void clif_bg_queue_lobby_notify(const char *name, map_session_data *sd)
  15238. {
  15239. nullpo_retv(sd);
  15240. int32 fd = sd->fd;
  15241. WFIFOHEAD(fd, packet_len(0x8df));
  15242. WFIFOW(fd,0) = 0x8df;
  15243. safestrncpy(WFIFOCP(fd,2), name, NAME_LENGTH);
  15244. safestrncpy(WFIFOCP(fd,2+NAME_LENGTH), name, NAME_LENGTH);
  15245. WFIFOSET(fd, packet_len(0x8df));
  15246. }
  15247. /// Incoming packet from client telling server whether player wants to enter battleground or cancel.
  15248. /// Result types: 1(Accept), 2(Decline).
  15249. /// 0x8e0 <result>.B <battleground name>.24B <lobby name>.24B (CZ_REPLY_LOBBY_ADMISSION)
  15250. void clif_parse_bg_queue_lobby_reply(int32 fd, map_session_data *sd)
  15251. {
  15252. nullpo_retv(sd);
  15253. if(sd->bg_queue_id > 0) {
  15254. uint8 result = RFIFOB(fd, 2);
  15255. if(result == 1) { // Accept
  15256. bg_queue_on_accept_invite(sd);
  15257. } else if(result == 2) { // Decline
  15258. bg_queue_leave(sd);
  15259. clif_bg_queue_entry_init(sd);
  15260. }
  15261. }
  15262. }
  15263. /// Plays a gong sound, signaling that someone has accepted the invite to enter a battleground.
  15264. /// 0x8e1 <result>.B <battleground name>.24B <lobby name>.24B (ZC_REPLY_ACK_LOBBY_ADMISSION)
  15265. void clif_bg_queue_ack_lobby(bool result, const char *name, const char *lobbyname, map_session_data *sd)
  15266. {
  15267. nullpo_retv(sd);
  15268. int32 fd = sd->fd;
  15269. WFIFOHEAD(fd, packet_len(0x8e1));
  15270. WFIFOW(fd,0) = 0x8e1;
  15271. WFIFOB(fd,2) = result;
  15272. safestrncpy(WFIFOCP(fd,3), name, NAME_LENGTH);
  15273. safestrncpy(WFIFOCP(fd,3+NAME_LENGTH), lobbyname, NAME_LENGTH);
  15274. WFIFOSET(fd, packet_len(0x8e1));
  15275. }
  15276. /// Battlegrounds queue incoming queue number request from client.
  15277. /// 0x90a <battleground name>.24B (CZ_REQ_ENTRY_QUEUE_RANKING)
  15278. void clif_parse_bg_queue_request_queue_number(int32 fd, map_session_data *sd)
  15279. {
  15280. nullpo_retv(sd);
  15281. char name[NAME_LENGTH];
  15282. safestrncpy( name, RFIFOCP(fd, 2), NAME_LENGTH );
  15283. clif_bg_queue_apply_notify(name, sd);
  15284. }
  15285. /// Silently removes all the battlegrounds stuff client side so that you will open the first BG window when you press battle on the interface.
  15286. /// Send this when a player joins a battleground so that it will remove all the queue stuff upon warping in.
  15287. /// 0x90e (ZC_ENTRY_QUEUE_INIT)
  15288. void clif_bg_queue_entry_init(map_session_data *sd)
  15289. {
  15290. nullpo_retv(sd);
  15291. PACKET_ZC_ENTRY_QUEUE_INIT p = {};
  15292. p.packetType = HEADER_ZC_ENTRY_QUEUE_INIT;
  15293. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15294. }
  15295. /// Custom Fonts (ZC_NOTIFY_FONT).
  15296. /// 02ef <account_id>.L <font id>.W
  15297. void clif_font(map_session_data *sd)
  15298. {
  15299. #if PACKETVER >= 20080102
  15300. unsigned char buf[8];
  15301. nullpo_retv(sd);
  15302. WBUFW(buf,0) = 0x2ef;
  15303. WBUFL(buf,2) = sd->bl.id;
  15304. WBUFW(buf,6) = sd->status.font;
  15305. clif_send(buf, packet_len(0x2ef), &sd->bl, AREA);
  15306. #endif
  15307. }
  15308. /// Required to start the instancing information window on Client
  15309. /// This window re-appears each "refresh" of client automatically until the keep_limit reaches 0.
  15310. /// S 0x2cb <Instance name>.61B <Standby Position>.W
  15311. void clif_instance_create( int32 instance_id, size_t num ){
  15312. #if PACKETVER >= 20071128
  15313. map_session_data *sd = nullptr;
  15314. enum send_target target = PARTY;
  15315. unsigned char buf[65];
  15316. instance_getsd(instance_id, sd, &target);
  15317. if (!sd)
  15318. return;
  15319. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15320. if (!db)
  15321. return;
  15322. WBUFW(buf,0) = 0x2cb;
  15323. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15324. WBUFW( buf, 63 ) = static_cast<int16>( num );
  15325. clif_send(buf,packet_len(0x2cb),&sd->bl,target);
  15326. #endif
  15327. }
  15328. /// To announce Instancing queue creation if no maps available
  15329. /// S 0x2cc <Standby Position>.W
  15330. void clif_instance_changewait(int32 instance_id, int32 num)
  15331. {
  15332. #if PACKETVER >= 20071128
  15333. map_session_data *sd = nullptr;
  15334. enum send_target target = PARTY;
  15335. unsigned char buf[4];
  15336. instance_getsd(instance_id, sd, &target);
  15337. if (!sd)
  15338. return;
  15339. WBUFW(buf,0) = 0x2cc;
  15340. WBUFW(buf,2) = num;
  15341. clif_send(buf,packet_len(0x2cc),&sd->bl,target);
  15342. #endif
  15343. return;
  15344. }
  15345. /// Notify the current status to members
  15346. /// S 0x2cd <Instance Name>.61B <Instance Remaining Time>.L <Instance Noplayers close time>.L
  15347. void clif_instance_status(int32 instance_id, uint32 limit1, uint32 limit2)
  15348. {
  15349. #if PACKETVER >= 20071128
  15350. map_session_data *sd = nullptr;
  15351. enum send_target target = PARTY;
  15352. unsigned char buf[71];
  15353. instance_getsd(instance_id, sd, &target);
  15354. if (!sd)
  15355. return;
  15356. std::shared_ptr<s_instance_db> db = instance_db.find(util::umap_find(instances, instance_id)->id);
  15357. if (!db)
  15358. return;
  15359. WBUFW(buf,0) = 0x2cd;
  15360. safestrncpy(WBUFCP(buf,2), db->name.c_str(), INSTANCE_NAME_LENGTH);
  15361. WBUFL(buf,63) = limit1;
  15362. WBUFL(buf,67) = limit2;
  15363. clif_send(buf,packet_len(0x2cd),&sd->bl,target);
  15364. #endif
  15365. return;
  15366. }
  15367. /// Notify a status change to members
  15368. /// S 0x2ce <Message ID>.L
  15369. /// 0 = Notification (EnterLimitDate update?)
  15370. /// 1 = The Memorial Dungeon expired; it has been destroyed
  15371. /// 2 = The Memorial Dungeon's entry time limit expired; it has been destroyed
  15372. /// 3 = The Memorial Dungeon has been removed.
  15373. /// 4 = Create failure (removes the instance window)
  15374. void clif_instance_changestatus(int32 instance_id, e_instance_notify type, uint32 limit)
  15375. {
  15376. #if PACKETVER >= 20071128
  15377. map_session_data *sd = nullptr;
  15378. enum send_target target = PARTY;
  15379. unsigned char buf[10];
  15380. instance_getsd(instance_id, sd, &target);
  15381. if (!sd)
  15382. return;
  15383. WBUFW(buf,0) = 0x2ce;
  15384. WBUFL(buf,2) = type;
  15385. WBUFL(buf,6) = limit;
  15386. clif_send(buf,packet_len(0x2ce),&sd->bl,target);
  15387. #endif
  15388. return;
  15389. }
  15390. /// Destroy an instance from the status window
  15391. /// 02cf <command>.L (CZ_MEMORIALDUNGEON_COMMAND)
  15392. void clif_parse_MemorialDungeonCommand(int32 fd, map_session_data *sd)
  15393. {
  15394. if (pc_istrading(sd) || pc_isdead(sd) || map_getmapdata(sd->bl.m)->instance_id > 0)
  15395. return;
  15396. const PACKET_CZ_MEMORIALDUNGEON_COMMAND* p = reinterpret_cast<PACKET_CZ_MEMORIALDUNGEON_COMMAND*>( RFIFOP( fd, 0 ) );
  15397. switch (p->command) {
  15398. case COMMAND_MEMORIALDUNGEON_DESTROY_FORCE:
  15399. instance_destroy_command(sd);
  15400. break;
  15401. }
  15402. }
  15403. void clif_instance_info( map_session_data& sd ){
  15404. if( sd.instance_id > 0 ){
  15405. instance_reqinfo( &sd, sd.instance_id );
  15406. }
  15407. if( sd.status.party_id > 0 ){
  15408. struct party_data* p = party_search( sd.status.party_id );
  15409. if( p != nullptr && p->instance_id > 0 ){
  15410. instance_reqinfo( &sd, p->instance_id );
  15411. }
  15412. }
  15413. if( sd.guild != nullptr && sd.guild->instance_id > 0 ){
  15414. instance_reqinfo( &sd, sd.guild->instance_id );
  15415. }
  15416. if( sd.clan != nullptr && sd.clan->instance_id > 0 ){
  15417. instance_reqinfo( &sd, sd.clan->instance_id );
  15418. }
  15419. }
  15420. /// Notifies clients about item picked up by a party member.
  15421. /// 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)
  15422. void clif_party_show_picker( map_session_data* sd, struct item* item_data ){
  15423. #if PACKETVER >= 20071002
  15424. nullpo_retv( sd );
  15425. nullpo_retv( item_data );
  15426. struct item_data* id = itemdb_search( item_data->nameid );
  15427. PACKET_ZC_ITEM_PICKUP_PARTY p = {};
  15428. p.packetType = HEADER_ZC_ITEM_PICKUP_PARTY;
  15429. p.AID = sd->status.account_id;
  15430. p.itemId = client_nameid( item_data->nameid );
  15431. p.identified = item_data->identify;
  15432. p.damaged = item_data->attribute;
  15433. p.refine = item_data->refine;
  15434. clif_addcards( &p.slot, item_data );
  15435. p.location = id->equip; // equip location
  15436. p.itemType = itemtype( id->nameid ); // item type
  15437. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15438. p.grade = item_data->enchantgrade;
  15439. #endif
  15440. clif_send( &p, sizeof( p ), &sd->bl, PARTY_SAMEMAP_WOS );
  15441. #endif
  15442. }
  15443. /** Display gained exp.
  15444. * 07f6 <account id>.L <amount>.L <var id>.W <exp type>.W (ZC_NOTIFY_EXP)
  15445. * 0acc <account id>.L <amount>.Q <var id>.W <exp type>.W (ZC_NOTIFY_EXP2)
  15446. * amount: INT32_MIN ~ INT32_MAX
  15447. * var id:
  15448. * SP_BASEEXP, SP_JOBEXP
  15449. * exp type:
  15450. * 0 = normal exp gained/lost
  15451. * 1 = quest exp gained/lost
  15452. * @param sd Player
  15453. * @param exp EXP value gained/loss
  15454. * @param type SP_BASEEXP, SP_JOBEXP
  15455. * @param quest False:Normal EXP; True:Quest EXP (displayed in purple color)
  15456. * @param lost True:if losing EXP
  15457. */
  15458. void clif_displayexp(map_session_data *sd, t_exp exp, char type, bool quest, bool lost)
  15459. {
  15460. int32 fd;
  15461. int32 offset;
  15462. #if PACKETVER >= 20170830
  15463. int32 cmd = 0xacc;
  15464. #else
  15465. int32 cmd = 0x7f6;
  15466. #endif
  15467. nullpo_retv(sd);
  15468. fd = sd->fd;
  15469. WFIFOHEAD(fd, packet_len(cmd));
  15470. WFIFOW(fd,0) = cmd;
  15471. WFIFOL(fd,2) = sd->bl.id;
  15472. #if PACKETVER >= 20170830
  15473. WFIFOQ(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15474. offset = 4;
  15475. #else
  15476. WFIFOL(fd,6) = client_exp(exp) * (lost ? -1 : 1);
  15477. offset = 0;
  15478. #endif
  15479. WFIFOW(fd,10+offset) = type;
  15480. WFIFOW(fd,12+offset) = (quest && type != SP_JOBEXP) ? 1 : 0; // NOTE: Somehow JobEXP always in yellow color
  15481. WFIFOSET(fd,packet_len(cmd));
  15482. }
  15483. /// Displays digital clock digits on top of the screen (ZC_SHOWDIGIT).
  15484. /// type:
  15485. /// 0 = Displays 'value' for 5 seconds.
  15486. /// 1 = Incremental counter (1 tick/second), negated 'value' specifies start value (e.g. using -10 lets the counter start at 10).
  15487. /// 2 = Decremental counter (1 tick/second), negated 'value' specifies start value (does not stop when reaching 0, but overflows).
  15488. /// 3 = Decremental counter (1 tick/second), 'value' specifies start value (stops when reaching 0, displays at most 2 digits).
  15489. /// value:
  15490. /// 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).
  15491. void clif_showdigit(map_session_data* sd, unsigned char type, int32 value)
  15492. {
  15493. WFIFOHEAD(sd->fd, packet_len(0x1b1));
  15494. WFIFOW(sd->fd,0) = 0x1b1;
  15495. WFIFOB(sd->fd,2) = type;
  15496. WFIFOL(sd->fd,3) = value;
  15497. WFIFOSET(sd->fd, packet_len(0x1b1));
  15498. }
  15499. /// Notification of the state of client command /effect.
  15500. /// 021d <state>.L (CZ_LESSEFFECT)
  15501. /// state:
  15502. /// 0 = Full effects
  15503. /// 1 = Reduced effects
  15504. ///
  15505. /// NOTE: The state is used on Aegis for sending skill unit packet
  15506. /// 0x11f (ZC_SKILL_ENTRY) instead of 0x1c9 (ZC_SKILL_ENTRY2)
  15507. /// whenever possible. Due to the way the decision check is
  15508. /// constructed, this state tracking was rendered useless,
  15509. /// as the only skill unit, that is sent with 0x1c9 is
  15510. /// Graffiti.
  15511. void clif_parse_LessEffect(int32 fd, map_session_data* sd){
  15512. #if PACKETVER >= 20041115
  15513. const PACKET_CZ_LESSEFFECT* p = reinterpret_cast<PACKET_CZ_LESSEFFECT*>( RFIFOP( fd, 0 ) );
  15514. sd->state.lesseffect = p->state != 0;
  15515. #endif
  15516. }
  15517. /// 07e4 <length>.w <option>.l <val>.l {<index>.w <amount>.w).4b* (CZ_ITEMLISTWIN_RES)
  15518. void clif_parse_ItemListWindowSelected(int32 fd, map_session_data* sd) {
  15519. if( sd == nullptr ){
  15520. return;
  15521. }
  15522. struct s_packet_db* info = &packet_db[RFIFOW(fd,0)];
  15523. int32 n = (RFIFOW(fd,info->pos[0])-12) / 4;
  15524. int32 type = RFIFOL(fd,info->pos[1]);
  15525. int32 flag = RFIFOL(fd,info->pos[2]); // Button clicked: 0 = Cancel, 1 = OK
  15526. unsigned short* item_list = (unsigned short*)RFIFOP(fd,info->pos[3]);
  15527. if( sd->state.trading || sd->npc_shopid )
  15528. return;
  15529. if( flag == 0 || n == 0) {
  15530. clif_menuskill_clear(sd);
  15531. return; // Canceled by player.
  15532. }
  15533. if( sd->menuskill_id != SO_EL_ANALYSIS && sd->menuskill_id != GN_CHANGEMATERIAL ) {
  15534. clif_menuskill_clear(sd);
  15535. return; // Prevent hacking.
  15536. }
  15537. switch( type ) {
  15538. case 0: // Change Material
  15539. skill_changematerial(sd,n,item_list);
  15540. break;
  15541. case 1: // Level 1: Pure to Rough
  15542. case 2: // Level 2: Rough to Pure
  15543. skill_elementalanalysis(*sd,n,type,item_list);
  15544. break;
  15545. }
  15546. clif_menuskill_clear(sd);
  15547. return;
  15548. }
  15549. /*==========================================
  15550. * Elemental System
  15551. *==========================================*/
  15552. /// Notifies client of a change in an elemental's status parameter.
  15553. /// 0x81e <type>.W <value>.L (ZC_EL_PAR_CHANGE)
  15554. void clif_elemental_updatestatus(map_session_data& sd, _sp type) {
  15555. #if PACKETVER >= 20100309
  15556. if (sd.ed == nullptr)
  15557. return;
  15558. PACKET_ZC_EL_PAR_CHANGE p = {};
  15559. p.packetType = HEADER_ZC_EL_PAR_CHANGE;
  15560. p.type = static_cast<decltype(p.type)>(type);
  15561. status_data* status = &sd.ed->battle_status;
  15562. switch( type ) {
  15563. case SP_HP:
  15564. p.value = static_cast<decltype(p.value)>(status->hp);
  15565. break;
  15566. case SP_MAXHP:
  15567. p.value = static_cast<decltype(p.value)>(status->max_hp);
  15568. break;
  15569. case SP_SP:
  15570. p.value = static_cast<decltype(p.value)>(status->sp);
  15571. break;
  15572. case SP_MAXSP:
  15573. p.value = static_cast<decltype(p.value)>(status->max_sp);
  15574. break;
  15575. }
  15576. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  15577. #endif
  15578. }
  15579. void clif_elemental_info(map_session_data *sd) {
  15580. int32 fd;
  15581. s_elemental_data *ed;
  15582. struct status_data *status;
  15583. if( !clif_session_isValid(sd) || (ed = sd->ed) == nullptr )
  15584. return;
  15585. fd = sd->fd;
  15586. status = &ed->battle_status;
  15587. WFIFOHEAD(fd,22);
  15588. WFIFOW(fd, 0) = 0x81d;
  15589. WFIFOL(fd, 2) = ed->bl.id;
  15590. WFIFOL(fd, 6) = status->hp;
  15591. WFIFOL(fd,10) = status->max_hp;
  15592. WFIFOL(fd,14) = status->sp;
  15593. WFIFOL(fd,18) = status->max_sp;
  15594. WFIFOSET(fd,22);
  15595. }
  15596. /// Buying Store System
  15597. ///
  15598. /// Opens preparation window for buying store (ZC_OPEN_BUYING_STORE).
  15599. /// 0810 <slots>.B
  15600. void clif_buyingstore_open(map_session_data* sd)
  15601. {
  15602. int32 fd = sd->fd;
  15603. WFIFOHEAD(fd,packet_len(0x810));
  15604. WFIFOW(fd,0) = 0x810;
  15605. WFIFOB(fd,2) = sd->buyingstore.slots;
  15606. WFIFOSET(fd,packet_len(0x810));
  15607. }
  15608. /// Request to create a buying store.
  15609. /// 0811 <packet len>.W <limit zeny>.L <result>.B <store name>.80B { <name id>.W <amount>.W <price>.L }* (CZ_REQ_OPEN_BUYING_STORE)
  15610. /// result:
  15611. /// 0 = cancel
  15612. /// 1 = open
  15613. static void clif_parse_ReqOpenBuyingStore( int32 fd, map_session_data* sd ){
  15614. const PACKET_CZ_REQ_OPEN_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_OPEN_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15615. // TODO: Make this check global for all variable length packets.
  15616. // minimum packet length
  15617. if( p->packetLength < sizeof( *p ) ){
  15618. ShowError( "clif_parse_ReqOpenBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15619. return;
  15620. }
  15621. if (map_getmapflag(sd->bl.m, MF_NOBUYINGSTORE)) {
  15622. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  15623. return;
  15624. }
  15625. if (map_getcell(sd->bl.m, sd->bl.x, sd->bl.y, CELL_CHKNOBUYINGSTORE)) {
  15626. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  15627. return;
  15628. }
  15629. char storename[MESSAGE_SIZE];
  15630. safestrncpy( storename, p->storeName, sizeof( storename ) );
  15631. // so that buyingstore_create knows, how many elements it has access to
  15632. int32 packet_len = p->packetLength - sizeof( *p );
  15633. if( packet_len % sizeof( p->items[0] ) ){
  15634. 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] ) );
  15635. return;
  15636. }
  15637. buyingstore_create( sd, p->zenyLimit, p->result, storename, p->items, packet_len / sizeof( p->items[0] ), nullptr );
  15638. }
  15639. /// Notification, that the requested buying store could not be created (ZC_FAILED_OPEN_BUYING_STORE_TO_BUYER).
  15640. /// 0812 <result>.W <total weight>.L
  15641. /// result:
  15642. /// 1 = "Failed to open buying store." (0x6cd, MSI_BUYINGSTORE_OPEN_FAILED)
  15643. /// 2 = "Total amount of then possessed items exceeds the weight limit by <weight/10-maxweight*90%>. Please re-enter." (0x6ce, MSI_BUYINGSTORE_OVERWEIGHT)
  15644. /// 8 = "No sale (purchase) information available." (0x705)
  15645. /// ? = nothing
  15646. void clif_buyingstore_open_failed(map_session_data* sd, unsigned short result, uint32 weight)
  15647. {
  15648. int32 fd = sd->fd;
  15649. WFIFOHEAD(fd,packet_len(0x812));
  15650. WFIFOW(fd,0) = 0x812;
  15651. WFIFOW(fd,2) = result;
  15652. WFIFOL(fd,4) = weight;
  15653. WFIFOSET(fd,packet_len(0x812));
  15654. }
  15655. /// Notification, that the requested buying store was created.
  15656. /// 0813 <packet len>.W <account id>.L <limit zeny>.L { <price>.L <count>.W <type>.B <name id>.W }* (ZC_MYITEMLIST_BUYING_STORE)
  15657. void clif_buyingstore_myitemlist( map_session_data& sd ){
  15658. #if PACKETVER >= 20100309
  15659. PACKET_ZC_MYITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_MYITEMLIST_BUYING_STORE*>( packet_buffer );
  15660. p->packetType = HEADER_ZC_MYITEMLIST_BUYING_STORE;
  15661. p->packetLength = sizeof( *p );
  15662. p->AID = sd.bl.id;
  15663. p->zenyLimit = sd.buyingstore.zenylimit;
  15664. for( int32 i = 0; i < sd.buyingstore.slots; i++ ){
  15665. PACKET_ZC_MYITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15666. entry.price = sd.buyingstore.items[i].price;
  15667. entry.amount = sd.buyingstore.items[i].amount;
  15668. entry.itemType = itemtype( sd.buyingstore.items[i].nameid );
  15669. entry.itemId = client_nameid( sd.buyingstore.items[i].nameid );
  15670. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15671. }
  15672. clif_send( p, p->packetLength, &sd.bl, SELF );
  15673. #endif
  15674. }
  15675. /// Notifies clients in area of a buying store (ZC_BUYING_STORE_ENTRY).
  15676. /// 0814 <account id>.L <store name>.80B
  15677. void clif_buyingstore_entry( map_session_data& sd, struct block_list* tbl ){
  15678. #if PACKETVER >= 20100420
  15679. enum send_target target;
  15680. if( tbl == nullptr ){
  15681. tbl = &sd.bl;
  15682. target = AREA_WOS;
  15683. }else{
  15684. target = SELF;
  15685. }
  15686. PACKET_ZC_BUYING_STORE_ENTRY p = {};
  15687. p.packetType = HEADER_ZC_BUYING_STORE_ENTRY;
  15688. p.makerAID = sd.status.account_id;
  15689. safestrncpy( p.storeName, sd.message, sizeof( p.storeName ) );
  15690. clif_send( &p, sizeof( p ), tbl, target );
  15691. #endif
  15692. }
  15693. /// Request to close own buying store (CZ_REQ_CLOSE_BUYING_STORE).
  15694. /// 0815
  15695. static void clif_parse_ReqCloseBuyingStore(int32 fd, map_session_data* sd)
  15696. {
  15697. buyingstore_close(sd);
  15698. }
  15699. /// Notifies clients in area that a buying store was closed (ZC_DISAPPEAR_BUYING_STORE_ENTRY).
  15700. /// 0816 <account id>.L
  15701. void clif_buyingstore_disappear_entry( map_session_data& sd, struct block_list* tbl ){
  15702. #if PACKETVER >= 20100309
  15703. enum send_target target;
  15704. if( tbl == nullptr ){
  15705. tbl = &sd.bl;
  15706. target = AREA_WOS;
  15707. }else{
  15708. target = SELF;
  15709. }
  15710. PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY p = {};
  15711. p.packetType = HEADER_ZC_DISAPPEAR_BUYING_STORE_ENTRY;
  15712. p.makerAID = sd.status.account_id;
  15713. clif_send( &p, sizeof( p ), tbl, target );
  15714. #endif
  15715. }
  15716. /// Request to open someone else's buying store (CZ_REQ_CLICK_TO_BUYING_STORE).
  15717. /// 0817 <account id>.L
  15718. static void clif_parse_ReqClickBuyingStore(int32 fd, map_session_data* sd)
  15719. {
  15720. uint32 account_id;
  15721. account_id = RFIFOL(fd,packet_db[RFIFOW(fd,0)].pos[0]);
  15722. buyingstore_open(sd, account_id);
  15723. }
  15724. /// Sends buying store item list.
  15725. /// 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)
  15726. void clif_buyingstore_itemlist( map_session_data& sd, map_session_data& pl_sd ){
  15727. #if PACKETVER >= 20100309
  15728. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE* p = reinterpret_cast<PACKET_ZC_ACK_ITEMLIST_BUYING_STORE*>( packet_buffer );
  15729. p->packetType = HEADER_ZC_ACK_ITEMLIST_BUYING_STORE;
  15730. p->packetLength = sizeof( *p );
  15731. p->AID = pl_sd.bl.id;
  15732. p->storeId = pl_sd.buyer_id;
  15733. p->zenyLimit = pl_sd.buyingstore.zenylimit;
  15734. for( int32 i = 0; i < pl_sd.buyingstore.slots; i++ ){
  15735. PACKET_ZC_ACK_ITEMLIST_BUYING_STORE_sub& entry = p->items[i];
  15736. entry.price = pl_sd.buyingstore.items[i].price;
  15737. entry.amount = pl_sd.buyingstore.items[i].amount; // TODO: Figure out, if no longer needed items (amount == 0) are listed on official.
  15738. entry.itemType = itemtype(pl_sd.buyingstore.items[i].nameid);
  15739. entry.itemId = client_nameid( pl_sd.buyingstore.items[i].nameid );
  15740. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15741. }
  15742. clif_send( p, p->packetLength, &sd.bl, SELF );
  15743. #endif
  15744. }
  15745. /// Request to sell items to a buying store.
  15746. /// 0819 <packet len>.W <account id>.L <store id>.L { <index>.W <name id>.W <amount>.W }* (CZ_REQ_TRADE_BUYING_STORE)
  15747. static void clif_parse_ReqTradeBuyingStore( int32 fd, map_session_data* sd ){
  15748. const PACKET_CZ_REQ_TRADE_BUYING_STORE* p = reinterpret_cast<PACKET_CZ_REQ_TRADE_BUYING_STORE*>( RFIFOP( fd, 0 ) );
  15749. // minimum packet length
  15750. if( p->packetLength < sizeof( *p ) ){
  15751. ShowError( "clif_parse_ReqTradeBuyingStore: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15752. return;
  15753. }
  15754. // so that buyingstore_trade knows, how many elements it has access to
  15755. int32 packet_len = p->packetLength - sizeof( *p );
  15756. if( packet_len % sizeof( p->items[0] ) ){
  15757. 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] ) );
  15758. return;
  15759. }
  15760. buyingstore_trade( sd, p->AID, p->storeId, p->items, packet_len / sizeof( p->items[0] ) );
  15761. }
  15762. /// Notifies the buyer, that the buying store has been closed due to a post-trade condition (ZC_FAILED_TRADE_BUYING_STORE_TO_BUYER).
  15763. /// 081a <result>.W
  15764. /// result:
  15765. /// 3 = "All items within the buy limit were purchased." (0x6cf, MSI_BUYINGSTORE_TRADE_OVERLIMITZENY)
  15766. /// 4 = "All items were purchased." (0x6d0, MSI_BUYINGSTORE_TRADE_BUYCOMPLETE)
  15767. /// ? = nothing
  15768. void clif_buyingstore_trade_failed_buyer(map_session_data* sd, short result)
  15769. {
  15770. int32 fd = sd->fd;
  15771. WFIFOHEAD(fd,packet_len(0x81a));
  15772. WFIFOW(fd,0) = 0x81a;
  15773. WFIFOW(fd,2) = result;
  15774. WFIFOSET(fd,packet_len(0x81a));
  15775. }
  15776. /// Updates the zeny limit and an item in the buying store item list.
  15777. /// 081b <name id>.W <amount>.W <limit zeny>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE)
  15778. /// 09e6 <name id>.W <amount>.W <zeny>.L <limit zeny>.L <GID>.L <Date>.L (ZC_UPDATE_ITEM_FROM_BUYING_STORE2)
  15779. void clif_buyingstore_update_item( map_session_data* sd, t_itemid nameid, unsigned short amount, uint32 char_id, int32 zeny ){
  15780. PACKET_ZC_UPDATE_ITEM_FROM_BUYING_STORE p = {};
  15781. p.packetType = buyingStoreUpdateItemType;
  15782. p.itemId = client_nameid( nameid );
  15783. p.amount = amount;
  15784. p.zenyLimit = sd->buyingstore.zenylimit;
  15785. #if PACKETVER >= 20141016
  15786. p.zeny = zeny;
  15787. p.charId = char_id; // GID
  15788. p.updateTime = (int32)time(nullptr);
  15789. #endif
  15790. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15791. }
  15792. /// Deletes item from inventory, that was sold to a buying store (ZC_ITEM_DELETE_BUYING_STORE).
  15793. /// 081c <index>.W <amount>.W <price>.L
  15794. /// message:
  15795. /// "%s (%d) were sold at %dz." (0x6d2, MSI_BUYINGSTORE_TRADE_SELLCOMPLETE)
  15796. ///
  15797. /// NOTE: This function has to be called _instead_ of clif_delitem/clif_dropitem.
  15798. void clif_buyingstore_delete_item(map_session_data* sd, short index, unsigned short amount, int32 price)
  15799. {
  15800. int32 fd = sd->fd;
  15801. WFIFOHEAD(fd,packet_len(0x81c));
  15802. WFIFOW(fd,0) = 0x81c;
  15803. WFIFOW(fd,2) = client_index( index );
  15804. WFIFOW(fd,4) = amount;
  15805. WFIFOL(fd,6) = price; // price per item, client calculates total Zeny by itself
  15806. WFIFOSET(fd,packet_len(0x81c));
  15807. }
  15808. /// Notifies the seller, that a buying store trade failed.
  15809. /// 0824 <result>.W <name id>.W (ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER)
  15810. /// result:
  15811. /// 5 = "The deal has failed." (0x39, MSI_DEAL_FAIL)
  15812. /// 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)
  15813. /// 7 = "The trade failed, because the buyer is lacking required balance." (0x6d1, MSI_BUYINGSTORE_TRADE_LACKBUYERZENY)
  15814. /// ? = nothing
  15815. void clif_buyingstore_trade_failed_seller( map_session_data* sd, short result, t_itemid nameid ){
  15816. PACKET_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER p = {};
  15817. p.packetType = HEADER_ZC_FAILED_TRADE_BUYING_STORE_TO_SELLER;
  15818. p.result = result;
  15819. p.itemId = client_nameid( nameid );
  15820. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  15821. }
  15822. /// Search Store Info System
  15823. ///
  15824. /// Request to search for stores.
  15825. /// 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)
  15826. /// type:
  15827. /// 0 = Vending
  15828. /// 1 = Buying Store
  15829. ///
  15830. /// NOTE: The client determines the item ids by specifying a name and optionally,
  15831. /// amount of card slots. If the client does not know about the item it
  15832. /// cannot be searched.
  15833. static void clif_parse_SearchStoreInfo( int32 fd, map_session_data *sd ){
  15834. if (!battle_config.feature_search_stores)
  15835. return;
  15836. const PACKET_CZ_SEARCH_STORE_INFO* p = reinterpret_cast<PACKET_CZ_SEARCH_STORE_INFO*>( RFIFOP( fd, 0 ) );
  15837. // minimum packet length
  15838. if( p->packetLength < sizeof( *p ) ){
  15839. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected length=%" PRIdPTR ", length=%u, account_id=%d).\n", sizeof( *p ), p->packetLength, sd->bl.id );
  15840. return;
  15841. }
  15842. // check, if there is enough data for the claimed count of items
  15843. int32 packet_len = p->packetLength - sizeof( *p );
  15844. if( packet_len % sizeof( p->items[0] ) ){
  15845. 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] ) );
  15846. return;
  15847. }
  15848. int32 count = packet_len / sizeof( p->items[0] );
  15849. if( count < p->itemsCount + p->cardsCount ){
  15850. ShowError( "clif_parse_SearchStoreInfo: Malformed packet (expected count=%u, count=%u, account_id=%d).\n", p->itemsCount + p->cardsCount, count, sd->bl.id );
  15851. return;
  15852. }
  15853. if ( p->searchType > SEARCHTYPE_BUYING_STORE ) {
  15854. ShowError( "clif_parse_SearchStoreInfo: Invalid search type %u (account_id=%d).\n", p->searchType, sd->bl.id );
  15855. return;
  15856. }
  15857. if ( p->minPrice > battle_config.vending_max_value ) {
  15858. ShowError( "clif_parse_SearchStoreInfo: Invalid min price %u (account_id=%d).\n", p->minPrice, sd->bl.id );
  15859. return;
  15860. }
  15861. if ( p->maxPrice > battle_config.vending_max_value ) {
  15862. ShowError( "clif_parse_SearchStoreInfo: Invalid max price %u (account_id=%d).\n", p->maxPrice, sd->bl.id );
  15863. return;
  15864. }
  15865. 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 );
  15866. }
  15867. /// Results for a store search request.
  15868. /// 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)
  15869. /// is first page:
  15870. /// 0 = appends to existing results
  15871. /// 1 = clears previous results before displaying this result set
  15872. /// is next page:
  15873. /// 0 = no "next" label
  15874. /// 1 = "next" label to retrieve more results
  15875. void clif_search_store_info_ack( map_session_data& sd ){
  15876. #if PACKETVER_MAIN_NUM >= 20100817 || PACKETVER_RE_NUM >= 20100706 || defined(PACKETVER_ZERO)
  15877. if (!battle_config.feature_search_stores)
  15878. return;
  15879. uint32 start = sd.searchstore.pages * SEARCHSTORE_RESULTS_PER_PAGE ;
  15880. uint32 end = umin( static_cast<uint32>( sd.searchstore.items.size() ), start + SEARCHSTORE_RESULTS_PER_PAGE );
  15881. PACKET_ZC_SEARCH_STORE_INFO_ACK* p = reinterpret_cast<PACKET_ZC_SEARCH_STORE_INFO_ACK*>( packet_buffer );
  15882. p->packetType = HEADER_ZC_SEARCH_STORE_INFO_ACK;
  15883. p->packetLength = sizeof( *p );
  15884. p->firstPage = !sd.searchstore.pages;
  15885. p->nextPage = searchstore_querynext( sd );
  15886. p->usesCount = static_cast<decltype(p->usesCount)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(p->usesCount)>::max() ) );
  15887. for( int32 i = 0, count = 0; i < end - start; i++ ){
  15888. std::shared_ptr<s_search_store_info_item> ssitem = sd.searchstore.items[start + i];
  15889. if( ssitem == nullptr ){
  15890. continue;
  15891. }
  15892. PACKET_ZC_SEARCH_STORE_INFO_ACK_sub& entry = p->items[count];
  15893. entry.storeId = ssitem->store_id;
  15894. entry.AID = ssitem->account_id;
  15895. safestrncpy( entry.shopName, ssitem->store_name, MESSAGE_SIZE );
  15896. entry.itemId = client_nameid( ssitem->nameid );
  15897. entry.itemType = itemtype( ssitem->nameid );
  15898. entry.price = ssitem->price;
  15899. entry.amount = ssitem->amount;
  15900. entry.refine = ssitem->refine;
  15901. // make-up an item for clif_addcards
  15902. struct item it = {};
  15903. for( int32 j = 0; j < MAX_SLOTS; j++ ){
  15904. it.card[j] = ssitem->card[j];
  15905. }
  15906. it.nameid = ssitem->nameid;
  15907. it.amount = ssitem->amount;
  15908. clif_addcards( &entry.slot, &it );
  15909. #if PACKETVER >= 20150226
  15910. clif_add_random_options( entry.option_data, it );
  15911. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  15912. entry.grade = ssitem->enchantgrade;
  15913. #endif
  15914. #endif
  15915. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  15916. count++;
  15917. }
  15918. clif_send( p, p->packetLength, &sd.bl, SELF );
  15919. #endif
  15920. }
  15921. /// Notification of failure when searching for stores.
  15922. /// 0837 <reason>.B (ZC_SEARCH_STORE_INFO_FAILED)
  15923. void clif_search_store_info_failed(map_session_data& sd, e_searchstore_failure reason){
  15924. #if PACKETVER_MAIN_NUM >= 20100601 || PACKETVER_RE_NUM >= 20100601 || defined(PACKETVER_ZERO)
  15925. if (!battle_config.feature_search_stores)
  15926. return;
  15927. PACKET_ZC_SEARCH_STORE_INFO_FAILED packet{};
  15928. packet.packetType = HEADER_ZC_SEARCH_STORE_INFO_FAILED;
  15929. packet.reason = static_cast<decltype(packet.reason)>(reason);
  15930. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15931. #endif
  15932. }
  15933. /// Request to display next page of results.
  15934. /// 0838 (CZ_SEARCH_STORE_INFO_NEXT_PAGE)
  15935. static void clif_parse_SearchStoreInfoNextPage(int32 fd, map_session_data* sd){
  15936. if (!battle_config.feature_search_stores)
  15937. return;
  15938. searchstore_next(*sd);
  15939. }
  15940. /// Opens the search store window.
  15941. /// 083a <effect>.W <remaining uses>.B (ZC_OPEN_SEARCH_STORE_INFO)
  15942. void clif_open_search_store_info(map_session_data& sd){
  15943. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15944. if (!battle_config.feature_search_stores)
  15945. return;
  15946. PACKET_ZC_OPEN_SEARCH_STORE_INFO packet{};
  15947. packet.packetType = HEADER_ZC_OPEN_SEARCH_STORE_INFO;
  15948. packet.effect = static_cast<decltype(packet.effect)>(sd.searchstore.effect);
  15949. #if PACKETVER_MAIN_NUM >= 20100701 || PACKETVER_RE_NUM >= 20100701 || defined(PACKETVER_ZERO)
  15950. packet.remainingUses = static_cast<decltype(packet.remainingUses)>( std::min<decltype(sd.searchstore.uses)>( sd.searchstore.uses, std::numeric_limits<decltype(packet.remainingUses)>::max() ) );
  15951. #endif
  15952. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15953. #endif
  15954. }
  15955. /// Request to close the store search window.
  15956. /// 083b (CZ_CLOSE_SEARCH_STORE_INFO)
  15957. static void clif_parse_CloseSearchStoreInfo(int32 fd, map_session_data* sd){
  15958. if (!battle_config.feature_search_stores)
  15959. return;
  15960. searchstore_close(*sd);
  15961. }
  15962. /// Request to invoke catalog effect on a store from search results.
  15963. /// 083c <account id>.L <store id>.L <nameid>.W (CZ_SSILIST_ITEM_CLICK)
  15964. static void clif_parse_SearchStoreInfoListItemClick( int32 fd, map_session_data* sd ){
  15965. if (!battle_config.feature_search_stores)
  15966. return;
  15967. const PACKET_CZ_SSILIST_ITEM_CLICK* p = reinterpret_cast<PACKET_CZ_SSILIST_ITEM_CLICK*>( RFIFOP( fd, 0 ) );
  15968. searchstore_click( *sd, p->AID, p->storeId, p->itemId );
  15969. }
  15970. /// Notification of the store position on current map.
  15971. /// 083d <xPos>.W <yPos>.W (ZC_SSILIST_ITEM_CLICK_ACK)
  15972. void clif_search_store_info_click_ack(map_session_data& sd, int16 x, int16 y){
  15973. #if PACKETVER_MAIN_NUM >= 20100608 || PACKETVER_RE_NUM >= 20100608 || defined(PACKETVER_ZERO)
  15974. if (!battle_config.feature_search_stores)
  15975. return;
  15976. PACKET_ZC_SSILIST_ITEM_CLICK_ACK packet{};
  15977. packet.packetType = HEADER_ZC_SSILIST_ITEM_CLICK_ACK;
  15978. packet.x = x;
  15979. packet.y = y;
  15980. clif_send(&packet, sizeof(packet), &sd.bl, SELF);
  15981. #endif
  15982. }
  15983. /// Parse function for packet debugging.
  15984. void clif_parse_debug(int32 fd,map_session_data *sd)
  15985. {
  15986. int16 packet_len;
  15987. // clif_parse ensures, that there is at least 2 bytes of data
  15988. uint16 cmd = RFIFOW(fd,0);
  15989. if( sd ) {
  15990. packet_len = packet_db[cmd].len;
  15991. if( packet_len == 0 )
  15992. {// unknown
  15993. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  15994. }
  15995. else if( packet_len == -1 )
  15996. {// variable length
  15997. packet_len = RFIFOW(fd,2); // clif_parse ensures, that this amount of data is already received
  15998. }
  15999. 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);
  16000. }
  16001. else
  16002. {
  16003. packet_len = static_cast<decltype(packet_len)>( RFIFOREST(fd) );
  16004. ShowDebug("Packet debug of 0x%04X (length %d), session #%d\n", cmd, packet_len, fd);
  16005. }
  16006. ShowDump(RFIFOP(fd,0), packet_len);
  16007. }
  16008. /*==========================================
  16009. * Server tells client to display a window similar to Magnifier (item) one
  16010. * Server populates the window with avilable elemental converter options according to player's inventory
  16011. *------------------------------------------*/
  16012. void clif_elementalconverter_list( map_session_data& sd ){
  16013. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16014. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16015. p->packetLength = sizeof( *p );
  16016. int32 count = 0;
  16017. for( int32 i = 0; i < MAX_SKILL_PRODUCE_DB; i++ ){
  16018. if( skill_can_produce_mix( &sd, skill_produce_db[i].nameid, 23, 1 ) ){
  16019. p->items[count].itemId = client_nameid( skill_produce_db[i].nameid );
  16020. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16021. count++;
  16022. }
  16023. }
  16024. clif_send( p, p->packetLength, &sd.bl, SELF );
  16025. if( count > 0 ){
  16026. sd.menuskill_id = SA_CREATECON;
  16027. sd.menuskill_val = count;
  16028. }
  16029. }
  16030. /**
  16031. * Rune Knight
  16032. **/
  16033. void clif_millenniumshield( block_list& bl, int16 shields ){
  16034. #if PACKETVER >= 20081126
  16035. PACKET_ZC_MILLENNIUMSHIELD packet{};
  16036. packet.packetType = HEADER_ZC_MILLENNIUMSHIELD;
  16037. packet.aid = bl.id;
  16038. packet.num = shields;
  16039. packet.state = 0;
  16040. clif_send( &packet, sizeof( packet ), &bl, AREA );
  16041. #endif
  16042. }
  16043. /**
  16044. * Mechanic
  16045. **/
  16046. /*==========================================
  16047. * Magic Decoy Material List
  16048. *------------------------------------------*/
  16049. void clif_magicdecoy_list( map_session_data& sd, uint16 skill_lv, short x, short y ){
  16050. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16051. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16052. p->packetLength = sizeof( *p );
  16053. size_t count = 0;
  16054. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  16055. if( itemdb_group.item_exists( IG_ELEMENT, sd.inventory.u.items_inventory[i].nameid ) ){
  16056. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  16057. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16058. count++;
  16059. }
  16060. }
  16061. if( count > 0 ){
  16062. clif_send( p, p->packetLength, &sd.bl, SELF );
  16063. sd.menuskill_id = NC_MAGICDECOY;
  16064. sd.menuskill_val = skill_lv;
  16065. sd.sc.comet_x = x;
  16066. sd.sc.comet_y = y;
  16067. }else{
  16068. clif_skill_fail( sd, NC_MAGICDECOY );
  16069. }
  16070. }
  16071. /*==========================================
  16072. * Guillotine Cross Poisons List
  16073. *------------------------------------------*/
  16074. void clif_poison_list( map_session_data& sd, uint16 skill_lv ){
  16075. PACKET_ZC_MAKINGARROW_LIST* p = reinterpret_cast<PACKET_ZC_MAKINGARROW_LIST*>( packet_buffer );
  16076. p->packetType = HEADER_ZC_MAKINGARROW_LIST;
  16077. p->packetLength = sizeof( *p );
  16078. size_t count = 0;
  16079. for( size_t i = 0; i < MAX_INVENTORY; i++ ){
  16080. if( itemdb_group.item_exists( IG_POISON, sd.inventory.u.items_inventory[i].nameid ) ){
  16081. p->items[count].itemId = client_nameid( sd.inventory.u.items_inventory[i].nameid );
  16082. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->items[0] ) );
  16083. count++;
  16084. }
  16085. }
  16086. if( count > 0 ){
  16087. clif_send( p, p->packetLength, &sd.bl, SELF );
  16088. sd.menuskill_id = GC_POISONINGWEAPON;
  16089. sd.menuskill_val = skill_lv;
  16090. }else{
  16091. clif_skill_fail( sd, GC_POISONINGWEAPON, USESKILL_FAIL_GUILLONTINE_POISON );
  16092. }
  16093. }
  16094. /// 0442 <Length>.W <why>.L { <Skill_Id>.W }* (ZC_SKILL_SELECT_REQUEST).
  16095. void clif_autoshadowspell_list( map_session_data& sd ){
  16096. #if PACKETVER >= 20081210
  16097. if( sd.menuskill_id == SC_AUTOSHADOWSPELL )
  16098. return;
  16099. PACKET_ZC_SKILL_SELECT_REQUEST* p = reinterpret_cast<PACKET_ZC_SKILL_SELECT_REQUEST*>( packet_buffer );
  16100. p->packetType = HEADER_ZC_SKILL_SELECT_REQUEST;
  16101. p->packetLength = sizeof( *p );
  16102. p->flag = 1; // enum PACKET_ZC_SKILL_SELECT_REQUEST::enumWHY::WHY_SC_AUTOSHADOWSPELL = 0x1
  16103. size_t count = 0;
  16104. for( size_t i = 0; i < MAX_SKILL; i++ ){
  16105. if( sd.status.skill[i].flag == SKILL_FLAG_PLAGIARIZED && sd.status.skill[i].id > 0 &&
  16106. skill_get_inf2(sd.status.skill[i].id, INF2_ISAUTOSHADOWSPELL))
  16107. {
  16108. p->skillIds[count] = sd.status.skill[i].id;
  16109. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->skillIds[0] ) );
  16110. count++;
  16111. }
  16112. }
  16113. if( count > 0 ) {
  16114. clif_send( p, p->packetLength, &sd.bl, SELF );
  16115. sd.menuskill_id = SC_AUTOSHADOWSPELL;
  16116. sd.menuskill_val = static_cast<decltype(sd.menuskill_val)>( count );
  16117. } else {
  16118. status_change_end( &sd.bl, SC_STOP );
  16119. clif_skill_fail( sd, SC_AUTOSHADOWSPELL, USESKILL_FAIL_IMITATION_SKILL_NONE );
  16120. }
  16121. #endif
  16122. }
  16123. /*===========================================
  16124. * Skill list for Four Elemental Analysis
  16125. * and Change Material skills.
  16126. * 07e3 <type>.L (ZC_ITEMLISTWIN_OPEN).
  16127. *------------------------------------------*/
  16128. int32 clif_skill_itemlistwindow( map_session_data *sd, uint16 skill_id, uint16 skill_lv ) {
  16129. #if PACKETVER >= 20090922
  16130. int32 fd = sd->fd;
  16131. nullpo_ret(sd);
  16132. sd->menuskill_id = skill_id; // To prevent hacking.
  16133. sd->menuskill_val = skill_lv;
  16134. if( skill_id == GN_CHANGEMATERIAL )
  16135. skill_lv = 0; // Changematerial
  16136. WFIFOHEAD(fd, packet_len(0x7e3));
  16137. WFIFOW(fd,0) = 0x7e3;
  16138. WFIFOL(fd,2) = skill_lv;
  16139. WFIFOL(fd,4) = 0;
  16140. WFIFOSET(fd, packet_len(0x7e3));
  16141. #endif
  16142. return 1;
  16143. }
  16144. /*==========================================
  16145. * Select a skill into a given list (used by SA_AUTOSPELL/SC_AUTOSHADOWSPELL)
  16146. * 0443 <type>.L <skill_id>.W (CZ_SKILL_SELECT_RESPONSE)
  16147. *------------------------------------------*/
  16148. void clif_parse_SkillSelectMenu(int32 fd, map_session_data *sd) {
  16149. #if PACKETVER >= 20081210
  16150. if( sd == nullptr ){
  16151. return;
  16152. }
  16153. const PACKET_CZ_SKILL_SELECT_RESPONSE* p = reinterpret_cast<PACKET_CZ_SKILL_SELECT_RESPONSE*>( RFIFOP( fd, 0 ) );
  16154. if (sd->menuskill_id == SA_AUTOSPELL) {
  16155. sd->state.workinprogress = WIP_DISABLE_NONE;
  16156. skill_autospell(sd, p->selectedSkillId);
  16157. } else if (sd->menuskill_id == SC_AUTOSHADOWSPELL) {
  16158. if (pc_istrading(sd)) {
  16159. clif_skill_fail( *sd, sd->ud.skill_id );
  16160. clif_menuskill_clear(sd);
  16161. return;
  16162. }
  16163. skill_select_menu(*sd, p->selectedSkillId);
  16164. } else
  16165. return;
  16166. clif_menuskill_clear(sd);
  16167. #endif
  16168. }
  16169. /// Kagerou/Oboro amulet spirit.
  16170. /// 08cf <id>.L <type>.W <num>.W (ZC_SPIRITS_ATTRIBUTE)
  16171. void clif_spiritcharm( map_session_data& sd ){
  16172. #if PACKETVER >= 20111102
  16173. PACKET_ZC_SPIRITS_ATTRIBUTE packet{};
  16174. packet.packetType = HEADER_ZC_SPIRITS_ATTRIBUTE;
  16175. packet.aid = sd.bl.id;
  16176. packet.spiritsType = static_cast<decltype(packet.spiritsType)>(sd.spiritcharm_type);
  16177. packet.num = static_cast<decltype(packet.num)>(sd.spiritcharm);
  16178. clif_send( &packet, sizeof( packet ), &sd.bl, AREA );
  16179. #endif
  16180. }
  16181. /// Move Item from or to Personal Tab
  16182. /// 0907 <index>.W <type>.B (CZ_INVENTORY_TAB)
  16183. /// type:
  16184. /// 0 = move item to personal tab
  16185. /// 1 = move item to normal tab
  16186. void clif_parse_MoveItem( int32 fd, map_session_data* sd ){
  16187. // TODO: Check for correct packet version
  16188. #if PACKETVER >= 20120410
  16189. /* can't move while dead. */
  16190. if(pc_isdead(sd)) {
  16191. return;
  16192. }
  16193. const PACKET_CZ_INVENTORY_TAB* p = reinterpret_cast<PACKET_CZ_INVENTORY_TAB*>( RFIFOP( fd, 0 ) );
  16194. uint16 index = server_index( p->index );
  16195. if( index >= MAX_INVENTORY ){
  16196. return;
  16197. }
  16198. if ( sd->inventory.u.items_inventory[index].favorite != 0 && p->favorite == true )
  16199. sd->inventory.u.items_inventory[index].favorite = 0;
  16200. else if( p->favorite == false )
  16201. sd->inventory.u.items_inventory[index].favorite = 1;
  16202. else
  16203. return;/* nothing to do. */
  16204. clif_favorite_item( *sd, index );
  16205. #endif
  16206. }
  16207. /// Items that are in favorite tab of inventory.
  16208. /// 0908 <index>.W <favorite>.B (ZC_INVENTORY_TAB)
  16209. static void clif_favorite_item( map_session_data& sd, uint16 index ){
  16210. // TODO: Check for correct packet version
  16211. #if PACKETVER >= 20111122
  16212. PACKET_ZC_INVENTORY_TAB p = {};
  16213. p.packetType = HEADER_ZC_INVENTORY_TAB;
  16214. p.index = client_index( index );
  16215. p.favorite = ( sd.inventory.u.items_inventory[index].favorite == 1 ) ? false : true;
  16216. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16217. #endif
  16218. }
  16219. /// 08d2 <id>.L <Pos X>.W <Pos Y>.W (ZC_FASTMOVE).
  16220. void clif_snap( struct block_list *bl, short x, short y ) {
  16221. unsigned char buf[10];
  16222. WBUFW(buf,0) = 0x8d2;
  16223. WBUFL(buf,2) = bl->id;
  16224. WBUFW(buf,6) = x;
  16225. WBUFW(buf,8) = y;
  16226. if( disguised(bl) )
  16227. {
  16228. clif_send(buf, packet_len(0x8d2), bl, AREA_WOS);
  16229. WBUFL(buf,2) = disguised_bl_id(bl->id);
  16230. clif_send(buf, packet_len(0x8d2), bl, SELF);
  16231. } else
  16232. clif_send(buf,packet_len(0x8d2),bl, AREA);
  16233. }
  16234. /// 0977 <id>.L <HP>.L <maxHP>.L (ZC_HP_INFO).
  16235. void clif_monster_hp_bar( struct mob_data* md, int32 fd ) {
  16236. #if PACKETVER >= 20120404
  16237. WFIFOHEAD(fd,packet_len(0x977));
  16238. WFIFOW(fd,0) = 0x977;
  16239. WFIFOL(fd,2) = md->bl.id;
  16240. WFIFOL(fd,6) = md->status.hp;
  16241. WFIFOL(fd,10) = md->status.max_hp;
  16242. WFIFOSET(fd,packet_len(0x977));
  16243. #endif
  16244. }
  16245. /* [Ind] placeholder for unsupported incoming packets (avoids server disconnecting client) */
  16246. void __attribute__ ((unused)) clif_parse_dull(int32 fd, map_session_data *sd) {
  16247. return;
  16248. }
  16249. /// Ack world info (ZC_ACK_BEFORE_WORLD_INFO)
  16250. /// 0979 <world name>.24B <char name>.24B
  16251. void clif_ackworldinfo(map_session_data* sd) {
  16252. int32 fd;
  16253. nullpo_retv(sd);
  16254. fd = sd->fd;
  16255. WFIFOHEAD(fd,packet_len(0x979));
  16256. WFIFOW(fd,0)=0x979;
  16257. //AID -> world name ?
  16258. safestrncpy(WFIFOCP(fd,2), "" /* World name */, 24);
  16259. safestrncpy(WFIFOCP(fd,26), sd->status.name, NAME_LENGTH);
  16260. WFIFOSET(fd,packet_len(0x979));
  16261. }
  16262. /// req world info (CZ_REQ_BEFORE_WORLD_INFO)
  16263. /// 0978 <AID>.L
  16264. void clif_parse_reqworldinfo(int32 fd,map_session_data *sd) {
  16265. //uint32 aid = RFIFOL(fd,2); //should we trust client ?
  16266. if(sd)
  16267. clif_ackworldinfo(sd);
  16268. }
  16269. static void clif_loadConfirm( map_session_data *sd ){
  16270. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410
  16271. nullpo_retv( sd );
  16272. PACKET_ZC_NOTIFY_ACTORINIT p = {};
  16273. p.packetType = HEADER_ZC_NOTIFY_ACTORINIT;
  16274. clif_send( &p, sizeof(p), &sd->bl, SELF );
  16275. clif_instance_info( *sd );
  16276. #endif
  16277. }
  16278. /// unknown usage (CZ_BLOCKING_PLAY_CANCEL)
  16279. /// 0447
  16280. void clif_parse_blocking_playcancel( int32 fd, map_session_data *sd ){
  16281. clif_loadConfirm( sd );
  16282. int32 mf = map_getmapflag(sd->bl.m, MF_SPECIALPOPUP);
  16283. if (mf > 0) {
  16284. clif_specialpopup(*sd, mf);
  16285. }
  16286. }
  16287. /// req world info (CZ_CLIENT_VERSION)
  16288. /// 044A <version>.L
  16289. void clif_parse_client_version(int32 fd, map_session_data *sd) {
  16290. //if(sd)
  16291. ;
  16292. }
  16293. /// Ranking list
  16294. /// ranking pointlist { <name>.24B }*10 { <point>.L }*10
  16295. void clif_sub_ranklist( RANKLIST& ranklist, e_rank rankingtype ){
  16296. struct fame_list* list;
  16297. size_t i;
  16298. size_t size = std::min<size_t>( ARRAYLENGTH( ranklist.names ), MAX_FAME_LIST );
  16299. switch( rankingtype ){
  16300. case RANK_BLACKSMITH:
  16301. list = smith_fame_list;
  16302. break;
  16303. case RANK_ALCHEMIST:
  16304. list = chemist_fame_list;
  16305. break;
  16306. case RANK_TAEKWON:
  16307. list = taekwon_fame_list;
  16308. break;
  16309. case RANK_KILLER:
  16310. // Currently not supported
  16311. list = nullptr;
  16312. size = 0;
  16313. break;
  16314. default:
  16315. ShowError( "clif_sub_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16316. return;
  16317. }
  16318. for( i = 0; i < size; i++ ){
  16319. ranklist.points[i] = list[i].fame;
  16320. if( list[i].id == 0 ){
  16321. safestrncpy( ranklist.names[i], "None", sizeof( ranklist.names[0] ) );
  16322. continue;
  16323. }
  16324. if( strcmp( list[i].name, "-" ) != 0 ){
  16325. safestrncpy( ranklist.names[i], list[i].name, sizeof( ranklist.names[0] ) );
  16326. continue;
  16327. }
  16328. const char* name = map_charid2nick( list[i].id );
  16329. if( name != nullptr ){
  16330. safestrncpy( ranklist.names[i], name, sizeof( ranklist.names[0] ) );
  16331. }else{
  16332. safestrncpy( ranklist.names[i], list[i].name, sizeof( ranklist.names[0] ) );
  16333. }
  16334. }
  16335. size = ARRAYLENGTH( ranklist.names );
  16336. for( ; i < size; i++ ){
  16337. safestrncpy( ranklist.names[i], "Unavailable", sizeof( ranklist.names[0] ) );
  16338. ranklist.points[i] = 0;
  16339. }
  16340. }
  16341. /// 0219 { <name>.24B }*10 { <point>.L }*10 (ZC_BLACKSMITH_RANK)
  16342. /// 021a { <name>.24B }*10 { <point>.L }*10 (ZC_ALCHEMIST_RANK)
  16343. /// 0226 { <name>.24B }*10 { <point>.L }*10 (ZC_TAEKWON_RANK)
  16344. /// 0238 { <name>.24B }*10 { <point>.L }*10 (ZC_KILLER_RANK)
  16345. /// 097d <RankingType>.W { <name>.24B }*10 { <point>.L }*10 <mypoint>L (ZC_ACK_RANKING)
  16346. /// 0af6 <RankingType>.W { <CID>.L }*10 { <point>.L }*10 <mypoint>L (ZC_ACK_RANKING2)
  16347. void clif_ranklist( map_session_data& sd, e_rank rankingtype ){
  16348. #if PACKETVER_MAIN_NUM >= 20190731 || PACKETVER_RE_NUM >= 20190703 || PACKETVER_ZERO_NUM >= 20190724
  16349. PACKET_ZC_ACK_RANKING2 p = {};
  16350. p.packetType = HEADER_ZC_ACK_RANKING2;
  16351. p.type = rankingtype;
  16352. struct fame_list* list;
  16353. size_t i;
  16354. size_t size = std::min<size_t>( ARRAYLENGTH( p.CIDs ), MAX_FAME_LIST );
  16355. switch( rankingtype ){
  16356. case RANK_BLACKSMITH:
  16357. list = smith_fame_list;
  16358. break;
  16359. case RANK_ALCHEMIST:
  16360. list = chemist_fame_list;
  16361. break;
  16362. case RANK_TAEKWON:
  16363. list = taekwon_fame_list;
  16364. break;
  16365. case RANK_KILLER:
  16366. // Currently not supported
  16367. list = nullptr;
  16368. size = 0;
  16369. break;
  16370. default:
  16371. ShowError( "clif_sub_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16372. return;
  16373. }
  16374. for( i = 0; i < size; i++ ){
  16375. p.CIDs[i] = list[i].id;
  16376. p.points[i] = list[i].fame;
  16377. }
  16378. size = ARRAYLENGTH( p.CIDs );
  16379. for( ; i < size; i++ ){
  16380. p.CIDs[i] = 0;
  16381. p.points[i] = 0;
  16382. }
  16383. switch( sd.class_&MAPID_UPPERMASK ){
  16384. case MAPID_BLACKSMITH:
  16385. case MAPID_ALCHEMIST:
  16386. case MAPID_TAEKWON:
  16387. p.mypoints = sd.status.fame;
  16388. break;
  16389. default:
  16390. p.mypoints = 0;
  16391. break;
  16392. }
  16393. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16394. #elif PACKETVER_MAIN_NUM >= 20130605 || PACKETVER_RE_NUM >= 20130529 || defined(PACKETVER_ZERO)
  16395. PACKET_ZC_ACK_RANKING p = {};
  16396. p.packetType = HEADER_ZC_ACK_RANKING;
  16397. p.type = rankingtype;
  16398. clif_sub_ranklist( p.list, rankingtype );
  16399. switch( sd.class_&MAPID_UPPERMASK ){
  16400. case MAPID_BLACKSMITH:
  16401. case MAPID_ALCHEMIST:
  16402. case MAPID_TAEKWON:
  16403. p.mypoints = sd.status.fame;
  16404. break;
  16405. default:
  16406. p.mypoints = 0;
  16407. break;
  16408. }
  16409. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16410. #else
  16411. switch( rankingtype ){
  16412. case RANK_BLACKSMITH: {
  16413. #if PACKETVER >= 20041108
  16414. PACKET_ZC_BLACKSMITH_RANK p = {};
  16415. p.packetType = HEADER_ZC_BLACKSMITH_RANK;
  16416. clif_sub_ranklist( p.list, RANK_BLACKSMITH );
  16417. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16418. #endif
  16419. } break;
  16420. case RANK_ALCHEMIST: {
  16421. #if PACKETVER >= 20041108
  16422. PACKET_ZC_ALCHEMIST_RANK p = {};
  16423. p.packetType = HEADER_ZC_ALCHEMIST_RANK;
  16424. clif_sub_ranklist( p.list, RANK_ALCHEMIST );
  16425. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16426. #endif
  16427. } break;
  16428. case RANK_TAEKWON: {
  16429. #if PACKETVER >= 20050328
  16430. PACKET_ZC_TAEKWON_RANK p = {};
  16431. p.packetType = HEADER_ZC_TAEKWON_RANK;
  16432. clif_sub_ranklist( p.list, RANK_TAEKWON );
  16433. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16434. #endif
  16435. } break;
  16436. case RANK_KILLER: {
  16437. #if PACKETVER >= 20050328
  16438. PACKET_ZC_KILLER_RANK p = {};
  16439. p.packetType = HEADER_ZC_KILLER_RANK;
  16440. clif_sub_ranklist( p.list, RANK_KILLER );
  16441. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16442. #endif
  16443. } break;
  16444. default:
  16445. ShowError( "clif_ranklist: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16446. return;
  16447. }
  16448. #endif
  16449. }
  16450. /// Request for the blacksmith ranklist.
  16451. /// /blacksmith command sends this packet to the server.
  16452. /// 0217 (CZ_BLACKSMITH_RANK)
  16453. void clif_parse_ranklist_blacksmith( int32 fd, map_session_data* sd ){
  16454. #if PACKETVER >= 20041108
  16455. const PACKET_CZ_BLACKSMITH_RANK* p = reinterpret_cast<PACKET_CZ_BLACKSMITH_RANK*>( RFIFOP( fd, 0 ) );
  16456. clif_ranklist( *sd, RANK_BLACKSMITH );
  16457. #endif
  16458. }
  16459. /// Request for the alchemist ranklist.
  16460. /// /alchemist command sends this packet to the server.
  16461. /// 0218 (CZ_ALCHEMIST_RANK)
  16462. void clif_parse_ranklist_alchemist( int32 fd, map_session_data* sd ){
  16463. #if PACKETVER >= 20041108
  16464. const PACKET_CZ_ALCHEMIST_RANK* p = reinterpret_cast<PACKET_CZ_ALCHEMIST_RANK*>( RFIFOP( fd, 0 ) );
  16465. clif_ranklist( *sd, RANK_ALCHEMIST );
  16466. #endif
  16467. }
  16468. /// Request for the taekwon ranklist.
  16469. /// /taekwon command sends this packet to the server.
  16470. /// 0225 (CZ_TAEKWON_RANK)
  16471. void clif_parse_ranklist_taekwon( int32 fd, map_session_data* sd ){
  16472. #if PACKETVER >= 20050328
  16473. const PACKET_CZ_TAEKWON_RANK* p = reinterpret_cast<PACKET_CZ_TAEKWON_RANK*>( RFIFOP( fd, 0 ) );
  16474. clif_ranklist( *sd, RANK_TAEKWON );
  16475. #endif
  16476. }
  16477. /// Request for the killer ranklist.
  16478. /// /pk command sends this packet to the server.
  16479. /// 0237 (CZ_KILLER_RANK)
  16480. void clif_parse_ranklist_killer( int32 fd, map_session_data* sd ){
  16481. #if PACKETVER >= 20050530
  16482. const PACKET_CZ_KILLER_RANK* p = reinterpret_cast<PACKET_CZ_KILLER_RANK*>( RFIFOP( fd, 0 ) );
  16483. clif_ranklist( *sd, RANK_KILLER );
  16484. #endif
  16485. }
  16486. ///
  16487. /// 097c <type>.W (CZ_REQ_RANKING)
  16488. /// type
  16489. /// 0: /blacksmith
  16490. /// 1: /alchemist
  16491. /// 2: /taekwon
  16492. /// 3: /pk
  16493. void clif_parse_ranklist( int fd, map_session_data* sd ){
  16494. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502
  16495. const PACKET_CZ_REQ_RANKING* p = reinterpret_cast<PACKET_CZ_REQ_RANKING*>( RFIFOP( fd, 0 ) );
  16496. switch( p->type ){
  16497. case RANK_BLACKSMITH:
  16498. case RANK_ALCHEMIST:
  16499. case RANK_TAEKWON:
  16500. case RANK_KILLER:
  16501. break;
  16502. default:
  16503. return;
  16504. }
  16505. clif_ranklist( *sd, static_cast<e_rank>( p->type ) );
  16506. #endif
  16507. }
  16508. /// Updates the fame rank points for the given ranking.
  16509. /// 021b <points>.L <total points>.L (ZC_BLACKSMITH_POINT)
  16510. /// 021c <points>.L <total points>.L (ZC_ALCHEMIST_POINT)
  16511. /// 0224 <points>.L <total points>.L (ZC_TAEKWON_POINT)
  16512. /// 0236 <points>.L <total points>.L (ZC_KILLER_POINT)
  16513. /// 097e <RankingType>.W <point>.L <TotalPoint>.L (ZC_UPDATE_RANKING_POINT)
  16514. void clif_update_rankingpoint( map_session_data& sd, e_rank rankingtype, uint32 point ){
  16515. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502
  16516. PACKET_ZC_UPDATE_RANKING_POINT p = {};
  16517. p.packetType = HEADER_ZC_UPDATE_RANKING_POINT;
  16518. p.type = rankingtype;
  16519. p.points = point;
  16520. p.points_total = sd.status.fame;
  16521. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16522. #else
  16523. switch(rankingtype){
  16524. case RANK_BLACKSMITH: {
  16525. #if PACKETVER >= 20041108
  16526. PACKET_ZC_BLACKSMITH_POINT p = {};
  16527. p.packetType = HEADER_ZC_BLACKSMITH_POINT;
  16528. p.points = point;
  16529. p.points_total = sd.status.fame;
  16530. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16531. #endif
  16532. } break;
  16533. case RANK_ALCHEMIST: {
  16534. #if PACKETVER >= 20041108
  16535. PACKET_ZC_ALCHEMIST_POINT p = {};
  16536. p.packetType = HEADER_ZC_ALCHEMIST_POINT;
  16537. p.points = point;
  16538. p.points_total = sd.status.fame;
  16539. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16540. #endif
  16541. } break;
  16542. case RANK_TAEKWON: {
  16543. #if PACKETVER >= 20050328
  16544. PACKET_ZC_TAEKWON_POINT p = {};
  16545. p.packetType = HEADER_ZC_TAEKWON_POINT;
  16546. p.points = point;
  16547. p.points_total = sd.status.fame;
  16548. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16549. #endif
  16550. } break;
  16551. case RANK_KILLER: {
  16552. #if PACKETVER >= 20050530
  16553. PACKET_ZC_KILLER_POINT p = {};
  16554. p.packetType = HEADER_ZC_KILLER_POINT;
  16555. p.points = point;
  16556. p.points_total = sd.status.fame;
  16557. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16558. #endif
  16559. } break;
  16560. default:
  16561. ShowError( "clif_update_rankingpoint: Unsupported ranking type '%d'. Please report this.\n", rankingtype );
  16562. return;
  16563. }
  16564. #endif
  16565. }
  16566. /**
  16567. * Transmit personal information to player. (rates)
  16568. * 0x08cb <packet len>.W <exp>.W <death>.W <drop>.W <DETAIL_EXP_INFO>7B (ZC_PERSONAL_INFOMATION)
  16569. * <InfoType>.B <Exp>.W <Death>.W <Drop>.W (DETAIL_EXP_INFO 0x08cb)
  16570. * 0x097b <packet len>.W <exp>.L <death>.L <drop>.L <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION2)
  16571. * 0x0981 <packet len>.W <exp>.W <death>.W <drop>.W <activity rate>.W <DETAIL_EXP_INFO>13B (ZC_PERSONAL_INFOMATION_CHN)
  16572. * <InfoType>.B <Exp>.L <Death>.L <Drop>.L (DETAIL_EXP_INFO 0x97b|0981)
  16573. * FIXME!
  16574. * - Find/decide for details of EXP, Drop, and Death penalty rates
  16575. * - For now, we're assuming values for DETAIL_EXP_INFO are:
  16576. * 0 - map adjustment (bexp mapflag), 1 - Premium/VIP adjustment, 2 - Server rate adjustment, 3 - None
  16577. */
  16578. #ifdef VIP_ENABLE
  16579. void clif_display_pinfo( map_session_data& sd ){
  16580. #if PACKETVER_MAIN_NUM >= 20110627 || PACKETVER_RE_NUM >= 20110628 || defined(PACKETVER_ZERO)
  16581. // 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]
  16582. int32 details_bexp[PINFO_MAX], details_drop[PINFO_MAX], details_penalty[PINFO_MAX];
  16583. /**
  16584. * EXP
  16585. */
  16586. //0:PCRoom
  16587. details_bexp[PINFO_BASIC] = map_getmapflag( sd.bl.m, MF_BEXP );
  16588. if (details_bexp[PINFO_BASIC] == 100 || !details_bexp[PINFO_BASIC])
  16589. details_bexp[PINFO_BASIC] = 0;
  16590. else {
  16591. if (details_bexp[PINFO_BASIC] < 100) {
  16592. details_bexp[PINFO_BASIC] = 100 - details_bexp[PINFO_BASIC];
  16593. details_bexp[PINFO_BASIC] = 0 - details_bexp[PINFO_BASIC];
  16594. } else
  16595. details_bexp[PINFO_BASIC] = details_bexp[PINFO_BASIC] - 100;
  16596. }
  16597. //1:Premium
  16598. if( pc_isvip( &sd ) ){
  16599. details_bexp[PINFO_PREMIUM] = battle_config.vip_base_exp_increase * battle_config.base_exp_rate / 100;
  16600. if (details_bexp[PINFO_PREMIUM] < 0)
  16601. details_bexp[PINFO_PREMIUM] = 0 - details_bexp[PINFO_PREMIUM];
  16602. } else
  16603. details_bexp[PINFO_PREMIUM] = 0;
  16604. //2:Server
  16605. details_bexp[PINFO_SERVER] = battle_config.base_exp_rate;
  16606. if (details_bexp[PINFO_SERVER] == 100)
  16607. details_bexp[PINFO_SERVER] = 0;
  16608. else {
  16609. if (details_bexp[PINFO_SERVER] < 100) {
  16610. details_bexp[PINFO_SERVER] = 100 - details_bexp[PINFO_SERVER];
  16611. details_bexp[PINFO_SERVER] = 0 - details_bexp[PINFO_SERVER];
  16612. } else
  16613. details_bexp[PINFO_SERVER] = details_bexp[PINFO_SERVER] - 100;
  16614. }
  16615. //3:TPLUS
  16616. details_bexp[PINFO_CAFE] = 0;
  16617. /**
  16618. * Drop rate
  16619. */
  16620. //0:PCRoom
  16621. details_drop[PINFO_BASIC] = 0;
  16622. //1:Premium
  16623. if( pc_isvip( &sd ) ){
  16624. details_drop[PINFO_PREMIUM] = (battle_config.vip_drop_increase * battle_config.item_rate_common) / 100;
  16625. if (details_drop[PINFO_PREMIUM] < 0)
  16626. details_drop[PINFO_PREMIUM] = 0 - details_drop[PINFO_PREMIUM];
  16627. } else
  16628. details_drop[PINFO_PREMIUM] = 0;
  16629. //2:Server
  16630. details_drop[PINFO_SERVER] = battle_config.item_rate_common;
  16631. if (details_drop[PINFO_SERVER] == 100)
  16632. details_drop[PINFO_SERVER] = 0;
  16633. else {
  16634. if (details_drop[PINFO_SERVER] < 100) {
  16635. details_drop[PINFO_SERVER] = 100 - details_drop[PINFO_SERVER];
  16636. details_drop[PINFO_SERVER] = 0 - details_drop[PINFO_SERVER];
  16637. } else
  16638. details_drop[PINFO_SERVER] = details_drop[PINFO_SERVER] - 100;
  16639. }
  16640. //3:TPLUS
  16641. details_drop[PINFO_CAFE] = 0;
  16642. /**
  16643. * Penalty rate
  16644. */
  16645. //! FIXME: Current penalty system makes this announcement unable to give info on + or - rate
  16646. //0:PCRoom
  16647. details_penalty[PINFO_BASIC] = 0;
  16648. //1:Premium
  16649. if( pc_isvip( &sd ) ){
  16650. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base;
  16651. if (details_penalty[PINFO_PREMIUM] == 100)
  16652. details_penalty[PINFO_PREMIUM] = 0;
  16653. else {
  16654. if (details_penalty[PINFO_PREMIUM] < 100) {
  16655. details_penalty[PINFO_PREMIUM] = 100 - details_penalty[PINFO_PREMIUM];
  16656. details_penalty[PINFO_PREMIUM] = 0 - details_penalty[PINFO_PREMIUM];
  16657. } else
  16658. details_penalty[PINFO_PREMIUM] = details_penalty[PINFO_PREMIUM] - 100;
  16659. }
  16660. if (battle_config.death_penalty_base > battle_config.vip_exp_penalty_base)
  16661. details_penalty[PINFO_PREMIUM] = battle_config.vip_exp_penalty_base - battle_config.death_penalty_base;
  16662. } else
  16663. details_penalty[PINFO_PREMIUM] = 0;
  16664. //2:Server
  16665. details_penalty[PINFO_SERVER] = battle_config.death_penalty_base;
  16666. if (details_penalty[PINFO_SERVER] == 100)
  16667. details_penalty[PINFO_SERVER] = 0;
  16668. else {
  16669. if (details_penalty[PINFO_SERVER] < 100) {
  16670. details_penalty[PINFO_SERVER] = 100 - details_penalty[PINFO_SERVER];
  16671. details_penalty[PINFO_SERVER] = 0 - details_penalty[PINFO_SERVER];
  16672. } else
  16673. details_penalty[PINFO_SERVER] = details_penalty[PINFO_SERVER] - 100;
  16674. }
  16675. //3:TPLUS
  16676. details_penalty[PINFO_CAFE] = 0;
  16677. PACKET_ZC_PERSONAL_INFOMATION* p = reinterpret_cast<PACKET_ZC_PERSONAL_INFOMATION*>( packet_buffer );
  16678. p->packetType = HEADER_ZC_PERSONAL_INFOMATION;
  16679. p->length = sizeof( *p );
  16680. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16681. p->total_exp = 100 * 1000;
  16682. p->total_death = 100 * 1000;
  16683. p->total_drop = 100 * 1000;
  16684. #else
  16685. p->total_exp = 100;
  16686. p->total_death = 100;
  16687. p->total_drop = 100;
  16688. #endif
  16689. for( int32 i = PINFO_BASIC; i < PINFO_MAX; i++ ){
  16690. p->details[i].type = i; // infotype 0 PCRoom, 1 Premium, 2 Server, 3 TPlus
  16691. #if PACKETVER_MAIN_NUM >= 20120503 || PACKETVER_RE_NUM >= 20120502 || defined(PACKETVER_ZERO)
  16692. p->details[i].exp = details_bexp[i] * 1000;
  16693. p->details[i].death = details_penalty[i] * 1000;
  16694. p->details[i].drop = details_drop[i] * 1000;
  16695. #else
  16696. p->details[i].exp = details_bexp[i];
  16697. p->details[i].death = details_penalty[i];
  16698. p->details[i].drop = details_drop[i];
  16699. #endif
  16700. p->total_exp += p->details[i].exp;
  16701. p->total_death += p->details[i].death;
  16702. p->total_drop += p->details[i].drop;
  16703. p->length += static_cast<decltype(p->length)>( sizeof( p->details[0] ) );
  16704. }
  16705. clif_send( p, p->length, &sd.bl, SELF );
  16706. #endif
  16707. }
  16708. #endif
  16709. void clif_parse_GMFullStrip(int32 fd, map_session_data *sd) {
  16710. char cmd[30];
  16711. int32 t_aid = RFIFOL(fd,2);
  16712. safesnprintf(cmd,sizeof(cmd),"%cfullstrip %d",atcommand_symbol,t_aid);
  16713. is_atcommand(fd, sd, cmd, 1);
  16714. }
  16715. /**
  16716. * Marks Crimson Marker target on mini-map to the caster
  16717. * 09C1 <id>.L <x>.W <y>.W (ZC_C_MARKERINFO)
  16718. * @param fd
  16719. * @param bl Crimson Marker target
  16720. **/
  16721. void clif_crimson_marker( map_session_data& sd, struct block_list& bl, bool remove ){
  16722. #if PACKETVER_MAIN_NUM >= 20130731 || PACKETVER_RE_NUM >= 20130707 || defined(PACKETVER_ZERO)
  16723. PACKET_ZC_C_MARKERINFO p = {};
  16724. p.PacketType = HEADER_ZC_C_MARKERINFO;
  16725. p.AID = bl.id;
  16726. if( remove ){
  16727. p.xPos = -1;
  16728. p.yPos = -1;
  16729. }else{
  16730. p.xPos = bl.x;
  16731. p.yPos = bl.y;
  16732. }
  16733. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16734. #endif
  16735. }
  16736. /**
  16737. * 02d3 <index>.W (ZC_NOTIFY_BIND_ON_EQUIP)
  16738. **/
  16739. void clif_notify_bindOnEquip( map_session_data& sd, int16 index ){
  16740. #if PACKETVER >= 20070227
  16741. PACKET_ZC_NOTIFY_BIND_ON_EQUIP p = {};
  16742. p.packetType = HEADER_ZC_NOTIFY_BIND_ON_EQUIP;
  16743. p.index = client_index( index );
  16744. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16745. #endif
  16746. }
  16747. /**
  16748. * [Ind/Hercules]
  16749. * 08b3 <Length>.W <id>.L <message>.?B (ZC_SHOWSCRIPT)
  16750. **/
  16751. void clif_showscript(struct block_list* bl, const char* message, enum send_target flag) {
  16752. char buf[256];
  16753. size_t len;
  16754. nullpo_retv(bl);
  16755. if(!message)
  16756. return;
  16757. len = strlen(message)+1;
  16758. if( len > sizeof(buf)-8 ) {
  16759. ShowWarning("clif_showscript: Truncating too long message '%s' (len=%" PRIuPTR ").\n", message, len);
  16760. len = sizeof(buf)-8;
  16761. }
  16762. WBUFW(buf,0) = 0x8b3;
  16763. WBUFW(buf,2) = (uint16)(len+8);
  16764. WBUFL(buf,4) = bl->id;
  16765. safestrncpy(WBUFCP(buf,8), message, len);
  16766. clif_send((unsigned char *) buf, WBUFW(buf,2), bl, flag);
  16767. }
  16768. /**
  16769. * [Ind/Hercules]
  16770. * 07fc <OldMasterAID>.L <NewMasterAID>.L (ZC_CHANGE_GROUP_MASTER)
  16771. **/
  16772. void clif_party_leaderchanged(map_session_data *sd, int32 prev_leader_aid, int32 new_leader_aid) {
  16773. unsigned char buf[10];
  16774. nullpo_retv(sd);
  16775. WBUFW(buf,0) = 0x7fc;
  16776. WBUFL(buf,2) = prev_leader_aid;
  16777. WBUFL(buf,6) = new_leader_aid;
  16778. clif_send(buf, packet_len(0x7fc), &sd->bl, PARTY);
  16779. }
  16780. /**
  16781. * Sends a clan message to a player
  16782. * 098e <length>.W <name>.24B <message>.?B (ZC_NOTIFY_CLAN_CHAT)
  16783. **/
  16784. void clif_clan_message( struct clan& clan, const char *mes, size_t len ){
  16785. #if PACKETVER >= 20131223
  16786. map_session_data* sd = clan_getavailablesd( clan );
  16787. if( len == 0 ){
  16788. return;
  16789. }
  16790. PACKET_ZC_NOTIFY_CLAN_CHAT* p = reinterpret_cast<PACKET_ZC_NOTIFY_CLAN_CHAT*>( packet_buffer );
  16791. // Maximum message length = size of our packet buffer minus fixed size of the packet and 1 byte zero termination
  16792. static size_t len_max = sizeof( packet_buffer ) - sizeof( *p ) - 1;
  16793. // Is the length bigger than the maximum message length
  16794. if( len > len_max ){
  16795. ShowWarning( "clif_clan_message: Truncated message '%s' (len=%" PRIuPTR ", max=%" PRIuPTR ", clan_id=%d).\n", mes, len, len_max, clan.id );
  16796. len = len_max;
  16797. }
  16798. p->PacketType = HEADER_ZC_NOTIFY_CLAN_CHAT;
  16799. p->PacketLength = sizeof( *p );
  16800. // 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
  16801. safestrncpy( p->MemberName, "", sizeof( p->MemberName ) );
  16802. safestrncpy( p->Message, mes, len + 1 );
  16803. p->PacketLength += static_cast<decltype(p->PacketLength)>( len + 1 );
  16804. clif_send( p, p->PacketLength, &sd->bl, CLAN );
  16805. #endif
  16806. }
  16807. /**
  16808. * Parses a clan message from a player.
  16809. * 098d <length>.W <text>.?B (<name> : <message>) (CZ_CLAN_CHAT)
  16810. **/
  16811. void clif_parse_clan_chat( int32 fd, map_session_data* sd ){
  16812. #if PACKETVER >= 20131223
  16813. if( sd == nullptr ){
  16814. return;
  16815. }
  16816. char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];
  16817. // validate packet and retrieve name and message
  16818. if( !clif_process_message(sd, false, name, message, output ) )
  16819. return;
  16820. clan_send_message( *sd, RFIFOCP(fd,4), RFIFOW(fd,2) - 4 );
  16821. #endif
  16822. }
  16823. /**
  16824. * Sends the basic clan informations to the client.
  16825. * 098a <length>.W <clan id>.L <clan name>.24B <clan master>.24B <clan map>.16B <alliance count>.B
  16826. * <antagonist count>.B { <alliance>.24B } * alliance count { <antagonist>.24B } * antagonist count (ZC_CLANINFO)
  16827. **/
  16828. void clif_clan_basicinfo( map_session_data& sd ){
  16829. #if PACKETVER_MAIN_NUM >= 20130626 || PACKETVER_RE_NUM >= 20130605 || defined(PACKETVER_ZERO)
  16830. struct clan* clan = sd.clan;
  16831. if( clan == nullptr ){
  16832. return;
  16833. }
  16834. PACKET_ZC_CLANINFO* p = reinterpret_cast<PACKET_ZC_CLANINFO*>( packet_buffer );
  16835. p->PacketType = HEADER_ZC_CLANINFO;
  16836. p->PacketLength = sizeof( *p );
  16837. p->ClanID = clan->id;
  16838. safestrncpy( p->ClanName, clan->name, sizeof( p->ClanName ) );
  16839. safestrncpy( p->MasterName, clan->master, sizeof( p->MasterName ) );
  16840. mapindex_getmapname_ext( clan->map, p->Map );
  16841. p->AllyCount = clan_get_alliance_count( *clan, 0 );
  16842. p->AntagonistCount = clan_get_alliance_count( *clan, 1 );
  16843. for( int32 flag = 0; flag < 2; flag++ ){
  16844. for( int32 i = 0; i < MAX_CLANALLIANCE; i++ ){
  16845. if( clan->alliance[i].clan_id > 0 && clan->alliance[i].opposition == flag ){
  16846. char* name = reinterpret_cast<char*>( WBUFP( p, p->PacketLength ) );
  16847. safestrncpy( name, clan->alliance[i].name, NAME_LENGTH );
  16848. p->PacketLength += static_cast<decltype(p->PacketLength)>( NAME_LENGTH );
  16849. }
  16850. }
  16851. }
  16852. clif_send( p, p->PacketLength, &sd.bl, SELF );
  16853. #endif
  16854. }
  16855. /**
  16856. * Updates the online and maximum player count of a clan.
  16857. * 0988 <online count>.W <maximum member amount>.W (ZC_NOTIFY_CLAN_CONNECTINFO)
  16858. **/
  16859. void clif_clan_onlinecount( struct clan& clan ){
  16860. #if PACKETVER >= 20131223
  16861. map_session_data* sd = clan_getavailablesd( clan );
  16862. if( sd == nullptr){
  16863. return;
  16864. }
  16865. PACKET_ZC_NOTIFY_CLAN_CONNECTINFO p = {};
  16866. p.PacketType = HEADER_ZC_NOTIFY_CLAN_CONNECTINFO;
  16867. p.NumConnect = clan.connect_member;
  16868. p.NumTotal = clan.max_member;
  16869. clif_send( &p, sizeof( p ), &sd->bl, CLAN );
  16870. #endif
  16871. }
  16872. /**
  16873. * Notifies the client that the player has left his clan.
  16874. * 0989 (ZC_ACK_CLAN_LEAVE)
  16875. **/
  16876. void clif_clan_leave( map_session_data& sd ){
  16877. #if PACKETVER >= 20131223
  16878. PACKET_ZC_ACK_CLAN_LEAVE p = {};
  16879. p.PacketType = HEADER_ZC_ACK_CLAN_LEAVE;
  16880. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  16881. #endif
  16882. }
  16883. /**
  16884. * Acknowledge the client about change title result (ZC_ACK_CHANGE_TITLE).
  16885. * 0A2F <result>.B <title_id>.L
  16886. */
  16887. void clif_change_title_ack(map_session_data *sd, unsigned char result, unsigned long title_id)
  16888. {
  16889. #if PACKETVER >= 20150513
  16890. int32 fd;
  16891. nullpo_retv(sd);
  16892. if (!clif_session_isValid(sd))
  16893. return;
  16894. fd = sd->fd;
  16895. WFIFOHEAD(fd, packet_len(0xa2f));
  16896. WFIFOW(fd, 0) = 0xa2f;
  16897. WFIFOB(fd, 2) = result;
  16898. WFIFOL(fd, 3) = title_id;
  16899. WFIFOSET(fd, packet_len(0xa2f));
  16900. #endif
  16901. }
  16902. /**
  16903. * Parsing a request from the client change title (CZ_REQ_CHANGE_TITLE).
  16904. * 0A2E <title_id>.L
  16905. */
  16906. void clif_parse_change_title(int32 fd, map_session_data *sd)
  16907. {
  16908. int32 title_id;
  16909. nullpo_retv(sd);
  16910. title_id = RFIFOL(fd, 2);
  16911. if( title_id == sd->status.title_id ){
  16912. // It is exactly the same as the old one
  16913. return;
  16914. }else if( title_id <= 0 ){
  16915. sd->status.title_id = 0;
  16916. }else{
  16917. if (std::find(sd->titles.begin(), sd->titles.end(), title_id) == sd->titles.end()) {
  16918. clif_change_title_ack(sd, 1, title_id);
  16919. return;
  16920. }
  16921. sd->status.title_id = title_id;
  16922. }
  16923. clif_name_area(&sd->bl);
  16924. clif_change_title_ack(sd, 0, title_id);
  16925. }
  16926. #ifdef DUMP_UNKNOWN_PACKET
  16927. void DumpUnknown(int32 fd,TBL_PC *sd,int32 cmd,int32 packet_len)
  16928. {
  16929. const char* packet_txt = "save/packet.txt";
  16930. FILE* fp;
  16931. time_t time_server;
  16932. struct tm *datetime;
  16933. char datestr[512];
  16934. time(&time_server); // get time in seconds since 1/1/1970
  16935. datetime = localtime(&time_server); // convert seconds in structure
  16936. // like sprintf, but only for date/time (Sunday, November 02 2003 15:12:52)
  16937. strftime(datestr, sizeof(datestr)-1, "%A, %B %d %Y %X.", datetime); // Server time (normal time): %A, %B %d %Y %X.
  16938. if( ( fp = fopen( packet_txt , "a" ) ) != nullptr ) {
  16939. if( sd ) {
  16940. 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);
  16941. } else {
  16942. fprintf(fp, "Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16943. }
  16944. WriteDump(fp, RFIFOP(fd,0), packet_len);
  16945. fprintf(fp, "\n");
  16946. fclose(fp);
  16947. } else {
  16948. ShowError("Failed to write '%s'.\n", packet_txt);
  16949. // Dump on console instead
  16950. if( sd ) {
  16951. 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);
  16952. } else {
  16953. ShowDebug("Unknown packet 0x%04X (length %d), session #%d at %s\n", cmd, packet_len, fd,datestr);
  16954. }
  16955. ShowDump(RFIFOP(fd,0), packet_len);
  16956. }
  16957. }
  16958. #endif
  16959. /// Roulette System
  16960. /// Author: Yommy
  16961. void roulette_generate_bonus( map_session_data& sd ){
  16962. if( battle_config.feature_roulette_bonus_reward && sd.roulette.bonusItemID == 0 ){
  16963. int32 next_stage = 0;
  16964. if( sd.roulette_point.bronze > 0 ){
  16965. next_stage = 0;
  16966. }else if( sd.roulette_point.silver > 9 ){
  16967. next_stage = 2;
  16968. }else if( sd.roulette_point.gold > 9 ){
  16969. next_stage = 4;
  16970. }
  16971. // Get bonus item stage only from current stage or higher
  16972. int32 reward_stage = rnd_value( next_stage, MAX_ROULETTE_LEVEL - 1 );
  16973. sd.roulette.bonusItemID = rd.nameid[reward_stage][rnd()%rd.items[reward_stage]];
  16974. }else{
  16975. sd.roulette.bonusItemID = 0;
  16976. }
  16977. }
  16978. /// Opens the roulette window
  16979. /// 0A1A <result>.B <serial>.L <stage>.B <price index>.B <additional item id>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_OPEN_ROULETTE)
  16980. void clif_roulette_open( map_session_data* sd ){
  16981. nullpo_retv( sd );
  16982. roulette_generate_bonus( *sd );
  16983. struct packet_roulette_open_ack p;
  16984. p.PacketType = 0xa1a;
  16985. p.Result = 0; // result
  16986. p.Serial = 0; // serial
  16987. p.Step = (sd->roulette.claimPrize) ? sd->roulette.stage - 1 : 0;
  16988. p.Idx = (sd->roulette.claimPrize) ? sd->roulette.prizeIdx : -1;
  16989. p.AdditionItemID = sd->roulette.bonusItemID;
  16990. p.GoldPoint = sd->roulette_point.gold;
  16991. p.SilverPoint = sd->roulette_point.silver;
  16992. p.BronzePoint = sd->roulette_point.bronze;
  16993. sd->state.roulette_open = true;
  16994. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  16995. }
  16996. /// Request to open the roulette window
  16997. /// 0A19 (CZ_REQ_OPEN_ROULETTE)
  16998. void clif_parse_roulette_open( int32 fd, map_session_data* sd ){
  16999. nullpo_retv(sd);
  17000. if (!battle_config.feature_roulette) {
  17001. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17002. return;
  17003. }
  17004. clif_roulette_open(sd);
  17005. }
  17006. /// Sends the info about the available roulette rewards to the client
  17007. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.W <amount>.W } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO)
  17008. /// 0A1C <length>.W <serial>.L { { <level>.W <column>.W <item>.L <amount>.L } * MAX_ROULETTE_COLUMNS } * MAX_ROULETTE_LEVEL (ZC_ACK_ROULEITTE_INFO) >= 20180516
  17009. void clif_roulette_info( map_session_data* sd ){
  17010. nullpo_retv(sd);
  17011. struct packet_roulette_info_ack p;
  17012. p.PacketType = rouletteinfoackType;
  17013. p.PacketLength = sizeof(p);
  17014. p.RouletteSerial = 1;
  17015. for( int32 i = 0, count = 0; i < MAX_ROULETTE_LEVEL; i++ ){
  17016. for( int32 j = 0; j < MAX_ROULETTE_COLUMNS - i; j++ ){
  17017. p.ItemInfo[count].Row = i;
  17018. p.ItemInfo[count].Position = j;
  17019. p.ItemInfo[count].ItemId = client_nameid( rd.nameid[i][j] );
  17020. p.ItemInfo[count].Count = rd.qty[i][j];
  17021. #if PACKETVER >= 20180511
  17022. p.ItemInfo[count].unused = 0;
  17023. #endif
  17024. count++;
  17025. }
  17026. }
  17027. clif_send( &p, sizeof(p), &sd->bl, SELF );
  17028. }
  17029. /// Request the roulette reward data
  17030. /// 0A1B (CZ_REQ_ROULETTE_INFO)
  17031. void clif_parse_roulette_info( int32 fd, map_session_data* sd ){
  17032. nullpo_retv(sd);
  17033. if (!battle_config.feature_roulette) {
  17034. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17035. return;
  17036. }
  17037. clif_roulette_info(sd);
  17038. }
  17039. /// Notification of the client that the roulette window was closed
  17040. /// 0A1D (CZ_REQ_CLOSE_ROULETTE)
  17041. void clif_parse_roulette_close( int32 fd, map_session_data* sd ){
  17042. nullpo_retv(sd);
  17043. if (!battle_config.feature_roulette) {
  17044. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17045. return;
  17046. }
  17047. sd->state.roulette_open = false;
  17048. }
  17049. /// Response to a item reward request
  17050. /// 0A22 <type>.B <bonus item>.W (ZC_RECV_ROULETTE_ITEM)
  17051. static void clif_roulette_recvitem_ack(map_session_data *sd, enum RECV_ROULETTE_ITEM_REQ type) {
  17052. #if PACKETVER >= 20141016
  17053. nullpo_retv(sd);
  17054. struct packet_roulette_itemrecv_ack p;
  17055. p.PacketType = roulettercvitemackType;
  17056. p.Result = type;
  17057. p.AdditionItemID = sd->roulette.bonusItemID;
  17058. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17059. #endif
  17060. }
  17061. /**
  17062. * Claim roulette reward
  17063. * @param sd Player
  17064. * @return 0:Success
  17065. **/
  17066. static uint8 clif_roulette_getitem(map_session_data *sd) {
  17067. struct item it;
  17068. uint8 res = 1;
  17069. unsigned short factor = 1;
  17070. nullpo_retr(1, sd);
  17071. if (sd->roulette.prizeIdx < 0)
  17072. return RECV_ITEM_FAILED;
  17073. memset(&it, 0, sizeof(it));
  17074. it.nameid = rd.nameid[sd->roulette.prizeStage][sd->roulette.prizeIdx];
  17075. it.identify = 1;
  17076. if( sd->roulette.bonusItemID != 0 ){
  17077. if( sd->roulette.bonusItemID == it.nameid && battle_config.feature_roulette_bonus_reward && !( rd.flag[sd->roulette.prizeStage][sd->roulette.prizeIdx]&1 ) ){
  17078. factor = 2;
  17079. // Reset the Bonus Item to trigger new calculation
  17080. sd->roulette.bonusItemID = 0;
  17081. }else{
  17082. for( int32 i = 0; i < MAX_ROULETTE_LEVEL && sd->roulette.bonusItemID != 0; i++ ){
  17083. for( int32 j = 0; j < MAX_ROULETTE_COLUMNS - i && sd->roulette.bonusItemID != 0; j++ ){
  17084. if( rd.nameid[i][j] == sd->roulette.bonusItemID ){
  17085. // 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
  17086. if( sd->roulette.prizeStage >= i ){
  17087. // Reset the Bonus Item to trigger new calculation and to cancel the loops
  17088. sd->roulette.bonusItemID = 0;
  17089. }
  17090. }
  17091. }
  17092. }
  17093. }
  17094. }
  17095. if ((res = pc_additem(sd, &it, rd.qty[sd->roulette.prizeStage][sd->roulette.prizeIdx] * factor, LOG_TYPE_ROULETTE)) == 0) {
  17096. ; // onSuccess
  17097. }
  17098. if( sd->roulette.bonusItemID == 0 ){
  17099. roulette_generate_bonus( *sd );
  17100. }
  17101. sd->roulette.claimPrize = false;
  17102. sd->roulette.prizeStage = 0;
  17103. sd->roulette.prizeIdx = -1;
  17104. sd->roulette.stage = 0;
  17105. return res;
  17106. }
  17107. /// Update Roulette window with current stats
  17108. /// 0A20 <result>.B <stage>.W <price index>.W <bonus item>.W <gold>.L <silver>.L <bronze>.L (ZC_ACK_GENERATE_ROULETTE)
  17109. void clif_roulette_generate( map_session_data *sd, unsigned char result, short stage, short prizeIdx, t_itemid bonusItemID ){
  17110. nullpo_retv( sd );
  17111. struct packet_roulette_generate_ack p;
  17112. p.PacketType = roulettgenerateackType;
  17113. p.Result = result;
  17114. p.Step = stage;
  17115. p.Idx = prizeIdx;
  17116. p.AdditionItemID = battle_config.feature_roulette_bonus_reward ? bonusItemID : 0;
  17117. p.RemainGold = sd->roulette_point.gold;
  17118. p.RemainSilver = sd->roulette_point.silver;
  17119. p.RemainBronze = sd->roulette_point.bronze;
  17120. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17121. }
  17122. /// Request to start the roulette
  17123. /// 0A1F (CZ_REQ_GENERATE_ROULETTE)
  17124. void clif_parse_roulette_generate( int32 fd, map_session_data* sd ){
  17125. enum GENERATE_ROULETTE_ACK result;
  17126. nullpo_retv(sd);
  17127. if (!battle_config.feature_roulette) {
  17128. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17129. return;
  17130. }
  17131. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  17132. return;
  17133. }
  17134. // Player has not claimed his prize yet
  17135. if( sd->roulette.claimPrize ){
  17136. clif_roulette_getitem( sd );
  17137. }
  17138. if (sd->roulette.stage >= MAX_ROULETTE_LEVEL){
  17139. // Make sure everything is reset
  17140. sd->roulette.stage = 0;
  17141. sd->roulette.claimPrize = false;
  17142. sd->roulette.prizeStage = 0;
  17143. sd->roulette.prizeIdx = -1;
  17144. }
  17145. if( !sd->roulette.stage && sd->roulette_point.bronze <= 0 && sd->roulette_point.silver < 10 && sd->roulette_point.gold < 10 ){
  17146. result = GENERATE_ROULETTE_NO_ENOUGH_POINT;
  17147. }else{
  17148. if (!sd->roulette.stage) {
  17149. if (sd->roulette_point.bronze > 0) {
  17150. sd->roulette_point.bronze -= 1;
  17151. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  17152. } else if (sd->roulette_point.silver > 9) {
  17153. sd->roulette_point.silver -= 10;
  17154. sd->roulette.stage = 2;
  17155. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  17156. } else if (sd->roulette_point.gold > 9) {
  17157. sd->roulette_point.gold -= 10;
  17158. sd->roulette.stage = 4;
  17159. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  17160. }
  17161. }
  17162. sd->roulette.prizeStage = sd->roulette.stage;
  17163. sd->roulette.prizeIdx = rnd()%rd.items[sd->roulette.stage];
  17164. sd->roulette.claimPrize = true;
  17165. sd->roulette.tick = gettick() + i64max( 1, ( MAX_ROULETTE_COLUMNS - sd->roulette.prizeStage - 3 ) ) * 1000;
  17166. if( rd.flag[sd->roulette.stage][sd->roulette.prizeIdx]&1 ){
  17167. result = GENERATE_ROULETTE_LOSING;
  17168. sd->roulette.stage = 0;
  17169. }else{
  17170. result = GENERATE_ROULETTE_SUCCESS;
  17171. sd->roulette.stage++;
  17172. }
  17173. }
  17174. clif_roulette_generate(sd,result,sd->roulette.prizeStage,(sd->roulette.prizeIdx == -1 ? 0 : sd->roulette.prizeIdx), sd->roulette.bonusItemID);
  17175. }
  17176. /// Request to claim a prize
  17177. /// 0A21 (CZ_RECV_ROULETTE_ITEM)
  17178. void clif_parse_roulette_item( int32 fd, map_session_data* sd ){
  17179. enum RECV_ROULETTE_ITEM_REQ type = RECV_ITEM_FAILED;
  17180. nullpo_retv(sd);
  17181. if (!battle_config.feature_roulette) {
  17182. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1497),false,SELF); //Roulette is disabled
  17183. return;
  17184. }
  17185. if( sd->roulette.tick && DIFF_TICK( sd->roulette.tick, gettick() ) > 0 ){
  17186. return;
  17187. }
  17188. if (sd->roulette.claimPrize && sd->roulette.prizeIdx != -1) {
  17189. switch (clif_roulette_getitem(sd)) {
  17190. case ADDITEM_SUCCESS:
  17191. type = RECV_ITEM_SUCCESS;
  17192. break;
  17193. case ADDITEM_INVALID:
  17194. case ADDITEM_OVERITEM:
  17195. case ADDITEM_OVERAMOUNT:
  17196. type = RECV_ITEM_OVERCOUNT;
  17197. break;
  17198. case ADDITEM_OVERWEIGHT:
  17199. type = RECV_ITEM_OVERWEIGHT;
  17200. break;
  17201. case ADDITEM_STACKLIMIT:
  17202. default:
  17203. type = RECV_ITEM_FAILED;
  17204. break;
  17205. }
  17206. }
  17207. clif_roulette_recvitem_ack(sd,type);
  17208. }
  17209. /// MERGE ITEM
  17210. /**
  17211. * Acknowledge the client about item merger result
  17212. * ZC 096F <index>.W <total>.W <result>.B (ZC_ACK_MERGE_ITEM)
  17213. * @param fd
  17214. * @param sd
  17215. **/
  17216. void clif_merge_item_ack( map_session_data &sd, enum MERGE_ITEM_ACK type, uint16 index = 0, uint16 count = 0 ){
  17217. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17218. PACKET_ZC_ACK_MERGE_ITEM p = {};
  17219. p.packetType = HEADER_ZC_ACK_MERGE_ITEM;
  17220. if( type == MERGE_ITEM_SUCCESS ){
  17221. p.index = client_index( index );
  17222. }else{
  17223. p.index = index;
  17224. }
  17225. p.amount = count;
  17226. p.reason = type;
  17227. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17228. #endif
  17229. }
  17230. /**
  17231. * Check if item has the pair to be merged
  17232. * @param sd
  17233. * @param it Item
  17234. * @return True if has pair, False if not
  17235. **/
  17236. static bool clif_merge_item_has_pair(map_session_data *sd, struct item *it) {
  17237. struct item *it_;
  17238. unsigned short i;
  17239. nullpo_retr(false, sd);
  17240. 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);
  17241. if (i < MAX_INVENTORY)
  17242. return true;
  17243. return false;
  17244. }
  17245. /**
  17246. * Check if item can be merged
  17247. * @param id Item Data
  17248. * @param it Item
  17249. * @return True if can be merged, False if not
  17250. **/
  17251. static bool clif_merge_item_check(struct item_data *id, struct item *it) {
  17252. if (!id || !it)
  17253. return false;
  17254. if (id->type == IT_CASH)
  17255. return false;
  17256. if (!itemdb_isstackable2(id))
  17257. return false;
  17258. if (itemdb_isspecial(it->card[0]))
  17259. return false;
  17260. if (!it->unique_id)
  17261. return false;
  17262. return true;
  17263. }
  17264. /**
  17265. * Open available item to be merged.
  17266. * Only show 1 item in 1 process.
  17267. * ZC 096D <size>.W { <index>.W }* (ZC_MERGE_ITEM_OPEN)
  17268. * @param sd
  17269. **/
  17270. void clif_merge_item_open( map_session_data& sd ){
  17271. #if PACKETVER_MAIN_NUM >= 20120314 || PACKETVER_RE_NUM >= 20120221 || defined(PACKETVER_ZERO)
  17272. PACKET_ZC_MERGE_ITEM_OPEN* p = reinterpret_cast<PACKET_ZC_MERGE_ITEM_OPEN*>( packet_buffer );
  17273. p->packetType = HEADER_ZC_MERGE_ITEM_OPEN;
  17274. p->packetLen = sizeof( *p );
  17275. int32 n = 0;
  17276. // Get entries
  17277. for( int32 i = 0; i < MAX_INVENTORY; i++ ){
  17278. struct item* it = &sd.inventory.u.items_inventory[i];
  17279. if( !clif_merge_item_check( sd.inventory_data[i], it ) ){
  17280. continue;
  17281. }
  17282. if( clif_merge_item_has_pair( &sd, it ) ){
  17283. p->items[n++].index = client_index( i );
  17284. p->packetLen += static_cast<decltype(p->packetLen)>( sizeof( p->items[0] ) );
  17285. }
  17286. }
  17287. // No item need to be merged
  17288. if( n < 2 ){
  17289. clif_msg( &sd, MSI_NOT_EXIST_MERGE_ITEM );
  17290. return;
  17291. }
  17292. clif_send( p, p->packetLen, &sd.bl, SELF );
  17293. #endif
  17294. }
  17295. /**
  17296. * Process item merge request.
  17297. * 096E <size>.W { <index>.W }* (CZ_REQ_MERGE_ITEM)
  17298. * @param fd
  17299. * @param sd
  17300. **/
  17301. void clif_parse_merge_item_req( int32 fd, map_session_data* sd ){
  17302. const PACKET_CZ_REQ_MERGE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_MERGE_ITEM*>( RFIFOP( fd, 0 ) );
  17303. int32 count = ( p->packetLength - sizeof( *p ) ) / sizeof( p->indices[0] );
  17304. // No item need to be merged
  17305. if( count < 2 ){
  17306. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17307. return;
  17308. }
  17309. uint16 idx_main = server_index( p->indices[0] );
  17310. if( idx_main >= MAX_INVENTORY ){
  17311. return;
  17312. }
  17313. if( !clif_merge_item_check( sd->inventory_data[idx_main], &sd->inventory.u.items_inventory[idx_main] ) ){
  17314. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17315. return;
  17316. }
  17317. // Ensure each index only comes once
  17318. std::unordered_set<uint16> indices;
  17319. for( int32 i = 1; i < count; i++ ){
  17320. uint16 idx = server_index( p->indices[i] );
  17321. if( idx >= MAX_INVENTORY ){
  17322. return;
  17323. }
  17324. if( sd->inventory_data[idx] == nullptr ){
  17325. return;
  17326. }
  17327. // Check if it is the same item
  17328. if( sd->inventory_data[idx]->nameid != sd->inventory_data[idx_main]->nameid ){
  17329. return;
  17330. }
  17331. if( !clif_merge_item_check( sd->inventory_data[idx], &sd->inventory.u.items_inventory[idx] ) ){
  17332. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17333. return;
  17334. }
  17335. indices.insert( idx );
  17336. }
  17337. if( indices.empty() ){
  17338. clif_msg( sd, MSI_NOT_EXIST_MERGE_ITEM );
  17339. return;
  17340. }
  17341. uint32 total_amount = sd->inventory.u.items_inventory[idx_main].amount;
  17342. for( uint16 idx : indices ){
  17343. total_amount += sd->inventory.u.items_inventory[idx].amount;
  17344. }
  17345. uint16 stack = sd->inventory_data[idx_main]->stack.amount;
  17346. if( stack == 0 ){
  17347. stack = MAX_AMOUNT;
  17348. }
  17349. if( total_amount >= stack ){
  17350. clif_merge_item_ack( *sd, MERGE_ITEM_FAILED_MAX_COUNT );
  17351. return;
  17352. }
  17353. // Merrrrge!!!!
  17354. for( uint16 idx : indices ){
  17355. uint16 amount = sd->inventory.u.items_inventory[idx].amount;
  17356. log_pick_pc( sd, LOG_TYPE_MERGE_ITEM, -amount, &sd->inventory.u.items_inventory[idx] );
  17357. memset( &sd->inventory.u.items_inventory[idx], 0, sizeof( sd->inventory.u.items_inventory[0] ) );
  17358. sd->inventory_data[idx] = nullptr;
  17359. clif_delitem( *sd, idx, amount, 0 );
  17360. }
  17361. sd->inventory.u.items_inventory[idx_main].amount = total_amount;
  17362. clif_merge_item_ack( *sd, MERGE_ITEM_SUCCESS, idx_main, total_amount );
  17363. }
  17364. /**
  17365. * Cancel item merge
  17366. * CZ 0974 (CZ_CANCEL_MERGE_ITEM)
  17367. * @param fd
  17368. * @param sd
  17369. **/
  17370. void clif_parse_merge_item_cancel(int32 fd, map_session_data* sd) {
  17371. return; // Nothing todo yet
  17372. }
  17373. static std::string clif_hide_name(const char* original_name)
  17374. {
  17375. std::string censored(original_name);
  17376. size_t hide = zmin(battle_config.broadcast_hide_name, censored.length() - 1);
  17377. censored.replace(censored.length() - hide, hide, hide, '*');
  17378. return censored;
  17379. }
  17380. /**
  17381. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <containerid>.W (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17382. * 07fd <size>.W <type>.B <itemid>.W <charname_len>.B <charname>.24B <source_len>.B <srcname>.24B (ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN)
  17383. * type: ITEMOBTAIN_TYPE_BOXITEM & ITEMOBTAIN_TYPE_MONSTER_ITEM "[playername] ... [sourcename] ... [itemname]" -> MsgStringTable[1629]
  17384. * type: ITEMOBTAIN_TYPE_NPC "[playername] ... [itemname]" -> MsgStringTable[1870]
  17385. **/
  17386. void clif_broadcast_obtain_special_item( const char *char_name, t_itemid nameid, t_itemid container, enum BROADCASTING_SPECIAL_ITEM_OBTAIN type ){
  17387. char name[NAME_LENGTH];
  17388. if( battle_config.broadcast_hide_name ){
  17389. std::string dispname = clif_hide_name(char_name);
  17390. safestrncpy(name, dispname.c_str(), sizeof(name));
  17391. }else{
  17392. safestrncpy( name, char_name, sizeof( name ) );
  17393. }
  17394. switch( type ){
  17395. case ITEMOBTAIN_TYPE_BOXITEM:
  17396. #if PACKETVER >= 20091201
  17397. {
  17398. PACKET_ZC_BROADCASTING_SPECIAL_ITEM_OBTAIN_item p = {};
  17399. p.PacketType = package_item_announceType;
  17400. p.PacketLength = sizeof( p );
  17401. p.type = type;
  17402. p.ItemID = client_nameid( nameid );
  17403. p.len = NAME_LENGTH;
  17404. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17405. p.boxItemID_len = (int8)sizeof( p.BoxItemID );
  17406. p.BoxItemID = client_nameid( container );
  17407. #if PACKETVER_MAIN_NUM >= 20220518 || PACKETVER_ZERO_NUM >= 20220518
  17408. p.refineLevel_len = (int8)sizeof( p.refineLevel );
  17409. p.refineLevel = 0; // TODO: implement
  17410. #endif
  17411. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17412. }
  17413. #endif
  17414. break;
  17415. case ITEMOBTAIN_TYPE_MONSTER_ITEM:
  17416. case ITEMOBTAIN_TYPE_NPC:
  17417. {
  17418. struct packet_item_drop_announce p;
  17419. p.PacketType = item_drop_announceType;
  17420. p.type = type;
  17421. p.ItemID = client_nameid( nameid );
  17422. p.len = NAME_LENGTH;
  17423. safestrncpy( p.Name, name, sizeof( p.Name ) );
  17424. if( type == ITEMOBTAIN_TYPE_MONSTER_ITEM ){
  17425. std::shared_ptr<s_mob_db> db = mob_db.find( container );
  17426. p.monsterNameLen = NAME_LENGTH;
  17427. safestrncpy( p.monsterName, db->name.c_str(), NAME_LENGTH );
  17428. }else{
  17429. p.monsterNameLen = 0;
  17430. p.monsterName[0] = '\0';
  17431. }
  17432. #if PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114
  17433. p.PacketLength = 12 + p.len + p.monsterNameLen;
  17434. #else
  17435. p.PacketLength = 8 + p.len + p.monsterNameLen;
  17436. #endif
  17437. clif_send( &p, p.PacketLength, nullptr, ALL_CLIENT );
  17438. }
  17439. break;
  17440. }
  17441. }
  17442. /// Open the dress room window.
  17443. /// 0A02 <view>.W (ZC_DRESSROOM_OPEN)
  17444. void clif_dressing_room( map_session_data& sd ){
  17445. #if PACKETVER >= 20140212
  17446. PACKET_ZC_DRESSROOM_OPEN p = {};
  17447. p.packetType = HEADER_ZC_DRESSROOM_OPEN;
  17448. p.view = 0; // Ignored
  17449. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17450. #endif
  17451. }
  17452. /// Parsing a request from the client item identify oneclick (CZ_REQ_ONECLICK_ITEMIDENTIFY).
  17453. /// 0A35 <result>.W
  17454. void clif_parse_Oneclick_Itemidentify(int32 fd, map_session_data *sd) {
  17455. #if PACKETVER >= 20150513
  17456. short idx = RFIFOW(fd,packet_db[RFIFOW(fd,0)].pos[0]) - 2, magnifier_idx;
  17457. // Ignore the request
  17458. // - Invalid item index
  17459. // - Invalid item ID or item doesn't exist
  17460. // - Item is already identified
  17461. if (idx < 0 || idx >= MAX_INVENTORY ||
  17462. sd->inventory.u.items_inventory[idx].nameid == 0 || sd->inventory_data[idx] == nullptr ||
  17463. sd->inventory.u.items_inventory[idx].identify)
  17464. return;
  17465. // Ignore the request - No magnifiers in inventory
  17466. if ((magnifier_idx = pc_search_inventory(sd, ITEMID_MAGNIFIER)) == -1 &&
  17467. (magnifier_idx = pc_search_inventory(sd, ITEMID_NOVICE_MAGNIFIER)) == -1)
  17468. return;
  17469. if (pc_delitem(sd, magnifier_idx, 1, 0, 0, LOG_TYPE_OTHER) != 0) // Deleting of magnifier failed, for whatever reason...
  17470. return;
  17471. skill_identify(sd, idx);
  17472. #endif
  17473. }
  17474. /// Starts navigation to the given target on client side
  17475. void clif_navigateTo(map_session_data *sd, const char* mapname, uint16 x, uint16 y, uint8 flag, bool hideWindow, uint16 mob_id ){
  17476. #if PACKETVER >= 20111010
  17477. int32 fd = sd->fd;
  17478. WFIFOHEAD(fd,27);
  17479. WFIFOW(fd,0) = 0x08e2;
  17480. // How detailed will our navigation be?
  17481. if( mob_id > 0 ){
  17482. x = 0;
  17483. y = 0;
  17484. WFIFOB(fd,2) = 3; // monster with destination field
  17485. }else if( x > 0 && y > 0 ){
  17486. WFIFOB(fd,2) = 0; // with coordinates
  17487. }else{
  17488. x = 0;
  17489. y = 0;
  17490. WFIFOB(fd,2) = 1; // without coordinates(will fail if you are already on the map)
  17491. }
  17492. // Which services can be used for transportation?
  17493. WFIFOB(fd,3) = flag;
  17494. // If this flag is set, the navigation window will not be opened up
  17495. WFIFOB(fd,4) = hideWindow;
  17496. // Target map
  17497. safestrncpy( WFIFOCP(fd,5),mapname,MAP_NAME_LENGTH_EXT);
  17498. // Target x
  17499. WFIFOW(fd,21) = x;
  17500. // Target y
  17501. WFIFOW(fd,23) = y;
  17502. // Target monster ID
  17503. WFIFOW(fd,25) = mob_id;
  17504. WFIFOSET(fd,27);
  17505. #endif
  17506. }
  17507. /// Send hat effects to the client.
  17508. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17509. void clif_hat_effects( map_session_data& sd, block_list& bl, enum send_target target ){
  17510. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17511. map_session_data *tsd;
  17512. block_list* tbl;
  17513. if( target == SELF ){
  17514. tsd = BL_CAST(BL_PC,&bl);
  17515. tbl = &sd.bl;
  17516. }else{
  17517. tsd = &sd;
  17518. tbl = &bl;
  17519. }
  17520. if( tsd == nullptr ){
  17521. return;
  17522. }
  17523. if( tsd->hatEffects.empty() || map_getmapdata(tbl->m)->getMapFlag(MF_NOCOSTUME) ){
  17524. return;
  17525. }
  17526. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17527. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17528. p->packetLength = sizeof( *p );
  17529. p->aid = tsd->bl.id;
  17530. p->status = 1;
  17531. for( size_t i = 0; i < tsd->hatEffects.size(); i++ ){
  17532. p->effects[i] = tsd->hatEffects[i];
  17533. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17534. }
  17535. clif_send( p, p->packetLength, tbl, target );
  17536. #endif
  17537. }
  17538. /// Send a single hat effect to the client.
  17539. /// 0A3B <Length>.W <AID>.L <Status>.B { <HatEffectId>.W } (ZC_EQUIPMENT_EFFECT)
  17540. void clif_hat_effect_single( map_session_data& sd, uint16 effectId, bool enable ){
  17541. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  17542. PACKET_ZC_EQUIPMENT_EFFECT* p = reinterpret_cast<PACKET_ZC_EQUIPMENT_EFFECT*>( packet_buffer );
  17543. p->packetType = HEADER_ZC_EQUIPMENT_EFFECT;
  17544. p->packetLength = sizeof( *p );
  17545. p->aid = sd.bl.id;
  17546. p->status = enable;
  17547. p->effects[0] = effectId;
  17548. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( p->effects[0] ) );
  17549. clif_send( p, p->packetLength, &sd.bl, AREA );
  17550. #endif
  17551. }
  17552. /// Notify the client that a sale has started
  17553. /// 09b2 <item id>.W <remaining time>.L (ZC_NOTIFY_BARGAIN_SALE_SELLING)
  17554. void clif_sale_start( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17555. #if PACKETVER_SUPPORTS_SALES
  17556. PACKET_ZC_NOTIFY_BARGAIN_SALE_SELLING p = {};
  17557. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_SELLING;
  17558. p.itemId = client_nameid( sale_item->nameid );
  17559. p.remainingTime = (uint32)(sale_item->end - time(nullptr)); // time in S
  17560. clif_send( &p, sizeof( p ), bl, target );
  17561. #endif
  17562. }
  17563. /// Notify the client that a sale has ended
  17564. /// 09b3 <item id>.W (ZC_NOTIFY_BARGAIN_SALE_CLOSE)
  17565. void clif_sale_end( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17566. #if PACKETVER_SUPPORTS_SALES
  17567. PACKET_ZC_NOTIFY_BARGAIN_SALE_CLOSE p = {};
  17568. p.packetType = HEADER_ZC_NOTIFY_BARGAIN_SALE_CLOSE;
  17569. p.itemId = client_nameid( sale_item->nameid );
  17570. clif_send( &p, sizeof( p ), bl, target );
  17571. #endif
  17572. }
  17573. /// Update the remaining amount of a sale item.
  17574. /// 09c4 <item id>.W <amount>.L (ZC_ACK_COUNT_BARGAIN_SALE_ITEM)
  17575. void clif_sale_amount( struct sale_item_data* sale_item, struct block_list* bl, enum send_target target ){
  17576. #if PACKETVER_SUPPORTS_SALES
  17577. PACKET_ZC_ACK_COUNT_BARGAIN_SALE_ITEM p = {};
  17578. p.packetType = HEADER_ZC_ACK_COUNT_BARGAIN_SALE_ITEM;
  17579. p.itemId = client_nameid( sale_item->nameid );
  17580. p.amount = sale_item->amount;
  17581. clif_send( &p, sizeof( p ), bl, target );
  17582. #endif
  17583. }
  17584. /// The client requested a refresh of the current remaining count of a sale item
  17585. /// 09ac <account id>.L <item id>.W (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17586. void clif_parse_sale_refresh( int32 fd, map_session_data* sd ){
  17587. #if PACKETVER_SUPPORTS_SALES
  17588. const PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO* p = reinterpret_cast<PACKET_CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO*>( RFIFOP( fd, 0 ) );
  17589. struct sale_item_data* sale;
  17590. if( p->AID != sd->status.account_id ){
  17591. return;
  17592. }
  17593. sale = sale_find_item( p->itemId, true );
  17594. if( sale == nullptr ){
  17595. return;
  17596. }
  17597. clif_sale_amount(sale, &sd->bl, SELF);
  17598. #endif
  17599. }
  17600. /// Opens the sale administration window on the client
  17601. /// 09b5 (ZC_OPEN_BARGAIN_SALE_TOOL)
  17602. void clif_sale_open( map_session_data* sd ){
  17603. #if PACKETVER_SUPPORTS_SALES
  17604. nullpo_retv(sd);
  17605. if( sd->state.sale_open ){
  17606. return;
  17607. }
  17608. sd->state.sale_open = true;
  17609. int32 fd = sd->fd;
  17610. WFIFOHEAD(fd, 2);
  17611. WFIFOW(fd, 0) = 0x9b5;
  17612. WFIFOSET(fd, 2);
  17613. #endif
  17614. }
  17615. /// Client request to open the sale administration window.
  17616. /// This is sent by /limitedsale
  17617. /// 09b4 <account id>.L (CZ_OPEN_BARGAIN_SALE_TOOL)
  17618. void clif_parse_sale_open( int32 fd, map_session_data* sd ){
  17619. #if PACKETVER_SUPPORTS_SALES
  17620. nullpo_retv(sd);
  17621. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17622. return;
  17623. }
  17624. char command[CHAT_SIZE_MAX];
  17625. safesnprintf( command, sizeof(command), "%climitedsale", atcommand_symbol );
  17626. is_atcommand(fd, sd, command, 1);
  17627. #endif
  17628. }
  17629. /// Closes the sale administration window on the client.
  17630. /// 09bd (ZC_CLOSE_BARGAIN_SALE_TOOL)
  17631. void clif_sale_close(map_session_data* sd) {
  17632. #if PACKETVER_SUPPORTS_SALES
  17633. nullpo_retv(sd);
  17634. if( !sd->state.sale_open ){
  17635. return;
  17636. }
  17637. sd->state.sale_open = false;
  17638. int32 fd = sd->fd;
  17639. WFIFOHEAD(fd, 2);
  17640. WFIFOW(fd, 0) = 0x9bd;
  17641. WFIFOSET(fd, 2);
  17642. #endif
  17643. }
  17644. /// Client request to close the sale administration window.
  17645. /// 09bc (CZ_CLOSE_BARGAIN_SALE_TOOL)
  17646. void clif_parse_sale_close(int32 fd, map_session_data* sd) {
  17647. #if PACKETVER_SUPPORTS_SALES
  17648. nullpo_retv(sd);
  17649. if( RFIFOL(fd, 2) != sd->status.account_id ){
  17650. return;
  17651. }
  17652. clif_sale_close(sd);
  17653. #endif
  17654. }
  17655. /// Reply to a item search request for item sale administration.
  17656. /// 09ad <result>.W <item id>.W <price>.L (ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO)
  17657. void clif_sale_search_reply( map_session_data* sd, std::shared_ptr<s_cash_item> item ){
  17658. #if PACKETVER_SUPPORTS_SALES
  17659. PACKET_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO p = {};
  17660. p.packetType = HEADER_ZC_ACK_CASH_BARGAIN_SALE_ITEM_INFO;
  17661. if( item != nullptr ){
  17662. p.result = 0;
  17663. p.itemId = client_nameid( item->nameid );
  17664. p.price = item->price;
  17665. }else{
  17666. p.result = 1;
  17667. p.itemId = 0;
  17668. p.price = 0;
  17669. }
  17670. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  17671. #endif
  17672. }
  17673. /// Search request for an item sale administration.
  17674. /// 09ac <length>.W <account id>.L <item name>.?B (CZ_REQ_CASH_BARGAIN_SALE_ITEM_INFO)
  17675. void clif_parse_sale_search( int32 fd, map_session_data* sd ){
  17676. #if PACKETVER_SUPPORTS_SALES
  17677. char item_name[ITEM_NAME_LENGTH];
  17678. nullpo_retv(sd);
  17679. if( RFIFOL(fd, 4) != sd->status.account_id ){
  17680. return;
  17681. }
  17682. if( !sd->state.sale_open ){
  17683. return;
  17684. }
  17685. safestrncpy( item_name, RFIFOCP(fd, 8), min(RFIFOW(fd, 2) - 7, ITEM_NAME_LENGTH) );
  17686. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  17687. // not found
  17688. if( id == nullptr ){
  17689. clif_sale_search_reply( sd, nullptr );
  17690. return;
  17691. }
  17692. clif_sale_search_reply( sd, cash_shop_db.findItemInTab( CASHSHOP_TAB_SALE, id->nameid ) );
  17693. #endif
  17694. }
  17695. /// Reply if an item was successfully put on sale or not.
  17696. /// 09af <result>.W (ZC_ACK_APPLY_BARGAIN_SALE_ITEM)
  17697. void clif_sale_add_reply( map_session_data* sd, enum e_sale_add_result result ){
  17698. #if PACKETVER_SUPPORTS_SALES
  17699. int32 fd = sd->fd;
  17700. WFIFOHEAD(fd, 4);
  17701. WFIFOW(fd, 0) = 0x9af;
  17702. WFIFOW(fd, 2) = (uint16)result;
  17703. WFIFOSET(fd, 4);
  17704. #endif
  17705. }
  17706. /// A client request to put an item on sale.
  17707. /// 09ae <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.B (CZ_REQ_APPLY_BARGAIN_SALE_ITEM)
  17708. /// 0a3d <account id>.L <item id>.W <amount>.L <start time>.L <hours on sale>.W (CZ_REQ_APPLY_BARGAIN_SALE_ITEM2)
  17709. void clif_parse_sale_add( int32 fd, map_session_data* sd ){
  17710. #if PACKETVER_SUPPORTS_SALES
  17711. nullpo_retv( sd );
  17712. const PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17713. if( p->AID != sd->status.account_id ){
  17714. return;
  17715. }
  17716. if( !sd->state.sale_open ){
  17717. return;
  17718. }
  17719. time_t endTime = p->startTime + p->hours * 60 * 60;
  17720. clif_sale_add_reply( sd, sale_add_item( p->itemId, p->amount, p->startTime, endTime ) );
  17721. #endif
  17722. }
  17723. /// Reply to an item removal from sale.
  17724. /// 09b1 <result>.W (ZC_ACK_REMOVE_BARGAIN_SALE_ITEM)
  17725. void clif_sale_remove_reply( map_session_data* sd, bool failed ){
  17726. #if PACKETVER_SUPPORTS_SALES
  17727. int32 fd = sd->fd;
  17728. WFIFOHEAD(fd, 4);
  17729. WFIFOW(fd, 0) = 0x9b1;
  17730. WFIFOW(fd, 2) = failed;
  17731. WFIFOSET(fd, 4);
  17732. #endif
  17733. }
  17734. /// Request to remove an item from sale.
  17735. /// 09b0 <account id>.L <item id>.W (CZ_REQ_REMOVE_BARGAIN_SALE_ITEM)
  17736. void clif_parse_sale_remove( int32 fd, map_session_data* sd ){
  17737. #if PACKETVER_SUPPORTS_SALES
  17738. nullpo_retv( sd );
  17739. const PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_REMOVE_BARGAIN_SALE_ITEM*>( RFIFOP( fd, 0 ) );
  17740. if( p->AID != sd->status.account_id ){
  17741. return;
  17742. }
  17743. if( !sd->state.sale_open ){
  17744. return;
  17745. }
  17746. clif_sale_remove_reply( sd, !sale_remove_item( p->itemId ) );
  17747. #endif
  17748. }
  17749. /// Achievement System
  17750. /// Author: Luxuri, Aleos
  17751. /**
  17752. * Sends all achievement data to the client (ZC_ALL_ACH_LIST).
  17753. * 0a23 <packetLength>.W <ACHCount>.L <ACHPoint>.L
  17754. */
  17755. void clif_achievement_list_all(map_session_data *sd)
  17756. {
  17757. nullpo_retv(sd);
  17758. if (!battle_config.feature_achievement) {
  17759. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17760. return;
  17761. }
  17762. int32 count = sd->achievement_data.count; // All achievements should be sent to the client
  17763. if (count == 0)
  17764. return;
  17765. int32 len = (50 * count) + 22;
  17766. int32 fd = sd->fd;
  17767. int32 *info = achievement_level(sd, true);
  17768. WFIFOHEAD(fd,len);
  17769. WFIFOW(fd, 0) = 0xa23;
  17770. WFIFOW(fd, 2) = len;
  17771. WFIFOL(fd, 4) = count; // Amount of achievements the player has in their list (started/completed)
  17772. WFIFOL(fd, 8) = sd->achievement_data.total_score; // Top number
  17773. WFIFOW(fd, 12) = sd->achievement_data.level; // Achievement Level (gold circle)
  17774. WFIFOL(fd, 14) = info[0]; // Achievement EXP (left number in bar)
  17775. WFIFOL(fd, 18) = info[1]; // Achievement EXP TNL (right number in bar)
  17776. for (int32 i = 0; i < count; i++) {
  17777. WFIFOL(fd, i * 50 + 22) = (uint32)sd->achievement_data.achievements[i].achievement_id;
  17778. WFIFOB(fd, i * 50 + 26) = (uint32)sd->achievement_data.achievements[i].completed > 0;
  17779. for (int32 j = 0; j < MAX_ACHIEVEMENT_OBJECTIVES; j++)
  17780. WFIFOL(fd, (i * 50) + 27 + (j * 4)) = (uint32)sd->achievement_data.achievements[i].count[j];
  17781. WFIFOL(fd, i * 50 + 67) = (uint32)sd->achievement_data.achievements[i].completed;
  17782. WFIFOB(fd, i * 50 + 71) = sd->achievement_data.achievements[i].rewarded > 0;
  17783. }
  17784. WFIFOSET(fd, len);
  17785. }
  17786. /**
  17787. * Sends a single achievement's data to the client (ZC_ACH_UPDATE).
  17788. * 0a24 <ACHPoint>.L
  17789. */
  17790. void clif_achievement_update(map_session_data *sd, struct achievement *ach, int32 count)
  17791. {
  17792. nullpo_retv(sd);
  17793. if (!battle_config.feature_achievement) {
  17794. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,772),false,SELF); // Achievements are disabled.
  17795. return;
  17796. }
  17797. int32 fd = sd->fd;
  17798. int32 *info = achievement_level(sd, true);
  17799. WFIFOHEAD(fd, packet_len(0xa24));
  17800. WFIFOW(fd, 0) = 0xa24;
  17801. WFIFOL(fd, 2) = sd->achievement_data.total_score; // Total Achievement Points (top of screen)
  17802. WFIFOW(fd, 6) = sd->achievement_data.level; // Achievement Level (gold circle)
  17803. WFIFOL(fd, 8) = info[0]; // Achievement EXP (left number in bar)
  17804. WFIFOL(fd, 12) = info[1]; // Achievement EXP TNL (right number in bar)
  17805. if (ach) {
  17806. WFIFOL(fd, 16) = ach->achievement_id; // Achievement ID
  17807. WFIFOB(fd, 20) = ach->completed > 0; // Is it complete?
  17808. for (int32 i = 0; i < MAX_ACHIEVEMENT_OBJECTIVES; i++)
  17809. WFIFOL(fd, 21 + (i * 4)) = (uint32)ach->count[i]; // 1~10 pre-reqs
  17810. WFIFOL(fd, 61) = (uint32)ach->completed; // Epoch time
  17811. WFIFOB(fd, 65) = ach->rewarded > 0; // Got reward?
  17812. } else
  17813. memset(WFIFOP(fd, 16), 0, 40);
  17814. WFIFOSET(fd, packet_len(0xa24));
  17815. }
  17816. /**
  17817. * Checks if an achievement reward can be rewarded (CZ_REQ_ACH_REWARD).
  17818. * 0a25 <achievementID>.L
  17819. */
  17820. void clif_parse_AchievementCheckReward(int32 fd, map_session_data *sd)
  17821. {
  17822. nullpo_retv(sd);
  17823. if( sd->achievement_data.save )
  17824. intif_achievement_save(sd);
  17825. achievement_check_reward(sd, RFIFOL(fd,2));
  17826. }
  17827. /**
  17828. * Returns the result of achievement_check_reward (ZC_REQ_ACH_REWARD_ACK).
  17829. * 0a26 <result>.W <achievementID>.L
  17830. */
  17831. void clif_achievement_reward_ack(int32 fd, unsigned char result, int32 achievement_id)
  17832. {
  17833. WFIFOHEAD(fd, packet_len(0xa26));
  17834. WFIFOW(fd, 0) = 0xa26;
  17835. WFIFOB(fd, 2) = result;
  17836. WFIFOL(fd, 3) = achievement_id;
  17837. WFIFOSET(fd, packet_len(0xa26));
  17838. }
  17839. /// Process the pet evolution request
  17840. /// 09fb <packetType>.W <packetLength>.W <evolutionPetEggITID>.W (CZ_PET_EVOLUTION)
  17841. void clif_parse_pet_evolution( int32 fd, map_session_data *sd ){
  17842. #if PACKETVER >= 20141008
  17843. const PACKET_CZ_PET_EVOLUTION* p = reinterpret_cast<PACKET_CZ_PET_EVOLUTION*>( RFIFOP( fd, 0 ) );
  17844. auto pet = pet_db_search( p->EvolvedPetEggID, PET_EGG );
  17845. if (!pet) {
  17846. clif_pet_evolution_result(sd, e_pet_evolution_result::FAIL_NOT_PETEGG);
  17847. return;
  17848. }
  17849. pet_evolution(sd, pet->class_);
  17850. #endif
  17851. }
  17852. /// Sends the result of the evolution to the client.
  17853. /// 09fc <result>.L (ZC_PET_EVOLUTION_RESULT)
  17854. void clif_pet_evolution_result( map_session_data* sd, e_pet_evolution_result result ){
  17855. #if PACKETVER >= 20141008
  17856. int32 fd = sd->fd;
  17857. WFIFOHEAD(fd, packet_len(0x9fc));
  17858. WFIFOW(fd, 0) = 0x9fc;
  17859. WFIFOL(fd, 2) = static_cast<uint32>(result);
  17860. WFIFOSET(fd, packet_len(0x9fc));
  17861. #endif
  17862. }
  17863. /*
  17864. * This packet is sent by /changedress or /nocosplay
  17865. *
  17866. * 0ae8
  17867. */
  17868. void clif_parse_changedress( int32 fd, map_session_data* sd ){
  17869. #if PACKETVER >= 20180103
  17870. char command[CHAT_SIZE_MAX];
  17871. safesnprintf( command, sizeof(command), "%cchangedress", atcommand_symbol );
  17872. is_atcommand( fd, sd, command, 1 );
  17873. #endif
  17874. }
  17875. /// Opens an UI window of the given type and initializes it with the given data
  17876. /// 0AE2 <type>.B <data>.L
  17877. void clif_ui_open( map_session_data& sd, enum out_ui_type ui_type, int32 data ){
  17878. #if PACKETVER >= 20151202
  17879. // If the UI requires state tracking
  17880. switch( ui_type ){
  17881. case OUT_UI_STYLIST:
  17882. sd.state.stylist_open = true;
  17883. break;
  17884. case OUT_UI_ENCHANTGRADE:
  17885. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  17886. sd.state.enchantgrade_open = true;
  17887. break;
  17888. #else
  17889. return;
  17890. #endif
  17891. }
  17892. PACKET_ZC_UI_OPEN p = {};
  17893. p.PacketType = HEADER_ZC_UI_OPEN;
  17894. p.UIType = ui_type;
  17895. #if PACKETVER >= 20171122
  17896. p.data = data;
  17897. #endif
  17898. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  17899. #endif
  17900. }
  17901. /// Request to open an UI window of the given type
  17902. /// 0A68 <type>.B
  17903. void clif_parse_open_ui( int32 fd, map_session_data* sd ){
  17904. switch( RFIFOB(fd,2) ){
  17905. case IN_UI_ATTENDANCE:
  17906. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  17907. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 791 ), false, SELF ); // You are not allowed to use the attendance system.
  17908. }else if( pc_attendance_enabled() ){
  17909. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  17910. }else{
  17911. clif_msg_color( sd, MSI_CHECK_ATTENDANCE_NOT_EVENT, color_table[COLOR_RED] );
  17912. }
  17913. break;
  17914. #if PACKETVER >= 20160316
  17915. case IN_UI_MACRO_REGISTER:
  17916. clif_ui_open(*sd, OUT_UI_CAPTCHA, 0);
  17917. break;
  17918. case IN_UI_MACRO_DETECTOR:
  17919. clif_ui_open(*sd, OUT_UI_MACRO, 0);
  17920. break;
  17921. #endif
  17922. }
  17923. }
  17924. /// Response for attedance request
  17925. /// 0AF0 <unknown>.L <data>.L
  17926. void clif_attendence_response( map_session_data *sd, int32 data ){
  17927. nullpo_retv(sd);
  17928. int32 fd = sd->fd;
  17929. WFIFOHEAD(fd,packet_len(0xAF0));
  17930. WFIFOW(fd,0) = 0xAF0;
  17931. WFIFOL(fd,2) = 0;
  17932. WFIFOL(fd,6) = data;
  17933. WFIFOSET(fd,packet_len(0xAF0));
  17934. }
  17935. /// Request from the client to retrieve today's attendance reward
  17936. /// 0AEF
  17937. void clif_parse_attendance_request( int32 fd, map_session_data* sd ){
  17938. pc_attendance_claim_reward(sd);
  17939. }
  17940. /// Send out the percentage of weight that causes it to be displayed in red.
  17941. /// 0ADE <percentage>.L
  17942. void clif_weight_limit( map_session_data* sd ){
  17943. #if PACKETVER >= 20171025
  17944. nullpo_retv(sd);
  17945. int32 fd = sd->fd;
  17946. WFIFOHEAD(fd, packet_len(0xADE));
  17947. WFIFOW(fd, 0) = 0xADE;
  17948. #ifdef RENEWAL
  17949. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate_renewal;
  17950. #else
  17951. WFIFOL(fd, 2) = battle_config.natural_heal_weight_rate;
  17952. #endif
  17953. WFIFOSET(fd, packet_len(0xADE));
  17954. #endif
  17955. }
  17956. enum e_private_airship_response : uint32{
  17957. PRIVATEAIRSHIP_OK,
  17958. PRIVATEAIRSHIP_RETRY,
  17959. PRIVATEAIRSHIP_ITEM_NOT_ENOUGH,
  17960. PRIVATEAIRSHIP_DESTINATION_MAP_INVALID,
  17961. PRIVATEAIRSHIP_SOURCE_MAP_INVALID,
  17962. PRIVATEAIRSHIP_ITEM_UNAVAILABLE
  17963. };
  17964. /// Send out the response to a private airship request
  17965. /// 0A4A <response>.L
  17966. void clif_private_airship_response( map_session_data* sd, enum e_private_airship_response response ){
  17967. #if PACKETVER >= 20180321
  17968. nullpo_retv( sd );
  17969. int32 fd = sd->fd;
  17970. WFIFOHEAD( fd, packet_len( 0xA4A ) );
  17971. WFIFOW( fd, 0 ) = 0xA4A;
  17972. WFIFOL( fd, 2 ) = response;
  17973. WFIFOSET( fd, packet_len( 0xA4A ) );
  17974. #endif
  17975. }
  17976. /// Parses a request for a private airship
  17977. /// 0A49 <mapname>.16B <itemid>.W
  17978. void clif_parse_private_airship_request( int32 fd, map_session_data* sd ){
  17979. #if PACKETVER >= 20180321
  17980. // Check if the feature is enabled
  17981. if( !battle_config.feature_privateairship ){
  17982. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 792 ), false, SELF ); // The private airship system is disabled.
  17983. return;
  17984. }
  17985. // Check if the player is allowed to warp from the source map
  17986. if( !map_getmapflag( sd->bl.m, MF_PRIVATEAIRSHIP_SOURCE ) ){
  17987. clif_private_airship_response( sd, PRIVATEAIRSHIP_SOURCE_MAP_INVALID );
  17988. return;
  17989. }
  17990. const PACKET_CZ_PRIVATE_AIRSHIP_REQUEST* p = reinterpret_cast<PACKET_CZ_PRIVATE_AIRSHIP_REQUEST*>( RFIFOP( fd, 0 ) );
  17991. char mapname[MAP_NAME_LENGTH_EXT];
  17992. safestrncpy( mapname, p->mapName, MAP_NAME_LENGTH_EXT );
  17993. int16 mapindex = mapindex_name2id( mapname );
  17994. // Check if we know the mapname
  17995. if( mapindex < 0 ){
  17996. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  17997. return;
  17998. }
  17999. int16 mapid = map_mapindex2mapid( mapindex );
  18000. // Check if the map is available on this server
  18001. if( mapid < 0 ){
  18002. // TODO: add multi map-server support, cant validate the mapflags of the other server
  18003. return;
  18004. }
  18005. // Check if the player tried to warp to the map he is on
  18006. if( sd->bl.m == mapid ){
  18007. // This is blocked by the client, but just to be sure
  18008. return;
  18009. }
  18010. // This can only be a hack, so we prevent it
  18011. if( map_getmapdata( mapid )->instance_id ){
  18012. // Ignore requests to warp directly into a running instance
  18013. return;
  18014. }
  18015. // Check if the player is allowed to warp to the target map
  18016. if( !map_getmapflag( mapid, MF_PRIVATEAIRSHIP_DESTINATION ) ){
  18017. clif_private_airship_response( sd, PRIVATEAIRSHIP_DESTINATION_MAP_INVALID );
  18018. return;
  18019. }
  18020. t_itemid item_id = p->ItemID;
  18021. // Check if the item sent by the client is known to us
  18022. if( !itemdb_group.item_exists(IG_PRIVATE_AIRSHIP, item_id) ){
  18023. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_UNAVAILABLE );
  18024. return;
  18025. }
  18026. int32 idx = pc_search_inventory( sd, item_id );
  18027. // Check if the player has the item at all
  18028. if( idx < 0 ){
  18029. clif_private_airship_response( sd, PRIVATEAIRSHIP_ITEM_NOT_ENOUGH );
  18030. return;
  18031. }
  18032. // Delete the chosen item
  18033. if( pc_delitem( sd, idx, 1, 0, 0, LOG_TYPE_PRIVATE_AIRSHIP ) ){
  18034. clif_private_airship_response( sd, PRIVATEAIRSHIP_RETRY );
  18035. return;
  18036. }
  18037. // Warp the player to a random spot on the destination map
  18038. pc_setpos( sd, mapindex, 0, 0, CLR_TELEPORT );
  18039. #endif
  18040. }
  18041. /// Sends out the usage history of the guild storage
  18042. /// 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
  18043. /// { <card item id>.W }*4 <name>.24B <time>.24B <attribute>.B }*count (ZC_ACK_GUILDSTORAGE_LOG)
  18044. void clif_guild_storage_log( map_session_data& sd, std::vector<struct guild_log_entry>& log, enum e_guild_storage_log result ){
  18045. #if PACKETVER >= 20140205
  18046. PACKET_ZC_ACK_GUILDSTORAGE_LOG* p = reinterpret_cast<PACKET_ZC_ACK_GUILDSTORAGE_LOG*>( packet_buffer );
  18047. p->packetType = HEADER_ZC_ACK_GUILDSTORAGE_LOG;
  18048. p->PacketLength = sizeof( *p );
  18049. p->result = result;
  18050. if( result == GUILDSTORAGE_LOG_FINAL_SUCCESS ){
  18051. p->amount = (uint16)log.size();
  18052. for( int32 i = 0; i < log.size(); i++ ){
  18053. PACKET_ZC_ACK_GUILDSTORAGE_LOG_sub& item = p->items[i];
  18054. struct guild_log_entry& entry = log[i];
  18055. item.id = entry.id;
  18056. item.itemId = client_nameid( entry.item.nameid );
  18057. item.amount = (uint16)( entry.amount > 0 ? entry.amount : ( entry.amount * -1 ) );
  18058. item.action = entry.amount > 0; // action = true(put), false(get)
  18059. item.refine = entry.item.refine;
  18060. item.uniqueId = entry.item.unique_id;
  18061. item.IsIdentified = entry.item.identify;
  18062. item.itemType = itemtype( entry.item.nameid );
  18063. clif_addcards( &item.slot, &entry.item );
  18064. safestrncpy( item.name, entry.name, NAME_LENGTH );
  18065. safestrncpy( item.time, entry.time, NAME_LENGTH );
  18066. item.attribute = entry.item.attribute;
  18067. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof ( item ) );
  18068. }
  18069. }else{
  18070. p->amount = 0;
  18071. }
  18072. clif_send( p, p->PacketLength, &sd.bl, SELF );
  18073. #endif
  18074. }
  18075. /// Activates the client camera info or updates the client camera with the given values.
  18076. /// 0A78 <type>.B <range>.F <rotation>.F <latitude>.F
  18077. void clif_camerainfo( map_session_data* sd, bool show, float range, float rotation, float latitude ){
  18078. #if PACKETVER >= 20160525
  18079. int32 fd = sd->fd;
  18080. WFIFOHEAD(fd, packet_len(0xa78));
  18081. WFIFOW(fd, 0) = 0xa78;
  18082. WFIFOB(fd, 2) = show;
  18083. WFIFOF(fd, 3) = range;
  18084. WFIFOF(fd, 7) = rotation;
  18085. WFIFOF(fd, 11) = latitude;
  18086. WFIFOSET(fd, packet_len(0xa78));
  18087. #endif
  18088. }
  18089. /// Activates or deactives the client camera info or updates the camera settings.
  18090. /// This packet is triggered by /viewpointvalue or /setcamera
  18091. /// 0A77 <type>.B <range>.F <rotation>.F <latitude>.F
  18092. void clif_parse_camerainfo( int32 fd, map_session_data* sd ){
  18093. char command[CHAT_SIZE_MAX];
  18094. // /viewpointvalue
  18095. if( RFIFOB( fd, 2 ) == 1 ){
  18096. safesnprintf( command, sizeof( command ), "%ccamerainfo", atcommand_symbol );
  18097. // /setcamera
  18098. }else{
  18099. safesnprintf( command, sizeof( command ), "%ccamerainfo %03.03f %03.03f %03.03f", atcommand_symbol, RFIFOF( fd, 3 ), RFIFOF( fd, 7 ), RFIFOF( fd, 11 ) );
  18100. }
  18101. is_atcommand( fd, sd, command, 1 );
  18102. }
  18103. /// Send the full list of items in the equip switch window
  18104. /// 0a9b <length>.W { <index>.W <position>.L }*
  18105. void clif_equipswitch_list( map_session_data* sd ){
  18106. #if PACKETVER >= 20170208
  18107. int32 fd = sd->fd;
  18108. int32 offset, i, position;
  18109. WFIFOW(fd, 0) = 0xa9b;
  18110. for( i = 0, offset = 4, position = 0; i < EQI_MAX; i++ ){
  18111. short index = sd->equip_switch_index[i];
  18112. if( index >= 0 && !( position & equip_bitmask[i] ) ){
  18113. WFIFOW(fd, offset) = index + 2;
  18114. WFIFOL(fd, offset + 2) = sd->inventory.u.items_inventory[index].equipSwitch;
  18115. position |= sd->inventory.u.items_inventory[index].equipSwitch;
  18116. offset += 6;
  18117. }
  18118. }
  18119. WFIFOW(fd, 2) = offset;
  18120. WFIFOSET(fd, offset);
  18121. #endif
  18122. }
  18123. /// Acknowledgement for removing an equip to the equip switch window
  18124. /// 0a9a <index>.W <position.>.L <failure>.W
  18125. void clif_equipswitch_remove( map_session_data* sd, uint16 index, uint32 pos, bool failed ){
  18126. #if PACKETVER >= 20170208
  18127. int32 fd = sd->fd;
  18128. WFIFOHEAD(fd, packet_len(0xa9a));
  18129. WFIFOW(fd, 0) = 0xa9a;
  18130. WFIFOW(fd, 2) = index + 2;
  18131. WFIFOL(fd, 4) = pos;
  18132. WFIFOW(fd, 8) = failed;
  18133. WFIFOSET(fd, packet_len(0xa9a));
  18134. #endif
  18135. }
  18136. /// Request to remove an equip from the equip switch window
  18137. /// 0a99 <index>.W <position>.L <= 20170502
  18138. /// 0a99 <index>.W
  18139. void clif_parse_equipswitch_remove( int32 fd, map_session_data* sd ){
  18140. #if PACKETVER >= 20170208
  18141. uint16 index = RFIFOW(fd, 2) - 2;
  18142. bool removed = false;
  18143. if( !battle_config.feature_equipswitch ){
  18144. return;
  18145. }
  18146. // Check if the index is valid
  18147. if( index >= MAX_INVENTORY ){
  18148. return;
  18149. }
  18150. pc_equipswitch_remove( sd, index );
  18151. #endif
  18152. }
  18153. /// Acknowledgement for adding an equip to the equip switch window
  18154. /// 0a98 <index>.W <position.>.L <flag>.L <= 20170502
  18155. /// 0a98 <index>.W <position.>.L <flag>.W
  18156. void clif_equipswitch_add( map_session_data* sd, uint16 index, uint32 pos, uint8 flag ){
  18157. #if PACKETVER >= 20170208
  18158. int32 fd = sd->fd;
  18159. WFIFOHEAD(fd, packet_len(0xa98));
  18160. WFIFOW(fd, 0) = 0xa98;
  18161. WFIFOW(fd, 2) = index + 2;
  18162. WFIFOL(fd, 4) = pos;
  18163. #if PACKETVER <= 20170502
  18164. WFIFOL(fd, 8) = flag;
  18165. #else
  18166. WFIFOW(fd, 8) = flag;
  18167. #endif
  18168. WFIFOSET(fd,packet_len(0xa98));
  18169. #endif
  18170. }
  18171. /// Request to add an equip to the equip switch window
  18172. /// 0a97 <index>.W <position>.L
  18173. void clif_parse_equipswitch_add( int32 fd, map_session_data* sd ){
  18174. #if PACKETVER >= 20170208
  18175. uint16 index = RFIFOW(fd, 2) - 2;
  18176. uint32 position = RFIFOL(fd, 4);
  18177. if( !battle_config.feature_equipswitch ){
  18178. return;
  18179. }
  18180. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  18181. return;
  18182. }
  18183. if( sd->state.trading || sd->npc_shopid ){
  18184. clif_equipswitch_add( sd, index, position, ITEM_EQUIP_ACK_OK );
  18185. return;
  18186. }
  18187. if( sd->inventory_data[index]->type == IT_AMMO ){
  18188. position = EQP_AMMO;
  18189. }
  18190. pc_equipitem( sd, index, position, true );
  18191. #endif
  18192. }
  18193. /// Acknowledgement packet for the full equip switch
  18194. /// 0a9d <failed>.W
  18195. void clif_equipswitch_reply( map_session_data* sd, bool failed ){
  18196. #if PACKETVER >= 20170208
  18197. int32 fd = sd->fd;
  18198. WFIFOHEAD(fd, packet_len(0xa9d));
  18199. WFIFOW(fd, 0) = 0xa9d;
  18200. WFIFOW(fd, 2) = failed;
  18201. WFIFOSET(fd, packet_len(0xa9d));
  18202. #endif
  18203. }
  18204. /// Request to do a full equip switch
  18205. /// 0a9c
  18206. void clif_parse_equipswitch_request( int32 fd, map_session_data* sd ){
  18207. #if PACKETVER >= 20170208
  18208. int32 i;
  18209. t_tick tick = gettick();
  18210. uint16 skill_id = ALL_EQSWITCH, skill_lv = 1;
  18211. if( DIFF_TICK(tick, sd->equipswitch_tick) < 0 ) {
  18212. // Client will not let you send a request
  18213. return;
  18214. }
  18215. sd->equipswitch_tick = tick + skill_get_cooldown( skill_id, skill_lv );
  18216. if( !battle_config.feature_equipswitch ){
  18217. return;
  18218. }
  18219. ARR_FIND( 0, EQI_MAX, i, sd->equip_switch_index[i] >= 0 );
  18220. if( i == EQI_MAX ){
  18221. // client will show: "There is no item to replace." and should not even come here
  18222. clif_equipswitch_reply( sd, false );
  18223. return;
  18224. }
  18225. if( pc_issit(sd) && !pc_setstand( sd, false ) ){
  18226. return;
  18227. }
  18228. clif_parse_skill_toid( sd, skill_id, skill_lv, sd->bl.id );
  18229. #endif
  18230. }
  18231. /// Request to do a single equip switch
  18232. /// 0ace <index>.W
  18233. void clif_parse_equipswitch_request_single( int32 fd, map_session_data* sd ){
  18234. #if PACKETVER >= 20170502
  18235. uint16 index = RFIFOW(fd, 2) - 2;
  18236. if( !battle_config.feature_equipswitch ){
  18237. return;
  18238. }
  18239. // Check if the index is valid
  18240. if( index >= MAX_INVENTORY ){
  18241. return;
  18242. }
  18243. // Check if the item exists
  18244. if (sd->inventory_data[index] == nullptr)
  18245. return;
  18246. // Check if the item was already added to equip switch
  18247. if( sd->inventory.u.items_inventory[index].equipSwitch ){
  18248. if( sd->npc_id ){
  18249. #ifdef RENEWAL
  18250. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) ){
  18251. clif_msg( sd, MSI_BUSY);
  18252. return;
  18253. }
  18254. #endif
  18255. if( !sd->npc_item_flag ){
  18256. return;
  18257. }
  18258. }
  18259. pc_equipswitch( sd, index );
  18260. return;
  18261. }
  18262. pc_equipitem( sd, index, pc_equippoint(sd, index), true );
  18263. #endif
  18264. }
  18265. void clif_parse_StartUseSkillToId( int32 fd, map_session_data* sd ){
  18266. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18267. const PACKET_CZ_USE_SKILL_START* p = reinterpret_cast<PACKET_CZ_USE_SKILL_START*>( RFIFOP( fd, 0 ) );
  18268. // Only rolling cutter is supported for now
  18269. if( p->skillId != GC_ROLLINGCUTTER ){
  18270. return;
  18271. }
  18272. // Already running - cant happen on officials, since only one skill is supported
  18273. if( sd->skill_keep_using.skill_id != 0 ){
  18274. return;
  18275. }
  18276. sd->skill_keep_using.tid = INVALID_TIMER;
  18277. sd->skill_keep_using.skill_id = p->skillId;
  18278. sd->skill_keep_using.level = p->skillLv;
  18279. sd->skill_keep_using.target = p->targetId;
  18280. clif_parse_skill_toid( sd, sd->skill_keep_using.skill_id, sd->skill_keep_using.level, sd->skill_keep_using.target );
  18281. #endif
  18282. }
  18283. void clif_parse_StopUseSkillToId( int32 fd, map_session_data* sd ){
  18284. #if PACKETVER_MAIN_NUM >= 20181002 || PACKETVER_RE_NUM >= 20181002 || PACKETVER_ZERO_NUM >= 20181010
  18285. const PACKET_CZ_USE_SKILL_END* p = reinterpret_cast<PACKET_CZ_USE_SKILL_END*>( RFIFOP( fd, 0 ) );
  18286. // Not running
  18287. if( sd->skill_keep_using.skill_id == 0 ){
  18288. return;
  18289. }
  18290. #if 0
  18291. // Hack
  18292. if( p->skillId != sd->skill_keep_using.skill_id ){
  18293. return;
  18294. }
  18295. #endif
  18296. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  18297. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  18298. sd->skill_keep_using.tid = INVALID_TIMER;
  18299. }
  18300. sd->skill_keep_using.skill_id = 0;
  18301. sd->skill_keep_using.level = 0;
  18302. sd->skill_keep_using.target = 0;
  18303. #endif
  18304. }
  18305. void clif_ping( map_session_data* sd ){
  18306. #if PACKETVER_MAIN_NUM >= 20190213 || PACKETVER_RE_NUM >= 20190213 || PACKETVER_ZERO_NUM >= 20190130
  18307. nullpo_retv( sd );
  18308. int32 fd = sd->fd;
  18309. if( !session_isActive( fd ) ){
  18310. return;
  18311. }
  18312. PACKET_ZC_PING_LIVE p = {};
  18313. p.packetType = HEADER_ZC_PING_LIVE;
  18314. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18315. #endif
  18316. }
  18317. int32 clif_ping_timer_sub( map_session_data *sd, va_list ap ){
  18318. nullpo_ret( sd );
  18319. int32 fd = sd->fd;
  18320. if( !session_isActive( fd ) ){
  18321. return 0;
  18322. }
  18323. t_tick tick = va_arg( ap, t_tick );
  18324. if( session[fd]->wdata_tick + battle_config.ping_time < tick ){
  18325. clif_ping( sd );
  18326. }
  18327. return 0;
  18328. }
  18329. TIMER_FUNC( clif_ping_timer ){
  18330. map_foreachpc( clif_ping_timer_sub, gettick() );
  18331. return 0;
  18332. }
  18333. /**
  18334. * Opens the refine UI on the designated client.
  18335. * 0aa0
  18336. */
  18337. void clif_refineui_open( map_session_data* sd ){
  18338. #if PACKETVER >= 20161012
  18339. nullpo_retv( sd );
  18340. PACKET_ZC_OPEN_REFINING_UI p = {};
  18341. p.packetType = HEADER_ZC_OPEN_REFINING_UI;
  18342. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18343. sd->state.refineui_open = true;
  18344. #endif
  18345. }
  18346. /**
  18347. * Parses cancel/close of the refine UI on client side.
  18348. * 0aa4
  18349. */
  18350. void clif_parse_refineui_close( int32 fd, map_session_data* sd ){
  18351. #if PACKETVER >= 20161012
  18352. sd->state.refineui_open = false;
  18353. #endif
  18354. }
  18355. /**
  18356. * Adds the selected item into the refine UI and sends the possible materials
  18357. * to the client.
  18358. * 0aa2 <length>.W <index>.W <catalyst count>.B { <material>.W <chance>.B <price>.L }*
  18359. */
  18360. void clif_refineui_info( map_session_data* sd, uint16 index ){
  18361. #if PACKETVER >= 20161012
  18362. int32 fd = sd->fd;
  18363. // Get the item db reference
  18364. struct item_data* id = sd->inventory_data[index];
  18365. // No item data was found
  18366. if( id == nullptr ){
  18367. return;
  18368. }
  18369. // Check the inventory
  18370. struct item* item = &sd->inventory.u.items_inventory[index];
  18371. // No item was found at the given index
  18372. if( item == nullptr ){
  18373. return;
  18374. }
  18375. // Check if the item is identified
  18376. if( !item->identify ){
  18377. return;
  18378. }
  18379. // Check if the item is broken
  18380. if( item->attribute ){
  18381. return;
  18382. }
  18383. PACKET_ZC_REFINING_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_REFINING_MATERIAL_LIST*>( packet_buffer );
  18384. p->packetType = HEADER_ZC_REFINING_MATERIAL_LIST;
  18385. p->packetLength = sizeof( *p );
  18386. p->itemIndex = client_index( index );
  18387. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18388. // No possibilities were found
  18389. if( info == nullptr ){
  18390. p->blacksmithBlessing = 0;
  18391. }else{
  18392. p->blacksmithBlessing = static_cast<decltype(p->blacksmithBlessing)>( info->blessing_amount );
  18393. for( uint16 i = REFINE_COST_NORMAL, count = 0; i < REFINE_COST_MAX; i++ ){
  18394. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, i );
  18395. if( cost == nullptr ){
  18396. continue;
  18397. }
  18398. PACKET_ZC_REFINING_MATERIAL_LIST_SUB& entry = p->req[count];
  18399. entry.itemId = client_nameid( cost->nameid );
  18400. entry.chance = static_cast<decltype(entry.chance)>( cost->chance / 100 );
  18401. entry.zeny = cost->zeny;
  18402. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  18403. count++;
  18404. }
  18405. }
  18406. clif_send( p, p->packetLength, &sd->bl, SELF );
  18407. #endif
  18408. }
  18409. /**
  18410. * Client request to add an item to the refine UI.
  18411. * 0aa1 <index>.W
  18412. */
  18413. void clif_parse_refineui_add( int32 fd, map_session_data* sd ){
  18414. #if PACKETVER >= 20161012
  18415. const PACKET_CZ_REFINING_SELECT_ITEM* p = reinterpret_cast<PACKET_CZ_REFINING_SELECT_ITEM*>( RFIFOP( fd, 0 ) );
  18416. uint16 index = server_index( p->index );
  18417. // Check if the refine UI is open
  18418. if( !sd->state.refineui_open ){
  18419. return;
  18420. }
  18421. // Check if the index is valid
  18422. if( index >= MAX_INVENTORY ){
  18423. return;
  18424. }
  18425. // Send out the requirements for the refine process
  18426. clif_refineui_info( sd, index );
  18427. #endif
  18428. }
  18429. /**
  18430. * Client requests to try to refine an item.
  18431. * 0aa3 <index>.W <material>.W <catalyst>.B
  18432. */
  18433. void clif_parse_refineui_refine( int32 fd, map_session_data* sd ){
  18434. #if PACKETVER >= 20161012
  18435. const PACKET_CZ_REQ_REFINING* p = reinterpret_cast<PACKET_CZ_REQ_REFINING*>( RFIFOP( fd, 0 ) );
  18436. uint16 index = server_index( p->index );
  18437. t_itemid material = p->itemId;
  18438. int16 j;
  18439. // Check if the refine UI is open
  18440. if( !sd->state.refineui_open ){
  18441. return;
  18442. }
  18443. // Check if the index is valid
  18444. if( index >= MAX_INVENTORY ){
  18445. return;
  18446. }
  18447. // Get the item db reference
  18448. struct item_data* id = sd->inventory_data[index];
  18449. // No item data was found
  18450. if( id == nullptr ){
  18451. return;
  18452. }
  18453. // Check the inventory
  18454. struct item* item = &sd->inventory.u.items_inventory[index];
  18455. // No item was found at the given index
  18456. if( item == nullptr ){
  18457. return;
  18458. }
  18459. // Check if the item is identified
  18460. if( !item->identify ){
  18461. return;
  18462. }
  18463. // Check if the item is broken
  18464. if( item->attribute ){
  18465. return;
  18466. }
  18467. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *id, *item );
  18468. // No refine possible
  18469. if( info == nullptr ){
  18470. return;
  18471. }
  18472. // No possibilities were found
  18473. if( info->costs.empty() ){
  18474. return;
  18475. }
  18476. // Check if the player has the selected material
  18477. if( ( j = pc_search_inventory( sd, material ) ) < 0 ){
  18478. return;
  18479. }
  18480. int16 blacksmith_index = -1;
  18481. uint16 blacksmith_amount = 0;
  18482. // Check if the player has enough blacksmith blessings
  18483. if( p->blacksmithBlessing != 0 ){
  18484. blacksmith_amount = info->blessing_amount;
  18485. // Player tried to use blacksmith blessing on a refine level, where it is not available
  18486. if( blacksmith_amount == 0 ){
  18487. return;
  18488. }
  18489. // Check if the player has blacksmith blessings
  18490. if( ( blacksmith_index = pc_search_inventory( sd, ITEMID_BLACKSMITH_BLESSING ) ) < 0 ){
  18491. return;
  18492. }
  18493. // Check if the player has enough blacksmith blessings
  18494. if( sd->inventory.u.items_inventory[blacksmith_index].amount < blacksmith_amount ){
  18495. return;
  18496. }
  18497. }
  18498. std::shared_ptr<s_refine_cost> cost = nullptr;
  18499. for( const auto& pair : info->costs ){
  18500. if( pair.second->nameid == material ){
  18501. cost = pair.second;
  18502. break;
  18503. }
  18504. }
  18505. // The material was not in the list of possible materials
  18506. if( cost == nullptr ){
  18507. return;
  18508. }
  18509. // Try to pay for the refine
  18510. if( pc_payzeny( sd, cost->zeny, LOG_TYPE_CONSUME ) ){
  18511. clif_npc_buy_result( sd, e_purchase_result::PURCHASE_FAIL_MONEY ); // "You do not have enough zeny."
  18512. return;
  18513. }
  18514. // Delete the required material
  18515. if( pc_delitem( sd, j, 1, 0, 0, LOG_TYPE_CONSUME ) ){
  18516. return;
  18517. }
  18518. // Delete the required blacksmith blessings
  18519. if( blacksmith_amount > 0 && pc_delitem( sd, blacksmith_index, blacksmith_amount, 0, 0, LOG_TYPE_CONSUME ) ){
  18520. return;
  18521. }
  18522. // Try to refine the item
  18523. if( cost->chance >= ( rnd() % 10000 ) ){
  18524. log_pick_pc( sd, LOG_TYPE_OTHER, -1, item );
  18525. // Success
  18526. item->refine = cap_value( item->refine + 1, 0, MAX_REFINE );
  18527. log_pick_pc( sd, LOG_TYPE_OTHER, 1, item );
  18528. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_SUCCESS );
  18529. clif_refine( *sd, index, ITEMREFINING_SUCCESS );
  18530. if (info->broadcast_success) {
  18531. clif_broadcast_refine_result(*sd, item->nameid, item->refine, true);
  18532. }
  18533. if( id->type == IT_WEAPON ){
  18534. achievement_update_objective( sd, AG_ENCHANT_SUCCESS, 2, id->weapon_level, item->refine );
  18535. }
  18536. clif_refineui_info( sd, index );
  18537. }else{
  18538. // Failure
  18539. if (info->broadcast_failure) {
  18540. clif_broadcast_refine_result(*sd, item->nameid, item->refine, false);
  18541. }
  18542. // Blacksmith blessings were used to prevent breaking and downgrading
  18543. if( blacksmith_amount > 0 ){
  18544. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18545. clif_refineui_info( sd, index );
  18546. // Delete the item if it is breakable
  18547. }else if( cost->breaking_rate > 0 && ( rnd() % 10000 ) < cost->breaking_rate ){
  18548. clif_refine( *sd, index, ITEMREFINING_FAILURE );
  18549. pc_delitem( sd, index, 1, 0, 2, LOG_TYPE_CONSUME );
  18550. // Downgrade the item if necessary
  18551. }else if( cost->downgrade_amount > 0 ){
  18552. item->refine = cap_value( item->refine - cost->downgrade_amount, 0, MAX_REFINE );
  18553. clif_refine( *sd, index, ITEMREFINING_DOWNGRADE );
  18554. clif_refineui_info(sd, index);
  18555. // Only show failure, but dont do anything
  18556. }else{
  18557. clif_refine( *sd, index, ITEMREFINING_FAILURE2 );
  18558. clif_refineui_info( sd, index );
  18559. }
  18560. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_FAILURE );
  18561. achievement_update_objective( sd, AG_ENCHANT_FAIL, 1, 1 );
  18562. }
  18563. #endif
  18564. }
  18565. void clif_unequipall_reply( map_session_data* sd, bool failed ){
  18566. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18567. PACKET_ZC_ACK_TAKEOFF_EQUIP_ALL p = {};
  18568. p.PacketType = HEADER_ZC_ACK_TAKEOFF_EQUIP_ALL;
  18569. p.result = failed;
  18570. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18571. #endif // PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103
  18572. }
  18573. void clif_parse_unequipall( int32 fd, map_session_data* sd ){
  18574. #if PACKETVER_MAIN_NUM >= 20210818 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20210818
  18575. if( pc_cant_act( sd ) ){
  18576. clif_unequipall_reply( sd, true );
  18577. return;
  18578. }
  18579. const PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL* p = reinterpret_cast<PACKET_CZ_REQ_TAKEOFF_EQUIP_ALL*>( RFIFOP( fd, 0 ) );
  18580. #if PACKETVER_MAIN_NUM >= 20230906
  18581. uint32 location = p->location;
  18582. int32 max = EQI_MAX;
  18583. #else
  18584. uint32 location = 0xFFFFFFFF;
  18585. int32 max = EQI_COSTUME_HEAD_TOP;
  18586. #endif
  18587. for( int32 i = 0; i < max; i++ ){
  18588. if( sd->equip_index[i] >= 0 && ( location & equip_bitmask[i] ) != 0 ){
  18589. pc_unequipitem( sd, sd->equip_index[i], 1 );
  18590. }
  18591. }
  18592. clif_unequipall_reply( sd, false );
  18593. #endif
  18594. }
  18595. void clif_stylist_response( map_session_data* sd, bool failed ){
  18596. #if PACKETVER >= 20151104
  18597. PACKET_ZC_STYLE_CHANGE_RES p = {};
  18598. p.PacketType = HEADER_ZC_STYLE_CHANGE_RES;
  18599. p.flag = failed;
  18600. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18601. if( !failed ){
  18602. sd->state.stylist_open = false;
  18603. }
  18604. #endif
  18605. }
  18606. bool clif_parse_stylist_buy_sub( map_session_data* sd, _look look, int16 index ){
  18607. std::shared_ptr<s_stylist_list> list = stylist_db.find( look );
  18608. if( list == nullptr ){
  18609. return false;
  18610. }
  18611. std::shared_ptr<s_stylist_entry> entry = util::umap_find( list->entries, index );
  18612. if( entry == nullptr ){
  18613. return false;
  18614. }
  18615. std::shared_ptr<s_stylist_costs> costs;
  18616. if( ( sd->class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  18617. costs = entry->doram;
  18618. }else{
  18619. costs = entry->human;
  18620. }
  18621. if( costs == nullptr ){
  18622. return false;
  18623. }
  18624. if( sd->status.zeny < costs->price ){
  18625. return false;
  18626. }
  18627. int16 inventoryIndex = -1;
  18628. if( costs->requiredItem != 0 ){
  18629. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18630. if( inventoryIndex < 0 ){
  18631. // No other option
  18632. if( costs->requiredItemBox == 0 ){
  18633. return false;
  18634. }
  18635. // Check if the box that contains the item is in the inventory
  18636. inventoryIndex = pc_search_inventory( sd, costs->requiredItemBox );
  18637. // The box containing the item also does not exist
  18638. if( inventoryIndex < 0 ){
  18639. return false;
  18640. }
  18641. }
  18642. }else if( costs->requiredItemBox != 0 ){
  18643. inventoryIndex = pc_search_inventory( sd, costs->requiredItem );
  18644. if( inventoryIndex < 0 ){
  18645. return false;
  18646. }
  18647. }
  18648. if( inventoryIndex >= 0 && pc_delitem( sd, inventoryIndex, 1, 0, 0, LOG_TYPE_OTHER ) != 0 ){
  18649. return false;
  18650. }
  18651. if( costs->price > 0 && pc_payzeny( sd, costs->price, LOG_TYPE_OTHER ) != 0 ){
  18652. return false;
  18653. }
  18654. switch( look ){
  18655. case LOOK_HAIR:
  18656. case LOOK_HAIR_COLOR:
  18657. case LOOK_CLOTHES_COLOR:
  18658. case LOOK_BODY2:
  18659. pc_changelook( sd, look, entry->value );
  18660. break;
  18661. case LOOK_HEAD_BOTTOM:
  18662. case LOOK_HEAD_MID:
  18663. case LOOK_HEAD_TOP: {
  18664. struct mail_message msg = {};
  18665. msg.dest_id = sd->status.char_id;
  18666. safestrncpy( msg.send_name, "Styling Shop", NAME_LENGTH );
  18667. safestrncpy( msg.title, "<MSG>2949</MSG>", MAIL_TITLE_LENGTH );
  18668. safestrncpy( msg.body, "<MSG>2950</MSG>", MAIL_BODY_LENGTH );
  18669. msg.item[0].nameid = entry->value;
  18670. msg.item[0].identify = 1;
  18671. msg.item[0].amount = 1;
  18672. msg.status = MAIL_NEW;
  18673. msg.type = MAIL_INBOX_NORMAL;
  18674. msg.timestamp = time( nullptr );
  18675. intif_Mail_send( 0, &msg );
  18676. } break;
  18677. }
  18678. return true;
  18679. }
  18680. void clif_parse_stylist_buy( int32 fd, map_session_data* sd ){
  18681. #if PACKETVER >= 20151104
  18682. #if PACKETVER >= 20180516
  18683. const PACKET_CZ_REQ_STYLE_CHANGE2* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE2*>( RFIFOP( fd, 0 ) );
  18684. #else
  18685. const PACKET_CZ_REQ_STYLE_CHANGE* p = reinterpret_cast<PACKET_CZ_REQ_STYLE_CHANGE*>( RFIFOP( fd, 0 ) );
  18686. #endif
  18687. if( p->HeadPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR_COLOR, p->HeadPalette ) ){
  18688. clif_stylist_response( sd, true );
  18689. return;
  18690. }
  18691. if( p->HeadStyle != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HAIR, p->HeadStyle ) ){
  18692. clif_stylist_response( sd, true );
  18693. return;
  18694. }
  18695. if( p->BodyPalette != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_CLOTHES_COLOR, p->BodyPalette ) ){
  18696. clif_stylist_response( sd, true );
  18697. return;
  18698. }
  18699. if( p->TopAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_TOP, p->TopAccessory ) ){
  18700. clif_stylist_response( sd, true );
  18701. return;
  18702. }
  18703. if( p->MidAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_MID, p->MidAccessory ) ){
  18704. clif_stylist_response( sd, true );
  18705. return;
  18706. }
  18707. if( p->BottomAccessory != 0 && !clif_parse_stylist_buy_sub( sd, LOOK_HEAD_BOTTOM, p->BottomAccessory ) ){
  18708. clif_stylist_response( sd, true );
  18709. return;
  18710. }
  18711. #if PACKETVER >= 20180516
  18712. if( p->BodyStyle != 0 && ( sd->class_ & JOBL_THIRD ) != 0 && ( sd->class_ & JOBL_FOURTH ) == 0 && !clif_parse_stylist_buy_sub( sd, LOOK_BODY2, p->BodyStyle ) ){
  18713. clif_stylist_response( sd, true );
  18714. return;
  18715. }
  18716. #endif
  18717. clif_stylist_response( sd, false );
  18718. #endif
  18719. }
  18720. void clif_parse_stylist_close( int32 fd, map_session_data* sd ){
  18721. #if PACKETVER >= 20151104
  18722. sd->state.stylist_open = false;
  18723. #endif
  18724. }
  18725. void clif_inventory_expansion_info( map_session_data* sd ){
  18726. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18727. nullpo_retv( sd );
  18728. PACKET_ZC_EXTEND_BODYITEM_SIZE p = {};
  18729. p.packetType = HEADER_ZC_EXTEND_BODYITEM_SIZE;
  18730. p.expansionSize = sd->status.inventory_slots - INVENTORY_BASE_SIZE;
  18731. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18732. #endif
  18733. }
  18734. enum class e_inventory_expansion_response : uint8{
  18735. ASK_CONFIRMATION = 0,
  18736. FAILED,
  18737. BUSY,
  18738. MISSING_ITEM,
  18739. MAXIMUM_REACHED
  18740. };
  18741. void clif_inventory_expansion_response( map_session_data* sd, e_inventory_expansion_response response ){
  18742. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18743. nullpo_retv( sd );
  18744. PACKET_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE p = {};
  18745. p.packetType = HEADER_ZC_ACK_OPEN_MSGBOX_EXTEND_BODYITEM_SIZE;
  18746. p.result = (uint8)response;
  18747. p.itemId = sd->state.inventory_expansion_confirmation;
  18748. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18749. #endif
  18750. }
  18751. void clif_parse_inventory_expansion_request( int32 fd, map_session_data* sd ){
  18752. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18753. // Check if player is dead or busy with other stuff
  18754. if( pc_isdead( sd ) || pc_cant_act( sd ) ){
  18755. clif_inventory_expansion_response( sd, e_inventory_expansion_response::BUSY );
  18756. return;
  18757. }
  18758. // Check if the player already reached the maximum
  18759. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18760. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18761. return;
  18762. }
  18763. static std::map<t_itemid, uint16> items = {
  18764. // The order of entries in this list defines which will be used first
  18765. // This order and the usable items are hardcoded into the client
  18766. // The number of increased slots is "hardcoded" in the message of the client and cannot be sent per item
  18767. { ITEMID_INVENTORY_EX_EVT, 10 },
  18768. { ITEMID_INVENTORY_EX_DIS, 10 },
  18769. { ITEMID_INVENTORY_EX, 10 },
  18770. };
  18771. int16 index = -1;
  18772. bool found_over_limit = false;
  18773. for( const auto& entry : items ){
  18774. // Check if the player has the required item
  18775. index = pc_search_inventory( sd, entry.first );
  18776. // Found an item
  18777. if( index >= 0 ){
  18778. // Check if the player would exceed the maximum
  18779. if( sd->status.inventory_slots + entry.second > MAX_INVENTORY ){
  18780. found_over_limit = true;
  18781. }else{
  18782. found_over_limit = false;
  18783. sd->state.inventory_expansion_confirmation = entry.first;
  18784. sd->state.inventory_expansion_amount = entry.second;
  18785. break;
  18786. }
  18787. }
  18788. }
  18789. // Check if an item was found
  18790. if( sd->state.inventory_expansion_confirmation == 0 ){
  18791. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MISSING_ITEM );
  18792. return;
  18793. }
  18794. // Check if an item would have been found, but the player would exceed the maximum
  18795. if( found_over_limit ){
  18796. clif_inventory_expansion_response( sd, e_inventory_expansion_response::MAXIMUM_REACHED );
  18797. return;
  18798. }
  18799. // The player met all requirements => ask him for confirmation
  18800. clif_inventory_expansion_response( sd, e_inventory_expansion_response::ASK_CONFIRMATION );
  18801. #endif
  18802. }
  18803. enum class e_inventory_expansion_result : uint8{
  18804. SUCCESS = 0,
  18805. FAILED,
  18806. BUSY,
  18807. MISSING_ITEM,
  18808. MAXIMUM_REACHED
  18809. };
  18810. void clif_inventory_expansion_result( map_session_data* sd, e_inventory_expansion_result result ){
  18811. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18812. nullpo_retv( sd );
  18813. PACKET_ZC_ACK_EXTEND_BODYITEM_SIZE p = {};
  18814. p.packetType = HEADER_ZC_ACK_EXTEND_BODYITEM_SIZE;
  18815. p.result = (uint8)result;
  18816. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  18817. // Reset the state tracking
  18818. sd->state.inventory_expansion_confirmation = 0;
  18819. sd->state.inventory_expansion_amount = 0;
  18820. #endif
  18821. }
  18822. void clif_parse_inventory_expansion_confirm( int32 fd, map_session_data* sd ){
  18823. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18824. if( sd->state.inventory_expansion_confirmation == 0 ){
  18825. return;
  18826. }
  18827. // Check if player is dead
  18828. if( pc_isdead( sd ) ){
  18829. clif_inventory_expansion_result( sd, e_inventory_expansion_result::BUSY );
  18830. return;
  18831. }
  18832. // Check if the player already reached the maximum
  18833. if( sd->status.inventory_slots >= MAX_INVENTORY ){
  18834. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18835. return;
  18836. }
  18837. // Check if the player has the required item
  18838. int32 index = pc_search_inventory( sd, sd->state.inventory_expansion_confirmation );
  18839. // The player did not have the item anymore
  18840. if( index < 0 ){
  18841. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MISSING_ITEM );
  18842. return;
  18843. }
  18844. // Check if the player would exceed the maximum
  18845. if( sd->status.inventory_slots + sd->state.inventory_expansion_amount > MAX_INVENTORY ){
  18846. clif_inventory_expansion_result( sd, e_inventory_expansion_result::MAXIMUM_REACHED );
  18847. return;
  18848. }
  18849. // Delete the required item
  18850. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_OTHER ) ){
  18851. clif_inventory_expansion_result( sd, e_inventory_expansion_result::FAILED );
  18852. return;
  18853. }
  18854. // Increase the slots
  18855. sd->status.inventory_slots += sd->state.inventory_expansion_amount;
  18856. // Save player data (slots) and inventory data (removed item)
  18857. chrif_save( sd, CSAVE_NORMAL | CSAVE_INVENTORY );
  18858. // Inform the player of success
  18859. clif_inventory_expansion_result( sd, e_inventory_expansion_result::SUCCESS );
  18860. clif_inventory_expansion_info( sd );
  18861. #endif
  18862. }
  18863. void clif_parse_inventory_expansion_reject( int32 fd, map_session_data* sd ){
  18864. #if PACKETVER_MAIN_NUM >= 20181219 || PACKETVER_RE_NUM >= 20181219 || PACKETVER_ZERO_NUM >= 20181212
  18865. sd->state.inventory_expansion_confirmation = 0;
  18866. sd->state.inventory_expansion_amount = 0;
  18867. #endif
  18868. }
  18869. void clif_barter_open( map_session_data& sd, struct npc_data& nd ){
  18870. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18871. if( nd.subtype != NPCTYPE_BARTER || nd.u.barter.extended || sd.state.barter_open ){
  18872. return;
  18873. }
  18874. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18875. if( barter == nullptr ){
  18876. return;
  18877. }
  18878. sd.state.barter_open = true;
  18879. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  18880. p->packetType = HEADER_ZC_NPC_BARTER_MARKET_ITEMINFO;
  18881. p->packetLength = sizeof( *p );
  18882. int16 count = 0;
  18883. for( const auto& itemPair : barter->items ){
  18884. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  18885. if( id == nullptr ){
  18886. continue;
  18887. }
  18888. PACKET_ZC_NPC_BARTER_MARKET_ITEMINFO_sub& item = p->list[count];
  18889. item.nameid = client_nameid( id->nameid );
  18890. item.type = itemtype( id->nameid );
  18891. if( itemPair.second->stockLimited ){
  18892. item.amount = itemPair.second->stock;
  18893. }else{
  18894. item.amount = -1;
  18895. }
  18896. item.weight = id->weight;
  18897. item.index = itemPair.second->index;
  18898. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  18899. item.viewSprite = id->look;
  18900. item.location = pc_equippoint_sub( &sd, id.get() );
  18901. #endif
  18902. // Use a loop if someone did not start with index 0
  18903. for( const auto& requirementPair : itemPair.second->requirements ){
  18904. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  18905. item.currencyNameid = client_nameid( requirement->nameid );
  18906. item.currencyAmount = requirement->amount;
  18907. // It is a normal barter, cancel after first entry
  18908. break;
  18909. }
  18910. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( item ) );
  18911. count++;
  18912. }
  18913. clif_send( p, p->packetLength, &sd.bl, SELF );
  18914. #endif
  18915. }
  18916. void clif_parse_barter_close( int32 fd, map_session_data* sd ){
  18917. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18918. if( sd->state.barter_open ){
  18919. sd->npc_shopid = 0;
  18920. sd->state.barter_open = false;
  18921. }
  18922. #endif
  18923. }
  18924. void clif_parse_barter_buy( int32 fd, map_session_data* sd ){
  18925. #if PACKETVER_MAIN_NUM >= 20190116 || PACKETVER_RE_NUM >= 20190116 || PACKETVER_ZERO_NUM >= 20181226
  18926. // No shop open
  18927. if( sd->npc_shopid == 0 || !sd->state.barter_open ){
  18928. return;
  18929. }
  18930. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  18931. // Unknown shop
  18932. if( nd == nullptr ){
  18933. return;
  18934. }
  18935. // Not a barter
  18936. if( nd->subtype != NPCTYPE_BARTER ){
  18937. return;
  18938. }
  18939. // It is an extended barter
  18940. if( nd->u.barter.extended ){
  18941. return;
  18942. }
  18943. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  18944. if( barter == nullptr ){
  18945. return;
  18946. }
  18947. const PACKET_CZ_NPC_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  18948. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  18949. // Empty purchase list
  18950. if( entries == 0 ){
  18951. return;
  18952. }
  18953. std::vector<s_barter_purchase> purchases;
  18954. purchases.reserve( entries );
  18955. // Make sure each shop index and target item id is only used once
  18956. for( int32 i = 0; i < entries; i++ ){
  18957. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  18958. // Invalid shop index
  18959. if( item == nullptr ){
  18960. return;
  18961. }
  18962. for( int32 j = i + 1; j < entries; j++ ){
  18963. // Same shop index
  18964. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  18965. return;
  18966. }
  18967. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  18968. // Invalid shop index
  18969. if( item2 == nullptr ){
  18970. return;
  18971. }
  18972. // Same target item id
  18973. if( item->nameid == item2->nameid ){
  18974. return;
  18975. }
  18976. }
  18977. s_barter_purchase purchase = {};
  18978. purchase.item = item;
  18979. purchase.amount = p->list[i].amount;
  18980. purchases.push_back( purchase );
  18981. }
  18982. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  18983. #endif
  18984. }
  18985. void clif_barter_extended_open( map_session_data& sd, struct npc_data& nd ){
  18986. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  18987. if( nd.subtype != NPCTYPE_BARTER || !nd.u.barter.extended || sd.state.barter_extended_open ){
  18988. return;
  18989. }
  18990. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd.exname );
  18991. if( barter == nullptr ){
  18992. return;
  18993. }
  18994. sd.state.barter_extended_open = true;
  18995. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO* p = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO*>( packet_buffer );
  18996. p->packetType = HEADER_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO;
  18997. p->packetLength = sizeof( *p );
  18998. p->items_count = 0;
  18999. for( const auto& itemPair : barter->items ){
  19000. std::shared_ptr<item_data> id = item_db.find(itemPair.second->nameid);
  19001. if( id == nullptr ){
  19002. continue;
  19003. }
  19004. // Needs dynamic calculation, because of variable currencies
  19005. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub* item = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub*>( WBUFP( p, p->packetLength ) );
  19006. item->nameid = client_nameid( id->nameid );
  19007. item->type = itemtype( id->nameid );
  19008. if( itemPair.second->stockLimited ){
  19009. item->amount = itemPair.second->stock;
  19010. }else{
  19011. item->amount = -1;
  19012. }
  19013. item->weight = id->weight;
  19014. item->index = itemPair.second->index;
  19015. item->zeny = itemPair.second->price;
  19016. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103
  19017. item->viewSprite = id->look;
  19018. item->location = pc_equippoint_sub( &sd, id.get() );
  19019. #endif
  19020. // Because of a MSVS bug, the currencies have been defined with a fixed array entry, which has to be substracted
  19021. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *item ) - sizeof( item->currencies ) );
  19022. p->items_count++;
  19023. item->currency_count = 0;
  19024. for( const auto& requirementPair : itemPair.second->requirements ){
  19025. // Needs dynamic calculation, because of variable currencies
  19026. PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2* req = reinterpret_cast<PACKET_ZC_NPC_EXPANDED_BARTER_MARKET_ITEMINFO_sub2*>( WBUFP( p, p->packetLength ) );
  19027. std::shared_ptr<s_npc_barter_requirement> requirement = requirementPair.second;
  19028. req->nameid = requirement->nameid;
  19029. if( requirement->refine >= 0 ){
  19030. req->refine_level = requirement->refine;
  19031. }else{
  19032. req->refine_level = 0;
  19033. }
  19034. req->amount = requirement->amount;
  19035. req->type = itemtype( requirement->nameid );
  19036. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( *req ) );
  19037. item->currency_count++;
  19038. }
  19039. }
  19040. clif_send( p, p->packetLength, &sd.bl, SELF );
  19041. #endif
  19042. }
  19043. void clif_parse_barter_extended_close( int32 fd, map_session_data* sd ){
  19044. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  19045. if( sd->state.barter_extended_open ){
  19046. sd->npc_shopid = 0;
  19047. sd->state.barter_extended_open = false;
  19048. }
  19049. #endif
  19050. }
  19051. void clif_parse_barter_extended_buy( int32 fd, map_session_data* sd ){
  19052. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106 || PACKETVER_ZERO_NUM >= 20191127
  19053. // No shop open
  19054. if( sd->npc_shopid == 0 || !sd->state.barter_extended_open ){
  19055. return;
  19056. }
  19057. struct npc_data* nd = map_id2nd( sd->npc_shopid );
  19058. // Unknown shop
  19059. if( nd == nullptr ){
  19060. return;
  19061. }
  19062. // Not a barter
  19063. if( nd->subtype != NPCTYPE_BARTER ){
  19064. return;
  19065. }
  19066. // Not an extended barter
  19067. if( !nd->u.barter.extended ){
  19068. return;
  19069. }
  19070. std::shared_ptr<s_npc_barter> barter = barter_db.find( nd->exname );
  19071. if( barter == nullptr ){
  19072. return;
  19073. }
  19074. const PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE* p = reinterpret_cast<PACKET_CZ_NPC_EXPANDED_BARTER_MARKET_PURCHASE*>( RFIFOP( fd, 0 ) );
  19075. uint16 entries = ( p->packetLength - sizeof( *p ) ) / sizeof( p->list[0] );
  19076. // Empty purchase list
  19077. if( entries == 0 ){
  19078. return;
  19079. }
  19080. std::vector<s_barter_purchase> purchases;
  19081. purchases.reserve( entries );
  19082. // Make sure each shop index and target item id is only used once
  19083. for( int32 i = 0; i < entries; i++ ){
  19084. std::shared_ptr<s_npc_barter_item> item = util::map_find( barter->items, (uint16)p->list[i].shopIndex );
  19085. // Invalid shop index
  19086. if( item == nullptr ){
  19087. return;
  19088. }
  19089. for( int32 j = i + 1; j < entries; j++ ){
  19090. // Same shop index
  19091. if( p->list[i].shopIndex == p->list[j].shopIndex ){
  19092. return;
  19093. }
  19094. std::shared_ptr<s_npc_barter_item> item2 = util::map_find( barter->items, (uint16)p->list[j].shopIndex );
  19095. // Invalid shop index
  19096. if( item2 == nullptr ){
  19097. return;
  19098. }
  19099. // Same target item id
  19100. if( item->nameid == item2->nameid ){
  19101. return;
  19102. }
  19103. }
  19104. s_barter_purchase purchase = {};
  19105. purchase.item = item;
  19106. purchase.amount = p->list[i].amount;
  19107. purchases.push_back( purchase );
  19108. }
  19109. clif_npc_buy_result( sd, npc_barter_purchase( *sd, barter, purchases ) );
  19110. #endif
  19111. }
  19112. void clif_summon_init(struct mob_data& md) {
  19113. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19114. struct block_list* master_bl = battle_get_master(&md.bl);
  19115. if( master_bl == nullptr ){
  19116. return;
  19117. }
  19118. PACKET_ZC_SUMMON_HP_INIT p = {};
  19119. p.PacketType = HEADER_ZC_SUMMON_HP_INIT;
  19120. p.summonAID = md.bl.id;
  19121. p.CurrentHP = md.status.hp;
  19122. p.MaxHP = md.status.max_hp;
  19123. clif_send( &p, sizeof( p ), master_bl, SELF );
  19124. #endif
  19125. }
  19126. void clif_summon_hp_bar(struct mob_data& md) {
  19127. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19128. struct block_list* master_bl = battle_get_master(&md.bl);
  19129. if( master_bl == nullptr ){
  19130. return;
  19131. }
  19132. PACKET_ZC_SUMMON_HP_UPDATE p = {};
  19133. p.PacketType = HEADER_ZC_SUMMON_HP_UPDATE;
  19134. p.summonAID = md.bl.id;
  19135. p.VarId = SP_HP; // HP parameter
  19136. p.Value = md.status.hp;
  19137. clif_send( &p, sizeof( p ), master_bl, SELF );
  19138. #endif
  19139. }
  19140. void clif_laphine_synthesis_open( map_session_data *sd, std::shared_ptr<s_laphine_synthesis> synthesis ){
  19141. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19142. nullpo_retv( sd );
  19143. sd->state.laphine_synthesis = synthesis->item_id;
  19144. PACKET_ZC_RANDOM_COMBINE_ITEM_UI_OPEN p = {};
  19145. p.packetType = HEADER_ZC_RANDOM_COMBINE_ITEM_UI_OPEN;
  19146. p.itemId = client_nameid( synthesis->item_id );
  19147. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19148. #endif
  19149. }
  19150. void clif_parse_laphine_synthesis_close( int32 fd, map_session_data* sd ){
  19151. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19152. sd->state.laphine_synthesis = 0;
  19153. #endif
  19154. }
  19155. enum e_laphine_synthesis_result : int16{
  19156. LAPHINE_SYNTHESIS_SUCCESS = 0,
  19157. LAPHINE_SYNTHESIS_AMOUNT = 5,
  19158. LAPHINE_SYNTHESIS_ITEM = 7
  19159. };
  19160. void clif_laphine_synthesis_result( map_session_data* sd, enum e_laphine_synthesis_result result ){
  19161. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19162. nullpo_retv( sd );
  19163. PACKET_ZC_ACK_RANDOM_COMBINE_ITEM p = {};
  19164. p.packetType = HEADER_ZC_ACK_RANDOM_COMBINE_ITEM;
  19165. p.result = result;
  19166. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19167. #endif
  19168. }
  19169. void clif_parse_laphine_synthesis( int32 fd, map_session_data* sd ){
  19170. #if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
  19171. if( sd->state.laphine_synthesis == 0 ){
  19172. return;
  19173. }
  19174. const PACKET_CZ_REQ_RANDOM_COMBINE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_COMBINE_ITEM*>( RFIFOP( fd, 0 ) );
  19175. if( sd->state.laphine_synthesis != p->itemId ){
  19176. return;
  19177. }
  19178. std::shared_ptr<s_laphine_synthesis> synthesis = laphine_synthesis_db.find( sd->state.laphine_synthesis );
  19179. if( synthesis == nullptr ){
  19180. return;
  19181. }
  19182. size_t count = ( p->packetLength - sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM ) ) / sizeof( PACKET_CZ_REQ_RANDOM_COMBINE_ITEM_sub );
  19183. // Player sent more or less than actually required
  19184. if( count != synthesis->requiredRequirements ){
  19185. return;
  19186. }
  19187. // Check for duplicates
  19188. for( size_t i = 0; i < count; i++ ){
  19189. for( size_t j = i + 1; j < count; j++ ){
  19190. if( p->items[i].index == p->items[j].index ){
  19191. return;
  19192. }
  19193. }
  19194. }
  19195. for( size_t i = 0; i < count; i++ ){
  19196. int16 index = server_index( p->items[i].index );
  19197. if( index >= MAX_INVENTORY ){
  19198. return;
  19199. }
  19200. if( sd->inventory_data[index] == nullptr ){
  19201. return;
  19202. }
  19203. struct item* item = &sd->inventory.u.items_inventory[index];
  19204. std::shared_ptr<s_laphine_synthesis_requirement> requirement = util::umap_find( synthesis->requirements, item->nameid );
  19205. if( requirement == nullptr ){
  19206. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19207. return;
  19208. }
  19209. if( p->items[i].count != requirement->amount ){
  19210. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19211. return;
  19212. }
  19213. if( item->amount < requirement->amount ){
  19214. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_AMOUNT );
  19215. return;
  19216. }
  19217. if( item->refine < synthesis->minimumRefine ){
  19218. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19219. return;
  19220. }
  19221. if( item->refine > synthesis->maximumRefine ){
  19222. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19223. return;
  19224. }
  19225. if( item->equip != 0 ){
  19226. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19227. return;
  19228. }
  19229. if( item->equipSwitch != 0 ){
  19230. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19231. return;
  19232. }
  19233. }
  19234. // If triggered from item
  19235. if( sd->itemid == sd->state.laphine_synthesis ){
  19236. int16 index = pc_search_inventory( sd, sd->state.laphine_synthesis );
  19237. if( index < 0 ){
  19238. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_ITEM );
  19239. return;
  19240. }
  19241. if( ( sd->inventory_data[index]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19242. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19243. return;
  19244. }
  19245. }
  19246. }
  19247. for( size_t i = 0; i < count; i++ ){
  19248. int16 index = server_index( p->items[i].index );
  19249. if( pc_delitem( sd, index, p->items[i].count, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19250. return;
  19251. }
  19252. }
  19253. itemdb_group.pc_get_itemgroup( synthesis->rewardGroupId, true, *sd );
  19254. clif_laphine_synthesis_result( sd, LAPHINE_SYNTHESIS_SUCCESS );
  19255. #endif
  19256. }
  19257. void clif_laphine_upgrade_open( map_session_data* sd, std::shared_ptr<s_laphine_upgrade> upgrade ){
  19258. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19259. nullpo_retv( sd );
  19260. sd->state.laphine_upgrade = upgrade->item_id;
  19261. PACKET_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN p = {};
  19262. p.packetType = HEADER_ZC_RANDOM_UPGRADE_ITEM_UI_OPEN;
  19263. p.itemId = client_nameid( upgrade->item_id );
  19264. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19265. #endif
  19266. }
  19267. void clif_parse_laphine_upgrade_close( int32 fd, map_session_data* sd ){
  19268. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19269. sd->state.laphine_upgrade = 0;
  19270. #endif
  19271. }
  19272. void clif_laphine_upgrade_result( map_session_data *sd, bool failed ){
  19273. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19274. PACKET_ZC_ACK_RANDOM_UPGRADE_ITEM p = {};
  19275. p.packetType = HEADER_ZC_ACK_RANDOM_UPGRADE_ITEM;
  19276. p.result = failed;
  19277. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19278. #endif
  19279. }
  19280. static void clif_item_preview( map_session_data *sd, int16 index ){
  19281. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19282. nullpo_retv( sd );
  19283. struct item* item = &sd->inventory.u.items_inventory[index];
  19284. PACKET_ZC_CHANGE_ITEM_OPTION p = {};
  19285. p.packetType = HEADER_ZC_CHANGE_ITEM_OPTION;
  19286. p.index = client_index( index );
  19287. #if PACKETVER_MAIN_NUM >= 20181017 || PACKETVER_RE_NUM >= 20181017 || PACKETVER_ZERO_NUM >= 20181024
  19288. p.isDamaged = item->attribute != 0;
  19289. #endif
  19290. p.refiningLevel = item->refine;
  19291. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200723
  19292. p.grade = item->enchantgrade;
  19293. #endif
  19294. clif_addcards( &p.slot, item );
  19295. clif_add_random_options( p.option_data, *item );
  19296. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  19297. #endif
  19298. }
  19299. void clif_parse_laphine_upgrade( int32 fd, map_session_data* sd ){
  19300. #if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
  19301. if( sd->state.laphine_upgrade == 0 ){
  19302. return;
  19303. }
  19304. const PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM* p = reinterpret_cast<PACKET_CZ_REQ_RANDOM_UPGRADE_ITEM*>( RFIFOP( fd, 0 ) );
  19305. if( sd->state.laphine_upgrade != p->itemId ){
  19306. return;
  19307. }
  19308. std::shared_ptr<s_laphine_upgrade> upgrade = laphine_upgrade_db.find( sd->state.laphine_upgrade );
  19309. if( upgrade == nullptr ){
  19310. return;
  19311. }
  19312. uint16 index = server_index( p->index );
  19313. if( index >= MAX_INVENTORY ){
  19314. return;
  19315. }
  19316. if( sd->inventory_data[index] == nullptr ){
  19317. return;
  19318. }
  19319. struct item* item = &sd->inventory.u.items_inventory[index];
  19320. // Not a valid target item
  19321. if( !util::vector_exists( upgrade->target_item_ids, item->nameid ) ){
  19322. clif_laphine_upgrade_result( sd, true );
  19323. return;
  19324. }
  19325. // If target item is not identified
  19326. if( item->identify == 0 ){
  19327. clif_laphine_upgrade_result( sd, true );
  19328. return;
  19329. }
  19330. // If target item is equipped
  19331. if( item->equip != 0 ){
  19332. clif_laphine_upgrade_result( sd, true );
  19333. return;
  19334. }
  19335. // If target item is in equipswitch
  19336. if( item->equipSwitch != 0 ){
  19337. clif_laphine_upgrade_result( sd, true );
  19338. return;
  19339. }
  19340. // Check minimum refine requirement
  19341. if( item->refine < upgrade->minimumRefine ){
  19342. clif_laphine_upgrade_result( sd, true );
  19343. return;
  19344. }
  19345. // Check maximum refine requirement
  19346. if( item->refine > upgrade->maximumRefine ){
  19347. clif_laphine_upgrade_result( sd, true );
  19348. return;
  19349. }
  19350. // If no cards are allowed
  19351. if( !upgrade->cardsAllowed ){
  19352. for( int32 i = 0; i < MAX_SLOTS; i++ ){
  19353. if( item->card[i] != 0 ){
  19354. clif_laphine_upgrade_result( sd, true );
  19355. return;
  19356. }
  19357. }
  19358. }
  19359. // If random options are required
  19360. if( upgrade->requiredRandomOptions > 0 ){
  19361. int32 i;
  19362. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19363. if( item->option[i].id != 0 ){
  19364. break;
  19365. }
  19366. }
  19367. if( ( i + 1 ) < upgrade->requiredRandomOptions ){
  19368. clif_laphine_upgrade_result( sd, true );
  19369. return;
  19370. }
  19371. }
  19372. int16 index2 = pc_search_inventory( sd, sd->state.laphine_upgrade );
  19373. if( index2 < 0 ){
  19374. clif_laphine_upgrade_result( sd, true );
  19375. return;
  19376. }
  19377. if( ( sd->inventory_data[index2]->flag.delay_consume & DELAYCONSUME_NOCONSUME ) == 0 ){
  19378. if( pc_delitem( sd, index2, 1, 0, 0, LOG_TYPE_LAPHINE ) != 0 ){
  19379. return;
  19380. }
  19381. }
  19382. // Log removal of item
  19383. log_pick_pc( sd, LOG_TYPE_LAPHINE, -1, item );
  19384. // Visually remove it from the client
  19385. clif_delitem( *sd, index, 1, 0 );
  19386. // Apply the random options
  19387. if( upgrade->randomOptionGroup != nullptr ){
  19388. upgrade->randomOptionGroup->apply( *item );
  19389. }
  19390. // Change the refine rate if needed
  19391. if( upgrade->resultRefine > 0 ){
  19392. // Absolute refine level change
  19393. item->refine = max( item->refine, upgrade->resultRefine );
  19394. }else if( upgrade->resultRefineMaximum > 0 ){
  19395. // If a minimum is specified it can also downgrade
  19396. if( upgrade->resultRefineMinimum ){
  19397. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, upgrade->resultRefineMaximum ) );
  19398. }else{
  19399. // Otherwise it can only be upgraded until the maximum, but not downgraded
  19400. item->refine = static_cast<uint8>( rnd_value<uint16>( item->refine, upgrade->resultRefineMaximum ) );
  19401. }
  19402. }else if( upgrade->resultRefineMinimum > 0 ){
  19403. // No maximum has been specified, so it can be anything between minimum and MAX_REFINE
  19404. item->refine = static_cast<uint8>( rnd_value<uint16>( upgrade->resultRefineMinimum, MAX_REFINE ) );
  19405. }
  19406. // Log retrieving the item again -> with the new options
  19407. log_pick_pc( sd, LOG_TYPE_LAPHINE, 1, item );
  19408. // Make it visible for the client again
  19409. clif_additem( sd, index, 1, 0 );
  19410. // Open a preview of the item
  19411. clif_item_preview( sd, index );
  19412. // Tell the client we are done
  19413. clif_laphine_upgrade_result( sd, false );
  19414. #endif
  19415. }
  19416. void clif_enchantgrade_add( map_session_data& sd, uint16 index = UINT16_MAX, std::shared_ptr<s_enchantgradelevel> gradeLevel = nullptr ){
  19417. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19418. PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST* p = reinterpret_cast<PACKET_ZC_GRADE_ENCHANT_MATERIAL_LIST*>( packet_buffer );
  19419. p->PacketType = HEADER_ZC_GRADE_ENCHANT_MATERIAL_LIST;
  19420. p->PacketLength = sizeof( *p );
  19421. if( index < UINT16_MAX ){
  19422. p->index = client_index( index );
  19423. p->success_chance = gradeLevel->chances[sd.inventory.u.items_inventory[index].refine] / 100;
  19424. p->blessing_info.id = client_nameid( gradeLevel->catalyst.item );
  19425. p->blessing_info.amount = gradeLevel->catalyst.amountPerStep;
  19426. p->blessing_info.max_blessing = gradeLevel->catalyst.maximumSteps;
  19427. p->blessing_info.bonus = gradeLevel->catalyst.chanceIncrease / 100;
  19428. // Not displayed by client
  19429. p->protect_itemid = 0;
  19430. p->protect_amount = 0;
  19431. int32 i = 0;
  19432. for( const auto& pair : gradeLevel->options ){
  19433. GRADE_ENCHANT_MATERIAL& mat = p->material_info[i];
  19434. std::shared_ptr<s_enchantgradeoption> option = pair.second;
  19435. mat.nameid = client_nameid( option->item );
  19436. mat.amount = option->amount;
  19437. mat.price = option->zeny;
  19438. mat.downgrade = option->downgrade_amount > 0;
  19439. mat.breakable = option->breaking_rate > 0;
  19440. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( mat ) );
  19441. i++;
  19442. }
  19443. }else{
  19444. p->index = -1;
  19445. p->success_chance = 0;
  19446. p->blessing_info.id = 0;
  19447. p->blessing_info.amount = 0;
  19448. p->blessing_info.max_blessing = 0;
  19449. p->blessing_info.bonus = 0;
  19450. p->protect_itemid = 0;
  19451. p->protect_amount = 0;
  19452. }
  19453. clif_send( p, p->PacketLength, &sd.bl, SELF );
  19454. #endif
  19455. }
  19456. void clif_parse_enchantgrade_add( int32 fd, map_session_data* sd ){
  19457. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19458. nullpo_retv( sd );
  19459. if( !sd->state.enchantgrade_open ){
  19460. return;
  19461. }
  19462. const PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_SELECT_EQUIPMENT*>( RFIFOP( fd, 0 ) );
  19463. uint16 index = server_index( p->index );
  19464. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19465. return;
  19466. }
  19467. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19468. // Unsupported item type - no answer, because client should have actually prevented this request
  19469. if( enchantgrade == nullptr ){
  19470. return;
  19471. }
  19472. // Item can't be enhanced
  19473. if( !sd->inventory_data[index]->flag.gradable ){
  19474. return;
  19475. }
  19476. uint16 level = 0;
  19477. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19478. level = sd->inventory_data[index]->weapon_level;
  19479. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19480. level = sd->inventory_data[index]->armor_level;
  19481. }
  19482. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19483. // Cannot upgrade this weapon or armor level
  19484. if( enchantgradelevels == enchantgrade->levels.end() ){
  19485. clif_enchantgrade_add( *sd );
  19486. return;
  19487. }
  19488. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19489. // Cannot increase enchantgrade any further - no answer, because client should have actually prevented this request
  19490. if( enchantgradelevel == nullptr ){
  19491. return;
  19492. }
  19493. clif_enchantgrade_add( *sd, index, enchantgradelevel );
  19494. #endif
  19495. }
  19496. /// <summary>
  19497. /// Sends the result for trying to enchant an item
  19498. /// </summary>
  19499. /// <param name="sd">The player session</param>
  19500. /// <param name="index">The target item</param>
  19501. /// <param name="result">
  19502. /// 0= The grade has been successfully upgraded.
  19503. /// 1= Refinement failed.
  19504. /// 2= The refine level has decreased.
  19505. /// 3= Equipment destroyed.
  19506. /// 4= The equipment is protected.
  19507. /// </param>
  19508. void clif_enchantgrade_result( map_session_data& sd, uint16 index, e_enchantgrade_result result ){
  19509. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19510. PACKET_ZC_GRADE_ENCHANT_ACK p = {};
  19511. p.PacketType = HEADER_ZC_GRADE_ENCHANT_ACK;
  19512. p.index = client_index( index );
  19513. p.grade = sd.inventory.u.items_inventory[index].enchantgrade;
  19514. p.result = result;
  19515. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19516. #endif
  19517. }
  19518. void clif_enchantgrade_announce( map_session_data& sd, struct item& item, bool success ){
  19519. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19520. PACKET_ZC_GRADE_ENCHANT_BROADCAST_RESULT p = {};
  19521. p.packetType = HEADER_ZC_GRADE_ENCHANT_BROADCAST_RESULT;
  19522. safestrncpy( p.name, sd.status.name, sizeof( p.name ) );
  19523. p.itemId = client_nameid( item.nameid );
  19524. p.grade = item.enchantgrade;
  19525. p.status = success;
  19526. clif_send( &p, sizeof( p ), nullptr, ALL_CLIENT );
  19527. #endif
  19528. }
  19529. void clif_parse_enchantgrade_start( int32 fd, map_session_data* sd ){
  19530. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19531. nullpo_retv( sd );
  19532. if( !sd->state.enchantgrade_open ){
  19533. return;
  19534. }
  19535. const PACKET_CZ_GRADE_ENCHANT_REQUEST* p = reinterpret_cast<PACKET_CZ_GRADE_ENCHANT_REQUEST*>( RFIFOP( fd, 0 ) );
  19536. uint16 index = server_index( p->index );
  19537. if( index >= MAX_INVENTORY || sd->inventory_data[index] == nullptr ){
  19538. return;
  19539. }
  19540. std::shared_ptr<s_enchantgrade> enchantgrade = enchantgrade_db.find( sd->inventory_data[index]->type );
  19541. // Unsupported item type - no answer
  19542. if( enchantgrade == nullptr ){
  19543. return;
  19544. }
  19545. uint16 level = 0;
  19546. if( sd->inventory_data[index]->type == IT_WEAPON ){
  19547. level = sd->inventory_data[index]->weapon_level;
  19548. }else if( sd->inventory_data[index]->type == IT_ARMOR ){
  19549. level = sd->inventory_data[index]->armor_level;
  19550. }
  19551. const auto& enchantgradelevels = enchantgrade->levels.find( level );
  19552. // Cannot upgrade this weapon or armor level - no answer
  19553. if( enchantgradelevels == enchantgrade->levels.end() ){
  19554. return;
  19555. }
  19556. std::shared_ptr<s_enchantgradelevel> enchantgradelevel = util::map_find( enchantgradelevels->second, (e_enchantgrade)sd->inventory.u.items_inventory[index].enchantgrade );
  19557. // Cannot increase enchantgrade any further - no answer
  19558. if( enchantgradelevel == nullptr ){
  19559. return;
  19560. }
  19561. uint16 totalChance = enchantgradelevel->chances[sd->inventory.u.items_inventory[index].refine];
  19562. // No chance to increase the enchantgrade
  19563. if( totalChance == 0 ){
  19564. return;
  19565. }
  19566. std::shared_ptr<s_enchantgradeoption> option = util::map_find( enchantgradelevel->options, (uint16)p->material_index );
  19567. // Unknown option id - no answer
  19568. if( option == nullptr ){
  19569. return;
  19570. }
  19571. // Not enough zeny
  19572. if( sd->status.zeny < option->zeny ){
  19573. return;
  19574. }
  19575. uint16 steps = min( p->blessing_amount, enchantgradelevel->catalyst.maximumSteps );
  19576. std::unordered_map<uint16, uint16> requiredItems;
  19577. if( p->blessing_flag ){
  19578. // If the catalysator item is the same as the option item build the sum of amounts
  19579. if( enchantgradelevel->catalyst.item == option->item ){
  19580. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps + option->amount;
  19581. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19582. if( index < 0 ){
  19583. return;
  19584. }
  19585. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19586. return;
  19587. }
  19588. requiredItems[index] = amount;
  19589. }else{
  19590. uint16 amount = enchantgradelevel->catalyst.amountPerStep * steps;
  19591. // Check catalysator item
  19592. int16 index = pc_search_inventory( sd, enchantgradelevel->catalyst.item );
  19593. if( index < 0 ){
  19594. return;
  19595. }
  19596. if( sd->inventory.u.items_inventory[index].amount < amount ){
  19597. return;
  19598. }
  19599. requiredItems[index] = amount;
  19600. // Check option item
  19601. index = pc_search_inventory( sd, option->item );
  19602. if( index < 0 ){
  19603. return;
  19604. }
  19605. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19606. return;
  19607. }
  19608. requiredItems[index] = option->amount;
  19609. }
  19610. totalChance += steps * enchantgradelevel->catalyst.chanceIncrease;
  19611. }else{
  19612. // Check option item
  19613. int16 index = pc_search_inventory( sd, option->item );
  19614. if( index < 0 ){
  19615. return;
  19616. }
  19617. if( sd->inventory.u.items_inventory[index].amount < option->amount ){
  19618. return;
  19619. }
  19620. requiredItems[index] = option->amount;
  19621. }
  19622. // All items should be there, start deleting
  19623. for( const auto& pair : requiredItems ){
  19624. if( pc_delitem( sd, pair.first, pair.second, 0, 0, LOG_TYPE_ENCHANTGRADE ) != 0 ){
  19625. return;
  19626. }
  19627. }
  19628. if( pc_payzeny( sd, option->zeny, LOG_TYPE_ENCHANTGRADE ) > 0 ){
  19629. return;
  19630. }
  19631. if( rnd()%10000 < totalChance ){
  19632. // Log removal of item
  19633. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19634. // Increase enchantgrade
  19635. sd->inventory.u.items_inventory[index].enchantgrade = min( sd->inventory.u.items_inventory[index].enchantgrade + 1, MAX_ENCHANTGRADE );
  19636. // On successful enchantgrade increase the refine is reset
  19637. sd->inventory.u.items_inventory[index].refine = 0;
  19638. // Log retrieving the item again -> with the new refine and enchantgrade
  19639. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19640. // Show success
  19641. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_SUCCESS );
  19642. // Check if it has to be announced
  19643. if( enchantgradelevel->announceSuccess ){
  19644. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], true );
  19645. }
  19646. }else{
  19647. // Check if it has to be announced (has to be done before deleting the item from inventory)
  19648. if( enchantgradelevel->announceFail ){
  19649. clif_enchantgrade_announce( *sd, sd->inventory.u.items_inventory[index], false );
  19650. }
  19651. // Delete the item if it is breakable
  19652. if( option->breaking_rate > 0 && ( rnd() % 10000 ) < option->breaking_rate ){
  19653. // Delete the item
  19654. pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_ENCHANTGRADE );
  19655. // Show failure
  19656. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_BREAK );
  19657. // Downgrade the item if necessary
  19658. }else if( option->downgrade_amount > 0 ){
  19659. // Log removal of item
  19660. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, -1, &sd->inventory.u.items_inventory[index] );
  19661. // Decrease refine level
  19662. sd->inventory.u.items_inventory[index].refine = cap_value( sd->inventory.u.items_inventory[index].refine - option->downgrade_amount, 0, MAX_REFINE );
  19663. // Log retrieving the item again -> with the new refine
  19664. log_pick_pc( sd, LOG_TYPE_ENCHANTGRADE, 1, &sd->inventory.u.items_inventory[index] );
  19665. // Show downgrade
  19666. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_DOWNGRADE );
  19667. // Only show failure, but dont do anything
  19668. }else{
  19669. clif_enchantgrade_result( *sd, index, ENCHANTGRADE_UPGRADE_FAILED );
  19670. }
  19671. }
  19672. #endif
  19673. }
  19674. void clif_parse_enchantgrade_close( int32 fd, map_session_data* sd ){
  19675. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  19676. nullpo_retv( sd );
  19677. sd->state.enchantgrade_open = false;
  19678. #endif
  19679. }
  19680. void clif_reputation_type( map_session_data& sd, int64 type, int64 points ){
  19681. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19682. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19683. p->packetType = HEADER_ZC_REPUTE_INFO;
  19684. p->packetLength = sizeof( *p );
  19685. p->success = true;
  19686. PACKET_ZC_REPUTE_INFO_sub& entry = p->list[0];
  19687. entry.type = type;
  19688. entry.points = points;
  19689. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( entry ) );
  19690. clif_send( p, p->packetLength, &sd.bl, SELF );
  19691. #endif
  19692. }
  19693. void clif_reputation_list( map_session_data& sd ){
  19694. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19695. PACKET_ZC_REPUTE_INFO* p = reinterpret_cast<PACKET_ZC_REPUTE_INFO*>( packet_buffer );
  19696. p->packetType = HEADER_ZC_REPUTE_INFO;
  19697. p->packetLength = sizeof( *p );
  19698. p->success = true;
  19699. size_t index = 0;
  19700. for( const auto& entry : reputation_db ){
  19701. std::shared_ptr<s_reputation> reputation = entry.second;
  19702. PACKET_ZC_REPUTE_INFO_sub& list_entry = p->list[index];
  19703. list_entry.type = reputation->id;
  19704. list_entry.points = pc_readreg2( &sd, reputation->variable.c_str() );
  19705. p->packetLength += static_cast<decltype(p->packetLength)>( sizeof( list_entry ) );
  19706. index++;
  19707. }
  19708. clif_send( p, p->packetLength, &sd.bl, SELF );
  19709. #endif
  19710. }
  19711. void clif_item_reform_open( map_session_data& sd, t_itemid item ){
  19712. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19713. PACKET_ZC_OPEN_REFORM_UI p = {};
  19714. p.PacketType = HEADER_ZC_OPEN_REFORM_UI;
  19715. p.ITID = item;
  19716. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19717. sd.state.item_reform = item;
  19718. #endif
  19719. }
  19720. void clif_parse_item_reform_close( int32 fd, map_session_data* sd ){
  19721. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19722. sd->state.item_reform = 0;
  19723. #endif
  19724. }
  19725. void clif_item_reform_result( map_session_data& sd, uint16 index, uint8 result ){
  19726. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19727. PACKET_ZC_ITEM_REFORM_ACK p = {};
  19728. p.PacketType = HEADER_ZC_ITEM_REFORM_ACK;
  19729. p.index = client_index( index );
  19730. p.result = result;
  19731. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19732. if( result == 0 ){
  19733. // Client closes the window on success
  19734. sd.state.item_reform = 0;
  19735. }
  19736. #endif
  19737. }
  19738. void clif_parse_item_reform_start( int32 fd, map_session_data* sd ){
  19739. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19740. // Not opened
  19741. if( sd->state.item_reform == 0 ){
  19742. return;
  19743. }
  19744. const PACKET_CZ_ITEM_REFORM* p = reinterpret_cast<PACKET_CZ_ITEM_REFORM*>( RFIFOP( fd, 0 ) );
  19745. // Item mismatch
  19746. if( p->ITID != sd->state.item_reform ){
  19747. return;
  19748. }
  19749. uint16 index = server_index( p->index );
  19750. if( index >= MAX_INVENTORY ){
  19751. return;
  19752. }
  19753. if( sd->inventory_data[index] == nullptr ){
  19754. return;
  19755. }
  19756. std::shared_ptr<s_item_reform> reform = item_reform_db.find( sd->state.item_reform );
  19757. if( reform == nullptr ){
  19758. return;
  19759. }
  19760. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19761. std::shared_ptr<s_item_reform_base> base = util::umap_find( reform->base_items, selected_item.nameid );
  19762. if( base == nullptr ){
  19763. return;
  19764. }
  19765. // If target item is not identified
  19766. if( selected_item.identify == 0 ){
  19767. return;
  19768. }
  19769. // If target item is equipped
  19770. if( selected_item.equip != 0 ){
  19771. return;
  19772. }
  19773. // Check minimum refine requirement
  19774. if( selected_item.refine < base->minimumRefine ){
  19775. return;
  19776. }
  19777. // Check maximum refine requirement
  19778. if( selected_item.refine > base->maximumRefine ){
  19779. return;
  19780. }
  19781. // If no cards are allowed
  19782. if( !base->cardsAllowed ){
  19783. for( int32 i = 0; i < MAX_SLOTS; i++ ){
  19784. if( selected_item.card[i] != 0 ){
  19785. return;
  19786. }
  19787. }
  19788. }
  19789. // If random options are required
  19790. if( base->requiredRandomOptions > 0 ){
  19791. int32 i;
  19792. for( i = MAX_ITEM_RDM_OPT - 1; i >= 0; i-- ){
  19793. if( selected_item.option[i].id != 0 ){
  19794. break;
  19795. }
  19796. }
  19797. if( ( i + 1 ) < base->requiredRandomOptions ){
  19798. return;
  19799. }
  19800. }
  19801. std::unordered_map<uint16, uint16> materials;
  19802. // Check if all materials exist
  19803. for( const auto& material : base->materials ){
  19804. int16 material_index = pc_search_inventory( sd, material.first );
  19805. if( material_index < 0 ){
  19806. return;
  19807. }
  19808. if( sd->inventory.u.items_inventory[material_index].amount < material.second ){
  19809. return;
  19810. }
  19811. materials[material_index] = material.second;
  19812. }
  19813. // Remove the material
  19814. for( const auto& material : materials ){
  19815. if( pc_delitem( sd, material.first, material.second, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19816. return;
  19817. }
  19818. }
  19819. // If triggered from item
  19820. if( sd->itemid == sd->state.item_reform && pc_delitem( sd, sd->itemindex, 1, 0, 0, LOG_TYPE_REFORM ) != 0 ){
  19821. return;
  19822. }
  19823. // Log removal of item
  19824. log_pick_pc( sd, LOG_TYPE_REFORM, -1, &selected_item );
  19825. // Visually remove it from the client
  19826. clif_delitem( *sd, index, 1, 0 );
  19827. // Apply the random options
  19828. if( base->randomOptionGroup != nullptr ){
  19829. base->randomOptionGroup->apply( selected_item );
  19830. }
  19831. // Change the refine rate if needed
  19832. if( base->refineChange != 0 ){
  19833. selected_item.refine = cap_value( selected_item.refine + base->refineChange, 0, MAX_REFINE );
  19834. }
  19835. // Remove all cards and socket enchants
  19836. if( base->clearSlots ){
  19837. for( int32 i = 0; i < MAX_SLOTS; i++ ){
  19838. selected_item.card[i] = 0;
  19839. }
  19840. }
  19841. // Remove the current enchantgrade
  19842. if( base->removeEnchantgrade ){
  19843. selected_item.enchantgrade = 0;
  19844. }
  19845. // Finally change the item id
  19846. selected_item.nameid = base->resultItemId;
  19847. // Link inventory data cache to the new item
  19848. sd->inventory_data[index] = itemdb_search( base->resultItemId );
  19849. // Log retrieving the item again -> with the new options
  19850. log_pick_pc( sd, LOG_TYPE_REFORM, 1, &selected_item );
  19851. // Make it visible for the client again
  19852. clif_additem( sd, index, 1, 0 );
  19853. clif_item_reform_result( *sd, index, 0 );
  19854. #endif
  19855. }
  19856. void clif_enchantwindow_open( map_session_data& sd, uint64 clientLuaIndex ){
  19857. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19858. // Hardcoded clientside check
  19859. if( sd.weight > ( ( sd.max_weight * 70 ) / 100 ) ){
  19860. clif_msg_color( &sd, MSI_ENCHANT_FAILED_OVER_WEIGHT, color_table[COLOR_RED] );
  19861. sd.state.item_enchant_index = 0;
  19862. return;
  19863. }
  19864. PACKET_ZC_UI_OPEN_V3 p = {};
  19865. p.packetType = HEADER_ZC_UI_OPEN_V3;
  19866. p.type = OUT_UI_ENCHANT;
  19867. p.data = clientLuaIndex;
  19868. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19869. sd.state.item_enchant_index = clientLuaIndex;
  19870. #endif
  19871. }
  19872. void clif_enchantwindow_result( map_session_data& sd, bool success, t_itemid enchant = 0 ){
  19873. #if PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330
  19874. PACKET_ZC_RESPONSE_ENCHANT p = {};
  19875. p.PacketType = HEADER_ZC_RESPONSE_ENCHANT;
  19876. if( success ){
  19877. p.msgId = MSI_ENCHANT_SUCCESS;
  19878. }else{
  19879. p.msgId = MSI_ENCHANT_FAILED;
  19880. }
  19881. p.ITID = enchant;
  19882. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  19883. sd.state.item_enchant_index = 0;
  19884. #endif
  19885. }
  19886. bool clif_parse_enchant_basecheck( struct item& selected_item, std::shared_ptr<s_item_enchant> enchant ){
  19887. if( selected_item.equip != 0 ){
  19888. return false;
  19889. }
  19890. if( selected_item.equipSwitch != 0 ){
  19891. return false;
  19892. }
  19893. if( selected_item.attribute != 0 ){
  19894. return false;
  19895. }
  19896. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  19897. return false;
  19898. }
  19899. if( selected_item.refine < enchant->minimumRefine ){
  19900. return false;
  19901. }
  19902. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  19903. return false;
  19904. }
  19905. if( !enchant->allowRandomOptions ){
  19906. for( const s_item_randomoption& option : selected_item.option ){
  19907. if( option.id != 0 ){
  19908. return false;
  19909. }
  19910. }
  19911. }
  19912. return true;
  19913. }
  19914. void clif_parse_enchantwindow_general( int32 fd, map_session_data* sd ){
  19915. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  19916. const PACKET_CZ_REQUEST_RANDOM_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RANDOM_ENCHANT*>( RFIFOP( fd, 0 ) );
  19917. if( sd->state.item_enchant_index != p->enchant_group ){
  19918. return;
  19919. }
  19920. uint16 index = server_index( p->index );
  19921. if( index >= MAX_INVENTORY ){
  19922. return;
  19923. }
  19924. if( sd->inventory_data[index] == nullptr ){
  19925. return;
  19926. }
  19927. struct item& selected_item = sd->inventory.u.items_inventory[index];
  19928. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  19929. if( enchant == nullptr ){
  19930. return;
  19931. }
  19932. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  19933. return;
  19934. }
  19935. uint16 slot = MAX_SLOTS;
  19936. for( uint16 nextSlot : enchant->order ){
  19937. if( selected_item.card[nextSlot] == 0 ){
  19938. slot = nextSlot;
  19939. break;
  19940. }
  19941. }
  19942. if( slot == MAX_SLOTS ){
  19943. return;
  19944. }
  19945. if( slot < sd->inventory_data[index]->slots ){
  19946. return;
  19947. }
  19948. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  19949. if( enchant_slot == nullptr ){
  19950. return;
  19951. }
  19952. std::shared_ptr<s_item_enchant_normal> enchants_for_enchantgrade = util::umap_find( enchant_slot->normal.enchants, (uint16)selected_item.enchantgrade );
  19953. if( enchants_for_enchantgrade == nullptr ){
  19954. clif_messagecolor( &sd->bl, color_table[COLOR_RED], msg_txt( sd, 829 ), false, SELF); // Enchanting is not possible for your item's enchant grade.
  19955. clif_enchantwindow_result( *sd, false );
  19956. return;
  19957. }
  19958. if( sd->status.zeny < enchant_slot->normal.zeny ){
  19959. return;
  19960. }
  19961. std::unordered_map<uint16, uint16> materials;
  19962. for( const auto& entry : enchant_slot->normal.materials ){
  19963. int16 idx = pc_search_inventory( sd, entry.first );
  19964. if( idx < 0 ){
  19965. return;
  19966. }
  19967. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  19968. return;
  19969. }
  19970. materials[idx] = entry.second;
  19971. }
  19972. if( pc_payzeny( sd, enchant_slot->normal.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  19973. return;
  19974. }
  19975. for( const auto& entry : materials ){
  19976. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  19977. return;
  19978. }
  19979. }
  19980. uint32 chance = enchant_slot->normal.chance;
  19981. for( int32 i = 0; i <= MAX_ENCHANTGRADE; i++ ){
  19982. chance += enchant_slot->normal.enchantgradeChanceIncrease[i];
  19983. }
  19984. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  19985. clif_enchantwindow_result( *sd, false );
  19986. return;
  19987. }
  19988. // Log removal of item
  19989. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  19990. size_t maximum = 3 * enchant_slot->normal.enchants.size();
  19991. bool enchanted = false;
  19992. for( int32 i = 0; i < maximum; i++ ){
  19993. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  19994. if( rnd_value( 0, 10000 ) < normal_enchant->chance ){
  19995. selected_item.card[slot] = normal_enchant->item_id;
  19996. enchanted = true;
  19997. break;
  19998. }
  19999. }
  20000. if( !enchanted ){
  20001. std::shared_ptr<s_item_enchant_normal_sub> normal_enchant = util::umap_random( enchants_for_enchantgrade->enchants );
  20002. selected_item.card[slot] = normal_enchant->item_id;
  20003. }
  20004. // Log retrieving the item again -> with the new enchant
  20005. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20006. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  20007. #endif
  20008. }
  20009. void clif_parse_enchantwindow_perfect( int32 fd, map_session_data* sd ){
  20010. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20011. const PACKET_CZ_REQUEST_PERFECT_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_PERFECT_ENCHANT*>( RFIFOP( fd, 0 ) );
  20012. if( sd->state.item_enchant_index != p->enchant_group ){
  20013. return;
  20014. }
  20015. uint16 index = server_index( p->index );
  20016. if( index >= MAX_INVENTORY ){
  20017. return;
  20018. }
  20019. if( sd->inventory_data[index] == nullptr ){
  20020. return;
  20021. }
  20022. struct item& selected_item = sd->inventory.u.items_inventory[index];
  20023. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  20024. if( enchant == nullptr ){
  20025. return;
  20026. }
  20027. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  20028. return;
  20029. }
  20030. uint16 slot = MAX_SLOTS;
  20031. for( uint16 nextSlot : enchant->order ){
  20032. if( selected_item.card[nextSlot] == 0 ){
  20033. slot = nextSlot;
  20034. break;
  20035. }
  20036. }
  20037. if( slot == MAX_SLOTS ){
  20038. return;
  20039. }
  20040. if( slot < sd->inventory_data[index]->slots ){
  20041. return;
  20042. }
  20043. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  20044. if( enchant_slot == nullptr ){
  20045. return;
  20046. }
  20047. std::shared_ptr<s_item_enchant_perfect> perfect_enchant = util::umap_find( enchant_slot->perfect.enchants, p->ITID );
  20048. if( perfect_enchant == nullptr ){
  20049. return;
  20050. }
  20051. if( sd->status.zeny < perfect_enchant->zeny ){
  20052. return;
  20053. }
  20054. std::unordered_map<uint16, uint16> materials;
  20055. for( const auto& entry : perfect_enchant->materials ){
  20056. int16 idx = pc_search_inventory( sd, entry.first );
  20057. if( idx < 0 ){
  20058. return;
  20059. }
  20060. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20061. return;
  20062. }
  20063. materials[idx] = entry.second;
  20064. }
  20065. if( pc_payzeny( sd, perfect_enchant->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20066. return;
  20067. }
  20068. for( const auto& entry : materials ){
  20069. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20070. return;
  20071. }
  20072. }
  20073. // Log removal of item
  20074. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20075. selected_item.card[slot] = perfect_enchant->item_id;
  20076. // Log retrieving the item again -> with the new enchant
  20077. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20078. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  20079. #endif
  20080. }
  20081. void clif_parse_enchantwindow_upgrade( int32 fd, map_session_data* sd ){
  20082. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20083. const PACKET_CZ_REQUEST_UPGRADE_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_UPGRADE_ENCHANT*>( RFIFOP( fd, 0 ) );
  20084. if( sd->state.item_enchant_index != p->enchant_group ){
  20085. return;
  20086. }
  20087. uint16 index = server_index( p->index );
  20088. if( index >= MAX_INVENTORY ){
  20089. return;
  20090. }
  20091. if( sd->inventory_data[index] == nullptr ){
  20092. return;
  20093. }
  20094. struct item& selected_item = sd->inventory.u.items_inventory[index];
  20095. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  20096. if( enchant == nullptr ){
  20097. return;
  20098. }
  20099. if( !clif_parse_enchant_basecheck( selected_item, enchant ) ){
  20100. return;
  20101. }
  20102. uint16 slot = p->slot;
  20103. if( slot >= MAX_SLOTS ){
  20104. return;
  20105. }
  20106. if( slot < sd->inventory_data[index]->slots ){
  20107. return;
  20108. }
  20109. if( selected_item.card[slot] == 0 ){
  20110. return;
  20111. }
  20112. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  20113. if( enchant_slot == nullptr ){
  20114. return;
  20115. }
  20116. std::shared_ptr<s_item_enchant_upgrade> upgrade = util::umap_find( enchant_slot->upgrade.enchants, selected_item.card[slot] );
  20117. if( upgrade == nullptr ){
  20118. return;
  20119. }
  20120. if( sd->status.zeny < upgrade->zeny ){
  20121. return;
  20122. }
  20123. std::unordered_map<uint16, uint16> materials;
  20124. for( const auto& entry : upgrade->materials ){
  20125. int16 idx = pc_search_inventory( sd, entry.first );
  20126. if( idx < 0 ){
  20127. return;
  20128. }
  20129. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20130. return;
  20131. }
  20132. materials[idx] = entry.second;
  20133. }
  20134. if( pc_payzeny( sd, upgrade->zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20135. return;
  20136. }
  20137. for( const auto& entry : materials ){
  20138. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20139. return;
  20140. }
  20141. }
  20142. // Log removal of item
  20143. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20144. selected_item.card[slot] = upgrade->upgrade_item_id;
  20145. // Log retrieving the item again -> with the new enchant
  20146. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20147. clif_enchantwindow_result( *sd, true, selected_item.card[slot] );
  20148. #endif
  20149. }
  20150. void clif_parse_enchantwindow_reset( int32 fd, map_session_data* sd ){
  20151. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20152. const PACKET_CZ_REQUEST_RESET_ENCHANT* p = reinterpret_cast<PACKET_CZ_REQUEST_RESET_ENCHANT*>( RFIFOP( fd, 0 ) );
  20153. if( sd->state.item_enchant_index != p->enchant_group ){
  20154. return;
  20155. }
  20156. uint16 index = server_index( p->index );
  20157. if( index >= MAX_INVENTORY ){
  20158. return;
  20159. }
  20160. if( sd->inventory_data[index] == nullptr ){
  20161. return;
  20162. }
  20163. struct item& selected_item = sd->inventory.u.items_inventory[index];
  20164. if( selected_item.equip != 0 ){
  20165. return;
  20166. }
  20167. if( selected_item.equipSwitch != 0 ){
  20168. return;
  20169. }
  20170. if( selected_item.attribute != 0 ){
  20171. return;
  20172. }
  20173. std::shared_ptr<s_item_enchant> enchant = item_enchant_db.find( p->enchant_group );
  20174. if( enchant == nullptr ){
  20175. return;
  20176. }
  20177. if( !util::vector_exists( enchant->target_item_ids, selected_item.nameid ) ){
  20178. return;
  20179. }
  20180. if( selected_item.refine < enchant->minimumRefine ){
  20181. return;
  20182. }
  20183. if( selected_item.enchantgrade < enchant->minimumEnchantgrade ){
  20184. return;
  20185. }
  20186. bool is_enchanted = false;
  20187. for( int32 i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20188. if( selected_item.card[i] != 0 ){
  20189. is_enchanted = true;
  20190. break;
  20191. }
  20192. }
  20193. if( !is_enchanted ){
  20194. return;
  20195. }
  20196. if( sd->status.zeny < enchant->reset.zeny ){
  20197. return;
  20198. }
  20199. std::unordered_map<uint16, uint16> materials;
  20200. for( const auto& entry : enchant->reset.materials ){
  20201. int16 idx = pc_search_inventory( sd, entry.first );
  20202. if( idx < 0 ){
  20203. return;
  20204. }
  20205. if( sd->inventory.u.items_inventory[idx].amount < entry.second ){
  20206. return;
  20207. }
  20208. materials[idx] = entry.second;
  20209. }
  20210. if( pc_payzeny( sd, enchant->reset.zeny, LOG_TYPE_ENCHANT ) != 0 ){
  20211. return;
  20212. }
  20213. for( const auto& entry : materials ){
  20214. if( pc_delitem( sd, entry.first, entry.second, 0, 0, LOG_TYPE_ENCHANT ) != 0 ){
  20215. return;
  20216. }
  20217. }
  20218. uint32 chance = enchant->reset.chance;
  20219. if( chance == 0 ){
  20220. return;
  20221. }
  20222. if( chance < 100000 && rnd_value( 0, 100000 ) > chance ){
  20223. clif_enchantwindow_result( *sd, false );
  20224. return;
  20225. }
  20226. // Log removal of item
  20227. log_pick_pc( sd, LOG_TYPE_ENCHANT, -1, &selected_item );
  20228. for( int32 i = sd->inventory_data[index]->slots; i < MAX_SLOTS; i++ ){
  20229. selected_item.card[i] = 0;
  20230. }
  20231. // Log retrieving the item again -> with the new enchant
  20232. log_pick_pc( sd, LOG_TYPE_ENCHANT, 1, &selected_item );
  20233. clif_enchantwindow_result( *sd, true );
  20234. #endif
  20235. }
  20236. void clif_parse_enchantwindow_close( int32 fd, map_session_data* sd ){
  20237. #if PACKETVER_MAIN_NUM >= 20201118 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20238. sd->state.item_enchant_index = 0;
  20239. #endif
  20240. }
  20241. void clif_parse_itempackage_select( int32 fd, map_session_data* sd ){
  20242. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220316
  20243. const PACKET_CZ_USE_PACKAGEITEM* p = reinterpret_cast<PACKET_CZ_USE_PACKAGEITEM*>( RFIFOP( fd, 0 ) );
  20244. if( p->AID != sd->status.account_id ){
  20245. return;
  20246. }
  20247. uint16 index = server_index( p->index );
  20248. if( index >= MAX_INVENTORY ){
  20249. return;
  20250. }
  20251. if( sd->inventory_data[index] == nullptr ){
  20252. return;
  20253. }
  20254. if( sd->inventory_data[index]->nameid != p->itemID ){
  20255. return;
  20256. }
  20257. if( sd->inventory_data[index]->elv > sd->status.base_level ){
  20258. return;
  20259. }
  20260. std::shared_ptr<s_item_package> package = item_package_db.find( p->itemID );
  20261. if( package == nullptr ){
  20262. return;
  20263. }
  20264. std::shared_ptr<s_item_package_group> group = util::umap_find( package->groups, p->BoxIndex );
  20265. if( group == nullptr ){
  20266. return;
  20267. }
  20268. if( pc_delitem( sd, index, 1, 0, 0, LOG_TYPE_PACKAGE ) != 0 ){
  20269. return;
  20270. }
  20271. for( const auto& entry : group->items ){
  20272. struct item item = {};
  20273. item.nameid = entry.second->item_id;
  20274. item.identify = 1;
  20275. item.refine = (char)entry.second->refine;
  20276. if( entry.second->rentalhours ){
  20277. item.expire_time = (uint32)( time( nullptr ) + entry.second->rentalhours * 3600 );
  20278. }
  20279. // Check if it is a pet egg
  20280. std::shared_ptr<s_pet_db> pet = pet_db_search( item.nameid, PET_EGG );
  20281. if( pet != nullptr ){
  20282. for( int32 i = 0; i < entry.second->amount; i++ ){
  20283. pet_create_egg( sd, item.nameid );
  20284. }
  20285. }else if( entry.second->amount > 1 && ( !itemdb_isstackable( item.nameid ) || item.expire_time > 0 ) ){
  20286. for( int32 i = 0; i < entry.second->amount; i++ ){
  20287. // New random options on each iteration
  20288. if( entry.second->randomOptionGroup != nullptr ){
  20289. entry.second->randomOptionGroup->apply( item );
  20290. }
  20291. pc_additem( sd, &item, 1, LOG_TYPE_PACKAGE );
  20292. }
  20293. }else{
  20294. if( entry.second->randomOptionGroup != nullptr ){
  20295. entry.second->randomOptionGroup->apply( item );
  20296. }
  20297. pc_additem( sd, &item, entry.second->amount, LOG_TYPE_PACKAGE );
  20298. }
  20299. }
  20300. #endif
  20301. }
  20302. void clif_broadcast_refine_result(map_session_data& sd, t_itemid itemId, int8 level, bool success)
  20303. {
  20304. #if PACKETVER_MAIN_NUM >= 20170906 || PACKETVER_RE_NUM >= 20170830 || defined(PACKETVER_ZERO)
  20305. PACKET_ZC_BROADCAST_ITEMREFINING_RESULT p{};
  20306. p.packetType = HEADER_ZC_BROADCAST_ITEMREFINING_RESULT;
  20307. p.itemId = itemId;
  20308. p.refine_level = level;
  20309. p.status = (int8)success;
  20310. if (battle_config.broadcast_hide_name) {
  20311. std::string dispname = clif_hide_name(sd.status.name);
  20312. safestrncpy(p.name, dispname.c_str(), sizeof(p.name));
  20313. }
  20314. else {
  20315. safestrncpy(p.name, sd.status.name, sizeof(p.name));
  20316. }
  20317. clif_send(&p, sizeof(p), &sd.bl, ALL_CLIENT);
  20318. #endif
  20319. }
  20320. void clif_parse_captcha_register(int32 fd, map_session_data *sd) {
  20321. #if PACKETVER >= 20160316
  20322. nullpo_retv(sd);
  20323. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20324. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20325. return;
  20326. }
  20327. const PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_UPLOAD_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20328. pc_macro_captcha_register(*sd, p->imageSize, p->answer);
  20329. #endif
  20330. }
  20331. void clif_captcha_upload_request(map_session_data &sd) {
  20332. #if PACKETVER >= 20160330
  20333. PACKET_ZC_ACK_UPLOAD_MACRO_DETECTOR p = {};
  20334. p.PacketType = HEADER_ZC_ACK_UPLOAD_MACRO_DETECTOR;
  20335. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20336. if (sd.captcha_upload.cd != nullptr) {
  20337. p.captchaFlag = 0;
  20338. } else {
  20339. p.captchaFlag = 1;
  20340. }
  20341. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20342. #endif
  20343. }
  20344. void clif_parse_captcha_upload(int32 fd, map_session_data *sd) {
  20345. #if PACKETVER >= 20160316
  20346. nullpo_retv(sd);
  20347. if (!pc_has_permission(sd, PC_PERM_MACRO_REGISTER)) {
  20348. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20349. return;
  20350. }
  20351. const PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_UPLOAD_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20352. int16 upload_size = p->PacketLength - sizeof( * p );
  20353. if (upload_size < 1 || upload_size > MAX_CAPTCHA_CHUNK_SIZE)
  20354. return;
  20355. if (sd->captcha_upload.upload_size + upload_size > sd->captcha_upload.cd->image_size)
  20356. return;
  20357. pc_macro_captcha_register_upload(*sd, upload_size, p->imageData);
  20358. #endif
  20359. }
  20360. void clif_captcha_upload_end(map_session_data &sd) {
  20361. #if PACKETVER >= 20160330
  20362. PACKET_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA p = {};
  20363. p.PacketType = HEADER_ZC_COMPLETE_UPLOAD_MACRO_DETECTOR_CAPTCHA;
  20364. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20365. #endif
  20366. }
  20367. void clif_parse_captcha_preview_request(int32 fd, map_session_data *sd) {
  20368. #if PACKETVER >= 20160323
  20369. nullpo_retv(sd);
  20370. if (!(pc_has_permission(sd, PC_PERM_MACRO_REGISTER) && pc_has_permission(sd, PC_PERM_MACRO_DETECT))) {
  20371. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20372. return;
  20373. }
  20374. const PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_PREVIEW_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20375. clif_captcha_preview_response(*sd, captcha_db.find(p->captchaID));
  20376. #endif
  20377. }
  20378. void clif_captcha_preview_response(map_session_data &sd, std::shared_ptr<s_captcha_data> cd) {
  20379. #if PACKETVER >= 20160330
  20380. PACKET_ZC_ACK_PREVIEW_MACRO_DETECTOR p = {};
  20381. p.PacketType = HEADER_ZC_ACK_PREVIEW_MACRO_DETECTOR;
  20382. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20383. if (cd == nullptr) {
  20384. p.captchaFlag = 1;
  20385. p.imageSize = 0;
  20386. } else {
  20387. p.captchaFlag = 0;
  20388. p.imageSize = cd->image_size;
  20389. }
  20390. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20391. if (cd != nullptr) {
  20392. for (uint16 offset = 0; offset < cd->image_size;) {
  20393. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20394. PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20395. p2->PacketType = HEADER_ZC_PREVIEW_MACRO_DETECTOR_CAPTCHA;
  20396. p2->PacketLength = sizeof( *p2 );
  20397. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20398. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20399. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20400. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20401. offset += chunk_size;
  20402. }
  20403. }
  20404. #endif
  20405. }
  20406. void clif_macro_detector_request(map_session_data &sd) {
  20407. #if PACKETVER >= 20160330
  20408. std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  20409. if (cd == nullptr) {
  20410. return;
  20411. }
  20412. // Send preview initialization request to the client.
  20413. PACKET_ZC_APPLY_MACRO_DETECTOR p = {};
  20414. p.PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR;
  20415. p.imageSize = cd->image_size;
  20416. safestrncpy(p.captchaKey, "", sizeof(p.captchaKey));
  20417. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20418. for (uint16 offset = 0; offset < cd->image_size;) {
  20419. uint16 chunk_size = min(cd->image_size - offset, MAX_CAPTCHA_CHUNK_SIZE);
  20420. PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA* p2 = reinterpret_cast<PACKET_ZC_APPLY_MACRO_DETECTOR_CAPTCHA*>( packet_buffer );
  20421. p2->PacketType = HEADER_ZC_APPLY_MACRO_DETECTOR_CAPTCHA;
  20422. p2->PacketLength = sizeof( *p2 );
  20423. safestrncpy(p2->captchaKey, p.captchaKey, sizeof(p2->captchaKey));
  20424. memcpy(p2->imageData, &cd->image_data[offset], chunk_size);
  20425. p2->PacketLength += static_cast<decltype(p2->PacketLength)>( chunk_size );
  20426. clif_send(p2, p2->PacketLength, &sd.bl, SELF);
  20427. offset += chunk_size;
  20428. }
  20429. #endif
  20430. }
  20431. void clif_macro_detector_request_show(map_session_data &sd) {
  20432. #if PACKETVER >= 20160330
  20433. PACKET_ZC_REQ_ANSWER_MACRO_DETECTOR p = {};
  20434. p.PacketType = HEADER_ZC_REQ_ANSWER_MACRO_DETECTOR;
  20435. p.retryCount = sd.macro_detect.retry;
  20436. p.timeout = battle_config.macro_detection_timeout;
  20437. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20438. #endif
  20439. }
  20440. void clif_parse_macro_detector_download_ack(int32 fd, map_session_data *sd) {
  20441. #if PACKETVER >= 20160316
  20442. nullpo_retv(sd);
  20443. if (sd->macro_detect.retry != 0) {
  20444. //const PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA* p = reinterpret_cast<PACKET_CZ_COMPLETE_APPLY_MACRO_DETECTOR_CAPTCHA*>( RFIFOP( fd, 0 ) );
  20445. clif_macro_detector_request_show(*sd);
  20446. }
  20447. #endif
  20448. }
  20449. void clif_parse_macro_detector_answer(int32 fd, map_session_data *sd) {
  20450. #if PACKETVER >= 20160316
  20451. nullpo_retv(sd);
  20452. const PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_ACK_ANSWER_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20453. pc_macro_detector_process_answer(*sd, p->answer);
  20454. #endif
  20455. }
  20456. void clif_macro_detector_status(map_session_data &sd, e_macro_detect_status stype) {
  20457. #if PACKETVER >= 20160330
  20458. PACKET_ZC_CLOSE_MACRO_DETECTOR p = {};
  20459. p.PacketType = HEADER_ZC_CLOSE_MACRO_DETECTOR;
  20460. p.status = stype;
  20461. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20462. #endif
  20463. }
  20464. void clif_parse_macro_reporter_select(int32 fd, map_session_data *sd) {
  20465. #if PACKETVER >= 20160330
  20466. nullpo_retv(sd);
  20467. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20468. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20469. return;
  20470. }
  20471. const PACKET_CZ_REQ_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_CZ_REQ_PLAYER_AID_IN_RANGE*>( RFIFOP( fd, 0 ) );
  20472. pc_macro_reporter_area_select(*sd, p->xPos, p->yPos, p->RadiusRange);
  20473. #endif
  20474. }
  20475. void clif_macro_reporter_select(map_session_data &sd, const std::vector<uint32> &aid_list) {
  20476. #if PACKETVER >= 20160330
  20477. PACKET_ZC_ACK_PLAYER_AID_IN_RANGE* p = reinterpret_cast<PACKET_ZC_ACK_PLAYER_AID_IN_RANGE*>( packet_buffer );
  20478. p->PacketType = HEADER_ZC_ACK_PLAYER_AID_IN_RANGE;
  20479. p->PacketLength = sizeof( *p );
  20480. for (size_t i = 0; i < aid_list.size(); i++){
  20481. p->AID[i] = aid_list[static_cast<int32>(i)];
  20482. p->PacketLength += static_cast<decltype(p->PacketLength)>( sizeof( p->AID[0] ) );
  20483. }
  20484. clif_send(p, p->PacketLength, &sd.bl, SELF);
  20485. #endif
  20486. }
  20487. void clif_parse_macro_reporter_ack(int32 fd, map_session_data *sd) {
  20488. #if PACKETVER >= 20160316
  20489. nullpo_retv(sd);
  20490. if (!pc_has_permission(sd, PC_PERM_MACRO_DETECT)) {
  20491. clif_displaymessage(sd->fd, msg_txt(sd, 246)); // Your GM level doesn't authorize you to perform this action.
  20492. return;
  20493. }
  20494. const PACKET_CZ_REQ_APPLY_MACRO_DETECTOR* p = reinterpret_cast<PACKET_CZ_REQ_APPLY_MACRO_DETECTOR*>( RFIFOP( fd, 0 ) );
  20495. map_session_data *tsd = map_id2sd(p->AID);
  20496. if (tsd == nullptr) {
  20497. clif_displaymessage(fd, msg_txt(sd, 3)); // Character not found.
  20498. return;
  20499. }
  20500. if (tsd->macro_detect.retry != 0) {
  20501. clif_macro_reporter_status(*sd, MCR_INPROGRESS);
  20502. return;
  20503. }
  20504. if (captcha_db.empty()) {
  20505. clif_macro_reporter_status(*sd, MCR_NO_DATA);
  20506. return;
  20507. }
  20508. pc_macro_reporter_process(*tsd, sd->status.account_id);
  20509. clif_macro_reporter_status(*sd, MCR_MONITORING);
  20510. #endif
  20511. }
  20512. void clif_macro_reporter_status(map_session_data &sd, e_macro_report_status stype) {
  20513. #if PACKETVER >= 20160330
  20514. PACKET_ZC_ACK_APPLY_MACRO_DETECTOR p = {};
  20515. p.PacketType = HEADER_ZC_ACK_APPLY_MACRO_DETECTOR;
  20516. p.status = stype;
  20517. clif_send(&p, sizeof(p), &sd.bl, SELF);
  20518. #endif
  20519. }
  20520. void clif_dynamicnpc_result( map_session_data& sd, e_dynamicnpc_result result ){
  20521. #if PACKETVER_MAIN_NUM >= 20140430 || PACKETVER_RE_NUM >= 20140430 || defined(PACKETVER_ZERO)
  20522. PACKET_ZC_DYNAMICNPC_CREATE_RESULT p = {};
  20523. p.PacketType = HEADER_ZC_DYNAMICNPC_CREATE_RESULT;
  20524. p.result = result;
  20525. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20526. #endif
  20527. }
  20528. void clif_partybooking_ask( map_session_data* sd, map_session_data* joining_sd ){
  20529. #if PACKETVER >= 20191204
  20530. PACKET_ZC_PARTY_REQ_MASTER_TO_JOIN p = {};
  20531. p.packetType = HEADER_ZC_PARTY_REQ_MASTER_TO_JOIN;
  20532. p.CID = joining_sd->status.char_id;
  20533. p.AID = joining_sd->status.account_id;
  20534. safestrncpy( p.name, joining_sd->status.name, NAME_LENGTH );
  20535. p.x = joining_sd->status.base_level;
  20536. p.y = joining_sd->status.class_;
  20537. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20538. #endif
  20539. }
  20540. void clif_parse_partybooking_join( int32 fd, map_session_data* sd ){
  20541. #if PACKETVER >= 20191204
  20542. const PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20543. // Character is already in a party
  20544. if( sd->status.party_id != 0 ){
  20545. return;
  20546. }
  20547. map_session_data* tsd = map_charid2sd( p->CID );
  20548. // Target player is offline
  20549. if( tsd == nullptr ){
  20550. return;
  20551. }
  20552. if( tsd->status.account_id != p->AID ){
  20553. return;
  20554. }
  20555. struct s_party_booking_requirement requirement;
  20556. if( !party_booking_load( tsd->status.account_id, tsd->status.char_id, &requirement ) ){
  20557. return;
  20558. }
  20559. if( sd->status.base_level < requirement.minimum_level ){
  20560. return;
  20561. }
  20562. if( sd->status.base_level > requirement.maximum_level ){
  20563. return;
  20564. }
  20565. // Already requested to join this party
  20566. if( util::vector_exists( tsd->party_booking_requests, sd->status.char_id ) ){
  20567. return;
  20568. }
  20569. // Store information that the player tried to join the party
  20570. tsd->party_booking_requests.push_back( sd->status.char_id );
  20571. clif_partybooking_ask( tsd, sd );
  20572. #endif
  20573. }
  20574. void clif_partybooking_reply( map_session_data* sd, map_session_data* party_leader_sd, bool accepted ){
  20575. #if PACKETVER >= 20191204
  20576. PACKET_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER p = {};
  20577. if( party_leader_sd->status.party_id == 0 ){
  20578. return;
  20579. }
  20580. struct party_data* party = party_search( party_leader_sd->status.party_id );
  20581. if( party == nullptr ){
  20582. return;
  20583. }
  20584. p.packetType = HEADER_ZC_PARTY_JOIN_REQ_ACK_FROM_MASTER;
  20585. safestrncpy( p.player_name, party_leader_sd->status.name, NAME_LENGTH );
  20586. safestrncpy( p.party_name, party->party.name, NAME_LENGTH );
  20587. p.AID = party_leader_sd->status.account_id;
  20588. p.refused = !accepted;
  20589. clif_send( &p, sizeof( p ), &sd->bl, SELF );
  20590. #endif
  20591. }
  20592. void clif_parse_partybooking_reply( int32 fd, map_session_data* sd ){
  20593. #if PACKETVER >= 20191204
  20594. const PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN* p = reinterpret_cast<PACKET_CZ_PARTY_REQ_ACK_MASTER_TO_JOIN*>( RFIFOP( fd, 0 ) );
  20595. map_session_data* tsd = map_charid2sd( p->CID );
  20596. // Target player is offline
  20597. if( tsd == nullptr ){
  20598. return;
  20599. }
  20600. if( tsd->status.account_id != p->AID ){
  20601. return;
  20602. }
  20603. // Check if the player even requested to join the party
  20604. if( !util::vector_exists( sd->party_booking_requests, p->CID ) ){
  20605. return;
  20606. }
  20607. util::vector_erase_if_exists( sd->party_booking_requests, p->CID );
  20608. // Only party leaders can reply
  20609. if( !party_isleader( sd ) ){
  20610. return;
  20611. }
  20612. struct party_data* party = party_search( sd->status.party_id );
  20613. if( party == nullptr ){
  20614. return;
  20615. }
  20616. if( party->instance_id > 0 && battle_config.instance_block_invite ){
  20617. return;
  20618. }
  20619. if( p->accept ){
  20620. party_join( *tsd, sd->status.party_id );
  20621. }
  20622. clif_partybooking_reply( tsd, sd, p->accept );
  20623. #endif
  20624. }
  20625. void clif_parse_reset_skill( int32 fd, map_session_data* sd ){
  20626. #if PACKETVER_MAIN_NUM >= 20220216 || PACKETVER_ZERO_NUM >= 20220203
  20627. const PACKET_CZ_RESET_SKILL* p = reinterpret_cast<PACKET_CZ_RESET_SKILL*>( RFIFOP( fd, 0 ) );
  20628. #endif
  20629. }
  20630. void clif_set_dialog_align(map_session_data& sd, int32 npcid, e_say_dialog_align align)
  20631. {
  20632. #if PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20633. PACKET_ZC_DIALOG_TEXT_ALIGN p = {};
  20634. p.PacketType = HEADER_ZC_DIALOG_TEXT_ALIGN;
  20635. p.align = align;
  20636. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20637. #endif // PACKETVER_MAIN_NUM >= 20210203 || PACKETVER_RE_NUM >= 20211103 || PACKETVER_ZERO_NUM >= 20221024
  20638. }
  20639. void clif_set_npc_window_size(map_session_data& sd, int32 width, int32 height)
  20640. {
  20641. #if PACKETVER_MAIN_NUM >= 20220504
  20642. PACKET_ZC_DIALOG_WINDOW_SIZE p = {};
  20643. p.PacketType = HEADER_ZC_DIALOG_WINDOW_SIZE;
  20644. p.width = width;
  20645. p.height = height;
  20646. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20647. #endif // PACKETVER_MAIN_NUM >= 20220504
  20648. }
  20649. void clif_set_npc_window_pos(map_session_data& sd, int32 x, int32 y)
  20650. {
  20651. #if PACKETVER_MAIN_NUM >= 20220504
  20652. PACKET_ZC_DIALOG_WINDOW_POS p = {};
  20653. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS;
  20654. p.x = x;
  20655. p.y = y;
  20656. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20657. #endif // PACKETVER_MAIN_NUM >= 20220504
  20658. }
  20659. void clif_set_npc_window_pos_percent(map_session_data& sd, int32 x, int32 y)
  20660. {
  20661. #if PACKETVER_MAIN_NUM >= 20220504
  20662. PACKET_ZC_DIALOG_WINDOW_POS2 p = {};
  20663. p.PacketType = HEADER_ZC_DIALOG_WINDOW_POS2;
  20664. p.x = x;
  20665. p.y = y;
  20666. clif_send( &p, sizeof( p ), &sd.bl, SELF );
  20667. #endif // PACKETVER_MAIN_NUM >= 20220504
  20668. }
  20669. /// Displays a special popup.
  20670. /// Works only if player moved from one map to another.
  20671. /// 0bbe <popup id>.L (ZC_SPECIALPOPUP)
  20672. void clif_specialpopup(map_session_data& sd, int32 id ){
  20673. #if PACKETVER >= 20221005
  20674. PACKET_ZC_SPECIALPOPUP p = {};
  20675. p.PacketType = HEADER_ZC_SPECIALPOPUP;
  20676. p.ppId = id;
  20677. clif_send( &p, sizeof( p ), &sd.bl, SELF);
  20678. #endif
  20679. }
  20680. /*==========================================
  20681. * Main client packet processing function
  20682. *------------------------------------------*/
  20683. static int32 clif_parse(int32 fd)
  20684. {
  20685. int32 cmd, packet_len;
  20686. TBL_PC* sd;
  20687. int32 pnum;
  20688. #ifdef PACKET_OBFUSCATION
  20689. int32 cmd2;
  20690. #endif
  20691. //TODO apply delays or disconnect based on packet throughput [FlavioJS]
  20692. // Note: "click masters" can do 80+ clicks in 10 seconds
  20693. 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]
  20694. { // begin main client packet processing loop
  20695. sd = (TBL_PC *)session[fd]->session_data;
  20696. if (session[fd]->flag.eof) {
  20697. if (sd) {
  20698. if (sd->state.autotrade) {
  20699. //Disassociate character from the socket connection.
  20700. session[fd]->session_data = nullptr;
  20701. sd->fd = 0;
  20702. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off (using @autotrade).\n", sd->status.name);
  20703. } else
  20704. if (sd->state.active) {
  20705. // Player logout display [Valaris]
  20706. ShowInfo("Character '" CL_WHITE "%s" CL_RESET "' logged off.\n", sd->status.name);
  20707. clif_quitsave(fd, sd);
  20708. } else {
  20709. //Unusual logout (during log on/off/map-changer procedure)
  20710. ShowInfo("Player AID:%d/CID:%d logged off.\n", sd->status.account_id, sd->status.char_id);
  20711. map_quit(sd);
  20712. }
  20713. } else {
  20714. ShowInfo("Closed connection from '" CL_WHITE "%s" CL_RESET "'.\n", ip2str(session[fd]->client_addr, nullptr));
  20715. }
  20716. do_close(fd);
  20717. return 0;
  20718. }
  20719. if (RFIFOREST(fd) < 2)
  20720. return 0;
  20721. cmd = RFIFOW(fd, 0);
  20722. #ifdef PACKET_OBFUSCATION
  20723. // Check if it is a player that tries to connect to the map server.
  20724. if( sd ){
  20725. // Decrypt the current packet ID with the last key stored in the session.
  20726. cmd = (cmd ^ ((sd->cryptKey >> 16) & 0x7FFF));
  20727. }else{
  20728. // Store the original value for checking
  20729. cmd2 = cmd;
  20730. // A player tries to connect - use the initial keys for the decryption of the packet ID.
  20731. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20732. }
  20733. #endif
  20734. // filter out invalid / unsupported packets
  20735. if (cmd > MAX_PACKET_DB || cmd < MIN_PACKET_DB || packet_db[cmd].len == 0) {
  20736. ShowWarning("clif_parse: Received unsupported packet (packet 0x%04x, %d bytes received), disconnecting session #%d.\n", cmd, RFIFOREST(fd), fd);
  20737. #ifdef DUMP_INVALID_PACKET
  20738. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20739. #endif
  20740. #ifdef PACKET_OBFUSCATION_WARN
  20741. // If it is a connection attempt, check if the packet would have been valid without decrypting it
  20742. if( !sd ){
  20743. #ifdef PACKET_OBFUSCATION
  20744. if( cmd2 >= MIN_PACKET_DB && cmd2 < MAX_PACKET_DB && packet_db[cmd2].len != 0 && packet_db[cmd2].func == clif_parse_WantToConnection ){
  20745. ShowWarning( "clif_parse: It looks like you have enabled PACKET_OBFUSCATION on server side, but disabled it on client side.\n" );
  20746. }
  20747. #else
  20748. // Try to use the initial keys for the decryption of the packet ID.
  20749. cmd = (cmd ^ ((((clif_cryptKey[0] * clif_cryptKey[1]) + clif_cryptKey[2]) >> 16) & 0x7FFF));
  20750. if( cmd >= MIN_PACKET_DB && cmd < MAX_PACKET_DB && packet_db[cmd].len != 0 && packet_db[cmd].func == clif_parse_WantToConnection ){
  20751. ShowWarning( "clif_parse: It looks like you have disabled PACKET_OBFUSCATION on server side, but enabled it on client side.\n" );
  20752. }
  20753. #endif
  20754. }
  20755. #endif
  20756. set_eof(fd);
  20757. return 0;
  20758. }
  20759. // determine real packet length
  20760. packet_len = packet_db[cmd].len;
  20761. if (packet_len == -1) { // variable-length packet
  20762. if (RFIFOREST(fd) < 4)
  20763. return 0;
  20764. packet_len = RFIFOW(fd,2);
  20765. if (packet_len < 4 || packet_len > 32768) {
  20766. ShowWarning("clif_parse: Received packet 0x%04x specifies invalid packet_len (%d), disconnecting session #%d.\n", cmd, packet_len, fd);
  20767. #ifdef DUMP_INVALID_PACKET
  20768. ShowDump(RFIFOP(fd,0), RFIFOREST(fd));
  20769. #endif
  20770. set_eof(fd);
  20771. return 0;
  20772. }
  20773. }
  20774. if ((int32)RFIFOREST(fd) < packet_len){
  20775. 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 );
  20776. #ifdef DUMP_INVALID_PACKET
  20777. ShowDump( RFIFOP( fd, 0 ), RFIFOREST( fd ) );
  20778. #endif
  20779. set_eof( fd );
  20780. return 0; // not enough data received to form the packet
  20781. }
  20782. #ifdef PACKET_OBFUSCATION
  20783. RFIFOW(fd, 0) = cmd;
  20784. if (sd)
  20785. sd->cryptKey = ((sd->cryptKey * clif_cryptKey[1]) + clif_cryptKey[2]) & 0xFFFFFFFF; // Update key for the next packet
  20786. #endif
  20787. if( packet_db[cmd].func == clif_parse_debug )
  20788. packet_db[cmd].func(fd, sd);
  20789. else if( packet_db[cmd].func != nullptr ) {
  20790. if( !sd && packet_db[cmd].func != clif_parse_WantToConnection )
  20791. ; //Only valid packet when there is no session
  20792. else
  20793. if( sd && sd->bl.prev == nullptr && packet_db[cmd].func != clif_parse_LoadEndAck )
  20794. ; //Only valid packet when player is not on a map
  20795. else
  20796. packet_db[cmd].func(fd, sd);
  20797. }
  20798. #ifdef DUMP_UNKNOWN_PACKET
  20799. else DumpUnknown(fd,sd,cmd,packet_len);
  20800. #endif
  20801. RFIFOSKIP(fd, packet_len);
  20802. }; // main loop end
  20803. return 0;
  20804. }
  20805. void packetdb_addpacket( uint16 cmd, uint16 length, void (*func)(int32, map_session_data *), ... ){
  20806. va_list argp;
  20807. int32 i;
  20808. if(cmd <= 0 || cmd > MAX_PACKET_DB)
  20809. return;
  20810. packet_db[cmd].len = length;
  20811. packet_db[cmd].func = func;
  20812. va_start(argp, func);
  20813. for( i = 0; i < MAX_PACKET_POS; i++ ){
  20814. int32 offset = va_arg(argp, int32);
  20815. if( offset == 0 ){
  20816. break;
  20817. }
  20818. packet_db[cmd].pos[i] = offset;
  20819. }
  20820. if( i == MAX_PACKET_POS ){
  20821. ShowError( "Too many positions found for packet 0x%04x (max=%d).\n", cmd, MAX_PACKET_POS );
  20822. }
  20823. va_end(argp);
  20824. }
  20825. /*==========================================
  20826. * Reads packets and setups its array reference
  20827. *------------------------------------------*/
  20828. void packetdb_readdb(){
  20829. memset(packet_db,0,sizeof(packet_db));
  20830. #include "clif_packetdb.hpp"
  20831. #include "clif_shuffle.hpp"
  20832. ShowStatus("Using packet version: " CL_WHITE "%d" CL_RESET ".\n", PACKETVER);
  20833. #ifdef PACKET_OBFUSCATION
  20834. 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]);
  20835. #else
  20836. ShowStatus("Packet Obfuscation: " CL_RED "Disabled" CL_RESET ".\n");
  20837. #endif
  20838. }
  20839. /*==========================================
  20840. *
  20841. *------------------------------------------*/
  20842. void do_init_clif(void) {
  20843. const int32 colors[COLOR_MAX] = {
  20844. 0x00FF00, // COLOR_DEFAULT
  20845. 0xFF0000, // COLOR_RED
  20846. 0xFFFFFF, // COLOR_WHITE
  20847. 0xFFFF00, // COLOR_YELLOW
  20848. 0x00FFFF, // COLOR_CYAN
  20849. 0xB5FFB5, // COLOR_LIGHT_GREEN
  20850. 0xFFFF63, // COLOR_LIGHT_YELLOW
  20851. };
  20852. /**
  20853. * Setup Color Table (saves unnecessary load of strtoul on every call)
  20854. **/
  20855. for( int32 i = 0; i < COLOR_MAX; i++ ){
  20856. color_table[i] = ( colors[i] & 0x0000FF ) << 16 | ( colors[i] & 0x00FF00 ) | ( colors[i] & 0xFF0000 ) >> 16; //RGB to BGR
  20857. }
  20858. packetdb_readdb();
  20859. set_defaultparse(clif_parse);
  20860. if( make_listen_bind(bind_ip,map_port) == -1 ) {
  20861. ShowFatalError("Failed to bind to port '" CL_WHITE "%d" CL_RESET "'\n",map_port);
  20862. exit(EXIT_FAILURE);
  20863. }
  20864. add_timer_func_list(clif_clearunit_delayed_sub, "clif_clearunit_delayed_sub");
  20865. add_timer_func_list(clif_delayquit, "clif_delayquit");
  20866. #if PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320
  20867. add_timer_func_list( clif_ping_timer, "clif_ping_timer" );
  20868. if( battle_config.ping_timer_interval > 0 ){
  20869. add_timer_interval( gettick() + battle_config.ping_timer_interval * 1000, clif_ping_timer, 0, 0, battle_config.ping_timer_interval * 1000 );
  20870. }
  20871. #endif
  20872. delay_clearunit_ers = ers_new(sizeof(struct block_list),"clif.cpp::delay_clearunit_ers",ERS_OPT_CLEAR);
  20873. }
  20874. void do_final_clif(void) {
  20875. ers_destroy(delay_clearunit_ers);
  20876. }