L2Character.java 196 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.actor;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Queue;
  27. import java.util.Set;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import java.util.concurrent.Future;
  30. import java.util.concurrent.LinkedBlockingDeque;
  31. import java.util.concurrent.TimeUnit;
  32. import java.util.concurrent.locks.ReentrantLock;
  33. import java.util.concurrent.locks.StampedLock;
  34. import java.util.logging.Level;
  35. import java.util.logging.Logger;
  36. import com.l2jserver.Config;
  37. import com.l2jserver.gameserver.GameTimeController;
  38. import com.l2jserver.gameserver.GeoData;
  39. import com.l2jserver.gameserver.ThreadPoolManager;
  40. import com.l2jserver.gameserver.ai.CtrlEvent;
  41. import com.l2jserver.gameserver.ai.CtrlIntention;
  42. import com.l2jserver.gameserver.ai.L2AttackableAI;
  43. import com.l2jserver.gameserver.ai.L2CharacterAI;
  44. import com.l2jserver.gameserver.data.xml.impl.CategoryData;
  45. import com.l2jserver.gameserver.data.xml.impl.DoorData;
  46. import com.l2jserver.gameserver.datatables.ItemTable;
  47. import com.l2jserver.gameserver.enums.CategoryType;
  48. import com.l2jserver.gameserver.enums.InstanceType;
  49. import com.l2jserver.gameserver.enums.Race;
  50. import com.l2jserver.gameserver.enums.ShotType;
  51. import com.l2jserver.gameserver.enums.Team;
  52. import com.l2jserver.gameserver.idfactory.IdFactory;
  53. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  54. import com.l2jserver.gameserver.instancemanager.MapRegionManager;
  55. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
  56. import com.l2jserver.gameserver.model.CharEffectList;
  57. import com.l2jserver.gameserver.model.L2AccessLevel;
  58. import com.l2jserver.gameserver.model.L2Clan;
  59. import com.l2jserver.gameserver.model.L2Object;
  60. import com.l2jserver.gameserver.model.L2Party;
  61. import com.l2jserver.gameserver.model.L2World;
  62. import com.l2jserver.gameserver.model.L2WorldRegion;
  63. import com.l2jserver.gameserver.model.Location;
  64. import com.l2jserver.gameserver.model.PcCondOverride;
  65. import com.l2jserver.gameserver.model.TeleportWhereType;
  66. import com.l2jserver.gameserver.model.TimeStamp;
  67. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  68. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  69. import com.l2jserver.gameserver.model.actor.instance.L2RiftInvaderInstance;
  70. import com.l2jserver.gameserver.model.actor.knownlist.CharKnownList;
  71. import com.l2jserver.gameserver.model.actor.stat.CharStat;
  72. import com.l2jserver.gameserver.model.actor.status.CharStatus;
  73. import com.l2jserver.gameserver.model.actor.tasks.character.FlyToLocationTask;
  74. import com.l2jserver.gameserver.model.actor.tasks.character.HitTask;
  75. import com.l2jserver.gameserver.model.actor.tasks.character.MagicUseTask;
  76. import com.l2jserver.gameserver.model.actor.tasks.character.NotifyAITask;
  77. import com.l2jserver.gameserver.model.actor.tasks.character.QueuedMagicUseTask;
  78. import com.l2jserver.gameserver.model.actor.tasks.character.UsePotionTask;
  79. import com.l2jserver.gameserver.model.actor.templates.L2CharTemplate;
  80. import com.l2jserver.gameserver.model.actor.transform.Transform;
  81. import com.l2jserver.gameserver.model.actor.transform.TransformTemplate;
  82. import com.l2jserver.gameserver.model.effects.EffectFlag;
  83. import com.l2jserver.gameserver.model.effects.L2EffectType;
  84. import com.l2jserver.gameserver.model.entity.Instance;
  85. import com.l2jserver.gameserver.model.events.Containers;
  86. import com.l2jserver.gameserver.model.events.EventDispatcher;
  87. import com.l2jserver.gameserver.model.events.EventType;
  88. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureAttack;
  89. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureAttackAvoid;
  90. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureAttacked;
  91. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureDamageDealt;
  92. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureDamageReceived;
  93. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureKill;
  94. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureSkillUse;
  95. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureTeleported;
  96. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcSkillSee;
  97. import com.l2jserver.gameserver.model.events.listeners.AbstractEventListener;
  98. import com.l2jserver.gameserver.model.events.returns.TerminateReturn;
  99. import com.l2jserver.gameserver.model.holders.InvulSkillHolder;
  100. import com.l2jserver.gameserver.model.holders.SkillHolder;
  101. import com.l2jserver.gameserver.model.holders.SkillUseHolder;
  102. import com.l2jserver.gameserver.model.interfaces.IDeletable;
  103. import com.l2jserver.gameserver.model.interfaces.ILocational;
  104. import com.l2jserver.gameserver.model.interfaces.ISkillsHolder;
  105. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  106. import com.l2jserver.gameserver.model.items.L2Item;
  107. import com.l2jserver.gameserver.model.items.L2Weapon;
  108. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  109. import com.l2jserver.gameserver.model.items.type.WeaponType;
  110. import com.l2jserver.gameserver.model.options.OptionsSkillHolder;
  111. import com.l2jserver.gameserver.model.options.OptionsSkillType;
  112. import com.l2jserver.gameserver.model.skills.AbnormalType;
  113. import com.l2jserver.gameserver.model.skills.AbnormalVisualEffect;
  114. import com.l2jserver.gameserver.model.skills.BuffInfo;
  115. import com.l2jserver.gameserver.model.skills.CommonSkill;
  116. import com.l2jserver.gameserver.model.skills.EffectScope;
  117. import com.l2jserver.gameserver.model.skills.Skill;
  118. import com.l2jserver.gameserver.model.skills.SkillChannelized;
  119. import com.l2jserver.gameserver.model.skills.SkillChannelizer;
  120. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  121. import com.l2jserver.gameserver.model.stats.BaseStats;
  122. import com.l2jserver.gameserver.model.stats.Calculator;
  123. import com.l2jserver.gameserver.model.stats.Formulas;
  124. import com.l2jserver.gameserver.model.stats.Stats;
  125. import com.l2jserver.gameserver.model.stats.functions.AbstractFunction;
  126. import com.l2jserver.gameserver.model.zone.ZoneId;
  127. import com.l2jserver.gameserver.network.SystemMessageId;
  128. import com.l2jserver.gameserver.network.serverpackets.AbstractNpcInfo;
  129. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  130. import com.l2jserver.gameserver.network.serverpackets.Attack;
  131. import com.l2jserver.gameserver.network.serverpackets.ChangeMoveType;
  132. import com.l2jserver.gameserver.network.serverpackets.ChangeWaitType;
  133. import com.l2jserver.gameserver.network.serverpackets.ExRotation;
  134. import com.l2jserver.gameserver.network.serverpackets.FlyToLocation.FlyType;
  135. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  136. import com.l2jserver.gameserver.network.serverpackets.MagicSkillCanceld;
  137. import com.l2jserver.gameserver.network.serverpackets.MagicSkillLaunched;
  138. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  139. import com.l2jserver.gameserver.network.serverpackets.MoveToLocation;
  140. import com.l2jserver.gameserver.network.serverpackets.Revive;
  141. import com.l2jserver.gameserver.network.serverpackets.ServerObjectInfo;
  142. import com.l2jserver.gameserver.network.serverpackets.SetupGauge;
  143. import com.l2jserver.gameserver.network.serverpackets.SocialAction;
  144. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  145. import com.l2jserver.gameserver.network.serverpackets.StopMove;
  146. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  147. import com.l2jserver.gameserver.network.serverpackets.TeleportToLocation;
  148. import com.l2jserver.gameserver.pathfinding.AbstractNodeLoc;
  149. import com.l2jserver.gameserver.pathfinding.PathFinding;
  150. import com.l2jserver.gameserver.taskmanager.AttackStanceTaskManager;
  151. import com.l2jserver.gameserver.util.Util;
  152. import com.l2jserver.util.EmptyQueue;
  153. import com.l2jserver.util.Rnd;
  154. /**
  155. * Mother class of all character objects of the world (PC, NPC...)<br>
  156. * L2Character:<br>
  157. * <ul>
  158. * <li>L2DoorInstance</li>
  159. * <li>L2Playable</li>
  160. * <li>L2Npc</li>
  161. * <li>L2StaticObjectInstance</li>
  162. * <li>L2Trap</li>
  163. * <li>L2Vehicle</li>
  164. * </ul>
  165. * <br>
  166. * <b>Concept of L2CharTemplate:</b><br>
  167. * Each L2Character owns generic and static properties (ex : all Keltir have the same number of HP...).<br>
  168. * All of those properties are stored in a different template for each type of L2Character.<br>
  169. * Each template is loaded once in the server cache memory (reduce memory use).<br>
  170. * When a new instance of L2Character is spawned, server just create a link between the instance and the template.<br>
  171. * This link is stored in {@link #_template}
  172. * @version $Revision: 1.53.2.45.2.34 $ $Date: 2005/04/11 10:06:08 $
  173. */
  174. public abstract class L2Character extends L2Object implements ISkillsHolder, IDeletable
  175. {
  176. public static final Logger _log = Logger.getLogger(L2Character.class.getName());
  177. private volatile Set<L2Character> _attackByList;
  178. private volatile boolean _isCastingNow = false;
  179. private volatile boolean _isCastingSimultaneouslyNow = false;
  180. private Skill _lastSkillCast;
  181. private Skill _lastSimultaneousSkillCast;
  182. private boolean _isDead = false;
  183. private boolean _isImmobilized = false;
  184. private boolean _isOverloaded = false; // the char is carrying too much
  185. private boolean _isParalyzed = false;
  186. private boolean _isPendingRevive = false;
  187. private boolean _isRunning = false;
  188. private boolean _isNoRndWalk = false; // Is no random walk
  189. protected boolean _showSummonAnimation = false;
  190. protected boolean _isTeleporting = false;
  191. private boolean _isInvul = false;
  192. private boolean _isMortal = true; // Char will die when HP decreased to 0
  193. private boolean _isFlying = false;
  194. private CharStat _stat;
  195. private CharStatus _status;
  196. private L2CharTemplate _template; // The link on the L2CharTemplate object containing generic and static properties of this L2Character type (ex : Max HP, Speed...)
  197. private String _title;
  198. public static final double MAX_HP_BAR_PX = 352.0;
  199. private double _hpUpdateIncCheck = .0;
  200. private double _hpUpdateDecCheck = .0;
  201. private double _hpUpdateInterval = .0;
  202. /** Table of Calculators containing all used calculator */
  203. private Calculator[] _calculators;
  204. /** Map containing all skills of this character. */
  205. private final Map<Integer, Skill> _skills = new ConcurrentHashMap<>();
  206. /** Map containing the skill reuse time stamps. */
  207. private volatile Map<Integer, TimeStamp> _reuseTimeStampsSkills = null;
  208. /** Map containing the item reuse time stamps. */
  209. private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null;
  210. /** Map containing all the disabled skills. */
  211. private volatile Map<Integer, Long> _disabledSkills = null;
  212. private boolean _allSkillsDisabled;
  213. private final byte[] _zones = new byte[ZoneId.getZoneCount()];
  214. protected byte _zoneValidateCounter = 4;
  215. private L2Character _debugger = null;
  216. private final ReentrantLock _teleportLock = new ReentrantLock();
  217. private final StampedLock _attackLock = new StampedLock();
  218. private Team _team = Team.NONE;
  219. protected long _exceptions = 0L;
  220. private boolean _lethalable = true;
  221. private volatile Map<Integer, OptionsSkillHolder> _triggerSkills;
  222. private volatile Map<Integer, InvulSkillHolder> _invulAgainst;
  223. /** Creatures effect list. */
  224. private final CharEffectList _effectList = new CharEffectList(this);
  225. /** The character that summons this character. */
  226. private L2Character _summoner = null;
  227. private SkillChannelizer _channelizer = null;
  228. private SkillChannelized _channelized = null;
  229. /** Map 32 bits, containing all abnormal visual effects in progress. */
  230. private int _abnormalVisualEffects;
  231. /** Map 32 bits, containing all special abnormal visual effects in progress. */
  232. private int _abnormalVisualEffectsSpecial;
  233. /** Map 32 bits, containing all event abnormal visual effects in progress. */
  234. private int _abnormalVisualEffectsEvent;
  235. /** Movement data of this L2Character */
  236. protected MoveData _move;
  237. /** This creature's target. */
  238. private L2Object _target;
  239. /** Represents the time where the attack should end, in nanoseconds. */
  240. private volatile long _attackEndTime;
  241. private int _disableBowAttackEndTime;
  242. private int _disableCrossBowAttackEndTime;
  243. private int _castInterruptTime;
  244. /** Table of calculators containing all standard NPC calculator (ex : ACCURACY_COMBAT, EVASION_RATE) */
  245. private static final Calculator[] NPC_STD_CALCULATOR = Formulas.getStdNPCCalculators();
  246. private volatile L2CharacterAI _ai = null;
  247. /** Future Skill Cast */
  248. protected Future<?> _skillCast;
  249. protected Future<?> _skillCast2;
  250. /**
  251. * Creates a creature.
  252. * @param template the creature template
  253. */
  254. public L2Character(L2CharTemplate template)
  255. {
  256. this(IdFactory.getInstance().getNextId(), template);
  257. }
  258. /**
  259. * Constructor of L2Character.<br>
  260. * <B><U>Concept</U>:</B><br>
  261. * Each L2Character owns generic and static properties (ex : all Keltir have the same number of HP...).<br>
  262. * All of those properties are stored in a different template for each type of L2Character. Each template is loaded once in the server cache memory (reduce memory use).<br>
  263. * When a new instance of L2Character is spawned, server just create a link between the instance and the template This link is stored in <B>_template</B><br>
  264. * <B><U> Actions</U>:</B>
  265. * <ul>
  266. * <li>Set the _template of the L2Character</li>
  267. * <li>Set _overloaded to false (the character can take more items)</li>
  268. * <li>If L2Character is a L2NPCInstance, copy skills from template to object</li>
  269. * <li>If L2Character is a L2NPCInstance, link _calculators to NPC_STD_CALCULATOR</li>
  270. * <li>If L2Character is NOT a L2NPCInstance, create an empty _skills slot</li>
  271. * <li>If L2Character is a L2PcInstance or L2Summon, copy basic Calculator set to object</li>
  272. * </ul>
  273. * @param objectId Identifier of the object to initialized
  274. * @param template The L2CharTemplate to apply to the object
  275. */
  276. public L2Character(int objectId, L2CharTemplate template)
  277. {
  278. super(objectId);
  279. if (template == null)
  280. {
  281. throw new NullPointerException("Template is null!");
  282. }
  283. setInstanceType(InstanceType.L2Character);
  284. initCharStat();
  285. initCharStatus();
  286. // Set its template to the new L2Character
  287. _template = template;
  288. if (isDoor())
  289. {
  290. _calculators = Formulas.getStdDoorCalculators();
  291. }
  292. else if (isNpc())
  293. {
  294. // Copy the Standard Calculators of the L2NPCInstance in _calculators
  295. _calculators = NPC_STD_CALCULATOR;
  296. // Copy the skills of the L2NPCInstance from its template to the L2Character Instance
  297. // The skills list can be affected by spell effects so it's necessary to make a copy
  298. // to avoid that a spell affecting a L2NpcInstance, affects others L2NPCInstance of the same type too.
  299. for (Skill skill : template.getSkills().values())
  300. {
  301. addSkill(skill);
  302. }
  303. }
  304. else
  305. {
  306. // If L2Character is a L2PcInstance or a L2Summon, create the basic calculator set
  307. _calculators = new Calculator[Stats.NUM_STATS];
  308. if (isSummon())
  309. {
  310. // Copy the skills of the L2Summon from its template to the L2Character Instance
  311. // The skills list can be affected by spell effects so it's necessary to make a copy
  312. // to avoid that a spell affecting a L2Summon, affects others L2Summon of the same type too.
  313. for (Skill skill : template.getSkills().values())
  314. {
  315. addSkill(skill);
  316. }
  317. }
  318. Formulas.addFuncsToNewCharacter(this);
  319. }
  320. setIsInvul(true);
  321. }
  322. public final CharEffectList getEffectList()
  323. {
  324. return _effectList;
  325. }
  326. /**
  327. * Verify if this character is under debug.
  328. * @return {@code true} if this character is under debug, {@code false} otherwise
  329. */
  330. public boolean isDebug()
  331. {
  332. return _debugger != null;
  333. }
  334. /**
  335. * Sets character instance, to which debug packets will be send.
  336. * @param debugger the character debugging this character
  337. */
  338. public void setDebug(L2Character debugger)
  339. {
  340. _debugger = debugger;
  341. }
  342. /**
  343. * Send debug packet.
  344. * @param pkt
  345. */
  346. public void sendDebugPacket(L2GameServerPacket pkt)
  347. {
  348. if (_debugger != null)
  349. {
  350. _debugger.sendPacket(pkt);
  351. }
  352. }
  353. /**
  354. * Send debug text string
  355. * @param msg
  356. */
  357. public void sendDebugMessage(String msg)
  358. {
  359. if (_debugger != null)
  360. {
  361. _debugger.sendMessage(msg);
  362. }
  363. }
  364. /**
  365. * @return character inventory, default null, overridden in L2Playable types and in L2NPcInstance
  366. */
  367. public Inventory getInventory()
  368. {
  369. return null;
  370. }
  371. public boolean destroyItemByItemId(String process, int itemId, long count, L2Object reference, boolean sendMessage)
  372. {
  373. // Default: NPCs consume virtual items for their skills
  374. // TODO: should be logged if even happens.. should be false
  375. return true;
  376. }
  377. public boolean destroyItem(String process, int objectId, long count, L2Object reference, boolean sendMessage)
  378. {
  379. // Default: NPCs consume virtual items for their skills
  380. // TODO: should be logged if even happens.. should be false
  381. return true;
  382. }
  383. /**
  384. * Check if the character is in the given zone Id.
  385. * @param zone the zone Id to check
  386. * @return {code true} if the character is in that zone
  387. */
  388. @Override
  389. public final boolean isInsideZone(ZoneId zone)
  390. {
  391. Instance instance = InstanceManager.getInstance().getInstance(getInstanceId());
  392. switch (zone)
  393. {
  394. case PVP:
  395. if ((instance != null) && instance.isPvPInstance())
  396. {
  397. return true;
  398. }
  399. return (_zones[ZoneId.PVP.ordinal()] > 0) && (_zones[ZoneId.PEACE.ordinal()] == 0);
  400. case PEACE:
  401. if ((instance != null) && instance.isPvPInstance())
  402. {
  403. return false;
  404. }
  405. }
  406. return _zones[zone.ordinal()] > 0;
  407. }
  408. /**
  409. * @param zone
  410. * @param state
  411. */
  412. public final void setInsideZone(ZoneId zone, final boolean state)
  413. {
  414. synchronized (_zones)
  415. {
  416. if (state)
  417. {
  418. _zones[zone.ordinal()]++;
  419. }
  420. else if (_zones[zone.ordinal()] > 0)
  421. {
  422. _zones[zone.ordinal()]--;
  423. }
  424. }
  425. }
  426. /**
  427. * This will return true if the player is transformed,<br>
  428. * but if the player is not transformed it will return false.
  429. * @return transformation status
  430. */
  431. public boolean isTransformed()
  432. {
  433. return false;
  434. }
  435. public Transform getTransformation()
  436. {
  437. return null;
  438. }
  439. /**
  440. * This will untransform a player if they are an instance of L2Pcinstance and if they are transformed.
  441. */
  442. public void untransform()
  443. {
  444. // Just a place holder
  445. }
  446. /**
  447. * This will return true if the player is GM,<br>
  448. * but if the player is not GM it will return false.
  449. * @return GM status
  450. */
  451. public boolean isGM()
  452. {
  453. return false;
  454. }
  455. /**
  456. * Overridden in L2PcInstance.
  457. * @return the access level.
  458. */
  459. public L2AccessLevel getAccessLevel()
  460. {
  461. return null;
  462. }
  463. protected void initCharStatusUpdateValues()
  464. {
  465. _hpUpdateIncCheck = getMaxHp();
  466. _hpUpdateInterval = _hpUpdateIncCheck / MAX_HP_BAR_PX;
  467. _hpUpdateDecCheck = _hpUpdateIncCheck - _hpUpdateInterval;
  468. }
  469. /**
  470. * Remove the L2Character from the world when the decay task is launched.<br>
  471. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T REMOVE the object from _allObjects of L2World </B></FONT><BR>
  472. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packets to players</B></FONT>
  473. */
  474. public void onDecay()
  475. {
  476. decayMe();
  477. final L2WorldRegion reg = getWorldRegion();
  478. if (reg != null)
  479. {
  480. reg.removeFromZones(this);
  481. }
  482. }
  483. @Override
  484. public void onSpawn()
  485. {
  486. super.onSpawn();
  487. revalidateZone(true);
  488. }
  489. public void onTeleported()
  490. {
  491. if (!_teleportLock.tryLock())
  492. {
  493. return;
  494. }
  495. try
  496. {
  497. if (!isTeleporting())
  498. {
  499. return;
  500. }
  501. spawnMe(getX(), getY(), getZ());
  502. setIsTeleporting(false);
  503. EventDispatcher.getInstance().notifyEventAsync(new OnCreatureTeleported(this), this);
  504. }
  505. finally
  506. {
  507. _teleportLock.unlock();
  508. }
  509. }
  510. /**
  511. * Add L2Character instance that is attacking to the attacker list.
  512. * @param player The L2Character that attacks this one
  513. */
  514. public void addAttackerToAttackByList(L2Character player)
  515. {
  516. // DS: moved to L2Attackable
  517. }
  518. /**
  519. * Send a packet to the L2Character AND to all L2PcInstance in the _KnownPlayers of the L2Character.<br>
  520. * <B><U>Concept</U>:</B><br>
  521. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<br>
  522. * In order to inform other players of state modification on the L2Character, server just need to go through _knownPlayers to send Server->Client Packet
  523. * @param mov
  524. */
  525. public void broadcastPacket(L2GameServerPacket mov)
  526. {
  527. mov.setInvisible(isInvisible());
  528. Collection<L2PcInstance> plrs = getKnownList().getKnownPlayers().values();
  529. for (L2PcInstance player : plrs)
  530. {
  531. if (player != null)
  532. {
  533. player.sendPacket(mov);
  534. }
  535. }
  536. }
  537. /**
  538. * Send a packet to the L2Character AND to all L2PcInstance in the radius (max knownlist radius) from the L2Character.<br>
  539. * <B><U>Concept</U>:</B><br>
  540. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<br>
  541. * In order to inform other players of state modification on the L2Character, server just need to go through _knownPlayers to send Server->Client Packet
  542. * @param mov
  543. * @param radiusInKnownlist
  544. */
  545. public void broadcastPacket(L2GameServerPacket mov, int radiusInKnownlist)
  546. {
  547. mov.setInvisible(isInvisible());
  548. Collection<L2PcInstance> plrs = getKnownList().getKnownPlayers().values();
  549. for (L2PcInstance player : plrs)
  550. {
  551. if ((player != null) && isInsideRadius(player, radiusInKnownlist, false, false))
  552. {
  553. player.sendPacket(mov);
  554. }
  555. }
  556. }
  557. /**
  558. * @return true if hp update should be done, false if not
  559. */
  560. protected boolean needHpUpdate()
  561. {
  562. double currentHp = getCurrentHp();
  563. double maxHp = getMaxHp();
  564. if ((currentHp <= 1.0) || (maxHp < MAX_HP_BAR_PX))
  565. {
  566. return true;
  567. }
  568. if ((currentHp < _hpUpdateDecCheck) || (Math.abs(currentHp - _hpUpdateDecCheck) <= 1e-6) || (currentHp > _hpUpdateIncCheck) || (Math.abs(currentHp - _hpUpdateIncCheck) <= 1e-6))
  569. {
  570. if (Math.abs(currentHp - maxHp) <= 1e-6)
  571. {
  572. _hpUpdateIncCheck = currentHp + 1;
  573. _hpUpdateDecCheck = currentHp - _hpUpdateInterval;
  574. }
  575. else
  576. {
  577. double doubleMulti = currentHp / _hpUpdateInterval;
  578. int intMulti = (int) doubleMulti;
  579. _hpUpdateDecCheck = _hpUpdateInterval * (doubleMulti < intMulti ? intMulti-- : intMulti);
  580. _hpUpdateIncCheck = _hpUpdateDecCheck + _hpUpdateInterval;
  581. }
  582. return true;
  583. }
  584. return false;
  585. }
  586. /**
  587. * Send the Server->Client packet StatusUpdate with current HP and MP to all other L2PcInstance to inform.<br>
  588. * <B><U>Actions</U>:</B>
  589. * <ul>
  590. * <li>Create the Server->Client packet StatusUpdate with current HP and MP</li>
  591. * <li>Send the Server->Client packet StatusUpdate with current HP and MP to all L2Character called _statusListener that must be informed of HP/MP updates of this L2Character</li>
  592. * </ul>
  593. * <FONT COLOR=#FF0000><B><U>Caution</U>: This method DOESN'T SEND CP information</B></FONT>
  594. */
  595. public void broadcastStatusUpdate()
  596. {
  597. if (getStatus().getStatusListener().isEmpty() || !needHpUpdate())
  598. {
  599. return;
  600. }
  601. // Create the Server->Client packet StatusUpdate with current HP
  602. StatusUpdate su = new StatusUpdate(this);
  603. su.addAttribute(StatusUpdate.MAX_HP, getMaxHp());
  604. su.addAttribute(StatusUpdate.CUR_HP, (int) getCurrentHp());
  605. // Go through the StatusListener
  606. // Send the Server->Client packet StatusUpdate with current HP and MP
  607. for (L2Character temp : getStatus().getStatusListener())
  608. {
  609. if (temp != null)
  610. {
  611. temp.sendPacket(su);
  612. }
  613. }
  614. }
  615. /**
  616. * @param text
  617. */
  618. public void sendMessage(String text)
  619. {
  620. // default implementation
  621. }
  622. /**
  623. * Teleport a L2Character and its pet if necessary.<br>
  624. * <B><U>Actions</U>:</B>
  625. * <ul>
  626. * <li>Stop the movement of the L2Character</li>
  627. * <li>Set the x,y,z position of the L2Object and if necessary modify its _worldRegion</li>
  628. * <li>Send a Server->Client packet TeleportToLocationt to the L2Character AND to all L2PcInstance in its _KnownPlayers</li>
  629. * <li>Modify the position of the pet if necessary</li>
  630. * </ul>
  631. * @param x
  632. * @param y
  633. * @param z
  634. * @param heading
  635. * @param instanceId
  636. * @param randomOffset
  637. */
  638. public void teleToLocation(int x, int y, int z, int heading, int instanceId, int randomOffset)
  639. {
  640. setInstanceId(instanceId);
  641. if (_isPendingRevive)
  642. {
  643. doRevive();
  644. }
  645. stopMove(null, false);
  646. abortAttack();
  647. abortCast();
  648. setIsTeleporting(true);
  649. setTarget(null);
  650. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  651. if (Config.OFFSET_ON_TELEPORT_ENABLED && (randomOffset > 0))
  652. {
  653. x += Rnd.get(-randomOffset, randomOffset);
  654. y += Rnd.get(-randomOffset, randomOffset);
  655. }
  656. z += 5;
  657. // Send a Server->Client packet TeleportToLocationt to the L2Character AND to all L2PcInstance in the _KnownPlayers of the L2Character
  658. broadcastPacket(new TeleportToLocation(this, x, y, z, heading));
  659. // remove the object from its old location
  660. decayMe();
  661. // Set the x,y,z position of the L2Object and if necessary modify its _worldRegion
  662. setXYZ(x, y, z);
  663. // temporary fix for heading on teleports
  664. if (heading != 0)
  665. {
  666. setHeading(heading);
  667. }
  668. // allow recall of the detached characters
  669. if (!isPlayer() || ((getActingPlayer().getClient() != null) && getActingPlayer().getClient().isDetached()))
  670. {
  671. onTeleported();
  672. }
  673. revalidateZone(true);
  674. }
  675. public void teleToLocation(int x, int y, int z, int heading, int instanceId, boolean randomOffset)
  676. {
  677. teleToLocation(x, y, z, heading, instanceId, (randomOffset) ? Config.MAX_OFFSET_ON_TELEPORT : 0);
  678. }
  679. public void teleToLocation(int x, int y, int z, int heading, int instanceId)
  680. {
  681. teleToLocation(x, y, z, heading, instanceId, 0);
  682. }
  683. public void teleToLocation(int x, int y, int z, int heading, boolean randomOffset)
  684. {
  685. teleToLocation(x, y, z, heading, -1, (randomOffset) ? Config.MAX_OFFSET_ON_TELEPORT : 0);
  686. }
  687. public void teleToLocation(int x, int y, int z, int heading)
  688. {
  689. teleToLocation(x, y, z, heading, -1, 0);
  690. }
  691. public void teleToLocation(int x, int y, int z, boolean randomOffset)
  692. {
  693. teleToLocation(x, y, z, 0, -1, (randomOffset) ? Config.MAX_OFFSET_ON_TELEPORT : 0);
  694. }
  695. public void teleToLocation(int x, int y, int z)
  696. {
  697. teleToLocation(x, y, z, 0, -1, 0);
  698. }
  699. public void teleToLocation(ILocational loc, int randomOffset)
  700. {
  701. teleToLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), loc.getInstanceId(), randomOffset);
  702. }
  703. public void teleToLocation(ILocational loc, int instanceId, int randomOffset)
  704. {
  705. teleToLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), instanceId, randomOffset);
  706. }
  707. public void teleToLocation(ILocational loc, boolean randomOffset)
  708. {
  709. teleToLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), loc.getInstanceId(), (randomOffset) ? Config.MAX_OFFSET_ON_TELEPORT : 0);
  710. }
  711. public void teleToLocation(ILocational loc)
  712. {
  713. teleToLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), loc.getInstanceId(), 0);
  714. }
  715. public void teleToLocation(TeleportWhereType teleportWhere)
  716. {
  717. teleToLocation(MapRegionManager.getInstance().getTeleToLocation(this, teleportWhere), true);
  718. }
  719. /**
  720. * Launch a physical attack against a target (Simple, Bow, Pole or Dual).<br>
  721. * <B><U>Actions</U>:</B>
  722. * <ul>
  723. * <li>Get the active weapon (always equipped in the right hand)</li>
  724. * <li>If weapon is a bow, check for arrows, MP and bow re-use delay (if necessary, equip the L2PcInstance with arrows in left hand)</li>
  725. * <li>If weapon is a bow, consume MP and set the new period of bow non re-use</li>
  726. * <li>Get the Attack Speed of the L2Character (delay (in milliseconds) before next attack)</li>
  727. * <li>Select the type of attack to start (Simple, Bow, Pole or Dual) and verify if SoulShot are charged then start calculation</li>
  728. * <li>If the Server->Client packet Attack contains at least 1 hit, send the Server->Client packet Attack to the L2Character AND to all L2PcInstance in the _KnownPlayers of the L2Character</li>
  729. * <li>Notify AI with EVT_READY_TO_ACT</li>
  730. * </ul>
  731. * @param target The L2Character targeted
  732. */
  733. public void doAttack(L2Character target)
  734. {
  735. final long stamp = _attackLock.tryWriteLock();
  736. try
  737. {
  738. if ((target == null) || isAttackingDisabled())
  739. {
  740. return;
  741. }
  742. // Notify to scripts
  743. final TerminateReturn attackReturn = EventDispatcher.getInstance().notifyEvent(new OnCreatureAttack(this, target), this, TerminateReturn.class);
  744. if ((attackReturn != null) && attackReturn.terminate())
  745. {
  746. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  747. sendPacket(ActionFailed.STATIC_PACKET);
  748. return;
  749. }
  750. // Notify to scripts
  751. final TerminateReturn attackedReturn = EventDispatcher.getInstance().notifyEvent(new OnCreatureAttacked(this, target), target, TerminateReturn.class);
  752. if ((attackedReturn != null) && attackedReturn.terminate())
  753. {
  754. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  755. sendPacket(ActionFailed.STATIC_PACKET);
  756. return;
  757. }
  758. if (!isAlikeDead())
  759. {
  760. if ((isNpc() && target.isAlikeDead()) || !getKnownList().knowsObject(target))
  761. {
  762. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  763. sendPacket(ActionFailed.STATIC_PACKET);
  764. return;
  765. }
  766. else if (isPlayer())
  767. {
  768. if (target.isDead())
  769. {
  770. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  771. sendPacket(ActionFailed.STATIC_PACKET);
  772. return;
  773. }
  774. final L2PcInstance actor = getActingPlayer();
  775. if (actor.isTransformed() && !actor.getTransformation().canAttack())
  776. {
  777. sendPacket(ActionFailed.STATIC_PACKET);
  778. return;
  779. }
  780. }
  781. }
  782. // Check if attacker's weapon can attack
  783. if (getActiveWeaponItem() != null)
  784. {
  785. L2Weapon wpn = getActiveWeaponItem();
  786. if (!wpn.isAttackWeapon() && !isGM())
  787. {
  788. if (wpn.getItemType() == WeaponType.FISHINGROD)
  789. {
  790. sendPacket(SystemMessageId.CANNOT_ATTACK_WITH_FISHING_POLE);
  791. }
  792. else
  793. {
  794. sendPacket(SystemMessageId.THAT_WEAPON_CANT_ATTACK);
  795. }
  796. sendPacket(ActionFailed.STATIC_PACKET);
  797. return;
  798. }
  799. }
  800. if (getActingPlayer() != null)
  801. {
  802. if (getActingPlayer().inObserverMode())
  803. {
  804. sendPacket(SystemMessageId.OBSERVERS_CANNOT_PARTICIPATE);
  805. sendPacket(ActionFailed.STATIC_PACKET);
  806. return;
  807. }
  808. else if ((target.getActingPlayer() != null) && (getActingPlayer().getSiegeState() > 0) && isInsideZone(ZoneId.SIEGE) && (target.getActingPlayer().getSiegeState() == getActingPlayer().getSiegeState()) && (target.getActingPlayer() != this) && (target.getActingPlayer().getSiegeSide() == getActingPlayer().getSiegeSide()))
  809. {
  810. if (TerritoryWarManager.getInstance().isTWInProgress())
  811. {
  812. sendPacket(SystemMessageId.YOU_CANNOT_ATTACK_A_MEMBER_OF_THE_SAME_TERRITORY);
  813. }
  814. else
  815. {
  816. sendPacket(SystemMessageId.FORCED_ATTACK_IS_IMPOSSIBLE_AGAINST_SIEGE_SIDE_TEMPORARY_ALLIED_MEMBERS);
  817. }
  818. sendPacket(ActionFailed.STATIC_PACKET);
  819. return;
  820. }
  821. // Checking if target has moved to peace zone
  822. else if (target.isInsidePeaceZone(getActingPlayer()))
  823. {
  824. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  825. sendPacket(ActionFailed.STATIC_PACKET);
  826. return;
  827. }
  828. }
  829. else if (isInsidePeaceZone(this, target))
  830. {
  831. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  832. sendPacket(ActionFailed.STATIC_PACKET);
  833. return;
  834. }
  835. stopEffectsOnAction();
  836. // GeoData Los Check here (or dz > 1000)
  837. if (!GeoData.getInstance().canSeeTarget(this, target))
  838. {
  839. sendPacket(SystemMessageId.CANT_SEE_TARGET);
  840. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  841. sendPacket(ActionFailed.STATIC_PACKET);
  842. return;
  843. }
  844. // BOW and CROSSBOW checks
  845. // Get the active weapon item corresponding to the active weapon instance (always equipped in the right hand)
  846. L2Weapon weaponItem = getActiveWeaponItem();
  847. if ((weaponItem != null) && !isTransformed())
  848. {
  849. if (weaponItem.getItemType() == WeaponType.BOW)
  850. {
  851. // Check for arrows and MP
  852. if (isPlayer())
  853. {
  854. // Checking if target has moved to peace zone - only for player-bow attacks at the moment
  855. // Other melee is checked in movement code and for offensive spells a check is done every time
  856. if (target.isInsidePeaceZone(getActingPlayer()))
  857. {
  858. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  859. sendPacket(ActionFailed.STATIC_PACKET);
  860. return;
  861. }
  862. // Equip arrows needed in left hand and send a Server->Client packet ItemList to the L2PcInstance then return True
  863. if (!checkAndEquipArrows())
  864. {
  865. // Cancel the action because the L2PcInstance have no arrow
  866. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  867. sendPacket(ActionFailed.STATIC_PACKET);
  868. sendPacket(SystemMessageId.NOT_ENOUGH_ARROWS);
  869. return;
  870. }
  871. // Verify if the bow can be use
  872. if (_disableBowAttackEndTime <= GameTimeController.getInstance().getGameTicks())
  873. {
  874. // Verify if L2PcInstance owns enough MP
  875. int mpConsume = weaponItem.getMpConsume();
  876. if ((weaponItem.getReducedMpConsume() > 0) && (Rnd.get(100) < weaponItem.getReducedMpConsumeChance()))
  877. {
  878. mpConsume = weaponItem.getReducedMpConsume();
  879. }
  880. mpConsume = (int) calcStat(Stats.BOW_MP_CONSUME_RATE, mpConsume, null, null);
  881. if (getCurrentMp() < mpConsume)
  882. {
  883. // If L2PcInstance doesn't have enough MP, stop the attack
  884. ThreadPoolManager.getInstance().scheduleAi(new NotifyAITask(this, CtrlEvent.EVT_READY_TO_ACT), 1000);
  885. sendPacket(SystemMessageId.NOT_ENOUGH_MP);
  886. sendPacket(ActionFailed.STATIC_PACKET);
  887. return;
  888. }
  889. // If L2PcInstance have enough MP, the bow consumes it
  890. if (mpConsume > 0)
  891. {
  892. getStatus().reduceMp(mpConsume);
  893. }
  894. // Set the period of bow no re-use
  895. _disableBowAttackEndTime = (5 * GameTimeController.TICKS_PER_SECOND) + GameTimeController.getInstance().getGameTicks();
  896. }
  897. else
  898. {
  899. // Cancel the action because the bow can't be re-use at this moment
  900. ThreadPoolManager.getInstance().scheduleAi(new NotifyAITask(this, CtrlEvent.EVT_READY_TO_ACT), 1000);
  901. sendPacket(ActionFailed.STATIC_PACKET);
  902. return;
  903. }
  904. }
  905. }
  906. if (weaponItem.getItemType() == WeaponType.CROSSBOW)
  907. {
  908. // Check for bolts
  909. if (isPlayer())
  910. {
  911. // Checking if target has moved to peace zone - only for player-crossbow attacks at the moment
  912. // Other melee is checked in movement code and for offensive spells a check is done every time
  913. if (target.isInsidePeaceZone(getActingPlayer()))
  914. {
  915. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  916. sendPacket(ActionFailed.STATIC_PACKET);
  917. return;
  918. }
  919. // Equip bolts needed in left hand and send a Server->Client packet ItemList to the L2PcINstance then return True
  920. if (!checkAndEquipBolts())
  921. {
  922. // Cancel the action because the L2PcInstance have no arrow
  923. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  924. sendPacket(ActionFailed.STATIC_PACKET);
  925. sendPacket(SystemMessageId.NOT_ENOUGH_BOLTS);
  926. return;
  927. }
  928. // Verify if the crossbow can be use
  929. if (_disableCrossBowAttackEndTime <= GameTimeController.getInstance().getGameTicks())
  930. {
  931. // Verify if L2PcInstance owns enough MP
  932. int mpConsume = weaponItem.getMpConsume();
  933. if ((weaponItem.getReducedMpConsume() > 0) && (Rnd.get(100) < weaponItem.getReducedMpConsumeChance()))
  934. {
  935. mpConsume = weaponItem.getReducedMpConsume();
  936. }
  937. mpConsume = (int) calcStat(Stats.BOW_MP_CONSUME_RATE, mpConsume, null, null);
  938. if (getCurrentMp() < mpConsume)
  939. {
  940. // If L2PcInstance doesn't have enough MP, stop the attack
  941. ThreadPoolManager.getInstance().scheduleAi(new NotifyAITask(this, CtrlEvent.EVT_READY_TO_ACT), 1000);
  942. sendPacket(SystemMessageId.NOT_ENOUGH_MP);
  943. sendPacket(ActionFailed.STATIC_PACKET);
  944. return;
  945. }
  946. // If L2PcInstance have enough MP, the bow consumes it
  947. if (mpConsume > 0)
  948. {
  949. getStatus().reduceMp(mpConsume);
  950. }
  951. // Set the period of crossbow no re-use
  952. _disableCrossBowAttackEndTime = (5 * GameTimeController.TICKS_PER_SECOND) + GameTimeController.getInstance().getGameTicks();
  953. }
  954. else
  955. {
  956. // Cancel the action because the crossbow can't be re-use at this moment
  957. ThreadPoolManager.getInstance().scheduleAi(new NotifyAITask(this, CtrlEvent.EVT_READY_TO_ACT), 1000);
  958. sendPacket(ActionFailed.STATIC_PACKET);
  959. return;
  960. }
  961. }
  962. else if (isNpc())
  963. {
  964. if (_disableCrossBowAttackEndTime > GameTimeController.getInstance().getGameTicks())
  965. {
  966. return;
  967. }
  968. }
  969. }
  970. }
  971. // Add the L2PcInstance to _knownObjects and _knownPlayer of the target
  972. target.getKnownList().addKnownObject(this);
  973. // Reduce the current CP if TIREDNESS configuration is activated
  974. if (Config.ALT_GAME_TIREDNESS)
  975. {
  976. setCurrentCp(getCurrentCp() - 10);
  977. }
  978. // Verify if soulshots are charged.
  979. final boolean wasSSCharged = isChargedShot(ShotType.SOULSHOTS);
  980. // Get the Attack Speed of the L2Character (delay (in milliseconds) before next attack)
  981. final int timeAtk = calculateTimeBetweenAttacks(target, weaponItem);
  982. // the hit is calculated to happen halfway to the animation - might need further tuning e.g. in bow case
  983. final int timeToHit = timeAtk / 2;
  984. _attackEndTime = System.nanoTime() + TimeUnit.NANOSECONDS.convert(timeAtk, TimeUnit.MILLISECONDS);
  985. final int ssGrade = (weaponItem != null) ? weaponItem.getItemGradeSPlus().getId() : 0;
  986. // Create a Server->Client packet Attack
  987. Attack attack = new Attack(this, target, wasSSCharged, ssGrade);
  988. // Make sure that char is facing selected target
  989. // also works: setHeading(Util.convertDegreeToClientHeading(Util.calculateAngleFrom(this, target)));
  990. setHeading(Util.calculateHeadingFrom(this, target));
  991. // Get the Attack Reuse Delay of the L2Weapon
  992. int reuse = calculateReuseTime(target, weaponItem);
  993. boolean hitted = false;
  994. switch (getAttackType())
  995. {
  996. case BOW:
  997. {
  998. hitted = doAttackHitByBow(attack, target, timeAtk, reuse);
  999. break;
  1000. }
  1001. case CROSSBOW:
  1002. {
  1003. hitted = doAttackHitByCrossBow(attack, target, timeAtk, reuse);
  1004. break;
  1005. }
  1006. case POLE:
  1007. {
  1008. hitted = doAttackHitByPole(attack, target, timeToHit);
  1009. break;
  1010. }
  1011. case FIST:
  1012. {
  1013. if (!isPlayer())
  1014. {
  1015. hitted = doAttackHitSimple(attack, target, timeToHit);
  1016. break;
  1017. }
  1018. }
  1019. case DUAL:
  1020. case DUALFIST:
  1021. case DUALDAGGER:
  1022. {
  1023. hitted = doAttackHitByDual(attack, target, timeToHit);
  1024. break;
  1025. }
  1026. default:
  1027. {
  1028. hitted = doAttackHitSimple(attack, target, timeToHit);
  1029. break;
  1030. }
  1031. }
  1032. // Flag the attacker if it's a L2PcInstance outside a PvP area
  1033. final L2PcInstance player = getActingPlayer();
  1034. if (player != null)
  1035. {
  1036. AttackStanceTaskManager.getInstance().addAttackStanceTask(player);
  1037. if (player.getSummon() != target)
  1038. {
  1039. player.updatePvPStatus(target);
  1040. }
  1041. }
  1042. // Check if hit isn't missed
  1043. if (!hitted)
  1044. {
  1045. abortAttack(); // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  1046. }
  1047. else
  1048. {
  1049. // If we didn't miss the hit, discharge the shoulshots, if any
  1050. setChargedShot(ShotType.SOULSHOTS, false);
  1051. if (player != null)
  1052. {
  1053. if (player.isCursedWeaponEquipped())
  1054. {
  1055. // If hit by a cursed weapon, CP is reduced to 0
  1056. if (!target.isInvul())
  1057. {
  1058. target.setCurrentCp(0);
  1059. }
  1060. }
  1061. else if (player.isHero())
  1062. {
  1063. // If a cursed weapon is hit by a Hero, CP is reduced to 0
  1064. if (target.isPlayer() && target.getActingPlayer().isCursedWeaponEquipped())
  1065. {
  1066. target.setCurrentCp(0);
  1067. }
  1068. }
  1069. }
  1070. }
  1071. // If the Server->Client packet Attack contains at least 1 hit, send the Server->Client packet Attack
  1072. // to the L2Character AND to all L2PcInstance in the _KnownPlayers of the L2Character
  1073. if (attack.hasHits())
  1074. {
  1075. broadcastPacket(attack);
  1076. }
  1077. // Notify AI with EVT_READY_TO_ACT
  1078. ThreadPoolManager.getInstance().scheduleAi(new NotifyAITask(this, CtrlEvent.EVT_READY_TO_ACT), timeAtk + reuse);
  1079. }
  1080. finally
  1081. {
  1082. _attackLock.unlockWrite(stamp);
  1083. }
  1084. }
  1085. /**
  1086. * Launch a Bow attack.<br>
  1087. * <B><U>Actions</U>:</B>
  1088. * <ul>
  1089. * <li>Calculate if hit is missed or not</li>
  1090. * <li>Consume arrows</li>
  1091. * <li>If hit isn't missed, calculate if shield defense is efficient</li>
  1092. * <li>If hit isn't missed, calculate if hit is critical</li>
  1093. * <li>If hit isn't missed, calculate physical damages</li>
  1094. * <li>If the L2Character is a L2PcInstance, Send a Server->Client packet SetupGauge</li>
  1095. * <li>Create a new hit task with Medium priority</li>
  1096. * <li>Calculate and set the disable delay of the bow in function of the Attack Speed</li>
  1097. * <li>Add this hit to the Server-Client packet Attack</li>
  1098. * </ul>
  1099. * @param attack Server->Client packet Attack in which the hit will be added
  1100. * @param target The L2Character targeted
  1101. * @param sAtk The Attack Speed of the attacker
  1102. * @param reuse
  1103. * @return True if the hit isn't missed
  1104. */
  1105. private boolean doAttackHitByBow(Attack attack, L2Character target, int sAtk, int reuse)
  1106. {
  1107. int damage1 = 0;
  1108. byte shld1 = 0;
  1109. boolean crit1 = false;
  1110. // Calculate if hit is missed or not
  1111. boolean miss1 = Formulas.calcHitMiss(this, target);
  1112. // Consume arrows
  1113. reduceArrowCount(false);
  1114. _move = null;
  1115. // Check if hit isn't missed
  1116. if (!miss1)
  1117. {
  1118. // Calculate if shield defense is efficient
  1119. shld1 = Formulas.calcShldUse(this, target);
  1120. // Calculate if hit is critical
  1121. crit1 = Formulas.calcCrit(getStat().getCriticalHit(target, null), false, target);
  1122. // Calculate physical damages
  1123. damage1 = (int) Formulas.calcPhysDam(this, target, null, shld1, crit1, attack.hasSoulshot());
  1124. // Bows Ranged Damage Formula (Damage gradually decreases when 60% or lower than full hit range, and increases when 60% or higher).
  1125. // full hit range is 500 which is the base bow range, and the 60% of this is 800.
  1126. damage1 *= (calculateDistance(target, true, false) / 4000) + 0.8;
  1127. }
  1128. // Check if the L2Character is a L2PcInstance
  1129. if (isPlayer())
  1130. {
  1131. sendPacket(new SetupGauge(SetupGauge.RED, sAtk + reuse));
  1132. }
  1133. // Create a new hit task with Medium priority
  1134. ThreadPoolManager.getInstance().scheduleAi(new HitTask(this, target, damage1, crit1, miss1, attack.hasSoulshot(), shld1), sAtk);
  1135. // Calculate and set the disable delay of the bow in function of the Attack Speed
  1136. _disableBowAttackEndTime = ((sAtk + reuse) / GameTimeController.MILLIS_IN_TICK) + GameTimeController.getInstance().getGameTicks();
  1137. // Add this hit to the Server-Client packet Attack
  1138. attack.addHit(target, damage1, miss1, crit1, shld1);
  1139. // Return true if hit isn't missed
  1140. return !miss1;
  1141. }
  1142. /**
  1143. * Launch a CrossBow attack.<br>
  1144. * <B><U>Actions</U>:</B>
  1145. * <ul>
  1146. * <li>Calculate if hit is missed or not</li>
  1147. * <li>Consume bolts</li>
  1148. * <li>If hit isn't missed, calculate if shield defense is efficient</li>
  1149. * <li>If hit isn't missed, calculate if hit is critical</li>
  1150. * <li>If hit isn't missed, calculate physical damages</li>
  1151. * <li>If the L2Character is a L2PcInstance, Send a Server->Client packet SetupGauge</li>
  1152. * <li>Create a new hit task with Medium priority</li>
  1153. * <li>Calculate and set the disable delay of the crossbow in function of the Attack Speed</li>
  1154. * <li>Add this hit to the Server-Client packet Attack</li>
  1155. * @param attack Server->Client packet Attack in which the hit will be added
  1156. * @param target The L2Character targeted
  1157. * @param sAtk The Attack Speed of the attacker
  1158. * @param reuse
  1159. * @return True if the hit isn't missed
  1160. */
  1161. private boolean doAttackHitByCrossBow(Attack attack, L2Character target, int sAtk, int reuse)
  1162. {
  1163. int damage1 = 0;
  1164. byte shld1 = 0;
  1165. boolean crit1 = false;
  1166. // Calculate if hit is missed or not
  1167. boolean miss1 = Formulas.calcHitMiss(this, target);
  1168. // Consume bolts
  1169. reduceArrowCount(true);
  1170. _move = null;
  1171. // Check if hit isn't missed
  1172. if (!miss1)
  1173. {
  1174. // Calculate if shield defense is efficient
  1175. shld1 = Formulas.calcShldUse(this, target);
  1176. // Calculate if hit is critical
  1177. crit1 = Formulas.calcCrit(getStat().getCriticalHit(target, null), false, target);
  1178. // Calculate physical damages
  1179. damage1 = (int) Formulas.calcPhysDam(this, target, null, shld1, crit1, attack.hasSoulshot());
  1180. }
  1181. // Check if the L2Character is a L2PcInstance
  1182. if (isPlayer())
  1183. {
  1184. // Send a system message
  1185. sendPacket(SystemMessageId.CROSSBOW_PREPARING_TO_FIRE);
  1186. // Send a Server->Client packet SetupGauge
  1187. SetupGauge sg = new SetupGauge(SetupGauge.RED, sAtk + reuse);
  1188. sendPacket(sg);
  1189. }
  1190. // Create a new hit task with Medium priority
  1191. ThreadPoolManager.getInstance().scheduleAi(new HitTask(this, target, damage1, crit1, miss1, attack.hasSoulshot(), shld1), sAtk);
  1192. // Calculate and set the disable delay of the bow in function of the Attack Speed
  1193. _disableCrossBowAttackEndTime = ((sAtk + reuse) / GameTimeController.MILLIS_IN_TICK) + GameTimeController.getInstance().getGameTicks();
  1194. // Add this hit to the Server-Client packet Attack
  1195. attack.addHit(target, damage1, miss1, crit1, shld1);
  1196. // Return true if hit isn't missed
  1197. return !miss1;
  1198. }
  1199. /**
  1200. * Launch a Dual attack.<br>
  1201. * <B><U>Actions</U>:</B>
  1202. * <ul>
  1203. * <li>Calculate if hits are missed or not</li>
  1204. * <li>If hits aren't missed, calculate if shield defense is efficient</li>
  1205. * <li>If hits aren't missed, calculate if hit is critical</li>
  1206. * <li>If hits aren't missed, calculate physical damages</li>
  1207. * <li>Create 2 new hit tasks with Medium priority</li>
  1208. * <li>Add those hits to the Server-Client packet Attack</li>
  1209. * </ul>
  1210. * @param attack Server->Client packet Attack in which the hit will be added
  1211. * @param target The L2Character targeted
  1212. * @param sAtk
  1213. * @return True if hit 1 or hit 2 isn't missed
  1214. */
  1215. private boolean doAttackHitByDual(Attack attack, L2Character target, int sAtk)
  1216. {
  1217. int damage1 = 0;
  1218. int damage2 = 0;
  1219. byte shld1 = 0;
  1220. byte shld2 = 0;
  1221. boolean crit1 = false;
  1222. boolean crit2 = false;
  1223. // Calculate if hits are missed or not
  1224. boolean miss1 = Formulas.calcHitMiss(this, target);
  1225. boolean miss2 = Formulas.calcHitMiss(this, target);
  1226. // Check if hit 1 isn't missed
  1227. if (!miss1)
  1228. {
  1229. // Calculate if shield defense is efficient against hit 1
  1230. shld1 = Formulas.calcShldUse(this, target);
  1231. // Calculate if hit 1 is critical
  1232. crit1 = Formulas.calcCrit(getStat().getCriticalHit(target, null), false, target);
  1233. // Calculate physical damages of hit 1
  1234. damage1 = (int) Formulas.calcPhysDam(this, target, null, shld1, crit1, attack.hasSoulshot());
  1235. damage1 /= 2;
  1236. }
  1237. // Check if hit 2 isn't missed
  1238. if (!miss2)
  1239. {
  1240. // Calculate if shield defense is efficient against hit 2
  1241. shld2 = Formulas.calcShldUse(this, target);
  1242. // Calculate if hit 2 is critical
  1243. crit2 = Formulas.calcCrit(getStat().getCriticalHit(target, null), false, target);
  1244. // Calculate physical damages of hit 2
  1245. damage2 = (int) Formulas.calcPhysDam(this, target, null, shld2, crit2, attack.hasSoulshot());
  1246. damage2 /= 2;
  1247. }
  1248. // Create a new hit task with Medium priority for hit 1
  1249. ThreadPoolManager.getInstance().scheduleAi(new HitTask(this, target, damage1, crit1, miss1, attack.hasSoulshot(), shld1), sAtk / 2);
  1250. // Create a new hit task with Medium priority for hit 2 with a higher delay
  1251. ThreadPoolManager.getInstance().scheduleAi(new HitTask(this, target, damage2, crit2, miss2, attack.hasSoulshot(), shld2), sAtk);
  1252. // Add those hits to the Server-Client packet Attack
  1253. attack.addHit(target, damage1, miss1, crit1, shld1);
  1254. attack.addHit(target, damage2, miss2, crit2, shld2);
  1255. // Return true if hit 1 or hit 2 isn't missed
  1256. return (!miss1 || !miss2);
  1257. }
  1258. /**
  1259. * Launch a Pole attack.<br>
  1260. * <B><U>Actions</U>:</B>
  1261. * <ul>
  1262. * <li>Get all visible objects in a spherical area near the L2Character to obtain possible targets</li>
  1263. * <li>If possible target is the L2Character targeted, launch a simple attack against it</li>
  1264. * <li>If possible target isn't the L2Character targeted but is attackable, launch a simple attack against it</li>
  1265. * </ul>
  1266. * @param attack Server->Client packet Attack in which the hit will be added
  1267. * @param target
  1268. * @param sAtk
  1269. * @return True if one hit isn't missed
  1270. */
  1271. private boolean doAttackHitByPole(Attack attack, L2Character target, int sAtk)
  1272. {
  1273. // double angleChar;
  1274. int maxRadius = getStat().getPhysicalAttackRange();
  1275. int maxAngleDiff = getStat().getPhysicalAttackAngle();
  1276. // o1 x: 83420 y: 148158 (Giran)
  1277. // o2 x: 83379 y: 148081 (Giran)
  1278. // dx = -41
  1279. // dy = -77
  1280. // distance between o1 and o2 = 87.24
  1281. // arctan2 = -120 (240) degree (excel arctan2(dx, dy); java arctan2(dy, dx))
  1282. //
  1283. // o2
  1284. //
  1285. // o1 ----- (heading)
  1286. // In the diagram above:
  1287. // o1 has a heading of 0/360 degree from horizontal (facing East)
  1288. // Degree of o2 in respect to o1 = -120 (240) degree
  1289. //
  1290. // o2 / (heading)
  1291. // /
  1292. // o1
  1293. // In the diagram above
  1294. // o1 has a heading of -80 (280) degree from horizontal (facing north east)
  1295. // Degree of o2 in respect to 01 = -40 (320) degree
  1296. // Get char's heading degree
  1297. // angleChar = Util.convertHeadingToDegree(getHeading());
  1298. // H5 Changes: without Polearm Mastery (skill 216) max simultaneous attacks is 3 (1 by default + 2 in skill 3599).
  1299. int attackRandomCountMax = (int) getStat().calcStat(Stats.ATTACK_COUNT_MAX, 1, null, null);
  1300. int attackcount = 0;
  1301. // if (angleChar <= 0) angleChar += 360;
  1302. boolean hitted = doAttackHitSimple(attack, target, 100, sAtk);
  1303. double attackpercent = 85;
  1304. L2Character temp;
  1305. Collection<L2Object> objs = getKnownList().getKnownObjects().values();
  1306. for (L2Object obj : objs)
  1307. {
  1308. if (obj == target)
  1309. {
  1310. continue; // do not hit twice
  1311. }
  1312. // Check if the L2Object is a L2Character
  1313. if (obj instanceof L2Character)
  1314. {
  1315. if (obj.isPet() && isPlayer() && (((L2PetInstance) obj).getOwner() == getActingPlayer()))
  1316. {
  1317. continue;
  1318. }
  1319. if (!Util.checkIfInRange(maxRadius, this, obj, false))
  1320. {
  1321. continue;
  1322. }
  1323. // otherwise hit too high/low. 650 because mob z coord
  1324. // sometimes wrong on hills
  1325. if (Math.abs(obj.getZ() - getZ()) > 650)
  1326. {
  1327. continue;
  1328. }
  1329. if (!isFacing(obj, maxAngleDiff))
  1330. {
  1331. continue;
  1332. }
  1333. if (isAttackable() && obj.isPlayer() && getTarget().isAttackable())
  1334. {
  1335. continue;
  1336. }
  1337. if (isAttackable() && obj.isAttackable() && !((L2Attackable) this).isChaos())
  1338. {
  1339. continue;
  1340. }
  1341. temp = (L2Character) obj;
  1342. // Launch a simple attack against the L2Character targeted
  1343. if (!temp.isAlikeDead())
  1344. {
  1345. if ((temp == getAI().getAttackTarget()) || temp.isAutoAttackable(this))
  1346. {
  1347. hitted |= doAttackHitSimple(attack, temp, attackpercent, sAtk);
  1348. attackpercent /= 1.15;
  1349. attackcount++;
  1350. if (attackcount > attackRandomCountMax)
  1351. {
  1352. break;
  1353. }
  1354. }
  1355. }
  1356. }
  1357. }
  1358. // Return true if one hit isn't missed
  1359. return hitted;
  1360. }
  1361. /**
  1362. * Launch a simple attack.<br>
  1363. * <B><U>Actions</U>:</B>
  1364. * <ul>
  1365. * <li>Calculate if hit is missed or not</li>
  1366. * <li>If hit isn't missed, calculate if shield defense is efficient</li>
  1367. * <li>If hit isn't missed, calculate if hit is critical</li>
  1368. * <li>If hit isn't missed, calculate physical damages</li>
  1369. * <li>Create a new hit task with Medium priority</li>
  1370. * <li>Add this hit to the Server-Client packet Attack</li>
  1371. * </ul>
  1372. * @param attack Server->Client packet Attack in which the hit will be added
  1373. * @param target The L2Character targeted
  1374. * @param sAtk
  1375. * @return True if the hit isn't missed
  1376. */
  1377. private boolean doAttackHitSimple(Attack attack, L2Character target, int sAtk)
  1378. {
  1379. return doAttackHitSimple(attack, target, 100, sAtk);
  1380. }
  1381. private boolean doAttackHitSimple(Attack attack, L2Character target, double attackpercent, int sAtk)
  1382. {
  1383. int damage1 = 0;
  1384. byte shld1 = 0;
  1385. boolean crit1 = false;
  1386. // Calculate if hit is missed or not
  1387. boolean miss1 = Formulas.calcHitMiss(this, target);
  1388. // Check if hit isn't missed
  1389. if (!miss1)
  1390. {
  1391. // Calculate if shield defense is efficient
  1392. shld1 = Formulas.calcShldUse(this, target);
  1393. // Calculate if hit is critical
  1394. crit1 = Formulas.calcCrit(getStat().getCriticalHit(target, null), false, target);
  1395. // Calculate physical damages
  1396. damage1 = (int) Formulas.calcPhysDam(this, target, null, shld1, crit1, attack.hasSoulshot());
  1397. if (attackpercent != 100)
  1398. {
  1399. damage1 = (int) ((damage1 * attackpercent) / 100);
  1400. }
  1401. }
  1402. // Create a new hit task with Medium priority
  1403. ThreadPoolManager.getInstance().scheduleAi(new HitTask(this, target, damage1, crit1, miss1, attack.hasSoulshot(), shld1), sAtk);
  1404. // Add this hit to the Server-Client packet Attack
  1405. attack.addHit(target, damage1, miss1, crit1, shld1);
  1406. // Return true if hit isn't missed
  1407. return !miss1;
  1408. }
  1409. /**
  1410. * Manage the casting task (casting and interrupt time, re-use delay...) and display the casting bar and animation on client.<br>
  1411. * <B><U>Actions</U>:</B>
  1412. * <ul>
  1413. * <li>Verify the possibility of the the cast : skill is a spell, caster isn't muted...</li>
  1414. * <li>Get the list of all targets (ex : area effects) and define the L2Charcater targeted (its stats will be used in calculation)</li>
  1415. * <li>Calculate the casting time (base + modifier of MAtkSpd), interrupt time and re-use delay</li>
  1416. * <li>Send a Server->Client packet MagicSkillUser (to display casting animation), a packet SetupGauge (to display casting bar) and a system message</li>
  1417. * <li>Disable all skills during the casting time (create a task EnableAllSkills)</li>
  1418. * <li>Disable the skill during the re-use delay (create a task EnableSkill)</li>
  1419. * <li>Create a task MagicUseTask (that will call method onMagicUseTimer) to launch the Magic Skill at the end of the casting time</li>
  1420. * </ul>
  1421. * @param skill The L2Skill to use
  1422. */
  1423. public void doCast(Skill skill)
  1424. {
  1425. beginCast(skill, false);
  1426. }
  1427. public void doSimultaneousCast(Skill skill)
  1428. {
  1429. beginCast(skill, true);
  1430. }
  1431. public void doCast(Skill skill, L2Character target, L2Object[] targets)
  1432. {
  1433. if (!checkDoCastConditions(skill))
  1434. {
  1435. setIsCastingNow(false);
  1436. return;
  1437. }
  1438. // Override casting type
  1439. if (skill.isSimultaneousCast())
  1440. {
  1441. doSimultaneousCast(skill, target, targets);
  1442. return;
  1443. }
  1444. stopEffectsOnAction();
  1445. // Recharge AutoSoulShot
  1446. // this method should not used with L2Playable
  1447. beginCast(skill, false, target, targets);
  1448. }
  1449. public void doSimultaneousCast(Skill skill, L2Character target, L2Object[] targets)
  1450. {
  1451. if (!checkDoCastConditions(skill))
  1452. {
  1453. setIsCastingSimultaneouslyNow(false);
  1454. return;
  1455. }
  1456. stopEffectsOnAction();
  1457. beginCast(skill, true, target, targets);
  1458. }
  1459. private void beginCast(Skill skill, boolean simultaneously)
  1460. {
  1461. if (!checkDoCastConditions(skill))
  1462. {
  1463. if (simultaneously)
  1464. {
  1465. setIsCastingSimultaneouslyNow(false);
  1466. }
  1467. else
  1468. {
  1469. setIsCastingNow(false);
  1470. }
  1471. if (isPlayer())
  1472. {
  1473. getAI().setIntention(AI_INTENTION_ACTIVE);
  1474. }
  1475. return;
  1476. }
  1477. // Override casting type
  1478. if (skill.isSimultaneousCast() && !simultaneously)
  1479. {
  1480. simultaneously = true;
  1481. }
  1482. stopEffectsOnAction();
  1483. // Set the target of the skill in function of Skill Type and Target Type
  1484. L2Character target = null;
  1485. // Get all possible targets of the skill in a table in function of the skill target type
  1486. L2Object[] targets = skill.getTargetList(this);
  1487. boolean doit = false;
  1488. // AURA skills should always be using caster as target
  1489. switch (skill.getTargetType())
  1490. {
  1491. case AREA_SUMMON: // We need it to correct facing
  1492. target = getSummon();
  1493. break;
  1494. case AURA:
  1495. case AURA_CORPSE_MOB:
  1496. case FRONT_AURA:
  1497. case BEHIND_AURA:
  1498. case GROUND:
  1499. target = this;
  1500. break;
  1501. case SELF:
  1502. case PET:
  1503. case SERVITOR:
  1504. case SUMMON:
  1505. case OWNER_PET:
  1506. case PARTY:
  1507. case CLAN:
  1508. case PARTY_CLAN:
  1509. case COMMAND_CHANNEL:
  1510. doit = true;
  1511. default:
  1512. if (targets.length == 0)
  1513. {
  1514. if (simultaneously)
  1515. {
  1516. setIsCastingSimultaneouslyNow(false);
  1517. }
  1518. else
  1519. {
  1520. setIsCastingNow(false);
  1521. }
  1522. // Send a Server->Client packet ActionFailed to the L2PcInstance
  1523. if (isPlayer())
  1524. {
  1525. sendPacket(ActionFailed.STATIC_PACKET);
  1526. getAI().setIntention(AI_INTENTION_ACTIVE);
  1527. }
  1528. return;
  1529. }
  1530. if ((skill.isContinuous() && !skill.isDebuff()) || skill.hasEffectType(L2EffectType.CPHEAL, L2EffectType.HEAL))
  1531. {
  1532. doit = true;
  1533. }
  1534. if (doit)
  1535. {
  1536. target = (L2Character) targets[0];
  1537. }
  1538. else
  1539. {
  1540. target = (L2Character) getTarget();
  1541. }
  1542. }
  1543. beginCast(skill, simultaneously, target, targets);
  1544. }
  1545. private void beginCast(Skill skill, boolean simultaneously, L2Character target, L2Object[] targets)
  1546. {
  1547. if (target == null)
  1548. {
  1549. if (simultaneously)
  1550. {
  1551. setIsCastingSimultaneouslyNow(false);
  1552. }
  1553. else
  1554. {
  1555. setIsCastingNow(false);
  1556. }
  1557. if (isPlayer())
  1558. {
  1559. sendPacket(ActionFailed.STATIC_PACKET);
  1560. getAI().setIntention(AI_INTENTION_ACTIVE);
  1561. }
  1562. return;
  1563. }
  1564. final TerminateReturn term = EventDispatcher.getInstance().notifyEvent(new OnCreatureSkillUse(this, skill, simultaneously, target, targets), this, TerminateReturn.class);
  1565. if ((term != null) && term.terminate())
  1566. {
  1567. if (simultaneously)
  1568. {
  1569. setIsCastingSimultaneouslyNow(false);
  1570. }
  1571. else
  1572. {
  1573. setIsCastingNow(false);
  1574. }
  1575. if (isPlayer())
  1576. {
  1577. sendPacket(ActionFailed.STATIC_PACKET);
  1578. getAI().setIntention(AI_INTENTION_ACTIVE);
  1579. }
  1580. return;
  1581. }
  1582. // TODO: Unhardcode using event listeners!
  1583. if (skill.hasEffectType(L2EffectType.RESURRECTION))
  1584. {
  1585. if (isResurrectionBlocked() || target.isResurrectionBlocked())
  1586. {
  1587. sendPacket(SystemMessageId.REJECT_RESURRECTION); // Reject resurrection
  1588. target.sendPacket(SystemMessageId.REJECT_RESURRECTION); // Reject resurrection
  1589. if (simultaneously)
  1590. {
  1591. setIsCastingSimultaneouslyNow(false);
  1592. }
  1593. else
  1594. {
  1595. setIsCastingNow(false);
  1596. }
  1597. if (isPlayer())
  1598. {
  1599. getAI().setIntention(AI_INTENTION_ACTIVE);
  1600. sendPacket(ActionFailed.STATIC_PACKET);
  1601. }
  1602. return;
  1603. }
  1604. }
  1605. // Get the Identifier of the skill
  1606. int magicId = skill.getId();
  1607. // Get the Base Casting Time of the Skills.
  1608. int skillTime = (skill.getHitTime() + skill.getCoolTime());
  1609. if (!skill.isChanneling() || (skill.getChannelingSkillId() == 0))
  1610. {
  1611. // Calculate the Casting Time of the "Non-Static" Skills (with caster PAtk/MAtkSpd).
  1612. if (!skill.isStatic())
  1613. {
  1614. skillTime = Formulas.calcAtkSpd(this, skill, skillTime);
  1615. }
  1616. // Calculate the Casting Time of Magic Skills (reduced in 40% if using SPS/BSPS)
  1617. if (skill.isMagic() && (isChargedShot(ShotType.SPIRITSHOTS) || isChargedShot(ShotType.BLESSED_SPIRITSHOTS)))
  1618. {
  1619. skillTime = (int) (0.6 * skillTime);
  1620. }
  1621. }
  1622. // Avoid broken Casting Animation.
  1623. // Client can't handle less than 550ms Casting Animation in Magic Skills with more than 550ms base.
  1624. if (skill.isMagic() && ((skill.getHitTime() + skill.getCoolTime()) > 550) && (skillTime < 550))
  1625. {
  1626. skillTime = 550;
  1627. }
  1628. // Client can't handle less than 500ms Casting Animation in Physical Skills with 500ms base or more.
  1629. else if (!skill.isStatic() && ((skill.getHitTime() + skill.getCoolTime()) >= 500) && (skillTime < 500))
  1630. {
  1631. skillTime = 500;
  1632. }
  1633. // queue herbs and potions
  1634. if (isCastingSimultaneouslyNow() && simultaneously)
  1635. {
  1636. ThreadPoolManager.getInstance().scheduleAi(new UsePotionTask(this, skill), 100);
  1637. return;
  1638. }
  1639. // Set the _castInterruptTime and casting status (L2PcInstance already has this true)
  1640. if (simultaneously)
  1641. {
  1642. setIsCastingSimultaneouslyNow(true);
  1643. }
  1644. else
  1645. {
  1646. setIsCastingNow(true);
  1647. }
  1648. if (!simultaneously)
  1649. {
  1650. _castInterruptTime = -2 + GameTimeController.getInstance().getGameTicks() + (skillTime / GameTimeController.MILLIS_IN_TICK);
  1651. setLastSkillCast(skill);
  1652. }
  1653. else
  1654. {
  1655. setLastSimultaneousSkillCast(skill);
  1656. }
  1657. // Calculate the Reuse Time of the Skill
  1658. int reuseDelay;
  1659. if (skill.isStaticReuse() || skill.isStatic())
  1660. {
  1661. reuseDelay = (skill.getReuseDelay());
  1662. }
  1663. else if (skill.isMagic())
  1664. {
  1665. reuseDelay = (int) (skill.getReuseDelay() * calcStat(Stats.MAGIC_REUSE_RATE, 1, null, null));
  1666. }
  1667. else if (skill.isPhysical())
  1668. {
  1669. reuseDelay = (int) (skill.getReuseDelay() * calcStat(Stats.P_REUSE, 1, null, null));
  1670. }
  1671. else
  1672. {
  1673. reuseDelay = (int) (skill.getReuseDelay() * calcStat(Stats.DANCE_REUSE, 1, null, null));
  1674. }
  1675. boolean skillMastery = Formulas.calcSkillMastery(this, skill);
  1676. // Skill reuse check
  1677. if ((reuseDelay > 30000) && !skillMastery)
  1678. {
  1679. addTimeStamp(skill, reuseDelay);
  1680. }
  1681. // Check if this skill consume mp on start casting
  1682. int initmpcons = getStat().getMpInitialConsume(skill);
  1683. if (initmpcons > 0)
  1684. {
  1685. getStatus().reduceMp(initmpcons);
  1686. StatusUpdate su = new StatusUpdate(this);
  1687. su.addAttribute(StatusUpdate.CUR_MP, (int) getCurrentMp());
  1688. sendPacket(su);
  1689. }
  1690. // Disable the skill during the re-use delay and create a task EnableSkill with Medium priority to enable it at the end of the re-use delay
  1691. if (reuseDelay > 10)
  1692. {
  1693. if (skillMastery)
  1694. {
  1695. reuseDelay = 100;
  1696. if (getActingPlayer() != null)
  1697. {
  1698. getActingPlayer().sendPacket(SystemMessageId.SKILL_READY_TO_USE_AGAIN);
  1699. }
  1700. }
  1701. disableSkill(skill, reuseDelay);
  1702. }
  1703. // Make sure that char is facing selected target
  1704. if (target != this)
  1705. {
  1706. setHeading(Util.calculateHeadingFrom(this, target));
  1707. broadcastPacket(new ExRotation(getObjectId(), getHeading()));
  1708. }
  1709. if (isPlayable())
  1710. {
  1711. if (skill.getItemConsumeId() > 0)
  1712. {
  1713. if (!destroyItemByItemId("Consume", skill.getItemConsumeId(), skill.getItemConsumeCount(), null, true))
  1714. {
  1715. getActingPlayer().sendPacket(SystemMessageId.NOT_ENOUGH_ITEMS);
  1716. abortCast();
  1717. return;
  1718. }
  1719. }
  1720. // reduce talisman mana on skill use
  1721. if ((skill.getReferenceItemId() > 0) && (ItemTable.getInstance().getTemplate(skill.getReferenceItemId()).getBodyPart() == L2Item.SLOT_DECO))
  1722. {
  1723. for (L2ItemInstance item : getInventory().getItemsByItemId(skill.getReferenceItemId()))
  1724. {
  1725. if (item.isEquipped())
  1726. {
  1727. if (item.getMana() < item.useSkillDisTime())
  1728. {
  1729. abortCast();
  1730. return;
  1731. }
  1732. item.decreaseMana(false, item.useSkillDisTime());
  1733. break;
  1734. }
  1735. }
  1736. }
  1737. }
  1738. // Send a Server->Client packet MagicSkillUser with target, displayId, level, skillTime, reuseDelay
  1739. // to the L2Character AND to all L2PcInstance in the _KnownPlayers of the L2Character
  1740. broadcastPacket(new MagicSkillUse(this, target, skill.getDisplayId(), skill.getDisplayLevel(), skillTime, reuseDelay));
  1741. // Send a system message to the player.
  1742. if (isPlayer() && !skill.isAbnormalInstant())
  1743. {
  1744. SystemMessage sm = null;
  1745. switch (magicId)
  1746. {
  1747. case 1312: // Fishing
  1748. {
  1749. // Done in L2PcInstance.startFishing()
  1750. break;
  1751. }
  1752. case 2046: // Wolf Collar
  1753. {
  1754. sm = SystemMessage.getSystemMessage(SystemMessageId.SUMMON_A_PET);
  1755. break;
  1756. }
  1757. default:
  1758. {
  1759. sm = SystemMessage.getSystemMessage(SystemMessageId.USE_S1);
  1760. sm.addSkillName(skill);
  1761. break;
  1762. }
  1763. }
  1764. sendPacket(sm);
  1765. }
  1766. if (skill.hasEffects(EffectScope.START))
  1767. {
  1768. skill.applyEffectScope(EffectScope.START, new BuffInfo(this, target, skill), true, false);
  1769. }
  1770. // Before start AI Cast Broadcast Fly Effect is Need
  1771. if (skill.getFlyType() != null)
  1772. {
  1773. ThreadPoolManager.getInstance().scheduleEffect(new FlyToLocationTask(this, target, skill), 50);
  1774. }
  1775. MagicUseTask mut = new MagicUseTask(this, targets, skill, skillTime, simultaneously);
  1776. // launch the magic in skillTime milliseconds
  1777. if (skillTime > 0)
  1778. {
  1779. // Send a Server->Client packet SetupGauge with the color of the gauge and the casting time
  1780. if (isPlayer() && !simultaneously)
  1781. {
  1782. sendPacket(new SetupGauge(SetupGauge.BLUE, skillTime));
  1783. }
  1784. if (skill.isChanneling() && (skill.getChannelingSkillId() > 0))
  1785. {
  1786. getSkillChannelizer().startChanneling(skill);
  1787. }
  1788. if (simultaneously)
  1789. {
  1790. Future<?> future = _skillCast2;
  1791. if (future != null)
  1792. {
  1793. future.cancel(true);
  1794. _skillCast2 = null;
  1795. }
  1796. // Create a task MagicUseTask to launch the MagicSkill at the end of the casting time (skillTime)
  1797. // For client animation reasons (party buffs especially) 400 ms before!
  1798. _skillCast2 = ThreadPoolManager.getInstance().scheduleEffect(mut, skillTime - 400);
  1799. }
  1800. else
  1801. {
  1802. Future<?> future = _skillCast;
  1803. if (future != null)
  1804. {
  1805. future.cancel(true);
  1806. _skillCast = null;
  1807. }
  1808. // Create a task MagicUseTask to launch the MagicSkill at the end of the casting time (skillTime)
  1809. // For client animation reasons (party buffs especially) 400 ms before!
  1810. _skillCast = ThreadPoolManager.getInstance().scheduleEffect(mut, skillTime - 400);
  1811. }
  1812. }
  1813. else
  1814. {
  1815. mut.setSkillTime(0);
  1816. onMagicLaunchedTimer(mut);
  1817. }
  1818. }
  1819. /**
  1820. * Check if casting of skill is possible
  1821. * @param skill
  1822. * @return True if casting is possible
  1823. */
  1824. public boolean checkDoCastConditions(Skill skill)
  1825. {
  1826. if ((skill == null) || isSkillDisabled(skill) || ((skill.getFlyType() == FlyType.CHARGE) && isMovementDisabled()))
  1827. {
  1828. // Send a Server->Client packet ActionFailed to the L2PcInstance
  1829. sendPacket(ActionFailed.STATIC_PACKET);
  1830. return false;
  1831. }
  1832. // Check if the caster has enough MP
  1833. if (getCurrentMp() < (getStat().getMpConsume(skill) + getStat().getMpInitialConsume(skill)))
  1834. {
  1835. // Send a System Message to the caster
  1836. sendPacket(SystemMessageId.NOT_ENOUGH_MP);
  1837. // Send a Server->Client packet ActionFailed to the L2PcInstance
  1838. sendPacket(ActionFailed.STATIC_PACKET);
  1839. return false;
  1840. }
  1841. // Check if the caster has enough HP
  1842. if (getCurrentHp() <= skill.getHpConsume())
  1843. {
  1844. // Send a System Message to the caster
  1845. sendPacket(SystemMessageId.NOT_ENOUGH_HP);
  1846. // Send a Server->Client packet ActionFailed to the L2PcInstance
  1847. sendPacket(ActionFailed.STATIC_PACKET);
  1848. return false;
  1849. }
  1850. // Skill mute checks.
  1851. if (!skill.isStatic())
  1852. {
  1853. // Check if the skill is a magic spell and if the L2Character is not muted
  1854. if (skill.isMagic())
  1855. {
  1856. if (isMuted())
  1857. {
  1858. // Send a Server->Client packet ActionFailed to the L2PcInstance
  1859. sendPacket(ActionFailed.STATIC_PACKET);
  1860. return false;
  1861. }
  1862. }
  1863. else
  1864. {
  1865. // Check if the skill is physical and if the L2Character is not physical_muted
  1866. if (isPhysicalMuted())
  1867. {
  1868. // Send a Server->Client packet ActionFailed to the L2PcInstance
  1869. sendPacket(ActionFailed.STATIC_PACKET);
  1870. return false;
  1871. }
  1872. }
  1873. }
  1874. // prevent casting signets to peace zone
  1875. if (skill.isChanneling() && (skill.getChannelingSkillId() > 0))
  1876. {
  1877. L2WorldRegion region = getWorldRegion();
  1878. if (region == null)
  1879. {
  1880. return false;
  1881. }
  1882. boolean canCast = true;
  1883. if ((skill.getTargetType() == L2TargetType.GROUND) && isPlayer())
  1884. {
  1885. Location wp = getActingPlayer().getCurrentSkillWorldPosition();
  1886. if (!region.checkEffectRangeInsidePeaceZone(skill, wp.getX(), wp.getY(), wp.getZ()))
  1887. {
  1888. canCast = false;
  1889. }
  1890. }
  1891. else if (!region.checkEffectRangeInsidePeaceZone(skill, getX(), getY(), getZ()))
  1892. {
  1893. canCast = false;
  1894. }
  1895. if (!canCast)
  1896. {
  1897. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_CANNOT_BE_USED);
  1898. sm.addSkillName(skill);
  1899. sendPacket(sm);
  1900. return false;
  1901. }
  1902. }
  1903. // Check if the caster's weapon is limited to use only its own skills
  1904. if (getActiveWeaponItem() != null)
  1905. {
  1906. L2Weapon wep = getActiveWeaponItem();
  1907. if (wep.useWeaponSkillsOnly() && !isGM() && wep.hasSkills())
  1908. {
  1909. boolean found = false;
  1910. for (SkillHolder sh : wep.getSkills())
  1911. {
  1912. if (sh.getSkillId() == skill.getId())
  1913. {
  1914. found = true;
  1915. }
  1916. }
  1917. if (!found)
  1918. {
  1919. if (getActingPlayer() != null)
  1920. {
  1921. sendPacket(SystemMessageId.WEAPON_CAN_USE_ONLY_WEAPON_SKILL);
  1922. }
  1923. return false;
  1924. }
  1925. }
  1926. }
  1927. // Check if the spell consumes an Item
  1928. // TODO: combine check and consume
  1929. if ((skill.getItemConsumeId() > 0) && (getInventory() != null))
  1930. {
  1931. // Get the L2ItemInstance consumed by the spell
  1932. L2ItemInstance requiredItems = getInventory().getItemByItemId(skill.getItemConsumeId());
  1933. // Check if the caster owns enough consumed Item to cast
  1934. if ((requiredItems == null) || (requiredItems.getCount() < skill.getItemConsumeCount()))
  1935. {
  1936. // Checked: when a summon skill failed, server show required consume item count
  1937. if (skill.hasEffectType(L2EffectType.SUMMON))
  1938. {
  1939. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.SUMMONING_SERVITOR_COSTS_S2_S1);
  1940. sm.addItemName(skill.getItemConsumeId());
  1941. sm.addInt(skill.getItemConsumeCount());
  1942. sendPacket(sm);
  1943. }
  1944. else
  1945. {
  1946. // Send a System Message to the caster
  1947. sendPacket(SystemMessageId.THERE_ARE_NOT_ENOUGH_NECESSARY_ITEMS_TO_USE_THE_SKILL);
  1948. }
  1949. return false;
  1950. }
  1951. }
  1952. return true;
  1953. }
  1954. /**
  1955. * Gets the item reuse time stamps map.
  1956. * @return the item reuse time stamps map
  1957. */
  1958. public final Map<Integer, TimeStamp> getItemReuseTimeStamps()
  1959. {
  1960. return _reuseTimeStampsItems;
  1961. }
  1962. /**
  1963. * Adds a item reuse time stamp.
  1964. * @param item the item
  1965. * @param reuse the reuse
  1966. */
  1967. public final void addTimeStampItem(L2ItemInstance item, long reuse)
  1968. {
  1969. addTimeStampItem(item, reuse, -1);
  1970. }
  1971. /**
  1972. * Adds a item reuse time stamp.<br>
  1973. * Used for restoring purposes.
  1974. * @param item the item
  1975. * @param reuse the reuse
  1976. * @param systime the system time
  1977. */
  1978. public final void addTimeStampItem(L2ItemInstance item, long reuse, long systime)
  1979. {
  1980. if (_reuseTimeStampsItems == null)
  1981. {
  1982. synchronized (this)
  1983. {
  1984. if (_reuseTimeStampsItems == null)
  1985. {
  1986. _reuseTimeStampsItems = new ConcurrentHashMap<>();
  1987. }
  1988. }
  1989. }
  1990. _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
  1991. }
  1992. /**
  1993. * Gets the item remaining reuse time for a given item object ID.
  1994. * @param itemObjId the item object ID
  1995. * @return if the item has a reuse time stamp, the remaining time, otherwise -1
  1996. */
  1997. public synchronized final long getItemRemainingReuseTime(int itemObjId)
  1998. {
  1999. final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null;
  2000. return reuseStamp != null ? reuseStamp.getRemaining() : -1;
  2001. }
  2002. /**
  2003. * Gets the item remaining reuse time for a given shared reuse item group.
  2004. * @param group the shared reuse item group
  2005. * @return if the shared reuse item group has a reuse time stamp, the remaining time, otherwise -1
  2006. */
  2007. public final long getReuseDelayOnGroup(int group)
  2008. {
  2009. if ((group > 0) && (_reuseTimeStampsItems != null))
  2010. {
  2011. for (TimeStamp ts : _reuseTimeStampsItems.values())
  2012. {
  2013. if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed())
  2014. {
  2015. return ts.getRemaining();
  2016. }
  2017. }
  2018. }
  2019. return -1;
  2020. }
  2021. /**
  2022. * Gets the skill reuse time stamps map.
  2023. * @return the skill reuse time stamps map
  2024. */
  2025. public final Map<Integer, TimeStamp> getSkillReuseTimeStamps()
  2026. {
  2027. return _reuseTimeStampsSkills;
  2028. }
  2029. /**
  2030. * Adds the skill reuse time stamp.
  2031. * @param skill the skill
  2032. * @param reuse the delay
  2033. */
  2034. public final void addTimeStamp(Skill skill, long reuse)
  2035. {
  2036. addTimeStamp(skill, reuse, -1);
  2037. }
  2038. /**
  2039. * Adds the skill reuse time stamp.<br>
  2040. * Used for restoring purposes.
  2041. * @param skill the skill
  2042. * @param reuse the reuse
  2043. * @param systime the system time
  2044. */
  2045. public final void addTimeStamp(Skill skill, long reuse, long systime)
  2046. {
  2047. if (_reuseTimeStampsSkills == null)
  2048. {
  2049. synchronized (this)
  2050. {
  2051. if (_reuseTimeStampsSkills == null)
  2052. {
  2053. _reuseTimeStampsSkills = new ConcurrentHashMap<>();
  2054. }
  2055. }
  2056. }
  2057. _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
  2058. }
  2059. /**
  2060. * Removes a skill reuse time stamp.
  2061. * @param skill the skill to remove
  2062. */
  2063. public synchronized final void removeTimeStamp(Skill skill)
  2064. {
  2065. if (_reuseTimeStampsSkills != null)
  2066. {
  2067. _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
  2068. }
  2069. }
  2070. /**
  2071. * Removes all skill reuse time stamps.
  2072. */
  2073. public synchronized final void resetTimeStamps()
  2074. {
  2075. if (_reuseTimeStampsSkills != null)
  2076. {
  2077. _reuseTimeStampsSkills.clear();
  2078. }
  2079. }
  2080. /**
  2081. * Gets the skill remaining reuse time for a given skill hash code.
  2082. * @param hashCode the skill hash code
  2083. * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
  2084. */
  2085. public synchronized final long getSkillRemainingReuseTime(int hashCode)
  2086. {
  2087. final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null;
  2088. return reuseStamp != null ? reuseStamp.getRemaining() : -1;
  2089. }
  2090. /**
  2091. * Verifies if the skill is under reuse time.
  2092. * @param hashCode the skill hash code
  2093. * @return {@code true} if the skill is under reuse time, {@code false} otherwise
  2094. */
  2095. public synchronized final boolean hasSkillReuse(int hashCode)
  2096. {
  2097. final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null;
  2098. return (reuseStamp != null) && reuseStamp.hasNotPassed();
  2099. }
  2100. /**
  2101. * Gets the skill reuse time stamp.
  2102. * @param hashCode the skill hash code
  2103. * @return if the skill has a reuse time stamp, the skill reuse time stamp, otherwise {@code null}
  2104. */
  2105. public synchronized final TimeStamp getSkillReuseTimeStamp(int hashCode)
  2106. {
  2107. return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null;
  2108. }
  2109. /**
  2110. * Gets the disabled skills map.
  2111. * @return the disabled skills map
  2112. */
  2113. public Map<Integer, Long> getDisabledSkills()
  2114. {
  2115. return _disabledSkills;
  2116. }
  2117. /**
  2118. * Enables a skill.
  2119. * @param skill the skill to enable
  2120. */
  2121. public void enableSkill(Skill skill)
  2122. {
  2123. if ((skill == null) || (_disabledSkills == null))
  2124. {
  2125. return;
  2126. }
  2127. _disabledSkills.remove(skill.getReuseHashCode());
  2128. }
  2129. /**
  2130. * Disables a skill for a given time.<br>
  2131. * If delay is lesser or equal than zero, skill will be disabled "forever".
  2132. * @param skill the skill to disable
  2133. * @param delay delay in milliseconds
  2134. */
  2135. public void disableSkill(Skill skill, long delay)
  2136. {
  2137. if (skill == null)
  2138. {
  2139. return;
  2140. }
  2141. if (_disabledSkills == null)
  2142. {
  2143. synchronized (this)
  2144. {
  2145. if (_disabledSkills == null)
  2146. {
  2147. _disabledSkills = new ConcurrentHashMap<>();
  2148. }
  2149. }
  2150. }
  2151. _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
  2152. }
  2153. /**
  2154. * Removes all the disabled skills.
  2155. */
  2156. public synchronized final void resetDisabledSkills()
  2157. {
  2158. if (_disabledSkills != null)
  2159. {
  2160. _disabledSkills.clear();
  2161. }
  2162. }
  2163. /**
  2164. * Verifies if the skill is disabled.
  2165. * @param skill the skill
  2166. * @return {@code true} if the skill is disabled, {@code false} otherwise
  2167. */
  2168. public boolean isSkillDisabled(Skill skill)
  2169. {
  2170. return (skill != null) && isSkillDisabled(skill.getReuseHashCode());
  2171. }
  2172. /**
  2173. * Verifies if the skill is disabled.
  2174. * @param hashCode the skill hash code
  2175. * @return {@code true} if the skill is disabled, {@code false} otherwise
  2176. */
  2177. public boolean isSkillDisabled(int hashCode)
  2178. {
  2179. if (isAllSkillsDisabled())
  2180. {
  2181. return true;
  2182. }
  2183. if (_disabledSkills == null)
  2184. {
  2185. return false;
  2186. }
  2187. final Long stamp = _disabledSkills.get(hashCode);
  2188. if (stamp == null)
  2189. {
  2190. return false;
  2191. }
  2192. if (stamp < System.currentTimeMillis())
  2193. {
  2194. _disabledSkills.remove(hashCode);
  2195. return false;
  2196. }
  2197. return true;
  2198. }
  2199. /**
  2200. * Disables all skills.
  2201. */
  2202. public void disableAllSkills()
  2203. {
  2204. _allSkillsDisabled = true;
  2205. }
  2206. /**
  2207. * Enables all skills, except those under reuse time or previously disabled.
  2208. */
  2209. public void enableAllSkills()
  2210. {
  2211. _allSkillsDisabled = false;
  2212. }
  2213. /**
  2214. * Kill the L2Character.<br>
  2215. * <B><U>Actions</U>:</B>
  2216. * <ul>
  2217. * <li>Set target to null and cancel Attack or Cast</li>
  2218. * <li>Stop movement</li>
  2219. * <li>Stop HP/MP/CP Regeneration task</li>
  2220. * <li>Stop all active skills effects in progress on the L2Character</li>
  2221. * <li>Send the Server->Client packet StatusUpdate with current HP and MP to all other L2PcInstance to inform</li>
  2222. * <li>Notify L2Character AI</li>
  2223. * </ul>
  2224. * @param killer The L2Character who killed it
  2225. * @return false if the player is already dead.
  2226. */
  2227. public boolean doDie(L2Character killer)
  2228. {
  2229. final TerminateReturn returnBack = EventDispatcher.getInstance().notifyEvent(new OnCreatureKill(killer, this), this, TerminateReturn.class);
  2230. if ((returnBack != null) && returnBack.terminate())
  2231. {
  2232. return false;
  2233. }
  2234. // killing is only possible one time
  2235. synchronized (this)
  2236. {
  2237. if (isDead())
  2238. {
  2239. return false;
  2240. }
  2241. // now reset currentHp to zero
  2242. setCurrentHp(0);
  2243. setIsDead(true);
  2244. }
  2245. // Set target to null and cancel Attack or Cast
  2246. setTarget(null);
  2247. // Stop movement
  2248. stopMove(null);
  2249. // Stop HP/MP/CP Regeneration task
  2250. getStatus().stopHpMpRegeneration();
  2251. stopAllEffectsExceptThoseThatLastThroughDeath();
  2252. calculateRewards(killer);
  2253. // Send the Server->Client packet StatusUpdate with current HP and MP to all other L2PcInstance to inform
  2254. broadcastStatusUpdate();
  2255. // Notify L2Character AI
  2256. if (hasAI())
  2257. {
  2258. getAI().notifyEvent(CtrlEvent.EVT_DEAD);
  2259. }
  2260. if (getWorldRegion() != null)
  2261. {
  2262. getWorldRegion().onDeath(this);
  2263. }
  2264. getAttackByList().clear();
  2265. if (isChannelized())
  2266. {
  2267. getSkillChannelized().abortChannelization();
  2268. }
  2269. return true;
  2270. }
  2271. @Override
  2272. public boolean deleteMe()
  2273. {
  2274. setDebug(null);
  2275. if (hasAI())
  2276. {
  2277. getAI().stopAITask();
  2278. }
  2279. return true;
  2280. }
  2281. public void detachAI()
  2282. {
  2283. if (isWalker())
  2284. {
  2285. return;
  2286. }
  2287. setAI(null);
  2288. }
  2289. protected void calculateRewards(L2Character killer)
  2290. {
  2291. }
  2292. /** Sets HP, MP and CP and revives the L2Character. */
  2293. public void doRevive()
  2294. {
  2295. if (!isDead())
  2296. {
  2297. return;
  2298. }
  2299. if (!isTeleporting())
  2300. {
  2301. setIsPendingRevive(false);
  2302. setIsDead(false);
  2303. if ((Config.RESPAWN_RESTORE_CP > 0) && (getCurrentCp() < (getMaxCp() * Config.RESPAWN_RESTORE_CP)))
  2304. {
  2305. _status.setCurrentCp(getMaxCp() * Config.RESPAWN_RESTORE_CP);
  2306. }
  2307. if ((Config.RESPAWN_RESTORE_HP > 0) && (getCurrentHp() < (getMaxHp() * Config.RESPAWN_RESTORE_HP)))
  2308. {
  2309. _status.setCurrentHp(getMaxHp() * Config.RESPAWN_RESTORE_HP);
  2310. }
  2311. if ((Config.RESPAWN_RESTORE_MP > 0) && (getCurrentMp() < (getMaxMp() * Config.RESPAWN_RESTORE_MP)))
  2312. {
  2313. _status.setCurrentMp(getMaxMp() * Config.RESPAWN_RESTORE_MP);
  2314. }
  2315. // Start broadcast status
  2316. broadcastPacket(new Revive(this));
  2317. if (getWorldRegion() != null)
  2318. {
  2319. getWorldRegion().onRevive(this);
  2320. }
  2321. }
  2322. else
  2323. {
  2324. setIsPendingRevive(true);
  2325. }
  2326. }
  2327. /**
  2328. * Revives the L2Character using skill.
  2329. * @param revivePower
  2330. */
  2331. public void doRevive(double revivePower)
  2332. {
  2333. doRevive();
  2334. }
  2335. /**
  2336. * Gets this creature's AI.
  2337. * @return the AI
  2338. */
  2339. public final L2CharacterAI getAI()
  2340. {
  2341. if (_ai == null)
  2342. {
  2343. synchronized (this)
  2344. {
  2345. if (_ai == null)
  2346. {
  2347. _ai = initAI();
  2348. }
  2349. }
  2350. }
  2351. return _ai;
  2352. }
  2353. /**
  2354. * Initialize this creature's AI.<br>
  2355. * OOP approach to be overridden in child classes.
  2356. * @return the new AI
  2357. */
  2358. protected L2CharacterAI initAI()
  2359. {
  2360. return new L2CharacterAI(this);
  2361. }
  2362. public void setAI(L2CharacterAI newAI)
  2363. {
  2364. L2CharacterAI oldAI = _ai;
  2365. if ((oldAI != null) && (oldAI != newAI) && (oldAI instanceof L2AttackableAI))
  2366. {
  2367. oldAI.stopAITask();
  2368. }
  2369. _ai = newAI;
  2370. }
  2371. /**
  2372. * Verifies if this creature has an AI,
  2373. * @return {@code true} if this creature has an AI, {@code false} otherwise
  2374. */
  2375. public boolean hasAI()
  2376. {
  2377. return _ai != null;
  2378. }
  2379. /**
  2380. * @return True if the L2Character is RaidBoss or his minion.
  2381. */
  2382. public boolean isRaid()
  2383. {
  2384. return false;
  2385. }
  2386. /**
  2387. * @return True if the L2Character is minion.
  2388. */
  2389. public boolean isMinion()
  2390. {
  2391. return false;
  2392. }
  2393. /**
  2394. * @return True if the L2Character is minion of RaidBoss.
  2395. */
  2396. public boolean isRaidMinion()
  2397. {
  2398. return false;
  2399. }
  2400. /**
  2401. * @return a list of L2Character that attacked.
  2402. */
  2403. public final Set<L2Character> getAttackByList()
  2404. {
  2405. if (_attackByList == null)
  2406. {
  2407. synchronized (this)
  2408. {
  2409. if (_attackByList == null)
  2410. {
  2411. _attackByList = ConcurrentHashMap.newKeySet();
  2412. }
  2413. }
  2414. }
  2415. return _attackByList;
  2416. }
  2417. public final Skill getLastSimultaneousSkillCast()
  2418. {
  2419. return _lastSimultaneousSkillCast;
  2420. }
  2421. public void setLastSimultaneousSkillCast(Skill skill)
  2422. {
  2423. _lastSimultaneousSkillCast = skill;
  2424. }
  2425. public final Skill getLastSkillCast()
  2426. {
  2427. return _lastSkillCast;
  2428. }
  2429. public void setLastSkillCast(Skill skill)
  2430. {
  2431. _lastSkillCast = skill;
  2432. }
  2433. public final boolean isNoRndWalk()
  2434. {
  2435. return _isNoRndWalk;
  2436. }
  2437. public final void setIsNoRndWalk(boolean value)
  2438. {
  2439. _isNoRndWalk = value;
  2440. }
  2441. public final boolean isAfraid()
  2442. {
  2443. return isAffected(EffectFlag.FEAR);
  2444. }
  2445. /**
  2446. * @return True if the L2Character can't use its skills (ex : stun, sleep...).
  2447. */
  2448. public final boolean isAllSkillsDisabled()
  2449. {
  2450. return _allSkillsDisabled || isStunned() || isSleeping() || isParalyzed();
  2451. }
  2452. /**
  2453. * @return True if the L2Character can't attack (stun, sleep, attackEndTime, fakeDeath, paralyze, attackMute).
  2454. */
  2455. public boolean isAttackingDisabled()
  2456. {
  2457. return isFlying() || isStunned() || isSleeping() || isAttackingNow() || isAlikeDead() || isParalyzed() || isPhysicalAttackMuted() || isCoreAIDisabled();
  2458. }
  2459. public final Calculator[] getCalculators()
  2460. {
  2461. return _calculators;
  2462. }
  2463. public final boolean isConfused()
  2464. {
  2465. return isAffected(EffectFlag.CONFUSED);
  2466. }
  2467. /**
  2468. * @return True if the L2Character is dead or use fake death.
  2469. */
  2470. public boolean isAlikeDead()
  2471. {
  2472. return _isDead;
  2473. }
  2474. /**
  2475. * @return True if the L2Character is dead.
  2476. */
  2477. public final boolean isDead()
  2478. {
  2479. return _isDead;
  2480. }
  2481. public final void setIsDead(boolean value)
  2482. {
  2483. _isDead = value;
  2484. }
  2485. public boolean isImmobilized()
  2486. {
  2487. return _isImmobilized;
  2488. }
  2489. public void setIsImmobilized(boolean value)
  2490. {
  2491. _isImmobilized = value;
  2492. }
  2493. public final boolean isMuted()
  2494. {
  2495. return isAffected(EffectFlag.MUTED);
  2496. }
  2497. public final boolean isPhysicalMuted()
  2498. {
  2499. return isAffected(EffectFlag.PSYCHICAL_MUTED);
  2500. }
  2501. public final boolean isPhysicalAttackMuted()
  2502. {
  2503. return isAffected(EffectFlag.PSYCHICAL_ATTACK_MUTED);
  2504. }
  2505. /**
  2506. * @return True if the L2Character can't move (stun, root, sleep, overload, paralyzed).
  2507. */
  2508. public boolean isMovementDisabled()
  2509. {
  2510. // check for isTeleporting to prevent teleport cheating (if appear packet not received)
  2511. return isStunned() || isRooted() || isSleeping() || isOverloaded() || isParalyzed() || isImmobilized() || isAlikeDead() || isTeleporting();
  2512. }
  2513. /**
  2514. * @return True if the L2Character can not be controlled by the player (confused, afraid).
  2515. */
  2516. public final boolean isOutOfControl()
  2517. {
  2518. return isConfused() || isAfraid();
  2519. }
  2520. public final boolean isOverloaded()
  2521. {
  2522. return _isOverloaded;
  2523. }
  2524. /**
  2525. * Set the overloaded status of the L2Character is overloaded (if True, the L2PcInstance can't take more item).
  2526. * @param value
  2527. */
  2528. public final void setIsOverloaded(boolean value)
  2529. {
  2530. _isOverloaded = value;
  2531. }
  2532. public final boolean isParalyzed()
  2533. {
  2534. return _isParalyzed || isAffected(EffectFlag.PARALYZED);
  2535. }
  2536. public final void setIsParalyzed(boolean value)
  2537. {
  2538. _isParalyzed = value;
  2539. }
  2540. public final boolean isPendingRevive()
  2541. {
  2542. return isDead() && _isPendingRevive;
  2543. }
  2544. public final void setIsPendingRevive(boolean value)
  2545. {
  2546. _isPendingRevive = value;
  2547. }
  2548. public final boolean isDisarmed()
  2549. {
  2550. return isAffected(EffectFlag.DISARMED);
  2551. }
  2552. /**
  2553. * @return the summon
  2554. */
  2555. public L2Summon getSummon()
  2556. {
  2557. return null;
  2558. }
  2559. /**
  2560. * @return {@code true} if the character has a summon, {@code false} otherwise
  2561. */
  2562. public final boolean hasSummon()
  2563. {
  2564. return getSummon() != null;
  2565. }
  2566. /**
  2567. * @return {@code true} if the character has a pet, {@code false} otherwise
  2568. */
  2569. public final boolean hasPet()
  2570. {
  2571. return hasSummon() && getSummon().isPet();
  2572. }
  2573. /**
  2574. * @return {@code true} if the character has a servitor, {@code false} otherwise
  2575. */
  2576. public final boolean hasServitor()
  2577. {
  2578. return hasSummon() && getSummon().isServitor();
  2579. }
  2580. public final boolean isRooted()
  2581. {
  2582. return isAffected(EffectFlag.ROOTED);
  2583. }
  2584. /**
  2585. * @return True if the L2Character is running.
  2586. */
  2587. public boolean isRunning()
  2588. {
  2589. return _isRunning;
  2590. }
  2591. public final void setIsRunning(boolean value)
  2592. {
  2593. if (_isRunning == value)
  2594. {
  2595. return;
  2596. }
  2597. _isRunning = value;
  2598. if (getRunSpeed() != 0)
  2599. {
  2600. broadcastPacket(new ChangeMoveType(this));
  2601. }
  2602. if (isPlayer())
  2603. {
  2604. getActingPlayer().broadcastUserInfo();
  2605. }
  2606. else if (isSummon())
  2607. {
  2608. broadcastStatusUpdate();
  2609. }
  2610. else if (isNpc())
  2611. {
  2612. Collection<L2PcInstance> plrs = getKnownList().getKnownPlayers().values();
  2613. for (L2PcInstance player : plrs)
  2614. {
  2615. if ((player == null) || !isVisibleFor(player))
  2616. {
  2617. continue;
  2618. }
  2619. else if (getRunSpeed() == 0)
  2620. {
  2621. player.sendPacket(new ServerObjectInfo((L2Npc) this, player));
  2622. }
  2623. else
  2624. {
  2625. player.sendPacket(new AbstractNpcInfo.NpcInfo((L2Npc) this, player));
  2626. }
  2627. }
  2628. }
  2629. }
  2630. /** Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance. */
  2631. public final void setRunning()
  2632. {
  2633. if (!isRunning())
  2634. {
  2635. setIsRunning(true);
  2636. }
  2637. }
  2638. public final boolean isSleeping()
  2639. {
  2640. return isAffected(EffectFlag.SLEEP);
  2641. }
  2642. public final boolean isStunned()
  2643. {
  2644. return isAffected(EffectFlag.STUNNED);
  2645. }
  2646. public final boolean isBetrayed()
  2647. {
  2648. return isAffected(EffectFlag.BETRAYED);
  2649. }
  2650. public final boolean isTeleporting()
  2651. {
  2652. return _isTeleporting;
  2653. }
  2654. public void setIsTeleporting(boolean value)
  2655. {
  2656. _isTeleporting = value;
  2657. }
  2658. public void setIsInvul(boolean b)
  2659. {
  2660. _isInvul = b;
  2661. }
  2662. public boolean isInvul()
  2663. {
  2664. return _isInvul || _isTeleporting || isAffected(EffectFlag.INVUL);
  2665. }
  2666. public void setIsMortal(boolean b)
  2667. {
  2668. _isMortal = b;
  2669. }
  2670. public boolean isMortal()
  2671. {
  2672. return _isMortal;
  2673. }
  2674. public boolean isUndead()
  2675. {
  2676. return false;
  2677. }
  2678. public boolean isResurrectionBlocked()
  2679. {
  2680. return isAffected(EffectFlag.BLOCK_RESURRECTION);
  2681. }
  2682. public final boolean isFlying()
  2683. {
  2684. return _isFlying;
  2685. }
  2686. public final void setIsFlying(boolean mode)
  2687. {
  2688. _isFlying = mode;
  2689. }
  2690. @Override
  2691. public CharKnownList getKnownList()
  2692. {
  2693. return ((CharKnownList) super.getKnownList());
  2694. }
  2695. @Override
  2696. public void initKnownList()
  2697. {
  2698. setKnownList(new CharKnownList(this));
  2699. }
  2700. public CharStat getStat()
  2701. {
  2702. return _stat;
  2703. }
  2704. /**
  2705. * Initializes the CharStat class of the L2Object, is overwritten in classes that require a different CharStat Type.<br>
  2706. * Removes the need for instanceof checks.
  2707. */
  2708. public void initCharStat()
  2709. {
  2710. _stat = new CharStat(this);
  2711. }
  2712. public final void setStat(CharStat value)
  2713. {
  2714. _stat = value;
  2715. }
  2716. public CharStatus getStatus()
  2717. {
  2718. return _status;
  2719. }
  2720. /**
  2721. * Initializes the CharStatus class of the L2Object, is overwritten in classes that require a different CharStatus Type.<br>
  2722. * Removes the need for instanceof checks.
  2723. */
  2724. public void initCharStatus()
  2725. {
  2726. _status = new CharStatus(this);
  2727. }
  2728. public final void setStatus(CharStatus value)
  2729. {
  2730. _status = value;
  2731. }
  2732. public L2CharTemplate getTemplate()
  2733. {
  2734. return _template;
  2735. }
  2736. /**
  2737. * Set the template of the L2Character.<br>
  2738. * <B><U>Concept</U>:</B><br>
  2739. * Each L2Character owns generic and static properties (ex : all Keltir have the same number of HP...).<br>
  2740. * All of those properties are stored in a different template for each type of L2Character.<br>
  2741. * Each template is loaded once in the server cache memory (reduce memory use).<br>
  2742. * When a new instance of L2Character is spawned, server just create a link between the instance and the template This link is stored in <B>_template</B>.
  2743. * @param template
  2744. */
  2745. protected final void setTemplate(L2CharTemplate template)
  2746. {
  2747. _template = template;
  2748. }
  2749. /**
  2750. * @return the Title of the L2Character.
  2751. */
  2752. public final String getTitle()
  2753. {
  2754. return _title;
  2755. }
  2756. /**
  2757. * Set the Title of the L2Character.
  2758. * @param value
  2759. */
  2760. public final void setTitle(String value)
  2761. {
  2762. if (value == null)
  2763. {
  2764. _title = "";
  2765. }
  2766. else
  2767. {
  2768. _title = value.length() > 21 ? value.substring(0, 20) : value;
  2769. }
  2770. }
  2771. /**
  2772. * Set the L2Character movement type to walk and send Server->Client packet ChangeMoveType to all others L2PcInstance.
  2773. */
  2774. public final void setWalking()
  2775. {
  2776. if (isRunning())
  2777. {
  2778. setIsRunning(false);
  2779. }
  2780. }
  2781. /**
  2782. * Gets the abnormal visual effects affecting this character.
  2783. * @return a map of 32 bits containing all abnormal visual effects in progress for this character
  2784. */
  2785. public int getAbnormalVisualEffects()
  2786. {
  2787. return _abnormalVisualEffects;
  2788. }
  2789. /**
  2790. * Gets the special abnormal visual effects affecting this character.
  2791. * @return a map of 32 bits containing all special effect in progress for this character
  2792. */
  2793. public int getAbnormalVisualEffectSpecial()
  2794. {
  2795. return _abnormalVisualEffectsSpecial;
  2796. }
  2797. /**
  2798. * Gets the event abnormal visual effects affecting this character.
  2799. * @return a map of 32 bits containing all event abnormal visual effects in progress for this character
  2800. */
  2801. public int getAbnormalVisualEffectEvent()
  2802. {
  2803. return _abnormalVisualEffectsEvent;
  2804. }
  2805. /**
  2806. * Verify if this creature is affected by the given abnormal visual effect.
  2807. * @param ave the abnormal visual effect
  2808. * @return {@code true} if the creature is affected by the abnormal visual effect, {@code false} otherwise
  2809. */
  2810. public boolean hasAbnormalVisualEffect(AbnormalVisualEffect ave)
  2811. {
  2812. if (ave.isEvent())
  2813. {
  2814. return (getAbnormalVisualEffectEvent() & ave.getMask()) == ave.getMask();
  2815. }
  2816. if (ave.isSpecial())
  2817. {
  2818. return (getAbnormalVisualEffectSpecial() & ave.getMask()) == ave.getMask();
  2819. }
  2820. return (getAbnormalVisualEffects() & ave.getMask()) == ave.getMask();
  2821. }
  2822. /**
  2823. * Adds the abnormal visual effect flags in the binary mask and send Server->Client UserInfo/CharInfo packet.
  2824. * @param update if {@code true} update packets will be sent
  2825. * @param aves the abnormal visual effects
  2826. */
  2827. public final void startAbnormalVisualEffect(boolean update, AbnormalVisualEffect... aves)
  2828. {
  2829. for (AbnormalVisualEffect ave : aves)
  2830. {
  2831. if (ave.isEvent())
  2832. {
  2833. _abnormalVisualEffectsEvent |= ave.getMask();
  2834. }
  2835. else if (ave.isSpecial())
  2836. {
  2837. _abnormalVisualEffectsSpecial |= ave.getMask();
  2838. }
  2839. else
  2840. {
  2841. _abnormalVisualEffects |= ave.getMask();
  2842. }
  2843. }
  2844. if (update)
  2845. {
  2846. updateAbnormalEffect();
  2847. }
  2848. }
  2849. /**
  2850. * Removes the abnormal visual effect flags from the binary mask and send Server->Client UserInfo/CharInfo packet.
  2851. * @param update if {@code true} update packets will be sent
  2852. * @param aves the abnormal visual effects
  2853. */
  2854. public final void stopAbnormalVisualEffect(boolean update, AbnormalVisualEffect... aves)
  2855. {
  2856. for (AbnormalVisualEffect ave : aves)
  2857. {
  2858. if (ave.isEvent())
  2859. {
  2860. _abnormalVisualEffectsEvent &= ~ave.getMask();
  2861. }
  2862. else if (ave.isSpecial())
  2863. {
  2864. _abnormalVisualEffectsSpecial &= ~ave.getMask();
  2865. }
  2866. else
  2867. {
  2868. _abnormalVisualEffects &= ~ave.getMask();
  2869. }
  2870. }
  2871. if (update)
  2872. {
  2873. updateAbnormalEffect();
  2874. }
  2875. }
  2876. /**
  2877. * Active the abnormal effect Fake Death flag, notify the L2Character AI and send Server->Client UserInfo/CharInfo packet.
  2878. */
  2879. public final void startFakeDeath()
  2880. {
  2881. if (!isPlayer())
  2882. {
  2883. return;
  2884. }
  2885. getActingPlayer().setIsFakeDeath(true);
  2886. // Aborts any attacks/casts if fake dead
  2887. abortAttack();
  2888. abortCast();
  2889. stopMove(null);
  2890. getAI().notifyEvent(CtrlEvent.EVT_FAKE_DEATH);
  2891. broadcastPacket(new ChangeWaitType(this, ChangeWaitType.WT_START_FAKEDEATH));
  2892. }
  2893. /**
  2894. * Launch a Stun Abnormal Effect on the L2Character.<br>
  2895. * <B><U>Actions</U>:</B>
  2896. * <ul>
  2897. * <li>Calculate the success rate of the Stun Abnormal Effect on this L2Character</li>
  2898. * <li>If Stun succeed, active the abnormal effect Stun flag, notify the L2Character AI and send Server->Client UserInfo/CharInfo packet</li>
  2899. * <li>If Stun NOT succeed, send a system message Failed to the L2PcInstance attacker</li>
  2900. * </ul>
  2901. */
  2902. public final void startStunning()
  2903. {
  2904. // Aborts any attacks/casts if stunned
  2905. abortAttack();
  2906. abortCast();
  2907. stopMove(null);
  2908. getAI().notifyEvent(CtrlEvent.EVT_STUNNED);
  2909. if (!isSummon())
  2910. {
  2911. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  2912. }
  2913. updateAbnormalEffect();
  2914. }
  2915. public final void startParalyze()
  2916. {
  2917. // Aborts any attacks/casts if paralyzed
  2918. abortAttack();
  2919. abortCast();
  2920. stopMove(null);
  2921. getAI().notifyEvent(CtrlEvent.EVT_PARALYZED);
  2922. }
  2923. /**
  2924. * Stop all active skills effects in progress on the L2Character.
  2925. */
  2926. public void stopAllEffects()
  2927. {
  2928. _effectList.stopAllEffects();
  2929. }
  2930. /**
  2931. * Stops all effects, except those that last through death.
  2932. */
  2933. public void stopAllEffectsExceptThoseThatLastThroughDeath()
  2934. {
  2935. _effectList.stopAllEffectsExceptThoseThatLastThroughDeath();
  2936. }
  2937. /**
  2938. * Stop and remove the effects corresponding to the skill ID.
  2939. * @param removed if {@code true} the effect will be set as removed, and a system message will be sent
  2940. * @param skillId the skill Id
  2941. */
  2942. public void stopSkillEffects(boolean removed, int skillId)
  2943. {
  2944. _effectList.stopSkillEffects(removed, skillId);
  2945. }
  2946. public final void stopEffects(L2EffectType type)
  2947. {
  2948. _effectList.stopEffects(type);
  2949. }
  2950. /**
  2951. * Exits all buffs effects of the skills with "removedOnAnyAction" set.<br>
  2952. * Called on any action except movement (attack, cast).
  2953. */
  2954. public final void stopEffectsOnAction()
  2955. {
  2956. _effectList.stopEffectsOnAction();
  2957. }
  2958. /**
  2959. * Exits all buffs effects of the skills with "removedOnDamage" set.<br>
  2960. * Called on decreasing HP and mana burn.
  2961. * @param awake
  2962. */
  2963. public final void stopEffectsOnDamage(boolean awake)
  2964. {
  2965. _effectList.stopEffectsOnDamage(awake);
  2966. }
  2967. /**
  2968. * Stop a specified/all Fake Death abnormal L2Effect.<br>
  2969. * <B><U>Actions</U>:</B>
  2970. * <ul>
  2971. * <li>Delete a specified/all (if effect=null) Fake Death abnormal L2Effect from L2Character and update client magic icon</li>
  2972. * <li>Set the abnormal effect flag _fake_death to False</li>
  2973. * <li>Notify the L2Character AI</li>
  2974. * </ul>
  2975. * @param removeEffects
  2976. */
  2977. public final void stopFakeDeath(boolean removeEffects)
  2978. {
  2979. if (removeEffects)
  2980. {
  2981. stopEffects(L2EffectType.FAKE_DEATH);
  2982. }
  2983. // if this is a player instance, start the grace period for this character (grace from mobs only)!
  2984. if (isPlayer())
  2985. {
  2986. getActingPlayer().setIsFakeDeath(false);
  2987. getActingPlayer().setRecentFakeDeath(true);
  2988. }
  2989. broadcastPacket(new ChangeWaitType(this, ChangeWaitType.WT_STOP_FAKEDEATH));
  2990. // TODO: Temp hack: players see FD on ppl that are moving: Teleport to someone who uses FD - if he gets up he will fall down again for that client -
  2991. // even tho he is actually standing... Probably bad info in CharInfo packet?
  2992. broadcastPacket(new Revive(this));
  2993. }
  2994. /**
  2995. * Stop a specified/all Stun abnormal L2Effect.<br>
  2996. * <B><U>Actions</U>:</B>
  2997. * <ul>
  2998. * <li>Delete a specified/all (if effect=null) Stun abnormal L2Effect from L2Character and update client magic icon</li>
  2999. * <li>Set the abnormal effect flag _stuned to False</li>
  3000. * <li>Notify the L2Character AI</li>
  3001. * <li>Send Server->Client UserInfo/CharInfo packet</li>
  3002. * </ul>
  3003. * @param removeEffects
  3004. */
  3005. public final void stopStunning(boolean removeEffects)
  3006. {
  3007. if (removeEffects)
  3008. {
  3009. stopEffects(L2EffectType.STUN);
  3010. }
  3011. if (!isPlayer())
  3012. {
  3013. getAI().notifyEvent(CtrlEvent.EVT_THINK);
  3014. }
  3015. updateAbnormalEffect();
  3016. }
  3017. /**
  3018. * Stop L2Effect: Transformation.<br>
  3019. * <B><U>Actions</U>:</B>
  3020. * <ul>
  3021. * <li>Remove Transformation Effect</li>
  3022. * <li>Notify the L2Character AI</li>
  3023. * <li>Send Server->Client UserInfo/CharInfo packet</li>
  3024. * </ul>
  3025. * @param removeEffects
  3026. */
  3027. public final void stopTransformation(boolean removeEffects)
  3028. {
  3029. if (removeEffects)
  3030. {
  3031. getEffectList().stopSkillEffects(false, AbnormalType.TRANSFORM);
  3032. }
  3033. // if this is a player instance, then untransform, also set the transform_id column equal to 0 if not cursed.
  3034. if (isPlayer())
  3035. {
  3036. if (getActingPlayer().getTransformation() != null)
  3037. {
  3038. getActingPlayer().untransform();
  3039. }
  3040. }
  3041. if (!isPlayer())
  3042. {
  3043. getAI().notifyEvent(CtrlEvent.EVT_THINK);
  3044. }
  3045. updateAbnormalEffect();
  3046. }
  3047. public abstract void updateAbnormalEffect();
  3048. /**
  3049. * Update active skills in progress (In Use and Not In Use because stacked) icons on client.<br>
  3050. * <B><U>Concept</U>:</B><br>
  3051. * All active skills effects in progress (In Use and Not In Use because stacked) are represented by an icon on the client.<br>
  3052. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method ONLY UPDATE the client of the player and not clients of all players in the party.</B></FONT>
  3053. */
  3054. public final void updateEffectIcons()
  3055. {
  3056. updateEffectIcons(false);
  3057. }
  3058. /**
  3059. * Updates Effect Icons for this character(player/summon) and his party if any.
  3060. * @param partyOnly
  3061. */
  3062. public void updateEffectIcons(boolean partyOnly)
  3063. {
  3064. // overridden
  3065. }
  3066. public boolean isAffectedBySkill(int skillId)
  3067. {
  3068. return _effectList.isAffectedBySkill(skillId);
  3069. }
  3070. /**
  3071. * This class group all movement data.<br>
  3072. * <B><U> Data</U> :</B>
  3073. * <ul>
  3074. * <li>_moveTimestamp : Last time position update</li>
  3075. * <li>_xDestination, _yDestination, _zDestination : Position of the destination</li>
  3076. * <li>_xMoveFrom, _yMoveFrom, _zMoveFrom : Position of the origin</li>
  3077. * <li>_moveStartTime : Start time of the movement</li>
  3078. * <li>_ticksToMove : Nb of ticks between the start and the destination</li>
  3079. * <li>_xSpeedTicks, _ySpeedTicks : Speed in unit/ticks</li>
  3080. * </ul>
  3081. */
  3082. public static class MoveData
  3083. {
  3084. // when we retrieve x/y/z we use GameTimeControl.getGameTicks()
  3085. // if we are moving, but move timestamp==gameticks, we don't need
  3086. // to recalculate position
  3087. public int _moveStartTime;
  3088. public int _moveTimestamp; // last update
  3089. public int _xDestination;
  3090. public int _yDestination;
  3091. public int _zDestination;
  3092. public double _xAccurate; // otherwise there would be rounding errors
  3093. public double _yAccurate;
  3094. public double _zAccurate;
  3095. public int _heading;
  3096. public boolean disregardingGeodata;
  3097. public int onGeodataPathIndex;
  3098. public List<AbstractNodeLoc> geoPath;
  3099. public int geoPathAccurateTx;
  3100. public int geoPathAccurateTy;
  3101. public int geoPathGtx;
  3102. public int geoPathGty;
  3103. }
  3104. /**
  3105. * Add a Func to the Calculator set of the L2Character.<br>
  3106. * <b><u>Concept</u>:</b> A L2Character owns a table of Calculators called <b>_calculators</b>.<br>
  3107. * Each Calculator (a calculator per state) own a table of Func object.<br>
  3108. * A Func object is a mathematical function that permit to calculate the modifier of a state (ex : REGENERATE_HP_RATE...).<br>
  3109. * To reduce cache memory use, L2NPCInstances who don't have skills share the same Calculator set called <b>NPC_STD_CALCULATOR</b>.<br>
  3110. * That's why, if a L2NPCInstance is under a skill/spell effect that modify one of its state, a copy of the NPC_STD_CALCULATOR must be create in its _calculators before adding new Func object.<br>
  3111. * <b><u>Actions</u>:</b>
  3112. * <ul>
  3113. * <li>If _calculators is linked to NPC_STD_CALCULATOR, create a copy of NPC_STD_CALCULATOR in _calculators</li>
  3114. * <li>Add the Func object to _calculators</li>
  3115. * </ul>
  3116. * @param function The Func object to add to the Calculator corresponding to the state affected
  3117. */
  3118. public final void addStatFunc(AbstractFunction function)
  3119. {
  3120. if (function == null)
  3121. {
  3122. return;
  3123. }
  3124. synchronized (this)
  3125. {
  3126. // Check if Calculator set is linked to the standard Calculator set of NPC
  3127. if (_calculators == NPC_STD_CALCULATOR)
  3128. {
  3129. // Create a copy of the standard NPC Calculator set
  3130. _calculators = new Calculator[Stats.NUM_STATS];
  3131. for (int i = 0; i < Stats.NUM_STATS; i++)
  3132. {
  3133. if (NPC_STD_CALCULATOR[i] != null)
  3134. {
  3135. _calculators[i] = new Calculator(NPC_STD_CALCULATOR[i]);
  3136. }
  3137. }
  3138. }
  3139. // Select the Calculator of the affected state in the Calculator set
  3140. int stat = function.getStat().ordinal();
  3141. if (_calculators[stat] == null)
  3142. {
  3143. _calculators[stat] = new Calculator();
  3144. }
  3145. // Add the Func to the calculator corresponding to the state
  3146. _calculators[stat].addFunc(function);
  3147. }
  3148. }
  3149. /**
  3150. * Add a list of Funcs to the Calculator set of the L2Character.<br>
  3151. * <B><U>Concept</U>:</B><br>
  3152. * A L2Character owns a table of Calculators called <B>_calculators</B>.<br>
  3153. * Each Calculator (a calculator per state) own a table of Func object.<br>
  3154. * A Func object is a mathematic function that permit to calculate the modifier of a state (ex : REGENERATE_HP_RATE...).<br>
  3155. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method is ONLY for L2PcInstance</B></FONT><br>
  3156. * <B><U>Example of use</U>:</B>
  3157. * <ul>
  3158. * <li>Equip an item from inventory</li>
  3159. * <li>Learn a new passive skill</li>
  3160. * <li>Use an active skill</li>
  3161. * </ul>
  3162. * @param functions The list of Func objects to add to the Calculator corresponding to the state affected
  3163. */
  3164. public final void addStatFuncs(List<AbstractFunction> functions)
  3165. {
  3166. if (!isPlayer() && getKnownList().getKnownPlayers().isEmpty())
  3167. {
  3168. for (AbstractFunction f : functions)
  3169. {
  3170. addStatFunc(f);
  3171. }
  3172. }
  3173. else
  3174. {
  3175. final List<Stats> modifiedStats = new ArrayList<>();
  3176. for (AbstractFunction f : functions)
  3177. {
  3178. modifiedStats.add(f.getStat());
  3179. addStatFunc(f);
  3180. }
  3181. broadcastModifiedStats(modifiedStats);
  3182. }
  3183. }
  3184. /**
  3185. * Remove a Func from the Calculator set of the L2Character.<br>
  3186. * <B><U>Concept</U>:</B><br>
  3187. * A L2Character owns a table of Calculators called <B>_calculators</B>.<br>
  3188. * Each Calculator (a calculator per state) own a table of Func object.<br>
  3189. * A Func object is a mathematic function that permit to calculate the modifier of a state (ex : REGENERATE_HP_RATE...).<br>
  3190. * To reduce cache memory use, L2NPCInstances who don't have skills share the same Calculator set called <B>NPC_STD_CALCULATOR</B>.<br>
  3191. * That's why, if a L2NPCInstance is under a skill/spell effect that modify one of its state, a copy of the NPC_STD_CALCULATOR must be create in its _calculators before addind new Func object.<br>
  3192. * <B><U>Actions</U>:</B>
  3193. * <ul>
  3194. * <li>Remove the Func object from _calculators</li>
  3195. * <li>If L2Character is a L2NPCInstance and _calculators is equal to NPC_STD_CALCULATOR, free cache memory and just create a link on NPC_STD_CALCULATOR in _calculators</li>
  3196. * </ul>
  3197. * @param function The Func object to remove from the Calculator corresponding to the state affected
  3198. */
  3199. public final void removeStatFunc(AbstractFunction function)
  3200. {
  3201. if (function == null)
  3202. {
  3203. return;
  3204. }
  3205. // Select the Calculator of the affected state in the Calculator set
  3206. int stat = function.getStat().ordinal();
  3207. synchronized (this)
  3208. {
  3209. if (_calculators[stat] == null)
  3210. {
  3211. return;
  3212. }
  3213. // Remove the Func object from the Calculator
  3214. _calculators[stat].removeFunc(function);
  3215. if (_calculators[stat].size() == 0)
  3216. {
  3217. _calculators[stat] = null;
  3218. }
  3219. // If possible, free the memory and just create a link on NPC_STD_CALCULATOR
  3220. if (isNpc())
  3221. {
  3222. int i = 0;
  3223. for (; i < Stats.NUM_STATS; i++)
  3224. {
  3225. if (!Calculator.equalsCals(_calculators[i], NPC_STD_CALCULATOR[i]))
  3226. {
  3227. break;
  3228. }
  3229. }
  3230. if (i >= Stats.NUM_STATS)
  3231. {
  3232. _calculators = NPC_STD_CALCULATOR;
  3233. }
  3234. }
  3235. }
  3236. }
  3237. /**
  3238. * Remove a list of Funcs from the Calculator set of the L2PcInstance.<br>
  3239. * <B><U>Concept</U>:</B><br>
  3240. * A L2Character owns a table of Calculators called <B>_calculators</B>.<br>
  3241. * Each Calculator (a calculator per state) own a table of Func object.<br>
  3242. * A Func object is a mathematic function that permit to calculate the modifier of a state (ex : REGENERATE_HP_RATE...).<br>
  3243. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method is ONLY for L2PcInstance</B></FONT><br>
  3244. * <B><U>Example of use</U>:</B>
  3245. * <ul>
  3246. * <li>Unequip an item from inventory</li>
  3247. * <li>Stop an active skill</li>
  3248. * </ul>
  3249. * @param functions The list of Func objects to add to the Calculator corresponding to the state affected
  3250. */
  3251. public final void removeStatFuncs(AbstractFunction[] functions)
  3252. {
  3253. if (!isPlayer() && getKnownList().getKnownPlayers().isEmpty())
  3254. {
  3255. for (AbstractFunction f : functions)
  3256. {
  3257. removeStatFunc(f);
  3258. }
  3259. }
  3260. else
  3261. {
  3262. final List<Stats> modifiedStats = new ArrayList<>();
  3263. for (AbstractFunction f : functions)
  3264. {
  3265. modifiedStats.add(f.getStat());
  3266. removeStatFunc(f);
  3267. }
  3268. broadcastModifiedStats(modifiedStats);
  3269. }
  3270. }
  3271. /**
  3272. * Remove all Func objects with the selected owner from the Calculator set of the L2Character.<br>
  3273. * <B><U>Concept</U>:</B><br>
  3274. * A L2Character owns a table of Calculators called <B>_calculators</B>.<br>
  3275. * Each Calculator (a calculator per state) own a table of Func object.<br>
  3276. * A Func object is a mathematic function that permit to calculate the modifier of a state (ex : REGENERATE_HP_RATE...).<br>
  3277. * To reduce cache memory use, L2NPCInstances who don't have skills share the same Calculator set called <B>NPC_STD_CALCULATOR</B>.<br>
  3278. * That's why, if a L2NPCInstance is under a skill/spell effect that modify one of its state, a copy of the NPC_STD_CALCULATOR must be create in its _calculators before addind new Func object.<br>
  3279. * <B><U>Actions</U>:</B>
  3280. * <ul>
  3281. * <li>Remove all Func objects of the selected owner from _calculators</li>
  3282. * <li>If L2Character is a L2NPCInstance and _calculators is equal to NPC_STD_CALCULATOR, free cache memory and just create a link on NPC_STD_CALCULATOR in _calculators</li>
  3283. * </ul>
  3284. * <B><U>Example of use</U>:</B>
  3285. * <ul>
  3286. * <li>Unequip an item from inventory</li>
  3287. * <li>Stop an active skill</li>
  3288. * </ul>
  3289. * @param owner The Object(Skill, Item...) that has created the effect
  3290. */
  3291. public final void removeStatsOwner(Object owner)
  3292. {
  3293. List<Stats> modifiedStats = null;
  3294. int i = 0;
  3295. // Go through the Calculator set
  3296. synchronized (this)
  3297. {
  3298. for (Calculator calc : _calculators)
  3299. {
  3300. if (calc != null)
  3301. {
  3302. // Delete all Func objects of the selected owner
  3303. if (modifiedStats != null)
  3304. {
  3305. modifiedStats.addAll(calc.removeOwner(owner));
  3306. }
  3307. else
  3308. {
  3309. modifiedStats = calc.removeOwner(owner);
  3310. }
  3311. if (calc.size() == 0)
  3312. {
  3313. _calculators[i] = null;
  3314. }
  3315. }
  3316. i++;
  3317. }
  3318. // If possible, free the memory and just create a link on NPC_STD_CALCULATOR
  3319. if (isNpc())
  3320. {
  3321. i = 0;
  3322. for (; i < Stats.NUM_STATS; i++)
  3323. {
  3324. if (!Calculator.equalsCals(_calculators[i], NPC_STD_CALCULATOR[i]))
  3325. {
  3326. break;
  3327. }
  3328. }
  3329. if (i >= Stats.NUM_STATS)
  3330. {
  3331. _calculators = NPC_STD_CALCULATOR;
  3332. }
  3333. }
  3334. broadcastModifiedStats(modifiedStats);
  3335. }
  3336. }
  3337. protected void broadcastModifiedStats(List<Stats> stats)
  3338. {
  3339. if ((stats == null) || stats.isEmpty())
  3340. {
  3341. return;
  3342. }
  3343. if (isSummon())
  3344. {
  3345. L2Summon summon = (L2Summon) this;
  3346. if (summon.getOwner() != null)
  3347. {
  3348. summon.updateAndBroadcastStatus(1);
  3349. }
  3350. }
  3351. else
  3352. {
  3353. boolean broadcastFull = false;
  3354. StatusUpdate su = new StatusUpdate(this);
  3355. for (Stats stat : stats)
  3356. {
  3357. if (stat == Stats.POWER_ATTACK_SPEED)
  3358. {
  3359. su.addAttribute(StatusUpdate.ATK_SPD, getPAtkSpd());
  3360. }
  3361. else if (stat == Stats.MAGIC_ATTACK_SPEED)
  3362. {
  3363. su.addAttribute(StatusUpdate.CAST_SPD, getMAtkSpd());
  3364. }
  3365. else if (stat == Stats.MOVE_SPEED)
  3366. {
  3367. broadcastFull = true;
  3368. }
  3369. }
  3370. if (isPlayer())
  3371. {
  3372. if (broadcastFull)
  3373. {
  3374. getActingPlayer().updateAndBroadcastStatus(2);
  3375. }
  3376. else
  3377. {
  3378. getActingPlayer().updateAndBroadcastStatus(1);
  3379. if (su.hasAttributes())
  3380. {
  3381. broadcastPacket(su);
  3382. }
  3383. }
  3384. if ((getSummon() != null) && isAffected(EffectFlag.SERVITOR_SHARE))
  3385. {
  3386. getSummon().broadcastStatusUpdate();
  3387. }
  3388. }
  3389. else if (isNpc())
  3390. {
  3391. if (broadcastFull)
  3392. {
  3393. Collection<L2PcInstance> plrs = getKnownList().getKnownPlayers().values();
  3394. for (L2PcInstance player : plrs)
  3395. {
  3396. if ((player == null) || !isVisibleFor(player))
  3397. {
  3398. continue;
  3399. }
  3400. if (getRunSpeed() == 0)
  3401. {
  3402. player.sendPacket(new ServerObjectInfo((L2Npc) this, player));
  3403. }
  3404. else
  3405. {
  3406. player.sendPacket(new AbstractNpcInfo.NpcInfo((L2Npc) this, player));
  3407. }
  3408. }
  3409. }
  3410. else if (su.hasAttributes())
  3411. {
  3412. broadcastPacket(su);
  3413. }
  3414. }
  3415. else if (su.hasAttributes())
  3416. {
  3417. broadcastPacket(su);
  3418. }
  3419. }
  3420. }
  3421. public final int getXdestination()
  3422. {
  3423. MoveData m = _move;
  3424. if (m != null)
  3425. {
  3426. return m._xDestination;
  3427. }
  3428. return getX();
  3429. }
  3430. /**
  3431. * @return the Y destination of the L2Character or the Y position if not in movement.
  3432. */
  3433. public final int getYdestination()
  3434. {
  3435. MoveData m = _move;
  3436. if (m != null)
  3437. {
  3438. return m._yDestination;
  3439. }
  3440. return getY();
  3441. }
  3442. /**
  3443. * @return the Z destination of the L2Character or the Z position if not in movement.
  3444. */
  3445. public final int getZdestination()
  3446. {
  3447. MoveData m = _move;
  3448. if (m != null)
  3449. {
  3450. return m._zDestination;
  3451. }
  3452. return getZ();
  3453. }
  3454. /**
  3455. * @return True if the L2Character is in combat.
  3456. */
  3457. public boolean isInCombat()
  3458. {
  3459. return hasAI() && ((getAI().getAttackTarget() != null) || getAI().isAutoAttacking());
  3460. }
  3461. /**
  3462. * @return True if the L2Character is moving.
  3463. */
  3464. public final boolean isMoving()
  3465. {
  3466. return _move != null;
  3467. }
  3468. /**
  3469. * @return True if the L2Character is travelling a calculated path.
  3470. */
  3471. public final boolean isOnGeodataPath()
  3472. {
  3473. MoveData m = _move;
  3474. if (m == null)
  3475. {
  3476. return false;
  3477. }
  3478. if (m.onGeodataPathIndex == -1)
  3479. {
  3480. return false;
  3481. }
  3482. if (m.onGeodataPathIndex == (m.geoPath.size() - 1))
  3483. {
  3484. return false;
  3485. }
  3486. return true;
  3487. }
  3488. /**
  3489. * @return True if the L2Character is casting.
  3490. */
  3491. public final boolean isCastingNow()
  3492. {
  3493. return _isCastingNow;
  3494. }
  3495. public void setIsCastingNow(boolean value)
  3496. {
  3497. _isCastingNow = value;
  3498. }
  3499. public final boolean isCastingSimultaneouslyNow()
  3500. {
  3501. return _isCastingSimultaneouslyNow;
  3502. }
  3503. public void setIsCastingSimultaneouslyNow(boolean value)
  3504. {
  3505. _isCastingSimultaneouslyNow = value;
  3506. }
  3507. /**
  3508. * @return True if the cast of the L2Character can be aborted.
  3509. */
  3510. public final boolean canAbortCast()
  3511. {
  3512. return _castInterruptTime > GameTimeController.getInstance().getGameTicks();
  3513. }
  3514. public int getCastInterruptTime()
  3515. {
  3516. return _castInterruptTime;
  3517. }
  3518. /**
  3519. * Verifies if the creature is attacking now.
  3520. * @return {@code true} if the creature is attacking now, {@code false} otherwise
  3521. */
  3522. public final boolean isAttackingNow()
  3523. {
  3524. return _attackEndTime > System.nanoTime();
  3525. }
  3526. /**
  3527. * Abort the attack of the L2Character and send Server->Client ActionFailed packet.
  3528. */
  3529. public final void abortAttack()
  3530. {
  3531. if (isAttackingNow())
  3532. {
  3533. sendPacket(ActionFailed.STATIC_PACKET);
  3534. }
  3535. }
  3536. /**
  3537. * Abort the cast of the L2Character and send Server->Client MagicSkillCanceld/ActionFailed packet.
  3538. */
  3539. public final void abortCast()
  3540. {
  3541. if (isCastingNow() || isCastingSimultaneouslyNow())
  3542. {
  3543. Future<?> future = _skillCast;
  3544. // cancels the skill hit scheduled task
  3545. if (future != null)
  3546. {
  3547. future.cancel(true);
  3548. _skillCast = null;
  3549. }
  3550. future = _skillCast2;
  3551. if (future != null)
  3552. {
  3553. future.cancel(true);
  3554. _skillCast2 = null;
  3555. }
  3556. // TODO: Handle removing spawned npc.
  3557. if (isChanneling())
  3558. {
  3559. getSkillChannelizer().stopChanneling();
  3560. }
  3561. if (_allSkillsDisabled)
  3562. {
  3563. enableAllSkills(); // this remains for forced skill use, e.g. scroll of escape
  3564. }
  3565. setIsCastingNow(false);
  3566. setIsCastingSimultaneouslyNow(false);
  3567. // safeguard for cannot be interrupt any more
  3568. _castInterruptTime = 0;
  3569. if (isPlayer())
  3570. {
  3571. getAI().notifyEvent(CtrlEvent.EVT_FINISH_CASTING); // setting back previous intention
  3572. }
  3573. broadcastPacket(new MagicSkillCanceld(getObjectId())); // broadcast packet to stop animations client-side
  3574. sendPacket(ActionFailed.STATIC_PACKET); // send an "action failed" packet to the caster
  3575. }
  3576. }
  3577. /**
  3578. * Update the position of the L2Character during a movement and return True if the movement is finished.<br>
  3579. * <B><U>Concept</U>:</B><br>
  3580. * At the beginning of the move action, all properties of the movement are stored in the MoveData object called <B>_move</B> of the L2Character.<br>
  3581. * The position of the start point and of the destination permit to estimated in function of the movement speed the time to achieve the destination.<br>
  3582. * When the movement is started (ex : by MovetoLocation), this method will be called each 0.1 sec to estimate and update the L2Character position on the server.<br>
  3583. * Note, that the current server position can differe from the current client position even if each movement is straight foward.<br>
  3584. * That's why, client send regularly a Client->Server ValidatePosition packet to eventually correct the gap on the server.<br>
  3585. * But, it's always the server position that is used in range calculation. At the end of the estimated movement time,<br>
  3586. * the L2Character position is automatically set to the destination position even if the movement is not finished.<br>
  3587. * <FONT COLOR=#FF0000><B><U>Caution</U>: The current Z position is obtained FROM THE CLIENT by the Client->Server ValidatePosition Packet.<br>
  3588. * But x and y positions must be calculated to avoid that players try to modify their movement speed.</B></FONT>
  3589. * @return True if the movement is finished
  3590. */
  3591. public boolean updatePosition()
  3592. {
  3593. // Get movement data
  3594. MoveData m = _move;
  3595. if (m == null)
  3596. {
  3597. return true;
  3598. }
  3599. if (!isVisible())
  3600. {
  3601. _move = null;
  3602. return true;
  3603. }
  3604. // Check if this is the first update
  3605. if (m._moveTimestamp == 0)
  3606. {
  3607. m._moveTimestamp = m._moveStartTime;
  3608. m._xAccurate = getX();
  3609. m._yAccurate = getY();
  3610. }
  3611. int gameTicks = GameTimeController.getInstance().getGameTicks();
  3612. // Check if the position has already been calculated
  3613. if (m._moveTimestamp == gameTicks)
  3614. {
  3615. return false;
  3616. }
  3617. int xPrev = getX();
  3618. int yPrev = getY();
  3619. int zPrev = getZ(); // the z coordinate may be modified by coordinate synchronizations
  3620. double dx, dy, dz;
  3621. if (Config.COORD_SYNCHRONIZE == 1)
  3622. // the only method that can modify x,y while moving (otherwise _move would/should be set null)
  3623. {
  3624. dx = m._xDestination - xPrev;
  3625. dy = m._yDestination - yPrev;
  3626. }
  3627. else
  3628. // otherwise we need saved temporary values to avoid rounding errors
  3629. {
  3630. dx = m._xDestination - m._xAccurate;
  3631. dy = m._yDestination - m._yAccurate;
  3632. }
  3633. final boolean isFloating = isFlying() || isInsideZone(ZoneId.WATER);
  3634. // Z coordinate will follow geodata or client values
  3635. if ((Config.COORD_SYNCHRONIZE == 2) && !isFloating && !m.disregardingGeodata && ((GameTimeController.getInstance().getGameTicks() % 10) == 0 // once a second to reduce possible cpu load
  3636. ) && GeoData.getInstance().hasGeo(xPrev, yPrev))
  3637. {
  3638. int geoHeight = GeoData.getInstance().getSpawnHeight(xPrev, yPrev, zPrev);
  3639. dz = m._zDestination - geoHeight;
  3640. // quite a big difference, compare to validatePosition packet
  3641. if (isPlayer() && (Math.abs(getActingPlayer().getClientZ() - geoHeight) > 200) && (Math.abs(getActingPlayer().getClientZ() - geoHeight) < 1500))
  3642. {
  3643. dz = m._zDestination - zPrev; // allow diff
  3644. }
  3645. else if (isInCombat() && (Math.abs(dz) > 200) && (((dx * dx) + (dy * dy)) < 40000)) // allow mob to climb up to pcinstance
  3646. {
  3647. dz = m._zDestination - zPrev; // climbing
  3648. }
  3649. else
  3650. {
  3651. zPrev = geoHeight;
  3652. }
  3653. }
  3654. else
  3655. {
  3656. dz = m._zDestination - zPrev;
  3657. }
  3658. double delta = (dx * dx) + (dy * dy);
  3659. if ((delta < 10000) && ((dz * dz) > 2500) // close enough, allows error between client and server geodata if it cannot be avoided
  3660. && !isFloating)
  3661. {
  3662. delta = Math.sqrt(delta);
  3663. }
  3664. else
  3665. {
  3666. delta = Math.sqrt(delta + (dz * dz));
  3667. }
  3668. double distFraction = Double.MAX_VALUE;
  3669. if (delta > 1)
  3670. {
  3671. final double distPassed = (getMoveSpeed() * (gameTicks - m._moveTimestamp)) / GameTimeController.TICKS_PER_SECOND;
  3672. distFraction = distPassed / delta;
  3673. }
  3674. // if (Config.DEVELOPER) _log.warning("Move Ticks:" + (gameTicks - m._moveTimestamp) + ", distPassed:" + distPassed + ", distFraction:" + distFraction);
  3675. if (distFraction > 1)
  3676. {
  3677. // Set the position of the L2Character to the destination
  3678. super.setXYZ(m._xDestination, m._yDestination, m._zDestination);
  3679. }
  3680. else
  3681. {
  3682. m._xAccurate += dx * distFraction;
  3683. m._yAccurate += dy * distFraction;
  3684. // Set the position of the L2Character to estimated after parcial move
  3685. super.setXYZ((int) (m._xAccurate), (int) (m._yAccurate), zPrev + (int) ((dz * distFraction) + 0.5));
  3686. }
  3687. revalidateZone(false);
  3688. // Set the timer of last position update to now
  3689. m._moveTimestamp = gameTicks;
  3690. if (distFraction > 1)
  3691. {
  3692. ThreadPoolManager.getInstance().executeAi(() ->
  3693. {
  3694. try
  3695. {
  3696. if (Config.MOVE_BASED_KNOWNLIST)
  3697. {
  3698. getKnownList().findObjects();
  3699. }
  3700. getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
  3701. }
  3702. catch (final Throwable e)
  3703. {
  3704. _log.log(Level.WARNING, "", e);
  3705. }
  3706. });
  3707. return true;
  3708. }
  3709. return false;
  3710. }
  3711. public void revalidateZone(boolean force)
  3712. {
  3713. if (getWorldRegion() == null)
  3714. {
  3715. return;
  3716. }
  3717. // This function is called too often from movement code
  3718. if (force)
  3719. {
  3720. _zoneValidateCounter = 4;
  3721. }
  3722. else
  3723. {
  3724. _zoneValidateCounter--;
  3725. if (_zoneValidateCounter < 0)
  3726. {
  3727. _zoneValidateCounter = 4;
  3728. }
  3729. else
  3730. {
  3731. return;
  3732. }
  3733. }
  3734. getWorldRegion().revalidateZones(this);
  3735. }
  3736. /**
  3737. * Stop movement of the L2Character (Called by AI Accessor only).<br>
  3738. * <B><U>Actions</U>:</B>
  3739. * <ul>
  3740. * <li>Delete movement data of the L2Character</li>
  3741. * <li>Set the current position (x,y,z), its current L2WorldRegion if necessary and its heading</li>
  3742. * <li>Remove the L2Object object from _gmList of GmListTable</li>
  3743. * <li>Remove object from _knownObjects and _knownPlayer of all surrounding L2WorldRegion L2Characters</li>
  3744. * </ul>
  3745. * <FONT COLOR=#FF0000><B><U>Caution</U>: This method DOESN'T send Server->Client packet StopMove/StopRotation</B></FONT>
  3746. * @param loc
  3747. */
  3748. public void stopMove(Location loc)
  3749. {
  3750. stopMove(loc, false);
  3751. }
  3752. public void stopMove(Location loc, boolean updateKnownObjects)
  3753. {
  3754. // Delete movement data of the L2Character
  3755. _move = null;
  3756. // if (getAI() != null)
  3757. // getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  3758. // Set the current position (x,y,z), its current L2WorldRegion if necessary and its heading
  3759. // All data are contained in a Location object
  3760. if (loc != null)
  3761. {
  3762. setXYZ(loc.getX(), loc.getY(), loc.getZ());
  3763. setHeading(loc.getHeading());
  3764. revalidateZone(true);
  3765. }
  3766. broadcastPacket(new StopMove(this));
  3767. if (Config.MOVE_BASED_KNOWNLIST && updateKnownObjects)
  3768. {
  3769. getKnownList().findObjects();
  3770. }
  3771. }
  3772. /**
  3773. * @return Returns the showSummonAnimation.
  3774. */
  3775. public boolean isShowSummonAnimation()
  3776. {
  3777. return _showSummonAnimation;
  3778. }
  3779. /**
  3780. * @param showSummonAnimation The showSummonAnimation to set.
  3781. */
  3782. public void setShowSummonAnimation(boolean showSummonAnimation)
  3783. {
  3784. _showSummonAnimation = showSummonAnimation;
  3785. }
  3786. /**
  3787. * Target a L2Object (add the target to the L2Character _target, _knownObject and L2Character to _KnownObject of the L2Object).<br>
  3788. * <B><U>Concept</U>:</B><br>
  3789. * The L2Object (including L2Character) targeted is identified in <B>_target</B> of the L2Character.<br>
  3790. * <B><U>Actions</U>:</B>
  3791. * <ul>
  3792. * <li>Set the _target of L2Character to L2Object</li>
  3793. * <li>If necessary, add L2Object to _knownObject of the L2Character</li>
  3794. * <li>If necessary, add L2Character to _KnownObject of the L2Object</li>
  3795. * <li>If object==null, cancel Attak or Cast</li>
  3796. * </ul>
  3797. * @param object L2object to target
  3798. */
  3799. public void setTarget(L2Object object)
  3800. {
  3801. if ((object != null) && !object.isVisible())
  3802. {
  3803. object = null;
  3804. }
  3805. if ((object != null) && (object != _target))
  3806. {
  3807. getKnownList().addKnownObject(object);
  3808. object.getKnownList().addKnownObject(this);
  3809. }
  3810. _target = object;
  3811. }
  3812. /**
  3813. * @return the identifier of the L2Object targeted or -1.
  3814. */
  3815. public final int getTargetId()
  3816. {
  3817. if (_target != null)
  3818. {
  3819. return _target.getObjectId();
  3820. }
  3821. return 0;
  3822. }
  3823. /**
  3824. * @return the L2Object targeted or null.
  3825. */
  3826. public final L2Object getTarget()
  3827. {
  3828. return _target;
  3829. }
  3830. /**
  3831. * Calculate movement data for a move to location action and add the L2Character to movingObjects of GameTimeController (only called by AI Accessor).<br>
  3832. * <B><U>Concept</U>:</B><br>
  3833. * At the beginning of the move action, all properties of the movement are stored in the MoveData object called <B>_move</B> of the L2Character.<br>
  3834. * The position of the start point and of the destination permit to estimated in function of the movement speed the time to achieve the destination.<br>
  3835. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController that will call the updatePosition method of those L2Character each 0.1s.<br>
  3836. * <B><U>Actions</U>:</B>
  3837. * <ul>
  3838. * <li>Get current position of the L2Character</li>
  3839. * <li>Calculate distance (dx,dy) between current position and destination including offset</li>
  3840. * <li>Create and Init a MoveData object</li>
  3841. * <li>Set the L2Character _move object to MoveData object</li>
  3842. * <li>Add the L2Character to movingObjects of the GameTimeController</li>
  3843. * <li>Create a task to notify the AI that L2Character arrives at a check point of the movement</li>
  3844. * </ul>
  3845. * <FONT COLOR=#FF0000><B><U>Caution</U>: This method DOESN'T send Server->Client packet MoveToPawn/CharMoveToLocation.</B></FONT><br>
  3846. * <B><U>Example of use</U>:</B>
  3847. * <ul>
  3848. * <li>AI : onIntentionMoveTo(Location), onIntentionPickUp(L2Object), onIntentionInteract(L2Object)</li>
  3849. * <li>FollowTask</li>
  3850. * </ul>
  3851. * @param x The X position of the destination
  3852. * @param y The Y position of the destination
  3853. * @param z The Y position of the destination
  3854. * @param offset The size of the interaction area of the L2Character targeted
  3855. */
  3856. public void moveToLocation(int x, int y, int z, int offset)
  3857. {
  3858. // Get the Move Speed of the L2Charcater
  3859. double speed = getMoveSpeed();
  3860. if ((speed <= 0) || isMovementDisabled())
  3861. {
  3862. return;
  3863. }
  3864. // Get current position of the L2Character
  3865. final int curX = super.getX();
  3866. final int curY = super.getY();
  3867. final int curZ = super.getZ();
  3868. // Calculate distance (dx,dy) between current position and destination
  3869. // TODO: improve Z axis move/follow support when dx,dy are small compared to dz
  3870. double dx = (x - curX);
  3871. double dy = (y - curY);
  3872. double dz = (z - curZ);
  3873. double distance = Math.sqrt((dx * dx) + (dy * dy));
  3874. final boolean verticalMovementOnly = isFlying() && (distance == 0) && (dz != 0);
  3875. if (verticalMovementOnly)
  3876. {
  3877. distance = Math.abs(dz);
  3878. }
  3879. // make water move short and use no geodata checks for swimming chars
  3880. // distance in a click can easily be over 3000
  3881. if (isInsideZone(ZoneId.WATER) && (distance > 700))
  3882. {
  3883. double divider = 700 / distance;
  3884. x = curX + (int) (divider * dx);
  3885. y = curY + (int) (divider * dy);
  3886. z = curZ + (int) (divider * dz);
  3887. dx = (x - curX);
  3888. dy = (y - curY);
  3889. dz = (z - curZ);
  3890. distance = Math.sqrt((dx * dx) + (dy * dy));
  3891. }
  3892. // @formatter:off
  3893. // Define movement angles needed
  3894. // ^
  3895. // | X (x,y)
  3896. // | /
  3897. // | / distance
  3898. // | /
  3899. // |/ angle
  3900. // X ---------->
  3901. // (curx,cury)
  3902. // @formatter:on
  3903. double cos;
  3904. double sin;
  3905. // Check if a movement offset is defined or no distance to go through
  3906. if ((offset > 0) || (distance < 1))
  3907. {
  3908. // approximation for moving closer when z coordinates are different
  3909. // TODO: handle Z axis movement better
  3910. offset -= Math.abs(dz);
  3911. if (offset < 5)
  3912. {
  3913. offset = 5;
  3914. }
  3915. // If no distance to go through, the movement is canceled
  3916. if ((distance < 1) || ((distance - offset) <= 0))
  3917. {
  3918. // Notify the AI that the L2Character is arrived at destination
  3919. getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
  3920. return;
  3921. }
  3922. // Calculate movement angles needed
  3923. sin = dy / distance;
  3924. cos = dx / distance;
  3925. distance -= (offset - 5); // due to rounding error, we have to move a bit closer to be in range
  3926. // Calculate the new destination with offset included
  3927. x = curX + (int) (distance * cos);
  3928. y = curY + (int) (distance * sin);
  3929. }
  3930. else
  3931. {
  3932. // Calculate movement angles needed
  3933. sin = dy / distance;
  3934. cos = dx / distance;
  3935. }
  3936. // Create and Init a MoveData object
  3937. MoveData m = new MoveData();
  3938. // GEODATA MOVEMENT CHECKS AND PATHFINDING
  3939. m.onGeodataPathIndex = -1; // Initialize not on geodata path
  3940. m.disregardingGeodata = false;
  3941. if (!isFlying() // flying chars not checked - even canSeeTarget doesn't work yet
  3942. && (!isInsideZone(ZoneId.WATER) || isInsideZone(ZoneId.SIEGE))) // swimming also not checked unless in siege zone - but distance is limited
  3943. {
  3944. final boolean isInVehicle = isPlayer() && (getActingPlayer().getVehicle() != null);
  3945. if (isInVehicle)
  3946. {
  3947. m.disregardingGeodata = true;
  3948. }
  3949. double originalDistance = distance;
  3950. int originalX = x;
  3951. int originalY = y;
  3952. int originalZ = z;
  3953. int gtx = (originalX - L2World.MAP_MIN_X) >> 4;
  3954. int gty = (originalY - L2World.MAP_MIN_Y) >> 4;
  3955. // Movement checks:
  3956. // when PATHFINDING > 0, for all characters except mobs returning home (could be changed later to teleport if pathfinding fails)
  3957. if (((Config.PATHFINDING > 0) && (!(isAttackable() && ((L2Attackable) this).isReturningToSpawnPoint()))) //
  3958. || (isPlayer() && !(isInVehicle && (distance > 1500))) //
  3959. || (this instanceof L2RiftInvaderInstance))
  3960. {
  3961. if (isOnGeodataPath())
  3962. {
  3963. try
  3964. {
  3965. if ((gtx == _move.geoPathGtx) && (gty == _move.geoPathGty))
  3966. {
  3967. return;
  3968. }
  3969. _move.onGeodataPathIndex = -1; // Set not on geodata path
  3970. }
  3971. catch (NullPointerException e)
  3972. {
  3973. // nothing
  3974. }
  3975. }
  3976. if ((curX < L2World.MAP_MIN_X) || (curX > L2World.MAP_MAX_X) || (curY < L2World.MAP_MIN_Y) || (curY > L2World.MAP_MAX_Y))
  3977. {
  3978. // Temporary fix for character outside world region errors
  3979. _log.warning("Character " + getName() + " outside world area, in coordinates x:" + curX + " y:" + curY);
  3980. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  3981. if (isPlayer())
  3982. {
  3983. getActingPlayer().logout();
  3984. }
  3985. else if (isSummon())
  3986. {
  3987. return; // preventation when summon get out of world coords, player will not loose him, unsummon handled from pcinstance
  3988. }
  3989. else
  3990. {
  3991. onDecay();
  3992. }
  3993. return;
  3994. }
  3995. Location destiny = GeoData.getInstance().moveCheck(curX, curY, curZ, x, y, z, getInstanceId());
  3996. // location different if destination wasn't reached (or just z coord is different)
  3997. x = destiny.getX();
  3998. y = destiny.getY();
  3999. z = destiny.getZ();
  4000. dx = x - curX;
  4001. dy = y - curY;
  4002. dz = z - curZ;
  4003. distance = verticalMovementOnly ? Math.abs(dz * dz) : Math.sqrt((dx * dx) + (dy * dy));
  4004. }
  4005. // Pathfinding checks. Only when geodata setting is 2, the LoS check gives shorter result
  4006. // than the original movement was and the LoS gives a shorter distance than 2000
  4007. // This way of detecting need for pathfinding could be changed.
  4008. if ((Config.PATHFINDING > 0) && ((originalDistance - distance) > 30) && (distance < 2000))
  4009. {
  4010. // Path calculation
  4011. // Overrides previous movement check
  4012. if ((isPlayable() && !isInVehicle) || isMinion() || isInCombat())
  4013. {
  4014. m.geoPath = PathFinding.getInstance().findPath(curX, curY, curZ, originalX, originalY, originalZ, getInstanceId(), isPlayable());
  4015. if ((m.geoPath == null) || (m.geoPath.size() < 2)) // No path found
  4016. {
  4017. // Even though there's no path found (remember geonodes aren't perfect),
  4018. // the mob is attacking and right now we set it so that the mob will go
  4019. // after target anyway, is dz is small enough.
  4020. // With cellpathfinding this approach could be changed but would require taking
  4021. // off the geonodes and some more checks.
  4022. // Summons will follow their masters no matter what.
  4023. // Currently minions also must move freely since L2AttackableAI commands them to move along with their leader
  4024. if (isPlayer() || (!isPlayable() && !isMinion() && (Math.abs(z - curZ) > 140)) || (isSummon() && !((L2Summon) this).getFollowStatus()))
  4025. {
  4026. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4027. return;
  4028. }
  4029. m.disregardingGeodata = true;
  4030. x = originalX;
  4031. y = originalY;
  4032. z = originalZ;
  4033. distance = originalDistance;
  4034. }
  4035. else
  4036. {
  4037. m.onGeodataPathIndex = 0; // on first segment
  4038. m.geoPathGtx = gtx;
  4039. m.geoPathGty = gty;
  4040. m.geoPathAccurateTx = originalX;
  4041. m.geoPathAccurateTy = originalY;
  4042. x = m.geoPath.get(m.onGeodataPathIndex).getX();
  4043. y = m.geoPath.get(m.onGeodataPathIndex).getY();
  4044. z = m.geoPath.get(m.onGeodataPathIndex).getZ();
  4045. // check for doors in the route
  4046. if (DoorData.getInstance().checkIfDoorsBetween(curX, curY, curZ, x, y, z, getInstanceId()))
  4047. {
  4048. m.geoPath = null;
  4049. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4050. return;
  4051. }
  4052. for (int i = 0; i < (m.geoPath.size() - 1); i++)
  4053. {
  4054. if (DoorData.getInstance().checkIfDoorsBetween(m.geoPath.get(i), m.geoPath.get(i + 1), getInstanceId()))
  4055. {
  4056. m.geoPath = null;
  4057. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4058. return;
  4059. }
  4060. }
  4061. dx = x - curX;
  4062. dy = y - curY;
  4063. dz = z - curZ;
  4064. distance = verticalMovementOnly ? Math.abs(dz * dz) : Math.sqrt((dx * dx) + (dy * dy));
  4065. sin = dy / distance;
  4066. cos = dx / distance;
  4067. }
  4068. }
  4069. }
  4070. // If no distance to go through, the movement is canceled
  4071. if ((distance < 1) && ((Config.PATHFINDING > 0) || isPlayable() || (this instanceof L2RiftInvaderInstance) || isAfraid()))
  4072. {
  4073. if (isSummon())
  4074. {
  4075. ((L2Summon) this).setFollowStatus(false);
  4076. }
  4077. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4078. return;
  4079. }
  4080. }
  4081. // Apply Z distance for flying or swimming for correct timing calculations
  4082. if ((isFlying() || isInsideZone(ZoneId.WATER)) && !verticalMovementOnly)
  4083. {
  4084. distance = Math.sqrt((distance * distance) + (dz * dz));
  4085. }
  4086. // Caclulate the Nb of ticks between the current position and the destination
  4087. // One tick added for rounding reasons
  4088. int ticksToMove = 1 + (int) ((GameTimeController.TICKS_PER_SECOND * distance) / speed);
  4089. m._xDestination = x;
  4090. m._yDestination = y;
  4091. m._zDestination = z; // this is what was requested from client
  4092. // Calculate and set the heading of the L2Character
  4093. m._heading = 0; // initial value for coordinate sync
  4094. // Does not broke heading on vertical movements
  4095. if (!verticalMovementOnly)
  4096. {
  4097. setHeading(Util.calculateHeadingFrom(cos, sin));
  4098. }
  4099. m._moveStartTime = GameTimeController.getInstance().getGameTicks();
  4100. // Set the L2Character _move object to MoveData object
  4101. _move = m;
  4102. // Add the L2Character to movingObjects of the GameTimeController
  4103. // The GameTimeController manage objects movement
  4104. GameTimeController.getInstance().registerMovingObject(this);
  4105. // Create a task to notify the AI that L2Character arrives at a check point of the movement
  4106. if ((ticksToMove * GameTimeController.MILLIS_IN_TICK) > 3000)
  4107. {
  4108. ThreadPoolManager.getInstance().scheduleAi(new NotifyAITask(this, CtrlEvent.EVT_ARRIVED_REVALIDATE), 2000);
  4109. }
  4110. // the CtrlEvent.EVT_ARRIVED will be sent when the character will actually arrive
  4111. // to destination by GameTimeController
  4112. }
  4113. public boolean moveToNextRoutePoint()
  4114. {
  4115. if (!isOnGeodataPath())
  4116. {
  4117. // Cancel the move action
  4118. _move = null;
  4119. return false;
  4120. }
  4121. // Get the Move Speed of the L2Charcater
  4122. double speed = getMoveSpeed();
  4123. if ((speed <= 0) || isMovementDisabled())
  4124. {
  4125. // Cancel the move action
  4126. _move = null;
  4127. return false;
  4128. }
  4129. MoveData md = _move;
  4130. if (md == null)
  4131. {
  4132. return false;
  4133. }
  4134. // Create and Init a MoveData object
  4135. MoveData m = new MoveData();
  4136. // Update MoveData object
  4137. m.onGeodataPathIndex = md.onGeodataPathIndex + 1; // next segment
  4138. m.geoPath = md.geoPath;
  4139. m.geoPathGtx = md.geoPathGtx;
  4140. m.geoPathGty = md.geoPathGty;
  4141. m.geoPathAccurateTx = md.geoPathAccurateTx;
  4142. m.geoPathAccurateTy = md.geoPathAccurateTy;
  4143. if (md.onGeodataPathIndex == (md.geoPath.size() - 2))
  4144. {
  4145. m._xDestination = md.geoPathAccurateTx;
  4146. m._yDestination = md.geoPathAccurateTy;
  4147. m._zDestination = md.geoPath.get(m.onGeodataPathIndex).getZ();
  4148. }
  4149. else
  4150. {
  4151. m._xDestination = md.geoPath.get(m.onGeodataPathIndex).getX();
  4152. m._yDestination = md.geoPath.get(m.onGeodataPathIndex).getY();
  4153. m._zDestination = md.geoPath.get(m.onGeodataPathIndex).getZ();
  4154. }
  4155. double dx = (m._xDestination - super.getX());
  4156. double dy = (m._yDestination - super.getY());
  4157. double distance = Math.sqrt((dx * dx) + (dy * dy));
  4158. // Calculate and set the heading of the L2Character
  4159. if (distance != 0)
  4160. {
  4161. setHeading(Util.calculateHeadingFrom(getX(), getY(), m._xDestination, m._yDestination));
  4162. }
  4163. // Caclulate the Nb of ticks between the current position and the destination
  4164. // One tick added for rounding reasons
  4165. int ticksToMove = 1 + (int) ((GameTimeController.TICKS_PER_SECOND * distance) / speed);
  4166. m._heading = 0; // initial value for coordinate sync
  4167. m._moveStartTime = GameTimeController.getInstance().getGameTicks();
  4168. // Set the L2Character _move object to MoveData object
  4169. _move = m;
  4170. // Add the L2Character to movingObjects of the GameTimeController
  4171. // The GameTimeController manage objects movement
  4172. GameTimeController.getInstance().registerMovingObject(this);
  4173. // Create a task to notify the AI that L2Character arrives at a check point of the movement
  4174. if ((ticksToMove * GameTimeController.MILLIS_IN_TICK) > 3000)
  4175. {
  4176. ThreadPoolManager.getInstance().scheduleAi(new NotifyAITask(this, CtrlEvent.EVT_ARRIVED_REVALIDATE), 2000);
  4177. }
  4178. // the CtrlEvent.EVT_ARRIVED will be sent when the character will actually arrive
  4179. // to destination by GameTimeController
  4180. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  4181. MoveToLocation msg = new MoveToLocation(this);
  4182. broadcastPacket(msg);
  4183. return true;
  4184. }
  4185. public boolean validateMovementHeading(int heading)
  4186. {
  4187. MoveData m = _move;
  4188. if (m == null)
  4189. {
  4190. return true;
  4191. }
  4192. boolean result = true;
  4193. if (m._heading != heading)
  4194. {
  4195. result = (m._heading == 0); // initial value or false
  4196. m._heading = heading;
  4197. }
  4198. return result;
  4199. }
  4200. /**
  4201. * Check if this object is inside the given radius around the given point.
  4202. * @param loc Location of the target
  4203. * @param radius the radius around the target
  4204. * @param checkZAxis should we check Z axis also
  4205. * @param strictCheck true if (distance < radius), false if (distance <= radius)
  4206. * @return true if the L2Character is inside the radius.
  4207. */
  4208. public final boolean isInsideRadius(ILocational loc, int radius, boolean checkZAxis, boolean strictCheck)
  4209. {
  4210. return isInsideRadius(loc.getX(), loc.getY(), loc.getZ(), radius, checkZAxis, strictCheck);
  4211. }
  4212. /**
  4213. * Check if this object is inside the given radius around the given point.
  4214. * @param x X position of the target
  4215. * @param y Y position of the target
  4216. * @param z Z position of the target
  4217. * @param radius the radius around the target
  4218. * @param checkZAxis should we check Z axis also
  4219. * @param strictCheck true if (distance < radius), false if (distance <= radius)
  4220. * @return true if the L2Character is inside the radius.
  4221. */
  4222. public final boolean isInsideRadius(int x, int y, int z, int radius, boolean checkZAxis, boolean strictCheck)
  4223. {
  4224. final double distance = calculateDistance(x, y, z, checkZAxis, true);
  4225. return (strictCheck) ? (distance < (radius * radius)) : (distance <= (radius * radius));
  4226. }
  4227. /**
  4228. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4229. * @return True if arrows are available.
  4230. */
  4231. protected boolean checkAndEquipArrows()
  4232. {
  4233. return true;
  4234. }
  4235. /**
  4236. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4237. * @return True if bolts are available.
  4238. */
  4239. protected boolean checkAndEquipBolts()
  4240. {
  4241. return true;
  4242. }
  4243. /**
  4244. * Add Exp and Sp to the L2Character.<br>
  4245. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li> <li>L2PetInstance</li>
  4246. * @param addToExp
  4247. * @param addToSp
  4248. */
  4249. public void addExpAndSp(long addToExp, int addToSp)
  4250. {
  4251. // Dummy method (overridden by players and pets)
  4252. }
  4253. /**
  4254. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4255. * @return the active weapon instance (always equiped in the right hand).
  4256. */
  4257. public abstract L2ItemInstance getActiveWeaponInstance();
  4258. /**
  4259. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4260. * @return the active weapon item (always equiped in the right hand).
  4261. */
  4262. public abstract L2Weapon getActiveWeaponItem();
  4263. /**
  4264. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4265. * @return the secondary weapon instance (always equiped in the left hand).
  4266. */
  4267. public abstract L2ItemInstance getSecondaryWeaponInstance();
  4268. /**
  4269. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4270. * @return the secondary {@link L2Item} item (always equiped in the left hand).
  4271. */
  4272. public abstract L2Item getSecondaryWeaponItem();
  4273. /**
  4274. * Manage hit process (called by Hit Task).<br>
  4275. * <B><U>Actions</U>:</B>
  4276. * <ul>
  4277. * <li>If the attacker/target is dead or use fake death, notify the AI with EVT_CANCEL and send a Server->Client packet ActionFailed (if attacker is a L2PcInstance)</li>
  4278. * <li>If attack isn't aborted, send a message system (critical hit, missed...) to attacker/target if they are L2PcInstance</li>
  4279. * <li>If attack isn't aborted and hit isn't missed, reduce HP of the target and calculate reflection damage to reduce HP of attacker if necessary</li>
  4280. * <li>if attack isn't aborted and hit isn't missed, manage attack or cast break of the target (calculating rate, sending message...)</li>
  4281. * </ul>
  4282. * @param target The L2Character targeted
  4283. * @param damage Nb of HP to reduce
  4284. * @param crit True if hit is critical
  4285. * @param miss True if hit is missed
  4286. * @param soulshot True if SoulShot are charged
  4287. * @param shld True if shield is efficient
  4288. */
  4289. public void onHitTimer(L2Character target, int damage, boolean crit, boolean miss, boolean soulshot, byte shld)
  4290. {
  4291. // If the attacker/target is dead or use fake death, notify the AI with EVT_CANCEL
  4292. // and send a Server->Client packet ActionFailed (if attacker is a L2PcInstance)
  4293. if ((target == null) || isAlikeDead() || (isNpc() && ((L2Npc) this).isEventMob()))
  4294. {
  4295. getAI().notifyEvent(CtrlEvent.EVT_CANCEL);
  4296. return;
  4297. }
  4298. if ((isNpc() && target.isAlikeDead()) || target.isDead() || (!getKnownList().knowsObject(target) && !isDoor()))
  4299. {
  4300. // getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE, null);
  4301. getAI().notifyEvent(CtrlEvent.EVT_CANCEL);
  4302. sendPacket(ActionFailed.STATIC_PACKET);
  4303. return;
  4304. }
  4305. if (miss)
  4306. {
  4307. // Notify target AI
  4308. if (target.hasAI())
  4309. {
  4310. target.getAI().notifyEvent(CtrlEvent.EVT_EVADED, this);
  4311. }
  4312. notifyAttackAvoid(target, false);
  4313. }
  4314. // Send message about damage/crit or miss
  4315. sendDamageMessage(target, damage, false, crit, miss);
  4316. // Check Raidboss attack
  4317. // Character will be petrified if attacking a raid that's more
  4318. // than 8 levels lower
  4319. if (target.isRaid() && target.giveRaidCurse() && !Config.RAID_DISABLE_CURSE)
  4320. {
  4321. if (getLevel() > (target.getLevel() + 8))
  4322. {
  4323. Skill skill = CommonSkill.RAID_CURSE2.getSkill();
  4324. if (skill != null)
  4325. {
  4326. abortAttack();
  4327. abortCast();
  4328. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4329. skill.applyEffects(target, this);
  4330. }
  4331. else
  4332. {
  4333. _log.warning("Skill 4515 at level 1 is missing in DP.");
  4334. }
  4335. damage = 0; // prevents messing up drop calculation
  4336. }
  4337. }
  4338. // If L2Character target is a L2PcInstance, send a system message
  4339. if (target.isPlayer())
  4340. {
  4341. L2PcInstance enemy = target.getActingPlayer();
  4342. enemy.getAI().clientStartAutoAttack();
  4343. }
  4344. if (!miss && (damage > 0))
  4345. {
  4346. L2Weapon weapon = getActiveWeaponItem();
  4347. boolean isBow = ((weapon != null) && ((weapon.getItemType() == WeaponType.BOW) || (weapon.getItemType() == WeaponType.CROSSBOW)));
  4348. int reflectedDamage = 0;
  4349. if (!isBow && !target.isInvul()) // Do not reflect if weapon is of type bow or target is invunlerable
  4350. {
  4351. // quick fix for no drop from raid if boss attack high-level char with damage reflection
  4352. if (!target.isRaid() || (getActingPlayer() == null) || (getActingPlayer().getLevel() <= (target.getLevel() + 8)))
  4353. {
  4354. // Reduce HP of the target and calculate reflection damage to reduce HP of attacker if necessary
  4355. double reflectPercent = target.getStat().calcStat(Stats.REFLECT_DAMAGE_PERCENT, 0, null, null);
  4356. if (reflectPercent > 0)
  4357. {
  4358. reflectedDamage = (int) ((reflectPercent / 100.) * damage);
  4359. if (reflectedDamage > target.getMaxHp())
  4360. {
  4361. reflectedDamage = target.getMaxHp();
  4362. }
  4363. }
  4364. }
  4365. }
  4366. // reduce targets HP
  4367. target.reduceCurrentHp(damage, this, null);
  4368. target.notifyDamageReceived(damage, this, null, crit, false);
  4369. if (reflectedDamage > 0)
  4370. {
  4371. reduceCurrentHp(reflectedDamage, target, true, false, null);
  4372. notifyDamageReceived(reflectedDamage, target, null, crit, false);
  4373. }
  4374. if (!isBow) // Do not absorb if weapon is of type bow
  4375. {
  4376. // Absorb HP from the damage inflicted
  4377. double absorbPercent = getStat().calcStat(Stats.ABSORB_DAMAGE_PERCENT, 0, null, null);
  4378. if (absorbPercent > 0)
  4379. {
  4380. int maxCanAbsorb = (int) (getMaxRecoverableHp() - getCurrentHp());
  4381. int absorbDamage = (int) ((absorbPercent / 100.) * damage);
  4382. if (absorbDamage > maxCanAbsorb)
  4383. {
  4384. absorbDamage = maxCanAbsorb; // Can't absord more than max hp
  4385. }
  4386. if (absorbDamage > 0)
  4387. {
  4388. setCurrentHp(getCurrentHp() + absorbDamage);
  4389. }
  4390. }
  4391. // Absorb MP from the damage inflicted
  4392. absorbPercent = getStat().calcStat(Stats.ABSORB_MANA_DAMAGE_PERCENT, 0, null, null);
  4393. if (absorbPercent > 0)
  4394. {
  4395. int maxCanAbsorb = (int) (getMaxRecoverableMp() - getCurrentMp());
  4396. int absorbDamage = (int) ((absorbPercent / 100.) * damage);
  4397. if (absorbDamage > maxCanAbsorb)
  4398. {
  4399. absorbDamage = maxCanAbsorb; // Can't absord more than max hp
  4400. }
  4401. if (absorbDamage > 0)
  4402. {
  4403. setCurrentMp(getCurrentMp() + absorbDamage);
  4404. }
  4405. }
  4406. }
  4407. // Notify AI with EVT_ATTACKED
  4408. if (target.hasAI())
  4409. {
  4410. target.getAI().notifyEvent(CtrlEvent.EVT_ATTACKED, this);
  4411. }
  4412. getAI().clientStartAutoAttack();
  4413. if (isSummon())
  4414. {
  4415. L2PcInstance owner = ((L2Summon) this).getOwner();
  4416. if (owner != null)
  4417. {
  4418. owner.getAI().clientStartAutoAttack();
  4419. }
  4420. }
  4421. // Manage attack or cast break of the target (calculating rate, sending message...)
  4422. if (!target.isRaid() && Formulas.calcAtkBreak(target, damage))
  4423. {
  4424. target.breakAttack();
  4425. target.breakCast();
  4426. }
  4427. if (_triggerSkills != null)
  4428. {
  4429. for (OptionsSkillHolder holder : _triggerSkills.values())
  4430. {
  4431. if ((!crit && (holder.getSkillType() == OptionsSkillType.ATTACK)) || ((holder.getSkillType() == OptionsSkillType.CRITICAL) && crit))
  4432. {
  4433. if (Rnd.get(100) < holder.getChance())
  4434. {
  4435. makeTriggerCast(holder.getSkill(), target);
  4436. }
  4437. }
  4438. }
  4439. }
  4440. // Launch weapon onCritical Special ability effect if available
  4441. if (crit && (weapon != null))
  4442. {
  4443. weapon.castOnCriticalSkill(this, target);
  4444. }
  4445. }
  4446. // Recharge any active auto-soulshot tasks for current creature.
  4447. rechargeShots(true, false);
  4448. }
  4449. /**
  4450. * Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character.
  4451. */
  4452. public void breakAttack()
  4453. {
  4454. if (isAttackingNow())
  4455. {
  4456. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  4457. abortAttack();
  4458. if (isPlayer())
  4459. {
  4460. // Send a system message
  4461. sendPacket(SystemMessageId.ATTACK_FAILED);
  4462. }
  4463. }
  4464. }
  4465. /**
  4466. * Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character.
  4467. */
  4468. public void breakCast()
  4469. {
  4470. // damage can only cancel magical & static skills
  4471. if (isCastingNow() && canAbortCast() && (getLastSkillCast() != null) && (getLastSkillCast().isMagic() || getLastSkillCast().isStatic()))
  4472. {
  4473. // Abort the cast of the L2Character and send Server->Client MagicSkillCanceld/ActionFailed packet.
  4474. abortCast();
  4475. if (isPlayer())
  4476. {
  4477. // Send a system message
  4478. sendPacket(SystemMessageId.CASTING_INTERRUPTED);
  4479. }
  4480. }
  4481. }
  4482. /**
  4483. * Reduce the arrow number of the L2Character.<br>
  4484. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4485. * @param bolts
  4486. */
  4487. protected void reduceArrowCount(boolean bolts)
  4488. {
  4489. // default is to do nothing
  4490. }
  4491. /**
  4492. * Manage Forced attack (shift + select target).<br>
  4493. * <B><U>Actions</U>:</B>
  4494. * <ul>
  4495. * <li>If L2Character or target is in a town area, send a system message TARGET_IN_PEACEZONE a Server->Client packet ActionFailed</li>
  4496. * <li>If target is confused, send a Server->Client packet ActionFailed</li>
  4497. * <li>If L2Character is a L2ArtefactInstance, send a Server->Client packet ActionFailed</li>
  4498. * <li>Send a Server->Client packet MyTargetSelected to start attack and Notify AI with AI_INTENTION_ATTACK</li>
  4499. * </ul>
  4500. * @param player The L2PcInstance to attack
  4501. */
  4502. @Override
  4503. public void onForcedAttack(L2PcInstance player)
  4504. {
  4505. if (isInsidePeaceZone(player))
  4506. {
  4507. // If L2Character or target is in a peace zone, send a system message TARGET_IN_PEACEZONE a Server->Client packet ActionFailed
  4508. player.sendPacket(SystemMessageId.TARGET_IN_PEACEZONE);
  4509. player.sendPacket(ActionFailed.STATIC_PACKET);
  4510. return;
  4511. }
  4512. if (player.isInOlympiadMode() && (player.getTarget() != null) && player.getTarget().isPlayable())
  4513. {
  4514. L2PcInstance target = null;
  4515. L2Object object = player.getTarget();
  4516. if ((object != null) && object.isPlayable())
  4517. {
  4518. target = object.getActingPlayer();
  4519. }
  4520. if ((target == null) || (target.isInOlympiadMode() && (!player.isOlympiadStart() || (player.getOlympiadGameId() != target.getOlympiadGameId()))))
  4521. {
  4522. // if L2PcInstance is in Olympia and the match isn't already start, send a Server->Client packet ActionFailed
  4523. player.sendPacket(ActionFailed.STATIC_PACKET);
  4524. return;
  4525. }
  4526. }
  4527. if ((player.getTarget() != null) && !player.getTarget().canBeAttacked() && !player.getAccessLevel().allowPeaceAttack())
  4528. {
  4529. // If target is not attackable, send a Server->Client packet ActionFailed
  4530. player.sendPacket(ActionFailed.STATIC_PACKET);
  4531. return;
  4532. }
  4533. if (player.isConfused())
  4534. {
  4535. // If target is confused, send a Server->Client packet ActionFailed
  4536. player.sendPacket(ActionFailed.STATIC_PACKET);
  4537. return;
  4538. }
  4539. // GeoData Los Check or dz > 1000
  4540. if (!GeoData.getInstance().canSeeTarget(player, this))
  4541. {
  4542. player.sendPacket(SystemMessageId.CANT_SEE_TARGET);
  4543. player.sendPacket(ActionFailed.STATIC_PACKET);
  4544. return;
  4545. }
  4546. if (player.getBlockCheckerArena() != -1)
  4547. {
  4548. player.sendPacket(ActionFailed.STATIC_PACKET);
  4549. return;
  4550. }
  4551. // Notify AI with AI_INTENTION_ATTACK
  4552. player.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, this);
  4553. }
  4554. /**
  4555. * @param attacker
  4556. * @return True if inside peace zone.
  4557. */
  4558. public boolean isInsidePeaceZone(L2PcInstance attacker)
  4559. {
  4560. return isInsidePeaceZone(attacker, this);
  4561. }
  4562. public boolean isInsidePeaceZone(L2PcInstance attacker, L2Object target)
  4563. {
  4564. return (!attacker.getAccessLevel().allowPeaceAttack() && isInsidePeaceZone((L2Object) attacker, target));
  4565. }
  4566. public boolean isInsidePeaceZone(L2Object attacker, L2Object target)
  4567. {
  4568. if (target == null)
  4569. {
  4570. return false;
  4571. }
  4572. if (!(target.isPlayable() && attacker.isPlayable()))
  4573. {
  4574. return false;
  4575. }
  4576. if (InstanceManager.getInstance().getInstance(getInstanceId()).isPvPInstance())
  4577. {
  4578. return false;
  4579. }
  4580. if (TerritoryWarManager.PLAYER_WITH_WARD_CAN_BE_KILLED_IN_PEACEZONE && TerritoryWarManager.getInstance().isTWInProgress())
  4581. {
  4582. if (target.isPlayer() && target.getActingPlayer().isCombatFlagEquipped())
  4583. {
  4584. return false;
  4585. }
  4586. }
  4587. if (Config.ALT_GAME_KARMA_PLAYER_CAN_BE_KILLED_IN_PEACEZONE)
  4588. {
  4589. // allows red to be attacked and red to attack flagged players
  4590. if ((target.getActingPlayer() != null) && (target.getActingPlayer().getKarma() > 0))
  4591. {
  4592. return false;
  4593. }
  4594. if ((attacker.getActingPlayer() != null) && (attacker.getActingPlayer().getKarma() > 0) && (target.getActingPlayer() != null) && (target.getActingPlayer().getPvpFlag() > 0))
  4595. {
  4596. return false;
  4597. }
  4598. }
  4599. return (target.isInsideZone(ZoneId.PEACE) || attacker.isInsideZone(ZoneId.PEACE));
  4600. }
  4601. /**
  4602. * @return true if this character is inside an active grid.
  4603. */
  4604. public boolean isInActiveRegion()
  4605. {
  4606. L2WorldRegion region = getWorldRegion();
  4607. return ((region != null) && (region.isActive()));
  4608. }
  4609. /**
  4610. * @return True if the L2Character has a Party in progress.
  4611. */
  4612. public boolean isInParty()
  4613. {
  4614. return false;
  4615. }
  4616. /**
  4617. * @return the L2Party object of the L2Character.
  4618. */
  4619. public L2Party getParty()
  4620. {
  4621. return null;
  4622. }
  4623. /**
  4624. * @param target
  4625. * @param weapon
  4626. * @return the Attack Speed of the L2Character (delay (in milliseconds) before next attack).
  4627. */
  4628. public int calculateTimeBetweenAttacks(L2Character target, L2Weapon weapon)
  4629. {
  4630. if ((weapon != null) && !isTransformed())
  4631. {
  4632. switch (weapon.getItemType())
  4633. {
  4634. case BOW:
  4635. return (1500 * 345) / getPAtkSpd();
  4636. case CROSSBOW:
  4637. return (1200 * 345) / getPAtkSpd();
  4638. case DAGGER:
  4639. // atkSpd /= 1.15;
  4640. break;
  4641. }
  4642. }
  4643. return Formulas.calcPAtkSpd(this, target, getPAtkSpd());
  4644. }
  4645. public int calculateReuseTime(L2Character target, L2Weapon weapon)
  4646. {
  4647. if ((weapon == null) || isTransformed())
  4648. {
  4649. return 0;
  4650. }
  4651. int reuse = weapon.getReuseDelay();
  4652. // only bows should continue for now
  4653. if (reuse == 0)
  4654. {
  4655. return 0;
  4656. }
  4657. reuse *= getStat().getWeaponReuseModifier(target);
  4658. double atkSpd = getStat().getPAtkSpd();
  4659. switch (weapon.getItemType())
  4660. {
  4661. case BOW:
  4662. case CROSSBOW:
  4663. return (int) ((reuse * 345) / atkSpd);
  4664. default:
  4665. return (int) ((reuse * 312) / atkSpd);
  4666. }
  4667. }
  4668. /**
  4669. * Add a skill to the L2Character _skills and its Func objects to the calculator set of the L2Character.<br>
  4670. * <B><U>Concept</U>:</B><br>
  4671. * All skills own by a L2Character are identified in <B>_skills</B><br>
  4672. * <B><U>Actions</U>:</B>
  4673. * <ul>
  4674. * <li>Replace oldSkill by newSkill or Add the newSkill</li>
  4675. * <li>If an old skill has been replaced, remove all its Func objects of L2Character calculator set</li>
  4676. * <li>Add Func objects of newSkill to the calculator set of the L2Character</li>
  4677. * </ul>
  4678. * <B><U>Overridden in</U>:</B>
  4679. * <ul>
  4680. * <li>L2PcInstance : Save update in the character_skills table of the database</li>
  4681. * </ul>
  4682. * @param newSkill The L2Skill to add to the L2Character
  4683. * @return The L2Skill replaced or null if just added a new L2Skill
  4684. */
  4685. @Override
  4686. public Skill addSkill(Skill newSkill)
  4687. {
  4688. Skill oldSkill = null;
  4689. if (newSkill != null)
  4690. {
  4691. // Replace oldSkill by newSkill or Add the newSkill
  4692. oldSkill = _skills.put(newSkill.getId(), newSkill);
  4693. // If an old skill has been replaced, remove all its Func objects
  4694. if (oldSkill != null)
  4695. {
  4696. removeStatsOwner(oldSkill);
  4697. }
  4698. // Add Func objects of newSkill to the calculator set of the L2Character
  4699. addStatFuncs(newSkill.getStatFuncs(null, this));
  4700. if (newSkill.isPassive())
  4701. {
  4702. newSkill.applyEffects(this, this, false, true, false, 0);
  4703. }
  4704. }
  4705. return oldSkill;
  4706. }
  4707. public Skill removeSkill(Skill skill, boolean cancelEffect)
  4708. {
  4709. return (skill != null) ? removeSkill(skill.getId(), cancelEffect) : null;
  4710. }
  4711. public Skill removeSkill(int skillId)
  4712. {
  4713. return removeSkill(skillId, true);
  4714. }
  4715. public Skill removeSkill(int skillId, boolean cancelEffect)
  4716. {
  4717. // Remove the skill from the L2Character _skills
  4718. Skill oldSkill = _skills.remove(skillId);
  4719. // Remove all its Func objects from the L2Character calculator set
  4720. if (oldSkill != null)
  4721. {
  4722. // Stop casting if this skill is used right now
  4723. if ((getLastSkillCast() != null) && isCastingNow())
  4724. {
  4725. if (oldSkill.getId() == getLastSkillCast().getId())
  4726. {
  4727. abortCast();
  4728. }
  4729. }
  4730. if ((getLastSimultaneousSkillCast() != null) && isCastingSimultaneouslyNow())
  4731. {
  4732. if (oldSkill.getId() == getLastSimultaneousSkillCast().getId())
  4733. {
  4734. abortCast();
  4735. }
  4736. }
  4737. // Stop effects.
  4738. if (cancelEffect || oldSkill.isToggle() || oldSkill.isPassive())
  4739. {
  4740. removeStatsOwner(oldSkill);
  4741. stopSkillEffects(true, oldSkill.getId());
  4742. }
  4743. }
  4744. return oldSkill;
  4745. }
  4746. /**
  4747. * <B><U>Concept</U>:</B><br>
  4748. * All skills own by a L2Character are identified in <B>_skills</B> the L2Character
  4749. * @return all skills own by the L2Character in a table of L2Skill.
  4750. */
  4751. public final Collection<Skill> getAllSkills()
  4752. {
  4753. return new ArrayList<>(_skills.values());
  4754. }
  4755. /**
  4756. * @return the map containing this character skills.
  4757. */
  4758. @Override
  4759. public Map<Integer, Skill> getSkills()
  4760. {
  4761. return _skills;
  4762. }
  4763. /**
  4764. * Return the level of a skill owned by the L2Character.
  4765. * @param skillId The identifier of the L2Skill whose level must be returned
  4766. * @return The level of the L2Skill identified by skillId
  4767. */
  4768. @Override
  4769. public int getSkillLevel(int skillId)
  4770. {
  4771. final Skill skill = getKnownSkill(skillId);
  4772. return (skill == null) ? -1 : skill.getLevel();
  4773. }
  4774. /**
  4775. * @param skillId The identifier of the L2Skill to check the knowledge
  4776. * @return the skill from the known skill.
  4777. */
  4778. @Override
  4779. public final Skill getKnownSkill(int skillId)
  4780. {
  4781. return _skills.get(skillId);
  4782. }
  4783. /**
  4784. * Return the number of buffs affecting this L2Character.
  4785. * @return The number of Buffs affecting this L2Character
  4786. */
  4787. public int getBuffCount()
  4788. {
  4789. return _effectList.getBuffCount();
  4790. }
  4791. public int getDanceCount()
  4792. {
  4793. return _effectList.getDanceCount();
  4794. }
  4795. /**
  4796. * Manage the magic skill launching task (MP, HP, Item consumation...) and display the magic skill animation on client.<br>
  4797. * <B><U>Actions</U>:</B>
  4798. * <ul>
  4799. * <li>Send a Server->Client packet MagicSkillLaunched (to display magic skill animation) to all L2PcInstance of L2Charcater _knownPlayers</li>
  4800. * <li>Consumme MP, HP and Item if necessary</li>
  4801. * <li>Send a Server->Client packet StatusUpdate with MP modification to the L2PcInstance</li>
  4802. * <li>Launch the magic skill in order to calculate its effects</li>
  4803. * <li>If the skill type is PDAM, notify the AI of the target with AI_INTENTION_ATTACK</li>
  4804. * <li>Notify the AI of the L2Character with EVT_FINISH_CASTING</li>
  4805. * </ul>
  4806. * <FONT COLOR=#FF0000><B><U>Caution</U>: A magic skill casting MUST BE in progress</B></FONT>
  4807. * @param mut
  4808. */
  4809. public void onMagicLaunchedTimer(MagicUseTask mut)
  4810. {
  4811. final Skill skill = mut.getSkill();
  4812. L2Object[] targets = mut.getTargets();
  4813. if ((skill == null) || (targets == null))
  4814. {
  4815. abortCast();
  4816. return;
  4817. }
  4818. if (targets.length == 0)
  4819. {
  4820. switch (skill.getTargetType())
  4821. {
  4822. // only AURA-type skills can be cast without target
  4823. case AURA:
  4824. case FRONT_AURA:
  4825. case BEHIND_AURA:
  4826. case AURA_CORPSE_MOB:
  4827. break;
  4828. default:
  4829. abortCast();
  4830. return;
  4831. }
  4832. }
  4833. // Escaping from under skill's radius and peace zone check. First version, not perfect in AoE skills.
  4834. int escapeRange = 0;
  4835. if (skill.getEffectRange() > escapeRange)
  4836. {
  4837. escapeRange = skill.getEffectRange();
  4838. }
  4839. else if ((skill.getCastRange() < 0) && (skill.getAffectRange() > 80))
  4840. {
  4841. escapeRange = skill.getAffectRange();
  4842. }
  4843. if ((targets.length > 0) && (escapeRange > 0))
  4844. {
  4845. int skipRange = 0;
  4846. int skipLOS = 0;
  4847. int skipPeaceZone = 0;
  4848. final List<L2Object> targetList = new ArrayList<>();
  4849. for (L2Object target : targets)
  4850. {
  4851. if (target instanceof L2Character)
  4852. {
  4853. if (!isInsideRadius(target.getX(), target.getY(), target.getZ(), escapeRange + getTemplate().getCollisionRadius(), true, false))
  4854. {
  4855. skipRange++;
  4856. continue;
  4857. }
  4858. // Healing party members should ignore LOS.
  4859. if (((skill.getTargetType() != L2TargetType.PARTY) || !skill.hasEffectType(L2EffectType.HEAL)) //
  4860. && !GeoData.getInstance().canSeeTarget(this, target))
  4861. {
  4862. skipLOS++;
  4863. continue;
  4864. }
  4865. if (skill.isBad())
  4866. {
  4867. if (isPlayer())
  4868. {
  4869. if (((L2Character) target).isInsidePeaceZone(getActingPlayer()))
  4870. {
  4871. skipPeaceZone++;
  4872. continue;
  4873. }
  4874. }
  4875. else
  4876. {
  4877. if (((L2Character) target).isInsidePeaceZone(this, target))
  4878. {
  4879. skipPeaceZone++;
  4880. continue;
  4881. }
  4882. }
  4883. }
  4884. targetList.add(target);
  4885. }
  4886. }
  4887. if (targetList.isEmpty())
  4888. {
  4889. if (isPlayer())
  4890. {
  4891. if (skipRange > 0)
  4892. {
  4893. sendPacket(SystemMessageId.DIST_TOO_FAR_CASTING_STOPPED);
  4894. }
  4895. else if (skipLOS > 0)
  4896. {
  4897. sendPacket(SystemMessageId.CANT_SEE_TARGET);
  4898. }
  4899. else if (skipPeaceZone > 0)
  4900. {
  4901. sendPacket(SystemMessageId.A_MALICIOUS_SKILL_CANNOT_BE_USED_IN_PEACE_ZONE);
  4902. }
  4903. }
  4904. abortCast();
  4905. return;
  4906. }
  4907. mut.setTargets(targetList.toArray(new L2Object[targetList.size()]));
  4908. }
  4909. // Ensure that a cast is in progress
  4910. // Check if player is using fake death.
  4911. // Static skills can be used while faking death.
  4912. if ((mut.isSimultaneous() && !isCastingSimultaneouslyNow()) || (!mut.isSimultaneous() && !isCastingNow()) || (isAlikeDead() && !skill.isStatic()))
  4913. {
  4914. // now cancels both, simultaneous and normal
  4915. getAI().notifyEvent(CtrlEvent.EVT_CANCEL);
  4916. return;
  4917. }
  4918. if (!skill.isToggle())
  4919. {
  4920. broadcastPacket(new MagicSkillLaunched(this, skill.getDisplayId(), skill.getDisplayLevel(), targets));
  4921. }
  4922. mut.setPhase(2);
  4923. if (mut.getSkillTime() == 0)
  4924. {
  4925. onMagicHitTimer(mut);
  4926. }
  4927. else
  4928. {
  4929. _skillCast = ThreadPoolManager.getInstance().scheduleEffect(mut, 400);
  4930. }
  4931. }
  4932. // Runs in the end of skill casting
  4933. public void onMagicHitTimer(MagicUseTask mut)
  4934. {
  4935. final Skill skill = mut.getSkill();
  4936. final L2Object[] targets = mut.getTargets();
  4937. if ((skill == null) || (targets == null))
  4938. {
  4939. abortCast();
  4940. return;
  4941. }
  4942. try
  4943. {
  4944. // Go through targets table
  4945. for (L2Object tgt : targets)
  4946. {
  4947. if (tgt.isPlayable())
  4948. {
  4949. if (isPlayer() && tgt.isSummon())
  4950. {
  4951. ((L2Summon) tgt).updateAndBroadcastStatus(1);
  4952. }
  4953. }
  4954. else if (isPlayable() && tgt.isAttackable())
  4955. {
  4956. L2Character target = (L2Character) tgt;
  4957. if (skill.getEffectPoint() > 0)
  4958. {
  4959. ((L2Attackable) target).reduceHate(this, skill.getEffectPoint());
  4960. }
  4961. else if (skill.getEffectPoint() < 0)
  4962. {
  4963. ((L2Attackable) target).addDamageHate(this, 0, -skill.getEffectPoint());
  4964. }
  4965. }
  4966. }
  4967. rechargeShots(skill.useSoulShot(), skill.useSpiritShot());
  4968. final StatusUpdate su = new StatusUpdate(this);
  4969. boolean isSendStatus = false;
  4970. // Consume MP of the L2Character and Send the Server->Client packet StatusUpdate with current HP and MP to all other L2PcInstance to inform
  4971. double mpConsume = getStat().getMpConsume(skill);
  4972. if (mpConsume > 0)
  4973. {
  4974. if (mpConsume > getCurrentMp())
  4975. {
  4976. sendPacket(SystemMessageId.NOT_ENOUGH_MP);
  4977. abortCast();
  4978. return;
  4979. }
  4980. getStatus().reduceMp(mpConsume);
  4981. su.addAttribute(StatusUpdate.CUR_MP, (int) getCurrentMp());
  4982. isSendStatus = true;
  4983. }
  4984. // Consume HP if necessary and Send the Server->Client packet StatusUpdate with current HP and MP to all other L2PcInstance to inform
  4985. if (skill.getHpConsume() > 0)
  4986. {
  4987. double consumeHp = skill.getHpConsume();
  4988. if (consumeHp >= getCurrentHp())
  4989. {
  4990. sendPacket(SystemMessageId.NOT_ENOUGH_HP);
  4991. abortCast();
  4992. return;
  4993. }
  4994. getStatus().reduceHp(consumeHp, this, true);
  4995. su.addAttribute(StatusUpdate.CUR_HP, (int) getCurrentHp());
  4996. isSendStatus = true;
  4997. }
  4998. // Send a Server->Client packet StatusUpdate with MP modification to the L2PcInstance
  4999. if (isSendStatus)
  5000. {
  5001. sendPacket(su);
  5002. }
  5003. if (isPlayer())
  5004. {
  5005. // Consume Charges
  5006. if (skill.getChargeConsume() > 0)
  5007. {
  5008. getActingPlayer().decreaseCharges(skill.getChargeConsume());
  5009. }
  5010. // Consume Souls if necessary
  5011. if (skill.getMaxSoulConsumeCount() > 0)
  5012. {
  5013. if (!getActingPlayer().decreaseSouls(skill.getMaxSoulConsumeCount(), skill))
  5014. {
  5015. abortCast();
  5016. return;
  5017. }
  5018. }
  5019. }
  5020. // Launch the magic skill in order to calculate its effects
  5021. callSkill(mut.getSkill(), mut.getTargets());
  5022. }
  5023. catch (NullPointerException e)
  5024. {
  5025. _log.log(Level.WARNING, "", e);
  5026. }
  5027. if (mut.getSkillTime() > 0)
  5028. {
  5029. mut.setCount(mut.getCount() + 1);
  5030. }
  5031. mut.setPhase(3);
  5032. if (mut.getSkillTime() == 0)
  5033. {
  5034. onMagicFinalizer(mut);
  5035. }
  5036. else
  5037. {
  5038. if (mut.isSimultaneous())
  5039. {
  5040. _skillCast2 = ThreadPoolManager.getInstance().scheduleEffect(mut, 0);
  5041. }
  5042. else
  5043. {
  5044. _skillCast = ThreadPoolManager.getInstance().scheduleEffect(mut, 0);
  5045. }
  5046. }
  5047. }
  5048. // Runs after skillTime
  5049. public void onMagicFinalizer(MagicUseTask mut)
  5050. {
  5051. if (mut.isSimultaneous())
  5052. {
  5053. _skillCast2 = null;
  5054. setIsCastingSimultaneouslyNow(false);
  5055. return;
  5056. }
  5057. // Cleanup
  5058. _skillCast = null;
  5059. _castInterruptTime = 0;
  5060. // Stop casting
  5061. setIsCastingNow(false);
  5062. final Skill skill = mut.getSkill();
  5063. final L2Object target = mut.getTargets().length > 0 ? mut.getTargets()[0] : null;
  5064. // On each repeat recharge shots before cast.
  5065. if (mut.getCount() > 0)
  5066. {
  5067. rechargeShots(mut.getSkill().useSoulShot(), mut.getSkill().useSpiritShot());
  5068. }
  5069. // Attack target after skill use
  5070. if ((skill.nextActionIsAttack()) && (getTarget() instanceof L2Character) && (getTarget() != this) && (target != null) && (getTarget() == target) && target.canBeAttacked())
  5071. {
  5072. if ((getAI().getNextIntention() == null) || (getAI().getNextIntention().getCtrlIntention() != CtrlIntention.AI_INTENTION_MOVE_TO))
  5073. {
  5074. getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  5075. }
  5076. }
  5077. if (skill.isBad() && (skill.getTargetType() != L2TargetType.UNLOCKABLE))
  5078. {
  5079. getAI().clientStartAutoAttack();
  5080. }
  5081. // Notify the AI of the L2Character with EVT_FINISH_CASTING
  5082. getAI().notifyEvent(CtrlEvent.EVT_FINISH_CASTING);
  5083. // Notify DP Scripts
  5084. notifyQuestEventSkillFinished(skill, target);
  5085. // If character is a player, then wipe their current cast state and check if a skill is queued.
  5086. // If there is a queued skill, launch it and wipe the queue.
  5087. if (isPlayer())
  5088. {
  5089. L2PcInstance currPlayer = getActingPlayer();
  5090. SkillUseHolder queuedSkill = currPlayer.getQueuedSkill();
  5091. currPlayer.setCurrentSkill(null, false, false);
  5092. if (queuedSkill != null)
  5093. {
  5094. currPlayer.setQueuedSkill(null, false, false);
  5095. // DON'T USE : Recursive call to useMagic() method
  5096. // currPlayer.useMagic(queuedSkill.getSkill(), queuedSkill.isCtrlPressed(), queuedSkill.isShiftPressed());
  5097. ThreadPoolManager.getInstance().executeGeneral(new QueuedMagicUseTask(currPlayer, queuedSkill.getSkill(), queuedSkill.isCtrlPressed(), queuedSkill.isShiftPressed()));
  5098. }
  5099. }
  5100. if (isChanneling())
  5101. {
  5102. getSkillChannelizer().stopChanneling();
  5103. }
  5104. }
  5105. // Quest event ON_SPELL_FNISHED
  5106. protected void notifyQuestEventSkillFinished(Skill skill, L2Object target)
  5107. {
  5108. }
  5109. /**
  5110. * Launch the magic skill and calculate its effects on each target contained in the targets table.
  5111. * @param skill The L2Skill to use
  5112. * @param targets The table of L2Object targets
  5113. */
  5114. public void callSkill(Skill skill, L2Object[] targets)
  5115. {
  5116. try
  5117. {
  5118. L2Weapon activeWeapon = getActiveWeaponItem();
  5119. // Check if the toggle skill effects are already in progress on the L2Character
  5120. if (skill.isToggle() && isAffectedBySkill(skill.getId()))
  5121. {
  5122. return;
  5123. }
  5124. // Initial checks
  5125. for (L2Object obj : targets)
  5126. {
  5127. if ((obj == null) || !obj.isCharacter())
  5128. {
  5129. continue;
  5130. }
  5131. final L2Character target = (L2Character) obj;
  5132. // Check raid monster attack and check buffing characters who attack raid monsters.
  5133. L2Character targetsAttackTarget = null;
  5134. L2Character targetsCastTarget = null;
  5135. if (target.hasAI())
  5136. {
  5137. targetsAttackTarget = target.getAI().getAttackTarget();
  5138. targetsCastTarget = target.getAI().getCastTarget();
  5139. }
  5140. if (!Config.RAID_DISABLE_CURSE && ((target.isRaid() && target.giveRaidCurse() && (getLevel() > (target.getLevel() + 8))) || (!skill.isBad() && (targetsAttackTarget != null) && targetsAttackTarget.isRaid() && targetsAttackTarget.giveRaidCurse() && targetsAttackTarget.getAttackByList().contains(target) && (getLevel() > (targetsAttackTarget.getLevel() + 8))) || (!skill.isBad() && (targetsCastTarget != null) && targetsCastTarget.isRaid() && targetsCastTarget.giveRaidCurse() && targetsCastTarget.getAttackByList().contains(target) && (getLevel() > (targetsCastTarget.getLevel() + 8)))))
  5141. {
  5142. final CommonSkill curse = skill.isMagic() ? CommonSkill.RAID_CURSE : CommonSkill.RAID_CURSE2;
  5143. Skill curseSkill = curse.getSkill();
  5144. if (curseSkill != null)
  5145. {
  5146. abortAttack();
  5147. abortCast();
  5148. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  5149. curseSkill.applyEffects(target, this);
  5150. }
  5151. else
  5152. {
  5153. _log.warning("Skill ID " + curse.getId() + " level " + curse.getLevel() + " is missing in DP!");
  5154. }
  5155. return;
  5156. }
  5157. // Check if over-hit is possible
  5158. if (skill.isOverhit())
  5159. {
  5160. if (target.isAttackable())
  5161. {
  5162. ((L2Attackable) target).overhitEnabled(true);
  5163. }
  5164. }
  5165. // Static skills not trigger any chance skills
  5166. if (!skill.isStatic())
  5167. {
  5168. // Launch weapon Special ability skill effect if available
  5169. if ((activeWeapon != null) && !target.isDead())
  5170. {
  5171. activeWeapon.castOnMagicSkill(this, target, skill);
  5172. }
  5173. if (_triggerSkills != null)
  5174. {
  5175. for (OptionsSkillHolder holder : _triggerSkills.values())
  5176. {
  5177. if ((skill.isMagic() && (holder.getSkillType() == OptionsSkillType.MAGIC)) || (skill.isPhysical() && (holder.getSkillType() == OptionsSkillType.ATTACK)))
  5178. {
  5179. if (Rnd.get(100) < holder.getChance())
  5180. {
  5181. makeTriggerCast(holder.getSkill(), target);
  5182. }
  5183. }
  5184. }
  5185. }
  5186. }
  5187. }
  5188. // Launch the magic skill and calculate its effects
  5189. skill.activateSkill(this, targets);
  5190. L2PcInstance player = getActingPlayer();
  5191. if (player != null)
  5192. {
  5193. for (L2Object target : targets)
  5194. {
  5195. // EVT_ATTACKED and PvPStatus
  5196. if (target instanceof L2Character)
  5197. {
  5198. if (skill.getEffectPoint() <= 0)
  5199. {
  5200. if ((target.isPlayable() || target.isTrap()) && skill.isBad())
  5201. {
  5202. // Casted on target_self but don't harm self
  5203. if (!target.equals(this))
  5204. {
  5205. // Combat-mode check
  5206. if (target.isPlayer())
  5207. {
  5208. target.getActingPlayer().getAI().clientStartAutoAttack();
  5209. }
  5210. else if (target.isSummon() && ((L2Character) target).hasAI())
  5211. {
  5212. L2PcInstance owner = ((L2Summon) target).getOwner();
  5213. if (owner != null)
  5214. {
  5215. owner.getAI().clientStartAutoAttack();
  5216. }
  5217. }
  5218. // attack of the own pet does not flag player
  5219. // triggering trap not flag trap owner
  5220. if ((player.getSummon() != target) && !isTrap() && !((skill.getEffectPoint() == 0) && (skill.getAffectRange() > 0)))
  5221. {
  5222. player.updatePvPStatus((L2Character) target);
  5223. }
  5224. }
  5225. }
  5226. else if (target.isAttackable())
  5227. {
  5228. switch (skill.getId())
  5229. {
  5230. case 51: // Lure
  5231. case 511: // Temptation
  5232. break;
  5233. default:
  5234. // add attacker into list
  5235. ((L2Character) target).addAttackerToAttackByList(this);
  5236. }
  5237. }
  5238. // notify target AI about the attack
  5239. if (((L2Character) target).hasAI() && !skill.hasEffectType(L2EffectType.HATE))
  5240. {
  5241. ((L2Character) target).getAI().notifyEvent(CtrlEvent.EVT_ATTACKED, this);
  5242. }
  5243. }
  5244. else
  5245. {
  5246. if (target.isPlayer())
  5247. {
  5248. // Casting non offensive skill on player with pvp flag set or with karma
  5249. if (!(target.equals(this) || target.equals(player)) && ((target.getActingPlayer().getPvpFlag() > 0) || (target.getActingPlayer().getKarma() > 0)))
  5250. {
  5251. player.updatePvPStatus();
  5252. }
  5253. }
  5254. else if (target.isAttackable())
  5255. {
  5256. player.updatePvPStatus();
  5257. }
  5258. }
  5259. }
  5260. }
  5261. // Mobs in range 1000 see spell
  5262. Collection<L2Object> objs = player.getKnownList().getKnownObjects().values();
  5263. for (L2Object spMob : objs)
  5264. {
  5265. if ((spMob != null) && spMob.isNpc())
  5266. {
  5267. final L2Npc npcMob = (L2Npc) spMob;
  5268. if ((npcMob.isInsideRadius(player, 1000, true, true)))
  5269. {
  5270. EventDispatcher.getInstance().notifyEventAsync(new OnNpcSkillSee(npcMob, player, skill, targets, isSummon()), npcMob);
  5271. // On Skill See logic
  5272. if (npcMob.isAttackable())
  5273. {
  5274. final L2Attackable attackable = (L2Attackable) npcMob;
  5275. int skillEffectPoint = skill.getEffectPoint();
  5276. if (player.hasSummon())
  5277. {
  5278. if ((targets.length == 1) && Util.contains(targets, player.getSummon()))
  5279. {
  5280. skillEffectPoint = 0;
  5281. }
  5282. }
  5283. if (skillEffectPoint > 0)
  5284. {
  5285. if (attackable.hasAI() && (attackable.getAI().getIntention() == AI_INTENTION_ATTACK))
  5286. {
  5287. L2Object npcTarget = attackable.getTarget();
  5288. for (L2Object skillTarget : targets)
  5289. {
  5290. if ((npcTarget == skillTarget) || (npcMob == skillTarget))
  5291. {
  5292. L2Character originalCaster = isSummon() ? getSummon() : player;
  5293. attackable.addDamageHate(originalCaster, 0, (skillEffectPoint * 150) / (attackable.getLevel() + 7));
  5294. }
  5295. }
  5296. }
  5297. }
  5298. }
  5299. }
  5300. }
  5301. }
  5302. }
  5303. // Notify AI
  5304. if (skill.isBad() && !skill.hasEffectType(L2EffectType.HATE))
  5305. {
  5306. for (L2Object target : targets)
  5307. {
  5308. if (target instanceof L2Character)
  5309. {
  5310. final L2Character creature = (L2Character) target;
  5311. if (creature.hasAI())
  5312. {
  5313. // Notify target AI about the attack
  5314. creature.getAI().notifyEvent(CtrlEvent.EVT_ATTACKED, this);
  5315. }
  5316. }
  5317. }
  5318. }
  5319. }
  5320. catch (Exception e)
  5321. {
  5322. _log.log(Level.WARNING, getClass().getSimpleName() + ": callSkill() failed.", e);
  5323. }
  5324. }
  5325. /**
  5326. * @param target
  5327. * @return True if the L2Character is behind the target and can't be seen.
  5328. */
  5329. public boolean isBehind(L2Object target)
  5330. {
  5331. double angleChar, angleTarget, angleDiff, maxAngleDiff = 60;
  5332. if (target == null)
  5333. {
  5334. return false;
  5335. }
  5336. if (target instanceof L2Character)
  5337. {
  5338. L2Character target1 = (L2Character) target;
  5339. angleChar = Util.calculateAngleFrom(this, target1);
  5340. angleTarget = Util.convertHeadingToDegree(target1.getHeading());
  5341. angleDiff = angleChar - angleTarget;
  5342. if (angleDiff <= (-360 + maxAngleDiff))
  5343. {
  5344. angleDiff += 360;
  5345. }
  5346. if (angleDiff >= (360 - maxAngleDiff))
  5347. {
  5348. angleDiff -= 360;
  5349. }
  5350. if (Math.abs(angleDiff) <= maxAngleDiff)
  5351. {
  5352. return true;
  5353. }
  5354. }
  5355. return false;
  5356. }
  5357. public boolean isBehindTarget()
  5358. {
  5359. return isBehind(getTarget());
  5360. }
  5361. /**
  5362. * @param target
  5363. * @return True if the target is facing the L2Character.
  5364. */
  5365. public boolean isInFrontOf(L2Character target)
  5366. {
  5367. double angleChar, angleTarget, angleDiff, maxAngleDiff = 60;
  5368. if (target == null)
  5369. {
  5370. return false;
  5371. }
  5372. angleTarget = Util.calculateAngleFrom(target, this);
  5373. angleChar = Util.convertHeadingToDegree(target.getHeading());
  5374. angleDiff = angleChar - angleTarget;
  5375. if (angleDiff <= (-360 + maxAngleDiff))
  5376. {
  5377. angleDiff += 360;
  5378. }
  5379. if (angleDiff >= (360 - maxAngleDiff))
  5380. {
  5381. angleDiff -= 360;
  5382. }
  5383. return Math.abs(angleDiff) <= maxAngleDiff;
  5384. }
  5385. /**
  5386. * @param target
  5387. * @param maxAngle
  5388. * @return true if target is in front of L2Character (shield def etc)
  5389. */
  5390. public boolean isFacing(L2Object target, int maxAngle)
  5391. {
  5392. double angleChar, angleTarget, angleDiff, maxAngleDiff;
  5393. if (target == null)
  5394. {
  5395. return false;
  5396. }
  5397. maxAngleDiff = maxAngle / 2.;
  5398. angleTarget = Util.calculateAngleFrom(this, target);
  5399. angleChar = Util.convertHeadingToDegree(getHeading());
  5400. angleDiff = angleChar - angleTarget;
  5401. if (angleDiff <= (-360 + maxAngleDiff))
  5402. {
  5403. angleDiff += 360;
  5404. }
  5405. if (angleDiff >= (360 - maxAngleDiff))
  5406. {
  5407. angleDiff -= 360;
  5408. }
  5409. return Math.abs(angleDiff) <= maxAngleDiff;
  5410. }
  5411. public boolean isInFrontOfTarget()
  5412. {
  5413. L2Object target = getTarget();
  5414. if (target instanceof L2Character)
  5415. {
  5416. return isInFrontOf((L2Character) target);
  5417. }
  5418. return false;
  5419. }
  5420. /**
  5421. * @return the Level Modifier ((level + 89) / 100).
  5422. */
  5423. public double getLevelMod()
  5424. {
  5425. return ((getLevel() + 89) / 100d);
  5426. }
  5427. public final void setSkillCast(Future<?> newSkillCast)
  5428. {
  5429. _skillCast = newSkillCast;
  5430. }
  5431. /**
  5432. * Sets _isCastingNow to true and _castInterruptTime is calculated from end time (ticks)
  5433. * @param newSkillCastEndTick
  5434. */
  5435. public final void forceIsCasting(int newSkillCastEndTick)
  5436. {
  5437. setIsCastingNow(true);
  5438. // for interrupt -400 ms
  5439. _castInterruptTime = newSkillCastEndTick - 4;
  5440. }
  5441. private boolean _AIdisabled = false;
  5442. public void updatePvPFlag(int value)
  5443. {
  5444. // Overridden in L2PcInstance
  5445. }
  5446. /**
  5447. * @return a multiplier based on weapon random damage
  5448. */
  5449. public final double getRandomDamageMultiplier()
  5450. {
  5451. L2Weapon activeWeapon = getActiveWeaponItem();
  5452. int random;
  5453. if (activeWeapon != null)
  5454. {
  5455. random = activeWeapon.getRandomDamage();
  5456. }
  5457. else
  5458. {
  5459. random = 5 + (int) Math.sqrt(getLevel());
  5460. }
  5461. return (1 + ((double) Rnd.get(0 - random, random) / 100));
  5462. }
  5463. public final long getAttackEndTime()
  5464. {
  5465. return _attackEndTime;
  5466. }
  5467. public int getBowAttackEndTime()
  5468. {
  5469. return _disableBowAttackEndTime;
  5470. }
  5471. /**
  5472. * Not Implemented.
  5473. * @return
  5474. */
  5475. public abstract int getLevel();
  5476. public final double calcStat(Stats stat, double init)
  5477. {
  5478. return getStat().calcStat(stat, init, null, null);
  5479. }
  5480. // Stat - NEED TO REMOVE ONCE L2CHARSTAT IS COMPLETE
  5481. public final double calcStat(Stats stat, double init, L2Character target, Skill skill)
  5482. {
  5483. return getStat().calcStat(stat, init, target, skill);
  5484. }
  5485. public int getAccuracy()
  5486. {
  5487. return getStat().getAccuracy();
  5488. }
  5489. public final float getAttackSpeedMultiplier()
  5490. {
  5491. return getStat().getAttackSpeedMultiplier();
  5492. }
  5493. public final double getCriticalDmg(L2Character target, double init)
  5494. {
  5495. return getStat().getCriticalDmg(target, init);
  5496. }
  5497. public int getCriticalHit(L2Character target, Skill skill)
  5498. {
  5499. return getStat().getCriticalHit(target, skill);
  5500. }
  5501. public int getEvasionRate(L2Character target)
  5502. {
  5503. return getStat().getEvasionRate(target);
  5504. }
  5505. public final int getMagicalAttackRange(Skill skill)
  5506. {
  5507. return getStat().getMagicalAttackRange(skill);
  5508. }
  5509. public final int getMaxCp()
  5510. {
  5511. return getStat().getMaxCp();
  5512. }
  5513. public final int getMaxRecoverableCp()
  5514. {
  5515. return getStat().getMaxRecoverableCp();
  5516. }
  5517. public int getMAtk(L2Character target, Skill skill)
  5518. {
  5519. return getStat().getMAtk(target, skill);
  5520. }
  5521. public int getMAtkSpd()
  5522. {
  5523. return getStat().getMAtkSpd();
  5524. }
  5525. public int getMaxMp()
  5526. {
  5527. return getStat().getMaxMp();
  5528. }
  5529. public int getMaxRecoverableMp()
  5530. {
  5531. return getStat().getMaxRecoverableMp();
  5532. }
  5533. public int getMaxHp()
  5534. {
  5535. return getStat().getMaxHp();
  5536. }
  5537. public int getMaxRecoverableHp()
  5538. {
  5539. return getStat().getMaxRecoverableHp();
  5540. }
  5541. public final int getMCriticalHit(L2Character target, Skill skill)
  5542. {
  5543. return getStat().getMCriticalHit(target, skill);
  5544. }
  5545. public int getMDef(L2Character target, Skill skill)
  5546. {
  5547. return getStat().getMDef(target, skill);
  5548. }
  5549. public double getMReuseRate(Skill skill)
  5550. {
  5551. return getStat().getMReuseRate(skill);
  5552. }
  5553. public int getPAtk(L2Character target)
  5554. {
  5555. return getStat().getPAtk(target);
  5556. }
  5557. public int getPAtkSpd()
  5558. {
  5559. return getStat().getPAtkSpd();
  5560. }
  5561. public int getPDef(L2Character target)
  5562. {
  5563. return getStat().getPDef(target);
  5564. }
  5565. public final int getPhysicalAttackRange()
  5566. {
  5567. return getStat().getPhysicalAttackRange();
  5568. }
  5569. public double getMovementSpeedMultiplier()
  5570. {
  5571. return getStat().getMovementSpeedMultiplier();
  5572. }
  5573. public double getRunSpeed()
  5574. {
  5575. return getStat().getRunSpeed();
  5576. }
  5577. public double getWalkSpeed()
  5578. {
  5579. return getStat().getWalkSpeed();
  5580. }
  5581. public final double getSwimRunSpeed()
  5582. {
  5583. return getStat().getSwimRunSpeed();
  5584. }
  5585. public final double getSwimWalkSpeed()
  5586. {
  5587. return getStat().getSwimWalkSpeed();
  5588. }
  5589. public double getMoveSpeed()
  5590. {
  5591. return getStat().getMoveSpeed();
  5592. }
  5593. public final int getShldDef()
  5594. {
  5595. return getStat().getShldDef();
  5596. }
  5597. public int getSTR()
  5598. {
  5599. return getStat().getSTR();
  5600. }
  5601. public int getDEX()
  5602. {
  5603. return getStat().getDEX();
  5604. }
  5605. public int getCON()
  5606. {
  5607. return getStat().getCON();
  5608. }
  5609. public int getINT()
  5610. {
  5611. return getStat().getINT();
  5612. }
  5613. public int getWIT()
  5614. {
  5615. return getStat().getWIT();
  5616. }
  5617. public int getMEN()
  5618. {
  5619. return getStat().getMEN();
  5620. }
  5621. // Status - NEED TO REMOVE ONCE L2CHARTATUS IS COMPLETE
  5622. public void addStatusListener(L2Character object)
  5623. {
  5624. getStatus().addStatusListener(object);
  5625. }
  5626. public void reduceCurrentHp(double i, L2Character attacker, Skill skill)
  5627. {
  5628. reduceCurrentHp(i, attacker, true, false, skill);
  5629. }
  5630. public void reduceCurrentHpByDOT(double i, L2Character attacker, Skill skill)
  5631. {
  5632. reduceCurrentHp(i, attacker, !skill.isToggle(), true, skill);
  5633. }
  5634. public void reduceCurrentHp(double i, L2Character attacker, boolean awake, boolean isDOT, Skill skill)
  5635. {
  5636. if (Config.L2JMOD_CHAMPION_ENABLE && isChampion() && (Config.L2JMOD_CHAMPION_HP != 0))
  5637. {
  5638. getStatus().reduceHp(i / Config.L2JMOD_CHAMPION_HP, attacker, awake, isDOT, false);
  5639. }
  5640. else
  5641. {
  5642. getStatus().reduceHp(i, attacker, awake, isDOT, false);
  5643. }
  5644. }
  5645. public void reduceCurrentMp(double i)
  5646. {
  5647. getStatus().reduceMp(i);
  5648. }
  5649. @Override
  5650. public void removeStatusListener(L2Character object)
  5651. {
  5652. getStatus().removeStatusListener(object);
  5653. }
  5654. protected void stopHpMpRegeneration()
  5655. {
  5656. getStatus().stopHpMpRegeneration();
  5657. }
  5658. public final double getCurrentCp()
  5659. {
  5660. return getStatus().getCurrentCp();
  5661. }
  5662. public final void setCurrentCp(double newCp)
  5663. {
  5664. getStatus().setCurrentCp(newCp);
  5665. }
  5666. public final double getCurrentHp()
  5667. {
  5668. return getStatus().getCurrentHp();
  5669. }
  5670. public final void setCurrentHp(double newHp)
  5671. {
  5672. getStatus().setCurrentHp(newHp);
  5673. }
  5674. public final void setCurrentHpMp(double newHp, double newMp)
  5675. {
  5676. getStatus().setCurrentHpMp(newHp, newMp);
  5677. }
  5678. public final double getCurrentMp()
  5679. {
  5680. return getStatus().getCurrentMp();
  5681. }
  5682. public final void setCurrentMp(double newMp)
  5683. {
  5684. getStatus().setCurrentMp(newMp);
  5685. }
  5686. /**
  5687. * @return the max weight that the L2Character can load.
  5688. */
  5689. public int getMaxLoad()
  5690. {
  5691. if (isPlayer() || isPet())
  5692. {
  5693. // Weight Limit = (CON Modifier*69000) * Skills
  5694. // Source http://l2p.bravehost.com/weightlimit.html (May 2007)
  5695. double baseLoad = Math.floor(BaseStats.CON.calcBonus(this) * 69000 * Config.ALT_WEIGHT_LIMIT);
  5696. return (int) calcStat(Stats.WEIGHT_LIMIT, baseLoad, this, null);
  5697. }
  5698. return 0;
  5699. }
  5700. public int getBonusWeightPenalty()
  5701. {
  5702. if (isPlayer() || isPet())
  5703. {
  5704. return (int) calcStat(Stats.WEIGHT_PENALTY, 1, this, null);
  5705. }
  5706. return 0;
  5707. }
  5708. /**
  5709. * @return the current weight of the L2Character.
  5710. */
  5711. public int getCurrentLoad()
  5712. {
  5713. if (isPlayer() || isPet())
  5714. {
  5715. return getInventory().getTotalWeight();
  5716. }
  5717. return 0;
  5718. }
  5719. public boolean isChampion()
  5720. {
  5721. return false;
  5722. }
  5723. /**
  5724. * Send system message about damage.
  5725. * @param target
  5726. * @param damage
  5727. * @param mcrit
  5728. * @param pcrit
  5729. * @param miss
  5730. */
  5731. public void sendDamageMessage(L2Character target, int damage, boolean mcrit, boolean pcrit, boolean miss)
  5732. {
  5733. }
  5734. public byte getAttackElement()
  5735. {
  5736. return getStat().getAttackElement();
  5737. }
  5738. public int getAttackElementValue(byte attackAttribute)
  5739. {
  5740. return getStat().getAttackElementValue(attackAttribute);
  5741. }
  5742. public int getDefenseElementValue(byte defenseAttribute)
  5743. {
  5744. return getStat().getDefenseElementValue(defenseAttribute);
  5745. }
  5746. public final void startPhysicalAttackMuted()
  5747. {
  5748. abortAttack();
  5749. }
  5750. public void disableCoreAI(boolean val)
  5751. {
  5752. _AIdisabled = val;
  5753. }
  5754. public boolean isCoreAIDisabled()
  5755. {
  5756. return _AIdisabled;
  5757. }
  5758. /**
  5759. * @return true
  5760. */
  5761. public boolean giveRaidCurse()
  5762. {
  5763. return true;
  5764. }
  5765. /**
  5766. * Check if target is affected with special buff
  5767. * @see CharEffectList#isAffected(EffectFlag)
  5768. * @param flag int
  5769. * @return boolean
  5770. */
  5771. public boolean isAffected(EffectFlag flag)
  5772. {
  5773. return _effectList.isAffected(flag);
  5774. }
  5775. public void broadcastSocialAction(int id)
  5776. {
  5777. broadcastPacket(new SocialAction(getObjectId(), id));
  5778. }
  5779. public Team getTeam()
  5780. {
  5781. return _team;
  5782. }
  5783. public void setTeam(Team team)
  5784. {
  5785. _team = team;
  5786. }
  5787. public void addOverrideCond(PcCondOverride... excs)
  5788. {
  5789. for (PcCondOverride exc : excs)
  5790. {
  5791. _exceptions |= exc.getMask();
  5792. }
  5793. }
  5794. public void removeOverridedCond(PcCondOverride... excs)
  5795. {
  5796. for (PcCondOverride exc : excs)
  5797. {
  5798. _exceptions &= ~exc.getMask();
  5799. }
  5800. }
  5801. public boolean canOverrideCond(PcCondOverride excs)
  5802. {
  5803. return (_exceptions & excs.getMask()) == excs.getMask();
  5804. }
  5805. public void setOverrideCond(long masks)
  5806. {
  5807. _exceptions = masks;
  5808. }
  5809. public void setLethalable(boolean val)
  5810. {
  5811. _lethalable = val;
  5812. }
  5813. public boolean isLethalable()
  5814. {
  5815. return _lethalable;
  5816. }
  5817. public Map<Integer, OptionsSkillHolder> getTriggerSkills()
  5818. {
  5819. if (_triggerSkills == null)
  5820. {
  5821. synchronized (this)
  5822. {
  5823. if (_triggerSkills == null)
  5824. {
  5825. _triggerSkills = new ConcurrentHashMap<>();
  5826. }
  5827. }
  5828. }
  5829. return _triggerSkills;
  5830. }
  5831. public void addTriggerSkill(OptionsSkillHolder holder)
  5832. {
  5833. getTriggerSkills().put(holder.getSkillId(), holder);
  5834. }
  5835. public void removeTriggerSkill(OptionsSkillHolder holder)
  5836. {
  5837. getTriggerSkills().remove(holder.getSkillId());
  5838. }
  5839. public void makeTriggerCast(Skill skill, L2Character target, boolean ignoreTargetType)
  5840. {
  5841. try
  5842. {
  5843. if ((skill == null))
  5844. {
  5845. return;
  5846. }
  5847. if (skill.checkCondition(this, target, false))
  5848. {
  5849. if (isSkillDisabled(skill))
  5850. {
  5851. return;
  5852. }
  5853. if (skill.getReuseDelay() > 0)
  5854. {
  5855. disableSkill(skill, skill.getReuseDelay());
  5856. }
  5857. // @formatter:off
  5858. final L2Object[] targets = !ignoreTargetType ? skill.getTargetList(this, false, target) : new L2Character[]{ target };
  5859. // @formatter:on
  5860. if (targets.length == 0)
  5861. {
  5862. return;
  5863. }
  5864. for (L2Object obj : targets)
  5865. {
  5866. if ((obj != null) && obj.isCharacter())
  5867. {
  5868. target = (L2Character) obj;
  5869. break;
  5870. }
  5871. }
  5872. if (Config.ALT_VALIDATE_TRIGGER_SKILLS && isPlayable() && (target != null) && target.isPlayable())
  5873. {
  5874. final L2PcInstance player = getActingPlayer();
  5875. if (!player.checkPvpSkill(target, skill))
  5876. {
  5877. return;
  5878. }
  5879. }
  5880. broadcastPacket(new MagicSkillUse(this, target, skill.getDisplayId(), skill.getLevel(), 0, 0));
  5881. broadcastPacket(new MagicSkillLaunched(this, skill.getDisplayId(), skill.getLevel(), targets));
  5882. // Launch the magic skill and calculate its effects
  5883. skill.activateSkill(this, targets);
  5884. }
  5885. }
  5886. catch (Exception e)
  5887. {
  5888. _log.log(Level.WARNING, "", e);
  5889. }
  5890. }
  5891. public void makeTriggerCast(Skill skill, L2Character target)
  5892. {
  5893. makeTriggerCast(skill, target, false);
  5894. }
  5895. /**
  5896. * Dummy method overriden in {@link L2PcInstance}
  5897. * @return {@code true} if current player can revive and shows 'To Village' button upon death, {@code false} otherwise.
  5898. */
  5899. public boolean canRevive()
  5900. {
  5901. return true;
  5902. }
  5903. /**
  5904. * Dummy method overriden in {@link L2PcInstance}
  5905. * @param val
  5906. */
  5907. public void setCanRevive(boolean val)
  5908. {
  5909. }
  5910. /**
  5911. * Dummy method overriden in {@link L2Attackable}
  5912. * @return {@code true} if there is a loot to sweep, {@code false} otherwise.
  5913. */
  5914. public boolean isSweepActive()
  5915. {
  5916. return false;
  5917. }
  5918. /**
  5919. * Dummy method overriden in {@link L2PcInstance}
  5920. * @return {@code true} if player is on event, {@code false} otherwise.
  5921. */
  5922. public boolean isOnEvent()
  5923. {
  5924. return false;
  5925. }
  5926. /**
  5927. * Dummy method overriden in {@link L2PcInstance}
  5928. * @return the clan id of current character.
  5929. */
  5930. public int getClanId()
  5931. {
  5932. return 0;
  5933. }
  5934. /**
  5935. * Dummy method overriden in {@link L2PcInstance}
  5936. * @return the clan of current character.
  5937. */
  5938. public L2Clan getClan()
  5939. {
  5940. return null;
  5941. }
  5942. /**
  5943. * Dummy method overriden in {@link L2PcInstance}
  5944. * @return {@code true} if player is in academy, {@code false} otherwise.
  5945. */
  5946. public boolean isAcademyMember()
  5947. {
  5948. return false;
  5949. }
  5950. /**
  5951. * Dummy method overriden in {@link L2PcInstance}
  5952. * @return the pledge type of current character.
  5953. */
  5954. public int getPledgeType()
  5955. {
  5956. return 0;
  5957. }
  5958. /**
  5959. * Dummy method overriden in {@link L2PcInstance}
  5960. * @return the alliance id of current character.
  5961. */
  5962. public int getAllyId()
  5963. {
  5964. return 0;
  5965. }
  5966. /**
  5967. * Notifies to listeners that current character received damage.
  5968. * @param damage
  5969. * @param attacker
  5970. * @param skill
  5971. * @param critical
  5972. * @param damageOverTime
  5973. */
  5974. public void notifyDamageReceived(double damage, L2Character attacker, Skill skill, boolean critical, boolean damageOverTime)
  5975. {
  5976. EventDispatcher.getInstance().notifyEventAsync(new OnCreatureDamageReceived(attacker, this, damage, skill, critical, damageOverTime), this);
  5977. EventDispatcher.getInstance().notifyEventAsync(new OnCreatureDamageDealt(attacker, this, damage, skill, critical, damageOverTime), attacker);
  5978. }
  5979. /**
  5980. * Notifies to listeners that current character avoid attack.
  5981. * @param target
  5982. * @param isDot
  5983. */
  5984. public void notifyAttackAvoid(final L2Character target, final boolean isDot)
  5985. {
  5986. EventDispatcher.getInstance().notifyEventAsync(new OnCreatureAttackAvoid(this, target, isDot), target);
  5987. }
  5988. /**
  5989. * @return {@link WeaponType} of current character's weapon or basic weapon type.
  5990. */
  5991. public final WeaponType getAttackType()
  5992. {
  5993. final L2Weapon weapon = getActiveWeaponItem();
  5994. if (weapon != null)
  5995. {
  5996. return weapon.getItemType();
  5997. }
  5998. else if (isTransformed())
  5999. {
  6000. final TransformTemplate template = getTransformation().getTemplate(getActingPlayer());
  6001. if (template != null)
  6002. {
  6003. return template.getBaseAttackType();
  6004. }
  6005. }
  6006. return getTemplate().getBaseAttackType();
  6007. }
  6008. public final boolean isInCategory(CategoryType type)
  6009. {
  6010. return CategoryData.getInstance().isInCategory(type, getId());
  6011. }
  6012. /**
  6013. * @return the character that summoned this NPC.
  6014. */
  6015. public L2Character getSummoner()
  6016. {
  6017. return _summoner;
  6018. }
  6019. /**
  6020. * @param summoner the summoner of this NPC.
  6021. */
  6022. public void setSummoner(L2Character summoner)
  6023. {
  6024. _summoner = summoner;
  6025. }
  6026. @Override
  6027. public boolean isCharacter()
  6028. {
  6029. return true;
  6030. }
  6031. /**
  6032. * @return {@code true} if current character is casting channeling skill, {@code false} otherwise.
  6033. */
  6034. public final boolean isChanneling()
  6035. {
  6036. return (_channelizer != null) && _channelizer.isChanneling();
  6037. }
  6038. public final SkillChannelizer getSkillChannelizer()
  6039. {
  6040. if (_channelizer == null)
  6041. {
  6042. _channelizer = new SkillChannelizer(this);
  6043. }
  6044. return _channelizer;
  6045. }
  6046. /**
  6047. * @return {@code true} if current character is affected by channeling skill, {@code false} otherwise.
  6048. */
  6049. public final boolean isChannelized()
  6050. {
  6051. return (_channelized != null) && !_channelized.isChannelized();
  6052. }
  6053. public final SkillChannelized getSkillChannelized()
  6054. {
  6055. if (_channelized == null)
  6056. {
  6057. _channelized = new SkillChannelized();
  6058. }
  6059. return _channelized;
  6060. }
  6061. public void addInvulAgainst(SkillHolder holder)
  6062. {
  6063. final InvulSkillHolder invulHolder = getInvulAgainstSkills().get(holder.getSkillId());
  6064. if (invulHolder != null)
  6065. {
  6066. invulHolder.increaseInstances();
  6067. return;
  6068. }
  6069. getInvulAgainstSkills().put(holder.getSkillId(), new InvulSkillHolder(holder));
  6070. }
  6071. public void removeInvulAgainst(SkillHolder holder)
  6072. {
  6073. final InvulSkillHolder invulHolder = getInvulAgainstSkills().get(holder.getSkillId());
  6074. if ((invulHolder != null) && (invulHolder.decreaseInstances() < 1))
  6075. {
  6076. getInvulAgainstSkills().remove(holder.getSkillId());
  6077. }
  6078. }
  6079. public boolean isInvulAgainst(int skillId, int skillLvl)
  6080. {
  6081. if (_invulAgainst != null)
  6082. {
  6083. final SkillHolder holder = getInvulAgainstSkills().get(skillId);
  6084. return ((holder != null) && ((holder.getSkillLvl() < 1) || (holder.getSkillLvl() == skillLvl)));
  6085. }
  6086. return false;
  6087. }
  6088. private Map<Integer, InvulSkillHolder> getInvulAgainstSkills()
  6089. {
  6090. if (_invulAgainst == null)
  6091. {
  6092. synchronized (this)
  6093. {
  6094. if (_invulAgainst == null)
  6095. {
  6096. _invulAgainst = new ConcurrentHashMap<>();
  6097. }
  6098. }
  6099. }
  6100. return _invulAgainst;
  6101. }
  6102. @Override
  6103. public Queue<AbstractEventListener> getListeners(EventType type)
  6104. {
  6105. final Queue<AbstractEventListener> objectListenres = super.getListeners(type);
  6106. final Queue<AbstractEventListener> templateListeners = getTemplate().getListeners(type);
  6107. final Queue<AbstractEventListener> globalListeners = isNpc() && !isMonster() ? Containers.Npcs().getListeners(type) : isMonster() ? Containers.Monsters().getListeners(type) : isPlayer() ? Containers.Players().getListeners(type) : EmptyQueue.emptyQueue();
  6108. // Attempt to do not create collection
  6109. if (objectListenres.isEmpty() && templateListeners.isEmpty() && globalListeners.isEmpty())
  6110. {
  6111. return EmptyQueue.emptyQueue();
  6112. }
  6113. else if (!objectListenres.isEmpty() && templateListeners.isEmpty() && globalListeners.isEmpty())
  6114. {
  6115. return objectListenres;
  6116. }
  6117. else if (!templateListeners.isEmpty() && objectListenres.isEmpty() && globalListeners.isEmpty())
  6118. {
  6119. return templateListeners;
  6120. }
  6121. else if (!globalListeners.isEmpty() && objectListenres.isEmpty() && templateListeners.isEmpty())
  6122. {
  6123. return globalListeners;
  6124. }
  6125. final Queue<AbstractEventListener> both = new LinkedBlockingDeque<>(objectListenres.size() + templateListeners.size() + globalListeners.size());
  6126. both.addAll(objectListenres);
  6127. both.addAll(templateListeners);
  6128. both.addAll(globalListeners);
  6129. return both;
  6130. }
  6131. public Race getRace()
  6132. {
  6133. return getTemplate().getRace();
  6134. }
  6135. public boolean isInDuel()
  6136. {
  6137. return false;
  6138. }
  6139. public int getDuelId()
  6140. {
  6141. return 0;
  6142. }
  6143. public byte getSiegeState()
  6144. {
  6145. return 0;
  6146. }
  6147. public int getSiegeSide()
  6148. {
  6149. return 0;
  6150. }
  6151. }